图像特效之老照片
关键代码:
int OldPicture(IMAGE_TYPE *BMP24_img)
{
DWORD width,height,i,j,bfsize;
WORD biBitCount;
T_U8 *dst,*bmp,*img_data;
int pix,R,G,B,PixR,PixG,PixB;
T_U32 line_byte,Imgindex;
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
FILE *OldPicture_fp = fopen("OldPicture.bmp","wb");
if(NULL == OldPicture_fp)
{
printf("Can't open Comic.bmp\n");
return -1;
}
memset(&bf, 0, sizeof(bf));
memset(&bi, 0, sizeof(bi));
bmp = BMP24_img;
memcpy(&bf,bmp,14);
memcpy(&bi,&bmp[14],40);
fwrite(&bf,sizeof(BITMAPFILEHEADER),1,OldPicture_fp);
fwrite(&bi,sizeof(BITMAPINFOHEADER),1,OldPicture_fp);
height = bi.biHeight;
width = bi.biWidth;
bfsize = bf.bfSize;
biBitCount = bi.biBitCount;
line_byte = WIDTHBYTES(width*bi.biBitCount);
img_data = bmp+54;
dst = (T_U8 *)malloc(line_byte*height);
memset(dst,0,line_byte*height);
memcpy(dst,img_data,line_byte*height);
for (i = 0;i < height;i++)
{
for (j = 0; j < width;j++)
{
Imgindex = i*line_byte+3*j;
R = img_data[Imgindex+2];
G = img_data[Imgindex+1];
B = img_data[Imgindex];
PixR = (25756 * R + 50397 * G + 12386 * B) >> 16;
PixG = (22872 * R + 44958 * G + 11010 * B) >> 16;
PixB = (17826 * R + 34996 * G + 8585 * B) >> 16;
if (PixR < 0) PixR = 0;
if (PixR > 255) PixR = 255;
if (PixG < 0) PixG = 0;
if (PixG > 255) PixG = 255;
if (PixB < 0) PixB = 0;
if (PixB > 255) PixB = 255;
dst[Imgindex] = PixB;
dst[Imgindex+1] = PixG;
dst[Imgindex+2] = PixR;
}
}
fwrite(dst,line_byte*height,1,OldPicture_fp);
fclose(OldPicture_fp);
OldPicture_fp = NULL;
free(dst);
return 0;
}
图像效果: