微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > DSP学习交流 > 项目篇:第三篇_LSB随机间隔数据隐藏

项目篇:第三篇_LSB随机间隔数据隐藏

时间:10-02 整理:3721RD 点击:

注:本帖不对CCS软件安装、DSP工程建立及相关配置介绍。

    上两篇中,我们介绍了LSB隐藏,然而顺序隐藏,信息极易抹去,故有采取随机间隔的方法对信息不在那么有序,首先得有随机间隔函数:

  1. //row为随机间隔像素行标
  2. //col为随机间隔像素列标
  3. //mrow为图像像素高度
  4. //mcol为像素宽度
  5. //count为信息数大小
  6. //key为rand函数的种子
  7. void randinterval(int *row, int *col, const int mrow, const int mcol, const int count, const int key)
  8. {
  9.         double *a = malloc(count * sizeof(double));
  10.         int r = 1, c = 1, i;
  11.         int interval1, interval2;
  12.         interval1 = mrow * mcol / count + 1;
  13.         interval2 = interval1 - 2;
  14.         if(interval2 == 0)
  15.         {
  16.             perror("载体太小不能将秘密信息隐藏进去!");
  17.                 exit(EXIT_FAILURE);
  18.         }
  19.         srand(key);
  20.         for(i = 0; i 0.5)
  21.                 c = c + interval1;
  22.             else
  23.                 c = c + interval2;
  24.             if(c > mcol)
  25.             {
  26.                 r = r + c / mcol;
  27.                 if(r > mrow)
  28.                 {
  29.                     perror("载体太小不能将秘密信息隐藏进去!");
  30.                     exit(EXIT_FAILURE);
  31.                 }
  32.                 c = c % mcol;
  33.                 if(c == 0)
  34.                     c = 1;
  35.             }
  36.             row[i] = r;
  37.             col[i] = c;
  38.         }
  39.         free(a);
  40. }

复制代码

