3 #endif /* ifdef HAVE_CONFIG_H */
8 # define alloca __builtin_alloca
10 # define alloca __alloca
11 #elif defined _MSC_VER
13 # define alloca _alloca
14 #else /* ifdef HAVE_ALLOCA_H */
18 # endif /* ifdef __cplusplus */
19 void * alloca (size_t);
20 #endif /* ifdef HAVE_ALLOCA_H */
22 #ifdef HAVE_NETINET_IN_H
24 # include <sys/types.h>
25 # endif /* ifdef __OpenBSD__ */
26 # include <netinet/in.h>
27 #endif /* ifdef HAVE_NETINET_IN_H */
30 # include <winsock2.h>
31 #endif /* ifdef _WIN32 */
40 #include "Eet_private.h"
44 typedef struct _JPEG_error_mgr * emptr;
48 struct _JPEG_error_mgr
50 struct jpeg_error_mgr pub;
51 jmp_buf setjmp_buffer;
54 struct jpeg_membuf_src
56 struct jpeg_source_mgr pub;
58 const unsigned char *buf;
63 _eet_jpeg_membuf_src_init(j_decompress_ptr cinfo)
65 /* FIXME: Use attribute unused */
67 } /* _eet_jpeg_membuf_src_init */
70 _eet_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
72 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
73 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
75 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
76 src->pub.next_input_byte = jpeg_eoi;
79 } /* _eet_jpeg_membuf_src_fill */
82 _eet_jpeg_membuf_src_skip(j_decompress_ptr cinfo,
85 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
87 src->pub.bytes_in_buffer -= num_bytes;
88 src->pub.next_input_byte += num_bytes;
89 } /* _eet_jpeg_membuf_src_skip */
92 _eet_jpeg_membuf_src_term(j_decompress_ptr cinfo)
96 } /* _eet_jpeg_membuf_src_term */
99 eet_jpeg_membuf_src(j_decompress_ptr cinfo,
103 struct jpeg_membuf_src *src;
105 src = malloc(sizeof(*src));
109 cinfo->src = &src->pub;
112 src->pub.init_source = _eet_jpeg_membuf_src_init;
113 src->pub.fill_input_buffer = _eet_jpeg_membuf_src_fill;
114 src->pub.skip_input_data = _eet_jpeg_membuf_src_skip;
115 src->pub.resync_to_restart = jpeg_resync_to_restart;
116 src->pub.term_source = _eet_jpeg_membuf_src_term;
117 src->pub.bytes_in_buffer = src->len;
118 src->pub.next_input_byte = src->buf;
121 } /* eet_jpeg_membuf_src */
123 struct jpeg_membuf_dst
125 struct jpeg_destination_mgr pub;
136 _eet_jpeg_membuf_dst_init(j_compress_ptr cinfo)
138 /* FIXME: Use eina attribute */
140 } /* _eet_jpeg_membuf_dst_init */
143 _eet_jpeg_membuf_dst_flush(j_compress_ptr cinfo)
145 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
148 if (dst->len >= 0x40000000 ||
149 (buf = realloc(dst->buf, dst->len * 2)) == NULL)
152 dst->pub.next_output_byte = dst->buf;
153 dst->pub.free_in_buffer = dst->len;
157 dst->pub.next_output_byte =
158 buf + ((unsigned char *)dst->pub.next_output_byte - dst->buf);
160 dst->pub.free_in_buffer += dst->len;
164 } /* _eet_jpeg_membuf_dst_flush */
167 _eet_jpeg_membuf_dst_term(j_compress_ptr cinfo)
169 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
173 *dst->dst_buf = NULL;
179 *dst->dst_buf = dst->buf;
180 *dst->dst_len = (unsigned char *)dst->pub.next_output_byte - dst->buf;
185 } /* _eet_jpeg_membuf_dst_term */
188 eet_jpeg_membuf_dst(j_compress_ptr cinfo,
192 struct jpeg_membuf_dst *dst;
194 dst = malloc(sizeof(*dst));
198 dst->buf = malloc(32768);
207 cinfo->dest = &dst->pub;
208 dst->pub.init_destination = _eet_jpeg_membuf_dst_init;
209 dst->pub.empty_output_buffer = _eet_jpeg_membuf_dst_flush;
210 dst->pub.term_destination = _eet_jpeg_membuf_dst_term;
211 dst->pub.free_in_buffer = dst->len;
212 dst->pub.next_output_byte = dst->buf;
218 } /* eet_jpeg_membuf_dst */
222 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
223 static void _JPEGErrorHandler(j_common_ptr cinfo);
224 static void _JPEGErrorHandler2(j_common_ptr cinfo,
227 static int eet_data_image_jpeg_header_decode(const void *data,
231 static int eet_data_image_jpeg_rgb_decode(const void *data,
238 unsigned int row_stride);
239 static void * eet_data_image_jpeg_alpha_decode(const void *data,
246 unsigned int row_stride);
247 static void * eet_data_image_lossless_convert(int *size,
252 static void * eet_data_image_lossless_compressed_convert(int *size,
258 static void * eet_data_image_jpeg_convert(int *size,
264 static void * eet_data_image_jpeg_alpha_convert(int *size,
273 static int _eet_image_words_bigendian = -1;
277 #define SWAP64(x) (x) =\
278 ((((unsigned long long)(x) & 0x00000000000000ffULL) << 56) |\
279 (((unsigned long long)(x) & 0x000000000000ff00ULL) << 40) |\
280 (((unsigned long long)(x) & 0x0000000000ff0000ULL) << 24) |\
281 (((unsigned long long)(x) & 0x00000000ff000000ULL) << 8) |\
282 (((unsigned long long)(x) & 0x000000ff00000000ULL) >> 8) |\
283 (((unsigned long long)(x) & 0x0000ff0000000000ULL) >> 24) |\
284 (((unsigned long long)(x) & 0x00ff000000000000ULL) >> 40) |\
285 (((unsigned long long)(x) & 0xff00000000000000ULL) >> 56))
286 #define SWAP32(x) (x) =\
287 ((((int)(x) & 0x000000ff) << 24) |\
288 (((int)(x) & 0x0000ff00) << 8) |\
289 (((int)(x) & 0x00ff0000) >> 8) |\
290 (((int)(x) & 0xff000000) >> 24))
291 #define SWAP16(x) (x) =\
292 ((((short)(x) & 0x00ff) << 8) |\
293 (((short)(x) & 0xff00) >> 8))
297 #endif /* ifdef CONV8 */
300 #endif /* ifdef CONV16 */
303 #endif /* ifdef CONV32 */
306 #endif /* ifdef CONV64 */
309 #define CONV16(x) {if (_eet_image_words_bigendian) {SWAP16(x); }}
310 #define CONV32(x) {if (_eet_image_words_bigendian) {SWAP32(x); }}
311 #define CONV64(x) {if (_eet_image_words_bigendian) {SWAP64(x); }}
316 _JPEGFatalErrorHandler(j_common_ptr cinfo)
320 errmgr = (emptr)cinfo->err;
321 /* cinfo->err->output_message(cinfo);*/
322 longjmp(errmgr->setjmp_buffer, 1);
324 } /* _JPEGFatalErrorHandler */
327 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
331 /* errmgr = (emptr) cinfo->err; */
332 /* cinfo->err->output_message(cinfo);*/
333 /* longjmp(errmgr->setjmp_buffer, 1);*/
335 } /* _JPEGErrorHandler */
338 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__,
339 int msg_level __UNUSED__)
343 /* errmgr = (emptr) cinfo->err; */
344 /* cinfo->err->output_message(cinfo);*/
345 /* longjmp(errmgr->setjmp_buffer, 1);*/
347 } /* _JPEGErrorHandler2 */
350 eet_data_image_jpeg_header_decode(const void *data,
355 struct jpeg_decompress_struct cinfo;
356 struct _JPEG_error_mgr jerr;
358 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
360 cinfo.err = jpeg_std_error(&(jerr.pub));
361 jerr.pub.error_exit = _JPEGFatalErrorHandler;
362 jerr.pub.emit_message = _JPEGErrorHandler2;
363 jerr.pub.output_message = _JPEGErrorHandler;
364 if (setjmp(jerr.setjmp_buffer))
367 jpeg_create_decompress(&cinfo);
369 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
371 jpeg_destroy_decompress(&cinfo);
375 jpeg_read_header(&cinfo, TRUE);
376 cinfo.do_fancy_upsampling = FALSE;
377 cinfo.do_block_smoothing = FALSE;
378 jpeg_start_decompress(&cinfo);
381 *w = cinfo.output_width;
382 *h = cinfo.output_height;
387 jpeg_destroy_decompress(&cinfo);
389 if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
393 } /* eet_data_image_jpeg_header_decode */
396 eet_data_image_jpeg_rgb_decode(const void *data,
403 unsigned int row_stride)
405 struct jpeg_decompress_struct cinfo;
406 struct _JPEG_error_mgr jerr;
407 unsigned char *ptr, *line[16], *tdata = NULL;
408 unsigned int *ptr2, *tmp;
410 unsigned int x, y, l, scans;
413 /* FIXME: handle src_x, src_y and row_stride correctly */
417 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
419 cinfo.err = jpeg_std_error(&(jerr.pub));
420 jerr.pub.error_exit = _JPEGFatalErrorHandler;
421 jerr.pub.emit_message = _JPEGErrorHandler2;
422 jerr.pub.output_message = _JPEGErrorHandler;
423 if (setjmp(jerr.setjmp_buffer))
426 jpeg_create_decompress(&cinfo);
428 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
430 jpeg_destroy_decompress(&cinfo);
434 jpeg_read_header(&cinfo, TRUE);
435 cinfo.dct_method = JDCT_FASTEST;
436 cinfo.do_fancy_upsampling = FALSE;
437 cinfo.do_block_smoothing = FALSE;
438 jpeg_start_decompress(&cinfo);
441 iw = cinfo.output_width;
442 ih = cinfo.output_height;
443 if ((iw != w) || (ih != h))
448 jpeg_destroy_decompress(&cinfo);
452 /* end head decoding */
454 if (cinfo.rec_outbuf_height > 16)
459 jpeg_destroy_decompress(&cinfo);
463 tdata = alloca((iw) * 16 * 3);
466 if (cinfo.output_components == 3)
468 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
469 line[i] = tdata + (i * (iw) * 3);
470 for (l = 0; l < ih; l += cinfo.rec_outbuf_height)
472 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
473 scans = cinfo.rec_outbuf_height;
474 if ((ih - l) < scans)
479 if (l + scans >= src_y && l < src_y + h)
485 for (ptr += 3 * iw * y; y < scans && (y + l) < (src_y + h);
490 for (x = 0; x < w; x++)
494 ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
499 ptr2 = tmp + row_stride / 4;
504 else if (cinfo.output_components == 1)
506 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
507 line[i] = tdata + (i * (iw));
508 for (l = 0; l < (ih); l += cinfo.rec_outbuf_height)
510 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
511 scans = cinfo.rec_outbuf_height;
512 if (((ih) - l) < scans)
517 if (l >= src_y && l < src_y + h)
523 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
527 for (x = 0; x < w; x++)
531 ((ptr[0]) << 16) | ((ptr[0]) << 8) | (ptr[0]);
536 ptr2 = tmp + row_stride / 4;
542 /* end data decoding */
543 jpeg_finish_decompress(&cinfo);
544 jpeg_destroy_decompress(&cinfo);
546 } /* eet_data_image_jpeg_rgb_decode */
549 eet_data_image_jpeg_alpha_decode(const void *data,
556 unsigned int row_stride)
558 struct jpeg_decompress_struct cinfo;
559 struct _JPEG_error_mgr jerr;
560 unsigned char *ptr, *line[16], *tdata = NULL;
561 unsigned int *ptr2, *tmp;
562 unsigned int x, y, l, scans;
565 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
567 cinfo.err = jpeg_std_error(&(jerr.pub));
568 jerr.pub.error_exit = _JPEGFatalErrorHandler;
569 jerr.pub.emit_message = _JPEGErrorHandler2;
570 jerr.pub.output_message = _JPEGErrorHandler;
571 if (setjmp(jerr.setjmp_buffer))
574 jpeg_create_decompress(&cinfo);
576 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
578 jpeg_destroy_decompress(&cinfo);
582 jpeg_read_header(&cinfo, TRUE);
583 cinfo.dct_method = JDCT_FASTEST;
584 cinfo.do_fancy_upsampling = FALSE;
585 cinfo.do_block_smoothing = FALSE;
586 jpeg_start_decompress(&cinfo);
589 iw = cinfo.output_width;
590 if (w != cinfo.output_width
591 || h != cinfo.output_height)
596 jpeg_destroy_decompress(&cinfo);
600 /* end head decoding */
602 if (cinfo.rec_outbuf_height > 16)
607 jpeg_destroy_decompress(&cinfo);
611 tdata = alloca(w * 16 * 3);
613 if (cinfo.output_components == 1)
615 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
616 line[i] = tdata + (i * w);
617 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
619 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
620 scans = cinfo.rec_outbuf_height;
626 if (l >= src_y && l < src_y + h)
632 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
636 for (x = 0; x < w; x++)
639 ((*ptr2) & 0x00ffffff) |
645 ptr2 = tmp + row_stride / 4;
651 /* end data decoding */
652 jpeg_finish_decompress(&cinfo);
653 jpeg_destroy_decompress(&cinfo);
655 } /* eet_data_image_jpeg_alpha_decode */
658 eet_data_image_lossless_convert(int *size,
664 if (_eet_image_words_bigendian == -1)
668 v = htonl(0x12345678);
670 _eet_image_words_bigendian = 1;
672 _eet_image_words_bigendian = 0;
679 d = malloc((w * h * 4) + (8 * 4));
686 header[0] = 0xac1dfeed;
691 memcpy(d + 32, data, w * h * 4);
693 if (_eet_image_words_bigendian)
697 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
700 *size = ((w * h * 4) + (8 * 4));
703 } /* eet_data_image_lossless_convert */
706 eet_data_image_lossless_compressed_convert(int *size,
713 if (_eet_image_words_bigendian == -1)
717 v = htonl(0x12345678);
719 _eet_image_words_bigendian = 1;
721 _eet_image_words_bigendian = 0;
731 d = malloc((w * h * 4) + (8 * 4));
735 buflen = (((w * h * 101) / 100) + 3) * 4;
736 comp = malloc(buflen);
746 header[0] = 0xac1dfeed;
750 header[4] = compression;
751 memcpy(d + 32, data, w * h * 4);
753 if (_eet_image_words_bigendian)
757 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
760 ret = compress2((Bytef *)comp, &buflen,
764 if (ret != Z_OK || buflen > (w * h * 4))
772 memcpy(d + 32, comp, buflen);
773 *size = (8 * 4) + buflen;
777 } /* eet_data_image_lossless_compressed_convert */
780 eet_data_image_jpeg_convert(int *size,
787 struct jpeg_compress_struct cinfo;
788 struct _JPEG_error_mgr jerr;
795 (void)alpha; /* unused */
799 memset(&cinfo, 0, sizeof (struct jpeg_compress_struct));
801 cinfo.err = jpeg_std_error(&(jerr.pub));
802 jerr.pub.error_exit = _JPEGFatalErrorHandler;
803 jerr.pub.emit_message = _JPEGErrorHandler2;
804 jerr.pub.output_message = _JPEGErrorHandler;
805 if (setjmp(jerr.setjmp_buffer))
808 jpeg_create_compress(&cinfo);
810 if (eet_jpeg_membuf_dst(&cinfo, &d, &sz))
812 jpeg_destroy_compress(&cinfo);
816 cinfo.image_width = w;
817 cinfo.image_height = h;
818 cinfo.input_components = 3;
819 cinfo.in_color_space = JCS_RGB;
820 jpeg_set_defaults(&cinfo);
821 jpeg_set_quality(&cinfo, quality, TRUE);
824 cinfo.comp_info[0].h_samp_factor = 1;
825 cinfo.comp_info[0].v_samp_factor = 1;
826 cinfo.comp_info[1].h_samp_factor = 1;
827 cinfo.comp_info[1].v_samp_factor = 1;
828 cinfo.comp_info[2].h_samp_factor = 1;
829 cinfo.comp_info[2].v_samp_factor = 1;
832 jpeg_start_compress(&cinfo, TRUE);
834 while (cinfo.next_scanline < cinfo.image_height)
838 /* convert scaline from ARGB to RGB packed */
839 ptr = ((const int *)data) + cinfo.next_scanline * w;
840 for (j = 0, i = 0; i < w; i++)
842 buf[j++] = ((*ptr) >> 16) & 0xff;
843 buf[j++] = ((*ptr) >> 8) & 0xff;
844 buf[j++] = ((*ptr)) & 0xff;
847 jbuf = (JSAMPROW *)(&buf);
848 jpeg_write_scanlines(&cinfo, jbuf, 1);
851 jpeg_finish_compress(&cinfo);
852 jpeg_destroy_compress(&cinfo);
856 } /* eet_data_image_jpeg_convert */
859 eet_data_image_jpeg_alpha_convert(int *size,
866 unsigned char *d1, *d2;
871 (void)alpha; /* unused */
873 if (_eet_image_words_bigendian == -1)
877 v = htonl(0x12345678);
879 _eet_image_words_bigendian = 1;
881 _eet_image_words_bigendian = 0;
888 struct _JPEG_error_mgr jerr;
890 struct jpeg_compress_struct cinfo;
895 cinfo.err = jpeg_std_error(&(jerr.pub));
896 jerr.pub.error_exit = _JPEGFatalErrorHandler;
897 jerr.pub.emit_message = _JPEGErrorHandler2;
898 jerr.pub.output_message = _JPEGErrorHandler;
899 if (setjmp(jerr.setjmp_buffer))
902 jpeg_create_compress(&cinfo);
903 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
905 jpeg_destroy_compress(&cinfo);
909 cinfo.image_width = w;
910 cinfo.image_height = h;
911 cinfo.input_components = 3;
912 cinfo.in_color_space = JCS_RGB;
913 jpeg_set_defaults(&cinfo);
914 jpeg_set_quality(&cinfo, quality, TRUE);
917 cinfo.comp_info[0].h_samp_factor = 1;
918 cinfo.comp_info[0].v_samp_factor = 1;
919 cinfo.comp_info[1].h_samp_factor = 1;
920 cinfo.comp_info[1].v_samp_factor = 1;
921 cinfo.comp_info[2].h_samp_factor = 1;
922 cinfo.comp_info[2].v_samp_factor = 1;
925 jpeg_start_compress(&cinfo, TRUE);
927 while (cinfo.next_scanline < cinfo.image_height)
931 ptr = ((const int *)data) + cinfo.next_scanline * w;
932 /* convert scaline from ARGB to RGB packed */
933 for (j = 0, i = 0; i < w; i++)
935 buf[j++] = ((*ptr) >> 16) & 0xff;
936 buf[j++] = ((*ptr) >> 8) & 0xff;
937 buf[j++] = ((*ptr)) & 0xff;
940 jbuf = (JSAMPROW *)(&buf);
941 jpeg_write_scanlines(&cinfo, jbuf, 1);
944 jpeg_finish_compress(&cinfo);
945 jpeg_destroy_compress(&cinfo);
954 struct _JPEG_error_mgr jerr;
956 struct jpeg_compress_struct cinfo;
961 cinfo.err = jpeg_std_error(&(jerr.pub));
962 jerr.pub.error_exit = _JPEGFatalErrorHandler;
963 jerr.pub.emit_message = _JPEGErrorHandler2;
964 jerr.pub.output_message = _JPEGErrorHandler;
965 if (setjmp(jerr.setjmp_buffer))
971 jpeg_create_compress(&cinfo);
972 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
974 jpeg_destroy_compress(&cinfo);
979 cinfo.image_width = w;
980 cinfo.image_height = h;
981 cinfo.input_components = 1;
982 cinfo.in_color_space = JCS_GRAYSCALE;
983 jpeg_set_defaults(&cinfo);
984 jpeg_set_quality(&cinfo, quality, TRUE);
987 cinfo.comp_info[0].h_samp_factor = 1;
988 cinfo.comp_info[0].v_samp_factor = 1;
989 cinfo.comp_info[1].h_samp_factor = 1;
990 cinfo.comp_info[1].v_samp_factor = 1;
991 cinfo.comp_info[2].h_samp_factor = 1;
992 cinfo.comp_info[2].v_samp_factor = 1;
995 jpeg_start_compress(&cinfo, TRUE);
997 while (cinfo.next_scanline < cinfo.image_height)
1001 ptr = ((const int *)data) + cinfo.next_scanline * w;
1002 /* convert scaline from ARGB to RGB packed */
1003 for (j = 0, i = 0; i < w; i++)
1005 buf[j++] = ((*ptr) >> 24) & 0xff;
1008 jbuf = (JSAMPROW *)(&buf);
1009 jpeg_write_scanlines(&cinfo, jbuf, 1);
1012 jpeg_finish_compress(&cinfo);
1013 jpeg_destroy_compress(&cinfo);
1018 d = malloc(12 + sz1 + sz2);
1027 header[0] = 0xbeeff00d;
1030 if (_eet_image_words_bigendian)
1034 for (i = 0; i < 3; i++) SWAP32(header[i]);
1037 memcpy(d + 12, d1, sz1);
1038 memcpy(d + 12 + sz1, d2, sz2);
1042 *size = 12 + sz1 + sz2;
1044 } /* eet_data_image_jpeg_alpha_convert */
1047 eet_data_image_write_cipher(Eet_File *ef,
1049 const char *cipher_key,
1061 d = eet_data_image_encode(data, &size, w, h, alpha, comp, quality, lossy);
1066 v = eet_write_cipher(ef, name, d, size, 0, cipher_key);
1072 } /* eet_data_image_write_cipher */
1075 eet_data_image_write(Eet_File *ef,
1085 return eet_data_image_write_cipher(ef,
1095 } /* eet_data_image_write */
1098 eet_data_image_read_cipher(Eet_File *ef,
1100 const char *cipher_key,
1108 unsigned int *d = NULL;
1114 data = (void *)eet_read_direct(ef, name, &size);
1118 data = eet_read_cipher(ef, name, &size, cipher_key);
1124 d = eet_data_image_decode(data, size, w, h, alpha, comp, quality, lossy);
1130 } /* eet_data_image_read_cipher */
1133 eet_data_image_read(Eet_File *ef,
1142 return eet_data_image_read_cipher(ef, name, NULL, w, h, alpha,
1143 comp, quality, lossy);
1144 } /* eet_data_image_read */
1147 eet_data_image_read_to_surface_cipher(Eet_File *ef,
1149 const char *cipher_key,
1155 unsigned int row_stride,
1167 data = (void *)eet_read_direct(ef, name, &size);
1171 data = eet_read_cipher(ef, name, &size, cipher_key);
1177 res = eet_data_image_decode_to_surface(data, size, src_x, src_y, d,
1178 w, h, row_stride, alpha,
1179 comp, quality, lossy);
1185 } /* eet_data_image_read_to_surface_cipher */
1188 eet_data_image_read_to_surface(Eet_File *ef,
1195 unsigned int row_stride,
1201 return eet_data_image_read_to_surface_cipher(ef, name, NULL,
1204 alpha, comp, quality,
1206 } /* eet_data_image_read_to_surface */
1209 eet_data_image_header_read_cipher(Eet_File *ef,
1211 const char *cipher_key,
1225 data = (void *)eet_read_direct(ef, name, &size);
1229 data = eet_read_cipher(ef, name, &size, cipher_key);
1235 d = eet_data_image_header_decode(data, size, w, h, alpha,
1236 comp, quality, lossy);
1241 } /* eet_data_image_header_read_cipher */
1244 eet_data_image_header_read(Eet_File *ef,
1253 return eet_data_image_header_read_cipher(ef, name, NULL,
1255 comp, quality, lossy);
1256 } /* eet_data_image_header_read */
1259 eet_data_image_encode_cipher(const void *data,
1260 const char *cipher_key,
1270 void *ciphered_d = NULL;
1271 unsigned int ciphered_sz = 0;
1277 d = eet_data_image_lossless_compressed_convert(&size, data,
1280 /* eet_data_image_lossless_compressed_convert will refuse to compress something
1281 if the result is bigger than the entry. */
1282 if (comp <= 0 || d == NULL)
1283 d = eet_data_image_lossless_convert(&size, data, w, h, alpha);
1288 d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality);
1290 d = eet_data_image_jpeg_alpha_convert(&size, data,
1291 w, h, alpha, quality);
1296 if(!eet_cipher(d, size, cipher_key, strlen(cipher_key), &ciphered_d,
1314 } /* eet_data_image_encode_cipher */
1317 eet_data_image_encode(const void *data,
1326 return eet_data_image_encode_cipher(data, NULL, w, h, alpha,
1327 comp, quality, lossy, size_ret);
1328 } /* eet_data_image_encode */
1331 eet_data_image_header_decode_cipher(const void *data,
1332 const char *cipher_key,
1342 void *deciphered_d = NULL;
1343 unsigned int deciphered_sz = 0;
1347 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1348 &deciphered_d, &deciphered_sz))
1350 data = deciphered_d;
1351 size = deciphered_sz;
1358 if (_eet_image_words_bigendian == -1)
1360 unsigned long int v;
1362 v = htonl(0x12345678);
1363 if (v == 0x12345678)
1364 _eet_image_words_bigendian = 1;
1366 _eet_image_words_bigendian = 0;
1372 memcpy(header, data, 32);
1373 if (_eet_image_words_bigendian)
1377 for (i = 0; i < 8; i++) SWAP32(header[i]);
1380 if ((unsigned)header[0] == 0xac1dfeed)
1388 if ((iw < 1) || (ih < 1) || (iw > 8192) || (ih > 8192))
1391 if ((cp == 0) && (size < ((iw * ih * 4) + 32)))
1401 *alpha = al ? 1 : 0;
1414 else if ((unsigned)header[0] == 0xbeeff00d)
1416 unsigned int iw = 0, ih = 0;
1417 unsigned const char *dt;
1422 /* sz2 = header[2]; */
1425 ok = eet_data_image_jpeg_header_decode(dt, sz1, &iw, &ih);
1451 unsigned int iw = 0, ih = 0;
1454 ok = eet_data_image_jpeg_header_decode(data, size, &iw, &ih);
1480 } /* eet_data_image_header_decode_cipher */
1483 eet_data_image_header_decode(const void *data,
1492 return eet_data_image_header_decode_cipher(data,
1501 } /* eet_data_image_header_decode */
1504 _eet_data_image_copy_buffer(const unsigned int *src,
1511 unsigned int row_stride)
1513 src += src_x + src_y * src_w;
1515 if (row_stride == src_w * 4 && w == src_w)
1516 memcpy(dst, src, row_stride * h);
1519 unsigned int *over = dst;
1522 for (y = 0; y < h; ++y, src += src_w, over += row_stride)
1523 memcpy(over, src, w * 4);
1525 } /* _eet_data_image_copy_buffer */
1528 _eet_data_image_decode_inside(const void *data,
1537 unsigned int row_stride,
1543 if (lossy == 0 && quality == 100)
1547 body = ((unsigned int *)data) + 8;
1549 _eet_data_image_copy_buffer(body, src_x, src_y, src_w, d,
1553 if (src_h == h && src_w == w && row_stride == src_w * 4)
1558 uncompress((Bytef *)d, &dlen, (Bytef *)body,
1559 (uLongf)(size - 32));
1564 uLongf dlen = src_w * src_h * 4;
1566 /* FIXME: This could create a huge alloc. So compressed data and tile could not always work. */
1567 dtmp = malloc(dlen);
1571 uncompress(dtmp, &dlen, (Bytef *)body, (uLongf)(size - 32));
1573 _eet_data_image_copy_buffer((unsigned int *)dtmp,
1574 src_x, src_y, src_w, d,
1581 /* Fix swapiness. */
1582 if (_eet_image_words_bigendian)
1586 for (x = 0; x < (w * h); x++) SWAP32(d[x]);
1589 else if (comp == 0 && lossy == 1)
1593 unsigned const char *dt;
1597 memcpy(header, data, 32);
1598 if (_eet_image_words_bigendian)
1602 for (i = 0; i < 8; i++) SWAP32(header[i]);
1610 if (eet_data_image_jpeg_rgb_decode(dt, sz1, src_x, src_y, d, w, h,
1614 if (!eet_data_image_jpeg_alpha_decode(dt, sz2, src_x, src_y,
1615 d, w, h, row_stride))
1619 else if (!eet_data_image_jpeg_rgb_decode(data, size, src_x, src_y, d, w,
1627 } /* _eet_data_image_decode_inside */
1630 eet_data_image_decode_cipher(const void *data,
1631 const char *cipher_key,
1640 unsigned int *d = NULL;
1641 unsigned int iw, ih;
1642 int ialpha, icompress, iquality, ilossy;
1643 void *deciphered_d = NULL;
1644 unsigned int deciphered_sz = 0;
1648 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1649 &deciphered_d, &deciphered_sz))
1651 data = deciphered_d;
1652 size = deciphered_sz;
1659 /* All check are done during header decode, this simplify the code a lot. */
1660 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1661 &iquality, &ilossy))
1664 d = malloc(iw * ih * 4);
1668 if (!_eet_data_image_decode_inside(data, size, 0, 0, iw, ih, d, iw, ih, iw *
1669 4, ialpha, icompress, iquality, ilossy))
1688 *quality = iquality;
1694 } /* eet_data_image_decode_cipher */
1697 eet_data_image_decode(const void *data,
1706 return eet_data_image_decode_cipher(data, NULL, size, w, h,
1707 alpha, comp, quality, lossy);
1708 } /* eet_data_image_decode */
1711 eet_data_image_decode_to_surface_cipher(const void *data,
1712 const char *cipher_key,
1719 unsigned int row_stride,
1725 unsigned int iw, ih;
1726 int ialpha, icompress, iquality, ilossy;
1727 void *deciphered_d = NULL;
1728 unsigned int deciphered_sz = 0;
1732 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1733 &deciphered_d, &deciphered_sz))
1735 data = deciphered_d;
1736 size = deciphered_sz;
1743 /* All check are done during header decode, this simplify the code a lot. */
1744 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1745 &iquality, &ilossy))
1751 if (w * 4 > row_stride)
1754 if (w > iw || h > ih)
1757 if (!_eet_data_image_decode_inside(data, size, src_x, src_y, iw, ih, d, w, h,
1758 row_stride, ialpha, icompress, iquality,
1769 *quality = iquality;
1775 } /* eet_data_image_decode_to_surface_cipher */
1778 eet_data_image_decode_to_surface(const void *data,
1785 unsigned int row_stride,
1791 return eet_data_image_decode_to_surface_cipher(data, NULL, size,
1794 alpha, comp, quality,
1796 } /* eet_data_image_decode_to_surface */