kusano 7d535a
/*
kusano 7d535a
 * jdatasrc.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2009-2011 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 decompression data source routines for the case of
kusano 7d535a
 * reading JPEG data from memory or from 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 source manager.
kusano 7d535a
 * IMPORTANT: we assume that fread() will correctly transcribe an array of
kusano 7d535a
 * JOCTETs from 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
kusano 7d535a
/* Expanded data source object for stdio input */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_source_mgr pub;	/* public fields */
kusano 7d535a
kusano 7d535a
  FILE * infile;		/* source stream */
kusano 7d535a
  JOCTET * buffer;		/* start of buffer */
kusano 7d535a
  boolean start_of_file;	/* have we gotten any data yet? */
kusano 7d535a
} my_source_mgr;
kusano 7d535a
kusano 7d535a
typedef my_source_mgr * my_src_ptr;
kusano 7d535a
kusano 7d535a
#define INPUT_BUF_SIZE  4096	/* choose an efficiently fread'able size */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize source --- called by jpeg_read_header
kusano 7d535a
 * before any data is actually read.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
init_source (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_src_ptr src = (my_src_ptr) cinfo->src;
kusano 7d535a
kusano 7d535a
  /* We reset the empty-input-file flag for each image,
kusano 7d535a
   * but we don't clear the input buffer.
kusano 7d535a
   * This is correct behavior for reading a series of images from one source.
kusano 7d535a
   */
kusano 7d535a
  src->start_of_file = TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
init_mem_source (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work necessary here */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Fill the input buffer --- called whenever buffer is emptied.
kusano 7d535a
 *
kusano 7d535a
 * In typical applications, this should read fresh data into the buffer
kusano 7d535a
 * (ignoring the current state of next_input_byte & bytes_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 reloaded.  It is not necessary to
kusano 7d535a
 * fill the buffer entirely, only to obtain at least one more byte.
kusano 7d535a
 *
kusano 7d535a
 * There is no such thing as an EOF return.  If the end of the file has been
kusano 7d535a
 * reached, the routine has a choice of ERREXIT() or inserting fake data into
kusano 7d535a
 * the buffer.  In most cases, generating a warning message and inserting a
kusano 7d535a
 * fake EOI marker is the best course of action --- this will allow the
kusano 7d535a
 * decompressor to output however much of the image is there.  However,
kusano 7d535a
 * the resulting error message is misleading if the real problem is an empty
kusano 7d535a
 * input file, so we handle that case specially.
kusano 7d535a
 *
kusano 7d535a
 * In applications that need to be able to suspend compression due to input
kusano 7d535a
 * not being available yet, a FALSE return indicates that no more data can be
kusano 7d535a
 * obtained right now, but more may be forthcoming later.  In this situation,
kusano 7d535a
 * the decompressor will return to its caller (with an indication of the
kusano 7d535a
 * number of scanlines it has read, if any).  The application should resume
kusano 7d535a
 * decompression after it has loaded more data into the input buffer.  Note
kusano 7d535a
 * that there are substantial restrictions on the use of suspension --- see
kusano 7d535a
 * the documentation.
kusano 7d535a
 *
kusano 7d535a
 * When suspending, the decompressor will back up to a convenient restart point
kusano 7d535a
 * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
kusano 7d535a
 * indicate where the restart point will be if the current call returns FALSE.
kusano 7d535a
 * Data beyond this point must be rescanned after resumption, so move it to
kusano 7d535a
 * the front of the buffer rather than discarding it.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
fill_input_buffer (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_src_ptr src = (my_src_ptr) cinfo->src;
kusano 7d535a
  size_t nbytes;
kusano 7d535a
kusano 7d535a
  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
kusano 7d535a
kusano 7d535a
  if (nbytes <= 0) {
kusano 7d535a
    if (src->start_of_file)	/* Treat empty input file as fatal error */
kusano 7d535a
      ERREXIT(cinfo, JERR_INPUT_EMPTY);
kusano 7d535a
    WARNMS(cinfo, JWRN_JPEG_EOF);
kusano 7d535a
    /* Insert a fake EOI marker */
kusano 7d535a
    src->buffer[0] = (JOCTET) 0xFF;
kusano 7d535a
    src->buffer[1] = (JOCTET) JPEG_EOI;
kusano 7d535a
    nbytes = 2;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  src->pub.next_input_byte = src->buffer;
kusano 7d535a
  src->pub.bytes_in_buffer = nbytes;
kusano 7d535a
  src->start_of_file = FALSE;
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
METHODDEF(boolean)
kusano 7d535a
fill_mem_input_buffer (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  static const JOCTET mybuffer[4] = {
kusano 7d535a
    (JOCTET) 0xFF, (JOCTET) JPEG_EOI, 0, 0
kusano 7d535a
  };
kusano 7d535a
kusano 7d535a
  /* The whole JPEG data is expected to reside in the supplied memory
kusano 7d535a
   * buffer, so any request for more data beyond the given buffer size
kusano 7d535a
   * is treated as an error.
kusano 7d535a
   */
kusano 7d535a
  WARNMS(cinfo, JWRN_JPEG_EOF);
kusano 7d535a
kusano 7d535a
  /* Insert a fake EOI marker */
kusano 7d535a
kusano 7d535a
  cinfo->src->next_input_byte = mybuffer;
kusano 7d535a
  cinfo->src->bytes_in_buffer = 2;
kusano 7d535a
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Skip data --- used to skip over a potentially large amount of
kusano 7d535a
 * uninteresting data (such as an APPn marker).
kusano 7d535a
 *
kusano 7d535a
 * Writers of suspendable-input applications must note that skip_input_data
kusano 7d535a
 * is not granted the right to give a suspension return.  If the skip extends
kusano 7d535a
 * beyond the data currently in the buffer, the buffer can be marked empty so
kusano 7d535a
 * that the next read will cause a fill_input_buffer call that can suspend.
kusano 7d535a
 * Arranging for additional bytes to be discarded before reloading the input
kusano 7d535a
 * buffer is the application writer's problem.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
kusano 7d535a
{
kusano 7d535a
  struct jpeg_source_mgr * src = cinfo->src;
kusano 7d535a
kusano 7d535a
  /* Just a dumb implementation for now.  Could use fseek() except
kusano 7d535a
   * it doesn't work on pipes.  Not clear that being smart is worth
kusano 7d535a
   * any trouble anyway --- large skips are infrequent.
kusano 7d535a
   */
kusano 7d535a
  if (num_bytes > 0) {
kusano 7d535a
    while (num_bytes > (long) src->bytes_in_buffer) {
kusano 7d535a
      num_bytes -= (long) src->bytes_in_buffer;
kusano 7d535a
      (void) (*src->fill_input_buffer) (cinfo);
kusano 7d535a
      /* note we assume that fill_input_buffer will never return FALSE,
kusano 7d535a
       * so suspension need not be handled.
kusano 7d535a
       */
kusano 7d535a
    }
kusano 7d535a
    src->next_input_byte += (size_t) num_bytes;
kusano 7d535a
    src->bytes_in_buffer -= (size_t) num_bytes;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * An additional method that can be provided by data source modules is the
kusano 7d535a
 * resync_to_restart method for error recovery in the presence of RST markers.
kusano 7d535a
 * For the moment, this source module just uses the default resync method
kusano 7d535a
 * provided by the JPEG library.  That method assumes that no backtracking
kusano 7d535a
 * is possible.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Terminate source --- called by jpeg_finish_decompress
kusano 7d535a
 * after all data has been read.  Often a no-op.
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_source (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work necessary here */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Prepare for input from a stdio stream.
kusano 7d535a
 * The caller must have already opened the stream, and is responsible
kusano 7d535a
 * for closing it after finishing decompression.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
kusano 7d535a
{
kusano 7d535a
  my_src_ptr src;
kusano 7d535a
kusano 7d535a
  /* The source object and input buffer are made permanent so that a series
kusano 7d535a
   * of JPEG images can be read from the same file by calling jpeg_stdio_src
kusano 7d535a
   * only before the first one.  (If we discarded the buffer at the end of
kusano 7d535a
   * one image, we'd likely lose the start of the next one.)
kusano 7d535a
   * This makes it unsafe to use this manager and a different source
kusano 7d535a
   * manager serially with the same JPEG object.  Caveat programmer.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
kusano 7d535a
    cinfo->src = (struct jpeg_source_mgr *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
kusano 7d535a
				  SIZEOF(my_source_mgr));
kusano 7d535a
    src = (my_src_ptr) cinfo->src;
kusano 7d535a
    src->buffer = (JOCTET *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
kusano 7d535a
				  INPUT_BUF_SIZE * SIZEOF(JOCTET));
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  src = (my_src_ptr) cinfo->src;
kusano 7d535a
  src->pub.init_source = init_source;
kusano 7d535a
  src->pub.fill_input_buffer = fill_input_buffer;
kusano 7d535a
  src->pub.skip_input_data = skip_input_data;
kusano 7d535a
  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
kusano 7d535a
  src->pub.term_source = term_source;
kusano 7d535a
  src->infile = infile;
kusano 7d535a
  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
kusano 7d535a
  src->pub.next_input_byte = NULL; /* until buffer loaded */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Prepare for input from a supplied memory buffer.
kusano 7d535a
 * The buffer must contain the whole JPEG data.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_mem_src (j_decompress_ptr cinfo,
kusano 7d535a
	      unsigned char * inbuffer, unsigned long insize)
kusano 7d535a
{
kusano 7d535a
  struct jpeg_source_mgr * src;
kusano 7d535a
kusano 7d535a
  if (inbuffer == NULL || insize == 0)	/* Treat empty input as fatal error */
kusano 7d535a
    ERREXIT(cinfo, JERR_INPUT_EMPTY);
kusano 7d535a
kusano 7d535a
  /* The source object is made permanent so that a series of JPEG images
kusano 7d535a
   * can be read from the same buffer by calling jpeg_mem_src only before
kusano 7d535a
   * the first one.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
kusano 7d535a
    cinfo->src = (struct jpeg_source_mgr *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
kusano 7d535a
				  SIZEOF(struct jpeg_source_mgr));
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  src = cinfo->src;
kusano 7d535a
  src->init_source = init_mem_source;
kusano 7d535a
  src->fill_input_buffer = fill_mem_input_buffer;
kusano 7d535a
  src->skip_input_data = skip_input_data;
kusano 7d535a
  src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
kusano 7d535a
  src->term_source = term_source;
kusano 7d535a
  src->bytes_in_buffer = (size_t) insize;
kusano 7d535a
  src->next_input_byte = (JOCTET *) inbuffer;
kusano 7d535a
}