Index: libgthumb/jpegutils/jpeg-memory-mgr.c
===================================================================
RCS file: /cvs/gnome/gthumb/libgthumb/jpegutils/jpeg-memory-mgr.c,v
retrieving revision 1.1
diff -u -p -r1.1 jpeg-memory-mgr.c
--- libgthumb/jpegutils/jpeg-memory-mgr.c	16 Nov 2006 15:52:33 -0000	1.1
+++ libgthumb/jpegutils/jpeg-memory-mgr.c	23 Nov 2006 14:14:49 -0000
@@ -117,7 +117,7 @@ term_destination (j_compress_ptr cinfo)
  */
 
 GLOBAL(void)
-jpeg_memory_src (j_decompress_ptr cinfo, void *data, size_t size)
+jpeg_memory_src (j_decompress_ptr cinfo, const void *data, size_t size)
 {
 	my_src_ptr src = NULL;
 
Index: libgthumb/jpegutils/jpeg-memory-mgr.h
===================================================================
RCS file: /cvs/gnome/gthumb/libgthumb/jpegutils/jpeg-memory-mgr.h,v
retrieving revision 1.1
diff -u -p -r1.1 jpeg-memory-mgr.h
--- libgthumb/jpegutils/jpeg-memory-mgr.h	16 Nov 2006 15:52:33 -0000	1.1
+++ libgthumb/jpegutils/jpeg-memory-mgr.h	23 Nov 2006 14:14:49 -0000
@@ -26,7 +26,7 @@
 #include <jpeglib.h>
 
 GLOBAL(void)
-jpeg_memory_src (j_decompress_ptr cinfo, void *data, size_t size);
+jpeg_memory_src (j_decompress_ptr cinfo, const void *data, size_t size);
 
 GLOBAL(void)
 jpeg_memory_dest (j_compress_ptr cinfo, void **data, size_t *size);
Index: libgthumb/jpegutils/jpegtran.c
===================================================================
RCS file: /cvs/gnome/gthumb/libgthumb/jpegutils/jpegtran.c,v
retrieving revision 1.8
diff -u -p -r1.8 jpegtran.c
--- libgthumb/jpegutils/jpegtran.c	16 Nov 2006 15:52:33 -0000	1.8
+++ libgthumb/jpegutils/jpegtran.c	23 Nov 2006 14:14:49 -0000
@@ -53,11 +53,11 @@
 
 #include <libexif/exif-data.h>
 
-static void
-jpegtran_internal (struct jpeg_decompress_struct *srcinfo,
-						struct jpeg_compress_struct *dstinfo,
-						JXFORM_CODE transformation,
-						gboolean trim);
+static int
+jpegtran_thumbnail (const void *idata, size_t isize,
+		void **odata, size_t *osize,
+		JXFORM_CODE transformation,
+		gboolean trim);
 
 /* error handler data */
 struct error_handler_data {
@@ -216,34 +216,22 @@ update_exif_thumbnail (ExifData *edata, 
 		return;
 
 	if (edata->data && edata->data[0] == 0xff && edata->data[1] == 0xd8) {
-    	/* Allocate output buffer */
-    	unsigned int osize = edata->size * 2;
-    	unsigned char *out = malloc(osize);
-
-		/* Initialize the JPEG decompression object
-		 * with default error handling. */
-		src.err = jpeg_std_error(&jsrcerr);
-		jpeg_create_decompress(&src);	
-
-		/* Initialize the JPEG compression object
-		 * with default error handling. */
-		dst.err = jpeg_std_error(&jdsterr);
-		jpeg_create_compress(&dst);
-		
-		/* Specify data source for decompression */
-		jpeg_memory_src (&src, edata->data, edata->size);
-		
-		/* Specify data source for decompression */
-		jpeg_memory_dest (&dst, &out, &osize);
-
-		/* transform image */
-		jpegtran_internal(&src, &dst, transform, FALSE);
-
-		/* cleanup */
-		jpeg_destroy_decompress(&src);
-		jpeg_destroy_compress(&dst);
+		/* Allocate output buffer */
+		unsigned int osize = edata->size * 2;
+		unsigned char *out = malloc(osize);
+    	
+		/* Transform thumbnail */
+		if (jpegtran_thumbnail (edata->data, edata->size, 
+				(void**)&out, &osize, transform, FALSE) != 0) {
+			/* Discard thumbnail */
+			free(out);
+			free(edata->data);
+			edata->data = NULL;
+			edata->size = 0;
+			return;
+		}
 
-		/* replace thumbnail */
+		/* Replace thumbnail */
 		free(edata->data);
 		edata->data = out;
 		edata->size = osize;
@@ -259,17 +247,17 @@ update_exif_data(struct jpeg_decompress_
 	unsigned char *data = NULL;
 	unsigned int size;
    
-   if (src == NULL)
+	if (src == NULL)
 		return;
    
-   // Find exif data.
+	// Find exif data.
 	for (mark = src->marker_list; mark != NULL; mark = mark->next) {
 		if (mark->marker != JPEG_APP0 +1)
 			continue;
 		edata = exif_data_new_from_data(mark->data, mark->data_length);
 		break;
 	}
-   if (edata == NULL)
+	if (edata == NULL)
 		return;
 
 	// Adjust exif orientation (set to top-left)
@@ -296,9 +284,9 @@ update_exif_data(struct jpeg_decompress_
 
 static void
 jpegtran_internal (struct jpeg_decompress_struct *srcinfo,
-						struct jpeg_compress_struct *dstinfo,
-						JXFORM_CODE transformation,
-						gboolean trim)
+		struct jpeg_compress_struct *dstinfo,
+		JXFORM_CODE transformation,
+		gboolean trim)
 {
 	jpeg_transform_info            transformoption; 
 	jvirt_barray_ptr              *src_coef_arrays;
@@ -355,12 +343,80 @@ jpegtran_internal (struct jpeg_decompres
 }
 
 
+static int
+jpegtran_thumbnail (const void *idata, size_t isize,
+		void **odata, size_t *osize,
+		JXFORM_CODE transformation,
+		gboolean trim)
+{
+	struct jpeg_decompress_struct  srcinfo;
+	struct jpeg_compress_struct    dstinfo;
+	struct error_handler_data      jsrcerr, jdsterr;
+
+	/* Initialize the JPEG decompression object with default error 
+	 * handling. */
+	srcinfo.err = jpeg_std_error (&(jsrcerr.pub));
+	jsrcerr.pub.error_exit = fatal_error_handler;
+	jsrcerr.pub.output_message = output_message_handler;
+	jsrcerr.filename = NULL;
+	jsrcerr.error = NULL;
+
+	jpeg_create_decompress (&srcinfo);
+
+	/* Initialize the JPEG compression object with default error 
+	 * handling. */
+	dstinfo.err = jpeg_std_error (&(jdsterr.pub));
+	jdsterr.pub.error_exit = fatal_error_handler;
+	jdsterr.pub.output_message = output_message_handler;
+	jdsterr.filename = NULL;
+	jdsterr.error = NULL;
+
+	jpeg_create_compress (&dstinfo);
+	
+	dstinfo.err->trace_level = 0;
+	dstinfo.arith_code = FALSE;
+	dstinfo.optimize_coding = FALSE;
+
+	jsrcerr.pub.trace_level = jdsterr.pub.trace_level;
+	srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
+
+	/* Decompression error handler */
+	if (sigsetjmp (jsrcerr.setjmp_buffer, 1)) {
+		jpeg_destroy_compress (&dstinfo);
+		jpeg_destroy_decompress (&srcinfo);
+		return 1;
+	}
+
+	/* Compression error handler */
+	if (sigsetjmp (jdsterr.setjmp_buffer, 1)) {
+		jpeg_destroy_compress (&dstinfo);
+		jpeg_destroy_decompress (&srcinfo);
+		return 1;
+	}
+
+	/* Specify data source for decompression */
+	jpeg_memory_src (&srcinfo, idata, isize);
+	
+	/* Specify data destination for compression */
+	jpeg_memory_dest (&dstinfo, odata, osize);
+
+	/* Apply transformation */
+	jpegtran_internal(&srcinfo, &dstinfo, transformation, trim);
+
+	/* Release memory */
+	jpeg_destroy_compress (&dstinfo);
+	jpeg_destroy_decompress (&srcinfo);
+
+	return 0;
+}
+
+
 int
-jpegtran (char         *input_filename,
-	  char         *output_filename,
-	  JXFORM_CODE   transformation,
-	  gboolean	trim,
-	  GError      **error)
+jpegtran (const char *input_filename,
+		const char *output_filename,
+		JXFORM_CODE transformation,
+		gboolean trim,
+		GError **error)
 {
 	struct jpeg_decompress_struct  srcinfo;
 	struct jpeg_compress_struct    dstinfo;
@@ -407,17 +463,17 @@ jpegtran (char         *input_filename,
 	jsrcerr.pub.trace_level = jdsterr.pub.trace_level;
 	srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
 
+	/* Decompression error handler */
 	if (sigsetjmp (jsrcerr.setjmp_buffer, 1)) {
-		/* Release memory and close files */
 		jpeg_destroy_compress (&dstinfo);
 		jpeg_destroy_decompress (&srcinfo);
 		fclose (input_file);
 		fclose (output_file);
 		return 1;
 	}
-
+	
+	/* Compression error handler */
 	if (sigsetjmp (jdsterr.setjmp_buffer, 1)) {
-		/* Release memory and close files */
 		jpeg_destroy_compress (&dstinfo);
 		jpeg_destroy_decompress (&srcinfo);
 		fclose (input_file);
Index: libgthumb/jpegutils/jpegtran.h
===================================================================
RCS file: /cvs/gnome/gthumb/libgthumb/jpegutils/jpegtran.h,v
retrieving revision 1.3
diff -u -p -r1.3 jpegtran.h
--- libgthumb/jpegutils/jpegtran.h	19 Oct 2006 17:42:32 -0000	1.3
+++ libgthumb/jpegutils/jpegtran.h	23 Nov 2006 14:14:50 -0000
@@ -29,10 +29,10 @@
 #include "transupp.h"
 
 
-int  jpegtran (char         *input_filename,
-	       char         *output_filename,
-	       JXFORM_CODE   transformation,
-	       gboolean	     trim,
-	       GError      **error);
+int jpegtran (const char *input_filename,
+		const char *output_filename,
+		JXFORM_CODE transformation,
+		gboolean trim,
+		GError **error);
 
 #endif /* JPEGTRAN_H */
