kusano 7d535a
/*
kusano 7d535a
 * jdapimin.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1998, Thomas G. Lane.
kusano 7d535a
 * Modified 2009 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 application interface code for the decompression half
kusano 7d535a
 * of the JPEG library.  These are the "minimum" API routines that may be
kusano 7d535a
 * needed in either the normal full-decompression case or the
kusano 7d535a
 * transcoding-only case.
kusano 7d535a
 *
kusano 7d535a
 * Most of the routines intended to be called directly by an application
kusano 7d535a
 * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
kusano 7d535a
 * shared by compression and decompression, and jdtrans.c for the transcoding
kusano 7d535a
 * case.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialization of a JPEG decompression object.
kusano 7d535a
 * The error manager must already be set up (in case memory manager fails).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
kusano 7d535a
{
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  /* Guard against version mismatches between library and caller. */
kusano 7d535a
  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
kusano 7d535a
  if (version != JPEG_LIB_VERSION)
kusano 7d535a
    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
kusano 7d535a
  if (structsize != SIZEOF(struct jpeg_decompress_struct))
kusano 7d535a
    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
kusano 7d535a
	     (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
kusano 7d535a
kusano 7d535a
  /* For debugging purposes, we zero the whole master structure.
kusano 7d535a
   * But the application has already set the err pointer, and may have set
kusano 7d535a
   * client_data, so we have to save and restore those fields.
kusano 7d535a
   * Note: if application hasn't set client_data, tools like Purify may
kusano 7d535a
   * complain here.
kusano 7d535a
   */
kusano 7d535a
  {
kusano 7d535a
    struct jpeg_error_mgr * err = cinfo->err;
kusano 7d535a
    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
kusano 7d535a
    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
kusano 7d535a
    cinfo->err = err;
kusano 7d535a
    cinfo->client_data = client_data;
kusano 7d535a
  }
kusano 7d535a
  cinfo->is_decompressor = TRUE;
kusano 7d535a
kusano 7d535a
  /* Initialize a memory manager instance for this object */
kusano 7d535a
  jinit_memory_mgr((j_common_ptr) cinfo);
kusano 7d535a
kusano 7d535a
  /* Zero out pointers to permanent structures. */
kusano 7d535a
  cinfo->progress = NULL;
kusano 7d535a
  cinfo->src = NULL;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_QUANT_TBLS; i++)
kusano 7d535a
    cinfo->quant_tbl_ptrs[i] = NULL;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
kusano 7d535a
    cinfo->dc_huff_tbl_ptrs[i] = NULL;
kusano 7d535a
    cinfo->ac_huff_tbl_ptrs[i] = NULL;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Initialize marker processor so application can override methods
kusano 7d535a
   * for COM, APPn markers before calling jpeg_read_header.
kusano 7d535a
   */
kusano 7d535a
  cinfo->marker_list = NULL;
kusano 7d535a
  jinit_marker_reader(cinfo);
kusano 7d535a
kusano 7d535a
  /* And initialize the overall input controller. */
kusano 7d535a
  jinit_input_controller(cinfo);
kusano 7d535a
kusano 7d535a
  /* OK, I'm ready */
