1  /********************************************************************** <BR>


2  This file is part of Crack dot Com's free source code release of


3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for


4  information about compiling & licensing issues visit this URL</a>


5  <PRE> If that doesn't help, contact Jonathan Clark at


6  golgotha_source@usa.net (Subject should have "GOLG" in it)


7  ***********************************************************************/


8 


9  /*


10  * jcdctmgr.c


11  *


12  * Copyright (C) 19941996, Thomas G. Lane.


13  * This file is part of the Independent JPEG Group's software.


14  * For conditions of distribution and use, see the accompanying README file.


15  *


16  * This file contains the forwardDCT management logic.


17  * This code selects a particular DCT implementation to be used,


18  * and it performs related housekeeping chores including coefficient


19  * quantization.


20  */


21 


22  #define JPEG_INTERNALS


23  #include "loaders/jpg/jinclude.h"


24  #include "loaders/jpg/jpeglib.h"


25  #include "loaders/jpg/jdct.h" /* Private declarations for DCT subsystem */


26 


27 


28  /* Private subobject for this module */


29 


30  typedef struct {


31  struct jpeg_forward_dct pub; /* public fields */


32 


33  /* Pointer to the DCT routine actually in use */


34  forward_DCT_method_ptr do_dct;


35 


36  /* The actual postDCT divisors  not identical to the quant table


37  * entries, because of scaling (especially for an unnormalized DCT).


38  * Each table is given in normal array order.


39  */


40  DCTELEM * divisors[NUM_QUANT_TBLS];


41 


42  #ifdef DCT_FLOAT_SUPPORTED


43  /* Same as above for the floatingpoint case. */


44  float_DCT_method_ptr do_float_dct;


45  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];


46  #endif


47  } my_fdct_controller;


48 


49  typedef my_fdct_controller * my_fdct_ptr;


50 


51 


52  /*


53  * Initialize for a processing pass.


54  * Verify that all referenced Qtables are present, and set up


55  * the divisor table for each one.


56  * In the current implementation, DCT of all components is done during


57  * the first pass, even if only some components will be output in the


58  * first scan. Hence all components should be examined here.


59  */


60 


61  METHODDEF(void)


62  start_pass_fdctmgr (j_compress_ptr cinfo)


63  {


64  my_fdct_ptr fdct = (my_fdct_ptr) cinfo>fdct;


65  int ci, qtblno, i;


66  jpeg_component_info *compptr;


67  JQUANT_TBL * qtbl;


68  DCTELEM * dtbl;


69 


70  for (ci = 0, compptr = cinfo>comp_info; ci < cinfo>num_components;


71  ci++, compptr++) {


72  qtblno = compptr>quant_tbl_no;


73  /* Make sure specified quantization table is present */


74  if (qtblno < 0  qtblno >= NUM_QUANT_TBLS 


75  cinfo>quant_tbl_ptrs[qtblno] == NULL)


76  ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);


77  qtbl = cinfo>quant_tbl_ptrs[qtblno];


78  /* Compute divisors for this quant table */


79  /* We may do this more than once for same table, but it's not a big deal */


80  switch (cinfo>dct_method) {


81  #ifdef DCT_ISLOW_SUPPORTED


82  case JDCT_ISLOW:


83  /* For LL&M IDCT method, divisors are equal to raw quantization


84  * coefficients multiplied by 8 (to counteract scaling).


85  */


86  if (fdct>divisors[qtblno] == NULL) {


87  fdct>divisors[qtblno] = (DCTELEM *)


88  (*cinfo>mem>alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,


89  DCTSIZE2 * SIZEOF(DCTELEM));


90  }


91  dtbl = fdct>divisors[qtblno];


92  for (i = 0; i < DCTSIZE2; i++) {


93  dtbl[i] = ((DCTELEM) qtbl>quantval[i]) << 3;


94  }


95  break;


96  #endif


97  #ifdef DCT_IFAST_SUPPORTED


98  case JDCT_IFAST:


99  {


100  /* For AA&N IDCT method, divisors are equal to quantization


101  * coefficients scaled by scalefactor[row]*scalefactor[col], where


102  * scalefactor[0] = 1


103  * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7


104  * We apply a further scale factor of 8.


105  */


106  #define CONST_BITS 14


107  static const INT16 aanscales[DCTSIZE2] = {


108  /* precomputed values scaled up by 14 bits */


109  16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,


110  22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,


111  21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,


112  19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,


113  16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,


114  12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,


115  8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,


116  4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247


117  };


118  SHIFT_TEMPS


119 


120  if (fdct>divisors[qtblno] == NULL) {


121  fdct>divisors[qtblno] = (DCTELEM *)


122  (*cinfo>mem>alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,


123  DCTSIZE2 * SIZEOF(DCTELEM));


124  }


125  dtbl = fdct>divisors[qtblno];


126  for (i = 0; i < DCTSIZE2; i++) {


127  dtbl[i] = (DCTELEM)


128  DESCALE(MULTIPLY16V16((INT32) qtbl>quantval[i],


129  (INT32) aanscales[i]),


130  CONST_BITS3);


131  }


132  }


133  break;


134  #endif


135  #ifdef DCT_FLOAT_SUPPORTED


136  case JDCT_FLOAT:


137  {


138  /* For float AA&N IDCT method, divisors are equal to quantization


139  * coefficients scaled by scalefactor[row]*scalefactor[col], where


140  * scalefactor[0] = 1


141  * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7


142  * We apply a further scale factor of 8.


143  * What's actually stored is 1/divisor so that the inner loop can


144  * use a multiplication rather than a division.


145  */


146  FAST_FLOAT * fdtbl;


147  int row, col;


148  static const double aanscalefactor[DCTSIZE] = {


149  1.0, 1.387039845, 1.306562965, 1.175875602,


150  1.0, 0.785694958, 0.541196100, 0.275899379


151  };


152 


153  if (fdct>float_divisors[qtblno] == NULL) {


154  fdct>float_divisors[qtblno] = (FAST_FLOAT *)


155  (*cinfo>mem>alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,


156  DCTSIZE2 * SIZEOF(FAST_FLOAT));


157  }


158  fdtbl = fdct>float_divisors[qtblno];


159  i = 0;


160  for (row = 0; row < DCTSIZE; row++) {


161  for (col = 0; col < DCTSIZE; col++) {


162  fdtbl[i] = (FAST_FLOAT)


163  (1.0 / (((double) qtbl>quantval[i] *


164  aanscalefactor[row] * aanscalefactor[col] * 8.0)));


165  i++;


166  }


167  }


168  }


169  break;


170  #endif


171  default:


172  ERREXIT(cinfo, JERR_NOT_COMPILED);


173  break;


174  }


175  }


