Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include "jpeglib.h"
00013 #include <setjmp.h>
00014
00015
00016 int JPEG_compress(JSAMPLE *image_buffer, long width, long height,
00017 unsigned char *jpeg_image_buffer, long size, int quality)
00018 {
00019 struct jpeg_compress_struct cinfo;
00020 struct jpeg_error_mgr jerr;
00021 JSAMPROW row_pointer[1];
00022 int row_stride;
00023 long data_count=size;
00024
00025 cinfo.err = jpeg_std_error(&jerr);
00026
00027 jpeg_create_compress(&cinfo);
00028
00029 jpeg_memio_dest(&cinfo, jpeg_image_buffer, &data_count);
00030
00031
00032
00033
00034
00035 cinfo.image_width = width;
00036 cinfo.image_height = height;
00037 cinfo.input_components = 3;
00038 cinfo.in_color_space = JCS_RGB;
00039
00040 jpeg_set_defaults(&cinfo);
00041 jpeg_set_quality(&cinfo, quality, TRUE );
00042
00043 jpeg_start_compress(&cinfo, TRUE);
00044
00045 row_stride = width * 3;
00046
00047 while (cinfo.next_scanline < cinfo.image_height) {
00048 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00049 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00050 }
00051
00052 jpeg_finish_compress(&cinfo);
00053 jpeg_destroy_compress(&cinfo);
00054 return(data_count);
00055 }
00056
00057
00058
00059 struct my_error_mgr {
00060 struct jpeg_error_mgr pub;
00061
00062 jmp_buf setjmp_buffer;
00063 };
00064
00065 typedef struct my_error_mgr * my_error_ptr;
00066
00067
00068 static void my_error_exit (j_common_ptr cinfo)
00069 {
00070 my_error_ptr myerr = (my_error_ptr) cinfo->err;
00071
00072 (*cinfo->err->output_message) (cinfo);
00073
00074 longjmp(myerr->setjmp_buffer, 1);
00075 }
00076
00077 extern int JPEG_header (JOCTET *jpeg_image, long jpeg_size,
00078 long *width, long *height , long *components)
00079 { struct jpeg_decompress_struct cinfo;
00080 struct my_error_mgr jerr;
00081 long total_size;
00082
00083 cinfo.err = jpeg_std_error(&jerr.pub);
00084 jerr.pub.error_exit = my_error_exit;
00085
00086 if (setjmp(jerr.setjmp_buffer)) {
00087 jpeg_destroy_decompress(&cinfo);
00088 return 0; }
00089
00090 jpeg_create_decompress(&cinfo);
00091
00092 jpeg_memio_src(&cinfo, jpeg_image, jpeg_size);
00093
00094 (void) jpeg_read_header(&cinfo, TRUE);
00095
00096 jpeg_calc_output_dimensions(&cinfo);
00097
00098 *width= cinfo.output_width;
00099 *height= cinfo.output_height;
00100 *components= cinfo.output_components;
00101 total_size = cinfo.output_width * cinfo.output_height *
00102 cinfo.output_components;
00103
00104 jpeg_destroy_decompress(&cinfo);
00105 return(total_size);
00106 }
00107
00108
00109 extern int JPEG_decompress (JOCTET *jpeg_image, long jpeg_size,
00110 JOCTET *result_image,
00111 long *width, long *height)
00112 {
00113 JOCTET *rimage;
00114 struct jpeg_decompress_struct cinfo;
00115 struct my_error_mgr jerr;
00116 JSAMPLE *jsampbuf;
00117 int row_stride;
00118 long total_size;
00119
00120 cinfo.err = jpeg_std_error(&jerr.pub);
00121 jerr.pub.error_exit = my_error_exit;
00122
00123 if (setjmp(jerr.setjmp_buffer)) {
00124 jpeg_destroy_decompress(&cinfo);
00125 return 0; }
00126
00127 jpeg_create_decompress(&cinfo);
00128
00129 jpeg_memio_src(&cinfo, jpeg_image, jpeg_size);
00130
00131 (void) jpeg_read_header(&cinfo, TRUE);
00132
00133 jpeg_calc_output_dimensions(&cinfo);
00134
00135 *width= cinfo.output_width;
00136 *height= cinfo.output_height;
00137
00138 row_stride = cinfo.output_width * cinfo.output_components;
00139 total_size = row_stride * cinfo.output_height;
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 (void) jpeg_start_decompress(&cinfo);
00150
00151 while (cinfo.output_scanline < cinfo.output_height) {
00152 jsampbuf= &result_image[cinfo.output_scanline * row_stride];
00153 (void) jpeg_read_scanlines(&cinfo, &jsampbuf, 1);
00154 }
00155
00156 (void) jpeg_finish_decompress(&cinfo);
00157
00158 jpeg_destroy_decompress(&cinfo);
00159 return(total_size);
00160 }
00161
00162