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

darco 56a656
/*
darco 56a656
 * jddctmgr.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 the inverse-DCT management logic.
darco 56a656
 * This code selects a particular IDCT implementation to be used,
darco 56a656
 * and it performs related housekeeping chores.  No code in this file
darco 56a656
 * is executed per IDCT step, only during output pass setup.
darco 56a656
 *
darco 56a656
 * Note that the IDCT routines are responsible for performing coefficient
darco 56a656
 * dequantization as well as the IDCT proper.  This module sets up the
darco 56a656
 * dequantization multiplier table needed by the IDCT routine.
darco 56a656
 */
darco 56a656
darco 56a656
#define JPEG_INTERNALS
darco 56a656
#include "jinclude.h"
darco 56a656
#include "jpeglib.h"
darco 56a656
#include "jdct.h"		/* Private declarations for DCT subsystem */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * The decompressor input side (jdinput.c) saves away the appropriate
darco 56a656
 * quantization table for each component at the start of the first scan
darco 56a656
 * involving that component.  (This is necessary in order to correctly
darco 56a656
 * decode files that reuse Q-table slots.)
darco 56a656
 * When we are ready to make an output pass, the saved Q-table is converted
darco 56a656
 * to a multiplier table that will actually be used by the IDCT routine.
darco 56a656
 * The multiplier table contents are IDCT-method-dependent.  To support
darco 56a656
 * application changes in IDCT method between scans, we can remake the
darco 56a656
 * multiplier tables if necessary.
darco 56a656
 * In buffered-image mode, the first output pass may occur before any data
darco 56a656
 * has been seen for some components, and thus before their Q-tables have
darco 56a656
 * been saved away.  To handle this case, multiplier tables are preset
darco 56a656
 * to zeroes; the result of the IDCT will be a neutral gray level.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
/* Private subobject for this module */
darco 56a656
darco 56a656
typedef struct {
darco 56a656
  struct jpeg_inverse_dct pub;	/* public fields */
darco 56a656
darco 56a656
  /* This array contains the IDCT method code that each multiplier table
darco 56a656
   * is currently set up for, or -1 if it's not yet set up.
darco 56a656
   * The actual multiplier tables are pointed to by dct_table in the
darco 56a656
   * per-component comp_info structures.
darco 56a656
   */
darco 56a656
  int cur_method[MAX_COMPONENTS];
darco 56a656
} my_idct_controller;
darco 56a656
darco 56a656
typedef my_idct_controller * my_idct_ptr;
darco 56a656
darco 56a656
darco 56a656
/* Allocated multiplier tables: big enough for any supported variant */
darco 56a656
darco 56a656
typedef union {
darco 56a656
  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
darco 56a656
#ifdef DCT_IFAST_SUPPORTED
darco 56a656
  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
darco 56a656
#endif
darco 56a656
#ifdef DCT_FLOAT_SUPPORTED
darco 56a656
  FLOAT_MULT_TYPE float_array[DCTSIZE2];
darco 56a656
#endif
darco 56a656
} multiplier_table;
darco 56a656
darco 56a656
darco 56a656
/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
darco 56a656
 * so be sure to compile that code if either ISLOW or SCALING is requested.
darco 56a656
 */
darco 56a656
#ifdef DCT_ISLOW_SUPPORTED
darco 56a656
#define PROVIDE_ISLOW_TABLES
darco 56a656
#else
darco 56a656
#ifdef IDCT_SCALING_SUPPORTED
darco 56a656
#define PROVIDE_ISLOW_TABLES
darco 56a656
#endif
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Prepare for an output pass.
darco 56a656
 * Here we select the proper IDCT routine for each component and build
darco 56a656
 * a matching multiplier table.
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
start_pass (j_decompress_ptr cinfo)
darco 56a656
{
darco 56a656
  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
darco 56a656
  int ci, i;
darco 56a656
  jpeg_component_info *compptr;
darco 56a656
  int method = 0;
darco 56a656
  inverse_DCT_method_ptr method_ptr = NULL;
darco 56a656
  JQUANT_TBL * qtbl;
darco 56a656
darco 56a656
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
darco 56a656
       ci++, compptr++) {
darco 56a656
    /* Select the proper IDCT routine for this component's scaling */
darco 56a656
    switch (compptr->DCT_scaled_size) {
darco 56a656
#ifdef IDCT_SCALING_SUPPORTED
darco 56a656
    case 1:
darco 56a656
      method_ptr = jpeg_idct_1x1;
darco 56a656
      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
darco 56a656
      break;
darco 56a656
    case 2:
darco 56a656
      method_ptr = jpeg_idct_2x2;
darco 56a656
      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
darco 56a656
      break;
darco 56a656
    case 4:
darco 56a656
      method_ptr = jpeg_idct_4x4;
darco 56a656
      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
darco 56a656
      break;
darco 56a656
#endif
darco 56a656
    case DCTSIZE:
darco 56a656
      switch (cinfo->dct_method) {
darco 56a656
#ifdef DCT_ISLOW_SUPPORTED
darco 56a656
      case JDCT_ISLOW:
darco 56a656
	method_ptr = jpeg_idct_islow;
darco 56a656
	method = JDCT_ISLOW;
darco 56a656
	break;
darco 56a656
#endif
darco 56a656
#ifdef DCT_IFAST_SUPPORTED
darco 56a656
      case JDCT_IFAST:
darco 56a656
	method_ptr = jpeg_idct_ifast;
darco 56a656
	method = JDCT_IFAST;
darco 56a656
	break;
darco 56a656
#endif
darco 56a656
#ifdef DCT_FLOAT_SUPPORTED
darco 56a656
      case JDCT_FLOAT:
darco 56a656
	method_ptr = jpeg_idct_float;
darco 56a656
	method = JDCT_FLOAT;
darco 56a656
	break;
darco 56a656
#endif
darco 56a656
      default:
darco 56a656
	ERREXIT(cinfo, JERR_NOT_COMPILED);
darco 56a656
	break;
darco 56a656
      }
darco 56a656
      break;
darco 56a656
    default:
darco 56a656
      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
darco 56a656
      break;
darco 56a656
    }
darco 56a656
    idct->pub.inverse_DCT[ci] = method_ptr;
darco 56a656
    /* Create multiplier table from quant table.
darco 56a656
     * However, we can skip this if the component is uninteresting
darco 56a656
     * or if we already built the table.  Also, if no quant table
darco 56a656
     * has yet been saved for the component, we leave the
darco 56a656
     * multiplier table all-zero; we'll be reading zeroes from the
darco 56a656
     * coefficient controller's buffer anyway.
darco 56a656
     */
darco 56a656
    if (! compptr->component_needed || idct->cur_method[ci] == method)
darco 56a656
      continue;
darco 56a656
    qtbl = compptr->quant_table;
darco 56a656
    if (qtbl == NULL)		/* happens if no data yet for component */
darco 56a656
      continue;
darco 56a656
    idct->cur_method[ci] = method;
darco 56a656
    switch (method) {
darco 56a656
#ifdef PROVIDE_ISLOW_TABLES
darco 56a656
    case JDCT_ISLOW:
darco 56a656
      {
darco 56a656
	/* For LL&M IDCT method, multipliers are equal to raw quantization
darco 56a656
	 * coefficients, but are stored as ints to ensure access efficiency.
darco 56a656
	 */
darco 56a656
	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
darco 56a656
	for (i = 0; i < DCTSIZE2; i++) {
darco 56a656
	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
darco 56a656
	}
darco 56a656
      }
darco 56a656
      break;
darco 56a656
#endif
darco 56a656
#ifdef DCT_IFAST_SUPPORTED
darco 56a656
    case JDCT_IFAST:
darco 56a656
      {
darco 56a656
	/* For AA&N IDCT method, multipliers are equal to quantization
darco 56a656
	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
darco 56a656
	 *   scalefactor[0] = 1
darco 56a656
	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
darco 56a656
	 * For integer operation, the multiplier table is to be scaled by
darco 56a656
	 * IFAST_SCALE_BITS.
darco 56a656
	 */
darco 56a656
	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
darco 56a656
#define CONST_BITS 14
darco 56a656
	static const INT16 aanscales[DCTSIZE2] = {
darco 56a656
	  /* precomputed values scaled up by 14 bits */
darco 56a656
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
darco 56a656
	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
darco 56a656
	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
darco 56a656
	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
darco 56a656
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
darco 56a656
	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
darco 56a656
	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
darco 56a656
	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
darco 56a656
	};
darco 56a656
	SHIFT_TEMPS
darco 56a656
darco 56a656
	for (i = 0; i < DCTSIZE2; i++) {
darco 56a656
	  ifmtbl[i] = (IFAST_MULT_TYPE)
darco 56a656
	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
darco 56a656
				  (INT32) aanscales[i]),
darco 56a656
		    CONST_BITS-IFAST_SCALE_BITS);
darco 56a656
	}
darco 56a656
      }
darco 56a656
      break;
darco 56a656
#endif
darco 56a656
#ifdef DCT_FLOAT_SUPPORTED
darco 56a656
    case JDCT_FLOAT:
darco 56a656
      {
darco 56a656
	/* For float AA&N IDCT method, multipliers are equal to quantization
darco 56a656
	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
darco 56a656
	 *   scalefactor[0] = 1
darco 56a656
	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
darco 56a656
	 */
darco 56a656
	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
darco 56a656
	int row, col;
darco 56a656
	static const double aanscalefactor[DCTSIZE] = {
darco 56a656
	  1.0, 1.387039845, 1.306562965, 1.175875602,
darco 56a656
	  1.0, 0.785694958, 0.541196100, 0.275899379
darco 56a656
	};
darco 56a656
darco 56a656
	i = 0;
darco 56a656
	for (row = 0; row < DCTSIZE; row++) {
darco 56a656
	  for (col = 0; col < DCTSIZE; col++) {
darco 56a656
	    fmtbl[i] = (FLOAT_MULT_TYPE)
darco 56a656
	      ((double) qtbl->quantval[i] *
darco 56a656
	       aanscalefactor[row] * aanscalefactor[col]);
darco 56a656
	    i++;
darco 56a656
	  }
darco 56a656
	}
darco 56a656
      }
darco 56a656
      break;
darco 56a656
#endif
darco 56a656
    default:
darco 56a656
      ERREXIT(cinfo, JERR_NOT_COMPILED);
darco 56a656
      break;
darco 56a656
    }
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Initialize IDCT manager.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jinit_inverse_dct (j_decompress_ptr cinfo)
darco 56a656
{
darco 56a656
  my_idct_ptr idct;
darco 56a656
  int ci;
darco 56a656
  jpeg_component_info *compptr;
darco 56a656
darco 56a656
  idct = (my_idct_ptr)
darco 56a656
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
darco 56a656
				SIZEOF(my_idct_controller));
darco 56a656
  cinfo->idct = (struct jpeg_inverse_dct *) idct;
darco 56a656
  idct->pub.start_pass = start_pass;
darco 56a656
darco 56a656
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
darco 56a656
       ci++, compptr++) {
darco 56a656
    /* Allocate and pre-zero a multiplier table for each component */
darco 56a656
    compptr->dct_table =
darco 56a656
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
darco 56a656
				  SIZEOF(multiplier_table));
darco 56a656
    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
darco 56a656
    /* Mark multiplier table not yet set up for any method */
darco 56a656
    idct->cur_method[ci] = -1;
darco 56a656
  }
darco 56a656
}