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;
60 struct jpeg_membuf_src *self;
64 _eet_jpeg_membuf_src_init(j_decompress_ptr cinfo)
66 /* FIXME: Use attribute unused */
68 } /* _eet_jpeg_membuf_src_init */
71 _eet_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
73 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
74 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
76 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
77 src->pub.next_input_byte = jpeg_eoi;
80 } /* _eet_jpeg_membuf_src_fill */
83 _eet_jpeg_membuf_src_skip(j_decompress_ptr cinfo,
86 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
88 src->pub.bytes_in_buffer -= num_bytes;
89 src->pub.next_input_byte += num_bytes;
90 } /* _eet_jpeg_membuf_src_skip */
93 _eet_jpeg_membuf_src_term(j_decompress_ptr cinfo)
95 struct jpeg_membuf_src *src = ((struct jpeg_membuf_src *)cinfo->src)->self;
99 } /* _eet_jpeg_membuf_src_term */
102 eet_jpeg_membuf_src(j_decompress_ptr cinfo,
106 struct jpeg_membuf_src *src;
108 src = calloc(1, sizeof(*src));
114 cinfo->src = &src->pub;
117 src->pub.init_source = _eet_jpeg_membuf_src_init;
118 src->pub.fill_input_buffer = _eet_jpeg_membuf_src_fill;
119 src->pub.skip_input_data = _eet_jpeg_membuf_src_skip;
120 src->pub.resync_to_restart = jpeg_resync_to_restart;
121 src->pub.term_source = _eet_jpeg_membuf_src_term;
122 src->pub.bytes_in_buffer = src->len;
123 src->pub.next_input_byte = src->buf;
126 } /* eet_jpeg_membuf_src */
128 struct jpeg_membuf_dst
130 struct jpeg_destination_mgr pub;
138 struct jpeg_membuf_dst *self;
142 _eet_jpeg_membuf_dst_init(j_compress_ptr cinfo)
144 /* FIXME: Use eina attribute */
146 } /* _eet_jpeg_membuf_dst_init */
149 _eet_jpeg_membuf_dst_flush(j_compress_ptr cinfo)
151 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
154 if (dst->len >= 0x40000000 ||
155 !(buf = realloc(dst->buf, dst->len * 2)))
158 dst->pub.next_output_byte = dst->buf;
159 dst->pub.free_in_buffer = dst->len;
163 dst->pub.next_output_byte =
164 buf + ((unsigned char *)dst->pub.next_output_byte - dst->buf);
166 dst->pub.free_in_buffer += dst->len;
170 } /* _eet_jpeg_membuf_dst_flush */
173 _eet_jpeg_membuf_dst_term(j_compress_ptr cinfo)
175 struct jpeg_membuf_dst *dst = ((struct jpeg_membuf_dst *)cinfo->dest)->self;
179 *dst->dst_buf = NULL;
185 *dst->dst_buf = dst->buf;
186 *dst->dst_len = (unsigned char *)dst->pub.next_output_byte - dst->buf;
191 } /* _eet_jpeg_membuf_dst_term */
194 eet_jpeg_membuf_dst(j_compress_ptr cinfo,
198 struct jpeg_membuf_dst *dst;
200 dst = calloc(1, sizeof(*dst));
204 dst->buf = malloc(32768);
214 cinfo->dest = &dst->pub;
215 dst->pub.init_destination = _eet_jpeg_membuf_dst_init;
216 dst->pub.empty_output_buffer = _eet_jpeg_membuf_dst_flush;
217 dst->pub.term_destination = _eet_jpeg_membuf_dst_term;
218 dst->pub.free_in_buffer = dst->len;
219 dst->pub.next_output_byte = dst->buf;
225 } /* eet_jpeg_membuf_dst */
229 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
230 static void _JPEGErrorHandler(j_common_ptr cinfo);
231 static void _JPEGErrorHandler2(j_common_ptr cinfo,
234 static int eet_data_image_jpeg_header_decode(const void *data,
238 static int eet_data_image_jpeg_rgb_decode(const void *data,
245 unsigned int row_stride);
246 static void * eet_data_image_jpeg_alpha_decode(const void *data,
253 unsigned int row_stride);
254 static void * eet_data_image_lossless_convert(int *size,
259 static void * eet_data_image_lossless_compressed_convert(int *size,
265 static void * eet_data_image_jpeg_convert(int *size,
271 static void * eet_data_image_jpeg_alpha_convert(int *size,
280 static int _eet_image_words_bigendian = -1;
284 #define SWAP64(x) (x) =\
285 ((((unsigned long long)(x) & 0x00000000000000ffULL) << 56) |\
286 (((unsigned long long)(x) & 0x000000000000ff00ULL) << 40) |\
287 (((unsigned long long)(x) & 0x0000000000ff0000ULL) << 24) |\
288 (((unsigned long long)(x) & 0x00000000ff000000ULL) << 8) |\
289 (((unsigned long long)(x) & 0x000000ff00000000ULL) >> 8) |\
290 (((unsigned long long)(x) & 0x0000ff0000000000ULL) >> 24) |\
291 (((unsigned long long)(x) & 0x00ff000000000000ULL) >> 40) |\
292 (((unsigned long long)(x) & 0xff00000000000000ULL) >> 56))
293 #define SWAP32(x) (x) =\
294 ((((int)(x) & 0x000000ff) << 24) |\
295 (((int)(x) & 0x0000ff00) << 8) |\
296 (((int)(x) & 0x00ff0000) >> 8) |\
297 (((int)(x) & 0xff000000) >> 24))
298 #define SWAP16(x) (x) =\
299 ((((short)(x) & 0x00ff) << 8) |\
300 (((short)(x) & 0xff00) >> 8))
304 #endif /* ifdef CONV8 */
307 #endif /* ifdef CONV16 */
310 #endif /* ifdef CONV32 */
313 #endif /* ifdef CONV64 */
316 #define CONV16(x) {if (_eet_image_words_bigendian) {SWAP16(x); }}
317 #define CONV32(x) {if (_eet_image_words_bigendian) {SWAP32(x); }}
318 #define CONV64(x) {if (_eet_image_words_bigendian) {SWAP64(x); }}
323 _JPEGFatalErrorHandler(j_common_ptr cinfo)
327 errmgr = (emptr)cinfo->err;
328 /* cinfo->err->output_message(cinfo);*/
329 longjmp(errmgr->setjmp_buffer, 1);
331 } /* _JPEGFatalErrorHandler */
334 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
338 /* errmgr = (emptr) cinfo->err; */
339 /* cinfo->err->output_message(cinfo);*/
340 /* longjmp(errmgr->setjmp_buffer, 1);*/
342 } /* _JPEGErrorHandler */
345 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__,
346 int msg_level __UNUSED__)
350 /* errmgr = (emptr) cinfo->err; */
351 /* cinfo->err->output_message(cinfo);*/
352 /* longjmp(errmgr->setjmp_buffer, 1);*/
354 } /* _JPEGErrorHandler2 */
357 eet_data_image_jpeg_header_decode(const void *data,
362 struct jpeg_decompress_struct cinfo;
363 struct _JPEG_error_mgr jerr;
365 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
367 cinfo.err = jpeg_std_error(&(jerr.pub));
368 jerr.pub.error_exit = _JPEGFatalErrorHandler;
369 jerr.pub.emit_message = _JPEGErrorHandler2;
370 jerr.pub.output_message = _JPEGErrorHandler;
371 if (setjmp(jerr.setjmp_buffer))
374 jpeg_create_decompress(&cinfo);
376 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
378 jpeg_destroy_decompress(&cinfo);
382 jpeg_read_header(&cinfo, TRUE);
383 cinfo.do_fancy_upsampling = FALSE;
384 cinfo.do_block_smoothing = FALSE;
385 jpeg_start_decompress(&cinfo);
388 *w = cinfo.output_width;
389 *h = cinfo.output_height;
394 jpeg_destroy_decompress(&cinfo);
396 if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
400 } /* eet_data_image_jpeg_header_decode */
403 eet_data_image_jpeg_rgb_decode(const void *data,
410 unsigned int row_stride)
412 struct jpeg_decompress_struct cinfo;
413 struct _JPEG_error_mgr jerr;
414 unsigned char *ptr, *line[16], *tdata = NULL;
415 unsigned int *ptr2, *tmp;
417 unsigned int x, y, l, scans;
420 /* FIXME: handle src_x, src_y and row_stride correctly */
424 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
426 cinfo.err = jpeg_std_error(&(jerr.pub));
427 jerr.pub.error_exit = _JPEGFatalErrorHandler;
428 jerr.pub.emit_message = _JPEGErrorHandler2;
429 jerr.pub.output_message = _JPEGErrorHandler;
430 if (setjmp(jerr.setjmp_buffer))
433 jpeg_create_decompress(&cinfo);
435 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
437 jpeg_destroy_decompress(&cinfo);
441 jpeg_read_header(&cinfo, TRUE);
442 cinfo.dct_method = JDCT_FASTEST;
443 cinfo.do_fancy_upsampling = FALSE;
444 cinfo.do_block_smoothing = FALSE;
445 jpeg_start_decompress(&cinfo);
448 iw = cinfo.output_width;
449 ih = cinfo.output_height;
450 if ((iw != w) || (ih != h))
455 jpeg_destroy_decompress(&cinfo);
459 /* end head decoding */
461 if (cinfo.rec_outbuf_height > 16)
466 jpeg_destroy_decompress(&cinfo);
470 tdata = alloca((iw) * 16 * 3);
473 if (cinfo.output_components == 3)
475 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
476 line[i] = tdata + (i * (iw) * 3);
477 for (l = 0; l < ih; l += cinfo.rec_outbuf_height)
479 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
480 scans = cinfo.rec_outbuf_height;
481 if ((ih - l) < scans)
486 if (l + scans >= src_y && l < src_y + h)
492 for (ptr += 3 * iw * y; y < scans && (y + l) < (src_y + h);
497 for (x = 0; x < w; x++)
501 ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
506 ptr2 = tmp + row_stride / 4;
511 else if (cinfo.output_components == 1)
513 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
514 line[i] = tdata + (i * (iw));
515 for (l = 0; l < (ih); l += cinfo.rec_outbuf_height)
517 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
518 scans = cinfo.rec_outbuf_height;
519 if (((ih) - l) < scans)
524 if (l >= src_y && l < src_y + h)
530 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
534 for (x = 0; x < w; x++)
538 ((ptr[0]) << 16) | ((ptr[0]) << 8) | (ptr[0]);
543 ptr2 = tmp + row_stride / 4;
549 /* end data decoding */
550 jpeg_finish_decompress(&cinfo);
551 jpeg_destroy_decompress(&cinfo);
553 } /* eet_data_image_jpeg_rgb_decode */
556 eet_data_image_jpeg_alpha_decode(const void *data,
563 unsigned int row_stride)
565 struct jpeg_decompress_struct cinfo;
566 struct _JPEG_error_mgr jerr;
567 unsigned char *ptr, *line[16], *tdata = NULL;
568 unsigned int *ptr2, *tmp;
569 unsigned int x, y, l, scans;
572 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
574 cinfo.err = jpeg_std_error(&(jerr.pub));
575 jerr.pub.error_exit = _JPEGFatalErrorHandler;
576 jerr.pub.emit_message = _JPEGErrorHandler2;
577 jerr.pub.output_message = _JPEGErrorHandler;
578 if (setjmp(jerr.setjmp_buffer))
581 jpeg_create_decompress(&cinfo);
583 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
585 jpeg_destroy_decompress(&cinfo);
589 jpeg_read_header(&cinfo, TRUE);
590 cinfo.dct_method = JDCT_FASTEST;
591 cinfo.do_fancy_upsampling = FALSE;
592 cinfo.do_block_smoothing = FALSE;
593 jpeg_start_decompress(&cinfo);
596 iw = cinfo.output_width;
597 if (w != cinfo.output_width
598 || h != cinfo.output_height)
603 jpeg_destroy_decompress(&cinfo);
607 /* end head decoding */
609 if (cinfo.rec_outbuf_height > 16)
614 jpeg_destroy_decompress(&cinfo);
618 tdata = alloca(w * 16 * 3);
620 if (cinfo.output_components == 1)
622 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
623 line[i] = tdata + (i * w);
624 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
626 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
627 scans = cinfo.rec_outbuf_height;
633 if (l >= src_y && l < src_y + h)
639 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
643 for (x = 0; x < w; x++)
646 ((*ptr2) & 0x00ffffff) |
652 ptr2 = tmp + row_stride / 4;
658 /* end data decoding */
659 jpeg_finish_decompress(&cinfo);
660 jpeg_destroy_decompress(&cinfo);
662 } /* eet_data_image_jpeg_alpha_decode */
665 eet_data_image_lossless_convert(int *size,
671 if (_eet_image_words_bigendian == -1)
675 v = htonl(0x12345678);
677 _eet_image_words_bigendian = 1;
679 _eet_image_words_bigendian = 0;
686 d = malloc((w * h * 4) + (8 * 4));
693 header[0] = 0xac1dfeed;
698 memcpy(d + 32, data, w * h * 4);
700 if (_eet_image_words_bigendian)
704 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
707 *size = ((w * h * 4) + (8 * 4));
710 } /* eet_data_image_lossless_convert */
713 eet_data_image_lossless_compressed_convert(int *size,
720 if (_eet_image_words_bigendian == -1)
724 v = htonl(0x12345678);
726 _eet_image_words_bigendian = 1;
728 _eet_image_words_bigendian = 0;
738 d = malloc((w * h * 4) + (8 * 4));
742 buflen = (((w * h * 101) / 100) + 3) * 4;
743 comp = malloc(buflen);
753 header[0] = 0xac1dfeed;
757 header[4] = compression;
758 memcpy(d + 32, data, w * h * 4);
760 if (_eet_image_words_bigendian)
764 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
767 ret = compress2((Bytef *)comp, &buflen,
771 if (ret != Z_OK || buflen > (w * h * 4))
779 memcpy(d + 32, comp, buflen);
780 *size = (8 * 4) + buflen;
784 } /* eet_data_image_lossless_compressed_convert */
787 eet_data_image_jpeg_convert(int *size,
794 struct jpeg_compress_struct cinfo;
795 struct _JPEG_error_mgr jerr;
802 (void)alpha; /* unused */
806 memset(&cinfo, 0, sizeof (struct jpeg_compress_struct));
808 cinfo.err = jpeg_std_error(&(jerr.pub));
809 jerr.pub.error_exit = _JPEGFatalErrorHandler;
810 jerr.pub.emit_message = _JPEGErrorHandler2;
811 jerr.pub.output_message = _JPEGErrorHandler;
812 if (setjmp(jerr.setjmp_buffer))
815 jpeg_create_compress(&cinfo);
817 if (eet_jpeg_membuf_dst(&cinfo, &d, &sz))
819 jpeg_destroy_compress(&cinfo);
823 cinfo.image_width = w;
824 cinfo.image_height = h;
825 cinfo.input_components = 3;
826 cinfo.in_color_space = JCS_RGB;
827 jpeg_set_defaults(&cinfo);
828 jpeg_set_quality(&cinfo, quality, TRUE);
831 cinfo.comp_info[0].h_samp_factor = 1;
832 cinfo.comp_info[0].v_samp_factor = 1;
833 cinfo.comp_info[1].h_samp_factor = 1;
834 cinfo.comp_info[1].v_samp_factor = 1;
835 cinfo.comp_info[2].h_samp_factor = 1;
836 cinfo.comp_info[2].v_samp_factor = 1;
839 jpeg_start_compress(&cinfo, TRUE);
841 while (cinfo.next_scanline < cinfo.image_height)
845 /* convert scaline from ARGB to RGB packed */
846 ptr = ((const int *)data) + cinfo.next_scanline * w;
847 for (j = 0, i = 0; i < w; i++)
849 buf[j++] = ((*ptr) >> 16) & 0xff;
850 buf[j++] = ((*ptr) >> 8) & 0xff;
851 buf[j++] = ((*ptr)) & 0xff;
854 jbuf = (JSAMPROW *)(&buf);
855 jpeg_write_scanlines(&cinfo, jbuf, 1);
858 jpeg_finish_compress(&cinfo);
859 jpeg_destroy_compress(&cinfo);
863 } /* eet_data_image_jpeg_convert */
866 eet_data_image_jpeg_alpha_convert(int *size,
873 unsigned char *d1, *d2;
878 (void)alpha; /* unused */
880 if (_eet_image_words_bigendian == -1)
884 v = htonl(0x12345678);
886 _eet_image_words_bigendian = 1;
888 _eet_image_words_bigendian = 0;
895 struct _JPEG_error_mgr jerr;
897 struct jpeg_compress_struct cinfo;
902 cinfo.err = jpeg_std_error(&(jerr.pub));
903 jerr.pub.error_exit = _JPEGFatalErrorHandler;
904 jerr.pub.emit_message = _JPEGErrorHandler2;
905 jerr.pub.output_message = _JPEGErrorHandler;
906 if (setjmp(jerr.setjmp_buffer))
909 jpeg_create_compress(&cinfo);
910 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
912 jpeg_destroy_compress(&cinfo);
916 cinfo.image_width = w;
917 cinfo.image_height = h;
918 cinfo.input_components = 3;
919 cinfo.in_color_space = JCS_RGB;
920 jpeg_set_defaults(&cinfo);
921 jpeg_set_quality(&cinfo, quality, TRUE);
924 cinfo.comp_info[0].h_samp_factor = 1;
925 cinfo.comp_info[0].v_samp_factor = 1;
926 cinfo.comp_info[1].h_samp_factor = 1;
927 cinfo.comp_info[1].v_samp_factor = 1;
928 cinfo.comp_info[2].h_samp_factor = 1;
929 cinfo.comp_info[2].v_samp_factor = 1;
932 jpeg_start_compress(&cinfo, TRUE);
934 while (cinfo.next_scanline < cinfo.image_height)
938 ptr = ((const int *)data) + cinfo.next_scanline * w;
939 /* convert scaline from ARGB to RGB packed */
940 for (j = 0, i = 0; i < w; i++)
942 buf[j++] = ((*ptr) >> 16) & 0xff;
943 buf[j++] = ((*ptr) >> 8) & 0xff;
944 buf[j++] = ((*ptr)) & 0xff;
947 jbuf = (JSAMPROW *)(&buf);
948 jpeg_write_scanlines(&cinfo, jbuf, 1);
951 jpeg_finish_compress(&cinfo);
952 jpeg_destroy_compress(&cinfo);
961 struct _JPEG_error_mgr jerr;
963 struct jpeg_compress_struct cinfo;
968 cinfo.err = jpeg_std_error(&(jerr.pub));
969 jerr.pub.error_exit = _JPEGFatalErrorHandler;
970 jerr.pub.emit_message = _JPEGErrorHandler2;
971 jerr.pub.output_message = _JPEGErrorHandler;
972 if (setjmp(jerr.setjmp_buffer))
978 jpeg_create_compress(&cinfo);
979 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
981 jpeg_destroy_compress(&cinfo);
986 cinfo.image_width = w;
987 cinfo.image_height = h;
988 cinfo.input_components = 1;
989 cinfo.in_color_space = JCS_GRAYSCALE;
990 jpeg_set_defaults(&cinfo);
991 jpeg_set_quality(&cinfo, quality, TRUE);
994 cinfo.comp_info[0].h_samp_factor = 1;
995 cinfo.comp_info[0].v_samp_factor = 1;
996 cinfo.comp_info[1].h_samp_factor = 1;
997 cinfo.comp_info[1].v_samp_factor = 1;
998 cinfo.comp_info[2].h_samp_factor = 1;
999 cinfo.comp_info[2].v_samp_factor = 1;
1002 jpeg_start_compress(&cinfo, TRUE);
1004 while (cinfo.next_scanline < cinfo.image_height)
1008 ptr = ((const int *)data) + cinfo.next_scanline * w;
1009 /* convert scaline from ARGB to RGB packed */
1010 for (j = 0, i = 0; i < w; i++)
1012 buf[j++] = ((*ptr) >> 24) & 0xff;
1015 jbuf = (JSAMPROW *)(&buf);
1016 jpeg_write_scanlines(&cinfo, jbuf, 1);
1019 jpeg_finish_compress(&cinfo);
1020 jpeg_destroy_compress(&cinfo);
1025 d = malloc(12 + sz1 + sz2);
1034 header[0] = 0xbeeff00d;
1037 if (_eet_image_words_bigendian)
1041 for (i = 0; i < 3; i++) SWAP32(header[i]);
1044 memcpy(d + 12, d1, sz1);
1045 memcpy(d + 12 + sz1, d2, sz2);
1049 *size = 12 + sz1 + sz2;
1051 } /* eet_data_image_jpeg_alpha_convert */
1054 eet_data_image_write_cipher(Eet_File *ef,
1056 const char *cipher_key,
1068 d = eet_data_image_encode(data, &size, w, h, alpha, comp, quality, lossy);
1073 v = eet_write_cipher(ef, name, d, size, 0, cipher_key);
1079 } /* eet_data_image_write_cipher */
1082 eet_data_image_write(Eet_File *ef,
1092 return eet_data_image_write_cipher(ef,
1102 } /* eet_data_image_write */
1105 eet_data_image_read_cipher(Eet_File *ef,
1107 const char *cipher_key,
1115 unsigned int *d = NULL;
1121 data = (void *)eet_read_direct(ef, name, &size);
1125 data = eet_read_cipher(ef, name, &size, cipher_key);
1131 d = eet_data_image_decode(data, size, w, h, alpha, comp, quality, lossy);
1137 } /* eet_data_image_read_cipher */
1140 eet_data_image_read(Eet_File *ef,
1149 return eet_data_image_read_cipher(ef, name, NULL, w, h, alpha,
1150 comp, quality, lossy);
1151 } /* eet_data_image_read */
1154 eet_data_image_read_to_surface_cipher(Eet_File *ef,
1156 const char *cipher_key,
1162 unsigned int row_stride,
1174 data = (void *)eet_read_direct(ef, name, &size);
1178 data = eet_read_cipher(ef, name, &size, cipher_key);
1184 res = eet_data_image_decode_to_surface(data, size, src_x, src_y, d,
1185 w, h, row_stride, alpha,
1186 comp, quality, lossy);
1192 } /* eet_data_image_read_to_surface_cipher */
1195 eet_data_image_read_to_surface(Eet_File *ef,
1202 unsigned int row_stride,
1208 return eet_data_image_read_to_surface_cipher(ef, name, NULL,
1211 alpha, comp, quality,
1213 } /* eet_data_image_read_to_surface */
1216 eet_data_image_header_read_cipher(Eet_File *ef,
1218 const char *cipher_key,
1232 data = (void *)eet_read_direct(ef, name, &size);
1236 data = eet_read_cipher(ef, name, &size, cipher_key);
1242 d = eet_data_image_header_decode(data, size, w, h, alpha,
1243 comp, quality, lossy);
1248 } /* eet_data_image_header_read_cipher */
1251 eet_data_image_header_read(Eet_File *ef,
1260 return eet_data_image_header_read_cipher(ef, name, NULL,
1262 comp, quality, lossy);
1263 } /* eet_data_image_header_read */
1266 eet_data_image_encode_cipher(const void *data,
1267 const char *cipher_key,
1277 void *ciphered_d = NULL;
1278 unsigned int ciphered_sz = 0;
1284 d = eet_data_image_lossless_compressed_convert(&size, data,
1287 /* eet_data_image_lossless_compressed_convert will refuse to compress something
1288 if the result is bigger than the entry. */
1289 if (comp <= 0 || !d)
1290 d = eet_data_image_lossless_convert(&size, data, w, h, alpha);
1295 d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality);
1297 d = eet_data_image_jpeg_alpha_convert(&size, data,
1298 w, h, alpha, quality);
1303 if(!eet_cipher(d, size, cipher_key, strlen(cipher_key), &ciphered_d,
1321 } /* eet_data_image_encode_cipher */
1324 eet_data_image_encode(const void *data,
1333 return eet_data_image_encode_cipher(data, NULL, w, h, alpha,
1334 comp, quality, lossy, size_ret);
1335 } /* eet_data_image_encode */
1338 eet_data_image_header_decode_cipher(const void *data,
1339 const char *cipher_key,
1349 void *deciphered_d = NULL;
1350 unsigned int deciphered_sz = 0;
1354 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1355 &deciphered_d, &deciphered_sz))
1357 data = deciphered_d;
1358 size = deciphered_sz;
1365 if (_eet_image_words_bigendian == -1)
1367 unsigned long int v;
1369 v = htonl(0x12345678);
1370 if (v == 0x12345678)
1371 _eet_image_words_bigendian = 1;
1373 _eet_image_words_bigendian = 0;
1379 memcpy(header, data, 32);
1380 if (_eet_image_words_bigendian)
1384 for (i = 0; i < 8; i++) SWAP32(header[i]);
1387 if ((unsigned)header[0] == 0xac1dfeed)
1395 if ((iw < 1) || (ih < 1) || (iw > 8192) || (ih > 8192))
1398 if ((cp == 0) && (size < ((iw * ih * 4) + 32)))
1408 *alpha = al ? 1 : 0;
1421 else if ((unsigned)header[0] == 0xbeeff00d)
1423 unsigned int iw = 0, ih = 0;
1424 unsigned const char *dt;
1429 /* sz2 = header[2]; */
1432 ok = eet_data_image_jpeg_header_decode(dt, sz1, &iw, &ih);
1458 unsigned int iw = 0, ih = 0;
1461 ok = eet_data_image_jpeg_header_decode(data, size, &iw, &ih);
1487 } /* eet_data_image_header_decode_cipher */
1490 eet_data_image_header_decode(const void *data,
1499 return eet_data_image_header_decode_cipher(data,
1508 } /* eet_data_image_header_decode */
1511 _eet_data_image_copy_buffer(const unsigned int *src,
1518 unsigned int row_stride)
1520 src += src_x + src_y * src_w;
1522 if (row_stride == src_w * 4 && w == src_w)
1523 memcpy(dst, src, row_stride * h);
1526 unsigned int *over = dst;
1529 for (y = 0; y < h; ++y, src += src_w, over += row_stride)
1530 memcpy(over, src, w * 4);
1532 } /* _eet_data_image_copy_buffer */
1535 _eet_data_image_decode_inside(const void *data,
1544 unsigned int row_stride,
1550 if (lossy == 0 && quality == 100)
1554 body = ((unsigned int *)data) + 8;
1556 _eet_data_image_copy_buffer(body, src_x, src_y, src_w, d,
1560 if (src_h == h && src_w == w && row_stride == src_w * 4)
1565 uncompress((Bytef *)d, &dlen, (Bytef *)body,
1566 (uLongf)(size - 32));
1571 uLongf dlen = src_w * src_h * 4;
1573 /* FIXME: This could create a huge alloc. So compressed
1574 data and tile could not always work. */
1575 dtmp = malloc(dlen);
1579 uncompress(dtmp, &dlen, (Bytef *)body, (uLongf)(size - 32));
1581 _eet_data_image_copy_buffer((unsigned int *)dtmp,
1582 src_x, src_y, src_w, d,
1589 /* Fix swapiness. */
1590 if (_eet_image_words_bigendian)
1594 for (x = 0; x < (w * h); x++) SWAP32(d[x]);
1597 else if (comp == 0 && lossy == 1)
1601 unsigned const char *dt;
1605 memcpy(header, data, 32);
1606 if (_eet_image_words_bigendian)
1610 for (i = 0; i < 8; i++) SWAP32(header[i]);
1618 if (eet_data_image_jpeg_rgb_decode(dt, sz1, src_x, src_y, d, w, h,
1622 if (!eet_data_image_jpeg_alpha_decode(dt, sz2, src_x, src_y,
1623 d, w, h, row_stride))
1627 else if (!eet_data_image_jpeg_rgb_decode(data, size, src_x, src_y, d, w,
1635 } /* _eet_data_image_decode_inside */
1638 eet_data_image_decode_cipher(const void *data,
1639 const char *cipher_key,
1648 unsigned int *d = NULL;
1649 unsigned int iw, ih;
1650 int ialpha, icompress, iquality, ilossy;
1651 void *deciphered_d = NULL;
1652 unsigned int deciphered_sz = 0;
1656 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1657 &deciphered_d, &deciphered_sz))
1659 data = deciphered_d;
1660 size = deciphered_sz;
1667 /* All check are done during header decode, this simplify the code a lot. */
1668 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1669 &iquality, &ilossy))
1672 d = malloc(iw * ih * 4);
1676 if (!_eet_data_image_decode_inside(data, size, 0, 0, iw, ih, d, iw, ih, iw *
1677 4, ialpha, icompress, iquality, ilossy))
1696 *quality = iquality;
1702 } /* eet_data_image_decode_cipher */
1705 eet_data_image_decode(const void *data,
1714 return eet_data_image_decode_cipher(data, NULL, size, w, h,
1715 alpha, comp, quality, lossy);
1716 } /* eet_data_image_decode */
1719 eet_data_image_decode_to_surface_cipher(const void *data,
1720 const char *cipher_key,
1727 unsigned int row_stride,
1733 unsigned int iw, ih;
1734 int ialpha, icompress, iquality, ilossy;
1735 void *deciphered_d = NULL;
1736 unsigned int deciphered_sz = 0;
1740 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1741 &deciphered_d, &deciphered_sz))
1743 data = deciphered_d;
1744 size = deciphered_sz;
1751 /* All check are done during header decode, this simplify the code a lot. */
1752 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1753 &iquality, &ilossy))
1759 if (w * 4 > row_stride)
1762 if (w > iw || h > ih)
1765 if (!_eet_data_image_decode_inside(data, size, src_x, src_y, iw, ih, d, w, h,
1766 row_stride, ialpha, icompress, iquality,
1777 *quality = iquality;
1783 } /* eet_data_image_decode_to_surface_cipher */
1786 eet_data_image_decode_to_surface(const void *data,
1793 unsigned int row_stride,
1799 return eet_data_image_decode_to_surface_cipher(data, NULL, size,
1802 alpha, comp, quality,
1804 } /* eet_data_image_decode_to_surface */