3dphotoshoot

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