原理: 取两张图片的颜色最大值或最小值组合成新图片.
1.添加头文件, 并定义相关宏和结构体
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
#define JpegStdError jpeg_std_error
#define JpegCreateDecompress jpeg_create_decompress
#define JpegCreateCompress jpeg_create_compress
#define JpegStdioSrc jpeg_stdio_src
#define JpegReadHeader jpeg_read_header
#define JpegStartDecompress jpeg_start_decompress
#define ScanLine output_scanline
#define NextLine next_scanline
#define JpegReadLine jpeg_read_scanlines
#define JpegFinishDecompress jpeg_finish_decompress
#define JpegDestroyDecompress jpeg_destroy_decompress
#define JpegStdioDest jpeg_stdio_dest
#define JpegSetDefault jpeg_set_defaults
#define JpegSetQuality jpeg_set_quality
#define JpegStartCompress jpeg_start_compress
#define JpegWriteLine jpeg_write_scanlines
#define JpegFinishCompress jpeg_finish_compress
#define JpegDestroyCompress jpeg_destroy_compress
typedef JSAMPARRAY JpegArray;
typedef FILE *FilePtr;
typedef struct jpeg_compress_struct JpegComp;
typedef struct jpeg_decompress_struct JpegDecomp;
typedef struct jpeg_error_mgr JpegErrMgr;
typedef struct ImagePicData ImagePicData;
typedef struct ImageRGBAColor ImageRGBAColor;
struct ImagePicData
{
unsigned char FName[256];
FilePtr FPtr;
int Width;
int Height;
int BitDepth;
int Flag;
int ColType;
unsigned char *RGBA;
};
struct ImageRGBAColor
{
short r;
short g;
short b;
short a;
};
2.计算颜色最大最小值.
int MaxColor(ImageRGBAColor *c, ImageRGBAColor a, ImageRGBAColor b)
{
c->r = a.r > b.r ? a.r : b.r;
c->g = a.g > b.g ? a.g : b.g;
c->b = a.b > b.b ? a.b : b.b;
c->a = a.a > b.a ? a.a : b.a;
return 0;
}
int MinColor(ImageRGBAColor *c, ImageRGBAColor a, ImageRGBAColor b)
{
c->r = a.r < b.r ? a.r : b.r;
c->g = a.g < b.g ? a.g : b.g;
c->b = a.b < b.b ? a.b : b.b;
c->a = a.a < b.a ? a.a : b.a;
return 0;
}
int MaxMinImage(ImagePicData *ImageDataA, ImagePicData *ImageDataB, int flag)
{
ImageRGBAColor a, b, c;
int Height = ImageDataA->Height;
int Width = ImageDataA->Width;
int Pos = 0;
int i, j;
for(i=0; i<Height; i++)
{
for(j=0; j<Width; j++)
{
a.r = ImageDataA->RGBA[Pos*4+0];
a.g = ImageDataA->RGBA[Pos*4+1];
a.b = ImageDataA->RGBA[Pos*4+2];
a.a = ImageDataA->RGBA[Pos*4+3];
b.r = ImageDataB->RGBA[Pos*4+0];
b.g = ImageDataB->RGBA[Pos*4+1];
b.b = ImageDataB->RGBA[Pos*4+2];
b.a = ImageDataB->RGBA[Pos*4+3];
if(flag) MaxColor(&c, a, b);
else MinColor(&c, a, b);
ImageDataA->RGBA[Pos*4+0] = c.r;
ImageDataA->RGBA[Pos*4+1] = c.g;
ImageDataA->RGBA[Pos*4+2] = c.b;
ImageDataA->RGBA[Pos*4+3] = c.a;
Pos++;
}
}
return 0;
}
3.读写图片
参见《C语言实现色彩平衡算法》
4.添加主函数
int main(int argc, char **argv)
{
ImagePicData PicData[2];
int Flag;
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 != 5)
{
printf("入参个数错误!\n");
return -1;
}
strncpy(JpegInputName[0], argv[1], 255);
strncpy(JpegInputName[1], argv[2], 255);
strncpy(JpegOutputName, argv[3], 255);
Flag = atoi(argv[4]);
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);
MaxMinImage(PicData, PicData+1, Flag);
CreateJPG(PicData);
return 0;
}
5.编译运行
$ gcc -o examle examle.c -L$HOME/local/prior/lib -ljpeg
$ ./examle a.jpg b.jpg c.jpg 1
$ ./examle a.jpg b.jpg d.jpg 0
6.运行结果
原图
效果