kusano 7d535a
/*
kusano 7d535a
 * jdatadst.c
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2009-2012 by Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2013, 2016, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains compression data destination routines for the case of
kusano 7d535a
 * emitting JPEG data to memory or to a file (or any stdio stream).
kusano 7d535a
 * While these routines are sufficient for most applications,
kusano 7d535a
 * some will want to use a different destination manager.
kusano 7d535a
 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
kusano 7d535a
 * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
kusano 7d535a
 * than 8 bits on your machine, you may need to do some tweaking.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
#include "jerror.h"
kusano 7d535a
shun-iwasawa 82a8f5
#ifndef HAVE_STDLIB_H           /* <stdlib.h> should declare malloc(),free() */</stdlib.h>
shun-iwasawa 82a8f5
extern void *malloc(size_t size);
shun-iwasawa 82a8f5
extern void free(void *ptr);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Expanded data destination object for stdio output */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_destination_mgr pub; /* public fields */
kusano 7d535a
shun-iwasawa 82a8f5
  FILE *outfile;                /* target stream */
shun-iwasawa 82a8f5
  JOCTET *buffer;               /* start of buffer */
kusano 7d535a
} my_destination_mgr;
kusano 7d535a
shun-iwasawa 82a8f5
typedef my_destination_mgr *my_dest_ptr;
kusano 7d535a
shun-iwasawa 82a8f5
#define OUTPUT_BUF_SIZE  4096   /* choose an efficiently fwrite'able size */
kusano 7d535a
kusano 7d535a
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
kusano 7d535a
/* Expanded data destination object for memory output */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_destination_mgr pub; /* public fields */
kusano 7d535a
shun-iwasawa 82a8f5
  unsigned char **outbuffer;    /* target buffer */
shun-iwasawa 82a8f5
  unsigned long *outsize;
shun-iwasawa 82a8f5
  unsigned char *newbuffer;     /* newly allocated buffer */
shun-iwasawa 82a8f5
  JOCTET *buffer;               /* start of buffer */
kusano 7d535a
  size_t bufsize;
kusano 7d535a
} my_mem_destination_mgr;
kusano 7d535a
shun-iwasawa 82a8f5
typedef my_mem_destination_mgr *my_mem_dest_ptr;
shun-iwasawa 82a8f5
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize destination --- called by jpeg_start_compress
kusano 7d535a
 * before any data is actually written.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
init_destination(j_compress_ptr cinfo)
kusano 7d535a
{
shun-iwasawa 82a8f5
  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
kusano 7d535a
kusano 7d535a
  /* Allocate the output buffer --- it will be released when done with image */
kusano 7d535a
  dest->buffer = (JOCTET *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                OUTPUT_BUF_SIZE * sizeof(JOCTET));
kusano 7d535a
kusano 7d535a
  dest->pub.next_output_byte = dest->buffer;
kusano 7d535a
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
kusano 7d535a
}
kusano 7d535a
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
init_mem_destination(j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work necessary here */
kusano 7d535a
}
shun-iwasawa 82a8f5
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Empty the output buffer --- called whenever buffer fills up.
kusano 7d535a
 *
kusano 7d535a
 * In typical applications, this should write the entire output buffer
kusano 7d535a
 * (ignoring the current state of next_output_byte & free_in_buffer),
kusano 7d535a
 * reset the pointer & count to the start of the buffer, and return TRUE
kusano 7d535a
 * indicating that the buffer has been dumped.
kusano 7d535a
 *
kusano 7d535a
 * In applications that need to be able to suspend compression due to output
kusano 7d535a
 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
kusano 7d535a
 * In this situation, the compressor will return to its caller (possibly with
kusano 7d535a
 * an indication that it has not accepted all the supplied scanlines).  The
kusano 7d535a
 * application should resume compression after it has made more room in the
kusano 7d535a
 * output buffer.  Note that there are substantial restrictions on the use of
kusano 7d535a
 * suspension --- see the documentation.
kusano 7d535a
 *
kusano 7d535a
 * When suspending, the compressor will back up to a convenient restart point
kusano 7d535a
 * (typically the start of the current MCU). next_output_byte & free_in_buffer
