原理: C = A + B x 2 - 255
1.添加头文件, 并定义相关宏和结构体
参见《C语言实现图像叠加效果》
2.实现线性光效果.
int LinearLightImage(ImagePicData *ImageDataA, ImagePicData *ImageDataB)
{
ImageRGBAColor a, b, c;
int Height = ImageDataA->Height;
int Width = ImageDataA->Width;
int Pos = 0;
int i, j;
for(i=0; i
{
for(j=0; j
{
a.r = ImageDataA->RGBA[Pos*4+0];
a.g = ImageDataA->RGBA[Pos*4+1];
a.b = ImageDataA->RGBA[Pos*4+2];
b.r = ImageDataB->RGBA[Pos*4+0];
b.g = ImageDataB->RGBA[Pos*4+1];
b.b = ImageDataB->RGBA[Pos*4+2];
c.r = a.r + b.r * 2 - 255;
c.g = a.g + b.g * 2 - 255;
c.b = a.b + b.b * 2 - 255;
if(c.r > 255) c.r = 255;
else if(c.r < 0) c.r = 0;
if(c.g > 255) c.g = 255;
else if(c.g < 0) c.g = 0;
if(c.b > 255) c.b = 255;
else if(c.b < 0) c.b = 0;
ImageDataA->RGBA[Pos*4+0] = c.r;
ImageDataA->RGBA[Pos*4+1] = c.g;
ImageDataA->RGBA[Pos*4+2] = c.b;
Pos++;
}
}
return 0;
}
3.读写图片
参见《C语言实现色彩平衡算法》
4.添加主函数
int main(int argc, char **argv)
{
ImagePicData PicData[2];
char RGBAData[2][16*300*300];
char TmpData[16*300*300];
char JpegInputName[2][256];
char JpegOutputName[256];
memset(PicData, 0x00, sizeof(PicData));
memset(TmpData, 0x00, sizeof(TmpData));
memset(JpegInputName, 0x00, sizeof(JpegInputName));
memset(JpegOutputName, 0x00, sizeof(JpegOutputName));
if(argc != 4)
{
printf("入参个数错误!\n");
return -1;
}
strncpy(JpegInputName[0], argv[1], 255);
strncpy(JpegInputName[1], argv[2], 255);
strncpy(JpegOutputName, argv[3], 255);
PicData[0].RGBA = RGBAData[0];
PicData[1].RGBA = RGBAData[1];
strcpy(PicData[0].FName, JpegInputName[0]);
strcpy(PicData[1].FName, JpegInputName[1]);
LoadJPG(PicData);
LoadJPG(PicData+1);
strcpy(PicData[0].FName, JpegOutputName);
LinearLightImage(PicData, PicData+1);
CreateJPG(PicData);
return 0;
}
5.编译运行
$ gcc -o examle examle.c -L$HOME/local/prior/lib -ljpeg
$ ./examle a.jpg e.jpg t.jpg
6.运行结果
原图
a.jpg
e.jpg
效果
t.jpg