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 */
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>
32 #endif /* ifdef _WIN32 */
41 #include "Eet_private.h"
48 typedef struct _JPEG_error_mgr *emptr;
52 struct _JPEG_error_mgr
54 struct jpeg_error_mgr pub;
55 jmp_buf setjmp_buffer;
58 struct jpeg_membuf_src
60 struct jpeg_source_mgr pub;
62 const unsigned char *buf;
64 struct jpeg_membuf_src *self;
68 _eet_jpeg_membuf_src_init(j_decompress_ptr cinfo)
70 /* FIXME: Use attribute unused */
75 _eet_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
77 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
78 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
80 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
81 src->pub.next_input_byte = jpeg_eoi;
87 _eet_jpeg_membuf_src_skip(j_decompress_ptr cinfo,
90 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
92 src->pub.bytes_in_buffer -= num_bytes;
93 src->pub.next_input_byte += num_bytes;
97 _eet_jpeg_membuf_src_term(j_decompress_ptr cinfo)
99 struct jpeg_membuf_src *src = ((struct jpeg_membuf_src *)cinfo->src)->self;
106 eet_jpeg_membuf_src(j_decompress_ptr cinfo,
110 struct jpeg_membuf_src *src;
112 src = calloc(1, sizeof(*src));
118 cinfo->src = &src->pub;
121 src->pub.init_source = _eet_jpeg_membuf_src_init;
122 src->pub.fill_input_buffer = _eet_jpeg_membuf_src_fill;
123 src->pub.skip_input_data = _eet_jpeg_membuf_src_skip;
124 src->pub.resync_to_restart = jpeg_resync_to_restart;
125 src->pub.term_source = _eet_jpeg_membuf_src_term;
126 src->pub.bytes_in_buffer = src->len;
127 src->pub.next_input_byte = src->buf;
132 struct jpeg_membuf_dst
134 struct jpeg_destination_mgr pub;
142 struct jpeg_membuf_dst *self;
146 _eet_jpeg_membuf_dst_init(j_compress_ptr cinfo)
148 /* FIXME: Use eina attribute */
153 _eet_jpeg_membuf_dst_flush(j_compress_ptr cinfo)
155 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
158 if (dst->len >= 0x40000000 ||
159 !(buf = realloc(dst->buf, dst->len * 2)))
162 dst->pub.next_output_byte = dst->buf;
163 dst->pub.free_in_buffer = dst->len;
167 dst->pub.next_output_byte =
168 buf + ((unsigned char *)dst->pub.next_output_byte - dst->buf);
170 dst->pub.free_in_buffer += dst->len;
177 _eet_jpeg_membuf_dst_term(j_compress_ptr cinfo)
179 struct jpeg_membuf_dst *dst = ((struct jpeg_membuf_dst *)cinfo->dest)->self;
183 *dst->dst_buf = NULL;
189 *dst->dst_buf = dst->buf;
190 *dst->dst_len = (unsigned char *)dst->pub.next_output_byte - dst->buf;
198 eet_jpeg_membuf_dst(j_compress_ptr cinfo,
202 struct jpeg_membuf_dst *dst;
204 dst = calloc(1, sizeof(*dst));
208 dst->buf = malloc(32768);
218 cinfo->dest = &dst->pub;
219 dst->pub.init_destination = _eet_jpeg_membuf_dst_init;
220 dst->pub.empty_output_buffer = _eet_jpeg_membuf_dst_flush;
221 dst->pub.term_destination = _eet_jpeg_membuf_dst_term;
222 dst->pub.free_in_buffer = dst->len;
223 dst->pub.next_output_byte = dst->buf;
233 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
234 static void _JPEGErrorHandler(j_common_ptr cinfo);
235 static void _JPEGErrorHandler2(j_common_ptr cinfo,
239 eet_data_image_jpeg_header_decode(const void *data,
244 eet_data_image_jpeg_rgb_decode(const void *data,
251 unsigned int row_stride);
253 eet_data_image_jpeg_alpha_decode(const void *data,
260 unsigned int row_stride);
262 eet_data_image_lossless_convert(int *size,
268 eet_data_image_lossless_compressed_convert(int *size,
275 eet_data_image_jpeg_convert(int *size,
282 eet_data_image_jpeg_alpha_convert(int *size,
291 static int _eet_image_words_bigendian = -1;
295 #define SWAP64(x) (x) = \
296 ((((unsigned long long)(x) & 0x00000000000000ffULL) << 56) | \
297 (((unsigned long long)(x) & 0x000000000000ff00ULL) << 40) | \
298 (((unsigned long long)(x) & 0x0000000000ff0000ULL) << 24) | \
299 (((unsigned long long)(x) & 0x00000000ff000000ULL) << 8) | \
300 (((unsigned long long)(x) & 0x000000ff00000000ULL) >> 8) | \
301 (((unsigned long long)(x) & 0x0000ff0000000000ULL) >> 24) | \
302 (((unsigned long long)(x) & 0x00ff000000000000ULL) >> 40) | \
303 (((unsigned long long)(x) & 0xff00000000000000ULL) >> 56))
304 #define SWAP32(x) (x) = \
305 ((((int)(x) & 0x000000ff) << 24) | \
306 (((int)(x) & 0x0000ff00) << 8) | \
307 (((int)(x) & 0x00ff0000) >> 8) | \
308 (((int)(x) & 0xff000000) >> 24))
309 #define SWAP16(x) (x) = \
310 ((((short)(x) & 0x00ff) << 8) | \
311 (((short)(x) & 0xff00) >> 8))
315 #endif /* ifdef CONV8 */
318 #endif /* ifdef CONV16 */
321 #endif /* ifdef CONV32 */
324 #endif /* ifdef CONV64 */
327 #define CONV16(x) {if (_eet_image_words_bigendian) {SWAP16(x); }}
328 #define CONV32(x) {if (_eet_image_words_bigendian) {SWAP32(x); }}
329 #define CONV64(x) {if (_eet_image_words_bigendian) {SWAP64(x); }}
334 _JPEGFatalErrorHandler(j_common_ptr cinfo)
338 errmgr = (emptr)cinfo->err;
339 /* cinfo->err->output_message(cinfo);*/
340 longjmp(errmgr->setjmp_buffer, 1);
345 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
349 /* errmgr = (emptr) cinfo->err; */
350 /* cinfo->err->output_message(cinfo);*/
351 /* longjmp(errmgr->setjmp_buffer, 1);*/
356 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__,
357 int msg_level __UNUSED__)
361 /* errmgr = (emptr) cinfo->err; */
362 /* cinfo->err->output_message(cinfo);*/
363 /* longjmp(errmgr->setjmp_buffer, 1);*/
368 eet_data_image_jpeg_header_decode(const void *data,
373 struct jpeg_decompress_struct cinfo;
374 struct _JPEG_error_mgr jerr;
376 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
378 cinfo.err = jpeg_std_error(&(jerr.pub));
379 jerr.pub.error_exit = _JPEGFatalErrorHandler;
380 jerr.pub.emit_message = _JPEGErrorHandler2;
381 jerr.pub.output_message = _JPEGErrorHandler;
382 if (setjmp(jerr.setjmp_buffer))
385 jpeg_create_decompress(&cinfo);
387 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
389 jpeg_destroy_decompress(&cinfo);
393 jpeg_read_header(&cinfo, TRUE);
394 cinfo.do_fancy_upsampling = FALSE;
395 cinfo.do_block_smoothing = FALSE;
396 jpeg_start_decompress(&cinfo);
399 *w = cinfo.output_width;
400 *h = cinfo.output_height;
405 jpeg_destroy_decompress(&cinfo);
407 if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
414 eet_data_image_jpeg_rgb_decode(const void *data,
421 unsigned int row_stride)
423 struct jpeg_decompress_struct cinfo;
424 struct _JPEG_error_mgr jerr;
425 unsigned char *ptr, *line[16], *tdata = NULL;
426 unsigned int *ptr2, *tmp;
428 unsigned int x, y, l, scans;
431 /* FIXME: handle src_x, src_y and row_stride correctly */
435 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
437 cinfo.err = jpeg_std_error(&(jerr.pub));
438 jerr.pub.error_exit = _JPEGFatalErrorHandler;
439 jerr.pub.emit_message = _JPEGErrorHandler2;
440 jerr.pub.output_message = _JPEGErrorHandler;
441 if (setjmp(jerr.setjmp_buffer))
444 jpeg_create_decompress(&cinfo);
446 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
448 jpeg_destroy_decompress(&cinfo);
452 jpeg_read_header(&cinfo, TRUE);
453 cinfo.dct_method = JDCT_ISLOW; // JDCT_FLOAT JDCT_IFAST(quality loss)
454 cinfo.do_fancy_upsampling = FALSE;
455 cinfo.do_block_smoothing = FALSE;
456 jpeg_start_decompress(&cinfo);
459 iw = cinfo.output_width;
460 ih = cinfo.output_height;
461 if ((iw != w) || (ih != h))
466 jpeg_destroy_decompress(&cinfo);
470 /* end head decoding */
472 if (cinfo.rec_outbuf_height > 16)
477 jpeg_destroy_decompress(&cinfo);
481 tdata = alloca((iw) * 16 * 3);
484 if (cinfo.output_components == 3)
486 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
487 line[i] = tdata + (i * (iw) * 3);
488 for (l = 0; l < ih; l += cinfo.rec_outbuf_height)
490 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
491 scans = cinfo.rec_outbuf_height;
492 if ((ih - l) < scans)
497 if (l + scans >= src_y && l < src_y + h)
503 for (ptr += 3 * iw * y; y < scans && (y + l) < (src_y + h);
508 for (x = 0; x < w; x++)
512 ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
517 ptr2 = tmp + row_stride / 4;
522 else if (cinfo.output_components == 1)
524 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
525 line[i] = tdata + (i * (iw));
526 for (l = 0; l < (ih); l += cinfo.rec_outbuf_height)
528 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
529 scans = cinfo.rec_outbuf_height;
530 if (((ih) - l) < scans)
535 if (l >= src_y && l < src_y + h)
541 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
545 for (x = 0; x < w; x++)
549 ((ptr[0]) << 16) | ((ptr[0]) << 8) | (ptr[0]);
554 ptr2 = tmp + row_stride / 4;
560 /* end data decoding */
561 jpeg_finish_decompress(&cinfo);
562 jpeg_destroy_decompress(&cinfo);
567 eet_data_image_jpeg_alpha_decode(const void *data,
574 unsigned int row_stride)
576 struct jpeg_decompress_struct cinfo;
577 struct _JPEG_error_mgr jerr;
578 unsigned char *ptr, *line[16], *tdata = NULL;
579 unsigned int *ptr2, *tmp;
580 unsigned int x, y, l, scans;
583 /* FIXME: handle src_x, src_y and row_stride correctly */
587 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
589 cinfo.err = jpeg_std_error(&(jerr.pub));
590 jerr.pub.error_exit = _JPEGFatalErrorHandler;
591 jerr.pub.emit_message = _JPEGErrorHandler2;
592 jerr.pub.output_message = _JPEGErrorHandler;
593 if (setjmp(jerr.setjmp_buffer))
596 jpeg_create_decompress(&cinfo);
598 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
600 jpeg_destroy_decompress(&cinfo);
604 jpeg_read_header(&cinfo, TRUE);
605 cinfo.dct_method = JDCT_ISLOW; // JDCT_FLOAT JDCT_IFAST(quality loss)
606 cinfo.do_fancy_upsampling = FALSE;
607 cinfo.do_block_smoothing = FALSE;
608 jpeg_start_decompress(&cinfo);
611 iw = cinfo.output_width;
612 if (w != cinfo.output_width
613 || h != cinfo.output_height)
618 jpeg_destroy_decompress(&cinfo);
622 /* end head decoding */
624 if (cinfo.rec_outbuf_height > 16)
629 jpeg_destroy_decompress(&cinfo);
633 tdata = alloca(w * 16 * 3);
636 if (cinfo.output_components == 1)
638 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
639 line[i] = tdata + (i * w);
640 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
642 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
643 scans = cinfo.rec_outbuf_height;
649 if (l >= src_y && l < src_y + h)
655 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
659 for (x = 0; x < w; x++)
662 ((*ptr2) & 0x00ffffff) |
668 ptr2 = tmp + row_stride / 4;
674 /* end data decoding */
675 jpeg_finish_decompress(&cinfo);
676 jpeg_destroy_decompress(&cinfo);
681 eet_data_image_lossless_convert(int *size,
687 if (_eet_image_words_bigendian == -1)
691 v = htonl(0x12345678);
693 _eet_image_words_bigendian = 1;
695 _eet_image_words_bigendian = 0;
702 d = malloc((w * h * 4) + (8 * 4));
709 header[0] = 0xac1dfeed;
714 memcpy(d + 32, data, w * h * 4);
716 if (_eet_image_words_bigendian)
720 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
723 *size = ((w * h * 4) + (8 * 4));
729 eet_data_image_lossless_compressed_convert(int *size,
736 if (_eet_image_words_bigendian == -1)
740 v = htonl(0x12345678);
742 _eet_image_words_bigendian = 1;
744 _eet_image_words_bigendian = 0;
748 unsigned char *d, *comp;
749 int *header, ret, ok = 1;
752 buflen = (((w * h * 101) / 100) + 3) * 4;
753 ret = LZ4_compressBound((w * h * 4));
754 if ((ret > 0) && ((uLongf)ret > buflen)) buflen = ret;
756 comp = malloc(buflen);
757 if (!comp) return NULL;
761 case EET_COMPRESSION_VERYFAST:
762 ret = LZ4_compressHC((const char *)data, (char *)comp,
764 if (ret <= 0) ok = 0;
767 case EET_COMPRESSION_SUPERFAST:
768 ret = LZ4_compress((const char *)data, (char *)comp,
770 if (ret <= 0) ok = 0;
773 default: /* zlib etc. */
774 ret = compress2((Bytef *)comp, &buflen, (Bytef *)(data),
775 (uLong)(w * h * 4), compression);
776 if (ret != Z_OK) ok = 0;
779 if ((!ok) || (buflen > (w * h * 4)))
786 d = malloc((8 * sizeof(int)) + buflen);
794 memset(d, 0, 8 * sizeof(int));
795 header[0] = 0xac1dfeed;
799 header[4] = compression;
801 if (_eet_image_words_bigendian)
805 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
808 memcpy(d + (8 * sizeof(int)), comp, buflen);
809 *size = (8 * sizeof(int)) + buflen;
816 eet_data_image_jpeg_convert(int *size,
823 struct jpeg_compress_struct cinfo;
824 struct _JPEG_error_mgr jerr;
831 (void)alpha; /* unused */
835 memset(&cinfo, 0, sizeof (struct jpeg_compress_struct));
837 cinfo.err = jpeg_std_error(&(jerr.pub));
838 jerr.pub.error_exit = _JPEGFatalErrorHandler;
839 jerr.pub.emit_message = _JPEGErrorHandler2;
840 jerr.pub.output_message = _JPEGErrorHandler;
841 if (setjmp(jerr.setjmp_buffer))
844 jpeg_create_compress(&cinfo);
846 if (eet_jpeg_membuf_dst(&cinfo, &d, &sz))
848 jpeg_destroy_compress(&cinfo);
852 cinfo.image_width = w;
853 cinfo.image_height = h;
854 cinfo.input_components = 3;
855 cinfo.in_color_space = JCS_RGB;
856 cinfo.optimize_coding = FALSE;
857 cinfo.dct_method = JDCT_ISLOW; // JDCT_FLOAT JDCT_IFAST(quality loss)
858 if (quality < 60) cinfo.dct_method = JDCT_IFAST;
859 jpeg_set_defaults(&cinfo);
860 jpeg_set_quality(&cinfo, quality, TRUE);
864 cinfo.comp_info[0].h_samp_factor = 1;
865 cinfo.comp_info[0].v_samp_factor = 1;
866 cinfo.comp_info[1].h_samp_factor = 1;
867 cinfo.comp_info[1].v_samp_factor = 1;
868 cinfo.comp_info[2].h_samp_factor = 1;
869 cinfo.comp_info[2].v_samp_factor = 1;
872 jpeg_start_compress(&cinfo, TRUE);
874 while (cinfo.next_scanline < cinfo.image_height)
878 /* convert scaline from ARGB to RGB packed */
879 ptr = ((const int *)data) + cinfo.next_scanline * w;
880 for (j = 0, i = 0; i < w; i++)
882 buf[j++] = ((*ptr) >> 16) & 0xff;
883 buf[j++] = ((*ptr) >> 8) & 0xff;
884 buf[j++] = ((*ptr)) & 0xff;
887 jbuf = (JSAMPROW *)(&buf);
888 jpeg_write_scanlines(&cinfo, jbuf, 1);
891 jpeg_finish_compress(&cinfo);
892 jpeg_destroy_compress(&cinfo);
899 eet_data_image_jpeg_alpha_convert(int *size,
906 unsigned char *d1, *d2;
911 (void)alpha; /* unused */
913 if (_eet_image_words_bigendian == -1)
917 v = htonl(0x12345678);
919 _eet_image_words_bigendian = 1;
921 _eet_image_words_bigendian = 0;
928 struct _JPEG_error_mgr jerr;
930 struct jpeg_compress_struct cinfo;
935 cinfo.err = jpeg_std_error(&(jerr.pub));
936 jerr.pub.error_exit = _JPEGFatalErrorHandler;
937 jerr.pub.emit_message = _JPEGErrorHandler2;
938 jerr.pub.output_message = _JPEGErrorHandler;
939 if (setjmp(jerr.setjmp_buffer))
942 jpeg_create_compress(&cinfo);
943 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
945 jpeg_destroy_compress(&cinfo);
949 cinfo.image_width = w;
950 cinfo.image_height = h;
951 cinfo.input_components = 3;
952 cinfo.in_color_space = JCS_RGB;
953 cinfo.optimize_coding = FALSE;
954 cinfo.dct_method = JDCT_ISLOW; // JDCT_FLOAT JDCT_IFAST(quality loss)
955 if (quality < 60) cinfo.dct_method = JDCT_IFAST;
956 jpeg_set_defaults(&cinfo);
957 jpeg_set_quality(&cinfo, quality, TRUE);
960 cinfo.comp_info[0].h_samp_factor = 1;
961 cinfo.comp_info[0].v_samp_factor = 1;
962 cinfo.comp_info[1].h_samp_factor = 1;
963 cinfo.comp_info[1].v_samp_factor = 1;
964 cinfo.comp_info[2].h_samp_factor = 1;
965 cinfo.comp_info[2].v_samp_factor = 1;
968 jpeg_start_compress(&cinfo, TRUE);
970 while (cinfo.next_scanline < cinfo.image_height)
974 ptr = ((const int *)data) + cinfo.next_scanline * w;
975 /* convert scaline from ARGB to RGB packed */
976 for (j = 0, i = 0; i < w; i++)
978 buf[j++] = ((*ptr) >> 16) & 0xff;
979 buf[j++] = ((*ptr) >> 8) & 0xff;
980 buf[j++] = ((*ptr)) & 0xff;
983 jbuf = (JSAMPROW *)(&buf);
984 jpeg_write_scanlines(&cinfo, jbuf, 1);
987 jpeg_finish_compress(&cinfo);
988 jpeg_destroy_compress(&cinfo);
997 struct _JPEG_error_mgr jerr;
999 struct jpeg_compress_struct cinfo;
1002 buf = alloca(3 * w);
1004 cinfo.err = jpeg_std_error(&(jerr.pub));
1005 jerr.pub.error_exit = _JPEGFatalErrorHandler;
1006 jerr.pub.emit_message = _JPEGErrorHandler2;
1007 jerr.pub.output_message = _JPEGErrorHandler;
1008 if (setjmp(jerr.setjmp_buffer))
1014 jpeg_create_compress(&cinfo);
1015 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
1017 jpeg_destroy_compress(&cinfo);
1022 cinfo.image_width = w;
1023 cinfo.image_height = h;
1024 cinfo.input_components = 1;
1025 cinfo.in_color_space = JCS_GRAYSCALE;
1026 jpeg_set_defaults(&cinfo);
1027 jpeg_set_quality(&cinfo, quality, TRUE);
1030 cinfo.comp_info[0].h_samp_factor = 1;
1031 cinfo.comp_info[0].v_samp_factor = 1;
1032 cinfo.comp_info[1].h_samp_factor = 1;
1033 cinfo.comp_info[1].v_samp_factor = 1;
1034 cinfo.comp_info[2].h_samp_factor = 1;
1035 cinfo.comp_info[2].v_samp_factor = 1;
1038 jpeg_start_compress(&cinfo, TRUE);
1040 while (cinfo.next_scanline < cinfo.image_height)
1044 ptr = ((const int *)data) + cinfo.next_scanline * w;
1045 /* convert scaline from ARGB to RGB packed */
1046 for (j = 0, i = 0; i < w; i++)
1048 buf[j++] = ((*ptr) >> 24) & 0xff;
1051 jbuf = (JSAMPROW *)(&buf);
1052 jpeg_write_scanlines(&cinfo, jbuf, 1);
1055 jpeg_finish_compress(&cinfo);
1056 jpeg_destroy_compress(&cinfo);
1061 d = malloc(12 + sz1 + sz2);
1070 header[0] = 0xbeeff00d;
1073 if (_eet_image_words_bigendian)
1077 for (i = 0; i < 3; i++) SWAP32(header[i]);
1080 memcpy(d + 12, d1, sz1);
1081 memcpy(d + 12 + sz1, d2, sz2);
1085 *size = 12 + sz1 + sz2;
1090 eet_data_image_write_cipher(Eet_File *ef,
1092 const char *cipher_key,
1104 d = eet_data_image_encode(data, &size, w, h, alpha, comp, quality, lossy);
1109 v = eet_write_cipher(ef, name, d, size, 0, cipher_key);
1118 eet_data_image_write(Eet_File *ef,
1128 return eet_data_image_write_cipher(ef,
1141 eet_data_image_read_cipher(Eet_File *ef,
1143 const char *cipher_key,
1151 unsigned int *d = NULL;
1157 data = (void *)eet_read_direct(ef, name, &size);
1161 data = eet_read_cipher(ef, name, &size, cipher_key);
1167 d = eet_data_image_decode(data, size, w, h, alpha, comp, quality, lossy);
1176 eet_data_image_read(Eet_File *ef,
1185 return eet_data_image_read_cipher(ef, name, NULL, w, h, alpha,
1186 comp, quality, lossy);
1190 eet_data_image_read_to_surface_cipher(Eet_File *ef,
1192 const char *cipher_key,
1198 unsigned int row_stride,
1210 data = (void *)eet_read_direct(ef, name, &size);
1214 data = eet_read_cipher(ef, name, &size, cipher_key);
1220 res = eet_data_image_decode_to_surface(data, size, src_x, src_y, d,
1221 w, h, row_stride, alpha,
1222 comp, quality, lossy);
1231 eet_data_image_read_to_surface(Eet_File *ef,
1238 unsigned int row_stride,
1244 return eet_data_image_read_to_surface_cipher(ef, name, NULL,
1247 alpha, comp, quality,
1252 eet_data_image_header_read_cipher(Eet_File *ef,
1254 const char *cipher_key,
1268 data = (void *)eet_read_direct(ef, name, &size);
1272 data = eet_read_cipher(ef, name, &size, cipher_key);
1278 d = eet_data_image_header_decode(data, size, w, h, alpha,
1279 comp, quality, lossy);
1287 eet_data_image_header_read(Eet_File *ef,
1296 return eet_data_image_header_read_cipher(ef, name, NULL,
1298 comp, quality, lossy);
1302 eet_data_image_encode_cipher(const void *data,
1303 const char *cipher_key,
1313 void *ciphered_d = NULL;
1314 unsigned int ciphered_sz = 0;
1320 d = eet_data_image_lossless_compressed_convert(&size, data,
1323 /* eet_data_image_lossless_compressed_convert will refuse to compress something
1324 if the result is bigger than the entry. */
1325 if (comp <= 0 || !d)
1326 d = eet_data_image_lossless_convert(&size, data, w, h, alpha);
1331 d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality);
1333 d = eet_data_image_jpeg_alpha_convert(&size, data,
1334 w, h, alpha, quality);
1339 if(!eet_cipher(d, size, cipher_key, strlen(cipher_key), &ciphered_d,
1360 eet_data_image_encode(const void *data,
1369 return eet_data_image_encode_cipher(data, NULL, w, h, alpha,
1370 comp, quality, lossy, size_ret);
1374 eet_data_image_header_decode_cipher(const void *data,
1375 const char *cipher_key,
1385 void *deciphered_d = NULL;
1386 unsigned int deciphered_sz = 0;
1390 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1391 &deciphered_d, &deciphered_sz))
1393 data = deciphered_d;
1394 size = deciphered_sz;
1401 if (_eet_image_words_bigendian == -1)
1403 unsigned long int v;
1405 v = htonl(0x12345678);
1406 if (v == 0x12345678)
1407 _eet_image_words_bigendian = 1;
1409 _eet_image_words_bigendian = 0;
1415 memcpy(header, data, 32);
1416 if (_eet_image_words_bigendian)
1420 for (i = 0; i < 8; i++) SWAP32(header[i]);
1423 if ((unsigned)header[0] == 0xac1dfeed)
1431 if ((iw < 1) || (ih < 1) || (iw > 8192) || (ih > 8192))
1434 if ((cp == 0) && (size < ((iw * ih * 4) + 32)))
1444 *alpha = al ? 1 : 0;
1457 else if ((unsigned)header[0] == 0xbeeff00d)
1459 unsigned int iw = 0, ih = 0;
1460 unsigned const char *dt;
1465 /* sz2 = header[2]; */
1468 ok = eet_data_image_jpeg_header_decode(dt, sz1, &iw, &ih);
1494 unsigned int iw = 0, ih = 0;
1497 ok = eet_data_image_jpeg_header_decode(data, size, &iw, &ih);
1526 eet_data_image_header_decode(const void *data,
1535 return eet_data_image_header_decode_cipher(data,
1547 _eet_data_image_copy_buffer(const unsigned int *src,
1554 unsigned int row_stride)
1556 src += src_x + src_y * src_w;
1558 if (row_stride == src_w * 4 && w == src_w)
1559 memcpy(dst, src, row_stride * h);
1562 unsigned int *over = dst;
1565 for (y = 0; y < h; ++y, src += src_w, over += row_stride)
1566 memcpy(over, src, w * 4);
1571 _eet_data_image_decode_inside(const void *data,
1580 unsigned int row_stride,
1586 if (lossy == 0 && quality == 100)
1590 body = ((unsigned int *)data) + 8;
1592 _eet_data_image_copy_buffer(body, src_x, src_y, src_w, d,
1596 if ((src_h == h) && (src_w == w) && (row_stride == src_w * 4))
1600 case EET_COMPRESSION_VERYFAST:
1601 case EET_COMPRESSION_SUPERFAST:
1602 if (LZ4_uncompress((const char *)body,
1603 (char *)d, w * h * 4)
1604 != (size - 32)) return 0;
1608 uLongf dlen = w * h * 4;
1610 if (uncompress((Bytef *)d, &dlen, (Bytef *)body,
1611 (uLongf)(size - 32)) != Z_OK)
1621 case EET_COMPRESSION_VERYFAST:
1622 case EET_COMPRESSION_SUPERFAST:
1626 dtmp = malloc(src_w * src_h * 4);
1627 if (!dtmp) return 0;
1628 if (LZ4_uncompress((const char *)body,
1635 _eet_data_image_copy_buffer((unsigned int *)dtmp,
1636 src_x, src_y, src_w, d,
1644 uLongf dlen = src_w * src_h * 4;
1646 /* FIXME: This could create a huge alloc. So
1647 compressed data and tile could not always work.*/
1648 dtmp = malloc(dlen);
1649 if (!dtmp) return 0;
1651 if (uncompress(dtmp, &dlen, (Bytef *)body,
1652 (uLongf)(size - 32)) != Z_OK)
1657 _eet_data_image_copy_buffer((unsigned int *)dtmp,
1658 src_x, src_y, src_w, d,
1665 /* Fix swapiness. */
1666 if (_eet_image_words_bigendian)
1670 for (x = 0; x < (w * h); x++) SWAP32(d[x]);
1673 else if (comp == 0 && lossy == 1)
1677 unsigned const char *dt;
1681 memcpy(header, data, 32);
1682 if (_eet_image_words_bigendian)
1686 for (i = 0; i < 8; i++) SWAP32(header[i]);
1694 if (eet_data_image_jpeg_rgb_decode(dt, sz1, src_x, src_y, d, w, h,
1698 if (!eet_data_image_jpeg_alpha_decode(dt, sz2, src_x, src_y,
1699 d, w, h, row_stride))
1703 else if (!eet_data_image_jpeg_rgb_decode(data, size, src_x, src_y, d, w,
1714 eet_data_image_decode_cipher(const void *data,
1715 const char *cipher_key,
1724 unsigned int *d = NULL;
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))
1748 d = malloc(iw * ih * 4);
1752 if (!_eet_data_image_decode_inside(data, size, 0, 0, iw, ih, d, iw, ih, iw *
1753 4, ialpha, icompress, iquality, ilossy))
1772 *quality = iquality;
1781 eet_data_image_decode(const void *data,
1790 return eet_data_image_decode_cipher(data, NULL, size, w, h,
1791 alpha, comp, quality, lossy);
1795 eet_data_image_decode_to_surface_cipher(const void *data,
1796 const char *cipher_key,
1803 unsigned int row_stride,
1809 unsigned int iw, ih;
1810 int ialpha, icompress, iquality, ilossy;
1811 void *deciphered_d = NULL;
1812 unsigned int deciphered_sz = 0;
1816 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1817 &deciphered_d, &deciphered_sz))
1819 data = deciphered_d;
1820 size = deciphered_sz;
1827 /* All check are done during header decode, this simplify the code a lot. */
1828 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1829 &iquality, &ilossy))
1835 if (w * 4 > row_stride)
1838 if (w > iw || h > ih)
1841 if (!_eet_data_image_decode_inside(data, size, src_x, src_y, iw, ih, d, w, h,
1842 row_stride, ialpha, icompress, iquality,
1853 *quality = iquality;
1862 eet_data_image_decode_to_surface(const void *data,
1869 unsigned int row_stride,
1875 return eet_data_image_decode_to_surface_cipher(data, NULL, size,
1878 alpha, comp, quality,