3dphotoshoot

diff libs/libjpeg/jddctmgr.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jddctmgr.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,269 @@
     1.4 +/*
     1.5 + * jddctmgr.c
     1.6 + *
     1.7 + * Copyright (C) 1994-1996, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + * For conditions of distribution and use, see the accompanying README file.
    1.10 + *
    1.11 + * This file contains the inverse-DCT management logic.
    1.12 + * This code selects a particular IDCT implementation to be used,
    1.13 + * and it performs related housekeeping chores.  No code in this file
    1.14 + * is executed per IDCT step, only during output pass setup.
    1.15 + *
    1.16 + * Note that the IDCT routines are responsible for performing coefficient
    1.17 + * dequantization as well as the IDCT proper.  This module sets up the
    1.18 + * dequantization multiplier table needed by the IDCT routine.
    1.19 + */
    1.20 +
    1.21 +#define JPEG_INTERNALS
    1.22 +#include "jinclude.h"
    1.23 +#include "jpeglib.h"
    1.24 +#include "jdct.h"		/* Private declarations for DCT subsystem */
    1.25 +
    1.26 +
    1.27 +/*
    1.28 + * The decompressor input side (jdinput.c) saves away the appropriate
    1.29 + * quantization table for each component at the start of the first scan
    1.30 + * involving that component.  (This is necessary in order to correctly
    1.31 + * decode files that reuse Q-table slots.)
    1.32 + * When we are ready to make an output pass, the saved Q-table is converted
    1.33 + * to a multiplier table that will actually be used by the IDCT routine.
    1.34 + * The multiplier table contents are IDCT-method-dependent.  To support
    1.35 + * application changes in IDCT method between scans, we can remake the
    1.36 + * multiplier tables if necessary.
    1.37 + * In buffered-image mode, the first output pass may occur before any data
    1.38 + * has been seen for some components, and thus before their Q-tables have
    1.39 + * been saved away.  To handle this case, multiplier tables are preset
    1.40 + * to zeroes; the result of the IDCT will be a neutral gray level.
    1.41 + */
    1.42 +
    1.43 +
    1.44 +/* Private subobject for this module */
    1.45 +
    1.46 +typedef struct {
    1.47 +  struct jpeg_inverse_dct pub;	/* public fields */
    1.48 +
    1.49 +  /* This array contains the IDCT method code that each multiplier table
    1.50 +   * is currently set up for, or -1 if it's not yet set up.
    1.51 +   * The actual multiplier tables are pointed to by dct_table in the
    1.52 +   * per-component comp_info structures.
    1.53 +   */
    1.54 +  int cur_method[MAX_COMPONENTS];
    1.55 +} my_idct_controller;
    1.56 +
    1.57 +typedef my_idct_controller * my_idct_ptr;
    1.58 +
    1.59 +
    1.60 +/* Allocated multiplier tables: big enough for any supported variant */
    1.61 +
    1.62 +typedef union {
    1.63 +  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
    1.64 +#ifdef DCT_IFAST_SUPPORTED
    1.65 +  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
    1.66 +#endif
    1.67 +#ifdef DCT_FLOAT_SUPPORTED
    1.68 +  FLOAT_MULT_TYPE float_array[DCTSIZE2];
    1.69 +#endif
    1.70 +} multiplier_table;
    1.71 +
    1.72 +
    1.73 +/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
    1.74 + * so be sure to compile that code if either ISLOW or SCALING is requested.
    1.75 + */
    1.76 +#ifdef DCT_ISLOW_SUPPORTED
    1.77 +#define PROVIDE_ISLOW_TABLES
    1.78 +#else
    1.79 +#ifdef IDCT_SCALING_SUPPORTED
    1.80 +#define PROVIDE_ISLOW_TABLES
    1.81 +#endif
    1.82 +#endif
    1.83 +
    1.84 +
    1.85 +/*
    1.86 + * Prepare for an output pass.
    1.87 + * Here we select the proper IDCT routine for each component and build
    1.88 + * a matching multiplier table.
    1.89 + */
    1.90 +
    1.91 +METHODDEF(void)
    1.92 +start_pass (j_decompress_ptr cinfo)
    1.93 +{
    1.94 +  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
    1.95 +  int ci, i;
    1.96 +  jpeg_component_info *compptr;
    1.97 +  int method = 0;
    1.98 +  inverse_DCT_method_ptr method_ptr = NULL;
    1.99 +  JQUANT_TBL * qtbl;
   1.100 +
   1.101 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.102 +       ci++, compptr++) {
   1.103 +    /* Select the proper IDCT routine for this component's scaling */
   1.104 +    switch (compptr->DCT_scaled_size) {
   1.105 +#ifdef IDCT_SCALING_SUPPORTED
   1.106 +    case 1:
   1.107 +      method_ptr = jpeg_idct_1x1;
   1.108 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
   1.109 +      break;
   1.110 +    case 2:
   1.111 +      method_ptr = jpeg_idct_2x2;
   1.112 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
   1.113 +      break;
   1.114 +    case 4:
   1.115 +      method_ptr = jpeg_idct_4x4;
   1.116 +      method = JDCT_ISLOW;	/* jidctred uses islow-style table */
   1.117 +      break;
   1.118 +#endif
   1.119 +    case DCTSIZE:
   1.120 +      switch (cinfo->dct_method) {
   1.121 +#ifdef DCT_ISLOW_SUPPORTED
   1.122 +      case JDCT_ISLOW:
   1.123 +	method_ptr = jpeg_idct_islow;
   1.124 +	method = JDCT_ISLOW;
   1.125 +	break;
   1.126 +#endif
   1.127 +#ifdef DCT_IFAST_SUPPORTED
   1.128 +      case JDCT_IFAST:
   1.129 +	method_ptr = jpeg_idct_ifast;
   1.130 +	method = JDCT_IFAST;
   1.131 +	break;
   1.132 +#endif
   1.133 +#ifdef DCT_FLOAT_SUPPORTED
   1.134 +      case JDCT_FLOAT:
   1.135 +	method_ptr = jpeg_idct_float;
   1.136 +	method = JDCT_FLOAT;
   1.137 +	break;
   1.138 +#endif
   1.139 +      default:
   1.140 +	ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.141 +	break;
   1.142 +      }
   1.143 +      break;
   1.144 +    default:
   1.145 +      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
   1.146 +      break;
   1.147 +    }
   1.148 +    idct->pub.inverse_DCT[ci] = method_ptr;
   1.149 +    /* Create multiplier table from quant table.
   1.150 +     * However, we can skip this if the component is uninteresting
   1.151 +     * or if we already built the table.  Also, if no quant table
   1.152 +     * has yet been saved for the component, we leave the
   1.153 +     * multiplier table all-zero; we'll be reading zeroes from the
   1.154 +     * coefficient controller's buffer anyway.
   1.155 +     */
   1.156 +    if (! compptr->component_needed || idct->cur_method[ci] == method)
   1.157 +      continue;
   1.158 +    qtbl = compptr->quant_table;
   1.159 +    if (qtbl == NULL)		/* happens if no data yet for component */
   1.160 +      continue;
   1.161 +    idct->cur_method[ci] = method;
   1.162 +    switch (method) {
   1.163 +#ifdef PROVIDE_ISLOW_TABLES
   1.164 +    case JDCT_ISLOW:
   1.165 +      {
   1.166 +	/* For LL&M IDCT method, multipliers are equal to raw quantization
   1.167 +	 * coefficients, but are stored as ints to ensure access efficiency.
   1.168 +	 */
   1.169 +	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
   1.170 +	for (i = 0; i < DCTSIZE2; i++) {
   1.171 +	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
   1.172 +	}
   1.173 +      }
   1.174 +      break;
   1.175 +#endif
   1.176 +#ifdef DCT_IFAST_SUPPORTED
   1.177 +    case JDCT_IFAST:
   1.178 +      {
   1.179 +	/* For AA&N IDCT method, multipliers are equal to quantization
   1.180 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   1.181 +	 *   scalefactor[0] = 1
   1.182 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   1.183 +	 * For integer operation, the multiplier table is to be scaled by
   1.184 +	 * IFAST_SCALE_BITS.
   1.185 +	 */
   1.186 +	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
   1.187 +#define CONST_BITS 14
   1.188 +	static const INT16 aanscales[DCTSIZE2] = {
   1.189 +	  /* precomputed values scaled up by 14 bits */
   1.190 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
   1.191 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
   1.192 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
   1.193 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
   1.194 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
   1.195 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
   1.196 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
   1.197 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
   1.198 +	};
   1.199 +	SHIFT_TEMPS
   1.200 +
   1.201 +	for (i = 0; i < DCTSIZE2; i++) {
   1.202 +	  ifmtbl[i] = (IFAST_MULT_TYPE)
   1.203 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
   1.204 +				  (INT32) aanscales[i]),
   1.205 +		    CONST_BITS-IFAST_SCALE_BITS);
   1.206 +	}
   1.207 +      }
   1.208 +      break;
   1.209 +#endif
   1.210 +#ifdef DCT_FLOAT_SUPPORTED
   1.211 +    case JDCT_FLOAT:
   1.212 +      {
   1.213 +	/* For float AA&N IDCT method, multipliers are equal to quantization
   1.214 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   1.215 +	 *   scalefactor[0] = 1
   1.216 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   1.217 +	 */
   1.218 +	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
   1.219 +	int row, col;
   1.220 +	static const double aanscalefactor[DCTSIZE] = {
   1.221 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
   1.222 +	  1.0, 0.785694958, 0.541196100, 0.275899379
   1.223 +	};
   1.224 +
   1.225 +	i = 0;
   1.226 +	for (row = 0; row < DCTSIZE; row++) {
   1.227 +	  for (col = 0; col < DCTSIZE; col++) {
   1.228 +	    fmtbl[i] = (FLOAT_MULT_TYPE)
   1.229 +	      ((double) qtbl->quantval[i] *
   1.230 +	       aanscalefactor[row] * aanscalefactor[col]);
   1.231 +	    i++;
   1.232 +	  }
   1.233 +	}
   1.234 +      }
   1.235 +      break;
   1.236 +#endif
   1.237 +    default:
   1.238 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.239 +      break;
   1.240 +    }
   1.241 +  }
   1.242 +}
   1.243 +
   1.244 +
   1.245 +/*
   1.246 + * Initialize IDCT manager.
   1.247 + */
   1.248 +
   1.249 +GLOBAL(void)
   1.250 +jinit_inverse_dct (j_decompress_ptr cinfo)
   1.251 +{
   1.252 +  my_idct_ptr idct;
   1.253 +  int ci;
   1.254 +  jpeg_component_info *compptr;
   1.255 +
   1.256 +  idct = (my_idct_ptr)
   1.257 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.258 +				SIZEOF(my_idct_controller));
   1.259 +  cinfo->idct = (struct jpeg_inverse_dct *) idct;
   1.260 +  idct->pub.start_pass = start_pass;
   1.261 +
   1.262 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.263 +       ci++, compptr++) {
   1.264 +    /* Allocate and pre-zero a multiplier table for each component */
   1.265 +    compptr->dct_table =
   1.266 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.267 +				  SIZEOF(multiplier_table));
   1.268 +    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
   1.269 +    /* Mark multiplier table not yet set up for any method */
   1.270 +    idct->cur_method[ci] = -1;
   1.271 +  }
   1.272 +}