1 #include "evas_common.h"
2 #include "evas_private.h"
9 typedef struct _JPEG_error_mgr *emptr;
10 struct _JPEG_error_mgr
12 struct jpeg_error_mgr pub;
13 jmp_buf setjmp_buffer;
16 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
17 static void _JPEGErrorHandler(j_common_ptr cinfo);
18 static void _JPEGErrorHandler2(j_common_ptr cinfo, int msg_level);
20 static int evas_image_load_file_head_jpeg_internal(Image_Entry *ie, FILE *f);
21 static int evas_image_load_file_data_jpeg_internal(Image_Entry *ie, FILE *f);
22 #if 0 /* not used at the moment */
23 static int evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f);
26 int evas_image_load_file_head_jpeg(Image_Entry *ie, const char *file, const char *key);
27 int evas_image_load_file_data_jpeg(Image_Entry *ie, const char *file, const char *key);
29 Evas_Image_Load_Func evas_image_load_jpeg_func =
31 evas_image_load_file_head_jpeg,
32 evas_image_load_file_data_jpeg
37 _JPEGFatalErrorHandler(j_common_ptr cinfo)
41 errmgr = (emptr) cinfo->err;
42 /* cinfo->err->output_message(cinfo);*/
43 longjmp(errmgr->setjmp_buffer, 1);
48 _JPEGErrorHandler(j_common_ptr cinfo)
52 errmgr = (emptr) cinfo->err;
53 /* cinfo->err->output_message(cinfo);*/
54 /* longjmp(errmgr->setjmp_buffer, 1);*/
59 _JPEGErrorHandler2(j_common_ptr cinfo, int msg_level)
63 errmgr = (emptr) cinfo->err;
64 /* cinfo->err->output_message(cinfo);*/
65 /* longjmp(errmgr->setjmp_buffer, 1);*/
71 evas_image_load_file_head_jpeg_internal(Image_Entry *ie, FILE *f)
73 int w, h, scalew, scaleh;
74 struct jpeg_decompress_struct cinfo;
75 struct _JPEG_error_mgr jerr;
78 cinfo.err = jpeg_std_error(&(jerr.pub));
79 jerr.pub.error_exit = _JPEGFatalErrorHandler;
80 jerr.pub.emit_message = _JPEGErrorHandler2;
81 jerr.pub.output_message = _JPEGErrorHandler;
82 if (setjmp(jerr.setjmp_buffer))
84 jpeg_destroy_decompress(&cinfo);
87 jpeg_create_decompress(&cinfo);
88 jpeg_stdio_src(&cinfo, f);
89 jpeg_read_header(&cinfo, TRUE);
90 cinfo.do_fancy_upsampling = FALSE;
91 cinfo.do_block_smoothing = FALSE;
92 cinfo.dct_method = JDCT_IFAST;
93 cinfo.dither_mode = JDITHER_ORDERED;
94 jpeg_start_decompress(&cinfo);
97 w = cinfo.output_width;
98 h = cinfo.output_height;
99 if ((w < 1) || (h < 1) || (w > 8192) || (h > 8192))
101 jpeg_destroy_decompress(&cinfo);
104 if (ie->load_opts.scale_down_by > 1)
106 w /= ie->load_opts.scale_down_by;
107 h /= ie->load_opts.scale_down_by;
109 else if (ie->load_opts.dpi > 0.0)
111 w = (w * ie->load_opts.dpi) / 90.0;
112 h = (h * ie->load_opts.dpi) / 90.0;
114 else if ((ie->load_opts.w > 0) &&
115 (ie->load_opts.h > 0))
119 w2 = ie->load_opts.w;
120 h2 = (ie->load_opts.w * h) / w;
121 if (h2 > ie->load_opts.h)
123 h2 = ie->load_opts.h;
124 w2 = (ie->load_opts.h * w) / h;
132 if ((w != cinfo.output_width) || (h != cinfo.output_height))
134 scalew = cinfo.output_width / w;
135 scaleh = cinfo.output_height / h;
138 if (scaleh < scalew) ie->scale = scaleh;
140 if (ie->scale > 8) ie->scale = 8;
141 else if (ie->scale < 1) ie->scale = 1;
143 if (ie->scale == 3) ie->scale = 2;
144 else if (ie->scale == 5) ie->scale = 4;
145 else if (ie->scale == 6) ie->scale = 4;
146 else if (ie->scale == 7) ie->scale = 4;
151 jpeg_destroy_decompress(&cinfo);
154 jpeg_create_decompress(&cinfo);
155 jpeg_stdio_src(&cinfo, f);
156 jpeg_read_header(&cinfo, TRUE);
157 cinfo.do_fancy_upsampling = FALSE;
158 cinfo.do_block_smoothing = FALSE;
160 cinfo.scale_denom = ie->scale;
161 jpeg_calc_output_dimensions(&(cinfo));
162 jpeg_start_decompress(&cinfo);
165 ie->w = cinfo.output_width;
166 ie->h = cinfo.output_height;
167 /* end head decoding */
169 jpeg_destroy_decompress(&cinfo);
174 evas_image_load_file_data_jpeg_internal(Image_Entry *ie, FILE *f)
177 struct jpeg_decompress_struct cinfo;
178 struct _JPEG_error_mgr jerr;
179 DATA8 *ptr, *line[16], *data;
181 int x, y, l, i, scans, count, prevy;
184 cinfo.err = jpeg_std_error(&(jerr.pub));
185 jerr.pub.error_exit = _JPEGFatalErrorHandler;
186 jerr.pub.emit_message = _JPEGErrorHandler2;
187 jerr.pub.output_message = _JPEGErrorHandler;
188 if (setjmp(jerr.setjmp_buffer))
190 jpeg_destroy_decompress(&cinfo);
193 jpeg_create_decompress(&cinfo);
194 jpeg_stdio_src(&cinfo, f);
195 jpeg_read_header(&cinfo, TRUE);
196 cinfo.do_fancy_upsampling = FALSE;
197 cinfo.do_block_smoothing = FALSE;
198 cinfo.dct_method = JDCT_IFAST;
199 cinfo.dither_mode = JDITHER_ORDERED;
204 cinfo.scale_denom = ie->scale;
208 jpeg_calc_output_dimensions(&(cinfo));
209 jpeg_start_decompress(&cinfo);
211 w = cinfo.output_width;
212 h = cinfo.output_height;
214 if ((w != ie->w) || (h != ie->h))
216 jpeg_destroy_decompress(&cinfo);
220 /* end head decoding */
222 if (cinfo.rec_outbuf_height > 16)
224 jpeg_destroy_decompress(&cinfo);
227 data = alloca(w * 16 * 3);
228 evas_cache_image_surface_alloc(ie, w, h);
229 if (ie->flags.loaded)
231 jpeg_destroy_decompress(&cinfo);
234 ptr2 = evas_cache_image_pixels(ie);
237 if (cinfo.output_components == 3)
239 for (i = 0; i < cinfo.rec_outbuf_height; i++)
240 line[i] = data + (i * w * 3);
241 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
243 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
244 scans = cinfo.rec_outbuf_height;
245 if ((h - l) < scans) scans = h - l;
247 for (y = 0; y < scans; y++)
249 for (x = 0; x < w; x++)
252 (0xff000000) | ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
259 else if (cinfo.output_components == 1)
261 for (i = 0; i < cinfo.rec_outbuf_height; i++)
262 line[i] = data + (i * w);
263 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
265 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
266 scans = cinfo.rec_outbuf_height;
267 if ((h - l) < scans) scans = h - l;
269 for (y = 0; y < scans; y++)
271 for (x = 0; x < w; x++)
274 (0xff000000) | ((ptr[0]) << 16) | ((ptr[0]) << 8) | (ptr[0]);
281 /* end data decoding */
282 jpeg_finish_decompress(&cinfo);
283 jpeg_destroy_decompress(&cinfo);
287 #if 0 /* not used at the moment */
289 evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f)
292 struct jpeg_decompress_struct cinfo;
293 struct _JPEG_error_mgr jerr;
294 DATA8 *ptr, *line[16], *data;
296 int x, y, l, i, scans, count, prevy;
299 cinfo.err = jpeg_std_error(&(jerr.pub));
300 jerr.pub.error_exit = _JPEGFatalErrorHandler;
301 jerr.pub.emit_message = _JPEGErrorHandler2;
302 jerr.pub.output_message = _JPEGErrorHandler;
303 if (setjmp(jerr.setjmp_buffer))
305 jpeg_destroy_decompress(&cinfo);
308 jpeg_create_decompress(&cinfo);
309 jpeg_stdio_src(&cinfo, f);
310 jpeg_read_header(&cinfo, TRUE);
311 cinfo.do_fancy_upsampling = FALSE;
312 cinfo.do_block_smoothing = FALSE;
313 jpeg_start_decompress(&cinfo);
316 ie->w = w = cinfo.output_width;
317 ie->h = h = cinfo.output_height;
318 /* end head decoding */
320 if (cinfo.rec_outbuf_height > 16)
322 jpeg_destroy_decompress(&cinfo);
325 data = alloca(w * 16 * 3);
326 if (!ie->flags.loaded)
328 jpeg_destroy_decompress(&cinfo);
331 ptr2 = evas_cache_image_pixels(ie);
334 if (cinfo.output_components == 3)
336 for (i = 0; i < cinfo.rec_outbuf_height; i++)
337 line[i] = data + (i * w * 3);
338 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
340 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
341 scans = cinfo.rec_outbuf_height;
342 if ((h - l) < scans) scans = h - l;
344 for (y = 0; y < scans; y++)
346 for (x = 0; x < w; x++)
349 ((*ptr2) & 0x00ffffff) |
350 (((ptr[0] + ptr[1] + ptr[2]) / 3) << 24);
357 else if (cinfo.output_components == 1)
359 for (i = 0; i < cinfo.rec_outbuf_height; i++)
360 line[i] = data + (i * w);
361 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
363 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
364 scans = cinfo.rec_outbuf_height;
365 if ((h - l) < scans) scans = h - l;
367 for (y = 0; y < scans; y++)
369 for (x = 0; x < w; x++)
372 ((*ptr2) & 0x00ffffff) |
380 /* end data decoding */
381 jpeg_finish_decompress(&cinfo);
382 jpeg_destroy_decompress(&cinfo);
388 evas_image_load_file_head_jpeg(Image_Entry *ie, const char *file, const char *key)
393 if ((!file)) return 0;
394 f = fopen(file, "rb");
396 val = evas_image_load_file_head_jpeg_internal(ie, f);
403 evas_image_load_file_data_jpeg(Image_Entry *ie, const char *file, const char *key)
408 if ((!file)) return 0;
409 f = fopen(file, "rb");
411 val = evas_image_load_file_data_jpeg_internal(ie, f);
418 module_open(Evas_Module *em)
421 em->functions = (void *)(&evas_image_load_jpeg_func);
431 EAPI Evas_Module_Api evas_modapi =
433 EVAS_MODULE_API_VERSION,
434 EVAS_MODULE_TYPE_IMAGE_LOADER,