kusano 7d535a
  cinfo->global_state = DSTATE_START;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Destruction of a JPEG decompression object
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_destroy_decompress (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Abort processing of a JPEG decompression operation,
kusano 7d535a
 * but don't destroy the object itself.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_abort_decompress (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Set default decompression parameters.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
default_decompress_parms (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* Guess the input colorspace, and set output colorspace accordingly. */
kusano 7d535a
  /* (Wish JPEG committee had provided a real way to specify this...) */
kusano 7d535a
  /* Note application may override our guesses. */
kusano 7d535a
  switch (cinfo->num_components) {
kusano 7d535a
  case 1:
kusano 7d535a
    cinfo->jpeg_color_space = JCS_GRAYSCALE;
kusano 7d535a
    cinfo->out_color_space = JCS_GRAYSCALE;
kusano 7d535a
    break;
kusano 7d535a
    
kusano 7d535a
  case 3:
kusano 7d535a
    if (cinfo->saw_JFIF_marker) {
kusano 7d535a
      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
kusano 7d535a
    } else if (cinfo->saw_Adobe_marker) {
kusano 7d535a
      switch (cinfo->Adobe_transform) {
kusano 7d535a
      case 0:
kusano 7d535a
	cinfo->jpeg_color_space = JCS_RGB;
kusano 7d535a
	break;
kusano 7d535a
      case 1:
kusano 7d535a
	cinfo->jpeg_color_space = JCS_YCbCr;
kusano 7d535a
	break;
kusano 7d535a
      default:
kusano 7d535a
	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
kusano 7d535a
	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
    } else {
kusano 7d535a
      /* Saw no special markers, try to guess from the component IDs */
kusano 7d535a
      int cid0 = cinfo->comp_info[0].component_id;
kusano 7d535a
      int cid1 = cinfo->comp_info[1].component_id;
kusano 7d535a
      int cid2 = cinfo->comp_info[2].component_id;
kusano 7d535a
kusano 7d535a
      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
kusano 7d535a
	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
kusano 7d535a
      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
kusano 7d535a
	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
kusano 7d535a
      else {
kusano 7d535a
	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
kusano 7d535a
	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Always guess RGB is proper output colorspace. */
kusano 7d535a
    cinfo->out_color_space = JCS_RGB;
kusano 7d535a
    break;
kusano 7d535a
    
kusano 7d535a
  case 4:
kusano 7d535a
    if (cinfo->saw_Adobe_marker) {
kusano 7d535a
      switch (cinfo->Adobe_transform) {
kusano 7d535a
      case 0:
kusano 7d535a
	cinfo->jpeg_color_space = JCS_CMYK;
kusano 7d535a
	break;
kusano 7d535a
      case 2:
kusano 7d535a
	cinfo->jpeg_color_space = JCS_YCCK;
kusano 7d535a
	break;
kusano 7d535a
      default:
kusano 7d535a
	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
kusano 7d535a
	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
    } else {
kusano 7d535a
      /* No special markers, assume straight CMYK. */
kusano 7d535a
      cinfo->jpeg_color_space = JCS_CMYK;
kusano 7d535a
    }
kusano 7d535a
    cinfo->out_color_space = JCS_CMYK;
kusano 7d535a
    break;
kusano 7d535a
    
kusano 7d535a
  default:
kusano 7d535a
    cinfo->jpeg_color_space = JCS_UNKNOWN;
kusano 7d535a
    cinfo->out_color_space = JCS_UNKNOWN;
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Set defaults for other decompression parameters. */
kusano 7d535a
  cinfo->scale_num = cinfo->block_size;		/* 1:1 scaling */
kusano 7d535a
  cinfo->scale_denom = cinfo->block_size;
kusano 7d535a
  cinfo->output_gamma = 1.0;
kusano 7d535a
  cinfo->buffered_image = FALSE;
kusano 7d535a
  cinfo->raw_data_out = FALSE;
kusano 7d535a
  cinfo->dct_method = JDCT_DEFAULT;
kusano 7d535a
  cinfo->do_fancy_upsampling = TRUE;
kusano 7d535a
  cinfo->do_block_smoothing = TRUE;
kusano 7d535a
  cinfo->quantize_colors = FALSE;
kusano 7d535a
  /* We set these in case application only sets quantize_colors. */
kusano 7d535a
  cinfo->dither_mode = JDITHER_FS;
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
  cinfo->two_pass_quantize = TRUE;
kusano 7d535a
#else
kusano 7d535a
  cinfo->two_pass_quantize = FALSE;
kusano 7d535a
#endif
kusano 7d535a
  cinfo->desired_number_of_colors = 256;
kusano 7d535a
  cinfo->colormap = NULL;
kusano 7d535a
  /* Initialize for no mode change in buffered-image mode. */
kusano 7d535a
  cinfo->enable_1pass_quant = FALSE;
kusano 7d535a
  cinfo->enable_external_quant = FALSE;
kusano 7d535a
  cinfo->enable_2pass_quant = FALSE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Decompression startup: read start of JPEG datastream to see what's there.
kusano 7d535a
 * Need only initialize JPEG object and supply a data source before calling.
kusano 7d535a
 *
kusano 7d535a
 * This routine will read as far as the first SOS marker (ie, actual start of
kusano 7d535a
 * compressed data), and will save all tables and parameters in the JPEG
kusano 7d535a
 * object.  It will also initialize the decompression parameters to default
kusano 7d535a
 * values, and finally return JPEG_HEADER_OK.  On return, the application may
kusano 7d535a
 * adjust the decompression parameters and then call jpeg_start_decompress.
kusano 7d535a
 * (Or, if the application only wanted to determine the image parameters,
kusano 7d535a
 * the data need not be decompressed.  In that case, call jpeg_abort or
kusano 7d535a
 * jpeg_destroy to release any temporary space.)
kusano 7d535a
 * If an abbreviated (tables only) datastream is presented, the routine will
kusano 7d535a
 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
kusano 7d535a
 * re-use the JPEG object to read the abbreviated image datastream(s).
kusano 7d535a
 * It is unnecessary (but OK) to call jpeg_abort in this case.
kusano 7d535a
 * The JPEG_SUSPENDED return code only occurs if the data source module
kusano 7d535a
 * requests suspension of the decompressor.  In this case the application
kusano 7d535a
 * should load more source data and then re-call jpeg_read_header to resume
kusano 7d535a
 * processing.
kusano 7d535a
 * If a non-suspending data source is used and require_image is TRUE, then the
kusano 7d535a
 * return code need not be inspected since only JPEG_HEADER_OK is possible.
kusano 7d535a
 *
kusano 7d535a
 * This routine is now just a front end to jpeg_consume_input, with some
kusano 7d535a
 * extra error checking.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(int)
kusano 7d535a
jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
kusano 7d535a
{
kusano 7d535a
  int retcode;
kusano 7d535a
kusano 7d535a
  if (cinfo->global_state != DSTATE_START &&
kusano 7d535a
      cinfo->global_state != DSTATE_INHEADER)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
kusano 7d535a
  retcode = jpeg_consume_input(cinfo);
kusano 7d535a
kusano 7d535a
  switch (retcode) {
kusano 7d535a
  case JPEG_REACHED_SOS:
kusano 7d535a
    retcode = JPEG_HEADER_OK;
kusano 7d535a
    break;
kusano 7d535a
  case JPEG_REACHED_EOI:
kusano 7d535a
    if (require_image)		/* Complain if application wanted an image */
kusano 7d535a
      ERREXIT(cinfo, JERR_NO_IMAGE);
kusano 7d535a
    /* Reset to start state; it would be safer to require the application to
kusano 7d535a
     * call jpeg_abort, but we can't change it now for compatibility reasons.
kusano 7d535a
     * A side effect is to free any temporary memory (there shouldn't be any).
kusano 7d535a
     */
kusano 7d535a
    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
kusano 7d535a
    retcode = JPEG_HEADER_TABLES_ONLY;
kusano 7d535a
    break;
kusano 7d535a
  case JPEG_SUSPENDED:
kusano 7d535a
    /* no work */
kusano 7d535a
    break;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return retcode;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Consume data in advance of what the decompressor requires.
kusano 7d535a
 * This can be called at any time once the decompressor object has
kusano 7d535a
 * been created and a data source has been set up.
kusano 7d535a
 *
kusano 7d535a
 * This routine is essentially a state machine that handles a couple
kusano 7d535a
 * of critical state-transition actions, namely initial setup and
kusano 7d535a
 * transition from header scanning to ready-for-start_decompress.
kusano 7d535a
 * All the actual input is done via the input controller's consume_input
kusano 7d535a
 * method.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(int)
kusano 7d535a
jpeg_consume_input (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  int retcode = JPEG_SUSPENDED;
kusano 7d535a
kusano 7d535a
  /* NB: every possible DSTATE value should be listed in this switch */
kusano 7d535a
  switch (cinfo->global_state) {
kusano 7d535a
  case DSTATE_START:
kusano 7d535a
    /* Start-of-datastream actions: reset appropriate modules */
kusano 7d535a
    (*cinfo->inputctl->reset_input_controller) (cinfo);
kusano 7d535a
    /* Initialize application's data source module */
kusano 7d535a
    (*cinfo->src->init_source) (cinfo);
kusano 7d535a
    cinfo->global_state = DSTATE_INHEADER;
kusano 7d535a
    /*FALLTHROUGH*/
kusano 7d535a
  case DSTATE_INHEADER:
kusano 7d535a
    retcode = (*cinfo->inputctl->consume_input) (cinfo);
kusano 7d535a
    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
kusano 7d535a
      /* Set up default parameters based on header data */
kusano 7d535a
      default_decompress_parms(cinfo);
kusano 7d535a
      /* Set global state: ready for start_decompress */
kusano 7d535a
      cinfo->global_state = DSTATE_READY;
kusano 7d535a
    }
kusano 7d535a
    break;
kusano 7d535a
  case DSTATE_READY:
kusano 7d535a
    /* Can't advance past first SOS until start_decompress is called */
kusano 7d535a
    retcode = JPEG_REACHED_SOS;
kusano 7d535a
    break;
kusano 7d535a
  case DSTATE_PRELOAD:
kusano 7d535a
  case DSTATE_PRESCAN:
kusano 7d535a
  case DSTATE_SCANNING:
kusano 7d535a
  case DSTATE_RAW_OK:
kusano 7d535a
  case DSTATE_BUFIMAGE:
kusano 7d535a
  case DSTATE_BUFPOST:
kusano 7d535a
  case DSTATE_STOPPING:
kusano 7d535a
    retcode = (*cinfo->inputctl->consume_input) (cinfo);
kusano 7d535a
    break;
kusano 7d535a
  default:
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  }
kusano 7d535a
  return retcode;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Have we finished reading the input file?
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jpeg_input_complete (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* Check for valid jpeg object */
kusano 7d535a
  if (cinfo->global_state < DSTATE_START ||
kusano 7d535a
      cinfo->global_state > DSTATE_STOPPING)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  return cinfo->inputctl->eoi_reached;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Is there more than one scan?
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jpeg_has_multiple_scans (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* Only valid after jpeg_read_header completes */
kusano 7d535a
  if (cinfo->global_state < DSTATE_READY ||
kusano 7d535a
      cinfo->global_state > DSTATE_STOPPING)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  return cinfo->inputctl->has_multiple_scans;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish JPEG decompression.
kusano 7d535a
 *
kusano 7d535a
 * This will normally just verify the file trailer and release temp storage.
kusano 7d535a
 *
kusano 7d535a
 * Returns FALSE if suspended.  The return value need be inspected only if
kusano 7d535a
 * a suspending data source is used.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jpeg_finish_decompress (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  if ((cinfo->global_state == DSTATE_SCANNING ||
kusano 7d535a
       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
kusano 7d535a
    /* Terminate final pass of non-buffered mode */
kusano 7d535a
    if (cinfo->output_scanline < cinfo->output_height)
kusano 7d535a
      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
kusano 7d535a
    (*cinfo->master->finish_output_pass) (cinfo);
kusano 7d535a
    cinfo->global_state = DSTATE_STOPPING;
kusano 7d535a
  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
kusano 7d535a
    /* Finishing after a buffered-image operation */
kusano 7d535a
    cinfo->global_state = DSTATE_STOPPING;
kusano 7d535a
  } else if (cinfo->global_state != DSTATE_STOPPING) {
kusano 7d535a
    /* STOPPING = repeat call after a suspension, anything else is error */
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  }
kusano 7d535a
  /* Read until EOI */
kusano 7d535a
  while (! cinfo->inputctl->eoi_reached) {
kusano 7d535a
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
kusano 7d535a
      return FALSE;		/* Suspend, come back later */
kusano 7d535a
  }
kusano 7d535a
  /* Do final cleanup */
kusano 7d535a
  (*cinfo->src->term_source) (cinfo);
kusano 7d535a
  /* We can use jpeg_abort to release memory and reset global_state */
kusano 7d535a
  jpeg_abort((j_common_ptr) cinfo);
kusano 7d535a
  return TRUE;
kusano 7d535a
}