176  }


177 


178 


179  /*


180  * Perform forward DCT on one or more blocks of a component.


181  *


182  * The input samples are taken from the sample_data[] array starting at


183  * position start_row/start_col, and moving to the right for any additional


184  * blocks. The quantized coefficients are returned in coef_blocks[].


185  */


186 


187  METHODDEF(void)


188  forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,


189  JSAMPARRAY sample_data, JBLOCKROW coef_blocks,


190  JDIMENSION start_row, JDIMENSION start_col,


191  JDIMENSION num_blocks)


192  /* This version is used for integer DCT implementations. */


193  {


194  /* This routine is heavily used, so it's worth coding it tightly. */


195  my_fdct_ptr fdct = (my_fdct_ptr) cinfo>fdct;


196  forward_DCT_method_ptr do_dct = fdct>do_dct;


197  DCTELEM * divisors = fdct>divisors[compptr>quant_tbl_no];


198  DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */


199  JDIMENSION bi;


200 


201  sample_data += start_row; /* fold in the vertical offset once */


202 


203  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {


204  /* Load data into workspace, applying unsigned>signed conversion */


205  { register DCTELEM *workspaceptr;


206  register JSAMPROW elemptr;


207  register int elemr;


208 


209  workspaceptr = workspace;


210  for (elemr = 0; elemr < DCTSIZE; elemr++) {


211  elemptr = sample_data[elemr] + start_col;


212  #if DCTSIZE == 8 /* unroll the inner loop */


213  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


214  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


215  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


216  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


217  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


218  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


219  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


220  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


221  #else


222  { register int elemc;


223  for (elemc = DCTSIZE; elemc > 0; elemc) {


224  *workspaceptr++ = GETJSAMPLE(*elemptr++)  CENTERJSAMPLE;


225  }


226  }


227  #endif


228  }


229  }


230 


231  /* Perform the DCT */


232  (*do_dct) (workspace);


233 


234  /* Quantize/descale the coefficients, and store into coef_blocks[] */


235  { register DCTELEM temp, qval;


236  register int i;


237  register JCOEFPTR output_ptr = coef_blocks[bi];


238 


239  for (i = 0; i < DCTSIZE2; i++) {


240  qval = divisors[i];


241  temp = workspace[i];


242  /* Divide the coefficient value by qval, ensuring proper rounding.


243  * Since C does not specify the direction of rounding for negative


244  * quotients, we have to force the dividend positive for portability.


245  *


246  * In most files, at least half of the output values will be zero


247  * (at default quantization settings, more like threequarters...)


248  * so we should ensure that this case is fast. On many machines,


249  * a comparison is enough cheaper than a divide to make a special test


250  * a win. Since both inputs will be nonnegative, we need only test


251  * for a < b to discover whether a/b is 0.


252  * If your machine's division is fast enough, define FAST_DIVIDE.


253  */


254  #ifdef FAST_DIVIDE


255  #define DIVIDE_BY(a,b) a /= b


256  #else


257  #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0


258  #endif


259  if (temp < 0) {


260  temp = temp;


261  temp += qval>>1; /* for rounding */


262  DIVIDE_BY(temp, qval);


263  temp = temp;


264  } else {


265  temp += qval>>1; /* for rounding */


266  DIVIDE_BY(temp, qval);


267  }


268  output_ptr[i] = (JCOEF) temp;


269  }


270  }


271  }


272  }


