3dphotoshoot

diff libs/libjpeg/jcdctmgr.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/jcdctmgr.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,387 @@
     1.4 +/*
     1.5 + * jcdctmgr.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 forward-DCT management logic.
    1.12 + * This code selects a particular DCT implementation to be used,
    1.13 + * and it performs related housekeeping chores including coefficient
    1.14 + * quantization.
    1.15 + */
    1.16 +
    1.17 +#define JPEG_INTERNALS
    1.18 +#include "jinclude.h"
    1.19 +#include "jpeglib.h"
    1.20 +#include "jdct.h"		/* Private declarations for DCT subsystem */
    1.21 +
    1.22 +
    1.23 +/* Private subobject for this module */
    1.24 +
    1.25 +typedef struct {
    1.26 +  struct jpeg_forward_dct pub;	/* public fields */
    1.27 +
    1.28 +  /* Pointer to the DCT routine actually in use */
    1.29 +  forward_DCT_method_ptr do_dct;
    1.30 +
    1.31 +  /* The actual post-DCT divisors --- not identical to the quant table
    1.32 +   * entries, because of scaling (especially for an unnormalized DCT).
    1.33 +   * Each table is given in normal array order.
    1.34 +   */
    1.35 +  DCTELEM * divisors[NUM_QUANT_TBLS];
    1.36 +
    1.37 +#ifdef DCT_FLOAT_SUPPORTED
    1.38 +  /* Same as above for the floating-point case. */
    1.39 +  float_DCT_method_ptr do_float_dct;
    1.40 +  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
    1.41 +#endif
    1.42 +} my_fdct_controller;
    1.43 +
    1.44 +typedef my_fdct_controller * my_fdct_ptr;
    1.45 +
    1.46 +
    1.47 +/*
    1.48 + * Initialize for a processing pass.
    1.49 + * Verify that all referenced Q-tables are present, and set up
    1.50 + * the divisor table for each one.
    1.51 + * In the current implementation, DCT of all components is done during
    1.52 + * the first pass, even if only some components will be output in the
    1.53 + * first scan.  Hence all components should be examined here.
    1.54 + */
    1.55 +
    1.56 +METHODDEF(void)
    1.57 +start_pass_fdctmgr (j_compress_ptr cinfo)
    1.58 +{
    1.59 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
    1.60 +  int ci, qtblno, i;
    1.61 +  jpeg_component_info *compptr;
    1.62 +  JQUANT_TBL * qtbl;
    1.63 +  DCTELEM * dtbl;
    1.64 +
    1.65 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
    1.66 +       ci++, compptr++) {
    1.67 +    qtblno = compptr->quant_tbl_no;
    1.68 +    /* Make sure specified quantization table is present */
    1.69 +    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
    1.70 +	cinfo->quant_tbl_ptrs[qtblno] == NULL)
    1.71 +      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
    1.72 +    qtbl = cinfo->quant_tbl_ptrs[qtblno];
    1.73 +    /* Compute divisors for this quant table */
    1.74 +    /* We may do this more than once for same table, but it's not a big deal */
    1.75 +    switch (cinfo->dct_method) {
    1.76 +#ifdef DCT_ISLOW_SUPPORTED
    1.77 +    case JDCT_ISLOW:
    1.78 +      /* For LL&M IDCT method, divisors are equal to raw quantization
    1.79 +       * coefficients multiplied by 8 (to counteract scaling).
    1.80 +       */
    1.81 +      if (fdct->divisors[qtblno] == NULL) {
    1.82 +	fdct->divisors[qtblno] = (DCTELEM *)
    1.83 +	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
    1.84 +				      DCTSIZE2 * SIZEOF(DCTELEM));
    1.85 +      }
    1.86 +      dtbl = fdct->divisors[qtblno];
    1.87 +      for (i = 0; i < DCTSIZE2; i++) {
    1.88 +	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
    1.89 +      }
    1.90 +      break;
    1.91 +#endif
    1.92 +#ifdef DCT_IFAST_SUPPORTED
    1.93 +    case JDCT_IFAST:
    1.94 +      {
    1.95 +	/* For AA&N IDCT method, divisors are equal to quantization
    1.96 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
    1.97 +	 *   scalefactor[0] = 1
    1.98 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
    1.99 +	 * We apply a further scale factor of 8.
   1.100 +	 */
   1.101 +#define CONST_BITS 14
   1.102 +	static const INT16 aanscales[DCTSIZE2] = {
   1.103 +	  /* precomputed values scaled up by 14 bits */
   1.104 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
   1.105 +	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
   1.106 +	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
   1.107 +	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
   1.108 +	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
   1.109 +	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
   1.110 +	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
   1.111 +	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
   1.112 +	};
   1.113 +	SHIFT_TEMPS
   1.114 +
   1.115 +	if (fdct->divisors[qtblno] == NULL) {
   1.116 +	  fdct->divisors[qtblno] = (DCTELEM *)
   1.117 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.118 +					DCTSIZE2 * SIZEOF(DCTELEM));
   1.119 +	}
   1.120 +	dtbl = fdct->divisors[qtblno];
   1.121 +	for (i = 0; i < DCTSIZE2; i++) {
   1.122 +	  dtbl[i] = (DCTELEM)
   1.123 +	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
   1.124 +				  (INT32) aanscales[i]),
   1.125 +		    CONST_BITS-3);
   1.126 +	}
   1.127 +      }
   1.128 +      break;
   1.129 +#endif
   1.130 +#ifdef DCT_FLOAT_SUPPORTED
   1.131 +    case JDCT_FLOAT:
   1.132 +      {
   1.133 +	/* For float AA&N IDCT method, divisors are equal to quantization
   1.134 +	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
   1.135 +	 *   scalefactor[0] = 1
   1.136 +	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   1.137 +	 * We apply a further scale factor of 8.
   1.138 +	 * What's actually stored is 1/divisor so that the inner loop can
   1.139 +	 * use a multiplication rather than a division.
   1.140 +	 */
   1.141 +	FAST_FLOAT * fdtbl;
   1.142 +	int row, col;
   1.143 +	static const double aanscalefactor[DCTSIZE] = {
   1.144 +	  1.0, 1.387039845, 1.306562965, 1.175875602,
   1.145 +	  1.0, 0.785694958, 0.541196100, 0.275899379
   1.146 +	};
   1.147 +
   1.148 +	if (fdct->float_divisors[qtblno] == NULL) {
   1.149 +	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)
   1.150 +	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.151 +					DCTSIZE2 * SIZEOF(FAST_FLOAT));
   1.152 +	}
   1.153 +	fdtbl = fdct->float_divisors[qtblno];
   1.154 +	i = 0;
   1.155 +	for (row = 0; row < DCTSIZE; row++) {
   1.156 +	  for (col = 0; col < DCTSIZE; col++) {
   1.157 +	    fdtbl[i] = (FAST_FLOAT)
   1.158 +	      (1.0 / (((double) qtbl->quantval[i] *
   1.159 +		       aanscalefactor[row] * aanscalefactor[col] * 8.0)));
   1.160 +	    i++;
   1.161 +	  }
   1.162 +	}
   1.163 +      }
   1.164 +      break;
   1.165 +#endif
   1.166 +    default:
   1.167 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.168 +      break;
   1.169 +    }
   1.170 +  }
   1.171 +}
   1.172 +
   1.173 +
   1.174 +/*
   1.175 + * Perform forward DCT on one or more blocks of a component.
   1.176 + *
   1.177 + * The input samples are taken from the sample_data[] array starting at
   1.178 + * position start_row/start_col, and moving to the right for any additional
   1.179 + * blocks. The quantized coefficients are returned in coef_blocks[].
   1.180 + */
   1.181 +
   1.182 +METHODDEF(void)
   1.183 +forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
   1.184 +	     JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
   1.185 +	     JDIMENSION start_row, JDIMENSION start_col,
   1.186 +	     JDIMENSION num_blocks)
   1.187 +/* This version is used for integer DCT implementations. */
   1.188 +{
   1.189 +  /* This routine is heavily used, so it's worth coding it tightly. */
   1.190 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
   1.191 +  forward_DCT_method_ptr do_dct = fdct->do_dct;
   1.192 +  DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
   1.193 +  DCTELEM workspace[DCTSIZE2];	/* work area for FDCT subroutine */
   1.194 +  JDIMENSION bi;
   1.195 +
   1.196 +  sample_data += start_row;	/* fold in the vertical offset once */
   1.197 +
   1.198 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
   1.199 +    /* Load data into workspace, applying unsigned->signed conversion */
   1.200 +    { register DCTELEM *workspaceptr;
   1.201 +      register JSAMPROW elemptr;
   1.202 +      register int elemr;
   1.203 +
   1.204 +      workspaceptr = workspace;
   1.205 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
   1.206 +	elemptr = sample_data[elemr] + start_col;
   1.207 +#if DCTSIZE == 8		/* unroll the inner loop */
   1.208 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.209 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.210 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.211 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.212 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.213 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.214 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.215 +	*workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.216 +#else
   1.217 +	{ register int elemc;
   1.218 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
   1.219 +	    *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
   1.220 +	  }
   1.221 +	}
   1.222 +#endif
   1.223 +      }
   1.224 +    }
   1.225 +
   1.226 +    /* Perform the DCT */
   1.227 +    (*do_dct) (workspace);
   1.228 +
   1.229 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
   1.230 +    { register DCTELEM temp, qval;
   1.231 +      register int i;
   1.232 +      register JCOEFPTR output_ptr = coef_blocks[bi];
   1.233 +
   1.234 +      for (i = 0; i < DCTSIZE2; i++) {
   1.235 +	qval = divisors[i];
   1.236 +	temp = workspace[i];
   1.237 +	/* Divide the coefficient value by qval, ensuring proper rounding.
   1.238 +	 * Since C does not specify the direction of rounding for negative
   1.239 +	 * quotients, we have to force the dividend positive for portability.
   1.240 +	 *
   1.241 +	 * In most files, at least half of the output values will be zero
   1.242 +	 * (at default quantization settings, more like three-quarters...)
   1.243 +	 * so we should ensure that this case is fast.  On many machines,
   1.244 +	 * a comparison is enough cheaper than a divide to make a special test
   1.245 +	 * a win.  Since both inputs will be nonnegative, we need only test
   1.246 +	 * for a < b to discover whether a/b is 0.
   1.247 +	 * If your machine's division is fast enough, define FAST_DIVIDE.
   1.248 +	 */
   1.249 +#ifdef FAST_DIVIDE
   1.250 +#define DIVIDE_BY(a,b)	a /= b
   1.251 +#else
   1.252 +#define DIVIDE_BY(a,b)	if (a >= b) a /= b; else a = 0
   1.253 +#endif
   1.254 +	if (temp < 0) {
   1.255 +	  temp = -temp;
   1.256 +	  temp += qval>>1;	/* for rounding */
   1.257 +	  DIVIDE_BY(temp, qval);
   1.258 +	  temp = -temp;
   1.259 +	} else {
   1.260 +	  temp += qval>>1;	/* for rounding */
   1.261 +	  DIVIDE_BY(temp, qval);
   1.262 +	}
   1.263 +	output_ptr[i] = (JCOEF) temp;
   1.264 +      }
   1.265 +    }
   1.266 +  }
   1.267 +}
   1.268 +
   1.269 +
   1.270 +#ifdef DCT_FLOAT_SUPPORTED
   1.271 +
   1.272 +METHODDEF(void)
   1.273 +forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
   1.274 +		   JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
   1.275 +		   JDIMENSION start_row, JDIMENSION start_col,
   1.276 +		   JDIMENSION num_blocks)
   1.277 +/* This version is used for floating-point DCT implementations. */
   1.278 +{
   1.279 +  /* This routine is heavily used, so it's worth coding it tightly. */
   1.280 +  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
   1.281 +  float_DCT_method_ptr do_dct = fdct->do_float_dct;
   1.282 +  FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
   1.283 +  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
   1.284 +  JDIMENSION bi;
   1.285 +
   1.286 +  sample_data += start_row;	/* fold in the vertical offset once */
   1.287 +
   1.288 +  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
   1.289 +    /* Load data into workspace, applying unsigned->signed conversion */
   1.290 +    { register FAST_FLOAT *workspaceptr;
   1.291 +      register JSAMPROW elemptr;
   1.292 +      register int elemr;
   1.293 +
   1.294 +      workspaceptr = workspace;
   1.295 +      for (elemr = 0; elemr < DCTSIZE; elemr++) {
   1.296 +	elemptr = sample_data[elemr] + start_col;
   1.297 +#if DCTSIZE == 8		/* unroll the inner loop */
   1.298 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.299 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.300 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.301 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.302 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.303 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.304 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.305 +	*workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.306 +#else
   1.307 +	{ register int elemc;
   1.308 +	  for (elemc = DCTSIZE; elemc > 0; elemc--) {
   1.309 +	    *workspaceptr++ = (FAST_FLOAT)
   1.310 +	      (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
   1.311 +	  }
   1.312 +	}
   1.313 +#endif
   1.314 +      }
   1.315 +    }
   1.316 +
   1.317 +    /* Perform the DCT */
   1.318 +    (*do_dct) (workspace);
   1.319 +
   1.320 +    /* Quantize/descale the coefficients, and store into coef_blocks[] */
   1.321 +    { register FAST_FLOAT temp;
   1.322 +      register int i;
   1.323 +      register JCOEFPTR output_ptr = coef_blocks[bi];
   1.324 +
   1.325 +      for (i = 0; i < DCTSIZE2; i++) {
   1.326 +	/* Apply the quantization and scaling factor */
   1.327 +	temp = workspace[i] * divisors[i];
   1.328 +	/* Round to nearest integer.
   1.329 +	 * Since C does not specify the direction of rounding for negative
   1.330 +	 * quotients, we have to force the dividend positive for portability.
   1.331 +	 * The maximum coefficient size is +-16K (for 12-bit data), so this
   1.332 +	 * code should work for either 16-bit or 32-bit ints.
   1.333 +	 */
   1.334 +	output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
   1.335 +      }
   1.336 +    }
   1.337 +  }
   1.338 +}
   1.339 +
   1.340 +#endif /* DCT_FLOAT_SUPPORTED */
   1.341 +
   1.342 +
   1.343 +/*
   1.344 + * Initialize FDCT manager.
   1.345 + */
   1.346 +
   1.347 +GLOBAL(void)
   1.348 +jinit_forward_dct (j_compress_ptr cinfo)
   1.349 +{
   1.350 +  my_fdct_ptr fdct;
   1.351 +  int i;
   1.352 +
   1.353 +  fdct = (my_fdct_ptr)
   1.354 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.355 +				SIZEOF(my_fdct_controller));
   1.356 +  cinfo->fdct = (struct jpeg_forward_dct *) fdct;
   1.357 +  fdct->pub.start_pass = start_pass_fdctmgr;
   1.358 +
   1.359 +  switch (cinfo->dct_method) {
   1.360 +#ifdef DCT_ISLOW_SUPPORTED
   1.361 +  case JDCT_ISLOW:
   1.362 +    fdct->pub.forward_DCT = forward_DCT;
   1.363 +    fdct->do_dct = jpeg_fdct_islow;
   1.364 +    break;
   1.365 +#endif
   1.366 +#ifdef DCT_IFAST_SUPPORTED
   1.367 +  case JDCT_IFAST:
   1.368 +    fdct->pub.forward_DCT = forward_DCT;
   1.369 +    fdct->do_dct = jpeg_fdct_ifast;
   1.370 +    break;
   1.371 +#endif
   1.372 +#ifdef DCT_FLOAT_SUPPORTED
   1.373 +  case JDCT_FLOAT:
   1.374 +    fdct->pub.forward_DCT = forward_DCT_float;
   1.375 +    fdct->do_float_dct = jpeg_fdct_float;
   1.376 +    break;
   1.377 +#endif
   1.378 +  default:
   1.379 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.380 +    break;
   1.381 +  }
   1.382 +
   1.383 +  /* Mark divisor tables unallocated */
   1.384 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
   1.385 +    fdct->divisors[i] = NULL;
   1.386 +#ifdef DCT_FLOAT_SUPPORTED
   1.387 +    fdct->float_divisors[i] = NULL;
   1.388 +#endif
   1.389 +  }
   1.390 +}