vrshoot

annotate libs/libjpeg/jcdctmgr.c @ 3:c179c72369be

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