在c语言中访问设定路径下的bmp图片文件,修改图片中像素RGB信息,从而达到修改图片中颜色的目的。
本程序实现的是将原图片中蜡笔小新的眼睛和嘴巴改了。
#include <head.h>
typedef struct
{
unsigned char B;
unsigned char G;
unsigned char R;
} rgb_t;
int main(int argc, const char *argv[])
{
rgb_t rgb;
FILE *fp;
int i = 0;
int j = 0;
if (argc != 2)
{
fprintf(stderr, "input error\n");
fprintf(stderr, "usage:./a.out picturename\n");
}
if ((fp = fopen(argv[1], "r+")) == NULL)
PRINT_ERR("fopen error");
fseek(fp, (712 - 190) * 700 * 3, SEEK_SET);
while (i < 50)
{
fseek(fp, 290 * 3, SEEK_CUR);
while (j < 50)
{
rgb.B = 255;
rgb.G = 0;
rgb.R = 0;
fwrite(&rgb, 3, 1, fp);
j++;
}
fseek(fp, 75 * 3, SEEK_CUR);
j = 0;
while (j < 50)
{
rgb.B = 255;
rgb.G = 0;
rgb.R = 0;
fwrite(&rgb, 3, 1, fp);
j++;
}
fseek(fp, 235 * 3, SEEK_CUR);
j = 0;
i++;
}
fseek(fp, 70*700*3, SEEK_CUR);
i=0;
j=0;
fseek(fp, (712-330)*700*3, SEEK_SET);
while(i<20){
fseek(fp, 290*3, SEEK_CUR);
while(j<20){
rgb.B = 0;
rgb.G = 255;
rgb.R = 0;
fwrite(&rgb, 3, 1, fp);
j++;
}
j=0;
fseek(fp, 390*3, SEEK_CUR);
i++;
}
return 0;
}
程序运行前图片:
程序运行后图片: