Blame gtkmm-osx/jpeg-6b/jdatadst.c

darco 56a656
/*
darco 56a656
 * jdatadst.c
darco 56a656
 *
darco 56a656
 * Copyright (C) 1994-1996, Thomas G. Lane.
darco 56a656
 * This file is part of the Independent JPEG Group's software.
darco 56a656
 * For conditions of distribution and use, see the accompanying README file.
darco 56a656
 *
darco 56a656
 * This file contains compression data destination routines for the case of
darco 56a656
 * emitting JPEG data to a file (or any stdio stream).  While these routines
darco 56a656
 * are sufficient for most applications, some will want to use a different
darco 56a656
 * destination manager.
darco 56a656
 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
darco 56a656
 * JOCTETs into 8-bit-wide elements on external storage.  If char is wider
darco 56a656
 * than 8 bits on your machine, you may need to do some tweaking.
darco 56a656
 */
darco 56a656
darco 56a656
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
darco 56a656
#include "jinclude.h"
darco 56a656
#include "jpeglib.h"
darco 56a656
#include "jerror.h"
darco 56a656
darco 56a656
darco 56a656
/* Expanded data destination object for stdio output */
darco 56a656
darco 56a656
typedef struct {
darco 56a656
  struct jpeg_destination_mgr pub; /* public fields */
darco 56a656
darco 56a656
  FILE * outfile;		/* target stream */
darco 56a656
  JOCTET * buffer;		/* start of buffer */
darco 56a656
} my_destination_mgr;
darco 56a656
darco 56a656
typedef my_destination_mgr * my_dest_ptr;
darco 56a656
darco 56a656
#define OUTPUT_BUF_SIZE  4096	/* choose an efficiently fwrite'able size */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Initialize destination --- called by jpeg_start_compress
darco 56a656
 * before any data is actually written.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
init_destination (j_compress_ptr cinfo)
darco 56a656
{
darco 56a656
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
darco 56a656
darco 56a656
  /* Allocate the output buffer --- it will be released when done with image */
darco 56a656
  dest->buffer = (JOCTET *)
darco 56a656
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
darco 56a656
				  OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
darco 56a656
darco 56a656
  dest->pub.next_output_byte = dest->buffer;
darco 56a656
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Empty the output buffer --- called whenever buffer fills up.
darco 56a656
 *
darco 56a656
 * In typical applications, this should write the entire output buffer
darco 56a656
 * (ignoring the current state of next_output_byte & free_in_buffer),
darco 56a656
 * reset the pointer & count to the start of the buffer, and return TRUE
darco 56a656
 * indicating that the buffer has been dumped.
darco 56a656
 *
darco 56a656
 * In applications that need to be able to suspend compression due to output
darco 56a656
 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
darco 56a656
 * In this situation, the compressor will return to its caller (possibly with
darco 56a656
 * an indication that it has not accepted all the supplied scanlines).  The
darco 56a656
 * application should resume compression after it has made more room in the
darco 56a656
 * output buffer.  Note that there are substantial restrictions on the use of
darco 56a656
 * suspension --- see the documentation.
darco 56a656
 *
darco 56a656
 * When suspending, the compressor will back up to a convenient restart point
darco 56a656
 * (typically the start of the current MCU). next_output_byte & free_in_buffer
darco 56a656
 * indicate where the restart point will be if the current call returns FALSE.
darco 56a656
 * Data beyond this point will be regenerated after resumption, so do not
darco 56a656
 * write it out when emptying the buffer externally.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(boolean)
darco 56a656
empty_output_buffer (j_compress_ptr cinfo)
darco 56a656
{
darco 56a656
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
darco 56a656
darco 56a656
  if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
darco 56a656
      (size_t) OUTPUT_BUF_SIZE)
darco 56a656
    ERREXIT(cinfo, JERR_FILE_WRITE);
darco 56a656
darco 56a656
  dest->pub.next_output_byte = dest->buffer;
darco 56a656
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
darco 56a656
darco 56a656
  return TRUE;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Terminate destination --- called by jpeg_finish_compress
darco 56a656
 * after all data has been written.  Usually needs to flush buffer.
darco 56a656
 *
darco 56a656
 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
darco 56a656
 * application must deal with any cleanup that should happen even
darco 56a656
 * for error exit.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
term_destination (j_compress_ptr cinfo)
darco 56a656
{
darco 56a656
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
darco 56a656
  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
darco 56a656
darco 56a656
  /* Write any data remaining in the buffer */
darco 56a656
  if (datacount > 0) {
darco 56a656
    if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
darco 56a656
      ERREXIT(cinfo, JERR_FILE_WRITE);
darco 56a656
  }
darco 56a656
  fflush(dest->outfile);
darco 56a656
  /* Make sure we wrote the output file OK */
darco 56a656
  if (ferror(dest->outfile))
darco 56a656
    ERREXIT(cinfo, JERR_FILE_WRITE);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Prepare for output to a stdio stream.
darco 56a656
 * The caller must have already opened the stream, and is responsible
darco 56a656
 * for closing it after finishing compression.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
darco 56a656
{
darco 56a656
  my_dest_ptr dest;
darco 56a656
darco 56a656
  /* The destination object is made permanent so that multiple JPEG images
darco 56a656
   * can be written to the same file without re-executing jpeg_stdio_dest.
darco 56a656
   * This makes it dangerous to use this manager and a different destination
darco 56a656
   * manager serially with the same JPEG object, because their private object
darco 56a656
   * sizes may be different.  Caveat programmer.
darco 56a656
   */
darco 56a656
  if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
darco 56a656
    cinfo->dest = (struct jpeg_destination_mgr *)
darco 56a656
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
darco 56a656
				  SIZEOF(my_destination_mgr));
darco 56a656
  }
darco 56a656
darco 56a656
  dest = (my_dest_ptr) cinfo->dest;
darco 56a656
  dest->pub.init_destination = init_destination;
darco 56a656
  dest->pub.empty_output_buffer = empty_output_buffer;
darco 56a656
  dest->pub.term_destination = term_destination;
darco 56a656
  dest->outfile = outfile;
darco 56a656
}