kusano 7d535a
 * indicate where the restart point will be if the current call returns FALSE.
kusano 7d535a
 * Data beyond this point will be regenerated after resumption, so do not
kusano 7d535a
 * write it out when emptying the buffer externally.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
shun-iwasawa 82a8f5
empty_output_buffer(j_compress_ptr cinfo)
kusano 7d535a
{
shun-iwasawa 82a8f5
  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
kusano 7d535a
kusano 7d535a
  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
shun-iwasawa 82a8f5
      (size_t)OUTPUT_BUF_SIZE)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
kusano 7d535a
  dest->pub.next_output_byte = dest->buffer;
kusano 7d535a
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
kusano 7d535a
METHODDEF(boolean)
shun-iwasawa 82a8f5
empty_mem_output_buffer(j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  size_t nextsize;
shun-iwasawa 82a8f5
  JOCTET *nextbuffer;
shun-iwasawa 82a8f5
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
kusano 7d535a
kusano 7d535a
  /* Try to allocate new buffer with double size */
kusano 7d535a
  nextsize = dest->bufsize * 2;
shun-iwasawa 82a8f5
  nextbuffer = (JOCTET *)malloc(nextsize);
kusano 7d535a
kusano 7d535a
  if (nextbuffer == NULL)
kusano 7d535a
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
kusano 7d535a
kusano 7d535a
  MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
kusano 7d535a
shun-iwasawa 82a8f5
  free(dest->newbuffer);
kusano 7d535a
kusano 7d535a
  dest->newbuffer = nextbuffer;
kusano 7d535a
kusano 7d535a
  dest->pub.next_output_byte = nextbuffer + dest->bufsize;
kusano 7d535a
  dest->pub.free_in_buffer = dest->bufsize;
kusano 7d535a
kusano 7d535a
  dest->buffer = nextbuffer;
kusano 7d535a
  dest->bufsize = nextsize;
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
shun-iwasawa 82a8f5
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Terminate destination --- called by jpeg_finish_compress
kusano 7d535a
 * after all data has been written.  Usually needs to flush buffer.
kusano 7d535a
 *
kusano 7d535a
 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
kusano 7d535a
 * application must deal with any cleanup that should happen even
kusano 7d535a
 * for error exit.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
term_destination(j_compress_ptr cinfo)
kusano 7d535a
{
shun-iwasawa 82a8f5
  my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
kusano 7d535a
  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
kusano 7d535a
kusano 7d535a
  /* Write any data remaining in the buffer */
kusano 7d535a
  if (datacount > 0) {
kusano 7d535a
    if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
kusano 7d535a
      ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
  }
kusano 7d535a
  fflush(dest->outfile);
kusano 7d535a
  /* Make sure we wrote the output file OK */
kusano 7d535a
  if (ferror(dest->outfile))
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
kusano 7d535a
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
term_mem_destination(j_compress_ptr cinfo)
kusano 7d535a
{
shun-iwasawa 82a8f5
  my_mem_dest_ptr dest = (my_mem_dest_ptr)cinfo->dest;
kusano 7d535a
kusano 7d535a
  *dest->outbuffer = dest->buffer;
shun-iwasawa 82a8f5
  *dest->outsize = (unsigned long)(dest->bufsize - dest->pub.free_in_buffer);
kusano 7d535a
}
shun-iwasawa 82a8f5
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Prepare for output to a stdio stream.
kusano 7d535a
 * The caller must have already opened the stream, and is responsible
kusano 7d535a
 * for closing it after finishing compression.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
shun-iwasawa 82a8f5
jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
kusano 7d535a
{
kusano 7d535a
  my_dest_ptr dest;
kusano 7d535a
kusano 7d535a
  /* The destination object is made permanent so that multiple JPEG images
kusano 7d535a
   * can be written to the same file without re-executing jpeg_stdio_dest.
kusano 7d535a
   */
shun-iwasawa 82a8f5
  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
kusano 7d535a
    cinfo->dest = (struct jpeg_destination_mgr *)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
shun-iwasawa 82a8f5
                                  sizeof(my_destination_mgr));
shun-iwasawa 82a8f5
  } else if (cinfo->dest->init_destination != init_destination) {
shun-iwasawa 82a8f5
    /* It is unsafe to reuse the existing destination manager unless it was
shun-iwasawa 82a8f5
     * created by this function.  Otherwise, there is no guarantee that the
shun-iwasawa 82a8f5
     * opaque structure is the right size.  Note that we could just create a
shun-iwasawa 82a8f5
     * new structure, but the old structure would not be freed until
shun-iwasawa 82a8f5
     * jpeg_destroy_compress() was called.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
kusano 7d535a
  }
kusano 7d535a
shun-iwasawa 82a8f5
  dest = (my_dest_ptr)cinfo->dest;
kusano 7d535a
  dest->pub.init_destination = init_destination;
kusano 7d535a
  dest->pub.empty_output_buffer = empty_output_buffer;
kusano 7d535a
  dest->pub.term_destination = term_destination;
kusano 7d535a
  dest->outfile = outfile;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
shun-iwasawa 82a8f5
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
kusano 7d535a
/*
kusano 7d535a
 * Prepare for output to a memory buffer.
kusano 7d535a
 * The caller may supply an own initial buffer with appropriate size.
kusano 7d535a
 * Otherwise, or when the actual data output exceeds the given size,
kusano 7d535a
 * the library adapts the buffer size as necessary.
kusano 7d535a
 * The standard library functions malloc/free are used for allocating
kusano 7d535a
 * larger memory, so the buffer is available to the application after
kusano 7d535a
 * finishing compression, and then the application is responsible for
kusano 7d535a
 * freeing the requested memory.
kusano 7d535a
 * Note:  An initial buffer supplied by the caller is expected to be
kusano 7d535a
 * managed by the application.  The library does not free such buffer
kusano 7d535a
 * when allocating a larger buffer.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
shun-iwasawa 82a8f5
jpeg_mem_dest(j_compress_ptr cinfo, unsigned char **outbuffer,
shun-iwasawa 82a8f5
              unsigned long *outsize)
kusano 7d535a
{
kusano 7d535a
  my_mem_dest_ptr dest;
kusano 7d535a
shun-iwasawa 82a8f5
  if (outbuffer == NULL || outsize == NULL)     /* sanity check */
kusano 7d535a
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
kusano 7d535a
kusano 7d535a
  /* The destination object is made permanent so that multiple JPEG images
kusano 7d535a
   * can be written to the same buffer without re-executing jpeg_mem_dest.
kusano 7d535a
   */
shun-iwasawa 82a8f5
  if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
kusano 7d535a
    cinfo->dest = (struct jpeg_destination_mgr *)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
shun-iwasawa 82a8f5
                                  sizeof(my_mem_destination_mgr));
shun-iwasawa 82a8f5
  } else if (cinfo->dest->init_destination != init_mem_destination) {
shun-iwasawa 82a8f5
    /* It is unsafe to reuse the existing destination manager unless it was
shun-iwasawa 82a8f5
     * created by this function.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
kusano 7d535a
  }
kusano 7d535a
shun-iwasawa 82a8f5
  dest = (my_mem_dest_ptr)cinfo->dest;
kusano 7d535a
  dest->pub.init_destination = init_mem_destination;
kusano 7d535a
  dest->pub.empty_output_buffer = empty_mem_output_buffer;
kusano 7d535a
  dest->pub.term_destination = term_mem_destination;
kusano 7d535a
  dest->outbuffer = outbuffer;
kusano 7d535a
  dest->outsize = outsize;
kusano 7d535a
  dest->newbuffer = NULL;
kusano 7d535a
kusano 7d535a
  if (*outbuffer == NULL || *outsize == 0) {
kusano 7d535a
    /* Allocate initial buffer */
shun-iwasawa 82a8f5
    dest->newbuffer = *outbuffer = (unsigned char *)malloc(OUTPUT_BUF_SIZE);
kusano 7d535a
    if (dest->newbuffer == NULL)
kusano 7d535a
      ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
kusano 7d535a
    *outsize = OUTPUT_BUF_SIZE;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  dest->pub.next_output_byte = dest->buffer = *outbuffer;
kusano 7d535a
  dest->pub.free_in_buffer = dest->bufsize = *outsize;
kusano 7d535a
}
shun-iwasawa 82a8f5
#endif