dbf-halloween2015

annotate libs/libjpeg/jcdctmgr.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
rev   line source
nuclear@1 1 /*
nuclear@1 2 * jcdctmgr.c
nuclear@1 3 *
nuclear@1 4 * Copyright (C) 1994-1996, Thomas G. Lane.
nuclear@1 5 * This file is part of the Independent JPEG Group's software.
nuclear@1 6 * For conditions of distribution and use, see the accompanying README file.
nuclear@1 7 *
nuclear@1 8 * This file contains the forward-DCT management logic.
nuclear@1 9 * This code selects a particular DCT implementation to be used,
nuclear@1 10 * and it performs related housekeeping chores including coefficient
nuclear@1 11 * quantization.
nuclear@1 12 */
nuclear@1 13
nuclear@1 14 #define JPEG_INTERNALS
nuclear@1 15 #include "jinclude.h"
nuclear@1 16 #include "jpeglib.h"
nuclear@1 17 #include "jdct.h" /* Private declarations for DCT subsystem */
nuclear@1 18
nuclear@1 19
nuclear@1 20 /* Private subobject for this module */
nuclear@1 21
nuclear@1 22 typedef struct {
nuclear@1 23 struct jpeg_forward_dct pub; /* public fields */
nuclear@1 24
nuclear@1 25 /* Pointer to the DCT routine actually in use */
nuclear@1 26 forward_DCT_method_ptr do_dct;
nuclear@1 27
nuclear@1 28 /* The actual post-DCT divisors --- not identical to the quant table
nuclear@1 29 * entries, because of scaling (especially for an unnormalized DCT).
nuclear@1 30 * Each table is given in normal array order.
nuclear@1 31 */
nuclear@1 32 DCTELEM * divisors[NUM_QUANT_TBLS];
nuclear@1 33
nuclear@1 34 #ifdef DCT_FLOAT_SUPPORTED
nuclear@1 35 /* Same as above for the floating-point case. */
nuclear@1 36 float_DCT_method_ptr do_float_dct;
nuclear@1 37 FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
nuclear@1 38 #endif
nuclear@1 39 } my_fdct_controller;
nuclear@1 40
nuclear@1 41 typedef my_fdct_controller * my_fdct_ptr;
nuclear@1 42
nuclear@1 43
nuclear@1 44 /*
nuclear@1 45 * Initialize for a processing pass.
nuclear@1 46 * Verify that all referenced Q-tables are present, and set up
nuclear@1 47 * the divisor table for each one.
nuclear@1 48 * In the current implementation, DCT of all components is done during
nuclear@1 49 * the first pass, even if only some components will be output in the
nuclear@1 50 * first scan. Hence all components should be examined here.
nuclear@1 51 */
nuclear@1 52
nuclear@1 53 METHODDEF(void)
nuclear@1 54 start_pass_fdctmgr (j_compress_ptr cinfo)
nuclear@1 55 {
nuclear@1 56 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
nuclear@1 57 int ci, qtblno, i;
nuclear@1 58 jpeg_component_info *compptr;
nuclear@1 59 JQUANT_TBL * qtbl;
nuclear@1 60 DCTELEM * dtbl;
nuclear@1 61
nuclear@1 62 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
nuclear@1 63 ci++, compptr++) {
nuclear@1 64 qtblno = compptr->quant_tbl_no;
nuclear@1 65 /* Make sure specified quantization table is present */
nuclear@1 66 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
nuclear@1 67 cinfo->quant_tbl_ptrs[qtblno] == NULL)
nuclear@1 68 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
nuclear@1 69 qtbl = cinfo->quant_tbl_ptrs[qtblno];
nuclear@1 70 /* Compute divisors for this quant table */
nuclear@1 71 /* We may do this more than once for same table, but it's not a big deal */
nuclear@1 72 switch (cinfo->dct_method) {
nuclear@1 73 #ifdef DCT_ISLOW_SUPPORTED
nuclear@1 74 case JDCT_ISLOW:
nuclear@1 75 /* For LL&M IDCT method, divisors are equal to raw quantization
nuclear@1 76 * coefficients multiplied by 8 (to counteract scaling).
nuclear@1 77 */
nuclear@1 78 if (fdct->divisors[qtblno] == NULL) {
nuclear@1 79 fdct->divisors[qtblno] = (DCTELEM *)
nuclear@1 80 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 81 DCTSIZE2 * SIZEOF(DCTELEM));
nuclear@1 82 }
nuclear@1 83 dtbl = fdct->divisors[qtblno];
nuclear@1 84 for (i = 0; i < DCTSIZE2; i++) {
nuclear@1 85 dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
nuclear@1 86 }
nuclear@1 87 break;
nuclear@1 88 #endif
nuclear@1 89 #ifdef DCT_IFAST_SUPPORTED
nuclear@1 90 case JDCT_IFAST:
nuclear@1 91 {
nuclear@1 92 /* For AA&N IDCT method, divisors are equal to quantization
nuclear@1 93 * coefficients scaled by scalefactor[row]*scalefactor[col], where
nuclear@1 94 * scalefactor[0] = 1
nuclear@1 95 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
nuclear@1 96 * We apply a further scale factor of 8.
nuclear@1 97 */
nuclear@1 98 #define CONST_BITS 14
nuclear@1 99 static const INT16 aanscales[DCTSIZE2] = {
nuclear@1 100 /* precomputed values scaled up by 14 bits */
nuclear@1 101 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
nuclear@1 102 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
nuclear@1 103 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
nuclear@1 104 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
nuclear@1 105 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
nuclear@1 106 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
nuclear@1 107 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
nuclear@1 108 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
nuclear@1 109 };
nuclear@1 110 SHIFT_TEMPS
nuclear@1 111
nuclear@1 112 if (fdct->divisors[qtblno] == NULL) {
nuclear@1 113 fdct->divisors[qtblno] = (DCTELEM *)
nuclear@1 114 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 115 DCTSIZE2 * SIZEOF(DCTELEM));
nuclear@1 116 }
nuclear@1 117 dtbl = fdct->divisors[qtblno];
nuclear@1 118 for (i = 0; i < DCTSIZE2; i++) {
nuclear@1 119 dtbl[i] = (DCTELEM)
nuclear@1 120 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
nuclear@1 121 (INT32) aanscales[i]),
nuclear@1 122 CONST_BITS-3);
nuclear@1 123 }
nuclear@1 124 }
nuclear@1 125 break;
nuclear@1 126 #endif
nuclear@1 127 #ifdef DCT_FLOAT_SUPPORTED
nuclear@1 128 case JDCT_FLOAT:
nuclear@1 129 {
nuclear@1 130 /* For float AA&N IDCT method, divisors are equal to quantization
nuclear@1 131 * coefficients scaled by scalefactor[row]*scalefactor[col], where
nuclear@1 132 * scalefactor[0] = 1
nuclear@1 133 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
nuclear@1 134 * We apply a further scale factor of 8.
nuclear@1 135 * What's actually stored is 1/divisor so that the inner loop can
nuclear@1 136 * use a multiplication rather than a division.
nuclear@1 137 */
nuclear@1 138 FAST_FLOAT * fdtbl;
nuclear@1 139 int row, col;
nuclear@1 140 static const double aanscalefactor[DCTSIZE] = {
nuclear@1 141 1.0, 1.387039845, 1.306562965, 1.175875602,
nuclear@1 142 1.0, 0.785694958, 0.541196100, 0.275899379
nuclear@1 143 };
nuclear@1 144
nuclear@1 145 if (fdct->float_divisors[qtblno] == NULL) {
nuclear@1 146 fdct->float_divisors[qtblno] = (FAST_FLOAT *)
nuclear@1 147 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 148 DCTSIZE2 * SIZEOF(FAST_FLOAT));
nuclear@1 149 }
nuclear@1 150 fdtbl = fdct->float_divisors[qtblno];
nuclear@1 151 i = 0;
nuclear@1 152 for (row = 0; row < DCTSIZE; row++) {
nuclear@1 153 for (col = 0; col < DCTSIZE; col++) {
nuclear@1 154 fdtbl[i] = (FAST_FLOAT)
nuclear@1 155 (1.0 / (((double) qtbl->quantval[i] *
nuclear@1 156 aanscalefactor[row] * aanscalefactor[col] * 8.0)));
nuclear@1 157 i++;
nuclear@1 158 }
nuclear@1 159 }
nuclear@1 160 }
nuclear@1 161 break;
nuclear@1 162 #endif
nuclear@1 163 default:
nuclear@1 164 ERREXIT(cinfo, JERR_NOT_COMPILED);
nuclear@1 165 break;
nuclear@1 166 }
nuclear@1 167 }
nuclear@1 168 }
nuclear@1 169
nuclear@1 170
nuclear@1 171 /*
nuclear@1 172 * Perform forward DCT on one or more blocks of a component.
nuclear@1 173 *
nuclear@1 174 * The input samples are taken from the sample_data[] array starting at
nuclear@1 175 * position start_row/start_col, and moving to the right for any additional
nuclear@1 176 * blocks. The quantized coefficients are returned in coef_blocks[].
nuclear@1 177 */
nuclear@1 178
nuclear@1 179 METHODDEF(void)
nuclear@1 180 forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
nuclear@1 181 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
nuclear@1 182 JDIMENSION start_row, JDIMENSION start_col,
nuclear@1 183 JDIMENSION num_blocks)
nuclear@1 184 /* This version is used for integer DCT implementations. */
nuclear@1 185 {
nuclear@1 186 /* This routine is heavily used, so it's worth coding it tightly. */
nuclear@1 187 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
nuclear@1 188 forward_DCT_method_ptr do_dct = fdct->do_dct;
nuclear@1 189 DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
nuclear@1 190 DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
nuclear@1 191 JDIMENSION bi;
nuclear@1 192
nuclear@1 193 sample_data += start_row; /* fold in the vertical offset once */
nuclear@1 194
nuclear@1 195 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
nuclear@1 196 /* Load data into workspace, applying unsigned->signed conversion */
nuclear@1 197 { register DCTELEM *workspaceptr;
nuclear@1 198 register JSAMPROW elemptr;
nuclear@1 199 register int elemr;
nuclear@1 200
nuclear@1 201 workspaceptr = workspace;
nuclear@1 202 for (elemr = 0; elemr < DCTSIZE; elemr++) {
nuclear@1 203 elemptr = sample_data[elemr] + start_col;
nuclear@1 204 #if DCTSIZE == 8 /* unroll the inner loop */
nuclear@1 205 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 206 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 207 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 208 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 209 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 210 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 211 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 212 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 213 #else
nuclear@1 214 { register int elemc;
nuclear@1 215 for (elemc = DCTSIZE; elemc > 0; elemc--) {
nuclear@1 216 *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
nuclear@1 217 }
nuclear@1 218 }
nuclear@1 219 #endif
nuclear@1 220 }
nuclear@1 221 }
nuclear@1 222
nuclear@1 223 /* Perform the DCT */
nuclear@1 224 (*do_dct) (workspace);
nuclear@1 225
nuclear@1 226 /* Quantize/descale the coefficients, and store into coef_blocks[] */
nuclear@1 227 { register DCTELEM temp, qval;
nuclear@1 228 register int i;
nuclear@1 229 register JCOEFPTR output_ptr = coef_blocks[bi];
nuclear@1 230
nuclear@1 231 for (i = 0; i < DCTSIZE2; i++) {
nuclear@1 232 qval = divisors[i];
nuclear@1 233 temp = workspace[i];
nuclear@1 234 /* Divide the coefficient value by qval, ensuring proper rounding.
nuclear@1 235 * Since C does not specify the direction of rounding for negative
nuclear@1 236 * quotients, we have to force the dividend positive for portability.
nuclear@1 237 *
nuclear@1 238 * In most files, at least half of the output values will be zero
nuclear@1 239 * (at default quantization settings, more like three-quarters...)
nuclear@1 240 * so we should ensure that this case is fast. On many machines,
nuclear@1 241 * a comparison is enough cheaper than a divide to make a special test
nuclear@1 242 * a win. Since both inputs will be nonnegative, we need only test
nuclear@1 243 * for a < b to discover whether a/b is 0.
nuclear@1 244 * If your machine's division is fast enough, define FAST_DIVIDE.
nuclear@1 245 */
nuclear@1 246 #ifdef FAST_DIVIDE
nuclear@1 247 #define DIVIDE_BY(a,b) a /= b
nuclear@1 248 #else
nuclear@1 249 #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
nuclear@1 250 #endif
nuclear@1 251 if (temp < 0) {
nuclear@1 252 temp = -temp;
nuclear@1 253 temp += qval>>1; /* for rounding */
nuclear@1 254 DIVIDE_BY(temp, qval);
nuclear@1 255 temp = -temp;
nuclear@1 256 } else {
nuclear@1 257 temp += qval>>1; /* for rounding */
nuclear@1 258 DIVIDE_BY(temp, qval);
nuclear@1 259 }
nuclear@1 260 output_ptr[i] = (JCOEF) temp;
nuclear@1 261 }
nuclear@1 262 }
nuclear@1 263 }
nuclear@1 264 }
nuclear@1 265
nuclear@1 266
nuclear@1 267 #ifdef DCT_FLOAT_SUPPORTED
nuclear@1 268
nuclear@1 269 METHODDEF(void)
nuclear@1 270 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
nuclear@1 271 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
nuclear@1 272 JDIMENSION start_row, JDIMENSION start_col,
nuclear@1 273 JDIMENSION num_blocks)
nuclear@1 274 /* This version is used for floating-point DCT implementations. */
nuclear@1 275 {
nuclear@1 276 /* This routine is heavily used, so it's worth coding it tightly. */
nuclear@1 277 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
nuclear@1 278 float_DCT_method_ptr do_dct = fdct->do_float_dct;
nuclear@1 279 FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
nuclear@1 280 FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
nuclear@1 281 JDIMENSION bi;
nuclear@1 282
nuclear@1 283 sample_data += start_row; /* fold in the vertical offset once */
nuclear@1 284
nuclear@1 285 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
nuclear@1 286 /* Load data into workspace, applying unsigned->signed conversion */
nuclear@1 287 { register FAST_FLOAT *workspaceptr;
nuclear@1 288 register JSAMPROW elemptr;
nuclear@1 289 register int elemr;
nuclear@1 290
nuclear@1 291 workspaceptr = workspace;
nuclear@1 292 for (elemr = 0; elemr < DCTSIZE; elemr++) {
nuclear@1 293 elemptr = sample_data[elemr] + start_col;
nuclear@1 294 #if DCTSIZE == 8 /* unroll the inner loop */
nuclear@1 295 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 296 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 297 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 298 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 299 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 300 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 301 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 302 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 303 #else
nuclear@1 304 { register int elemc;
nuclear@1 305 for (elemc = DCTSIZE; elemc > 0; elemc--) {
nuclear@1 306 *workspaceptr++ = (FAST_FLOAT)
nuclear@1 307 (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
nuclear@1 308 }
nuclear@1 309 }
nuclear@1 310 #endif
nuclear@1 311 }
nuclear@1 312 }
nuclear@1 313
nuclear@1 314 /* Perform the DCT */
nuclear@1 315 (*do_dct) (workspace);
nuclear@1 316
nuclear@1 317 /* Quantize/descale the coefficients, and store into coef_blocks[] */
nuclear@1 318 { register FAST_FLOAT temp;
nuclear@1 319 register int i;
nuclear@1 320 register JCOEFPTR output_ptr = coef_blocks[bi];
nuclear@1 321
nuclear@1 322 for (i = 0; i < DCTSIZE2; i++) {
nuclear@1 323 /* Apply the quantization and scaling factor */
nuclear@1 324 temp = workspace[i] * divisors[i];
nuclear@1 325 /* Round to nearest integer.
nuclear@1 326 * Since C does not specify the direction of rounding for negative
nuclear@1 327 * quotients, we have to force the dividend positive for portability.
nuclear@1 328 * The maximum coefficient size is +-16K (for 12-bit data), so this
nuclear@1 329 * code should work for either 16-bit or 32-bit ints.
nuclear@1 330 */
nuclear@1 331 output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
nuclear@1 332 }
nuclear@1 333 }
nuclear@1 334 }
nuclear@1 335 }
nuclear@1 336
nuclear@1 337 #endif /* DCT_FLOAT_SUPPORTED */
nuclear@1 338
nuclear@1 339
nuclear@1 340 /*
nuclear@1 341 * Initialize FDCT manager.
nuclear@1 342 */
nuclear@1 343
nuclear@1 344 GLOBAL(void)
nuclear@1 345 jinit_forward_dct (j_compress_ptr cinfo)
nuclear@1 346 {
nuclear@1 347 my_fdct_ptr fdct;
nuclear@1 348 int i;
nuclear@1 349
nuclear@1 350 fdct = (my_fdct_ptr)
nuclear@1 351 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
nuclear@1 352 SIZEOF(my_fdct_controller));
nuclear@1 353 cinfo->fdct = (struct jpeg_forward_dct *) fdct;
nuclear@1 354 fdct->pub.start_pass = start_pass_fdctmgr;
nuclear@1 355
nuclear@1 356 switch (cinfo->dct_method) {
nuclear@1 357 #ifdef DCT_ISLOW_SUPPORTED
nuclear@1 358 case JDCT_ISLOW:
nuclear@1 359 fdct->pub.forward_DCT = forward_DCT;
nuclear@1 360 fdct->do_dct = jpeg_fdct_islow;
nuclear@1 361 break;
nuclear@1 362 #endif
nuclear@1 363 #ifdef DCT_IFAST_SUPPORTED
nuclear@1 364 case JDCT_IFAST:
nuclear@1 365 fdct->pub.forward_DCT = forward_DCT;
nuclear@1 366 fdct->do_dct = jpeg_fdct_ifast;
nuclear@1 367 break;
nuclear@1 368 #endif
nuclear@1 369 #ifdef DCT_FLOAT_SUPPORTED
nuclear@1 370 case JDCT_FLOAT:
nuclear@1 371 fdct->pub.forward_DCT = forward_DCT_float;
nuclear@1 372 fdct->do_float_dct = jpeg_fdct_float;
nuclear@1 373 break;
nuclear@1 374 #endif
nuclear@1 375 default:
nuclear@1 376 ERREXIT(cinfo, JERR_NOT_COMPILED);
nuclear@1 377 break;
nuclear@1 378 }
nuclear@1 379
nuclear@1 380 /* Mark divisor tables unallocated */
nuclear@1 381 for (i = 0; i < NUM_QUANT_TBLS; i++) {
nuclear@1 382 fdct->divisors[i] = NULL;
nuclear@1 383 #ifdef DCT_FLOAT_SUPPORTED
nuclear@1 384 fdct->float_divisors[i] = NULL;
nuclear@1 385 #endif
nuclear@1 386 }
nuclear@1 387 }