#include
#include
#include
#include
#include
#include
char * jpgFileName = "test.jpg";
char * FileName = "output.jpeg";
int main()
{
FILE* f1=NULL;
FILE* f2=NULL;
unsigned char * RGB_Ptr = NULL;
int width,height;
char *DataBuffer=NULL;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err=jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
if((f1=fopen(jpgFileName,"rb"))==NULL)
{
printf("can not find file\n");
return 0;
}
jpeg_stdio_src(&cinfo,f1);
jpeg_read_header(&cinfo,1);
{
width=cinfo.image_width;
height=cinfo.image_height;
if(cinfo.num_components!=3)
{
printf("the coular number is zero\n");
return 0;
}
printf("Width:%d;Height:%d;Components:%d\n",width,height,cinfo.num_components);
}
unsigned char *lineData =NULL;
JSAMPARRAY buffer;
jpeg_start_decompress(&cinfo);
DataBuffer=malloc(width*height*3);
lineData=malloc(width*3);
buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,JPOOL_IMAGE,(width*3),1);
while(cinfo.output_scanline < cinfo.output_height)
{
int j=0;
jpeg_read_scanlines(&cinfo,buffer,1);
for(j=0;j<(width*3);j++)
{
DataBuffer[(height-cinfo.output_scanline)*(width*3)+j] = buffer[0][j+2]; //R
j++;
DataBuffer[(height-cinfo.output_scanline)*(width*3)+j] = buffer[0][j]; //G
j++;
DataBuffer[(height-cinfo.output_scanline)*(width*3)+j] = buffer[0][j-2]; //B
}
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
struct jpeg_compress_struct jcs;
struct jpeg_error_mgr jem;
jcs.err=jpeg_std_error(&jem);
jpeg_create_compress(&jcs);
f2=fopen(FileName,"wb");
jpeg_stdio_dest(&jcs,f2);
printf("Width:%d;Height:%d;Components:%d\n",width,height,cinfo.num_components);
RGB_Ptr=malloc(width*height*3+1);
strcpy(RGB_Ptr,DataBuffer);
jcs.image_width=width;
jcs.image_height=height;
jcs.input_components=3;
jcs.in_color_space=JCS_RGB;
jpeg_set_defaults(&jcs);
jpeg_set_quality(&jcs,80,1);
jpeg_start_compress(&jcs,1);
{
int i=0;
long rgb_index=height*width*3-1;
JSAMPROW *row_pointer;
rgb_index=0;
row_pointer=malloc(height*width*3);
for(i=0;i {
int j=0;
unsigned char *lineData=NULL;
lineData =malloc(width*3);
for(j=0;j {
lineData[j*3+2]=RGB_Ptr[rgb_index];
rgb_index++;
lineData[j*3+1]=RGB_Ptr[rgb_index];
rgb_index++;
lineData[j*3+0]=RGB_Ptr[rgb_index];
rgb_index++;
}
row_pointer[height-i-1]=lineData;
}
jpeg_write_scanlines(&jcs,row_pointer,height);
}
jpeg_finish_compress(&jcs);
jpeg_destroy_compress(&jcs);
fclose(f1);
return 0;
}
阅读(1492) | 评论(0) | 转发(0) |