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(const char *path)
176 /* open file and get 22type frome file */
180 unsigned char buf[4096];
181 unsigned char orientation[2];
182 ExifByteAlign byte_align;
183 int num_directory = 0;
187 f = fopen (path, "rb");
191 size = fread (buf, 1, 22, f);
199 /* 2. check 2,3 bypte with APP1(0xFFE1) */
200 if (memcmp(buf + 2, App1, sizeof (App1)))
206 /* 3. check 6~11bype with Exif Header (0x45786966 0000) */
207 if (memcmp(buf + 6, ExifHeader, sizeof (ExifHeader)))
213 /* 4. get 12&13 byte get info of "II(0x4949)" or "MM(0x4d4d)" */
214 /* 5. get [20]&[21] get directory entry # */
215 if (!strncmp(buf + 12, "MM", 2))
217 byte_align = EXIF_BYTE_ALIGN_MM;
218 num_directory = ((buf[20] << 8) + buf[21]);
219 orientation[0] = 0x01;
220 orientation[1] = 0x12;
222 else if (!strncmp(buf + 12, "II", 2))
224 byte_align = EXIF_BYTE_ALIGN_II;
225 num_directory = ((buf[21] << 8) + buf[20]);
226 orientation[0] = 0x12;
227 orientation[1] = 0x01;
235 if (fread(buf, 12, num_directory, f) > 4096)
240 /* we get all info from file, close file first */
244 for (i = 0; i < num_directory; i++ )
246 if (!strncmp(buf + j, orientation, 2))
248 /*get orientation tag */
249 if (byte_align == EXIF_BYTE_ALIGN_MM)
250 direction = buf[j + 11];
251 else direction = buf[j + 8];
274 evas_image_load_file_head_jpeg_internal(Image_Entry *ie,
275 void *map, size_t length,
278 unsigned int w, h, scalew, scaleh;
279 struct jpeg_decompress_struct cinfo;
280 struct _JPEG_error_mgr jerr;
282 /* for rotation decoding */
284 Eina_Bool change_wh = EINA_FALSE;
285 unsigned int load_opts_w, load_opts_h;
287 cinfo.err = jpeg_std_error(&(jerr.pub));
288 jerr.pub.error_exit = _JPEGFatalErrorHandler;
289 jerr.pub.emit_message = _JPEGErrorHandler2;
290 jerr.pub.output_message = _JPEGErrorHandler;
291 if (setjmp(jerr.setjmp_buffer))
293 jpeg_destroy_decompress(&cinfo);
294 _evas_jpeg_membuf_src_term(&cinfo);
295 if (cinfo.saw_JFIF_marker)
296 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
298 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
301 jpeg_create_decompress(&cinfo);
303 if (_evas_jpeg_membuf_src(&cinfo, map, length))
305 jpeg_destroy_decompress(&cinfo);
306 _evas_jpeg_membuf_src_term(&cinfo);
307 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
311 jpeg_read_header(&cinfo, TRUE);
312 cinfo.do_fancy_upsampling = FALSE;
313 cinfo.do_block_smoothing = FALSE;
314 cinfo.dct_method = JDCT_IFAST;
315 cinfo.dither_mode = JDITHER_ORDERED;
316 jpeg_start_decompress(&cinfo);
319 if (ie->load_opts.orientation)
321 degree = _get_orientation(ie->file);
324 ie->load_opts.degree = degree;
325 ie->flags.rotated = EINA_TRUE;
327 if (degree == 90 || degree == 270)
328 change_wh = EINA_TRUE;
333 w = cinfo.output_width;
334 h = cinfo.output_height;
335 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
338 jpeg_destroy_decompress(&cinfo);
339 _evas_jpeg_membuf_src_term(&cinfo);
340 if (IMG_TOO_BIG(w, h))
341 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
343 *error = EVAS_LOAD_ERROR_GENERIC;
346 if (ie->load_opts.scale_down_by > 1)
348 w /= ie->load_opts.scale_down_by;
349 h /= ie->load_opts.scale_down_by;
351 else if (ie->load_opts.dpi > 0.0)
353 w = (w * ie->load_opts.dpi) / 90.0;
354 h = (h * ie->load_opts.dpi) / 90.0;
356 else if ((ie->load_opts.w > 0) && (ie->load_opts.h > 0))
358 unsigned int w2 = w, h2 = h;
359 /* user set load_opts' w,h on the assumption
360 that image already rotated according to it's orientation info */
363 load_opts_w = ie->load_opts.w;
364 load_opts_h = ie->load_opts.h;
365 ie->load_opts.w = load_opts_h;
366 ie->load_opts.h = load_opts_w;
369 if (ie->load_opts.w > 0)
371 w2 = ie->load_opts.w;
372 h2 = (ie->load_opts.w * h) / w;
373 if ((ie->load_opts.h > 0) && (h2 > ie->load_opts.h))
376 h2 = ie->load_opts.h;
377 w3 = (ie->load_opts.h * w) / h;
382 else if (ie->load_opts.h > 0)
384 h2 = ie->load_opts.h;
385 w2 = (ie->load_opts.h * w) / h;
391 ie->load_opts.w = load_opts_w;
392 ie->load_opts.h = load_opts_h;
398 if ((w != cinfo.output_width) || (h != cinfo.output_height))
400 scalew = cinfo.output_width / w;
401 scaleh = cinfo.output_height / h;
404 if (scaleh < scalew) ie->scale = scaleh;
406 if (ie->scale > 8) ie->scale = 8;
407 else if (ie->scale < 1) ie->scale = 1;
409 if (ie->scale == 3) ie->scale = 2;
410 else if (ie->scale == 5) ie->scale = 4;
411 else if (ie->scale == 6) ie->scale = 4;
412 else if (ie->scale == 7) ie->scale = 4;
417 jpeg_destroy_decompress(&cinfo);
418 _evas_jpeg_membuf_src_term(&cinfo);
419 jpeg_create_decompress(&cinfo);
421 if (_evas_jpeg_membuf_src(&cinfo, map, length))
423 jpeg_destroy_decompress(&cinfo);
424 _evas_jpeg_membuf_src_term(&cinfo);
425 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
429 jpeg_read_header(&cinfo, TRUE);
430 cinfo.do_fancy_upsampling = FALSE;
431 cinfo.do_block_smoothing = FALSE;
433 cinfo.scale_denom = ie->scale;
434 jpeg_calc_output_dimensions(&(cinfo));
435 jpeg_start_decompress(&cinfo);
438 ie->w = cinfo.output_width;
439 ie->h = cinfo.output_height;
441 // be nice and clip region to image. if its totally outside, fail load
442 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
444 unsigned int load_region_x, load_region_y,load_region_w,load_region_h;
445 if (ie->flags.rotated)
447 load_region_x = ie->load_opts.region.x;
448 load_region_y = ie->load_opts.region.y;
449 load_region_w = ie->load_opts.region.w;
450 load_region_h = ie->load_opts.region.h;
455 ie->load_opts.region.x = load_region_y;
456 ie->load_opts.region.y = h - (load_region_x + load_region_w);
457 ie->load_opts.region.w = load_region_h;
458 ie->load_opts.region.h = load_region_w;
461 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
462 ie->load_opts.region.y = h - (load_region_y + load_region_h);
466 ie->load_opts.region.x = w - (load_region_y + load_region_h);
467 ie->load_opts.region.y = load_region_x;
468 ie->load_opts.region.w = load_region_h;
469 ie->load_opts.region.h = load_region_w;
476 RECTS_CLIP_TO_RECT(ie->load_opts.region.x, ie->load_opts.region.y,
477 ie->load_opts.region.w, ie->load_opts.region.h,
479 if ((ie->load_opts.region.w <= 0) || (ie->load_opts.region.h <= 0))
481 jpeg_destroy_decompress(&cinfo);
482 _evas_jpeg_membuf_src_term(&cinfo);
483 *error = EVAS_LOAD_ERROR_GENERIC;
486 ie->w = ie->load_opts.region.w;
487 ie->h = ie->load_opts.region.h;
488 if (ie->flags.rotated)
490 ie->load_opts.region.x = load_region_x;
491 ie->load_opts.region.y = load_region_y;
492 ie->load_opts.region.w = load_region_w;
493 ie->load_opts.region.h = load_region_h;
496 /* end head decoding */
504 jpeg_destroy_decompress(&cinfo);
505 _evas_jpeg_membuf_src_term(&cinfo);
506 *error = EVAS_LOAD_ERROR_NONE;
514 struct timeval timev;
516 gettimeofday(&timev, NULL);
517 return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
522 evas_image_load_file_data_jpeg_internal(Image_Entry *ie,
523 void *map, size_t size,
527 struct jpeg_decompress_struct cinfo;
528 struct _JPEG_error_mgr jerr;
529 DATA8 *ptr, *line[16], *data;
530 DATA32 *ptr2,*ptr_rotate;
531 unsigned int x, y, l, i, scans;
533 unsigned int tmp, load_region_x, load_region_y, load_region_w, load_region_h;/* for rotation decoding */
535 /* rotation setting */
537 Eina_Bool change_wh = EINA_FALSE;
538 Eina_Bool line_done = EINA_FALSE;
539 if (ie->flags.rotated)
541 degree = ie->load_opts.degree;
542 if (degree == 90 || degree == 270)
543 change_wh = EINA_TRUE;
545 cinfo.err = jpeg_std_error(&(jerr.pub));
546 jerr.pub.error_exit = _JPEGFatalErrorHandler;
547 jerr.pub.emit_message = _JPEGErrorHandler2;
548 jerr.pub.output_message = _JPEGErrorHandler;
549 if (setjmp(jerr.setjmp_buffer))
551 jpeg_destroy_decompress(&cinfo);
552 _evas_jpeg_membuf_src_term(&cinfo);
553 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
556 jpeg_create_decompress(&cinfo);
558 if (_evas_jpeg_membuf_src(&cinfo, map, size))
560 jpeg_destroy_decompress(&cinfo);
561 _evas_jpeg_membuf_src_term(&cinfo);
562 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
566 jpeg_read_header(&cinfo, TRUE);
567 cinfo.do_fancy_upsampling = FALSE;
568 cinfo.do_block_smoothing = FALSE;
569 cinfo.dct_method = JDCT_IFAST;
570 cinfo.dither_mode = JDITHER_ORDERED;
575 cinfo.scale_denom = ie->scale;
578 /* Colorspace conversion options */
579 /* libjpeg can do the following conversions: */
580 /* GRAYSCLAE => RGB YCbCr => RGB and YCCK => CMYK */
581 switch (cinfo.jpeg_color_space)
588 cinfo.out_color_space = JCS_RGB;
592 cinfo.out_color_space = JCS_CMYK;
597 jpeg_calc_output_dimensions(&(cinfo));
598 jpeg_start_decompress(&cinfo);
600 w = cinfo.output_width;
601 h = cinfo.output_height;
610 if ((ie->load_opts.region.w > 0) && (ie->load_opts.region.h > 0))
613 #ifdef BUILD_LOADER_JPEG_REGION
615 if (ie->flags.rotated)
617 load_region_x = ie->load_opts.region.x;
618 load_region_y = ie->load_opts.region.y;
619 load_region_w = ie->load_opts.region.w;
620 load_region_h = ie->load_opts.region.h;
625 ie->load_opts.region.x = load_region_y;
626 ie->load_opts.region.y = h - (load_region_x + load_region_w);
627 ie->load_opts.region.w = load_region_h;
628 ie->load_opts.region.h = load_region_w;
631 ie->load_opts.region.x = w - (load_region_x+ load_region_w);
632 ie->load_opts.region.y = h - (load_region_y + load_region_h);
636 ie->load_opts.region.x = w - (load_region_y + load_region_h);
637 ie->load_opts.region.y = load_region_x;
638 ie->load_opts.region.w = load_region_h;
639 ie->load_opts.region.h = load_region_w;
646 cinfo.region_x = ie->load_opts.region.x;
647 cinfo.region_y = ie->load_opts.region.y;
648 cinfo.region_w = ie->load_opts.region.w;
649 cinfo.region_h = ie->load_opts.region.h;
652 if ((!region) && ((w != ie->w) || (h != ie->h)))
654 // race condition, the file could have change from when we call header
655 // this test will not solve the problem with region code.
656 jpeg_destroy_decompress(&cinfo);
657 _evas_jpeg_membuf_src_term(&cinfo);
658 *error = EVAS_LOAD_ERROR_GENERIC;
662 ((ie->w != ie->load_opts.region.w) || (ie->h != ie->load_opts.region.h)))
664 ie->w = ie->load_opts.region.w;
665 ie->h = ie->load_opts.region.h;
668 if (!(((cinfo.out_color_space == JCS_RGB) &&
669 ((cinfo.output_components == 3) || (cinfo.output_components == 1))) ||
670 ((cinfo.out_color_space == JCS_CMYK) && (cinfo.output_components == 4))))
672 jpeg_destroy_decompress(&cinfo);
673 _evas_jpeg_membuf_src_term(&cinfo);
674 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
678 /* end head decoding */
680 if (cinfo.rec_outbuf_height > 16)
682 jpeg_destroy_decompress(&cinfo);
683 _evas_jpeg_membuf_src_term(&cinfo);
684 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
687 data = alloca(w * 16 * cinfo.output_components);
688 evas_cache_image_surface_alloc(ie, ie->w, ie->h);
689 if (ie->flags.loaded)
691 jpeg_destroy_decompress(&cinfo);
692 _evas_jpeg_membuf_src_term(&cinfo);
693 *error = EVAS_LOAD_ERROR_NONE;
694 if (region && ie->flags.rotated)
696 ie->load_opts.region.x = load_region_x;
697 ie->load_opts.region.y = load_region_y;
698 ie->load_opts.region.w = load_region_w;
699 ie->load_opts.region.h = load_region_h;
703 if ((ie->flags.rotated) && change_wh)
705 ptr2 = malloc(ie->w * ie->h * sizeof(DATA32));
709 ptr2 = evas_cache_image_pixels(ie);
712 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
716 /* We handle first CMYK (4 components) */
717 if (cinfo.output_components == 4)
719 // FIXME: handle region
720 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
721 line[i] = data + (i * w * 4);
722 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
724 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
725 scans = cinfo.rec_outbuf_height;
726 if ((h - l) < scans) scans = h - l;
730 for (y = 0; y < scans; y++)
732 if (cinfo.saw_Adobe_marker)
734 for (x = 0; x < w; x++)
736 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
737 /* that is C is replaces by 255 - C, etc...*/
738 /* See the comment below for the computation of RGB values from CMYK ones. */
741 ((ptr[0] * ptr[3] / 255) << 16) |
742 ((ptr[1] * ptr[3] / 255) << 8) |
743 ((ptr[2] * ptr[3] / 255));
750 for (x = 0; x < w; x++)
752 /* Conversion from CMYK to RGB is done in 2 steps: */
753 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
754 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
755 /* R = (1 - C) * (1 - K) * 255 */
756 /* G = (1 - M) * (1 - K) * 255 */
757 /* B = (1 - Y) * (1 - K) * 255 */
758 /* libjpeg stores CMYK values between 0 and 255, */
759 /* so we replace C by C * 255 / 255, etc... and we obtain: */
760 /* R = (255 - C) * (255 - K) / 255 */
761 /* G = (255 - M) * (255 - K) / 255 */
762 /* B = (255 - Y) * (255 - K) / 255 */
763 /* with C, M, Y and K between 0 and 255. */
766 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
767 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
768 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
777 // if line # > region last line, break
778 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
780 line_done = EINA_TRUE;
781 /* if rotation flag is set , we have to rotate image */
783 /*jpeg_destroy_decompress(&cinfo);
784 _evas_jpeg_membuf_src_term(&cinfo);
785 *error = EVAS_LOAD_ERROR_NONE;
788 // els if scan block intersects region start or later
789 else if ((l + scans) >
790 (ie->load_opts.region.y))
792 for (y = 0; y < scans; y++)
794 if (((y + l) >= ie->load_opts.region.y) &&
795 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
797 ptr += ie->load_opts.region.x;
798 if (cinfo.saw_Adobe_marker)
800 for (x = 0; x < ie->load_opts.region.w; x++)
802 /* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
803 /* that is C is replaces by 255 - C, etc...*/
804 /* See the comment below for the computation of RGB values from CMYK ones. */
807 ((ptr[0] * ptr[3] / 255) << 16) |
808 ((ptr[1] * ptr[3] / 255) << 8) |
809 ((ptr[2] * ptr[3] / 255));
816 for (x = 0; x < ie->load_opts.region.w; x++)
818 /* Conversion from CMYK to RGB is done in 2 steps: */
819 /* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
820 /* after computation, if C, M, Y and K are between 0 and 1, we have: */
821 /* R = (1 - C) * (1 - K) * 255 */
822 /* G = (1 - M) * (1 - K) * 255 */
823 /* B = (1 - Y) * (1 - K) * 255 */
824 /* libjpeg stores CMYK values between 0 and 255, */
825 /* so we replace C by C * 255 / 255, etc... and we obtain: */
826 /* R = (255 - C) * (255 - K) / 255 */
827 /* G = (255 - M) * (255 - K) / 255 */
828 /* B = (255 - Y) * (255 - K) / 255 */
829 /* with C, M, Y and K between 0 and 255. */
832 (((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
833 (((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
834 (((255 - ptr[2]) * (255 - ptr[3]) / 255));
839 ptr += (4 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
848 /* We handle then RGB with 3 components */
849 else if (cinfo.output_components == 3)
856 printf("R| %p %5ix%5i %s: %5i %5i %5ix%5i - ",
860 ie->load_opts.region.x,
861 ie->load_opts.region.y,
862 ie->load_opts.region.w,
863 ie->load_opts.region.h);
867 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
868 line[i] = data + (i * w * 3);
869 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
871 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
872 scans = cinfo.rec_outbuf_height;
873 if ((h - l) < scans) scans = h - l;
877 for (y = 0; y < scans; y++)
879 for (x = 0; x < w; x++)
881 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
889 // if line # > region last line, break
890 // but not return immediately for rotation job
891 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
893 line_done = EINA_TRUE;
894 /* if rotation flag is set , we have to rotate image */
897 // else if scan block intersects region start or later
898 else if ((l + scans) >
899 (ie->load_opts.region.y))
901 for (y = 0; y < scans; y++)
903 if (((y + l) >= ie->load_opts.region.y) &&
904 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
906 ptr += (3 * ie->load_opts.region.x);
907 for (x = 0; x < ie->load_opts.region.w; x++)
909 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
913 ptr += (3 * (w - (ie->load_opts.region.x + ie->load_opts.region.w)));
923 printf("%3.3f\n", t);
926 /* We finally handle RGB with 1 component */
927 else if (cinfo.output_components == 1)
929 for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
930 line[i] = data + (i * w);
931 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
933 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
934 scans = cinfo.rec_outbuf_height;
935 if ((h - l) < scans) scans = h - l;
939 for (y = 0; y < scans; y++)
941 for (x = 0; x < w; x++)
943 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
951 // if line # > region last line, break
952 if (l >= (ie->load_opts.region.y + ie->load_opts.region.h))
954 line_done = EINA_TRUE;
955 /* if rotation flag is set , we have to rotate image */
957 /*jpeg_destroy_decompress(&cinfo);
958 _evas_jpeg_membuf_src_term(&cinfo);
959 *error = EVAS_LOAD_ERROR_NONE;
962 // els if scan block intersects region start or later
963 else if ((l + scans) >
964 (ie->load_opts.region.y))
966 for (y = 0; y < scans; y++)
968 if (((y + l) >= ie->load_opts.region.y) &&
969 ((y + l) < (ie->load_opts.region.y + ie->load_opts.region.h)))
971 ptr += ie->load_opts.region.x;
972 for (x = 0; x < ie->load_opts.region.w; x++)
974 *ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
978 ptr += w - (ie->load_opts.region.x + ie->load_opts.region.w);
987 /* if rotation operation need, rotate it */
989 if (ie->flags.rotated)
991 DATA32 *data1, *data2, *to, *from;
1005 data1 = evas_cache_image_pixels(ie);
1011 data2 = data1 + (h * w) -1;
1012 for (x = (w * h) / 2; --x >= 0;)
1031 else if (degree == 270)
1033 to = data1 + hw - w;
1039 for (x = ie->w; --x >= 0;)
1041 for (y =ie->h; --y >= 0;)
1058 ie->load_opts.region.x = load_region_x;
1059 ie->load_opts.region.y = load_region_y;
1060 ie->load_opts.region.w = load_region_w;
1061 ie->load_opts.region.h = load_region_h;
1067 jpeg_destroy_decompress(&cinfo);
1068 _evas_jpeg_membuf_src_term(&cinfo);
1069 *error = EVAS_LOAD_ERROR_NONE;
1072 /* end data decoding */
1073 jpeg_finish_decompress(&cinfo);
1074 jpeg_destroy_decompress(&cinfo);
1075 _evas_jpeg_membuf_src_term(&cinfo);
1076 *error = EVAS_LOAD_ERROR_NONE;
1080 #if 0 /* not used at the moment */
1082 evas_image_load_file_data_jpeg_alpha_internal(Image_Entry *ie, FILE *f, int *error)
1085 struct jpeg_decompress_struct cinfo;
1086 struct _JPEG_error_mgr jerr;
1087 DATA8 *ptr, *line[16], *data;
1089 int x, y, l, i, scans, prevy;
1093 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1096 cinfo.err = jpeg_std_error(&(jerr.pub));
1097 jerr.pub.error_exit = _JPEGFatalErrorHandler;
1098 jerr.pub.emit_message = _JPEGErrorHandler2;
1099 jerr.pub.output_message = _JPEGErrorHandler;
1100 if (setjmp(jerr.setjmp_buffer))
1102 jpeg_destroy_decompress(&cinfo);
1103 *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
1106 jpeg_create_decompress(&cinfo);
1107 jpeg_stdio_src(&cinfo, f);
1108 jpeg_read_header(&cinfo, TRUE);
1109 cinfo.do_fancy_upsampling = FALSE;
1110 cinfo.do_block_smoothing = FALSE;
1111 jpeg_start_decompress(&cinfo);
1114 ie->w = w = cinfo.output_width;
1115 ie->h = h = cinfo.output_height;
1116 /* end head decoding */
1118 if (cinfo.rec_outbuf_height > 16)
1120 jpeg_destroy_decompress(&cinfo);
1121 *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT;
1124 data = alloca(w * 16 * 3);
1125 if (!ie->flags.loaded)
1127 jpeg_destroy_decompress(&cinfo);
1128 *error = EVAS_LOAD_ERROR_NONE;
1131 ptr2 = evas_cache_image_pixels(ie);
1133 if (cinfo.output_components == 3)
1135 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1136 line[i] = data + (i * w * 3);
1137 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1139 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1140 scans = cinfo.rec_outbuf_height;
1141 if ((h - l) < scans) scans = h - l;
1143 for (y = 0; y < scans; y++)
1145 for (x = 0; x < w; x++)
1148 ((*ptr2) & 0x00ffffff) |
1149 (((ptr[0] + ptr[1] + ptr[2]) / 3) << 24);
1156 else if (cinfo.output_components == 1)
1158 for (i = 0; i < cinfo.rec_outbuf_height; i++)
1159 line[i] = data + (i * w);
1160 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1162 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
1163 scans = cinfo.rec_outbuf_height;
1164 if ((h - l) < scans) scans = h - l;
1166 for (y = 0; y < scans; y++)
1168 for (x = 0; x < w; x++)
1171 ((*ptr2) & 0x00ffffff) |
1179 /* end data decoding */
1180 jpeg_finish_decompress(&cinfo);
1181 jpeg_destroy_decompress(&cinfo);
1182 *error = EVAS_LOAD_ERROR_NONE;
1188 evas_image_load_file_head_jpeg(Image_Entry *ie,
1189 const char *file, const char *key __UNUSED__,
1194 Eina_Bool val = EINA_FALSE;
1196 f = eina_file_open(file, EINA_FALSE);
1199 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1202 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1205 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1209 val = evas_image_load_file_head_jpeg_internal(ie,
1210 map, eina_file_size_get(f),
1213 eina_file_map_free(f, map);
1221 evas_image_load_file_data_jpeg(Image_Entry *ie,
1222 const char *file, const char *key __UNUSED__,
1227 Eina_Bool val = EINA_FALSE;
1229 f = eina_file_open(file, EINA_FALSE);
1232 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1235 map = eina_file_map_all(f, EINA_FILE_WILLNEED);
1238 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
1242 val = evas_image_load_file_data_jpeg_internal(ie,
1243 map, eina_file_size_get(f),
1246 eina_file_map_free(f, map);
1254 module_open(Evas_Module *em)
1257 em->functions = (void *)(&evas_image_load_jpeg_func);
1262 module_close(Evas_Module *em __UNUSED__)
1266 static Evas_Module_Api evas_modapi =
1268 EVAS_MODULE_API_VERSION,
1277 EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_IMAGE_LOADER, image_loader, jpeg);
1279 #ifndef EVAS_STATIC_BUILD_JPEG
1280 EVAS_EINA_MODULE_DEFINE(image_loader, jpeg);