kusano 7d535a
/*
kusano 7d535a
 * jdatadst.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2009-2012 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README 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
kusano 7d535a
#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc(),free() */</stdlib.h>
kusano 7d535a
extern void * malloc JPP((size_t size));
kusano 7d535a
extern void free JPP((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
kusano 7d535a
  FILE * outfile;		/* target stream */
kusano 7d535a
  JOCTET * buffer;		/* start of buffer */
kusano 7d535a
} my_destination_mgr;
kusano 7d535a
kusano 7d535a
typedef my_destination_mgr * my_dest_ptr;
kusano 7d535a
kusano 7d535a
#define OUTPUT_BUF_SIZE  4096	/* choose an efficiently fwrite'able size */
kusano 7d535a
kusano 7d535a
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
kusano 7d535a
  unsigned char ** outbuffer;	/* target buffer */
kusano 7d535a
  unsigned long * outsize;
kusano 7d535a
  unsigned char * newbuffer;	/* newly allocated buffer */
kusano 7d535a
  JOCTET * buffer;		/* start of buffer */
kusano 7d535a
  size_t bufsize;
kusano 7d535a
} my_mem_destination_mgr;
kusano 7d535a
kusano 7d535a
typedef my_mem_destination_mgr * my_mem_dest_ptr;
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)
kusano 7d535a
init_destination (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  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 *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  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
kusano 7d535a
METHODDEF(void)
kusano 7d535a
init_mem_destination (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work necessary here */
kusano 7d535a
}
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)
kusano 7d535a
empty_output_buffer (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
kusano 7d535a
kusano 7d535a
  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
kusano 7d535a
      (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
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
empty_mem_output_buffer (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  size_t nextsize;
kusano 7d535a
  JOCTET * nextbuffer;
kusano 7d535a
  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;
kusano 7d535a
  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
kusano 7d535a
  if (dest->newbuffer != NULL)
kusano 7d535a
    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
}
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)
kusano 7d535a
term_destination (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  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
kusano 7d535a
METHODDEF(void)
kusano 7d535a
term_mem_destination (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
kusano 7d535a
kusano 7d535a
  *dest->outbuffer = dest->buffer;
kusano 7d535a
  *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
kusano 7d535a
}
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)
kusano 7d535a
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
   * This makes it dangerous to use this manager and a different destination
kusano 7d535a
   * manager serially with the same JPEG object, because their private object
kusano 7d535a
   * sizes may be different.  Caveat programmer.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
kusano 7d535a
    cinfo->dest = (struct jpeg_destination_mgr *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
kusano 7d535a
				  SIZEOF(my_destination_mgr));
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  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
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)
kusano 7d535a
jpeg_mem_dest (j_compress_ptr cinfo,
kusano 7d535a
	       unsigned char ** outbuffer, unsigned long * outsize)
kusano 7d535a
{
kusano 7d535a
  my_mem_dest_ptr dest;
kusano 7d535a
kusano 7d535a
  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
   */
kusano 7d535a
  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
kusano 7d535a
    cinfo->dest = (struct jpeg_destination_mgr *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
kusano 7d535a
				  SIZEOF(my_mem_destination_mgr));
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  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 */
kusano 7d535a
    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
}