00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <memory.h>
00025
00026 #include "jpeg.h"
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <setjmp.h>
00037
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041 #include <jpeglib.h>
00042 #ifdef __cplusplus
00043 }
00044 #endif
00045
00046
00047
00048
00049
00050 namespace HAIR
00051 {
00052
00053
00054
00055
00056
00057
00058
00059 bool write_JPEG_file (char * filename, int quality, imageRGB img)
00060 {
00061
00062 int image_width = img.width;
00063 int image_height = img.height;
00064 unsigned char* image = img.data;
00065 int ltrysave = 0;
00066 bool booltrysave = true;
00067 bool boolreturn = false;
00068
00069
00070 JSAMPLE * image_buffer = (JSAMPLE *) image;
00071 struct jpeg_compress_struct cinfo;
00072 struct jpeg_error_mgr jerr;
00073
00074 FILE * outfile;
00075 JSAMPROW row_pointer[1];
00076 int row_stride;
00077
00078 cinfo.err = jpeg_std_error(&jerr);
00079 jpeg_create_compress(&cinfo);
00080
00081 while(booltrysave)
00082 {
00083 ltrysave++;
00084 if ((outfile = fopen(filename, "wb")) == NULL)
00085 {
00086 fprintf(stderr, " > can't open %s %d/4\n", filename,ltrysave);
00087 }
00088 else
00089 booltrysave = false;
00090
00091 if(ltrysave == 4)
00092 return false;
00093 }
00094
00095 jpeg_stdio_dest(&cinfo, outfile);
00096
00097 cinfo.image_width = image_width;
00098 cinfo.image_height = image_height;
00099 cinfo.input_components = 3;
00100 cinfo.in_color_space = JCS_RGB;
00101 jpeg_set_defaults(&cinfo);
00102 jpeg_set_quality(&cinfo, quality, TRUE );
00103
00104 jpeg_start_compress(&cinfo, TRUE);
00105
00106 row_stride = image_width * 3;
00107
00108 while (cinfo.next_scanline < cinfo.image_height)
00109 {
00110 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00111 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00112 }
00113
00114 jpeg_finish_compress(&cinfo);
00115 fclose(outfile);
00116 jpeg_destroy_compress(&cinfo);
00117
00118 return true;
00119
00120 }
00121
00122
00123 struct my_error_mgr
00124 {
00125 struct jpeg_error_mgr pub;
00126 jmp_buf setjmp_buffer;
00127 };
00128
00129 typedef struct my_error_mgr * my_error_ptr;
00130
00131 METHODDEF(void) my_error_exit (j_common_ptr cinfo)
00132 {
00133 my_error_ptr myerr = (my_error_ptr) cinfo->err;
00134
00135 (*cinfo->err->output_message) (cinfo);
00136
00137 longjmp(myerr->setjmp_buffer, 1);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 GLOBAL(imageRGB) read_JPEG_file (char * filename)
00147 {
00148 struct jpeg_decompress_struct cinfo;
00149 struct my_error_mgr jerr;
00150 FILE * infile;
00151 JSAMPARRAY buffer;
00152 int row_stride;
00153
00154 if ((infile = fopen(filename, "rb")) == NULL)
00155 {
00156 fprintf(stderr, "can't open %s\n", filename);
00157 imageRGB zero = {0,0,0};
00158 return zero;
00159 }
00160
00161 cinfo.err = jpeg_std_error(&jerr.pub);
00162 jerr.pub.error_exit = my_error_exit;
00163 if (setjmp(jerr.setjmp_buffer))
00164 {
00165 jpeg_destroy_decompress(&cinfo);
00166 fclose(infile);
00167 imageRGB zero = {0,0,0};
00168 return zero;
00169 }
00170
00171 jpeg_create_decompress(&cinfo);
00172 jpeg_stdio_src(&cinfo, infile);
00173 (void) jpeg_read_header(&cinfo, TRUE);
00174 (void) jpeg_start_decompress(&cinfo);
00175 row_stride = cinfo.output_width * cinfo.output_components;
00176 buffer = (*cinfo.mem->alloc_sarray)
00177 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
00178 imageRGB img;
00179 img.width = cinfo.output_width;
00180 img.height = cinfo.output_height;
00181
00182 img.data = (unsigned char*)malloc(cinfo.output_components * img.width * img.height);
00183 unsigned char* ptr = img.data;
00184 while (cinfo.output_scanline < cinfo.output_height)
00185 {
00186 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
00187 memcpy(ptr, buffer[0], row_stride); ptr += row_stride;
00188 }
00189
00190 (void) jpeg_finish_decompress(&cinfo);
00191 jpeg_destroy_decompress(&cinfo);
00192
00193 fclose(infile);
00194
00195 return img;
00196 }
00197
00198 }
00199
00200