kusano 7d535a
/*
kusano 7d535a
 * jdtrans.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1995-1997, Thomas G. Lane.
kusano 7d535a
 * Modified 2000-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 library routines for transcoding decompression,
kusano 7d535a
 * that is, reading raw DCT coefficient arrays from an input JPEG file.
kusano 7d535a
 * The routines in jdapimin.c will also be needed by a transcoder.
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
/* Forward declarations */
kusano 7d535a
LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read the coefficient arrays from a JPEG file.
kusano 7d535a
 * jpeg_read_header must be completed before calling this.
kusano 7d535a
 *
kusano 7d535a
 * The entire image is read into a set of virtual coefficient-block arrays,
kusano 7d535a
 * one per component.  The return value is a pointer to the array of
kusano 7d535a
 * virtual-array descriptors.  These can be manipulated directly via the
kusano 7d535a
 * JPEG memory manager, or handed off to jpeg_write_coefficients().
kusano 7d535a
 * To release the memory occupied by the virtual arrays, call
kusano 7d535a
 * jpeg_finish_decompress() when done with the data.
kusano 7d535a
 *
kusano 7d535a
 * An alternative usage is to simply obtain access to the coefficient arrays
kusano 7d535a
 * during a buffered-image-mode decompression operation.  This is allowed
kusano 7d535a
 * after any jpeg_finish_output() call.  The arrays can be accessed until
kusano 7d535a
 * jpeg_finish_decompress() is called.  (Note that any call to the library
kusano 7d535a
 * may reposition the arrays, so don't rely on access_virt_barray() results
kusano 7d535a
 * to stay valid across library calls.)
kusano 7d535a
 *
kusano 7d535a
 * Returns NULL if suspended.  This case need be checked only if
kusano 7d535a
 * a suspending data source is used.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(jvirt_barray_ptr *)
kusano 7d535a
jpeg_read_coefficients (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  if (cinfo->global_state == DSTATE_READY) {
kusano 7d535a
    /* First call: initialize active modules */
kusano 7d535a
    transdecode_master_selection(cinfo);
kusano 7d535a
    cinfo->global_state = DSTATE_RDCOEFS;
kusano 7d535a
  }
kusano 7d535a
  if (cinfo->global_state == DSTATE_RDCOEFS) {
kusano 7d535a
    /* Absorb whole file into the coef buffer */
kusano 7d535a
    for (;;) {
kusano 7d535a
      int retcode;
kusano 7d535a
      /* Call progress monitor hook if present */
kusano 7d535a
      if (cinfo->progress != NULL)
kusano 7d535a
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
      /* Absorb some more input */
kusano 7d535a
      retcode = (*cinfo->inputctl->consume_input) (cinfo);
kusano 7d535a
      if (retcode == JPEG_SUSPENDED)
kusano 7d535a
	return NULL;
kusano 7d535a
      if (retcode == JPEG_REACHED_EOI)
kusano 7d535a
	break;
kusano 7d535a
      /* Advance progress counter if appropriate */
kusano 7d535a
      if (cinfo->progress != NULL &&
kusano 7d535a
	  (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
kusano 7d535a
	if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
kusano 7d535a
	  /* startup underestimated number of scans; ratchet up one scan */
kusano 7d535a
	  cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Set state so that jpeg_finish_decompress does the right thing */
kusano 7d535a
    cinfo->global_state = DSTATE_STOPPING;
kusano 7d535a
  }
kusano 7d535a
  /* At this point we should be in state DSTATE_STOPPING if being used
kusano 7d535a
   * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
kusano 7d535a
   * to the coefficients during a full buffered-image-mode decompression.
kusano 7d535a
   */
kusano 7d535a
  if ((cinfo->global_state == DSTATE_STOPPING ||
kusano 7d535a
       cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
kusano 7d535a
    return cinfo->coef->coef_arrays;
kusano 7d535a
  }
kusano 7d535a
  /* Oops, improper usage */
kusano 7d535a
  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  return NULL;			/* keep compiler happy */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Master selection of decompression modules for transcoding.
kusano 7d535a
 * This substitutes for jdmaster.c's initialization of the full decompressor.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
transdecode_master_selection (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* This is effectively a buffered-image operation. */
kusano 7d535a
  cinfo->buffered_image = TRUE;
kusano 7d535a
kusano 7d535a
  /* Compute output image dimensions and related values. */
kusano 7d535a
  jpeg_core_output_dimensions(cinfo);
kusano 7d535a
kusano 7d535a
  /* Entropy decoding: either Huffman or arithmetic coding. */
kusano 7d535a
  if (cinfo->arith_code)
kusano 7d535a
    jinit_arith_decoder(cinfo);
kusano 7d535a
  else {
kusano 7d535a
    jinit_huff_decoder(cinfo);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Always get a full-image coefficient buffer. */
kusano 7d535a
  jinit_d_coef_controller(cinfo, TRUE);
kusano 7d535a
kusano 7d535a
  /* We can now tell the memory manager to allocate virtual arrays. */
kusano 7d535a
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
kusano 7d535a
kusano 7d535a
  /* Initialize input side of decompressor to consume first scan. */
kusano 7d535a
  (*cinfo->inputctl->start_input_pass) (cinfo);
kusano 7d535a
kusano 7d535a
  /* Initialize progress monitoring. */
kusano 7d535a
  if (cinfo->progress != NULL) {
kusano 7d535a
    int nscans;
kusano 7d535a
    /* Estimate number of scans to set pass_limit. */
kusano 7d535a
    if (cinfo->progressive_mode) {
kusano 7d535a
      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
kusano 7d535a
      nscans = 2 + 3 * cinfo->num_components;
kusano 7d535a
    } else if (cinfo->inputctl->has_multiple_scans) {
kusano 7d535a
      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
kusano 7d535a
      nscans = cinfo->num_components;
kusano 7d535a
    } else {
kusano 7d535a
      nscans = 1;
kusano 7d535a
    }
kusano 7d535a
    cinfo->progress->pass_counter = 0L;
kusano 7d535a
    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
kusano 7d535a
    cinfo->progress->completed_passes = 0;
kusano 7d535a
    cinfo->progress->total_passes = 1;
kusano 7d535a
  }
kusano 7d535a
}