shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jdatasrc-tj.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1994-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * Modified 2009-2011 by Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2011, 2016, 2019, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains decompression data source routines for the case of
shun-iwasawa 82a8f5
 * reading JPEG data from memory or from a file (or any stdio stream).
shun-iwasawa 82a8f5
 * While these routines are sufficient for most applications,
shun-iwasawa 82a8f5
 * some will want to use a different source manager.
shun-iwasawa 82a8f5
 * IMPORTANT: we assume that fread() will correctly transcribe an array of
shun-iwasawa 82a8f5
 * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
shun-iwasawa 82a8f5
 * than 8 bits on your machine, you may need to do some tweaking.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jpeglib.h"
shun-iwasawa 82a8f5
#include "jerror.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
void jpeg_mem_src_tj(j_decompress_ptr cinfo, const unsigned char *inbuffer,
shun-iwasawa 82a8f5
                     unsigned long insize);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize source --- called by jpeg_read_header
shun-iwasawa 82a8f5
 * before any data is actually read.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
init_mem_source(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* no work necessary here */
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Fill the input buffer --- called whenever buffer is emptied.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * In typical applications, this should read fresh data into the buffer
shun-iwasawa 82a8f5
 * (ignoring the current state of next_input_byte & bytes_in_buffer),
shun-iwasawa 82a8f5
 * reset the pointer & count to the start of the buffer, and return TRUE
shun-iwasawa 82a8f5
 * indicating that the buffer has been reloaded.  It is not necessary to
shun-iwasawa 82a8f5
 * fill the buffer entirely, only to obtain at least one more byte.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * There is no such thing as an EOF return.  If the end of the file has been
shun-iwasawa 82a8f5
 * reached, the routine has a choice of ERREXIT() or inserting fake data into
shun-iwasawa 82a8f5
 * the buffer.  In most cases, generating a warning message and inserting a
shun-iwasawa 82a8f5
 * fake EOI marker is the best course of action --- this will allow the
shun-iwasawa 82a8f5
 * decompressor to output however much of the image is there.  However,
shun-iwasawa 82a8f5
 * the resulting error message is misleading if the real problem is an empty
shun-iwasawa 82a8f5
 * input file, so we handle that case specially.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * In applications that need to be able to suspend compression due to input
shun-iwasawa 82a8f5
 * not being available yet, a FALSE return indicates that no more data can be
shun-iwasawa 82a8f5
 * obtained right now, but more may be forthcoming later.  In this situation,
shun-iwasawa 82a8f5
 * the decompressor will return to its caller (with an indication of the
shun-iwasawa 82a8f5
 * number of scanlines it has read, if any).  The application should resume
shun-iwasawa 82a8f5
 * decompression after it has loaded more data into the input buffer.  Note
shun-iwasawa 82a8f5
 * that there are substantial restrictions on the use of suspension --- see
shun-iwasawa 82a8f5
 * the documentation.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * When suspending, the decompressor will back up to a convenient restart point
shun-iwasawa 82a8f5
 * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
shun-iwasawa 82a8f5
 * indicate where the restart point will be if the current call returns FALSE.
shun-iwasawa 82a8f5
 * Data beyond this point must be rescanned after resumption, so move it to
shun-iwasawa 82a8f5
 * the front of the buffer rather than discarding it.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(boolean)
shun-iwasawa 82a8f5
fill_mem_input_buffer(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  static const JOCTET mybuffer[4] = {
shun-iwasawa 82a8f5
    (JOCTET)0xFF, (JOCTET)JPEG_EOI, 0, 0
shun-iwasawa 82a8f5
  };
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* The whole JPEG data is expected to reside in the supplied memory
shun-iwasawa 82a8f5
   * buffer, so any request for more data beyond the given buffer size
shun-iwasawa 82a8f5
   * is treated as an error.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  WARNMS(cinfo, JWRN_JPEG_EOF);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Insert a fake EOI marker */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->src->next_input_byte = mybuffer;
shun-iwasawa 82a8f5
  cinfo->src->bytes_in_buffer = 2;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Skip data --- used to skip over a potentially large amount of
shun-iwasawa 82a8f5
 * uninteresting data (such as an APPn marker).
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Writers of suspendable-input applications must note that skip_input_data
shun-iwasawa 82a8f5
 * is not granted the right to give a suspension return.  If the skip extends
shun-iwasawa 82a8f5
 * beyond the data currently in the buffer, the buffer can be marked empty so
shun-iwasawa 82a8f5
 * that the next read will cause a fill_input_buffer call that can suspend.
shun-iwasawa 82a8f5
 * Arranging for additional bytes to be discarded before reloading the input
shun-iwasawa 82a8f5
 * buffer is the application writer's problem.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
skip_input_data(j_decompress_ptr cinfo, long num_bytes)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  struct jpeg_source_mgr *src = cinfo->src;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Just a dumb implementation for now.  Could use fseek() except
shun-iwasawa 82a8f5
   * it doesn't work on pipes.  Not clear that being smart is worth
shun-iwasawa 82a8f5
   * any trouble anyway --- large skips are infrequent.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (num_bytes > 0) {
shun-iwasawa 82a8f5
    while (num_bytes > (long)src->bytes_in_buffer) {
shun-iwasawa 82a8f5
      num_bytes -= (long)src->bytes_in_buffer;
shun-iwasawa 82a8f5
      (void)(*src->fill_input_buffer) (cinfo);
shun-iwasawa 82a8f5
      /* note we assume that fill_input_buffer will never return FALSE,
shun-iwasawa 82a8f5
       * so suspension need not be handled.
shun-iwasawa 82a8f5
       */
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    src->next_input_byte += (size_t)num_bytes;
shun-iwasawa 82a8f5
    src->bytes_in_buffer -= (size_t)num_bytes;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * An additional method that can be provided by data source modules is the
shun-iwasawa 82a8f5
 * resync_to_restart method for error recovery in the presence of RST markers.
shun-iwasawa 82a8f5
 * For the moment, this source module just uses the default resync method
shun-iwasawa 82a8f5
 * provided by the JPEG library.  That method assumes that no backtracking
shun-iwasawa 82a8f5
 * is possible.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Terminate source --- called by jpeg_finish_decompress
shun-iwasawa 82a8f5
 * after all data has been read.  Often a no-op.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
shun-iwasawa 82a8f5
 * application must deal with any cleanup that should happen even
shun-iwasawa 82a8f5
 * for error exit.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
term_source(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* no work necessary here */
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Prepare for input from a supplied memory buffer.
shun-iwasawa 82a8f5
 * The buffer must contain the whole JPEG data.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jpeg_mem_src_tj(j_decompress_ptr cinfo, const unsigned char *inbuffer,
shun-iwasawa 82a8f5
                unsigned long insize)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  struct jpeg_source_mgr *src;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (inbuffer == NULL || insize == 0)  /* Treat empty input as fatal error */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EMPTY);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* The source object is made permanent so that a series of JPEG images
shun-iwasawa 82a8f5
   * can be read from the same buffer by calling jpeg_mem_src only before
shun-iwasawa 82a8f5
   * the first one.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (cinfo->src == NULL) {     /* first time for this JPEG object? */
shun-iwasawa 82a8f5
    cinfo->src = (struct jpeg_source_mgr *)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
shun-iwasawa 82a8f5
                                  sizeof(struct jpeg_source_mgr));
shun-iwasawa 82a8f5
  } else if (cinfo->src->init_source != init_mem_source) {
shun-iwasawa 82a8f5
    /* It is unsafe to reuse the existing source manager unless it was created
shun-iwasawa 82a8f5
     * by this function.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  src = cinfo->src;
shun-iwasawa 82a8f5
  src->init_source = init_mem_source;
shun-iwasawa 82a8f5
  src->fill_input_buffer = fill_mem_input_buffer;
shun-iwasawa 82a8f5
  src->skip_input_data = skip_input_data;
shun-iwasawa 82a8f5
  src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
shun-iwasawa 82a8f5
  src->term_source = term_source;
shun-iwasawa 82a8f5
  src->bytes_in_buffer = (size_t)insize;
shun-iwasawa 82a8f5
  src->next_input_byte = (const JOCTET *)inbuffer;
shun-iwasawa 82a8f5
}