项目篇:第三篇_LSB随机间隔数据隐藏
时间:10-02
整理:3721RD
点击:
注:本帖不对CCS软件安装、DSP工程建立及相关配置介绍。
上两篇中,我们介绍了LSB隐藏,然而顺序隐藏,信息极易抹去,故有采取随机间隔的方法对信息不在那么有序,首先得有随机间隔函数:
- //row为随机间隔像素行标
- //col为随机间隔像素列标
- //mrow为图像像素高度
- //mcol为像素宽度
- //count为信息数大小
- //key为rand函数的种子
- void randinterval(int *row, int *col, const int mrow, const int mcol, const int count, const int key)
- {
- double *a = malloc(count * sizeof(double));
- int r = 1, c = 1, i;
- int interval1, interval2;
- interval1 = mrow * mcol / count + 1;
- interval2 = interval1 - 2;
- if(interval2 == 0)
- {
- perror("载体太小不能将秘密信息隐藏进去!");
- exit(EXIT_FAILURE);
- }
- srand(key);
- for(i = 0; i 0.5)
- c = c + interval1;
- else
- c = c + interval2;
- if(c > mcol)
- {
- r = r + c / mcol;
- if(r > mrow)
- {
- perror("载体太小不能将秘密信息隐藏进去!");
- exit(EXIT_FAILURE);
- }
- c = c % mcol;
- if(c == 0)
- c = 1;
- }
- row[i] = r;
- col[i] = c;
- }
- free(a);
- }
有了以上函数,仅需要对之前的函数稍加修改即可:
- bmp LsbHide(bmp *m, const int *row, const int *col)
- {
- int i, j;
- bmp newm;
- int bmpWidth; // 图像的宽
- int bmpHeight; // 图像的高
- int biBitCount; // 图像类型,每像素位数
- int lineByte;
- int newBmpWidth; // 新图像的宽
- int newBmpHeight; // 新图像的高
- int newLineByte;
- int temp;
- // 获取图像宽、高、每像素所占位数等信息
- bmpWidth = m->info.biWidth;
- bmpHeight = m->info.biHeight;
- biBitCount = m->info.biBitcount;
- // 定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
- lineByte=(bmpWidth * biBitCount / 8 + 3) / 4 * 4;
- newBmpWidth = bmpWidth;
- newBmpHeight = bmpHeight;
- newLineByte = (newBmpWidth * biBitCount / 8 + 3) / 4 * 4;
- // 申请位图数据区
- newBmpBuf = (unsigned char *)malloc(newLineByte * newBmpHeight);
- int t = 0;
- for(i = 0; i imgBuf + i * lineByte + j));
- *(unsigned char *)(newBmpBuf + i*newLineByte + j) = temp;
- }
- }
- for(i = 0; i pColorTable), 256 * 4);
- newm.pColorTable = (RGBQUAD*)newpColorTable;
- //写入位图数据
- newm.imgBuf = newBmpBuf;
- return newm;
- }
- bmp Compare(bmp *input, bmp *output)
- {
- int i, j;
- bmp newm;
- int bmpWidth; // 图像的宽
- int bmpHeight; // 图像的高
- int biBitCount; // 图像类型,每像素位数
- int lineByte;
- int newBmpWidth; // 新图像的宽
- int newBmpHeight; // 新图像的高
- int newLineByte;
- int tempi, tempo;
- // 获取图像宽、高、每像素所占位数等信息
- bmpWidth = input->info.biWidth;
- bmpHeight = input->info.biHeight;
- biBitCount = input->info.biBitcount;
- // 定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
- lineByte=(bmpWidth * biBitCount / 8 + 3) / 4 * 4;
- newBmpWidth = bmpWidth;
- newBmpHeight = bmpHeight;
- newLineByte = (newBmpWidth * biBitCount / 8 + 3) / 4 * 4;
- // 申请位图数据区
- newBmpBuf = (unsigned char *)malloc(newLineByte * newBmpHeight);
- for(i = 0; i imgBuf + i * lineByte + j));
- tempo = (*(unsigned char *)(output->imgBuf + i * lineByte + j));
- *(unsigned char *)(newBmpBuf + i*newLineByte + j) = tempo - tempi;
- }
- }
- newm.file.bfType = 0x4d42; // 类型
- newm.file.bfSize = 54 + 246 *4 +
- newLineByte * newBmpHeight; // 文件长度
- newm.file.bfReserverd1 = 0; // 保留字 1
- newm.file.bfReserverd2 = 0; // 保留字 2
- newm.file.bfbfOffBits = 54 + 256 * 4; // 偏移量
- newm.info.biSize = 40; // 此结构大小
- newm.info.biWidth = newBmpWidth; // 位图的宽度
- newm.info.biHeight = newBmpHeight; // 位图的高度
- newm.info.biPlanes = 1; // 目标设备位图数
- newm.info.biBitcount = 8; // 颜色深度
- newm.info.biCompression = 0; // 位图压缩类型
- newm.info.biSizeImage = newLineByte * newBmpHeight; // 位图大小
- newm.info.biXPelsPermeter = 0; // 位图水平分辨率
- newm.info.biYPelsPermeter = 0; // 位图垂直分辨率
- newm.info.biClrUsed = 0; // 位图实际使用颜色数
- newm.info.biClrImportant = 0; // 位图显示中比较重要颜色数
- // 写入调色板
- memcpy((void *)(newpColorTable), (void *)(input->pColorTable), 256 * 4);
- newm.pColorTable = (RGBQUAD*)newpColorTable;
- //写入位图数据
- newm.imgBuf = newBmpBuf;
- return newm;
- }
- void LsbGet(bmp *m, int *goaltext, const int *row, const int *col)
- {
- int i, j;
- int bmpWidth; // 图像的宽
- int bmpHeight; // 图像的高
- int biBitCount; // 图像类型,每像素位数
- int lineByte;
- int temp;
- // 获取图像宽、高、每像素所占位数等信息
- bmpWidth = m->info.biWidth;
- bmpHeight = m->info.biHeight;
- biBitCount = m->info.biBitcount;
- // 定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
- lineByte=(bmpWidth * biBitCount / 8 + 3) / 4 * 4;
- int t = 0;
- int p = 112;
- for(i = 0; i imgBuf + (row[i]) * lineByte + col[j]));
- int maxlen = i*bmpWidth +j;
- if(p>= maxlen )
- {
- goaltext[t] = temp & 1;
- t ++;
- }
- else
- return;
- }
- }
- }
运行结果:
![](../imgqa/ele/DSP/DSP-3721rd.com-29171x3nuunshbcs.png)
![](../imgqa/ele/DSP/DSP-3721rd.com-29172bm33piemj3a.png)
![](../imgqa/ele/DSP/DSP-3721rd.com-29173boeknpszepp.png)