有了以上函数,仅需要对之前的函数稍加修改即可:

  1. bmp LsbHide(bmp *m, const int *row, const int *col)
  2. {
  3.         int i, j;
  4.         bmp newm;
  5.         int bmpWidth;                // 图像的宽
  6.         int bmpHeight;                // 图像的高
  7.         int biBitCount;                // 图像类型,每像素位数
  8.         int lineByte;
  9.         int newBmpWidth;         // 新图像的宽
  10.         int newBmpHeight;         // 新图像的高
  11.         int newLineByte;
  12.         int temp;

  13.         // 获取图像宽、高、每像素所占位数等信息
  14.         bmpWidth = m->info.biWidth;
  15.         bmpHeight = m->info.biHeight;
  16.         biBitCount = m->info.biBitcount;

  17.         // 定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
  18.         lineByte=(bmpWidth * biBitCount / 8 + 3) / 4 * 4;

  19.         newBmpWidth = bmpWidth;
  20.         newBmpHeight = bmpHeight;
  21.         newLineByte = (newBmpWidth * biBitCount / 8 + 3) / 4 * 4;
  22.         // 申请位图数据区
  23.         newBmpBuf = (unsigned char *)malloc(newLineByte * newBmpHeight);

  24.         int t = 0;

  25.         for(i = 0; i imgBuf + i * lineByte + j));
  26.                         *(unsigned char *)(newBmpBuf + i*newLineByte + j) = temp;
  27.                 }
  28.         }

  29.         for(i = 0; i pColorTable), 256 * 4);
  30.         newm.pColorTable = (RGBQUAD*)newpColorTable;

  31.         //写入位图数据
  32.         newm.imgBuf = newBmpBuf;

  33.         return newm;
  34. }

  35. bmp Compare(bmp *input, bmp *output)
  36. {
  37.         int i, j;
  38.         bmp newm;
  39.         int bmpWidth;                // 图像的宽
  40.         int bmpHeight;                // 图像的高
  41.         int biBitCount;                // 图像类型,每像素位数
  42.         int lineByte;
  43.         int newBmpWidth;         // 新图像的宽
  44.         int newBmpHeight;         // 新图像的高
  45.         int newLineByte;
  46.         int tempi, tempo;

  47.         // 获取图像宽、高、每像素所占位数等信息
  48.         bmpWidth = input->info.biWidth;
  49.         bmpHeight = input->info.biHeight;
  50.         biBitCount = input->info.biBitcount;

  51.         // 定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
  52.         lineByte=(bmpWidth * biBitCount / 8 + 3) / 4 * 4;

  53.         newBmpWidth = bmpWidth;
  54.         newBmpHeight = bmpHeight;
  55.         newLineByte = (newBmpWidth * biBitCount / 8 + 3) / 4 * 4;
  56.         // 申请位图数据区
  57.         newBmpBuf = (unsigned char *)malloc(newLineByte * newBmpHeight);

  58.         for(i = 0; i imgBuf + i * lineByte + j));
  59.                         tempo = (*(unsigned char *)(output->imgBuf + i * lineByte + j));
  60.                         *(unsigned char *)(newBmpBuf + i*newLineByte + j) = tempo - tempi;
  61.                 }
  62.         }

  63.         newm.file.bfType = 0x4d42;                                                        // 类型
  64.         newm.file.bfSize = 54 + 246 *4 +
  65.                                                 newLineByte * newBmpHeight;                // 文件长度
  66.         newm.file.bfReserverd1 = 0;                                                        // 保留字 1
  67.         newm.file.bfReserverd2 = 0;                                                        // 保留字 2
  68.         newm.file.bfbfOffBits = 54 + 256 * 4;                                // 偏移量
  69.         newm.info.biSize = 40;                                                                // 此结构大小
  70.         newm.info.biWidth = newBmpWidth;                                        // 位图的宽度
  71.         newm.info.biHeight = newBmpHeight;                                        // 位图的高度
  72.         newm.info.biPlanes = 1;                                                                // 目标设备位图数
  73.         newm.info.biBitcount = 8;                                                        // 颜色深度
  74.         newm.info.biCompression = 0;                                                // 位图压缩类型
  75.         newm.info.biSizeImage = newLineByte * newBmpHeight;        // 位图大小
  76.         newm.info.biXPelsPermeter = 0;                      // 位图水平分辨率
  77.         newm.info.biYPelsPermeter = 0;                      // 位图垂直分辨率
  78.         newm.info.biClrUsed = 0;                            // 位图实际使用颜色数
  79.         newm.info.biClrImportant = 0;                       // 位图显示中比较重要颜色数

  80.         // 写入调色板
  81.         memcpy((void *)(newpColorTable), (void *)(input->pColorTable), 256 * 4);
  82.         newm.pColorTable = (RGBQUAD*)newpColorTable;

  83.         //写入位图数据
  84.         newm.imgBuf = newBmpBuf;

  85.         return newm;
  86. }

  87. void LsbGet(bmp *m, int *goaltext, const int *row, const int *col)
  88. {
  89.         int i, j;
  90.         int bmpWidth;                // 图像的宽
  91.         int bmpHeight;                // 图像的高
  92.         int biBitCount;                // 图像类型,每像素位数
  93.         int lineByte;
  94.         int temp;

  95.         // 获取图像宽、高、每像素所占位数等信息
  96.         bmpWidth = m->info.biWidth;
  97.         bmpHeight = m->info.biHeight;
  98.         biBitCount = m->info.biBitcount;

  99.         // 定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
  100.         lineByte=(bmpWidth * biBitCount / 8 + 3) / 4 * 4;


  101.         int t = 0;
  102.         int p = 112;
  103.         for(i = 0; i imgBuf + (row[i]) * lineByte + col[j]));
  104.                     int maxlen = i*bmpWidth +j;
  105.                         if(p>= maxlen )
  106.                         {
  107.                                 goaltext[t] = temp & 1;
  108.                                 t ++;
  109.                         }
  110.                         else
  111.                                 return;
  112.                 }
  113.         }
  114. }

复制代码

运行结果:






Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top