原理: 浮雕通过勾画图像轮廓和降低周围像素颜色值,从而生产具有凹凸感的浮雕效果。
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;
typedef struct ImagePicPos ImagePicPos;
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 EmbossImage(ImagePicData *ImageData, unsigned char *ImageXcgData, short offset)
{
ImagePicData ImageTempData;
ImageRGBAColor RGBAColor;
int Pos = 0;
int i, j;
ImageTempData.RGBA = ImageXcgData;
//printf("for[i]循环开始\n");
for(i = 0; i < ImageData->Height-1; i++)
{
for(j = 0; j < ImageData->Width-1; j++)
{
RGBAColor.r = 4*ImageData->RGBA[((i)*ImageData->Width+(j))*4+0] - ImageData->RGBA[((i-1)*ImageData->Width+(j-1))*4+0] - ImageData->RGBA[((i-1)*ImageData->Width+(j+1))*4+0] - ImageData->RGBA[((i+1)*ImageData->Width+(j-1))*4+0] - ImageData->RGBA[((i+1)*ImageData->Width+(j+1))*4+0];
RGBAColor.g = 4*ImageData->RGBA[((i)*ImageData->Width+(j))*4+1] - ImageData->RGBA[((i-1)*ImageData->Width+(j-1))*4+1] - ImageData->RGBA[((i-1)*ImageData->Width+(j+1))*4+1] - ImageData->RGBA[((i+1)*ImageData->Width+(j-1))*4+1] - ImageData->RGBA[((i+1)*ImageData->Width+(j+1))*4+1];
RGBAColor.b = 4*ImageData->RGBA[((i)*ImageData->Width+(j))*4+2] - ImageData->RGBA[((i-1)*ImageData->Width+(j-1))*4+2] - ImageData->RGBA[((i-1)*ImageData->Width+(j+1))*4+2] - ImageData->RGBA[((i+1)*ImageData->Width+(j-1))*4+2] - ImageData->RGBA[((i+1)*ImageData->Width+(j+1))*4+2];
RGBAColor.r += offset;
RGBAColor.g += offset;
RGBAColor.b += offset;
if(RGBAColor.r < 0) RGBAColor.r = 0;
else if(RGBAColor.r > 255) RGBAColor.r = 255;
if(RGBAColor.g < 0) RGBAColor.g = 0;
else if(RGBAColor.g > 255) RGBAColor.g = 255;
if(RGBAColor.b < 0) RGBAColor.b = 0;
else if(RGBAColor.b > 255) RGBAColor.b = 255;
ImageTempData.RGBA[((i)*ImageData->Width+(j))*4+0] = RGBAColor.r;
ImageTempData.RGBA[((i)*ImageData->Width+(j))*4+1] = RGBAColor.g;
ImageTempData.RGBA[((i)*ImageData->Width+(j))*4+2] = RGBAColor.b;
}
}
memcpy(ImageData->RGBA, ImageTempData.RGBA, ImageData->Width*ImageData->Height*4);
return 0;
}
3.读写图片
参见《C语言实现色彩平衡算法》
4.添加主函数
int main(int argc, char **argv)
{
ImagePicData PicData;
int a = 127;
char RGBAData[16*300*300];
char RGBATempData[16*300*300];
char JpegInputName[256];
char JpegOutputName[256];
memset(&PicData, 0x00, sizeof(PicData));
memset(JpegInputName, 0x00, sizeof(JpegInputName));
memset(JpegOutputName, 0x00, sizeof(JpegOutputName));
if(argc != 4)
{
printf("入参个数错误!\n");
return -1;
}
strncpy(JpegInputName, argv[1], 255);
strncpy(JpegOutputName, argv[2], 255);
PicData.RGBA = RGBAData;
a = atoi(argv[3]);
strcpy(PicData.FName, JpegInputName);
LoadJPG(&PicData);
strcpy(PicData.FName, JpegOutputName);
EmbossImage(&PicData, RGBATempData, a);
CreateJPG(&PicData);
return 0;
}
5.编译运行
$ gcc -o examle examle.c -L$HOME/local/prior/lib -ljpeg
$ ./examle a.jpg b.jpg 150
6.运行结果
原图
效果