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 unsigned char orientation[2];
177 ExifByteAlign byte_align;
178 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;
217 /* we get all info from file, close file first */
221 for (i = 0; i < num_directory; i++ )
223 if (!strncmp(buf + j, orientation, 2))
225 /*get orientation tag */
226 if (byte_align == EXIF_BYTE_ALIGN_MM)
227 direction = *(buf+ j + 11);
228 else direction = *(buf+ j + 8);
251 evas_image_load_file_head_jpeg_internal(Image_Entry *ie,
252 void *map, size_t length,
255 unsigned int w, h, scalew, scaleh;
256 struct jpeg_decompress_struct cinfo;
257 struct _JPEG_error_mgr jerr;
259 /* for rotation decoding */
261 Eina_Bool change_wh = EINA_FALSE;
262 unsigned int load_opts_w, load_opts_h;
264 cinfo.err = jpeg_std_error(&(jerr.pub));
265 jerr.pub.error_exit = _JPEGFatalErrorHandler;
266 jerr.pub.emit_message = _JPEGErrorHandler2;
267 jerr.pub.output_message = _JPEGErrorHandler;
268 if (setjmp(jerr.setjmp_buffer))
270 jpeg_destroy_decompress(&cinfo);
271 _evas_jpeg_membuf_src_term(&cinfo);
272 if (cinfo.saw_JFIF_marker)
273 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
275 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
278 jpeg_create_decompress(&cinfo);
280 if (_evas_jpeg_membuf_src(&cinfo, map, length))
282 jpeg_destroy_decompress(&cinfo);
283 _evas_jpeg_membuf_src_term(&cinfo);
284 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
288 jpeg_read_header(&cinfo, TRUE);
289 cinfo.do_fancy_upsampling = FALSE;
290 cinfo.do_block_smoothing = FALSE;
291 cinfo.dct_method = JDCT_IFAST;
292 cinfo.dither_mode = JDITHER_ORDERED;
293 jpeg_start_decompress(&cinfo);
295 /* rotation decoding */
296 if (ie->load_opts.orientation)
298 degree = _get_orientation(map, length);
301 ie->load_opts.degree = degree;
302 ie->flags.rotated = EINA_TRUE;
304 if (degree == 90 || degree == 270)
305 change_wh = EINA_TRUE;
311 w = cinfo.output_width;
312 h = cinfo.output_height;
313 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
316 jpeg_destroy_decompress(&cinfo);
317 _evas_jpeg_membuf_src_term(&cinfo);
318 if (IMG_TOO_BIG(w, h))
319 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
321 *error = EVAS_LOAD_ERROR_GENERIC;
324 if (ie->load_opts.scale_down_by > 1)
326 w /= ie->load_opts.scale_down_by;
327 h /= ie->load_opts.scale_down_by;
329 else if (ie->load_opts.dpi > 0.0)
331 w = (w * ie->load_opts.dpi) / 90.0;
332 h = (h * ie->load_opts.dpi) / 90.0;
334 else if ((ie->load_opts.w > 0) && (ie->load_opts.h > 0))
336 unsigned int w2 = w, h2 = h;
337 /* user set load_opts' w,h on the assumption
338 that image already rotated according to it's orientation info */
341 load_opts_w = ie->load_opts.w;
342 load_opts_h = ie->load_opts.h;
343 ie->load_opts.w = load_opts_h;
344 ie->load_opts.h = load_opts_w;
347 if (ie->load_opts.w > 0)
349 w2 = ie->load_opts.w;
350 h2 = (ie->load_opts.w * h) / w;
351 if ((ie->load_opts.h > 0) && (h2 > ie->load_opts.h))
354 h2 = ie->load_opts.h;
355 w3 = (ie->load_opts.h * w) / h;
360 else if (ie->load_opts.h > 0)
362 h2 = ie->load_opts.h;
363 w2 = (ie->load_opts.h * w) / h;
369 ie->load_opts.w = load_opts_w;
370 ie->load_opts.h = load_opts_h;
376 if ((w != cinfo.output_width) || (h != cinfo.output_height))
378 scalew = cinfo.output_width / w;
379 scaleh = cinfo.output_height / h;
382 if (scaleh < scalew) ie->scale = scaleh;
384 if (ie->scale > 8) ie->scale = 8;
385 else if (ie->scale < 1) ie->scale = 1;
387 if (ie->scale == 3) ie->scale = 2;
388 else if (ie->scale == 5) ie->scale = 4;
389 else if (ie->scale == 6) ie->scale = 4;
390 else if (ie->scale == 7) ie->scale = 4;
395 jpeg_destroy_decompress(&cinfo);
396 _evas_jpeg_membuf_src_term(&cinfo);
397 jpeg_create_decompress(&cinfo);
399 if (_evas_jpeg_membuf_src(&cinfo, map, length))
401 jpeg_destroy_decompress(&cinfo);
402 _evas_jpeg_membuf_src_term(&cinfo);
403 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
407 jpeg_read_header(&cinfo, TRUE);
408 cinfo.do_fancy_upsampling = FALSE;
409 cinfo.do_block_smoothing = FALSE;
411 cinfo.scale_denom = ie->scale;
412 jpeg_calc_output_dimensions(&(cinfo));
413 jpeg_start_decompress(&cinfo);
416 ie->w = cinfo.output_width;
417 ie->h = cinfo.output_height;
419 // be nice and clip region to image. if its totally outside, fail load
420 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
422 unsigned int load_region_x, load_region_y, load_region_w, load_region_h;
423 if (ie->flags.rotated)
425 load_region_x = ie->load_opts.region.x;
426 load_region_y = ie->load_opts.region.y;
427 load_region_w = ie->load_opts.region.w;
428 load_region_h = ie->load_opts.region.h;
433 ie->load_opts.region.x = load_region_y;
434 ie->load_opts.region.y = h - (load_region_x + load_region_w);
435 ie->load_opts.region.w = load_region_h;
436 ie->load_opts.region.h = load_region_w;
439 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
440 ie->load_opts.region.y = h - (load_region_y + load_region_h);
444 ie->load_opts.region.x = w - (load_region_y + load_region_h);
445 ie->load_opts.region.y = load_region_x;
446 ie->load_opts.region.w = load_region_h;
447 ie->load_opts.region.h = load_region_w;
454 RECTS_CLIP_TO_RECT(ie->load_opts.region.x, ie->load_opts.region.y,
455 ie->load_opts.region.w, ie->load_opts.region.h,
457 if ((ie->load_opts.region.w <= 0) || (ie->load_opts.region.h <= 0))
459 jpeg_destroy_decompress(&cinfo);
460 _evas_jpeg_membuf_src_term(&cinfo);
461 *error = EVAS_LOAD_ERROR_GENERIC;
464 ie->w = ie->load_opts.region.w;
465 ie->h = ie->load_opts.region.h;
466 if (ie->flags.rotated)
468 ie->load_opts.region.x = load_region_x;
469 ie->load_opts.region.y = load_region_y;
470 ie->load_opts.region.w = load_region_w;
471 ie->load_opts.region.h = load_region_h;
474 /* end head decoding */
483 jpeg_destroy_decompress(&cinfo);
484 _evas_jpeg_membuf_src_term(&cinfo);
485 *error = EVAS_LOAD_ERROR_NONE;
493 struct timeval timev;
495 gettimeofday(&timev, NULL);
496 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
501 evas_image_load_file_data_jpeg_internal(Image_Entry *ie,
502 void *map, size_t size,
506 struct jpeg_decompress_struct cinfo;
507 struct _JPEG_error_mgr jerr;
508 DATA8 *ptr, *line[16], *data;
509 DATA32 *ptr2,*ptr_rotate;
510 unsigned int x, y, l, i, scans;
513 /* rotation setting */
514 unsigned int tmp, load_region_x, load_region_y, load_region_w, load_region_h;
516 Eina_Bool change_wh = EINA_FALSE;
517 Eina_Bool line_done = EINA_FALSE;
519 if (ie->flags.rotated)
521 degree = ie->load_opts.degree;
522 if (degree == 90 || degree == 270)
523 change_wh = EINA_TRUE;
526 cinfo.err = jpeg_std_error(&(jerr.pub));
527 jerr.pub.error_exit = _JPEGFatalErrorHandler;
528 jerr.pub.emit_message = _JPEGErrorHandler2;
529 jerr.pub.output_message = _JPEGErrorHandler;
530 if (setjmp(jerr.setjmp_buffer))
532 jpeg_destroy_decompress(&cinfo);
533 _evas_jpeg_membuf_src_term(&cinfo);
534 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
537 jpeg_create_decompress(&cinfo);
539 if (_evas_jpeg_membuf_src(&cinfo, map, size))
541 jpeg_destroy_decompress(&cinfo);
542 _evas_jpeg_membuf_src_term(&cinfo);
543 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
547 jpeg_read_header(&cinfo, TRUE);
548 cinfo.do_fancy_upsampling = FALSE;
549 cinfo.do_block_smoothing = FALSE;
550 cinfo.dct_method = JDCT_IFAST;
551 cinfo.dither_mode = JDITHER_ORDERED;
556 cinfo.scale_denom = ie->scale;
559 /* Colorspace conversion options */
560 /* libjpeg can do the following conversions: */
561 /* GRAYSCLAE => RGB YCbCr => RGB and YCCK => CMYK */
562 switch (cinfo.jpeg_color_space)
569 cinfo.out_color_space = JCS_RGB;
573 cinfo.out_color_space = JCS_CMYK;
578 jpeg_calc_output_dimensions(&(cinfo));
579 jpeg_start_decompress(&cinfo);
581 w = cinfo.output_width;
582 h = cinfo.output_height;
591 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
595 if (ie->flags.rotated)
597 load_region_x = ie->load_opts.region.x;
598 load_region_y = ie->load_opts.region.y;
599 load_region_w = ie->load_opts.region.w;
600 load_region_h = ie->load_opts.region.h;
605 ie->load_opts.region.x = load_region_y;
606 ie->load_opts.region.y = h - (load_region_x + load_region_w);
607 ie->load_opts.region.w = load_region_h;
608 ie->load_opts.region.h = load_region_w;
611 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
612 ie->load_opts.region.y = h - (load_region_y + load_region_h);
616 ie->load_opts.region.x = w - (load_region_y + load_region_h);
617 ie->load_opts.region.y = load_region_x;
618 ie->load_opts.region.w = load_region_h;
619 ie->load_opts.region.h = load_region_w;
626 #ifdef BUILD_LOADER_JPEG_REGION
627 cinfo.region_x = ie->load_opts.region.x;
628 cinfo.region_y = ie->load_opts.region.y;
629 cinfo.region_w = ie->load_opts.region.w;
630 cinfo.region_h = ie->load_opts.region.h;
633 if ((!region) && ((w != ie->w) || (h != ie->h)))
635 // race condition, the file could have change from when we call header
636 // this test will not solve the problem with region code.
637 jpeg_destroy_decompress(&cinfo);
638 _evas_jpeg_membuf_src_term(&cinfo);
639 *error = EVAS_LOAD_ERROR_GENERIC;
643 ((ie->w != ie->load_opts.region.w) || (ie->h != ie->load_opts.region.h)))
645 ie->w = ie->load_opts.region.w;
646 ie->h = ie->load_opts.region.h;
649 if (!(((cinfo.out_color_space == JCS_RGB) &&
650 ((cinfo.output_components == 3) || (cinfo.output_components == 1))) ||
651 ((cinfo.out_color_space == JCS_CMYK) && (cinfo.output_components == 4))))
653 jpeg_destroy_decompress(&cinfo);
654 _evas_jpeg_membuf_src_term(&cinfo);
655 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
659 /* end head decoding */
661 if (cinfo.rec_outbuf_height > 16)
663 jpeg_destroy_decompress(&cinfo);
664 _evas_jpeg_membuf_src_term(&cinfo);
665 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
668 data = alloca(w * 16 * cinfo.output_components);
669 evas_cache_image_surface_alloc(ie, ie->w, ie->h);
670 if (ie->flags.loaded)
672 jpeg_destroy_decompress(&cinfo);
673 _evas_jpeg_membuf_src_term(&cinfo);
674 *error = EVAS_LOAD_ERROR_NONE;
675 if (region && ie->flags.rotated)
677 ie->load_opts.region.x = load_region_x;
678 ie->load_opts.region.y = load_region_y;
679 ie->load_opts.region.w = load_region_w;
680 ie->load_opts.region.h = load_region_h;
684 if ((ie->flags.rotated) && change_wh)
686 ptr2 = malloc(ie->w * ie->h * sizeof(DATA32));
690 ptr2 = evas_cache_image_pixels(ie);
694 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
698 /* We handle first CMYK (4 components) */
699 if (cinfo.output_components == 4)
701 // FIXME: handle region
702 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
703 line[i] = data + (i * w * 4);
704 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
706 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
707 scans = cinfo.rec_outbuf_height;
708 if ((h - l) < scans) scans = h - l;
712 for (y = 0; y < scans; y++)
714 if (cinfo.saw_Adobe_marker)
716 for (x = 0; x < w; x++)
718 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
719 /* that is C is replaces by 255 - C, etc...*/
720 /* See the comment below for the computation of RGB values from CMYK ones. */
723 ((ptr[0] * ptr[3] / 255) << 16) |
724 ((ptr[1] * ptr[3] / 255) << 8) |
725 ((ptr[2] * ptr[3] / 255));
732 for (x = 0; x < w; x++)
734 /* Conversion from CMYK to RGB is done in 2 steps: */
735 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
736 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
737 /* R = (1 - C) * (1 - K) * 255 */
738 /* G = (1 - M) * (1 - K) * 255 */
739 /* B = (1 - Y) * (1 - K) * 255 */
740 /* libjpeg stores CMYK values between 0 and 255, */
741 /* so we replace C by C * 255 / 255, etc... and we obtain: */
742 /* R = (255 - C) * (255 - K) / 255 */
743 /* G = (255 - M) * (255 - K) / 255 */
744 /* B = (255 - Y) * (255 - K) / 255 */
745 /* with C, M, Y and K between 0 and 255. */
748 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
749 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
750 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
759 // if line # > region last line, break
760 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
762 line_done = EINA_TRUE;
763 /* if rotation flag is set , we have to rotate image */
765 /*jpeg_destroy_decompress(&cinfo);
766 _evas_jpeg_membuf_src_term(&cinfo);
767 *error = EVAS_LOAD_ERROR_NONE;
770 // els if scan block intersects region start or later
771 else if ((l + scans) >
772 (ie->load_opts.region.y))
774 for (y = 0; y < scans; y++)
776 if (((y + l) >= ie->load_opts.region.y) &&
777 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
779 ptr += ie->load_opts.region.x;
780 if (cinfo.saw_Adobe_marker)
782 for (x = 0; x < ie->load_opts.region.w; x++)
784 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
785 /* that is C is replaces by 255 - C, etc...*/
786 /* See the comment below for the computation of RGB values from CMYK ones. */
789 ((ptr[0] * ptr[3] / 255) << 16) |
790 ((ptr[1] * ptr[3] / 255) << 8) |
791 ((ptr[2] * ptr[3] / 255));
798 for (x = 0; x < ie->load_opts.region.w; x++)
800 /* Conversion from CMYK to RGB is done in 2 steps: */
801 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
802 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
803 /* R = (1 - C) * (1 - K) * 255 */
804 /* G = (1 - M) * (1 - K) * 255 */
805 /* B = (1 - Y) * (1 - K) * 255 */
806 /* libjpeg stores CMYK values between 0 and 255, */
807 /* so we replace C by C * 255 / 255, etc... and we obtain: */
808 /* R = (255 - C) * (255 - K) / 255 */
809 /* G = (255 - M) * (255 - K) / 255 */
810 /* B = (255 - Y) * (255 - K) / 255 */
811 /* with C, M, Y and K between 0 and 255. */
814 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
815 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
816 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
821 ptr += (4 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
830 /* We handle then RGB with 3 components */
831 else if (cinfo.output_components == 3)
838 printf("R| %p %5ix%5i %s: %5i %5i %5ix%5i - ",
842 ie->load_opts.region.x,
843 ie->load_opts.region.y,
844 ie->load_opts.region.w,
845 ie->load_opts.region.h);
849 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
850 line[i] = data + (i * w * 3);
851 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
853 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
854 scans = cinfo.rec_outbuf_height;
855 if ((h - l) < scans) scans = h - l;
859 for (y = 0; y < scans; y++)
861 for (x = 0; x < w; x++)
863 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
871 // if line # > region last line, break
872 // but not return immediately for rotation job
873 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
875 line_done = EINA_TRUE;
876 /* if rotation flag is set , we have to rotate image */
879 // else if scan block intersects region start or later
880 else if ((l + scans) >
881 (ie->load_opts.region.y))
883 for (y = 0; y < scans; y++)
885 if (((y + l) >= ie->load_opts.region.y) &&
886 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
888 ptr += (3 * ie->load_opts.region.x);
889 for (x = 0; x < ie->load_opts.region.w; x++)
891 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
895 ptr += (3 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
905 printf("%3.3f\n", t);
908 /* We finally handle RGB with 1 component */
909 else if (cinfo.output_components == 1)
911 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
912 line[i] = data + (i * w);
913 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
915 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
916 scans = cinfo.rec_outbuf_height;
917 if ((h - l) < scans) scans = h - l;
921 for (y = 0; y < scans; y++)
923 for (x = 0; x < w; x++)
925 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
933 // if line # > region last line, break
934 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
936 line_done = EINA_TRUE;
937 /* if rotation flag is set , we have to rotate image */
939 /*jpeg_destroy_decompress(&cinfo);
940 _evas_jpeg_membuf_src_term(&cinfo);
941 *error = EVAS_LOAD_ERROR_NONE;
944 // els if scan block intersects region start or later
945 else if ((l + scans) >
946 (ie->load_opts.region.y))
948 for (y = 0; y < scans; y++)
950 if (((y + l) >= ie->load_opts.region.y) &&
951 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
953 ptr += ie->load_opts.region.x;
954 for (x = 0; x < ie->load_opts.region.w; x++)
956 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
960 ptr += w - (ie->load_opts.region.x + ie->load_opts.region.w);
969 /* if rotation operation need, rotate it */
972 if (ie->flags.rotated)
974 DATA32 *data1, *data2, *to, *from;
988 data1 = evas_cache_image_pixels(ie);
994 data2 = data1 + (h * w) -1;
995 for (x = (w * h) / 2; --x >= 0;)
1014 else if (degree == 270)
1016 to = data1 + hw - w;
1022 for (x = ie->w; --x >= 0;)
1024 for (y =ie->h; --y >= 0;)
1041 ie->load_opts.region.x = load_region_x;
1042 ie->load_opts.region.y = load_region_y;
1043 ie->load_opts.region.w = load_region_w;
1044 ie->load_opts.region.h = load_region_h;
1049 jpeg_destroy_decompress(&cinfo);
1050 _evas_jpeg_membuf_src_term(&cinfo);
1051 *error = EVAS_LOAD_ERROR_NONE;
1054 /* end data decoding */
1055 jpeg_finish_decompress(&cinfo);
1056 jpeg_destroy_decompress(&cinfo);
1057 _evas_jpeg_membuf_src_term(&cinfo);
1058 *error = EVAS_LOAD_ERROR_NONE;
1062 #if 0 /* not used at the moment */
1064 evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f, int *error)
1067 struct jpeg_decompress_struct cinfo;
1068 struct _JPEG_error_mgr jerr;
1069 DATA8 *ptr, *line[16], *data;
1071 int x, y, l, i, scans, prevy;
1075 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1078 cinfo.err = jpeg_std_error(&(jerr.pub));
1079 jerr.pub.error_exit = _JPEGFatalErrorHandler;
1080 jerr.pub.emit_message = _JPEGErrorHandler2;
1081 jerr.pub.output_message = _JPEGErrorHandler;
1082 if (setjmp(jerr.setjmp_buffer))
1084 jpeg_destroy_decompress(&cinfo);
1085 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
1088 jpeg_create_decompress(&cinfo);
1089 jpeg_stdio_src(&cinfo, f);
1090 jpeg_read_header(&cinfo, TRUE);
1091 cinfo.do_fancy_upsampling = FALSE;
1092 cinfo.do_block_smoothing = FALSE;
1093 jpeg_start_decompress(&cinfo);
1096 ie->w = w = cinfo.output_width;
1097 ie->h = h = cinfo.output_height;
1098 /* end head decoding */
1100 if (cinfo.rec_outbuf_height > 16)
1102 jpeg_destroy_decompress(&cinfo);
1103 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
1106 data = alloca(w * 16 * 3);
1107 if (!ie->flags.loaded)
1109 jpeg_destroy_decompress(&cinfo);
1110 *error = EVAS_LOAD_ERROR_NONE;
1113 ptr2 = evas_cache_image_pixels(ie);
1115 if (cinfo.output_components == 3)
1117 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1118 line[i] = data + (i * w * 3);
1119 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1121 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1122 scans = cinfo.rec_outbuf_height;
1123 if ((h - l) < scans) scans = h - l;
1125 for (y = 0; y < scans; y++)
1127 for (x = 0; x < w; x++)
1130 ((*ptr2) & 0x00ffffff) |
1131 (((ptr[0] + ptr[1] + ptr[2]) / 3) << 24);
1138 else if (cinfo.output_components == 1)
1140 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1141 line[i] = data + (i * w);
1142 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1144 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1145 scans = cinfo.rec_outbuf_height;
1146 if ((h - l) < scans) scans = h - l;
1148 for (y = 0; y < scans; y++)
1150 for (x = 0; x < w; x++)
1153 ((*ptr2) & 0x00ffffff) |
1161 /* end data decoding */
1162 jpeg_finish_decompress(&cinfo);
1163 jpeg_destroy_decompress(&cinfo);
1164 *error = EVAS_LOAD_ERROR_NONE;
1170 evas_image_load_file_head_jpeg(Image_Entry *ie,
1171 const char *file, const char *key __UNUSED__,
1176 Eina_Bool val = EINA_FALSE;
1178 f = eina_file_open(file, EINA_FALSE);
1181 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1184 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1187 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1191 val = evas_image_load_file_head_jpeg_internal(ie,
1192 map, eina_file_size_get(f),
1195 eina_file_map_free(f, map);
1203 evas_image_load_file_data_jpeg(Image_Entry *ie,
1204 const char *file, const char *key __UNUSED__,
1209 Eina_Bool val = EINA_FALSE;
1211 f = eina_file_open(file, EINA_FALSE);
1214 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1217 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1220 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1224 val = evas_image_load_file_data_jpeg_internal(ie,
1225 map, eina_file_size_get(f),
1228 eina_file_map_free(f, map);
1236 module_open(Evas_Module *em)
1239 em->functions = (void *)(&evas_image_load_jpeg_func);
1244 module_close(Evas_Module *em __UNUSED__)
1248 static Evas_Module_Api evas_modapi =
1250 EVAS_MODULE_API_VERSION,
1259 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, jpeg);
1261 #ifndef EVAS_STATIC_BUILD_JPEG
1262 EVAS_EINA_MODULE_DEFINE(image_loader, jpeg);