Blame jdatadst-tj.c

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