273 


274 


275  #ifdef DCT_FLOAT_SUPPORTED


276 


277  METHODDEF(void)


278  forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,


279  JSAMPARRAY sample_data, JBLOCKROW coef_blocks,


280  JDIMENSION start_row, JDIMENSION start_col,


281  JDIMENSION num_blocks)


282  /* This version is used for floatingpoint DCT implementations. */


283  {


284  /* This routine is heavily used, so it's worth coding it tightly. */


285  my_fdct_ptr fdct = (my_fdct_ptr) cinfo>fdct;


286  float_DCT_method_ptr do_dct = fdct>do_float_dct;


287  FAST_FLOAT * divisors = fdct>float_divisors[compptr>quant_tbl_no];


288  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */


289  JDIMENSION bi;


290 


291  sample_data += start_row; /* fold in the vertical offset once */


292 


293  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {


294  /* Load data into workspace, applying unsigned>signed conversion */


295  { register FAST_FLOAT *workspaceptr;


296  register JSAMPROW elemptr;


297  register int elemr;


298 


299  workspaceptr = workspace;


300  for (elemr = 0; elemr < DCTSIZE; elemr++) {


301  elemptr = sample_data[elemr] + start_col;


302  #if DCTSIZE == 8 /* unroll the inner loop */


303  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


304  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


305  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


306  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


307  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


308  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


309  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


310  *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


311  #else


312  { register int elemc;


313  for (elemc = DCTSIZE; elemc > 0; elemc) {


314  *workspaceptr++ = (FAST_FLOAT)


315  (GETJSAMPLE(*elemptr++)  CENTERJSAMPLE);


316  }


317  }


318  #endif


319  }


320  }


321 


322  /* Perform the DCT */


323  (*do_dct) (workspace);


324 


325  /* Quantize/descale the coefficients, and store into coef_blocks[] */


326  { register FAST_FLOAT temp;


327  register int i;


328  register JCOEFPTR output_ptr = coef_blocks[bi];


329 


330  for (i = 0; i < DCTSIZE2; i++) {


331  /* Apply the quantization and scaling factor */


332  temp = workspace[i] * divisors[i];


333  /* Round to nearest integer.


334  * Since C does not specify the direction of rounding for negative


335  * quotients, we have to force the dividend positive for portability.


336  * The maximum coefficient size is +16K (for 12bit data), so this


337  * code should work for either 16bit or 32bit ints.


338  */


339  output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5)  16384);


340  }


341  }


342  }


343  }


344 


345  #endif /* DCT_FLOAT_SUPPORTED */


346 


347 


348  /*


349  * Initialize FDCT manager.


350  */


351 


352  GLOBAL(void)


353  jinit_forward_dct (j_compress_ptr cinfo)


354  {


355  my_fdct_ptr fdct;


356  int i;


357 


358  fdct = (my_fdct_ptr)


359  (*cinfo>mem>alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,


360  SIZEOF(my_fdct_controller));


361  cinfo>fdct = (struct jpeg_forward_dct *) fdct;


362  fdct>pub.start_pass = start_pass_fdctmgr;


363 


364  switch (cinfo>dct_method) {


365  #ifdef DCT_ISLOW_SUPPORTED


366  case JDCT_ISLOW:


367  fdct>pub.forward_DCT = forward_DCT;


368  fdct>do_dct = jpeg_fdct_islow;


369  break;


370  #endif


371  #ifdef DCT_IFAST_SUPPORTED


372  case JDCT_IFAST:


373  fdct>pub.forward_DCT = forward_DCT;


374  fdct>do_dct = jpeg_fdct_ifast;


375  break;


376  #endif


377  #ifdef DCT_FLOAT_SUPPORTED


378  case JDCT_FLOAT:


379  fdct>pub.forward_DCT = forward_DCT_float;


380  fdct>do_float_dct = jpeg_fdct_float;


381  break;


382  #endif


383  default:


384  ERREXIT(cinfo, JERR_NOT_COMPILED);


385  break;


386  }


387 


388  /* Mark divisor tables unallocated */


389  for (i = 0; i < NUM_QUANT_TBLS; i++) {


390  fdct>divisors[i] = NULL;


391  #ifdef DCT_FLOAT_SUPPORTED


392  fdct>float_divisors[i] = NULL;


393  #endif


394  }


395  }

