Hi all,
attached patch converts the reading and writing of JPEG files to
directly use the libjpeg interface and not a memory file.
The memory file is problematic on !Linux && !BSD and redundant.
As the memfile code is unused, it is removed as well.
Joerg
Index: src/lib/Eet_private.h
===================================================================
RCS file: /var/cvs/e/e17/libs/eet/src/lib/Eet_private.h,v
retrieving revision 1.27
diff -u -p -r1.27 Eet_private.h
--- src/lib/Eet_private.h 17 Jul 2008 20:28:59 -0000 1.27
+++ src/lib/Eet_private.h 20 Aug 2008 16:33:39 -0000
@@ -62,11 +62,6 @@ int eet_dictionary_string_g
int eet_dictionary_string_get_double(const Eet_Dictionary *ed,
int index, double *result);
int eet_dictionary_string_get_hash(const Eet_Dictionary *ed, int
index);
-FILE *_eet_memfile_read_open(const void *data, size_t size);
-void _eet_memfile_read_close(FILE *f);
-FILE *_eet_memfile_write_open(void **data, size_t *size);
-void _eet_memfile_write_close(FILE *f);
-void _eet_memfile_shutdown(void);
int _eet_hash_gen(const char *key, int hash_size);
int _eet_string_to_double_convert(const char *src, long long *m, long *e);
void _eet_double_to_string_convert(char des[128], double d);
Index: src/lib/Makefile.am
===================================================================
RCS file: /var/cvs/e/e17/libs/eet/src/lib/Makefile.am,v
retrieving revision 1.25
diff -u -p -r1.25 Makefile.am
--- src/lib/Makefile.am 16 May 2008 15:07:03 -0000 1.25
+++ src/lib/Makefile.am 20 Aug 2008 16:33:39 -0000
@@ -18,7 +18,6 @@ libeet_la_SOURCES = \
eet_lib.c \
eet_data.c \
eet_image.c \
-eet_memfile.c \
eet_dictionary.c \
eet_utils.c
Index: src/lib/eet_image.c
===================================================================
RCS file: /var/cvs/e/e17/libs/eet/src/lib/eet_image.c,v
retrieving revision 1.23
diff -u -p -r1.23 eet_image.c
--- src/lib/eet_image.c 24 Jul 2008 13:25:18 -0000 1.23
+++ src/lib/eet_image.c 20 Aug 2008 16:33:40 -0000
@@ -48,6 +48,138 @@ struct _JPEG_error_mgr
jmp_buf setjmp_buffer;
};
+struct jpeg_membuf_src {
+ struct jpeg_source_mgr pub;
+
+ const char *buf;
+ size_t len;
+};
+
+static void
+jpeg_membuf_src_init(j_decompress_ptr cinfo)
+{
+}
+
+static boolean
+jpeg_membuf_src_fill(j_decompress_ptr cinfo)
+{
+ static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
+ struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
+
+ src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
+ src->pub.next_input_byte = jpeg_eoi;
+}
+
+static void
+jpeg_membuf_src_skip(j_decompress_ptr cinfo, long num_bytes)
+{
+ struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
+
+ src->pub.bytes_in_buffer -= num_bytes;
+ src->pub.next_input_byte += num_bytes;
+}
+
+static void
+jpeg_membuf_src_term(j_decompress_ptr cinfo)
+{
+ free(cinfo->src);
+ cinfo->src = NULL;
+}
+
+static int
+jpeg_membuf_src(j_decompress_ptr cinfo, const void *buf, size_t len)
+{
+ struct jpeg_membuf_src *src;
+
+ if ((src = malloc(sizeof(*src))) == NULL)
+ return -1;
+ cinfo->src = &src->pub;
+ src->buf = buf;
+ src->len = len;
+ src->pub.init_source = jpeg_membuf_src_init;
+ src->pub.fill_input_buffer = jpeg_membuf_src_fill;
+ src->pub.skip_input_data = jpeg_membuf_src_skip;
+ src->pub.resync_to_restart = jpeg_resync_to_restart;
+ src->pub.term_source = jpeg_membuf_src_term;
+ src->pub.bytes_in_buffer = src->len;
+ src->pub.next_input_byte = src->buf;
+}
+
+struct jpeg_membuf_dst {
+ struct jpeg_destination_mgr pub;
+
+ void **dst_buf;
+ size_t *dst_len;
+
+ char *buf;
+ size_t len;
+ int failed;
+};
+
+static void
+jpeg_membuf_dst_init(j_compress_ptr cinfo)
+{
+}
+
+static boolean
+jpeg_membuf_dst_flush(j_compress_ptr cinfo)
+{
+ struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
+ char *buf;
+
+ if (dst->len >= 0x40000000 ||
+ (buf = realloc(dst->buf, dst->len * 2)) == NULL) {
+ dst->failed;
+ dst->pub.next_output_byte = dst->buf;
+ dst->pub.free_in_buffer = dst->len;
+ return TRUE;
+ }
+
+ dst->buf = buf;
+ dst->pub.next_output_byte =
+ dst->buf + dst->len - dst->pub.free_in_buffer;
+ dst->pub.free_in_buffer += dst->len;
+ dst->len *= 2;
+}
+
+static void
+jpeg_membuf_dst_term(j_compress_ptr cinfo)
+{
+ struct jpeg_membuf_dst *dst = (struct jpeg_membuf_dst *)cinfo->dest;
+
+ if (dst->failed) {
+ *dst->dst_buf = NULL;
+ *dst->dst_len = 0;
+ free(dst->buf);
+ } else {
+ *dst->dst_buf = dst->buf;
+ *dst->dst_len = dst->len;
+ }
+ free(dst);
+ cinfo->dest = NULL;
+}
+
+static int
+jpeg_membuf_dst(j_compress_ptr cinfo, void **buf, size_t *len)
+{
+ struct jpeg_membuf_dst *dst;
+
+ if ((dst = malloc(sizeof(*dst))) == NULL)
+ return -1;
+ if ((dst->buf = malloc(32768)) == NULL) {
+ free(dst);
+ return -1;
+ }
+ dst->len = 32768;
+
+ cinfo->dest = &dst->pub;
+ dst->pub.init_destination = jpeg_membuf_dst_init;
+ dst->pub.empty_output_buffer = jpeg_membuf_dst_flush;
+ dst->pub.term_destination = jpeg_membuf_dst_term;
+ dst->pub.free_in_buffer = dst->len;
+ dst->pub.next_output_byte = dst->buf;
+}
+
/*---*/
static void _JPEGFatalErrorHandler(j_common_ptr cinfo);
@@ -132,22 +264,19 @@ eet_data_image_jpeg_header_decode(const
{
struct jpeg_decompress_struct cinfo;
struct _JPEG_error_mgr jerr;
- FILE *f;
- f = _eet_memfile_read_open(data, (size_t)size);
- if (!f) return 0;
cinfo.err = jpeg_std_error(&(jerr.pub));
jerr.pub.error_exit = _JPEGFatalErrorHandler;
jerr.pub.emit_message = _JPEGErrorHandler2;
jerr.pub.output_message = _JPEGErrorHandler;
if (setjmp(jerr.setjmp_buffer))
{
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return 0;
}
jpeg_create_decompress(&cinfo);
- jpeg_stdio_src(&cinfo, f);
+ jpeg_membuf_src(&cinfo, data, (size_t)size);
jpeg_read_header(&cinfo, TRUE);
cinfo.do_fancy_upsampling = FALSE;
cinfo.do_block_smoothing = FALSE;
@@ -156,15 +285,11 @@ eet_data_image_jpeg_header_decode(const
/* head decoding */
*w = cinfo.output_width;
*h = cinfo.output_height;
- if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
- {
- jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
- return 0;
- }
- /* end head decoding */
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
+
+ if ((*w < 1) || (*h < 1) || (*w > 8192) || (*h > 8192))
+ return 0;
return 1;
}
@@ -179,13 +304,10 @@ eet_data_image_jpeg_rgb_decode(const voi
unsigned int iw, ih;
int x, y, l, scans;
int i, count, prevy;
- FILE *f;
/* FIXME: handle src_x, src_y and row_stride correctly */
if (!d) return 0;
- f = _eet_memfile_read_open(data, (size_t)size);
- if (!f) return 0;
cinfo.err = jpeg_std_error(&(jerr.pub));
jerr.pub.error_exit = _JPEGFatalErrorHandler;
jerr.pub.emit_message = _JPEGErrorHandler2;
@@ -193,12 +315,12 @@ eet_data_image_jpeg_rgb_decode(const voi
if (setjmp(jerr.setjmp_buffer))
{
if (tdata) free(tdata);
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return 0;
}
jpeg_create_decompress(&cinfo);
- jpeg_stdio_src(&cinfo, f);
+ jpeg_membuf_src(&cinfo, data, (size_t)size);
jpeg_read_header(&cinfo, TRUE);
cinfo.dct_method = JDCT_FASTEST;
cinfo.do_fancy_upsampling = FALSE;
@@ -210,16 +332,16 @@ eet_data_image_jpeg_rgb_decode(const voi
ih = cinfo.output_height;
if ((iw != w) || (ih != h))
{
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return 0;
}
/* end head decoding */
/* data decoding */
if (cinfo.rec_outbuf_height > 16)
{
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return 0;
}
tdata = alloca((iw) * 16 * 3);
@@ -302,7 +424,6 @@ eet_data_image_jpeg_rgb_decode(const voi
/* end data decoding */
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return 1;
}
@@ -316,19 +437,7 @@ eet_data_image_jpeg_alpha_decode(const v
unsigned int *ptr2, *tmp;
int x, y, l, scans;
int i, count, prevy, iw;
- FILE *f;
- f = _eet_memfile_read_open(data, (size_t)size);
- if (!f) return NULL;
-
- if (0)
- {
- char buf[1];
-
- while (fread(buf, 1, 1, f));
- _eet_memfile_read_close(f);
- return d;
- }
cinfo.err = jpeg_std_error(&(jerr.pub));
jerr.pub.error_exit = _JPEGFatalErrorHandler;
jerr.pub.emit_message = _JPEGErrorHandler2;
@@ -336,12 +445,12 @@ eet_data_image_jpeg_alpha_decode(const v
if (setjmp(jerr.setjmp_buffer))
{
if (tdata) free(tdata);
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return NULL;
}
jpeg_create_decompress(&cinfo);
- jpeg_stdio_src(&cinfo, f);
+ jpeg_membuf_src(&cinfo, data, (size_t)size);
jpeg_read_header(&cinfo, TRUE);
cinfo.dct_method = JDCT_FASTEST;
cinfo.do_fancy_upsampling = FALSE;
@@ -353,16 +462,16 @@ eet_data_image_jpeg_alpha_decode(const v
if (w != cinfo.output_width
|| h != cinfo.output_height)
{
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return NULL;
}
/* end head decoding */
/* data decoding */
if (cinfo.rec_outbuf_height > 16)
{
+ free(cinfo.src);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return NULL;
}
tdata = alloca(w * 16 * 3);
@@ -409,7 +518,6 @@ eet_data_image_jpeg_alpha_decode(const v
/* end data decoding */
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
- _eet_memfile_read_close(f);
return d;
}
@@ -503,9 +611,8 @@ eet_data_image_lossless_compressed_conve
if (buflen > (w * h * 4))
{
free(comp);
- free(d);
- *size = -1;
- return NULL;
+ *size = ((w * h * 4) + (8 * 4));
+ return d;
}
memcpy(d + 32, comp, buflen);
*size = (8 * 4) + buflen;
@@ -528,9 +635,6 @@ eet_data_image_jpeg_convert(int *size, c
(void) alpha; /* unused */
- f =_eet_memfile_write_open(&d, &sz);
- if (!f) return NULL;
-
buf = alloca(3 * w);
cinfo.err = jpeg_std_error(&(jerr.pub));
@@ -540,12 +644,12 @@ eet_data_image_jpeg_convert(int *size, c
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
- _eet_memfile_write_close(f);
+ /* XXX free buffer in cinfo */
if (d) free(d);
return NULL;
}
jpeg_create_compress(&cinfo);
- jpeg_stdio_dest(&cinfo, f);
+ jpeg_membuf_dst(&cinfo, &d, &sz);
cinfo.image_width = w;
cinfo.image_height = h;
cinfo.input_components = 3;
@@ -583,7 +687,6 @@ eet_data_image_jpeg_convert(int *size, c
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
- _eet_memfile_write_close(f);
*size = sz;
return d;
}
@@ -614,12 +717,8 @@ eet_data_image_jpeg_alpha_convert(int *s
struct _JPEG_error_mgr jerr;
JSAMPROW *jbuf;
struct jpeg_compress_struct cinfo;
- FILE *f;
unsigned char *buf;
- f = _eet_memfile_write_open(&d, &sz);
- if (!f) return NULL;
-
buf = alloca(3 * w);
cinfo.err = jpeg_std_error(&(jerr.pub));
@@ -629,12 +728,11 @@ eet_data_image_jpeg_alpha_convert(int *s
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
- _eet_memfile_write_close(f);
if (d) free(d);
return NULL;
}
jpeg_create_compress(&cinfo);
- jpeg_stdio_dest(&cinfo, f);
+ jpeg_membuf_dst(&cinfo, &d, &sz);
cinfo.image_width = w;
cinfo.image_height = h;
cinfo.input_components = 3;
@@ -672,7 +770,6 @@ eet_data_image_jpeg_alpha_convert(int *s
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
- _eet_memfile_write_close(f);
d1 = d;
sz1 = sz;
}
@@ -683,16 +780,8 @@ eet_data_image_jpeg_alpha_convert(int *s
struct _JPEG_error_mgr jerr;
JSAMPROW *jbuf;
struct jpeg_compress_struct cinfo;
- FILE *f;
unsigned char *buf;
- f = _eet_memfile_write_open(&d, &sz);
- if (!f)
- {
- free(d1);
- return NULL;
- }
-
buf = alloca(3 * w);
cinfo.err = jpeg_std_error(&(jerr.pub));
@@ -702,13 +791,12 @@ eet_data_image_jpeg_alpha_convert(int *s
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
- _eet_memfile_write_close(f);
if (d) free(d);
free(d1);
return NULL;
}
jpeg_create_compress(&cinfo);
- jpeg_stdio_dest(&cinfo, f);
+ jpeg_membuf_dst(&cinfo, &d, &sz);
cinfo.image_width = w;
cinfo.image_height = h;
cinfo.input_components = 1;
@@ -744,7 +832,6 @@ eet_data_image_jpeg_alpha_convert(int *s
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
- _eet_memfile_write_close(f);
d2 = d;
sz2 = sz;
}
@@ -881,13 +968,10 @@ eet_data_image_encode(const void *data,
if (lossy == 0)
{
- if (compress > 0)
- d = eet_data_image_lossless_compressed_convert(&size, data, w, h,
alpha, compress);
-
- /* eet_data_image_lossless_compressed_convert will refuse to compress
something
- if the result is bigger than the entry. */
- if (compress <= 0 || d == NULL)
+ if (compress <= 0)
d = eet_data_image_lossless_convert(&size, data, w, h, alpha);
+ else
+ d = eet_data_image_lossless_compressed_convert(&size, data, w, h,
alpha, compress);
}
else
{
Index: src/lib/eet_lib.c
===================================================================
RCS file: /var/cvs/e/e17/libs/eet/src/lib/eet_lib.c,v
retrieving revision 1.113
diff -u -p -r1.113 eet_lib.c
--- src/lib/eet_lib.c 13 Aug 2008 16:40:53 -0000 1.113
+++ src/lib/eet_lib.c 20 Aug 2008 16:33:40 -0000
@@ -669,7 +669,6 @@ eet_shutdown(void)
if (--eet_initcount == 0)
{
eet_clearcache();
- _eet_memfile_shutdown();
}
return eet_initcount;
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel