注:本帖不对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 < count; i++)
- {
- a[i] = rand() / 32768.0;
- row[i] = 0;
- col[i] = 0;
- }
- row[1] = r;
- col[1] = c;
- for(i = 1; i < count; i ++)
- {
- if(a[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 L***Hide(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 < newBmpHeight; i++)
- {
- for(j = 0; j < newBmpWidth; j++)
- {
- temp = (*(unsigned char *)(m->imgBuf + i * lineByte + j));
- *(unsigned char *)(newBmpBuf + i*newLineByte + j) = temp;
- }
- }
- for(i = 0; i < 1; i++)
- {
- for(j = 0; j < 112; j++)
- {
- temp = (*(unsigned char *)(newBmpBuf + (row[i]) * newLineByte + col[j]));
- *(unsigned char *)(newBmpBuf + (row[i])*newLineByte + col[j]) = temp - temp %2 +text[t];
- t ++;
- }
- }
- 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 *)(m->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 < newBmpHeight; i++)
- {
- for(j = 0; j < newBmpWidth; j++)
- {
- tempi = (*(unsigned char *)(input->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 L***Get(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 < bmpHeight; i++)
- {
- for(j = 0; j < bmpWidth; j++)
- {
- temp = (*(unsigned char *)(m->imgBuf + (row[i]) * lineByte + col[j]));
- int maxlen = i*bmpWidth +j;
- if(p>= maxlen )
- {
- goaltext[t] = temp & 1;
- t ++;
- }
- else
- return;
- }
- }
- }
复制代码
运行结果: |