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>
32 #endif /* ifdef _WIN32 */
41 #include "Eet_private.h"
45 typedef struct _JPEG_error_mgr * emptr;
49 struct _JPEG_error_mgr
51 struct jpeg_error_mgr pub;
52 jmp_buf setjmp_buffer;
55 struct jpeg_membuf_src
57 struct jpeg_source_mgr pub;
59 const unsigned char *buf;
61 struct jpeg_membuf_src *self;
65 _eet_jpeg_membuf_src_init(j_decompress_ptr cinfo)
67 /* FIXME: Use attribute unused */
69 } /* _eet_jpeg_membuf_src_init */
72 _eet_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
74 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
75 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
77 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
78 src->pub.next_input_byte = jpeg_eoi;
81 } /* _eet_jpeg_membuf_src_fill */
84 _eet_jpeg_membuf_src_skip(j_decompress_ptr cinfo,
87 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
89 src->pub.bytes_in_buffer -= num_bytes;
90 src->pub.next_input_byte += num_bytes;
91 } /* _eet_jpeg_membuf_src_skip */
94 _eet_jpeg_membuf_src_term(j_decompress_ptr cinfo)
96 struct jpeg_membuf_src *src = ((struct jpeg_membuf_src *)cinfo->src)->self;
100 } /* _eet_jpeg_membuf_src_term */
103 eet_jpeg_membuf_src(j_decompress_ptr cinfo,
107 struct jpeg_membuf_src *src;
109 src = calloc(1, sizeof(*src));
115 cinfo->src = &src->pub;
118 src->pub.init_source = _eet_jpeg_membuf_src_init;
119 src->pub.fill_input_buffer = _eet_jpeg_membuf_src_fill;
120 src->pub.skip_input_data = _eet_jpeg_membuf_src_skip;
121 src->pub.resync_to_restart = jpeg_resync_to_restart;
122 src->pub.term_source = _eet_jpeg_membuf_src_term;
123 src->pub.bytes_in_buffer = src->len;
124 src->pub.next_input_byte = src->buf;
127 } /* eet_jpeg_membuf_src */
129 struct jpeg_membuf_dst
131 struct jpeg_destination_mgr pub;
139 struct jpeg_membuf_dst *self;
143 _eet_jpeg_membuf_dst_init(j_compress_ptr cinfo)
145 /* FIXME: Use eina attribute */
147 } /* _eet_jpeg_membuf_dst_init */
150 _eet_jpeg_membuf_dst_flush(j_compress_ptr cinfo)
152 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
155 if (dst->len >= 0x40000000 ||
156 !(buf = realloc(dst->buf, dst->len * 2)))
159 dst->pub.next_output_byte = dst->buf;
160 dst->pub.free_in_buffer = dst->len;
164 dst->pub.next_output_byte =
165 buf + ((unsigned char *)dst->pub.next_output_byte - dst->buf);
167 dst->pub.free_in_buffer += dst->len;
171 } /* _eet_jpeg_membuf_dst_flush */
174 _eet_jpeg_membuf_dst_term(j_compress_ptr cinfo)
176 struct jpeg_membuf_dst *dst = ((struct jpeg_membuf_dst *)cinfo->dest)->self;
180 *dst->dst_buf = NULL;
186 *dst->dst_buf = dst->buf;
187 *dst->dst_len = (unsigned char *)dst->pub.next_output_byte - dst->buf;
192 } /* _eet_jpeg_membuf_dst_term */
195 eet_jpeg_membuf_dst(j_compress_ptr cinfo,
199 struct jpeg_membuf_dst *dst;
201 dst = calloc(1, sizeof(*dst));
205 dst->buf = malloc(32768);
215 cinfo->dest = &dst->pub;
216 dst->pub.init_destination = _eet_jpeg_membuf_dst_init;
217 dst->pub.empty_output_buffer = _eet_jpeg_membuf_dst_flush;
218 dst->pub.term_destination = _eet_jpeg_membuf_dst_term;
219 dst->pub.free_in_buffer = dst->len;
220 dst->pub.next_output_byte = dst->buf;
226 } /* eet_jpeg_membuf_dst */
230 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
231 static void _JPEGErrorHandler(j_common_ptr cinfo);
232 static void _JPEGErrorHandler2(j_common_ptr cinfo,
235 static int eet_data_image_jpeg_header_decode(const void *data,
239 static int eet_data_image_jpeg_rgb_decode(const void *data,
246 unsigned int row_stride);
247 static void * eet_data_image_jpeg_alpha_decode(const void *data,
254 unsigned int row_stride);
255 static void * eet_data_image_lossless_convert(int *size,
260 static void * eet_data_image_lossless_compressed_convert(int *size,
266 static void * eet_data_image_jpeg_convert(int *size,
272 static void * eet_data_image_jpeg_alpha_convert(int *size,
281 static int _eet_image_words_bigendian = -1;
285 #define SWAP64(x) (x) =\
286 ((((unsigned long long)(x) & 0x00000000000000ffULL) << 56) |\
287 (((unsigned long long)(x) & 0x000000000000ff00ULL) << 40) |\
288 (((unsigned long long)(x) & 0x0000000000ff0000ULL) << 24) |\
289 (((unsigned long long)(x) & 0x00000000ff000000ULL) << 8) |\
290 (((unsigned long long)(x) & 0x000000ff00000000ULL) >> 8) |\
291 (((unsigned long long)(x) & 0x0000ff0000000000ULL) >> 24) |\
292 (((unsigned long long)(x) & 0x00ff000000000000ULL) >> 40) |\
293 (((unsigned long long)(x) & 0xff00000000000000ULL) >> 56))
294 #define SWAP32(x) (x) =\
295 ((((int)(x) & 0x000000ff) << 24) |\
296 (((int)(x) & 0x0000ff00) << 8) |\
297 (((int)(x) & 0x00ff0000) >> 8) |\
298 (((int)(x) & 0xff000000) >> 24))
299 #define SWAP16(x) (x) =\
300 ((((short)(x) & 0x00ff) << 8) |\
301 (((short)(x) & 0xff00) >> 8))
305 #endif /* ifdef CONV8 */
308 #endif /* ifdef CONV16 */
311 #endif /* ifdef CONV32 */
314 #endif /* ifdef CONV64 */
317 #define CONV16(x) {if (_eet_image_words_bigendian) {SWAP16(x); }}
318 #define CONV32(x) {if (_eet_image_words_bigendian) {SWAP32(x); }}
319 #define CONV64(x) {if (_eet_image_words_bigendian) {SWAP64(x); }}
324 _JPEGFatalErrorHandler(j_common_ptr cinfo)
328 errmgr = (emptr)cinfo->err;
329 /* cinfo->err->output_message(cinfo);*/
330 longjmp(errmgr->setjmp_buffer, 1);
332 } /* _JPEGFatalErrorHandler */
335 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
339 /* errmgr = (emptr) cinfo->err; */
340 /* cinfo->err->output_message(cinfo);*/
341 /* longjmp(errmgr->setjmp_buffer, 1);*/
343 } /* _JPEGErrorHandler */
346 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__,
347 int msg_level __UNUSED__)
351 /* errmgr = (emptr) cinfo->err; */
352 /* cinfo->err->output_message(cinfo);*/
353 /* longjmp(errmgr->setjmp_buffer, 1);*/
355 } /* _JPEGErrorHandler2 */
358 eet_data_image_jpeg_header_decode(const void *data,
363 struct jpeg_decompress_struct cinfo;
364 struct _JPEG_error_mgr jerr;
366 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
368 cinfo.err = jpeg_std_error(&(jerr.pub));
369 jerr.pub.error_exit = _JPEGFatalErrorHandler;
370 jerr.pub.emit_message = _JPEGErrorHandler2;
371 jerr.pub.output_message = _JPEGErrorHandler;
372 if (setjmp(jerr.setjmp_buffer))
375 jpeg_create_decompress(&cinfo);
377 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
379 jpeg_destroy_decompress(&cinfo);
383 jpeg_read_header(&cinfo, TRUE);
384 cinfo.do_fancy_upsampling = FALSE;
385 cinfo.do_block_smoothing = FALSE;
386 jpeg_start_decompress(&cinfo);
389 *w = cinfo.output_width;
390 *h = cinfo.output_height;
395 jpeg_destroy_decompress(&cinfo);
397 if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
401 } /* eet_data_image_jpeg_header_decode */
404 eet_data_image_jpeg_rgb_decode(const void *data,
411 unsigned int row_stride)
413 struct jpeg_decompress_struct cinfo;
414 struct _JPEG_error_mgr jerr;
415 unsigned char *ptr, *line[16], *tdata = NULL;
416 unsigned int *ptr2, *tmp;
418 unsigned int x, y, l, scans;
421 /* FIXME: handle src_x, src_y and row_stride correctly */
425 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
427 cinfo.err = jpeg_std_error(&(jerr.pub));
428 jerr.pub.error_exit = _JPEGFatalErrorHandler;
429 jerr.pub.emit_message = _JPEGErrorHandler2;
430 jerr.pub.output_message = _JPEGErrorHandler;
431 if (setjmp(jerr.setjmp_buffer))
434 jpeg_create_decompress(&cinfo);
436 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
438 jpeg_destroy_decompress(&cinfo);
442 jpeg_read_header(&cinfo, TRUE);
443 cinfo.dct_method = JDCT_FASTEST;
444 cinfo.do_fancy_upsampling = FALSE;
445 cinfo.do_block_smoothing = FALSE;
446 jpeg_start_decompress(&cinfo);
449 iw = cinfo.output_width;
450 ih = cinfo.output_height;
451 if ((iw != w) || (ih != h))
456 jpeg_destroy_decompress(&cinfo);
460 /* end head decoding */
462 if (cinfo.rec_outbuf_height > 16)
467 jpeg_destroy_decompress(&cinfo);
471 tdata = alloca((iw) * 16 * 3);
474 if (cinfo.output_components == 3)
476 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
477 line[i] = tdata + (i * (iw) * 3);
478 for (l = 0; l < ih; l += cinfo.rec_outbuf_height)
480 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
481 scans = cinfo.rec_outbuf_height;
482 if ((ih - l) < scans)
487 if (l + scans >= src_y && l < src_y + h)
493 for (ptr += 3 * iw * y; y < scans && (y + l) < (src_y + h);
498 for (x = 0; x < w; x++)
502 ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
507 ptr2 = tmp + row_stride / 4;
512 else if (cinfo.output_components == 1)
514 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
515 line[i] = tdata + (i * (iw));
516 for (l = 0; l < (ih); l += cinfo.rec_outbuf_height)
518 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
519 scans = cinfo.rec_outbuf_height;
520 if (((ih) - l) < scans)
525 if (l >= src_y && l < src_y + h)
531 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
535 for (x = 0; x < w; x++)
539 ((ptr[0]) << 16) | ((ptr[0]) << 8) | (ptr[0]);
544 ptr2 = tmp + row_stride / 4;
550 /* end data decoding */
551 jpeg_finish_decompress(&cinfo);
552 jpeg_destroy_decompress(&cinfo);
554 } /* eet_data_image_jpeg_rgb_decode */
557 eet_data_image_jpeg_alpha_decode(const void *data,
564 unsigned int row_stride)
566 struct jpeg_decompress_struct cinfo;
567 struct _JPEG_error_mgr jerr;
568 unsigned char *ptr, *line[16], *tdata = NULL;
569 unsigned int *ptr2, *tmp;
570 unsigned int x, y, l, scans;
573 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
575 cinfo.err = jpeg_std_error(&(jerr.pub));
576 jerr.pub.error_exit = _JPEGFatalErrorHandler;
577 jerr.pub.emit_message = _JPEGErrorHandler2;
578 jerr.pub.output_message = _JPEGErrorHandler;
579 if (setjmp(jerr.setjmp_buffer))
582 jpeg_create_decompress(&cinfo);
584 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
586 jpeg_destroy_decompress(&cinfo);
590 jpeg_read_header(&cinfo, TRUE);
591 cinfo.dct_method = JDCT_FASTEST;
592 cinfo.do_fancy_upsampling = FALSE;
593 cinfo.do_block_smoothing = FALSE;
594 jpeg_start_decompress(&cinfo);
597 iw = cinfo.output_width;
598 if (w != cinfo.output_width
599 || h != cinfo.output_height)
604 jpeg_destroy_decompress(&cinfo);
608 /* end head decoding */
610 if (cinfo.rec_outbuf_height > 16)
615 jpeg_destroy_decompress(&cinfo);
619 tdata = alloca(w * 16 * 3);
621 if (cinfo.output_components == 1)
623 for (i = 0; i < (unsigned int)cinfo.rec_outbuf_height; i++)
624 line[i] = tdata + (i * w);
625 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
627 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
628 scans = cinfo.rec_outbuf_height;
634 if (l >= src_y && l < src_y + h)
640 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
644 for (x = 0; x < w; x++)
647 ((*ptr2) & 0x00ffffff) |
653 ptr2 = tmp + row_stride / 4;
659 /* end data decoding */
660 jpeg_finish_decompress(&cinfo);
661 jpeg_destroy_decompress(&cinfo);
663 } /* eet_data_image_jpeg_alpha_decode */
666 eet_data_image_lossless_convert(int *size,
672 if (_eet_image_words_bigendian == -1)
676 v = htonl(0x12345678);
678 _eet_image_words_bigendian = 1;
680 _eet_image_words_bigendian = 0;
687 d = malloc((w * h * 4) + (8 * 4));
694 header[0] = 0xac1dfeed;
699 memcpy(d + 32, data, w * h * 4);
701 if (_eet_image_words_bigendian)
705 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
708 *size = ((w * h * 4) + (8 * 4));
711 } /* eet_data_image_lossless_convert */
714 eet_data_image_lossless_compressed_convert(int *size,
721 if (_eet_image_words_bigendian == -1)
725 v = htonl(0x12345678);
727 _eet_image_words_bigendian = 1;
729 _eet_image_words_bigendian = 0;
739 d = malloc((w * h * 4) + (8 * 4));
743 buflen = (((w * h * 101) / 100) + 3) * 4;
744 comp = malloc(buflen);
754 header[0] = 0xac1dfeed;
758 header[4] = compression;
759 memcpy(d + 32, data, w * h * 4);
761 if (_eet_image_words_bigendian)
765 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
768 ret = compress2((Bytef *)comp, &buflen,
772 if (ret != Z_OK || buflen > (w * h * 4))
780 memcpy(d + 32, comp, buflen);
781 *size = (8 * 4) + buflen;
785 } /* eet_data_image_lossless_compressed_convert */
788 eet_data_image_jpeg_convert(int *size,
795 struct jpeg_compress_struct cinfo;
796 struct _JPEG_error_mgr jerr;
803 (void)alpha; /* unused */
807 memset(&cinfo, 0, sizeof (struct jpeg_compress_struct));
809 cinfo.err = jpeg_std_error(&(jerr.pub));
810 jerr.pub.error_exit = _JPEGFatalErrorHandler;
811 jerr.pub.emit_message = _JPEGErrorHandler2;
812 jerr.pub.output_message = _JPEGErrorHandler;
813 if (setjmp(jerr.setjmp_buffer))
816 jpeg_create_compress(&cinfo);
818 if (eet_jpeg_membuf_dst(&cinfo, &d, &sz))
820 jpeg_destroy_compress(&cinfo);
824 cinfo.image_width = w;
825 cinfo.image_height = h;
826 cinfo.input_components = 3;
827 cinfo.in_color_space = JCS_RGB;
828 jpeg_set_defaults(&cinfo);
829 jpeg_set_quality(&cinfo, quality, TRUE);
832 cinfo.comp_info[0].h_samp_factor = 1;
833 cinfo.comp_info[0].v_samp_factor = 1;
834 cinfo.comp_info[1].h_samp_factor = 1;
835 cinfo.comp_info[1].v_samp_factor = 1;
836 cinfo.comp_info[2].h_samp_factor = 1;
837 cinfo.comp_info[2].v_samp_factor = 1;
840 jpeg_start_compress(&cinfo, TRUE);
842 while (cinfo.next_scanline < cinfo.image_height)
846 /* convert scaline from ARGB to RGB packed */
847 ptr = ((const int *)data) + cinfo.next_scanline * w;
848 for (j = 0, i = 0; i < w; i++)
850 buf[j++] = ((*ptr) >> 16) & 0xff;
851 buf[j++] = ((*ptr) >> 8) & 0xff;
852 buf[j++] = ((*ptr)) & 0xff;
855 jbuf = (JSAMPROW *)(&buf);
856 jpeg_write_scanlines(&cinfo, jbuf, 1);
859 jpeg_finish_compress(&cinfo);
860 jpeg_destroy_compress(&cinfo);
864 } /* eet_data_image_jpeg_convert */
867 eet_data_image_jpeg_alpha_convert(int *size,
874 unsigned char *d1, *d2;
879 (void)alpha; /* unused */
881 if (_eet_image_words_bigendian == -1)
885 v = htonl(0x12345678);
887 _eet_image_words_bigendian = 1;
889 _eet_image_words_bigendian = 0;
896 struct _JPEG_error_mgr jerr;
898 struct jpeg_compress_struct cinfo;
903 cinfo.err = jpeg_std_error(&(jerr.pub));
904 jerr.pub.error_exit = _JPEGFatalErrorHandler;
905 jerr.pub.emit_message = _JPEGErrorHandler2;
906 jerr.pub.output_message = _JPEGErrorHandler;
907 if (setjmp(jerr.setjmp_buffer))
910 jpeg_create_compress(&cinfo);
911 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
913 jpeg_destroy_compress(&cinfo);
917 cinfo.image_width = w;
918 cinfo.image_height = h;
919 cinfo.input_components = 3;
920 cinfo.in_color_space = JCS_RGB;
921 jpeg_set_defaults(&cinfo);
922 jpeg_set_quality(&cinfo, quality, TRUE);
925 cinfo.comp_info[0].h_samp_factor = 1;
926 cinfo.comp_info[0].v_samp_factor = 1;
927 cinfo.comp_info[1].h_samp_factor = 1;
928 cinfo.comp_info[1].v_samp_factor = 1;
929 cinfo.comp_info[2].h_samp_factor = 1;
930 cinfo.comp_info[2].v_samp_factor = 1;
933 jpeg_start_compress(&cinfo, TRUE);
935 while (cinfo.next_scanline < cinfo.image_height)
939 ptr = ((const int *)data) + cinfo.next_scanline * w;
940 /* convert scaline from ARGB to RGB packed */
941 for (j = 0, i = 0; i < w; i++)
943 buf[j++] = ((*ptr) >> 16) & 0xff;
944 buf[j++] = ((*ptr) >> 8) & 0xff;
945 buf[j++] = ((*ptr)) & 0xff;
948 jbuf = (JSAMPROW *)(&buf);
949 jpeg_write_scanlines(&cinfo, jbuf, 1);
952 jpeg_finish_compress(&cinfo);
953 jpeg_destroy_compress(&cinfo);
962 struct _JPEG_error_mgr jerr;
964 struct jpeg_compress_struct cinfo;
969 cinfo.err = jpeg_std_error(&(jerr.pub));
970 jerr.pub.error_exit = _JPEGFatalErrorHandler;
971 jerr.pub.emit_message = _JPEGErrorHandler2;
972 jerr.pub.output_message = _JPEGErrorHandler;
973 if (setjmp(jerr.setjmp_buffer))
979 jpeg_create_compress(&cinfo);
980 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
982 jpeg_destroy_compress(&cinfo);
987 cinfo.image_width = w;
988 cinfo.image_height = h;
989 cinfo.input_components = 1;
990 cinfo.in_color_space = JCS_GRAYSCALE;
991 jpeg_set_defaults(&cinfo);
992 jpeg_set_quality(&cinfo, quality, TRUE);
995 cinfo.comp_info[0].h_samp_factor = 1;
996 cinfo.comp_info[0].v_samp_factor = 1;
997 cinfo.comp_info[1].h_samp_factor = 1;
998 cinfo.comp_info[1].v_samp_factor = 1;
999 cinfo.comp_info[2].h_samp_factor = 1;
1000 cinfo.comp_info[2].v_samp_factor = 1;
1003 jpeg_start_compress(&cinfo, TRUE);
1005 while (cinfo.next_scanline < cinfo.image_height)
1009 ptr = ((const int *)data) + cinfo.next_scanline * w;
1010 /* convert scaline from ARGB to RGB packed */
1011 for (j = 0, i = 0; i < w; i++)
1013 buf[j++] = ((*ptr) >> 24) & 0xff;
1016 jbuf = (JSAMPROW *)(&buf);
1017 jpeg_write_scanlines(&cinfo, jbuf, 1);
1020 jpeg_finish_compress(&cinfo);
1021 jpeg_destroy_compress(&cinfo);
1026 d = malloc(12 + sz1 + sz2);
1035 header[0] = 0xbeeff00d;
1038 if (_eet_image_words_bigendian)
1042 for (i = 0; i < 3; i++) SWAP32(header[i]);
1045 memcpy(d + 12, d1, sz1);
1046 memcpy(d + 12 + sz1, d2, sz2);
1050 *size = 12 + sz1 + sz2;
1052 } /* eet_data_image_jpeg_alpha_convert */
1055 eet_data_image_write_cipher(Eet_File *ef,
1057 const char *cipher_key,
1069 d = eet_data_image_encode(data, &size, w, h, alpha, comp, quality, lossy);
1074 v = eet_write_cipher(ef, name, d, size, 0, cipher_key);
1080 } /* eet_data_image_write_cipher */
1083 eet_data_image_write(Eet_File *ef,
1093 return eet_data_image_write_cipher(ef,
1103 } /* eet_data_image_write */
1106 eet_data_image_read_cipher(Eet_File *ef,
1108 const char *cipher_key,
1116 unsigned int *d = NULL;
1122 data = (void *)eet_read_direct(ef, name, &size);
1126 data = eet_read_cipher(ef, name, &size, cipher_key);
1132 d = eet_data_image_decode(data, size, w, h, alpha, comp, quality, lossy);
1138 } /* eet_data_image_read_cipher */
1141 eet_data_image_read(Eet_File *ef,
1150 return eet_data_image_read_cipher(ef, name, NULL, w, h, alpha,
1151 comp, quality, lossy);
1152 } /* eet_data_image_read */
1155 eet_data_image_read_to_surface_cipher(Eet_File *ef,
1157 const char *cipher_key,
1163 unsigned int row_stride,
1175 data = (void *)eet_read_direct(ef, name, &size);
1179 data = eet_read_cipher(ef, name, &size, cipher_key);
1185 res = eet_data_image_decode_to_surface(data, size, src_x, src_y, d,
1186 w, h, row_stride, alpha,
1187 comp, quality, lossy);
1193 } /* eet_data_image_read_to_surface_cipher */
1196 eet_data_image_read_to_surface(Eet_File *ef,
1203 unsigned int row_stride,
1209 return eet_data_image_read_to_surface_cipher(ef, name, NULL,
1212 alpha, comp, quality,
1214 } /* eet_data_image_read_to_surface */
1217 eet_data_image_header_read_cipher(Eet_File *ef,
1219 const char *cipher_key,
1233 data = (void *)eet_read_direct(ef, name, &size);
1237 data = eet_read_cipher(ef, name, &size, cipher_key);
1243 d = eet_data_image_header_decode(data, size, w, h, alpha,
1244 comp, quality, lossy);
1249 } /* eet_data_image_header_read_cipher */
1252 eet_data_image_header_read(Eet_File *ef,
1261 return eet_data_image_header_read_cipher(ef, name, NULL,
1263 comp, quality, lossy);
1264 } /* eet_data_image_header_read */
1267 eet_data_image_encode_cipher(const void *data,
1268 const char *cipher_key,
1278 void *ciphered_d = NULL;
1279 unsigned int ciphered_sz = 0;
1285 d = eet_data_image_lossless_compressed_convert(&size, data,
1288 /* eet_data_image_lossless_compressed_convert will refuse to compress something
1289 if the result is bigger than the entry. */
1290 if (comp <= 0 || !d)
1291 d = eet_data_image_lossless_convert(&size, data, w, h, alpha);
1296 d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality);
1298 d = eet_data_image_jpeg_alpha_convert(&size, data,
1299 w, h, alpha, quality);
1304 if(!eet_cipher(d, size, cipher_key, strlen(cipher_key), &ciphered_d,
1322 } /* eet_data_image_encode_cipher */
1325 eet_data_image_encode(const void *data,
1334 return eet_data_image_encode_cipher(data, NULL, w, h, alpha,
1335 comp, quality, lossy, size_ret);
1336 } /* eet_data_image_encode */
1339 eet_data_image_header_decode_cipher(const void *data,
1340 const char *cipher_key,
1350 void *deciphered_d = NULL;
1351 unsigned int deciphered_sz = 0;
1355 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1356 &deciphered_d, &deciphered_sz))
1358 data = deciphered_d;
1359 size = deciphered_sz;
1366 if (_eet_image_words_bigendian == -1)
1368 unsigned long int v;
1370 v = htonl(0x12345678);
1371 if (v == 0x12345678)
1372 _eet_image_words_bigendian = 1;
1374 _eet_image_words_bigendian = 0;
1380 memcpy(header, data, 32);
1381 if (_eet_image_words_bigendian)
1385 for (i = 0; i < 8; i++) SWAP32(header[i]);
1388 if ((unsigned)header[0] == 0xac1dfeed)
1396 if ((iw < 1) || (ih < 1) || (iw > 8192) || (ih > 8192))
1399 if ((cp == 0) && (size < ((iw * ih * 4) + 32)))
1409 *alpha = al ? 1 : 0;
1422 else if ((unsigned)header[0] == 0xbeeff00d)
1424 unsigned int iw = 0, ih = 0;
1425 unsigned const char *dt;
1430 /* sz2 = header[2]; */
1433 ok = eet_data_image_jpeg_header_decode(dt, sz1, &iw, &ih);
1459 unsigned int iw = 0, ih = 0;
1462 ok = eet_data_image_jpeg_header_decode(data, size, &iw, &ih);
1488 } /* eet_data_image_header_decode_cipher */
1491 eet_data_image_header_decode(const void *data,
1500 return eet_data_image_header_decode_cipher(data,
1509 } /* eet_data_image_header_decode */
1512 _eet_data_image_copy_buffer(const unsigned int *src,
1519 unsigned int row_stride)
1521 src += src_x + src_y * src_w;
1523 if (row_stride == src_w * 4 && w == src_w)
1524 memcpy(dst, src, row_stride * h);
1527 unsigned int *over = dst;
1530 for (y = 0; y < h; ++y, src += src_w, over += row_stride)
1531 memcpy(over, src, w * 4);
1533 } /* _eet_data_image_copy_buffer */
1536 _eet_data_image_decode_inside(const void *data,
1545 unsigned int row_stride,
1551 if (lossy == 0 && quality == 100)
1555 body = ((unsigned int *)data) + 8;
1557 _eet_data_image_copy_buffer(body, src_x, src_y, src_w, d,
1561 if (src_h == h && src_w == w && row_stride == src_w * 4)
1566 uncompress((Bytef *)d, &dlen, (Bytef *)body,
1567 (uLongf)(size - 32));
1572 uLongf dlen = src_w * src_h * 4;
1574 /* FIXME: This could create a huge alloc. So compressed
1575 data and tile could not always work. */
1576 dtmp = malloc(dlen);
1580 uncompress(dtmp, &dlen, (Bytef *)body, (uLongf)(size - 32));
1582 _eet_data_image_copy_buffer((unsigned int *)dtmp,
1583 src_x, src_y, src_w, d,
1590 /* Fix swapiness. */
1591 if (_eet_image_words_bigendian)
1595 for (x = 0; x < (w * h); x++) SWAP32(d[x]);
1598 else if (comp == 0 && lossy == 1)
1602 unsigned const char *dt;
1606 memcpy(header, data, 32);
1607 if (_eet_image_words_bigendian)
1611 for (i = 0; i < 8; i++) SWAP32(header[i]);
1619 if (eet_data_image_jpeg_rgb_decode(dt, sz1, src_x, src_y, d, w, h,
1623 if (!eet_data_image_jpeg_alpha_decode(dt, sz2, src_x, src_y,
1624 d, w, h, row_stride))
1628 else if (!eet_data_image_jpeg_rgb_decode(data, size, src_x, src_y, d, w,
1636 } /* _eet_data_image_decode_inside */
1639 eet_data_image_decode_cipher(const void *data,
1640 const char *cipher_key,
1649 unsigned int *d = NULL;
1650 unsigned int iw, ih;
1651 int ialpha, icompress, iquality, ilossy;
1652 void *deciphered_d = NULL;
1653 unsigned int deciphered_sz = 0;
1657 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1658 &deciphered_d, &deciphered_sz))
1660 data = deciphered_d;
1661 size = deciphered_sz;
1668 /* All check are done during header decode, this simplify the code a lot. */
1669 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1670 &iquality, &ilossy))
1673 d = malloc(iw * ih * 4);
1677 if (!_eet_data_image_decode_inside(data, size, 0, 0, iw, ih, d, iw, ih, iw *
1678 4, ialpha, icompress, iquality, ilossy))
1697 *quality = iquality;
1703 } /* eet_data_image_decode_cipher */
1706 eet_data_image_decode(const void *data,
1715 return eet_data_image_decode_cipher(data, NULL, size, w, h,
1716 alpha, comp, quality, lossy);
1717 } /* eet_data_image_decode */
1720 eet_data_image_decode_to_surface_cipher(const void *data,
1721 const char *cipher_key,
1728 unsigned int row_stride,
1734 unsigned int iw, ih;
1735 int ialpha, icompress, iquality, ilossy;
1736 void *deciphered_d = NULL;
1737 unsigned int deciphered_sz = 0;
1741 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key),
1742 &deciphered_d, &deciphered_sz))
1744 data = deciphered_d;
1745 size = deciphered_sz;
1752 /* All check are done during header decode, this simplify the code a lot. */
1753 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress,
1754 &iquality, &ilossy))
1760 if (w * 4 > row_stride)
1763 if (w > iw || h > ih)
1766 if (!_eet_data_image_decode_inside(data, size, src_x, src_y, iw, ih, d, w, h,
1767 row_stride, ialpha, icompress, iquality,
1778 *quality = iquality;
1784 } /* eet_data_image_decode_to_surface_cipher */
1787 eet_data_image_decode_to_surface(const void *data,
1794 unsigned int row_stride,
1800 return eet_data_image_decode_to_surface_cipher(data, NULL, size,
1803 alpha, comp, quality,
1805 } /* eet_data_image_decode_to_surface */