14 #include "evas_common.h"
15 #include "evas_private.h"
18 typedef struct _JPEG_error_mgr *emptr;
19 struct _JPEG_error_mgr
21 struct jpeg_error_mgr pub;
22 jmp_buf setjmp_buffer;
25 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
26 static void _JPEGErrorHandler(j_common_ptr cinfo);
27 static void _JPEGErrorHandler2(j_common_ptr cinfo, int msg_level);
29 static Eina_Bool evas_image_load_file_head_jpeg_internal(Image_Entry *ie,
32 int *error) EINA_ARG_NONNULL(1, 2, 4);
33 static Eina_Bool evas_image_load_file_data_jpeg_internal(Image_Entry *ie,
36 int *error) EINA_ARG_NONNULL(1, 2, 4);
37 #if 0 /* not used at the moment */
38 static int evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f) EINA_ARG_NONNULL(1, 2);
41 static Eina_Bool evas_image_load_file_head_jpeg(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4);
42 static Eina_Bool evas_image_load_file_data_jpeg(Image_Entry *ie, const char *file, const char *key, int *error) EINA_ARG_NONNULL(1, 2, 4);
44 static Evas_Image_Load_Func evas_image_load_jpeg_func =
47 evas_image_load_file_head_jpeg,
48 evas_image_load_file_data_jpeg,
55 _JPEGFatalErrorHandler(j_common_ptr cinfo)
59 errmgr = (emptr) cinfo->err;
60 /* cinfo->err->output_message(cinfo);*/
61 longjmp(errmgr->setjmp_buffer, 1);
66 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
70 /* errmgr = (emptr) cinfo->err; */
71 /* cinfo->err->output_message(cinfo);*/
72 /* longjmp(errmgr->setjmp_buffer, 1);*/
77 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__, int msg_level __UNUSED__)
81 /* errmgr = (emptr) cinfo->err; */
82 /* cinfo->err->output_message(cinfo);*/
83 /* longjmp(errmgr->setjmp_buffer, 1);*/
87 struct jpeg_membuf_src
89 struct jpeg_source_mgr pub;
91 const unsigned char *buf;
93 struct jpeg_membuf_src *self;
97 _evas_jpeg_membuf_src_init(j_decompress_ptr cinfo __UNUSED__)
102 _evas_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
104 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
105 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
107 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
108 src->pub.next_input_byte = jpeg_eoi;
114 _evas_jpeg_membuf_src_skip(j_decompress_ptr cinfo,
117 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
119 if ((((long)src->pub.bytes_in_buffer - (long)src->len) > num_bytes) ||
120 ((long)src->pub.bytes_in_buffer < num_bytes))
122 (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo));
125 src->pub.bytes_in_buffer -= num_bytes;
126 src->pub.next_input_byte += num_bytes;
130 _evas_jpeg_membuf_src_term(j_decompress_ptr cinfo)
132 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
139 _evas_jpeg_membuf_src(j_decompress_ptr cinfo,
140 void *map, size_t length)
142 struct jpeg_membuf_src *src;
144 src = calloc(1, sizeof(*src));
150 cinfo->src = &src->pub;
153 src->pub.init_source = _evas_jpeg_membuf_src_init;
154 src->pub.fill_input_buffer = _evas_jpeg_membuf_src_fill;
155 src->pub.skip_input_data = _evas_jpeg_membuf_src_skip;
156 src->pub.resync_to_restart = jpeg_resync_to_restart;
157 src->pub.term_source = _evas_jpeg_membuf_src_term;
158 src->pub.bytes_in_buffer = src->len;
159 src->pub.next_input_byte = src->buf;
164 /*! Magic number for EXIF header & App1*/
165 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
166 static const unsigned char App1[] = {0xff, 0xe1};
173 _get_orientation(void *map, size_t length)
177 ExifByteAlign byte_align;
178 unsigned int num_directory = 0;
182 /* open file and get 22 byte frome file */
185 if (length < 22) return 0;
188 /* 2. check 2,3 bypte with APP1(0xFFE1) */
189 if (memcmp(buf + 2, App1, sizeof (App1))) return 0;
191 /* 3. check 6~11bype with Exif Header (0x45786966 0000) */
192 if (memcmp(buf + 6, ExifHeader, sizeof (ExifHeader))) return 0;
194 /* 4. get 12&13 byte get info of "II(0x4949)" or "MM(0x4d4d)" */
195 /* 5. get [20]&[21] get directory entry # */
196 if (!strncmp(buf + 12, "MM", 2))
198 byte_align = EXIF_BYTE_ALIGN_MM;
199 num_directory = ((*(buf + 20) << 8) + *(buf + 21));
200 orientation[0] = 0x01;
201 orientation[1] = 0x12;
203 else if (!strncmp(buf + 12, "II", 2))
205 byte_align = EXIF_BYTE_ALIGN_II;
206 num_directory = ((*(buf + 21) << 8) + *(buf + 20));
207 orientation[0] = 0x12;
208 orientation[1] = 0x01;
214 if (length < (12 * num_directory + 22)) return 0;
218 for (i = 0; i < num_directory; i++ )
220 if (!strncmp(buf + j, orientation, 2))
222 /*get orientation tag */
223 if (byte_align == EXIF_BYTE_ALIGN_MM)
224 direction = *(buf+ j + 11);
225 else direction = *(buf+ j + 8);
248 evas_image_load_file_head_jpeg_internal(Image_Entry *ie,
249 void *map, size_t length,
252 unsigned int w, h, scalew, scaleh;
253 struct jpeg_decompress_struct cinfo;
254 struct _JPEG_error_mgr jerr;
256 /* for rotation decoding */
258 Eina_Bool change_wh = EINA_FALSE;
259 unsigned int load_opts_w = 0, load_opts_h = 0;
261 memset(&cinfo, 0, sizeof(cinfo));
262 cinfo.err = jpeg_std_error(&(jerr.pub));
263 jerr.pub.error_exit = _JPEGFatalErrorHandler;
264 jerr.pub.emit_message = _JPEGErrorHandler2;
265 jerr.pub.output_message = _JPEGErrorHandler;
266 if (setjmp(jerr.setjmp_buffer))
268 jpeg_destroy_decompress(&cinfo);
269 _evas_jpeg_membuf_src_term(&cinfo);
270 if (cinfo.saw_JFIF_marker)
271 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
273 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
276 jpeg_create_decompress(&cinfo);
278 if (_evas_jpeg_membuf_src(&cinfo, map, length))
280 jpeg_destroy_decompress(&cinfo);
281 _evas_jpeg_membuf_src_term(&cinfo);
282 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
286 jpeg_read_header(&cinfo, TRUE);
287 cinfo.do_fancy_upsampling = FALSE;
288 cinfo.do_block_smoothing = FALSE;
289 cinfo.dct_method = JDCT_ISLOW; // JDCT_FLOAT JDCT_IFAST(quality loss)
290 cinfo.dither_mode = JDITHER_ORDERED;
291 jpeg_start_decompress(&cinfo);
293 /* rotation decoding */
294 if (ie->load_opts.orientation)
296 degree = _get_orientation(map, length);
299 ie->load_opts.degree = degree;
300 ie->flags.rotated = EINA_TRUE;
302 if (degree == 90 || degree == 270)
303 change_wh = EINA_TRUE;
309 w = cinfo.output_width;
310 h = cinfo.output_height;
311 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
314 jpeg_destroy_decompress(&cinfo);
315 _evas_jpeg_membuf_src_term(&cinfo);
316 if (IMG_TOO_BIG(w, h))
317 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
319 *error = EVAS_LOAD_ERROR_GENERIC;
322 if (ie->load_opts.scale_down_by > 1)
324 w /= ie->load_opts.scale_down_by;
325 h /= ie->load_opts.scale_down_by;
327 else if (ie->load_opts.dpi > 0.0)
329 w = (w * ie->load_opts.dpi) / 90.0;
330 h = (h * ie->load_opts.dpi) / 90.0;
332 else if ((ie->load_opts.w > 0) && (ie->load_opts.h > 0))
334 unsigned int w2 = w, h2 = h;
335 /* user set load_opts' w,h on the assumption
336 that image already rotated according to it's orientation info */
339 load_opts_w = ie->load_opts.w;
340 load_opts_h = ie->load_opts.h;
341 ie->load_opts.w = load_opts_h;
342 ie->load_opts.h = load_opts_w;
345 if (ie->load_opts.w > 0)
347 w2 = ie->load_opts.w;
348 h2 = (ie->load_opts.w * h) / w;
349 if ((ie->load_opts.h > 0) && (h2 > ie->load_opts.h))
352 h2 = ie->load_opts.h;
353 w3 = (ie->load_opts.h * w) / h;
358 else if (ie->load_opts.h > 0)
360 h2 = ie->load_opts.h;
361 w2 = (ie->load_opts.h * w) / h;
367 ie->load_opts.w = load_opts_w;
368 ie->load_opts.h = load_opts_h;
374 if ((w != cinfo.output_width) || (h != cinfo.output_height))
376 scalew = cinfo.output_width / w;
377 scaleh = cinfo.output_height / h;
380 if (scaleh < scalew) ie->scale = scaleh;
382 if (ie->scale > 8) ie->scale = 8;
383 else if (ie->scale < 1) ie->scale = 1;
385 if (ie->scale == 3) ie->scale = 2;
386 else if (ie->scale == 5) ie->scale = 4;
387 else if (ie->scale == 6) ie->scale = 4;
388 else if (ie->scale == 7) ie->scale = 4;
393 jpeg_destroy_decompress(&cinfo);
394 _evas_jpeg_membuf_src_term(&cinfo);
395 jpeg_create_decompress(&cinfo);
397 if (_evas_jpeg_membuf_src(&cinfo, map, length))
399 jpeg_destroy_decompress(&cinfo);
400 _evas_jpeg_membuf_src_term(&cinfo);
401 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
405 jpeg_read_header(&cinfo, TRUE);
406 cinfo.do_fancy_upsampling = FALSE;
407 cinfo.do_block_smoothing = FALSE;
409 cinfo.scale_denom = ie->scale;
410 jpeg_calc_output_dimensions(&(cinfo));
411 jpeg_start_decompress(&cinfo);
414 ie->w = cinfo.output_width;
415 ie->h = cinfo.output_height;
417 // be nice and clip region to image. if its totally outside, fail load
418 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
420 unsigned int load_region_x = 0, load_region_y = 0;
421 unsigned int load_region_w = 0, load_region_h = 0;
422 if (ie->flags.rotated)
424 load_region_x = ie->load_opts.region.x;
425 load_region_y = ie->load_opts.region.y;
426 load_region_w = ie->load_opts.region.w;
427 load_region_h = ie->load_opts.region.h;
432 ie->load_opts.region.x = load_region_y;
433 ie->load_opts.region.y = h - (load_region_x + load_region_w);
434 ie->load_opts.region.w = load_region_h;
435 ie->load_opts.region.h = load_region_w;
438 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
439 ie->load_opts.region.y = h - (load_region_y + load_region_h);
443 ie->load_opts.region.x = w - (load_region_y + load_region_h);
444 ie->load_opts.region.y = load_region_x;
445 ie->load_opts.region.w = load_region_h;
446 ie->load_opts.region.h = load_region_w;
453 RECTS_CLIP_TO_RECT(ie->load_opts.region.x, ie->load_opts.region.y,
454 ie->load_opts.region.w, ie->load_opts.region.h,
456 if ((ie->load_opts.region.w <= 0) || (ie->load_opts.region.h <= 0))
458 jpeg_destroy_decompress(&cinfo);
459 _evas_jpeg_membuf_src_term(&cinfo);
460 *error = EVAS_LOAD_ERROR_GENERIC;
463 ie->w = ie->load_opts.region.w;
464 ie->h = ie->load_opts.region.h;
465 if (ie->flags.rotated)
467 ie->load_opts.region.x = load_region_x;
468 ie->load_opts.region.y = load_region_y;
469 ie->load_opts.region.w = load_region_w;
470 ie->load_opts.region.h = load_region_h;
473 /* end head decoding */
482 jpeg_destroy_decompress(&cinfo);
483 _evas_jpeg_membuf_src_term(&cinfo);
484 *error = EVAS_LOAD_ERROR_NONE;
492 struct timeval timev;
494 gettimeofday(&timev, NULL);
495 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
500 evas_image_load_file_data_jpeg_internal(Image_Entry *ie,
501 void *map, size_t size,
505 struct jpeg_decompress_struct cinfo;
506 struct _JPEG_error_mgr jerr;
507 DATA8 *ptr, *line[16], *data;
508 DATA32 *ptr2, *ptr_rotate = NULL;
509 unsigned int x, y, l, i, scans;
511 /* rotation setting */
512 unsigned int ie_w = 0, ie_h = 0;
513 unsigned int load_region_x = 0, load_region_y = 0;
514 unsigned int load_region_w = 0, load_region_h = 0;
515 volatile int degree = 0;
516 volatile Eina_Bool change_wh = EINA_FALSE;
517 Eina_Bool line_done = EINA_FALSE;
519 memset(&cinfo, 0, sizeof(cinfo));
520 if (ie->flags.rotated)
522 degree = ie->load_opts.degree;
523 if (degree == 90 || degree == 270)
524 change_wh = EINA_TRUE;
527 cinfo.err = jpeg_std_error(&(jerr.pub));
528 jerr.pub.error_exit = _JPEGFatalErrorHandler;
529 jerr.pub.emit_message = _JPEGErrorHandler2;
530 jerr.pub.output_message = _JPEGErrorHandler;
531 if (setjmp(jerr.setjmp_buffer))
533 jpeg_destroy_decompress(&cinfo);
534 _evas_jpeg_membuf_src_term(&cinfo);
535 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
538 jpeg_create_decompress(&cinfo);
540 if (_evas_jpeg_membuf_src(&cinfo, map, size))
542 jpeg_destroy_decompress(&cinfo);
543 _evas_jpeg_membuf_src_term(&cinfo);
544 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
548 jpeg_read_header(&cinfo, TRUE);
549 cinfo.do_fancy_upsampling = FALSE;
550 cinfo.do_block_smoothing = FALSE;
551 cinfo.dct_method = JDCT_ISLOW; // JDCT_FLOAT JDCT_IFAST(quality loss)
552 cinfo.dither_mode = JDITHER_ORDERED;
557 cinfo.scale_denom = ie->scale;
560 /* Colorspace conversion options */
561 /* libjpeg can do the following conversions: */
562 /* GRAYSCLAE => RGB YCbCr => RGB and YCCK => CMYK */
563 switch (cinfo.jpeg_color_space)
570 cinfo.out_color_space = JCS_RGB;
574 cinfo.out_color_space = JCS_CMYK;
579 jpeg_calc_output_dimensions(&(cinfo));
580 jpeg_start_decompress(&cinfo);
582 w = cinfo.output_width;
583 h = cinfo.output_height;
596 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
600 if (ie->flags.rotated)
602 load_region_x = ie->load_opts.region.x;
603 load_region_y = ie->load_opts.region.y;
604 load_region_w = ie->load_opts.region.w;
605 load_region_h = ie->load_opts.region.h;
610 ie->load_opts.region.x = load_region_y;
611 ie->load_opts.region.y = h - (load_region_x + load_region_w);
612 ie->load_opts.region.w = load_region_h;
613 ie->load_opts.region.h = load_region_w;
616 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
617 ie->load_opts.region.y = h - (load_region_y + load_region_h);
621 ie->load_opts.region.x = w - (load_region_y + load_region_h);
622 ie->load_opts.region.y = load_region_x;
623 ie->load_opts.region.w = load_region_h;
624 ie->load_opts.region.h = load_region_w;
631 #ifdef BUILD_LOADER_JPEG_REGION
632 cinfo.region_x = ie->load_opts.region.x;
633 cinfo.region_y = ie->load_opts.region.y;
634 cinfo.region_w = ie->load_opts.region.w;
635 cinfo.region_h = ie->load_opts.region.h;
638 if ((!region) && ((w != ie_w) || (h != ie_h)))
640 // race condition, the file could have change from when we call header
641 // this test will not solve the problem with region code.
642 jpeg_destroy_decompress(&cinfo);
643 _evas_jpeg_membuf_src_term(&cinfo);
644 *error = EVAS_LOAD_ERROR_GENERIC;
648 ((ie_w != ie->load_opts.region.w) || (ie_h != ie->load_opts.region.h)))
650 ie_w = ie->load_opts.region.w;
651 ie_h = ie->load_opts.region.h;
665 if (!(((cinfo.out_color_space == JCS_RGB) &&
666 ((cinfo.output_components == 3) || (cinfo.output_components == 1))) ||
667 ((cinfo.out_color_space == JCS_CMYK) && (cinfo.output_components == 4))))
669 jpeg_destroy_decompress(&cinfo);
670 _evas_jpeg_membuf_src_term(&cinfo);
671 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
675 /* end head decoding */
677 if (cinfo.rec_outbuf_height > 16)
679 jpeg_destroy_decompress(&cinfo);
680 _evas_jpeg_membuf_src_term(&cinfo);
681 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
684 data = alloca(w * 16 * cinfo.output_components);
685 evas_cache_image_surface_alloc(ie, ie->w, ie->h);
686 if (ie->flags.loaded)
688 jpeg_destroy_decompress(&cinfo);
689 _evas_jpeg_membuf_src_term(&cinfo);
690 *error = EVAS_LOAD_ERROR_NONE;
691 if (region && ie->flags.rotated)
693 ie->load_opts.region.x = load_region_x;
694 ie->load_opts.region.y = load_region_y;
695 ie->load_opts.region.w = load_region_w;
696 ie->load_opts.region.h = load_region_h;
700 if ((ie->flags.rotated) && change_wh)
702 ptr2 = malloc(ie->w * ie->h * sizeof(DATA32));
706 ptr2 = evas_cache_image_pixels(ie);
710 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
714 /* We handle first CMYK (4 components) */
715 if (cinfo.output_components == 4)
717 // FIXME: handle region
718 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
719 line[i] = data + (i * w * 4);
720 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
722 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
723 scans = cinfo.rec_outbuf_height;
724 if ((h - l) < scans) scans = h - l;
728 for (y = 0; y < scans; y++)
730 if (cinfo.saw_Adobe_marker)
732 for (x = 0; x < w; x++)
734 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
735 /* that is C is replaces by 255 - C, etc...*/
736 /* See the comment below for the computation of RGB values from CMYK ones. */
739 ((ptr[0] * ptr[3] / 255) << 16) |
740 ((ptr[1] * ptr[3] / 255) << 8) |
741 ((ptr[2] * ptr[3] / 255));
748 for (x = 0; x < w; x++)
750 /* Conversion from CMYK to RGB is done in 2 steps: */
751 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
752 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
753 /* R = (1 - C) * (1 - K) * 255 */
754 /* G = (1 - M) * (1 - K) * 255 */
755 /* B = (1 - Y) * (1 - K) * 255 */
756 /* libjpeg stores CMYK values between 0 and 255, */
757 /* so we replace C by C * 255 / 255, etc... and we obtain: */
758 /* R = (255 - C) * (255 - K) / 255 */
759 /* G = (255 - M) * (255 - K) / 255 */
760 /* B = (255 - Y) * (255 - K) / 255 */
761 /* with C, M, Y and K between 0 and 255. */
764 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
765 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
766 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
775 // if line # > region last line, break
776 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
778 line_done = EINA_TRUE;
779 /* if rotation flag is set , we have to rotate image */
781 /*jpeg_destroy_decompress(&cinfo);
782 _evas_jpeg_membuf_src_term(&cinfo);
783 *error = EVAS_LOAD_ERROR_NONE;
786 // els if scan block intersects region start or later
787 else if ((l + scans) >
788 (ie->load_opts.region.y))
790 for (y = 0; y < scans; y++)
792 if (((y + l) >= ie->load_opts.region.y) &&
793 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
795 ptr += ie->load_opts.region.x;
796 if (cinfo.saw_Adobe_marker)
798 for (x = 0; x < ie->load_opts.region.w; x++)
800 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
801 /* that is C is replaces by 255 - C, etc...*/
802 /* See the comment below for the computation of RGB values from CMYK ones. */
805 ((ptr[0] * ptr[3] / 255) << 16) |
806 ((ptr[1] * ptr[3] / 255) << 8) |
807 ((ptr[2] * ptr[3] / 255));
814 for (x = 0; x < ie->load_opts.region.w; x++)
816 /* Conversion from CMYK to RGB is done in 2 steps: */
817 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
818 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
819 /* R = (1 - C) * (1 - K) * 255 */
820 /* G = (1 - M) * (1 - K) * 255 */
821 /* B = (1 - Y) * (1 - K) * 255 */
822 /* libjpeg stores CMYK values between 0 and 255, */
823 /* so we replace C by C * 255 / 255, etc... and we obtain: */
824 /* R = (255 - C) * (255 - K) / 255 */
825 /* G = (255 - M) * (255 - K) / 255 */
826 /* B = (255 - Y) * (255 - K) / 255 */
827 /* with C, M, Y and K between 0 and 255. */
830 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
831 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
832 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
837 ptr += (4 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
846 /* We handle then RGB with 3 components */
847 else if (cinfo.output_components == 3)
854 printf("R| %p %5ix%5i %s: %5i %5i %5ix%5i - ",
858 ie->load_opts.region.x,
859 ie->load_opts.region.y,
860 ie->load_opts.region.w,
861 ie->load_opts.region.h);
865 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
866 line[i] = data + (i * w * 3);
867 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
869 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
870 scans = cinfo.rec_outbuf_height;
871 if ((h - l) < scans) scans = h - l;
875 for (y = 0; y < scans; y++)
877 for (x = 0; x < w; x++)
879 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
887 // if line # > region last line, break
888 // but not return immediately for rotation job
889 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
891 line_done = EINA_TRUE;
892 /* if rotation flag is set , we have to rotate image */
895 // else if scan block intersects region start or later
896 else if ((l + scans) >
897 (ie->load_opts.region.y))
899 for (y = 0; y < scans; y++)
901 if (((y + l) >= ie->load_opts.region.y) &&
902 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
904 ptr += (3 * ie->load_opts.region.x);
905 for (x = 0; x < ie->load_opts.region.w; x++)
907 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
911 ptr += (3 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
921 printf("%3.3f\n", t);
924 /* We finally handle RGB with 1 component */
925 else if (cinfo.output_components == 1)
927 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
928 line[i] = data + (i * w);
929 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
931 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
932 scans = cinfo.rec_outbuf_height;
933 if ((h - l) < scans) scans = h - l;
937 for (y = 0; y < scans; y++)
939 for (x = 0; x < w; x++)
941 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
949 // if line # > region last line, break
950 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
952 line_done = EINA_TRUE;
953 /* if rotation flag is set , we have to rotate image */
955 /*jpeg_destroy_decompress(&cinfo);
956 _evas_jpeg_membuf_src_term(&cinfo);
957 *error = EVAS_LOAD_ERROR_NONE;
960 // els if scan block intersects region start or later
961 else if ((l + scans) >
962 (ie->load_opts.region.y))
964 for (y = 0; y < scans; y++)
966 if (((y + l) >= ie->load_opts.region.y) &&
967 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
969 ptr += ie->load_opts.region.x;
970 for (x = 0; x < ie->load_opts.region.w; x++)
972 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
976 ptr += w - (ie->load_opts.region.x + ie->load_opts.region.w);
985 /* if rotation operation need, rotate it */
988 if (ie->flags.rotated)
990 DATA32 *data1, *data2, *to, *from;
991 int lx, ly, lw, lh, hw;
997 data1 = evas_cache_image_pixels(ie);
1003 data2 = data1 + (lh * lw) -1;
1004 for (lx = (lw * lh) / 2; --lx >= 0;)
1017 if (ptr_rotate) data2 = ptr_rotate;
1021 to = data1 + lw - 1;
1024 else if (degree == 270)
1026 to = data1 + hw - lw;
1034 for (lx = ie->w; --lx >= 0;)
1036 for (ly =ie->h; --ly >= 0;)
1053 ie->load_opts.region.x = load_region_x;
1054 ie->load_opts.region.y = load_region_y;
1055 ie->load_opts.region.w = load_region_w;
1056 ie->load_opts.region.h = load_region_h;
1062 jpeg_destroy_decompress(&cinfo);
1063 _evas_jpeg_membuf_src_term(&cinfo);
1064 *error = EVAS_LOAD_ERROR_NONE;
1067 /* end data decoding */
1068 jpeg_finish_decompress(&cinfo);
1069 jpeg_destroy_decompress(&cinfo);
1070 _evas_jpeg_membuf_src_term(&cinfo);
1071 *error = EVAS_LOAD_ERROR_NONE;
1075 #if 0 /* not used at the moment */
1077 evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f, int *error)
1080 struct jpeg_decompress_struct cinfo;
1081 struct _JPEG_error_mgr jerr;
1082 DATA8 *ptr, *line[16], *data;
1084 int x, y, l, i, scans, prevy;
1088 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1091 cinfo.err = jpeg_std_error(&(jerr.pub));
1092 jerr.pub.error_exit = _JPEGFatalErrorHandler;
1093 jerr.pub.emit_message = _JPEGErrorHandler2;
1094 jerr.pub.output_message = _JPEGErrorHandler;
1095 if (setjmp(jerr.setjmp_buffer))
1097 jpeg_destroy_decompress(&cinfo);
1098 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
1101 jpeg_create_decompress(&cinfo);
1102 jpeg_stdio_src(&cinfo, f);
1103 jpeg_read_header(&cinfo, TRUE);
1104 cinfo.do_fancy_upsampling = FALSE;
1105 cinfo.do_block_smoothing = FALSE;
1106 jpeg_start_decompress(&cinfo);
1109 ie->w = w = cinfo.output_width;
1110 ie->h = h = cinfo.output_height;
1111 /* end head decoding */
1113 if (cinfo.rec_outbuf_height > 16)
1115 jpeg_destroy_decompress(&cinfo);
1116 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
1119 data = alloca(w * 16 * 3);
1120 if (!ie->flags.loaded)
1122 jpeg_destroy_decompress(&cinfo);
1123 *error = EVAS_LOAD_ERROR_NONE;
1126 ptr2 = evas_cache_image_pixels(ie);
1128 if (cinfo.output_components == 3)
1130 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1131 line[i] = data + (i * w * 3);
1132 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1134 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1135 scans = cinfo.rec_outbuf_height;
1136 if ((h - l) < scans) scans = h - l;
1138 for (y = 0; y < scans; y++)
1140 for (x = 0; x < w; x++)
1143 ((*ptr2) & 0x00ffffff) |
1144 (((ptr[0] + ptr[1] + ptr[2]) / 3) << 24);
1151 else if (cinfo.output_components == 1)
1153 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1154 line[i] = data + (i * w);
1155 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1157 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1158 scans = cinfo.rec_outbuf_height;
1159 if ((h - l) < scans) scans = h - l;
1161 for (y = 0; y < scans; y++)
1163 for (x = 0; x < w; x++)
1166 ((*ptr2) & 0x00ffffff) |
1174 /* end data decoding */
1175 jpeg_finish_decompress(&cinfo);
1176 jpeg_destroy_decompress(&cinfo);
1177 *error = EVAS_LOAD_ERROR_NONE;
1183 evas_image_load_file_head_jpeg(Image_Entry *ie,
1184 const char *file, const char *key __UNUSED__,
1189 Eina_Bool val = EINA_FALSE;
1191 f = eina_file_open(file, EINA_FALSE);
1194 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1197 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1200 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1204 val = evas_image_load_file_head_jpeg_internal(ie,
1205 map, eina_file_size_get(f),
1208 eina_file_map_free(f, map);
1216 evas_image_load_file_data_jpeg(Image_Entry *ie,
1217 const char *file, const char *key __UNUSED__,
1222 Eina_Bool val = EINA_FALSE;
1224 f = eina_file_open(file, EINA_FALSE);
1227 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1230 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1233 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1237 val = evas_image_load_file_data_jpeg_internal(ie,
1238 map, eina_file_size_get(f),
1241 eina_file_map_free(f, map);
1249 module_open(Evas_Module *em)
1252 em->functions = (void *)(&evas_image_load_jpeg_func);
1257 module_close(Evas_Module *em __UNUSED__)
1261 static Evas_Module_Api evas_modapi =
1263 EVAS_MODULE_API_VERSION,
1272 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, jpeg);
1274 #ifndef EVAS_STATIC_BUILD_JPEG
1275 EVAS_EINA_MODULE_DEFINE(image_loader, jpeg);