2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
11 #elif defined __GNUC__
12 # define alloca __builtin_alloca
14 # define alloca __alloca
15 #elif defined _MSC_VER
17 # define alloca _alloca
23 void *alloca (size_t);
26 #ifdef HAVE_NETINET_IN_H
28 # include <sys/types.h>
30 # include <netinet/in.h>
34 # include <winsock2.h>
44 #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 {
59 struct jpeg_source_mgr pub;
61 const unsigned char *buf;
66 _eet_jpeg_membuf_src_init(j_decompress_ptr cinfo)
68 /* FIXME: Use attribute unused */
73 _eet_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
75 static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
76 struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
78 src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
79 src->pub.next_input_byte = jpeg_eoi;
85 _eet_jpeg_membuf_src_skip(j_decompress_ptr cinfo, long num_bytes)
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;
94 _eet_jpeg_membuf_src_term(j_decompress_ptr cinfo)
101 eet_jpeg_membuf_src(j_decompress_ptr cinfo, const void *buf, size_t len)
103 struct jpeg_membuf_src *src;
105 src = malloc(sizeof(*src));
108 cinfo->src = &src->pub;
111 src->pub.init_source = _eet_jpeg_membuf_src_init;
112 src->pub.fill_input_buffer = _eet_jpeg_membuf_src_fill;
113 src->pub.skip_input_data = _eet_jpeg_membuf_src_skip;
114 src->pub.resync_to_restart = jpeg_resync_to_restart;
115 src->pub.term_source = _eet_jpeg_membuf_src_term;
116 src->pub.bytes_in_buffer = src->len;
117 src->pub.next_input_byte = src->buf;
122 struct jpeg_membuf_dst {
123 struct jpeg_destination_mgr pub;
134 _eet_jpeg_membuf_dst_init(j_compress_ptr cinfo)
136 /* FIXME: Use eina attribute */
141 _eet_jpeg_membuf_dst_flush(j_compress_ptr cinfo)
143 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
146 if (dst->len >= 0x40000000 ||
147 (buf = realloc(dst->buf, dst->len * 2)) == NULL) {
149 dst->pub.next_output_byte = dst->buf;
150 dst->pub.free_in_buffer = dst->len;
154 dst->pub.next_output_byte =
155 buf + ((unsigned char *)dst->pub.next_output_byte - dst->buf);
157 dst->pub.free_in_buffer += dst->len;
164 _eet_jpeg_membuf_dst_term(j_compress_ptr cinfo)
166 struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
169 *dst->dst_buf = NULL;
173 *dst->dst_buf = dst->buf;
174 *dst->dst_len = (unsigned char *)dst->pub.next_output_byte - dst->buf;
181 eet_jpeg_membuf_dst(j_compress_ptr cinfo, void **buf, size_t *len)
183 struct jpeg_membuf_dst *dst;
185 dst = malloc(sizeof(*dst));
188 dst->buf = malloc(32768);
195 cinfo->dest = &dst->pub;
196 dst->pub.init_destination = _eet_jpeg_membuf_dst_init;
197 dst->pub.empty_output_buffer = _eet_jpeg_membuf_dst_flush;
198 dst->pub.term_destination = _eet_jpeg_membuf_dst_term;
199 dst->pub.free_in_buffer = dst->len;
200 dst->pub.next_output_byte = dst->buf;
210 static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
211 static void _JPEGErrorHandler(j_common_ptr cinfo);
212 static void _JPEGErrorHandler2(j_common_ptr cinfo, int msg_level);
214 static int eet_data_image_jpeg_header_decode(const void *data, int size, unsigned int *w, unsigned int *h);
215 static int eet_data_image_jpeg_rgb_decode(const void *data, int size, unsigned int src_x, unsigned int src_y, unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride);
216 static void *eet_data_image_jpeg_alpha_decode(const void *data, int size, unsigned int src_x, unsigned int src_y, unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride);
217 static void *eet_data_image_lossless_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha);
218 static void *eet_data_image_lossless_compressed_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha, int compression);
219 static void *eet_data_image_jpeg_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha, int quality);
220 static void *eet_data_image_jpeg_alpha_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha, int quality);
224 static int _eet_image_words_bigendian = -1;
228 #define SWAP64(x) (x) = \
229 ((((unsigned long long)(x) & 0x00000000000000ffULL ) << 56) |\
230 (((unsigned long long)(x) & 0x000000000000ff00ULL ) << 40) |\
231 (((unsigned long long)(x) & 0x0000000000ff0000ULL ) << 24) |\
232 (((unsigned long long)(x) & 0x00000000ff000000ULL ) << 8) |\
233 (((unsigned long long)(x) & 0x000000ff00000000ULL ) >> 8) |\
234 (((unsigned long long)(x) & 0x0000ff0000000000ULL ) >> 24) |\
235 (((unsigned long long)(x) & 0x00ff000000000000ULL ) >> 40) |\
236 (((unsigned long long)(x) & 0xff00000000000000ULL ) >> 56))
237 #define SWAP32(x) (x) = \
238 ((((int)(x) & 0x000000ff ) << 24) |\
239 (((int)(x) & 0x0000ff00 ) << 8) |\
240 (((int)(x) & 0x00ff0000 ) >> 8) |\
241 (((int)(x) & 0xff000000 ) >> 24))
242 #define SWAP16(x) (x) = \
243 ((((short)(x) & 0x00ff ) << 8) |\
244 (((short)(x) & 0xff00 ) >> 8))
260 #define CONV16(x) {if (_eet_image_words_bigendian) SWAP16(x);}
261 #define CONV32(x) {if (_eet_image_words_bigendian) SWAP32(x);}
262 #define CONV64(x) {if (_eet_image_words_bigendian) SWAP64(x);}
267 _JPEGFatalErrorHandler(j_common_ptr cinfo)
271 errmgr = (emptr) cinfo->err;
272 /* cinfo->err->output_message(cinfo);*/
273 longjmp(errmgr->setjmp_buffer, 1);
278 _JPEGErrorHandler(j_common_ptr cinfo __UNUSED__)
282 /* errmgr = (emptr) cinfo->err; */
283 /* cinfo->err->output_message(cinfo);*/
284 /* longjmp(errmgr->setjmp_buffer, 1);*/
289 _JPEGErrorHandler2(j_common_ptr cinfo __UNUSED__, int msg_level __UNUSED__)
293 /* errmgr = (emptr) cinfo->err; */
294 /* cinfo->err->output_message(cinfo);*/
295 /* longjmp(errmgr->setjmp_buffer, 1);*/
300 eet_data_image_jpeg_header_decode(const void *data, int size, unsigned int *w, unsigned int *h)
302 struct jpeg_decompress_struct cinfo;
303 struct _JPEG_error_mgr jerr;
305 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
307 cinfo.err = jpeg_std_error(&(jerr.pub));
308 jerr.pub.error_exit = _JPEGFatalErrorHandler;
309 jerr.pub.emit_message = _JPEGErrorHandler2;
310 jerr.pub.output_message = _JPEGErrorHandler;
311 if (setjmp(jerr.setjmp_buffer)) return 0;
312 jpeg_create_decompress(&cinfo);
314 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
316 jpeg_destroy_decompress(&cinfo);
320 jpeg_read_header(&cinfo, TRUE);
321 cinfo.do_fancy_upsampling = FALSE;
322 cinfo.do_block_smoothing = FALSE;
323 jpeg_start_decompress(&cinfo);
326 *w = cinfo.output_width;
327 *h = cinfo.output_height;
332 jpeg_destroy_decompress(&cinfo);
334 if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
340 eet_data_image_jpeg_rgb_decode(const void *data, int size, unsigned int src_x, unsigned int src_y,
341 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride)
343 struct jpeg_decompress_struct cinfo;
344 struct _JPEG_error_mgr jerr;
345 unsigned char *ptr, *line[16], *tdata = NULL;
346 unsigned int *ptr2, *tmp;
348 unsigned int x, y, l, scans;
351 /* FIXME: handle src_x, src_y and row_stride correctly */
354 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
356 cinfo.err = jpeg_std_error(&(jerr.pub));
357 jerr.pub.error_exit = _JPEGFatalErrorHandler;
358 jerr.pub.emit_message = _JPEGErrorHandler2;
359 jerr.pub.output_message = _JPEGErrorHandler;
360 if (setjmp(jerr.setjmp_buffer)) return 0;
361 jpeg_create_decompress(&cinfo);
363 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
365 jpeg_destroy_decompress(&cinfo);
369 jpeg_read_header(&cinfo, TRUE);
370 cinfo.dct_method = JDCT_FASTEST;
371 cinfo.do_fancy_upsampling = FALSE;
372 cinfo.do_block_smoothing = FALSE;
373 jpeg_start_decompress(&cinfo);
376 iw = cinfo.output_width;
377 ih = cinfo.output_height;
378 if ((iw != w) || (ih != h))
383 jpeg_destroy_decompress(&cinfo);
386 /* end head decoding */
388 if (cinfo.rec_outbuf_height > 16)
393 jpeg_destroy_decompress(&cinfo);
396 tdata = alloca((iw) * 16 * 3);
399 if (cinfo.output_components == 3)
401 for (i = 0; i < (unsigned int) cinfo.rec_outbuf_height; i++)
402 line[i] = tdata + (i * (iw) * 3);
403 for (l = 0; l < ih; l += cinfo.rec_outbuf_height)
405 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
406 scans = cinfo.rec_outbuf_height;
407 if ((ih - l) < scans) scans = ih - l;
410 if (l + scans >= src_y && l < src_y + h)
413 if (src_y < l) y = 0;
414 for (ptr += 3 * iw * y; y < scans && (y + l) < (src_y + h); y++)
418 for (x = 0; x < w; x++)
421 (0xff000000) | ((ptr[0]) << 16) | ((ptr[1]) << 8) | (ptr[2]);
426 ptr2 = tmp + row_stride / 4;
431 else if (cinfo.output_components == 1)
433 for (i = 0; i < (unsigned int) cinfo.rec_outbuf_height; i++)
434 line[i] = tdata + (i * (iw));
435 for (l = 0; l < (ih); l += cinfo.rec_outbuf_height)
437 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
438 scans = cinfo.rec_outbuf_height;
439 if (((ih) - l) < scans) scans = (ih) - l;
442 if (l >= src_y && l < src_y + h)
445 if (src_y < l) y = 0;
446 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
450 for (x = 0; x < w; x++)
453 (0xff000000) | ((ptr[0]) << 16) | ((ptr[0]) << 8) | (ptr[0]);
458 ptr2 = tmp + row_stride / 4;
463 /* end data decoding */
464 jpeg_finish_decompress(&cinfo);
465 jpeg_destroy_decompress(&cinfo);
470 eet_data_image_jpeg_alpha_decode(const void *data, int size, unsigned int src_x, unsigned int src_y,
471 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride)
473 struct jpeg_decompress_struct cinfo;
474 struct _JPEG_error_mgr jerr;
475 unsigned char *ptr, *line[16], *tdata = NULL;
476 unsigned int *ptr2, *tmp;
477 unsigned int x, y, l, scans;
480 memset(&cinfo, 0, sizeof (struct jpeg_decompress_struct));
482 cinfo.err = jpeg_std_error(&(jerr.pub));
483 jerr.pub.error_exit = _JPEGFatalErrorHandler;
484 jerr.pub.emit_message = _JPEGErrorHandler2;
485 jerr.pub.output_message = _JPEGErrorHandler;
486 if (setjmp(jerr.setjmp_buffer)) return NULL;
487 jpeg_create_decompress(&cinfo);
489 if (eet_jpeg_membuf_src(&cinfo, data, (size_t)size))
491 jpeg_destroy_decompress(&cinfo);
495 jpeg_read_header(&cinfo, TRUE);
496 cinfo.dct_method = JDCT_FASTEST;
497 cinfo.do_fancy_upsampling = FALSE;
498 cinfo.do_block_smoothing = FALSE;
499 jpeg_start_decompress(&cinfo);
502 iw = cinfo.output_width;
503 if (w != cinfo.output_width
504 || h != cinfo.output_height)
509 jpeg_destroy_decompress(&cinfo);
512 /* end head decoding */
514 if (cinfo.rec_outbuf_height > 16)
519 jpeg_destroy_decompress(&cinfo);
522 tdata = alloca(w * 16 * 3);
524 if (cinfo.output_components == 1)
526 for (i = 0; i < (unsigned int) cinfo.rec_outbuf_height; i++)
527 line[i] = tdata + (i * w);
528 for (l = 0; l < h; l += cinfo.rec_outbuf_height)
530 jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
531 scans = cinfo.rec_outbuf_height;
532 if ((h - l) < scans) scans = h - l;
535 if (l >= src_y && l < src_y + h)
538 if (src_y < l) y = 0;
539 for (ptr += iw * y; y < scans && (y + l) < (src_y + h); y++)
543 for (x = 0; x < w; x++)
546 ((*ptr2) & 0x00ffffff) |
552 ptr2 = tmp + row_stride / 4;
557 /* end data decoding */
558 jpeg_finish_decompress(&cinfo);
559 jpeg_destroy_decompress(&cinfo);
564 eet_data_image_lossless_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha)
566 if (_eet_image_words_bigendian == -1)
570 v = htonl(0x12345678);
571 if (v == 0x12345678) _eet_image_words_bigendian = 1;
572 else _eet_image_words_bigendian = 0;
578 d = malloc((w * h * 4) + (8 * 4));
584 header[0] = 0xac1dfeed;
589 memcpy(d + 32, data, w * h * 4);
591 if (_eet_image_words_bigendian)
595 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
597 *size = ((w * h * 4) + (8 * 4));
603 eet_data_image_lossless_compressed_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha, int compression)
605 if (_eet_image_words_bigendian == -1)
609 v = htonl(0x12345678);
610 if (v == 0x12345678) _eet_image_words_bigendian = 1;
611 else _eet_image_words_bigendian = 0;
621 d = malloc((w * h * 4) + (8 * 4));
623 buflen = (((w * h * 101) / 100) + 3) * 4;
624 comp = malloc(buflen);
633 header[0] = 0xac1dfeed;
637 header[4] = compression;
638 memcpy(d + 32, data, w * h * 4);
640 if (_eet_image_words_bigendian)
644 for (i = 0; i < ((w * h) + 8); i++) SWAP32(header[i]);
646 ret = compress2((Bytef *)comp, &buflen,
650 if (ret != Z_OK || buflen > (w * h * 4))
657 memcpy(d + 32, comp, buflen);
658 *size = (8 * 4) + buflen;
665 eet_data_image_jpeg_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha, int quality)
667 struct jpeg_compress_struct cinfo;
668 struct _JPEG_error_mgr jerr;
675 (void) alpha; /* unused */
679 memset(&cinfo, 0, sizeof (struct jpeg_compress_struct));
681 cinfo.err = jpeg_std_error(&(jerr.pub));
682 jerr.pub.error_exit = _JPEGFatalErrorHandler;
683 jerr.pub.emit_message = _JPEGErrorHandler2;
684 jerr.pub.output_message = _JPEGErrorHandler;
685 if (setjmp(jerr.setjmp_buffer)) return NULL;
686 jpeg_create_compress(&cinfo);
688 if (eet_jpeg_membuf_dst(&cinfo, &d, &sz))
690 jpeg_destroy_compress(&cinfo);
694 cinfo.image_width = w;
695 cinfo.image_height = h;
696 cinfo.input_components = 3;
697 cinfo.in_color_space = JCS_RGB;
698 jpeg_set_defaults(&cinfo);
699 jpeg_set_quality(&cinfo, quality, TRUE);
702 cinfo.comp_info[0].h_samp_factor = 1;
703 cinfo.comp_info[0].v_samp_factor = 1;
704 cinfo.comp_info[1].h_samp_factor = 1;
705 cinfo.comp_info[1].v_samp_factor = 1;
706 cinfo.comp_info[2].h_samp_factor = 1;
707 cinfo.comp_info[2].v_samp_factor = 1;
709 jpeg_start_compress(&cinfo, TRUE);
711 while (cinfo.next_scanline < cinfo.image_height)
715 /* convert scaline from ARGB to RGB packed */
716 ptr = ((const int*) data) + cinfo.next_scanline * w;
717 for (j = 0, i = 0; i < w; i++)
719 buf[j++] = ((*ptr) >> 16) & 0xff;
720 buf[j++] = ((*ptr) >> 8) & 0xff;
721 buf[j++] = ((*ptr)) & 0xff;
724 jbuf = (JSAMPROW *) (&buf);
725 jpeg_write_scanlines(&cinfo, jbuf, 1);
728 jpeg_finish_compress(&cinfo);
729 jpeg_destroy_compress(&cinfo);
736 eet_data_image_jpeg_alpha_convert(int *size, const void *data, unsigned int w, unsigned int h, int alpha, int quality)
738 unsigned char *d1, *d2;
743 (void) alpha; /* unused */
745 if (_eet_image_words_bigendian == -1)
749 v = htonl(0x12345678);
750 if (v == 0x12345678) _eet_image_words_bigendian = 1;
751 else _eet_image_words_bigendian = 0;
758 struct _JPEG_error_mgr jerr;
760 struct jpeg_compress_struct cinfo;
765 cinfo.err = jpeg_std_error(&(jerr.pub));
766 jerr.pub.error_exit = _JPEGFatalErrorHandler;
767 jerr.pub.emit_message = _JPEGErrorHandler2;
768 jerr.pub.output_message = _JPEGErrorHandler;
769 if (setjmp(jerr.setjmp_buffer)) return NULL;
771 jpeg_create_compress(&cinfo);
772 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
774 jpeg_destroy_compress(&cinfo);
778 cinfo.image_width = w;
779 cinfo.image_height = h;
780 cinfo.input_components = 3;
781 cinfo.in_color_space = JCS_RGB;
782 jpeg_set_defaults(&cinfo);
783 jpeg_set_quality(&cinfo, quality, TRUE);
786 cinfo.comp_info[0].h_samp_factor = 1;
787 cinfo.comp_info[0].v_samp_factor = 1;
788 cinfo.comp_info[1].h_samp_factor = 1;
789 cinfo.comp_info[1].v_samp_factor = 1;
790 cinfo.comp_info[2].h_samp_factor = 1;
791 cinfo.comp_info[2].v_samp_factor = 1;
793 jpeg_start_compress(&cinfo, TRUE);
795 while (cinfo.next_scanline < cinfo.image_height)
799 ptr = ((const int*) data) + cinfo.next_scanline * w;
800 /* convert scaline from ARGB to RGB packed */
801 for (j = 0, i = 0; i < w; i++)
803 buf[j++] = ((*ptr) >> 16) & 0xff;
804 buf[j++] = ((*ptr) >> 8) & 0xff;
805 buf[j++] = ((*ptr)) & 0xff;
808 jbuf = (JSAMPROW *) (&buf);
809 jpeg_write_scanlines(&cinfo, jbuf, 1);
812 jpeg_finish_compress(&cinfo);
813 jpeg_destroy_compress(&cinfo);
822 struct _JPEG_error_mgr jerr;
824 struct jpeg_compress_struct cinfo;
829 cinfo.err = jpeg_std_error(&(jerr.pub));
830 jerr.pub.error_exit = _JPEGFatalErrorHandler;
831 jerr.pub.emit_message = _JPEGErrorHandler2;
832 jerr.pub.output_message = _JPEGErrorHandler;
833 if (setjmp(jerr.setjmp_buffer))
839 jpeg_create_compress(&cinfo);
840 if (eet_jpeg_membuf_dst(&cinfo, &dst, &sz))
842 jpeg_destroy_compress(&cinfo);
847 cinfo.image_width = w;
848 cinfo.image_height = h;
849 cinfo.input_components = 1;
850 cinfo.in_color_space = JCS_GRAYSCALE;
851 jpeg_set_defaults(&cinfo);
852 jpeg_set_quality(&cinfo, quality, TRUE);
855 cinfo.comp_info[0].h_samp_factor = 1;
856 cinfo.comp_info[0].v_samp_factor = 1;
857 cinfo.comp_info[1].h_samp_factor = 1;
858 cinfo.comp_info[1].v_samp_factor = 1;
859 cinfo.comp_info[2].h_samp_factor = 1;
860 cinfo.comp_info[2].v_samp_factor = 1;
862 jpeg_start_compress(&cinfo, TRUE);
864 while (cinfo.next_scanline < cinfo.image_height)
868 ptr = ((const int*) data) + cinfo.next_scanline * w;
869 /* convert scaline from ARGB to RGB packed */
870 for (j = 0, i = 0; i < w; i++)
872 buf[j++] = ((*ptr) >> 24) & 0xff;
875 jbuf = (JSAMPROW *) (&buf);
876 jpeg_write_scanlines(&cinfo, jbuf, 1);
879 jpeg_finish_compress(&cinfo);
880 jpeg_destroy_compress(&cinfo);
885 d = malloc(12 + sz1 + sz2);
893 header[0] = 0xbeeff00d;
896 if (_eet_image_words_bigendian)
900 for (i = 0; i < 3; i++) SWAP32(header[i]);
902 memcpy(d + 12, d1, sz1);
903 memcpy(d + 12 + sz1, d2, sz2);
907 *size = 12 + sz1 + sz2;
912 eet_data_image_write_cipher(Eet_File *ef, const char *name, const char *cipher_key,
913 const void *data, unsigned int w, unsigned int h, int alpha,
914 int comp, int quality, int lossy)
919 d = eet_data_image_encode(data, &size, w, h, alpha, comp, quality, lossy);
924 v = eet_write_cipher(ef, name, d, size, 0, cipher_key);
932 eet_data_image_write(Eet_File *ef, const char *name,
933 const void *data, unsigned int w, unsigned int h, int alpha,
934 int comp, int quality, int lossy)
936 return eet_data_image_write_cipher(ef, name, NULL, data, w, h, alpha, comp, quality, lossy);
941 eet_data_image_read_cipher(Eet_File *ef, const char *name, const char *cipher_key,
942 unsigned int *w, unsigned int *h, int *alpha,
943 int *comp, int *quality, int *lossy)
945 unsigned int *d = NULL;
951 data = (void *)eet_read_direct(ef, name, &size);
954 data = eet_read_cipher(ef, name, &size, cipher_key);
956 if (!data) return NULL;
959 d = eet_data_image_decode(data, size, w, h, alpha, comp, quality, lossy);
968 eet_data_image_read(Eet_File *ef, const char *name,
969 unsigned int *w, unsigned int *h, int *alpha,
970 int *comp, int *quality, int *lossy)
972 return eet_data_image_read_cipher(ef, name, NULL, w, h, alpha, comp, quality, lossy);
976 eet_data_image_read_to_surface_cipher(Eet_File *ef, const char *name, const char *cipher_key, unsigned int src_x, unsigned int src_y,
977 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride,
978 int *alpha, int *comp, int *quality, int *lossy)
986 data = (void *)eet_read_direct(ef, name, &size);
989 data = eet_read_cipher(ef, name, &size, cipher_key);
994 res = eet_data_image_decode_to_surface(data, size, src_x, src_y, d, w, h, row_stride, alpha, comp, quality, lossy);
1003 eet_data_image_read_to_surface(Eet_File *ef, const char *name, unsigned int src_x, unsigned int src_y,
1004 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride,
1005 int *alpha, int *comp, int *quality, int *lossy)
1007 return eet_data_image_read_to_surface_cipher(ef, name, NULL, src_x, src_y, d, w, h, row_stride, alpha, comp, quality, lossy);
1011 eet_data_image_header_read_cipher(Eet_File *ef, const char *name, const char *cipher_key,
1012 unsigned int *w, unsigned int *h, int *alpha,
1013 int *comp, int *quality, int *lossy)
1021 data = (void *)eet_read_direct(ef, name, &size);
1024 data = eet_read_cipher(ef, name, &size, cipher_key);
1026 if (!data) return 0;
1029 d = eet_data_image_header_decode(data, size, w, h, alpha, comp, quality, lossy);
1037 eet_data_image_header_read(Eet_File *ef, const char *name,
1038 unsigned int *w, unsigned int *h, int *alpha,
1039 int *comp, int *quality, int *lossy)
1041 return eet_data_image_header_read_cipher(ef, name, NULL, w, h, alpha, comp, quality, lossy);
1046 eet_data_image_encode_cipher(const void *data, const char *cipher_key, unsigned int w, unsigned int h, int alpha, int comp, int quality, int lossy, int *size_ret)
1049 void *ciphered_d = NULL;
1050 unsigned int ciphered_sz = 0;
1056 d = eet_data_image_lossless_compressed_convert(&size, data, w, h, alpha, comp);
1058 /* eet_data_image_lossless_compressed_convert will refuse to compress something
1059 if the result is bigger than the entry. */
1060 if (comp <= 0 || d == NULL)
1061 d = eet_data_image_lossless_convert(&size, data, w, h, alpha);
1066 d = eet_data_image_jpeg_convert(&size, data, w, h, alpha, quality);
1068 d = eet_data_image_jpeg_alpha_convert(&size, data, w, h, alpha, quality);
1072 if(!eet_cipher(d, size, cipher_key, strlen(cipher_key), &ciphered_d, &ciphered_sz))
1079 if (ciphered_d) free(ciphered_d);
1082 if (size_ret) *size_ret = size;
1087 eet_data_image_encode(const void *data, int *size_ret, unsigned int w, unsigned int h, int alpha, int comp, int quality, int lossy)
1089 return eet_data_image_encode_cipher(data, NULL, w, h, alpha, comp, quality, lossy, size_ret);
1093 eet_data_image_header_decode_cipher(const void *data, const char *cipher_key, int size, unsigned int *w, unsigned int *h, int *alpha, int *comp, int *quality, int *lossy)
1096 void *deciphered_d = NULL;
1097 unsigned int deciphered_sz = 0;
1101 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key), &deciphered_d, &deciphered_sz))
1103 data = deciphered_d;
1104 size = deciphered_sz;
1107 if (deciphered_d) free(deciphered_d);
1110 if (_eet_image_words_bigendian == -1)
1112 unsigned long int v;
1114 v = htonl(0x12345678);
1115 if (v == 0x12345678) _eet_image_words_bigendian = 1;
1116 else _eet_image_words_bigendian = 0;
1119 if (size < 32) return 0;
1121 memcpy(header, data, 32);
1122 if (_eet_image_words_bigendian)
1126 for (i = 0; i < 8; i++) SWAP32(header[i]);
1128 if ((unsigned)header[0] == 0xac1dfeed)
1136 if ((iw < 1) || (ih < 1) || (iw > 8192) || (ih > 8192)) return 0;
1137 if ((cp == 0) && (size < ((iw * ih * 4) + 32))) return 0;
1140 if (alpha) *alpha = al ? 1 : 0;
1141 if (comp) *comp = cp;
1142 if (lossy) *lossy = 0;
1143 if (quality) *quality = 100;
1146 else if ((unsigned)header[0] == 0xbeeff00d)
1148 unsigned int iw = 0, ih = 0;
1149 unsigned const char *dt;
1154 /* sz2 = header[2]; */
1157 ok = eet_data_image_jpeg_header_decode(dt, sz1, &iw, &ih);
1162 if (alpha) *alpha = 1;
1163 if (comp) *comp = 0;
1164 if (lossy) *lossy = 1;
1165 if (quality) *quality = 75;
1171 unsigned int iw = 0, ih = 0;
1174 ok = eet_data_image_jpeg_header_decode(data, size, &iw, &ih);
1179 if (alpha) *alpha = 0;
1180 if (comp) *comp = 0;
1181 if (lossy) *lossy = 1;
1182 if (quality) *quality = 75;
1190 eet_data_image_header_decode(const void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *comp, int *quality, int *lossy)
1192 return eet_data_image_header_decode_cipher(data, NULL, size, w, h, alpha, comp, quality, lossy);
1196 _eet_data_image_copy_buffer(const unsigned int *src, unsigned int src_x, unsigned int src_y, unsigned int src_w,
1197 unsigned int *dst, unsigned int w, unsigned int h, unsigned int row_stride)
1199 src += src_x + src_y * src_w;
1201 if (row_stride == src_w * 4 && w == src_w)
1203 memcpy(dst, src, row_stride * h);
1207 unsigned int *over = dst;
1210 for (y = 0; y < h; ++y, src += src_w, over += row_stride)
1211 memcpy(over, src, w * 4);
1217 _eet_data_image_decode_inside(const void *data, int size, unsigned int src_x, unsigned int src_y,
1218 unsigned int src_w, unsigned int src_h,
1219 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride,
1220 int alpha, int comp, int quality, int lossy)
1222 if (lossy == 0 && quality == 100)
1226 body = ((unsigned int *)data) + 8;
1229 _eet_data_image_copy_buffer(body, src_x, src_y, src_w, d, w, h, row_stride);
1233 if (src_h == h && src_w == w && row_stride == src_w * 4)
1238 uncompress((Bytef *)d, &dlen, (Bytef *)body,
1239 (uLongf)(size - 32));
1244 uLongf dlen = src_w * src_h * 4;
1246 /* FIXME: This could create a huge alloc. So compressed data and tile could not always work. */
1247 dtmp = malloc(dlen);
1248 if (!dtmp) return 0;
1250 uncompress(dtmp, &dlen, (Bytef *)body, (uLongf)(size - 32));
1252 _eet_data_image_copy_buffer((unsigned int *) dtmp, src_x, src_y, src_w, d, w, h, row_stride);
1258 /* Fix swapiness. */
1259 if (_eet_image_words_bigendian)
1263 for (x = 0; x < (w * h); x++) SWAP32(d[x]);
1266 else if (comp == 0 && lossy == 1)
1270 unsigned const char *dt;
1274 memcpy(header, data, 32);
1275 if (_eet_image_words_bigendian)
1279 for (i = 0; i < 8; i++) SWAP32(header[i]);
1287 if (eet_data_image_jpeg_rgb_decode(dt, sz1, src_x, src_y, d, w, h, row_stride))
1290 if (!eet_data_image_jpeg_alpha_decode(dt, sz2, src_x, src_y, d, w, h, row_stride))
1296 if (!eet_data_image_jpeg_rgb_decode(data, size, src_x, src_y, d, w, h, row_stride))
1309 eet_data_image_decode_cipher(const void *data, const char *cipher_key, int size, unsigned int *w, unsigned int *h, int *alpha, int *comp, int *quality, int *lossy)
1311 unsigned int *d = NULL;
1312 unsigned int iw, ih;
1313 int ialpha, icompress, iquality, ilossy;
1314 void *deciphered_d = NULL;
1315 unsigned int deciphered_sz = 0;
1319 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key), &deciphered_d, &deciphered_sz))
1321 data = deciphered_d;
1322 size = deciphered_sz;
1325 if (deciphered_d) free(deciphered_d);
1328 /* All check are done during header decode, this simplify the code a lot. */
1329 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress, &iquality, &ilossy))
1332 d = malloc(iw * ih * 4);
1333 if (!d) return NULL;
1335 if (!_eet_data_image_decode_inside(data, size, 0, 0, iw, ih, d, iw, ih, iw * 4, ialpha, icompress, iquality, ilossy))
1343 if (alpha) *alpha = ialpha;
1344 if (comp) *comp = icompress;
1345 if (quality) *quality = iquality;
1346 if (lossy) *lossy = ilossy;
1352 eet_data_image_decode(const void *data, int size, unsigned int *w, unsigned int *h, int *alpha, int *comp, int *quality, int *lossy)
1354 return eet_data_image_decode_cipher(data, NULL, size, w, h, alpha, comp, quality, lossy);
1358 eet_data_image_decode_to_surface_cipher(const void *data, const char *cipher_key, int size, unsigned int src_x, unsigned int src_y,
1359 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride,
1360 int *alpha, int *comp, int *quality, int *lossy)
1362 unsigned int iw, ih;
1363 int ialpha, icompress, iquality, ilossy;
1364 void *deciphered_d = NULL;
1365 unsigned int deciphered_sz = 0;
1369 if (!eet_decipher(data, size, cipher_key, strlen(cipher_key), &deciphered_d, &deciphered_sz))
1371 data = deciphered_d;
1372 size = deciphered_sz;
1375 if (deciphered_d) free(deciphered_d);
1378 /* All check are done during header decode, this simplify the code a lot. */
1379 if (!eet_data_image_header_decode(data, size, &iw, &ih, &ialpha, &icompress, &iquality, &ilossy))
1383 if (w * 4 > row_stride) return 0;
1384 if (w > iw || h > ih) return 0;
1386 if (!_eet_data_image_decode_inside(data, size, src_x, src_y, iw, ih, d, w, h, row_stride, ialpha, icompress, iquality, ilossy))
1389 if (alpha) *alpha = ialpha;
1390 if (comp) *comp = icompress;
1391 if (quality) *quality = iquality;
1392 if (lossy) *lossy = ilossy;
1398 eet_data_image_decode_to_surface(const void *data, int size, unsigned int src_x, unsigned int src_y,
1399 unsigned int *d, unsigned int w, unsigned int h, unsigned int row_stride,
1400 int *alpha, int *comp, int *quality, int *lossy)
1402 return eet_data_image_decode_to_surface_cipher(data, NULL, size, src_x, src_y, d, w, h, row_stride, alpha, comp, quality, lossy);