| Index: jdatadst-tj.c
 | 
| diff --git a/jdatadst-tj.c b/jdatadst-tj.c
 | 
| index a8bf2401abe345c49534a51a24218e686c7cd6a8..5d4260a9d184078035b7f132e21db58b1f18f2a9 100644
 | 
| --- a/jdatadst-tj.c
 | 
| +++ b/jdatadst-tj.c
 | 
| @@ -5,8 +5,9 @@
 | 
|   * Copyright (C) 1994-1996, Thomas G. Lane.
 | 
|   * Modified 2009-2012 by Guido Vollbeding.
 | 
|   * libjpeg-turbo Modifications:
 | 
| - * Copyright (C) 2011, D. R. Commander.
 | 
| - * For conditions of distribution and use, see the accompanying README file.
 | 
| + * Copyright (C) 2011, 2014 D. R. Commander.
 | 
| + * For conditions of distribution and use, see the accompanying README.ijg
 | 
| + * file.
 | 
|   *
 | 
|   * This file contains compression data destination routines for the case of
 | 
|   * emitting JPEG data to memory or to a file (or any stdio stream).
 | 
| @@ -22,13 +23,13 @@
 | 
|  #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,15 +37,15 @@ extern void free JPP((void *ptr));
 | 
|  typedef struct {
 | 
|    struct jpeg_destination_mgr pub; /* public fields */
 | 
|  
 | 
| -  unsigned char ** outbuffer;	/* target buffer */
 | 
| -  unsigned long * outsize;
 | 
| -  unsigned char * newbuffer;	/* newly allocated buffer */
 | 
| -  JOCTET * buffer;		/* start of buffer */
 | 
| +  unsigned char **outbuffer;    /* target buffer */
 | 
| +  unsigned long *outsize;
 | 
| +  unsigned char *newbuffer;     /* newly allocated buffer */
 | 
| +  JOCTET *buffer;               /* start of buffer */
 | 
|    size_t bufsize;
 | 
|    boolean alloc;
 | 
|  } my_mem_destination_mgr;
 | 
|  
 | 
| -typedef my_mem_destination_mgr * my_mem_dest_ptr;
 | 
| +typedef my_mem_destination_mgr *my_mem_dest_ptr;
 | 
|  
 | 
|  
 | 
|  /*
 | 
| @@ -86,7 +87,7 @@ METHODDEF(boolean)
 | 
|  empty_mem_output_buffer (j_compress_ptr cinfo)
 | 
|  {
 | 
|    size_t nextsize;
 | 
| -  JOCTET * nextbuffer;
 | 
| +  JOCTET *nextbuffer;
 | 
|    my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
 | 
|  
 | 
|    if (!dest->alloc) ERREXIT(cinfo, JERR_BUFFER_SIZE);
 | 
| @@ -147,29 +148,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 +191,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;
 | 
|  }
 | 
| 
 |