图片隐藏,生成的图片只有下半张,求问哪里出错了
/*将一张图片隐藏到另一张图片中
载体图为灰度图
blog:http://
2015年6月3日17:59:34
*/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
/*
位图头结构
*/
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER
{
unsigned char bfType[2];//文件格式
unsigned long bfSize;//文件大小
unsigned short bfReserved1;//保留
unsigned short bfReserved2;
unsigned long bfOffBits; //DIB数据在文件中的偏移量
}fileHeader;
#pragma pack()
/*
位图数据信息结构
*/
#pragma pack(1)
typedef struct tagBITMAPINFOHEADER
{
unsigned long biSize;//该结构的大小
long biWidth;//文件宽度
long biHeight;//文件高度
unsigned short biPlanes;//平面数
unsigned short biBitCount;//颜色位数
unsigned long biCompression;//压缩类型
unsigned long biSizeImage;//DIB数据区大小
long biXPixPerMeter;
long biYPixPerMeter;
unsigned long biClrUsed;//多少颜色索引表
unsigned long biClrImporant;//多少重要颜色
}fileInfo;
#pragma pack()
/*
调色板结构
*/
#pragma pack(1)
typedef struct tagRGBQUAD
{
unsigned char rgbBlue; //蓝色分量亮度
unsigned char rgbGreen;//绿色分量亮度
unsigned char rgbRed;//红色分量亮度
unsigned char rgbReserved;
}rgbq;
#pragma pack()
int main()
{
FILE * fpCov, *fpSec, *fpSte;
int i, j;
unsigned char ImgData[300][300];
unsigned char ImgData2[300][300];
fileHeader * fh, *fh2;
fileInfo * fi, *fi2;
rgbq * fq, *fq2;
if ((fpCov = fopen("D:/yincang/1.bmp", "rb")) == NULL)
{
printf("打开文件失败");
exit(0);
}
if ((fpSec = fopen("D:/yincang/2.bmp", "rb")) == NULL)
{
printf("打开文件失败");
exit(0);
}
if ((fpSte = fopen("D:/yincang/123.bmp", "wb")) == NULL)
{
printf("创建文件失败");
exit(0);
}
/*
读取载体图片
*/
fh = (fileHeader *)malloc(sizeof(fileHeader));
fi = (fileInfo *)malloc(sizeof(fileInfo));
fq = (rgbq *)malloc(sizeof(rgbq) * 256);
fread(fh, sizeof(fileHeader), 1, fpCov);
fread(fi, sizeof(fileInfo), 1, fpCov);
fread(fq, sizeof(rgbq), 256, fpCov);
for (i = 0; i<fi->biHeight; i++)
{
for (j = 0; j<(fi->biWidth + 3) / 4 * 4; j++)
fread(&ImgData[i][j], 1, 1, fpCov);
}
/*读取秘密图片*/
fh2 = (fileHeader *)malloc(sizeof(fileHeader));
fi2 = (fileInfo *)malloc(sizeof(fileInfo));
fq2 = (rgbq *)malloc(sizeof(rgbq) * 256);
fread(fh2, sizeof(fileHeader), 1, fpSec);
fread(fi2, sizeof(fileInfo), 1, fpSec);
fread(fq2, sizeof(rgbq), 256, fpSec);
for (i = 0; i<fi->biHeight; i++)
{
for (j = 0; j<(fi->biWidth + 3) / 4 * 4; j++)
fread(&ImgData2[i][j], 1, 1, fpSec);
}
/*隐藏图片*/
for (i = 0; i<fi->biHeight; i++)
{
for (j = 0; j<(fi->biWidth + 3) / 4 * 4; j++)
{
/*将载体图片低四位置零*/
ImgData[i][j] = ImgData[i][j] & 240;
/*将秘密图片低四位置零*/
ImgData2[i][j] = ImgData2[i][j] & 240;
/*将秘密图片高四位移到低四位*/
ImgData2[i][j] = ImgData2[i][j] >> 4;
/*将秘密图片隐藏到载体图片中*/
ImgData[i][j] = ImgData[i][j] + ImgData2[i][j];
}
}
/*将图片数据写入*/
fwrite(fh, sizeof(fileHeader), 1, fpSte);
fwrite(fi, sizeof(fileInfo), 1, fpSte);
fwrite(fq, sizeof(rgbq), 256, fpSte);
for (i = 0; i<fi->biHeight;i++)
{
for (j = 0; j<(fi->biWidth + 3) / 4 * 4; j++)
fwrite(&ImgData[i][j], 1, 1, fpSte);
}
free(fh);
free(fi);
free(fq);
fclose(fpCov);
fclose(fpSec);
fclose(fpSte);
printf("success\n");
return 0;
}
[此贴子已经被作者于2017-12-19 22:55编辑过]