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, 3);
33 static Eina_Bool evas_image_load_file_data_jpeg_internal(Image_Entry *ie,
36 int *error) EINA_ARG_NONNULL(1, 2, 3);
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,
54 _JPEGFatalErrorHandler(j_common_ptr cinfo)
58 errmgr = (emptr) cinfo->err;
59 /* cinfo->err->output_message(cinfo);*/
60 longjmp(errmgr->setjmp_buffer, 1);
65 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
69 /* errmgr = (emptr) cinfo->err; */
70 /* cinfo->err->output_message(cinfo);*/
71 /* longjmp(errmgr->setjmp_buffer, 1);*/
76 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__, int msg_level __UNUSED__)
80 /* errmgr = (emptr) cinfo->err; */
81 /* cinfo->err->output_message(cinfo);*/
82 /* longjmp(errmgr->setjmp_buffer, 1);*/
86 struct jpeg_membuf_src
88 struct jpeg_source_mgr pub;
90 const unsigned char *buf;
92 struct jpeg_membuf_src *self;
96 _evas_jpeg_membuf_src_init(j_decompress_ptr cinfo __UNUSED__)
101 _evas_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
103 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
104 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
106 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
107 src->pub.next_input_byte = jpeg_eoi;
113 _evas_jpeg_membuf_src_skip(j_decompress_ptr cinfo,
116 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
118 if ((((long)src->pub.bytes_in_buffer - (long)src->len) > num_bytes) ||
119 ((long)src->pub.bytes_in_buffer < num_bytes))
121 (*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo));
124 src->pub.bytes_in_buffer -= num_bytes;
125 src->pub.next_input_byte += num_bytes;
129 _evas_jpeg_membuf_src_term(j_decompress_ptr cinfo)
131 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
138 _evas_jpeg_membuf_src(j_decompress_ptr cinfo,
139 void *map, size_t length)
141 struct jpeg_membuf_src *src;
143 src = calloc(1, sizeof(*src));
149 cinfo->src = &src->pub;
152 src->pub.init_source = _evas_jpeg_membuf_src_init;
153 src->pub.fill_input_buffer = _evas_jpeg_membuf_src_fill;
154 src->pub.skip_input_data = _evas_jpeg_membuf_src_skip;
155 src->pub.resync_to_restart = jpeg_resync_to_restart;
156 src->pub.term_source = _evas_jpeg_membuf_src_term;
157 src->pub.bytes_in_buffer = src->len;
158 src->pub.next_input_byte = src->buf;
163 /*! Magic number for EXIF header & App1*/
164 static const unsigned char ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
165 static const unsigned char App1[] = {0xff, 0xe1};
172 _get_orientation(void *map, size_t length)
176 ExifByteAlign byte_align;
177 unsigned int num_directory = 0;
181 /* open file and get 22 byte frome file */
184 if (length < 22) return 0;
187 /* 2. check 2,3 bypte with APP1(0xFFE1) */
188 if (memcmp(buf + 2, App1, sizeof (App1))) return 0;
190 /* 3. check 6~11bype with Exif Header (0x45786966 0000) */
191 if (memcmp(buf + 6, ExifHeader, sizeof (ExifHeader))) return 0;
193 /* 4. get 12&13 byte get info of "II(0x4949)" or "MM(0x4d4d)" */
194 /* 5. get [20]&[21] get directory entry # */
195 if (!strncmp(buf + 12, "MM", 2))
197 byte_align = EXIF_BYTE_ALIGN_MM;
198 num_directory = ((*(buf + 20) << 8) + *(buf + 21));
199 orientation[0] = 0x01;
200 orientation[1] = 0x12;
202 else if (!strncmp(buf + 12, "II", 2))
204 byte_align = EXIF_BYTE_ALIGN_II;
205 num_directory = ((*(buf + 21) << 8) + *(buf + 20));
206 orientation[0] = 0x12;
207 orientation[1] = 0x01;
213 if (length < (12 * num_directory + 22)) return 0;
217 for (i = 0; i < num_directory; i++ )
219 if (!strncmp(buf + j, orientation, 2))
221 /*get orientation tag */
222 if (byte_align == EXIF_BYTE_ALIGN_MM)
223 direction = *(buf+ j + 11);
224 else direction = *(buf+ j + 8);
247 evas_image_load_file_head_jpeg_internal(Image_Entry *ie,
248 void *map, size_t length,
251 unsigned int w, h, scalew, scaleh;
252 struct jpeg_decompress_struct cinfo;
253 struct _JPEG_error_mgr jerr;
255 /* for rotation decoding */
257 Eina_Bool change_wh = EINA_FALSE;
258 unsigned int load_opts_w = 0, load_opts_h = 0;
260 cinfo.err = jpeg_std_error(&(jerr.pub));
261 jerr.pub.error_exit = _JPEGFatalErrorHandler;
262 jerr.pub.emit_message = _JPEGErrorHandler2;
263 jerr.pub.output_message = _JPEGErrorHandler;
264 if (setjmp(jerr.setjmp_buffer))
266 jpeg_destroy_decompress(&cinfo);
267 _evas_jpeg_membuf_src_term(&cinfo);
268 if (cinfo.saw_JFIF_marker)
269 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
271 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
274 jpeg_create_decompress(&cinfo);
276 if (_evas_jpeg_membuf_src(&cinfo, map, length))
278 jpeg_destroy_decompress(&cinfo);
279 _evas_jpeg_membuf_src_term(&cinfo);
280 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
284 jpeg_read_header(&cinfo, TRUE);
285 cinfo.do_fancy_upsampling = FALSE;
286 cinfo.do_block_smoothing = FALSE;
287 cinfo.dct_method = JDCT_IFAST;
288 cinfo.dither_mode = JDITHER_ORDERED;
289 jpeg_start_decompress(&cinfo);
291 /* rotation decoding */
292 if (ie->load_opts.orientation)
294 degree = _get_orientation(map, length);
297 ie->load_opts.degree = degree;
298 ie->flags.rotated = EINA_TRUE;
300 if (degree == 90 || degree == 270)
301 change_wh = EINA_TRUE;
307 w = cinfo.output_width;
308 h = cinfo.output_height;
309 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
312 jpeg_destroy_decompress(&cinfo);
313 _evas_jpeg_membuf_src_term(&cinfo);
314 if (IMG_TOO_BIG(w, h))
315 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
317 *error = EVAS_LOAD_ERROR_GENERIC;
320 if (ie->load_opts.scale_down_by > 1)
322 w /= ie->load_opts.scale_down_by;
323 h /= ie->load_opts.scale_down_by;
325 else if (ie->load_opts.dpi > 0.0)
327 w = (w * ie->load_opts.dpi) / 90.0;
328 h = (h * ie->load_opts.dpi) / 90.0;
330 else if ((ie->load_opts.w > 0) && (ie->load_opts.h > 0))
332 unsigned int w2 = w, h2 = h;
333 /* user set load_opts' w,h on the assumption
334 that image already rotated according to it's orientation info */
337 load_opts_w = ie->load_opts.w;
338 load_opts_h = ie->load_opts.h;
339 ie->load_opts.w = load_opts_h;
340 ie->load_opts.h = load_opts_w;
343 if (ie->load_opts.w > 0)
345 w2 = ie->load_opts.w;
346 h2 = (ie->load_opts.w * h) / w;
347 if ((ie->load_opts.h > 0) && (h2 > ie->load_opts.h))
350 h2 = ie->load_opts.h;
351 w3 = (ie->load_opts.h * w) / h;
356 else if (ie->load_opts.h > 0)
358 h2 = ie->load_opts.h;
359 w2 = (ie->load_opts.h * w) / h;
365 ie->load_opts.w = load_opts_w;
366 ie->load_opts.h = load_opts_h;
372 if ((w != cinfo.output_width) || (h != cinfo.output_height))
374 scalew = cinfo.output_width / w;
375 scaleh = cinfo.output_height / h;
378 if (scaleh < scalew) ie->scale = scaleh;
380 if (ie->scale > 8) ie->scale = 8;
381 else if (ie->scale < 1) ie->scale = 1;
383 if (ie->scale == 3) ie->scale = 2;
384 else if (ie->scale == 5) ie->scale = 4;
385 else if (ie->scale == 6) ie->scale = 4;
386 else if (ie->scale == 7) ie->scale = 4;
391 jpeg_destroy_decompress(&cinfo);
392 _evas_jpeg_membuf_src_term(&cinfo);
393 jpeg_create_decompress(&cinfo);
395 if (_evas_jpeg_membuf_src(&cinfo, map, length))
397 jpeg_destroy_decompress(&cinfo);
398 _evas_jpeg_membuf_src_term(&cinfo);
399 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
403 jpeg_read_header(&cinfo, TRUE);
404 cinfo.do_fancy_upsampling = FALSE;
405 cinfo.do_block_smoothing = FALSE;
407 cinfo.scale_denom = ie->scale;
408 jpeg_calc_output_dimensions(&(cinfo));
409 jpeg_start_decompress(&cinfo);
412 ie->w = cinfo.output_width;
413 ie->h = cinfo.output_height;
415 // be nice and clip region to image. if its totally outside, fail load
416 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
418 unsigned int load_region_x = 0, load_region_y = 0;
419 unsigned int load_region_w = 0, load_region_h = 0;
420 if (ie->flags.rotated)
422 load_region_x = ie->load_opts.region.x;
423 load_region_y = ie->load_opts.region.y;
424 load_region_w = ie->load_opts.region.w;
425 load_region_h = ie->load_opts.region.h;
430 ie->load_opts.region.x = load_region_y;
431 ie->load_opts.region.y = h - (load_region_x + load_region_w);
432 ie->load_opts.region.w = load_region_h;
433 ie->load_opts.region.h = load_region_w;
436 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
437 ie->load_opts.region.y = h - (load_region_y + load_region_h);
441 ie->load_opts.region.x = w - (load_region_y + load_region_h);
442 ie->load_opts.region.y = load_region_x;
443 ie->load_opts.region.w = load_region_h;
444 ie->load_opts.region.h = load_region_w;
451 RECTS_CLIP_TO_RECT(ie->load_opts.region.x, ie->load_opts.region.y,
452 ie->load_opts.region.w, ie->load_opts.region.h,
454 if ((ie->load_opts.region.w <= 0) || (ie->load_opts.region.h <= 0))
456 jpeg_destroy_decompress(&cinfo);
457 _evas_jpeg_membuf_src_term(&cinfo);
458 *error = EVAS_LOAD_ERROR_GENERIC;
461 ie->w = ie->load_opts.region.w;
462 ie->h = ie->load_opts.region.h;
463 if (ie->flags.rotated)
465 ie->load_opts.region.x = load_region_x;
466 ie->load_opts.region.y = load_region_y;
467 ie->load_opts.region.w = load_region_w;
468 ie->load_opts.region.h = load_region_h;
471 /* end head decoding */
480 jpeg_destroy_decompress(&cinfo);
481 _evas_jpeg_membuf_src_term(&cinfo);
482 *error = EVAS_LOAD_ERROR_NONE;
490 struct timeval timev;
492 gettimeofday(&timev, NULL);
493 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
498 evas_image_load_file_data_jpeg_internal(Image_Entry *ie,
499 void *map, size_t size,
503 struct jpeg_decompress_struct cinfo;
504 struct _JPEG_error_mgr jerr;
505 DATA8 *ptr, *line[16], *data;
506 DATA32 *ptr2,*ptr_rotate;
507 unsigned int x, y, l, i, scans;
510 /* rotation setting */
512 unsigned int load_region_x = 0, load_region_y = 0;
513 unsigned int load_region_w = 0, load_region_h = 0;
515 Eina_Bool change_wh = EINA_FALSE;
516 Eina_Bool line_done = EINA_FALSE;
518 if (ie->flags.rotated)
520 degree = ie->load_opts.degree;
521 if (degree == 90 || degree == 270)
522 change_wh = EINA_TRUE;
525 cinfo.err = jpeg_std_error(&(jerr.pub));
526 jerr.pub.error_exit = _JPEGFatalErrorHandler;
527 jerr.pub.emit_message = _JPEGErrorHandler2;
528 jerr.pub.output_message = _JPEGErrorHandler;
529 if (setjmp(jerr.setjmp_buffer))
531 jpeg_destroy_decompress(&cinfo);
532 _evas_jpeg_membuf_src_term(&cinfo);
533 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
536 jpeg_create_decompress(&cinfo);
538 if (_evas_jpeg_membuf_src(&cinfo, map, size))
540 jpeg_destroy_decompress(&cinfo);
541 _evas_jpeg_membuf_src_term(&cinfo);
542 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
546 jpeg_read_header(&cinfo, TRUE);
547 cinfo.do_fancy_upsampling = FALSE;
548 cinfo.do_block_smoothing = FALSE;
549 cinfo.dct_method = JDCT_IFAST;
550 cinfo.dither_mode = JDITHER_ORDERED;
555 cinfo.scale_denom = ie->scale;
558 /* Colorspace conversion options */
559 /* libjpeg can do the following conversions: */
560 /* GRAYSCLAE => RGB YCbCr => RGB and YCCK => CMYK */
561 switch (cinfo.jpeg_color_space)
568 cinfo.out_color_space = JCS_RGB;
572 cinfo.out_color_space = JCS_CMYK;
577 jpeg_calc_output_dimensions(&(cinfo));
578 jpeg_start_decompress(&cinfo);
580 w = cinfo.output_width;
581 h = cinfo.output_height;
590 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
594 if (ie->flags.rotated)
596 load_region_x = ie->load_opts.region.x;
597 load_region_y = ie->load_opts.region.y;
598 load_region_w = ie->load_opts.region.w;
599 load_region_h = ie->load_opts.region.h;
604 ie->load_opts.region.x = load_region_y;
605 ie->load_opts.region.y = h - (load_region_x + load_region_w);
606 ie->load_opts.region.w = load_region_h;
607 ie->load_opts.region.h = load_region_w;
610 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
611 ie->load_opts.region.y = h - (load_region_y + load_region_h);
615 ie->load_opts.region.x = w - (load_region_y + load_region_h);
616 ie->load_opts.region.y = load_region_x;
617 ie->load_opts.region.w = load_region_h;
618 ie->load_opts.region.h = load_region_w;
625 #ifdef BUILD_LOADER_JPEG_REGION
626 cinfo.region_x = ie->load_opts.region.x;
627 cinfo.region_y = ie->load_opts.region.y;
628 cinfo.region_w = ie->load_opts.region.w;
629 cinfo.region_h = ie->load_opts.region.h;
632 if ((!region) && ((w != ie->w) || (h != ie->h)))
634 // race condition, the file could have change from when we call header
635 // this test will not solve the problem with region code.
636 jpeg_destroy_decompress(&cinfo);
637 _evas_jpeg_membuf_src_term(&cinfo);
638 *error = EVAS_LOAD_ERROR_GENERIC;
642 ((ie->w != ie->load_opts.region.w) || (ie->h != ie->load_opts.region.h)))
644 ie->w = ie->load_opts.region.w;
645 ie->h = ie->load_opts.region.h;
648 if (!(((cinfo.out_color_space == JCS_RGB) &&
649 ((cinfo.output_components == 3) || (cinfo.output_components == 1))) ||
650 ((cinfo.out_color_space == JCS_CMYK) && (cinfo.output_components == 4))))
652 jpeg_destroy_decompress(&cinfo);
653 _evas_jpeg_membuf_src_term(&cinfo);
654 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
658 /* end head decoding */
660 if (cinfo.rec_outbuf_height > 16)
662 jpeg_destroy_decompress(&cinfo);
663 _evas_jpeg_membuf_src_term(&cinfo);
664 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
667 data = alloca(w * 16 * cinfo.output_components);
668 evas_cache_image_surface_alloc(ie, ie->w, ie->h);
669 if (ie->flags.loaded)
671 jpeg_destroy_decompress(&cinfo);
672 _evas_jpeg_membuf_src_term(&cinfo);
673 *error = EVAS_LOAD_ERROR_NONE;
674 if (region && ie->flags.rotated)
676 ie->load_opts.region.x = load_region_x;
677 ie->load_opts.region.y = load_region_y;
678 ie->load_opts.region.w = load_region_w;
679 ie->load_opts.region.h = load_region_h;
683 if ((ie->flags.rotated) && change_wh)
685 ptr2 = malloc(ie->w * ie->h * sizeof(DATA32));
689 ptr2 = evas_cache_image_pixels(ie);
693 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
697 /* We handle first CMYK (4 components) */
698 if (cinfo.output_components == 4)
700 // FIXME: handle region
701 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
702 line[i] = data + (i * w * 4);
703 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
705 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
706 scans = cinfo.rec_outbuf_height;
707 if ((h - l) < scans) scans = h - l;
711 for (y = 0; y < scans; y++)
713 if (cinfo.saw_Adobe_marker)
715 for (x = 0; x < w; x++)
717 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
718 /* that is C is replaces by 255 - C, etc...*/
719 /* See the comment below for the computation of RGB values from CMYK ones. */
722 ((ptr[0] * ptr[3] / 255) << 16) |
723 ((ptr[1] * ptr[3] / 255) << 8) |
724 ((ptr[2] * ptr[3] / 255));
731 for (x = 0; x < w; x++)
733 /* Conversion from CMYK to RGB is done in 2 steps: */
734 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
735 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
736 /* R = (1 - C) * (1 - K) * 255 */
737 /* G = (1 - M) * (1 - K) * 255 */
738 /* B = (1 - Y) * (1 - K) * 255 */
739 /* libjpeg stores CMYK values between 0 and 255, */
740 /* so we replace C by C * 255 / 255, etc... and we obtain: */
741 /* R = (255 - C) * (255 - K) / 255 */
742 /* G = (255 - M) * (255 - K) / 255 */
743 /* B = (255 - Y) * (255 - K) / 255 */
744 /* with C, M, Y and K between 0 and 255. */
747 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
748 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
749 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
758 // if line # > region last line, break
759 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
761 line_done = EINA_TRUE;
762 /* if rotation flag is set , we have to rotate image */
764 /*jpeg_destroy_decompress(&cinfo);
765 _evas_jpeg_membuf_src_term(&cinfo);
766 *error = EVAS_LOAD_ERROR_NONE;
769 // els if scan block intersects region start or later
770 else if ((l + scans) >
771 (ie->load_opts.region.y))
773 for (y = 0; y < scans; y++)
775 if (((y + l) >= ie->load_opts.region.y) &&
776 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
778 ptr += ie->load_opts.region.x;
779 if (cinfo.saw_Adobe_marker)
781 for (x = 0; x < ie->load_opts.region.w; x++)
783 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
784 /* that is C is replaces by 255 - C, etc...*/
785 /* See the comment below for the computation of RGB values from CMYK ones. */
788 ((ptr[0] * ptr[3] / 255) << 16) |
789 ((ptr[1] * ptr[3] / 255) << 8) |
790 ((ptr[2] * ptr[3] / 255));
797 for (x = 0; x < ie->load_opts.region.w; x++)
799 /* Conversion from CMYK to RGB is done in 2 steps: */
800 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
801 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
802 /* R = (1 - C) * (1 - K) * 255 */
803 /* G = (1 - M) * (1 - K) * 255 */
804 /* B = (1 - Y) * (1 - K) * 255 */
805 /* libjpeg stores CMYK values between 0 and 255, */
806 /* so we replace C by C * 255 / 255, etc... and we obtain: */
807 /* R = (255 - C) * (255 - K) / 255 */
808 /* G = (255 - M) * (255 - K) / 255 */
809 /* B = (255 - Y) * (255 - K) / 255 */
810 /* with C, M, Y and K between 0 and 255. */
813 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
814 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
815 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
820 ptr += (4 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
829 /* We handle then RGB with 3 components */
830 else if (cinfo.output_components == 3)
837 printf("R| %p %5ix%5i %s: %5i %5i %5ix%5i - ",
841 ie->load_opts.region.x,
842 ie->load_opts.region.y,
843 ie->load_opts.region.w,
844 ie->load_opts.region.h);
848 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
849 line[i] = data + (i * w * 3);
850 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
852 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
853 scans = cinfo.rec_outbuf_height;
854 if ((h - l) < scans) scans = h - l;
858 for (y = 0; y < scans; y++)
860 for (x = 0; x < w; x++)
862 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
870 // if line # > region last line, break
871 // but not return immediately for rotation job
872 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
874 line_done = EINA_TRUE;
875 /* if rotation flag is set , we have to rotate image */
878 // else if scan block intersects region start or later
879 else if ((l + scans) >
880 (ie->load_opts.region.y))
882 for (y = 0; y < scans; y++)
884 if (((y + l) >= ie->load_opts.region.y) &&
885 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
887 ptr += (3 * ie->load_opts.region.x);
888 for (x = 0; x < ie->load_opts.region.w; x++)
890 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
894 ptr += (3 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
904 printf("%3.3f\n", t);
907 /* We finally handle RGB with 1 component */
908 else if (cinfo.output_components == 1)
910 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
911 line[i] = data + (i * w);
912 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
914 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
915 scans = cinfo.rec_outbuf_height;
916 if ((h - l) < scans) scans = h - l;
920 for (y = 0; y < scans; y++)
922 for (x = 0; x < w; x++)
924 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
932 // if line # > region last line, break
933 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
935 line_done = EINA_TRUE;
936 /* if rotation flag is set , we have to rotate image */
938 /*jpeg_destroy_decompress(&cinfo);
939 _evas_jpeg_membuf_src_term(&cinfo);
940 *error = EVAS_LOAD_ERROR_NONE;
943 // els if scan block intersects region start or later
944 else if ((l + scans) >
945 (ie->load_opts.region.y))
947 for (y = 0; y < scans; y++)
949 if (((y + l) >= ie->load_opts.region.y) &&
950 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
952 ptr += ie->load_opts.region.x;
953 for (x = 0; x < ie->load_opts.region.w; x++)
955 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
959 ptr += w - (ie->load_opts.region.x + ie->load_opts.region.w);
968 /* if rotation operation need, rotate it */
971 if (ie->flags.rotated)
973 DATA32 *data1, *data2, *to, *from;
987 data1 = evas_cache_image_pixels(ie);
993 data2 = data1 + (h * w) -1;
994 for (x = (w * h) / 2; --x >= 0;)
1013 else if (degree == 270)
1015 to = data1 + hw - w;
1021 for (x = ie->w; --x >= 0;)
1023 for (y =ie->h; --y >= 0;)
1040 ie->load_opts.region.x = load_region_x;
1041 ie->load_opts.region.y = load_region_y;
1042 ie->load_opts.region.w = load_region_w;
1043 ie->load_opts.region.h = load_region_h;
1048 jpeg_destroy_decompress(&cinfo);
1049 _evas_jpeg_membuf_src_term(&cinfo);
1050 *error = EVAS_LOAD_ERROR_NONE;
1053 /* end data decoding */
1054 jpeg_finish_decompress(&cinfo);
1055 jpeg_destroy_decompress(&cinfo);
1056 _evas_jpeg_membuf_src_term(&cinfo);
1057 *error = EVAS_LOAD_ERROR_NONE;
1061 #if 0 /* not used at the moment */
1063 evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f, int *error)
1066 struct jpeg_decompress_struct cinfo;
1067 struct _JPEG_error_mgr jerr;
1068 DATA8 *ptr, *line[16], *data;
1070 int x, y, l, i, scans, prevy;
1074 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1077 cinfo.err = jpeg_std_error(&(jerr.pub));
1078 jerr.pub.error_exit = _JPEGFatalErrorHandler;
1079 jerr.pub.emit_message = _JPEGErrorHandler2;
1080 jerr.pub.output_message = _JPEGErrorHandler;
1081 if (setjmp(jerr.setjmp_buffer))
1083 jpeg_destroy_decompress(&cinfo);
1084 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
1087 jpeg_create_decompress(&cinfo);
1088 jpeg_stdio_src(&cinfo, f);
1089 jpeg_read_header(&cinfo, TRUE);
1090 cinfo.do_fancy_upsampling = FALSE;
1091 cinfo.do_block_smoothing = FALSE;
1092 jpeg_start_decompress(&cinfo);
1095 ie->w = w = cinfo.output_width;
1096 ie->h = h = cinfo.output_height;
1097 /* end head decoding */
1099 if (cinfo.rec_outbuf_height > 16)
1101 jpeg_destroy_decompress(&cinfo);
1102 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
1105 data = alloca(w * 16 * 3);
1106 if (!ie->flags.loaded)
1108 jpeg_destroy_decompress(&cinfo);
1109 *error = EVAS_LOAD_ERROR_NONE;
1112 ptr2 = evas_cache_image_pixels(ie);
1114 if (cinfo.output_components == 3)
1116 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1117 line[i] = data + (i * w * 3);
1118 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1120 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1121 scans = cinfo.rec_outbuf_height;
1122 if ((h - l) < scans) scans = h - l;
1124 for (y = 0; y < scans; y++)
1126 for (x = 0; x < w; x++)
1129 ((*ptr2) & 0x00ffffff) |
1130 (((ptr[0] + ptr[1] + ptr[2]) / 3) << 24);
1137 else if (cinfo.output_components == 1)
1139 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1140 line[i] = data + (i * w);
1141 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1143 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1144 scans = cinfo.rec_outbuf_height;
1145 if ((h - l) < scans) scans = h - l;
1147 for (y = 0; y < scans; y++)
1149 for (x = 0; x < w; x++)
1152 ((*ptr2) & 0x00ffffff) |
1160 /* end data decoding */
1161 jpeg_finish_decompress(&cinfo);
1162 jpeg_destroy_decompress(&cinfo);
1163 *error = EVAS_LOAD_ERROR_NONE;
1169 evas_image_load_file_head_jpeg(Image_Entry *ie,
1170 const char *file, const char *key __UNUSED__,
1175 Eina_Bool val = EINA_FALSE;
1177 f = eina_file_open(file, EINA_FALSE);
1180 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1183 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1186 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1190 val = evas_image_load_file_head_jpeg_internal(ie,
1191 map, eina_file_size_get(f),
1194 eina_file_map_free(f, map);
1202 evas_image_load_file_data_jpeg(Image_Entry *ie,
1203 const char *file, const char *key __UNUSED__,
1208 Eina_Bool val = EINA_FALSE;
1210 f = eina_file_open(file, EINA_FALSE);
1213 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1216 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1219 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1223 val = evas_image_load_file_data_jpeg_internal(ie,
1224 map, eina_file_size_get(f),
1227 eina_file_map_free(f, map);
1235 module_open(Evas_Module *em)
1238 em->functions = (void *)(&evas_image_load_jpeg_func);
1243 module_close(Evas_Module *em __UNUSED__)
1247 static Evas_Module_Api evas_modapi =
1249 EVAS_MODULE_API_VERSION,
1258 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, jpeg);
1260 #ifndef EVAS_STATIC_BUILD_JPEG
1261 EVAS_EINA_MODULE_DEFINE(image_loader, jpeg);