Imported Upstream version 1.4.2
[platform/upstream/libjpeg-turbo.git] / jdatadst-tj.c
index a8bf240..1f6f3a5 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright (C) 1994-1996, Thomas G. Lane.
  * Modified 2009-2012 by Guido Vollbeding.
  * libjpeg-turbo Modifications:
- * Copyright (C) 2011, D. R. Commander.
+ * Copyright (C) 2011, 2014 D. R. Commander.
  * For conditions of distribution and use, see the accompanying README file.
  *
  * This file contains compression data destination routines for the case of
 #include "jpeglib.h"
 #include "jerror.h"
 
-#ifndef HAVE_STDLIB_H          /* <stdlib.h> should declare malloc(),free() */
-extern void * malloc JPP((size_t size));
-extern void free JPP((void *ptr));
+#ifndef HAVE_STDLIB_H           /* <stdlib.h> should declare malloc(),free() */
+extern void * malloc (size_t size);
+extern void free (void *ptr);
 #endif
 
 
-#define OUTPUT_BUF_SIZE  4096  /* choose an efficiently fwrite'able size */
+#define OUTPUT_BUF_SIZE  4096   /* choose an efficiently fwrite'able size */
 
 
 /* Expanded data destination object for memory output */
@@ -36,10 +36,10 @@ extern void free JPP((void *ptr));
 typedef struct {
   struct jpeg_destination_mgr pub; /* public fields */
 
-  unsigned char ** outbuffer;  /* target buffer */
+  unsigned char ** outbuffer;   /* target buffer */
   unsigned long * outsize;
-  unsigned char * newbuffer;   /* newly allocated buffer */
-  JOCTET * buffer;             /* start of buffer */
+  unsigned char * newbuffer;    /* newly allocated buffer */
+  JOCTET * buffer;              /* start of buffer */
   size_t bufsize;
   boolean alloc;
 } my_mem_destination_mgr;
@@ -147,29 +147,33 @@ term_mem_destination (j_compress_ptr cinfo)
 
 GLOBAL(void)
 jpeg_mem_dest_tj (j_compress_ptr cinfo,
-              unsigned char ** outbuffer, unsigned long * outsize,
-              boolean alloc)
+               unsigned char ** outbuffer, unsigned long * outsize,
+               boolean alloc)
 {
+  boolean reused = FALSE;
   my_mem_dest_ptr dest;
 
-  if (outbuffer == NULL || outsize == NULL)    /* sanity check */
+  if (outbuffer == NULL || outsize == NULL)     /* sanity check */
     ERREXIT(cinfo, JERR_BUFFER_SIZE);
 
   /* The destination object is made permanent so that multiple JPEG images
    * can be written to the same buffer without re-executing jpeg_mem_dest.
    */
-  if (cinfo->dest == NULL) {   /* first time for this JPEG object? */
+  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
     cinfo->dest = (struct jpeg_destination_mgr *)
       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
-                                 SIZEOF(my_mem_destination_mgr));
+                                  sizeof(my_mem_destination_mgr));
     dest = (my_mem_dest_ptr) cinfo->dest;
     dest->newbuffer = NULL;
+    dest->buffer = NULL;
   }
 
   dest = (my_mem_dest_ptr) cinfo->dest;
   dest->pub.init_destination = init_mem_destination;
   dest->pub.empty_output_buffer = empty_mem_output_buffer;
   dest->pub.term_destination = term_mem_destination;
+  if (dest->buffer == *outbuffer && *outbuffer != NULL && alloc)
+    reused = TRUE;
   dest->outbuffer = outbuffer;
   dest->outsize = outsize;
   dest->alloc = alloc;
@@ -186,5 +190,7 @@ jpeg_mem_dest_tj (j_compress_ptr cinfo,
   }
 
   dest->pub.next_output_byte = dest->buffer = *outbuffer;
-  dest->pub.free_in_buffer = dest->bufsize = *outsize;
+  if (!reused)
+    dest->bufsize = *outsize;
+  dest->pub.free_in_buffer = dest->bufsize;
 }