前几天从网上得到一段程序,我把它改造了一下,先做一个可以对简单图像中的某些颜色进行去除和替换,自娱自乐了一把。以下是代码和处理图片。
程序可以在linux下和windows下通用,不过好像只能处理微软的bmp图片,ibm的bmp图片无法处理。
#include <stdlib.h>
#include <stdio.h>
typedef unsigned char BYTE;
typedef unsigned short uint16;
typedef unsigned long DWORD;
int main(int argc,char* argv[])
{
FILE *infile;
FILE *outfile;
BYTE Head[54];
BYTE *Buff;
DWORD ImgWidth,ImgHeight,i,j,p;
BYTE bt0,bt1,bt2;
BYTE stuff[4];
int mt=1,pt0=200,pt1=100,pt2=100;
if(argc<1)
{
printf("requit files\n",argv[0]);
return -1;
}
infile=fopen("004.bmp","rb+");
ōutfile=fopen("0055.bmp","wb+");
if(infile==NULL)
{
printf("Open 24 bit bitmap file failed!");
return -2;
}
fread(Head,54,1,infile);
fwrite((BYTE *)Head,1,54,outfile);
for(j=0;j<54;j++)
printf("%d %02x ",j,Head[j]);
ImgWidth =*(Head+18)+(*(Head+19))*256;
printf("18 %02x 19 %02x ",*(Head+18),*(Head+19));
ImgHeight=*(Head+22)+(*(Head+23))*256;
printf("22 %02x 23 %02x ",*(Head+22),*(Head+23));
p=(ImgWidth*3)%4;
printf("Image width: %d height: %d p: %d \n",ImgWidth,ImgHeight,p);
Buff = (BYTE *)malloc(ImgWidth*ImgHeight*3);
if(!Buff)
{
printf("Malloc memory failed!\n");
return -3;
}
for(i=0;i<ImgHeight;i++) // read bitmap pixels array to buffer
{
for(j=0;j<ImgWidth;j++)
{
fread((BYTE *)&bt0,1,1,infile);
fread((BYTE *)&bt1,1,1,infile);
fread((BYTE *)&bt2,1,1,infile);
*(Buff+i*ImgWidth*3+j*3+0)= bt0;
*(Buff+i*ImgWidth*3+j*3+1)= bt1;
*(Buff+i*ImgWidth*3+j*3+2)= bt2;
if((bt0<(0xff-pt0))&&(bt1>(0xff-pt1))&&(bt2>(0xff-pt2)))
{
bt0=0xff;
bt1=0xff;
bt2=0xff;
}
fwrite((BYTE *)&bt0,1,1,outfile);
fwrite((BYTE *)&bt1,1,1,outfile);
fwrite((BYTE *)&bt2,1,1,outfile);
}
if(p!=0) fread(stuff,1,(4-p),infile);
}
fclose(infile);
return 0;
}
不过由于这里上传不了bmp的图片,我就把它们转换成了jpg了。
原图:
经程序处理后的图片:

我还采用了其他的参数处理了几张不同效果的图片。但是图片是别人的不好在这里贴出来。另外贴上我处理的一张图片。
原图:

处理后:
附件: