Shoaib Ahmed / Mbed 2 deprecated uzairkhan

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jcdctmgr.c Source File

jcdctmgr.c

00001 /*
00002  * jcdctmgr.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * Modified 2003-2013 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains the forward-DCT management logic.
00010  * This code selects a particular DCT implementation to be used,
00011  * and it performs related housekeeping chores including coefficient
00012  * quantization.
00013  */
00014 
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018 #include "jdct.h"       /* Private declarations for DCT subsystem */
00019 
00020 
00021 /* Private subobject for this module */
00022 
00023 typedef struct {
00024   struct jpeg_forward_dct pub;  /* public fields */
00025 
00026   /* Pointer to the DCT routine actually in use */
00027   forward_DCT_method_ptr do_dct[MAX_COMPONENTS];
00028 
00029 #ifdef DCT_FLOAT_SUPPORTED
00030   /* Same as above for the floating-point case. */
00031   float_DCT_method_ptr do_float_dct[MAX_COMPONENTS];
00032 #endif
00033 } my_fdct_controller;
00034 
00035 typedef my_fdct_controller * my_fdct_ptr;
00036 
00037 
00038 /* The allocated post-DCT divisor tables -- big enough for any
00039  * supported variant and not identical to the quant table entries,
00040  * because of scaling (especially for an unnormalized DCT) --
00041  * are pointed to by dct_table in the per-component comp_info
00042  * structures.  Each table is given in normal array order.
00043  */
00044 
00045 typedef union {
00046   DCTELEM int_array[DCTSIZE2];
00047 #ifdef DCT_FLOAT_SUPPORTED
00048   FAST_FLOAT float_array[DCTSIZE2];
00049 #endif
00050 } divisor_table;
00051 
00052 
00053 /* The current scaled-DCT routines require ISLOW-style divisor tables,
00054  * so be sure to compile that code if either ISLOW or SCALING is requested.
00055  */
00056 #ifdef DCT_ISLOW_SUPPORTED
00057 #define PROVIDE_ISLOW_TABLES
00058 #else
00059 #ifdef DCT_SCALING_SUPPORTED
00060 #define PROVIDE_ISLOW_TABLES
00061 #endif
00062 #endif
00063 
00064 
00065 /*
00066  * Perform forward DCT on one or more blocks of a component.
00067  *
00068  * The input samples are taken from the sample_data[] array starting at
00069  * position start_row/start_col, and moving to the right for any additional
00070  * blocks. The quantized coefficients are returned in coef_blocks[].
00071  */
00072 
00073 METHODDEF(void)
00074 forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
00075          JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
00076          JDIMENSION start_row, JDIMENSION start_col,
00077          JDIMENSION num_blocks)
00078 /* This version is used for integer DCT implementations. */
00079 {
00080   /* This routine is heavily used, so it's worth coding it tightly. */
00081   my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
00082   forward_DCT_method_ptr do_dct = fdct->do_dct[compptr->component_index];
00083   DCTELEM * divisors = (DCTELEM *) compptr->dct_table;
00084   DCTELEM workspace[DCTSIZE2];  /* work area for FDCT subroutine */
00085   JDIMENSION bi;
00086 
00087   sample_data += start_row; /* fold in the vertical offset once */
00088 
00089   for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
00090     /* Perform the DCT */
00091     (*do_dct) (workspace, sample_data, start_col);
00092 
00093     /* Quantize/descale the coefficients, and store into coef_blocks[] */
00094     { register DCTELEM temp, qval;
00095       register int i;
00096       register JCOEFPTR output_ptr = coef_blocks[bi];
00097 
00098       for (i = 0; i < DCTSIZE2; i++) {
00099     qval = divisors[i];
00100     temp = workspace[i];
00101     /* Divide the coefficient value by qval, ensuring proper rounding.
00102      * Since C does not specify the direction of rounding for negative
00103      * quotients, we have to force the dividend positive for portability.
00104      *
00105      * In most files, at least half of the output values will be zero
00106      * (at default quantization settings, more like three-quarters...)
00107      * so we should ensure that this case is fast.  On many machines,
00108      * a comparison is enough cheaper than a divide to make a special test
00109      * a win.  Since both inputs will be nonnegative, we need only test
00110      * for a < b to discover whether a/b is 0.
00111      * If your machine's division is fast enough, define FAST_DIVIDE.
00112      */
00113 #ifdef FAST_DIVIDE
00114 #define DIVIDE_BY(a,b)  a /= b
00115 #else
00116 #define DIVIDE_BY(a,b)  if (a >= b) a /= b; else a = 0
00117 #endif
00118     if (temp < 0) {
00119       temp = -temp;
00120       temp += qval>>1;  /* for rounding */
00121       DIVIDE_BY(temp, qval);
00122       temp = -temp;
00123     } else {
00124       temp += qval>>1;  /* for rounding */
00125       DIVIDE_BY(temp, qval);
00126     }
00127     output_ptr[i] = (JCOEF) temp;
00128       }
00129     }
00130   }
00131 }
00132 
00133 
00134 #ifdef DCT_FLOAT_SUPPORTED
00135 
00136 METHODDEF(void)
00137 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
00138            JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
00139            JDIMENSION start_row, JDIMENSION start_col,
00140            JDIMENSION num_blocks)
00141 /* This version is used for floating-point DCT implementations. */
00142 {
00143   /* This routine is heavily used, so it's worth coding it tightly. */
00144   my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
00145   float_DCT_method_ptr do_dct = fdct->do_float_dct[compptr->component_index];
00146   FAST_FLOAT * divisors = (FAST_FLOAT *) compptr->dct_table;
00147   FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
00148   JDIMENSION bi;
00149 
00150   sample_data += start_row; /* fold in the vertical offset once */
00151 
00152   for (bi = 0; bi < num_blocks; bi++, start_col += compptr->DCT_h_scaled_size) {
00153     /* Perform the DCT */
00154     (*do_dct) (workspace, sample_data, start_col);
00155 
00156     /* Quantize/descale the coefficients, and store into coef_blocks[] */
00157     { register FAST_FLOAT temp;
00158       register int i;
00159       register JCOEFPTR output_ptr = coef_blocks[bi];
00160 
00161       for (i = 0; i < DCTSIZE2; i++) {
00162     /* Apply the quantization and scaling factor */
00163     temp = workspace[i] * divisors[i];
00164     /* Round to nearest integer.
00165      * Since C does not specify the direction of rounding for negative
00166      * quotients, we have to force the dividend positive for portability.
00167      * The maximum coefficient size is +-16K (for 12-bit data), so this
00168      * code should work for either 16-bit or 32-bit ints.
00169      */
00170     output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
00171       }
00172     }
00173   }
00174 }
00175 
00176 #endif /* DCT_FLOAT_SUPPORTED */
00177 
00178 
00179 /*
00180  * Initialize for a processing pass.
00181  * Verify that all referenced Q-tables are present, and set up
00182  * the divisor table for each one.
00183  * In the current implementation, DCT of all components is done during
00184  * the first pass, even if only some components will be output in the
00185  * first scan.  Hence all components should be examined here.
00186  */
00187 
00188 METHODDEF(void)
00189 start_pass_fdctmgr (j_compress_ptr cinfo)
00190 {
00191   my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
00192   int ci, qtblno, i;
00193   jpeg_component_info *compptr;
00194   int method = 0;
00195   JQUANT_TBL * qtbl;
00196   DCTELEM * dtbl;
00197 
00198   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00199        ci++, compptr++) {
00200     /* Select the proper DCT routine for this component's scaling */
00201     switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) {
00202 #ifdef DCT_SCALING_SUPPORTED
00203     case ((1 << 8) + 1):
00204       fdct->do_dct[ci] = jpeg_fdct_1x1;
00205       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00206       break;
00207     case ((2 << 8) + 2):
00208       fdct->do_dct[ci] = jpeg_fdct_2x2;
00209       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00210       break;
00211     case ((3 << 8) + 3):
00212       fdct->do_dct[ci] = jpeg_fdct_3x3;
00213       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00214       break;
00215     case ((4 << 8) + 4):
00216       fdct->do_dct[ci] = jpeg_fdct_4x4;
00217       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00218       break;
00219     case ((5 << 8) + 5):
00220       fdct->do_dct[ci] = jpeg_fdct_5x5;
00221       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00222       break;
00223     case ((6 << 8) + 6):
00224       fdct->do_dct[ci] = jpeg_fdct_6x6;
00225       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00226       break;
00227     case ((7 << 8) + 7):
00228       fdct->do_dct[ci] = jpeg_fdct_7x7;
00229       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00230       break;
00231     case ((9 << 8) + 9):
00232       fdct->do_dct[ci] = jpeg_fdct_9x9;
00233       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00234       break;
00235     case ((10 << 8) + 10):
00236       fdct->do_dct[ci] = jpeg_fdct_10x10;
00237       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00238       break;
00239     case ((11 << 8) + 11):
00240       fdct->do_dct[ci] = jpeg_fdct_11x11;
00241       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00242       break;
00243     case ((12 << 8) + 12):
00244       fdct->do_dct[ci] = jpeg_fdct_12x12;
00245       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00246       break;
00247     case ((13 << 8) + 13):
00248       fdct->do_dct[ci] = jpeg_fdct_13x13;
00249       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00250       break;
00251     case ((14 << 8) + 14):
00252       fdct->do_dct[ci] = jpeg_fdct_14x14;
00253       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00254       break;
00255     case ((15 << 8) + 15):
00256       fdct->do_dct[ci] = jpeg_fdct_15x15;
00257       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00258       break;
00259     case ((16 << 8) + 16):
00260       fdct->do_dct[ci] = jpeg_fdct_16x16;
00261       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00262       break;
00263     case ((16 << 8) + 8):
00264       fdct->do_dct[ci] = jpeg_fdct_16x8;
00265       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00266       break;
00267     case ((14 << 8) + 7):
00268       fdct->do_dct[ci] = jpeg_fdct_14x7;
00269       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00270       break;
00271     case ((12 << 8) + 6):
00272       fdct->do_dct[ci] = jpeg_fdct_12x6;
00273       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00274       break;
00275     case ((10 << 8) + 5):
00276       fdct->do_dct[ci] = jpeg_fdct_10x5;
00277       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00278       break;
00279     case ((8 << 8) + 4):
00280       fdct->do_dct[ci] = jpeg_fdct_8x4;
00281       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00282       break;
00283     case ((6 << 8) + 3):
00284       fdct->do_dct[ci] = jpeg_fdct_6x3;
00285       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00286       break;
00287     case ((4 << 8) + 2):
00288       fdct->do_dct[ci] = jpeg_fdct_4x2;
00289       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00290       break;
00291     case ((2 << 8) + 1):
00292       fdct->do_dct[ci] = jpeg_fdct_2x1;
00293       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00294       break;
00295     case ((8 << 8) + 16):
00296       fdct->do_dct[ci] = jpeg_fdct_8x16;
00297       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00298       break;
00299     case ((7 << 8) + 14):
00300       fdct->do_dct[ci] = jpeg_fdct_7x14;
00301       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00302       break;
00303     case ((6 << 8) + 12):
00304       fdct->do_dct[ci] = jpeg_fdct_6x12;
00305       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00306       break;
00307     case ((5 << 8) + 10):
00308       fdct->do_dct[ci] = jpeg_fdct_5x10;
00309       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00310       break;
00311     case ((4 << 8) + 8):
00312       fdct->do_dct[ci] = jpeg_fdct_4x8;
00313       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00314       break;
00315     case ((3 << 8) + 6):
00316       fdct->do_dct[ci] = jpeg_fdct_3x6;
00317       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00318       break;
00319     case ((2 << 8) + 4):
00320       fdct->do_dct[ci] = jpeg_fdct_2x4;
00321       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00322       break;
00323     case ((1 << 8) + 2):
00324       fdct->do_dct[ci] = jpeg_fdct_1x2;
00325       method = JDCT_ISLOW;  /* jfdctint uses islow-style table */
00326       break;
00327 #endif
00328     case ((DCTSIZE << 8) + DCTSIZE):
00329       switch (cinfo->dct_method) {
00330 #ifdef DCT_ISLOW_SUPPORTED
00331       case JDCT_ISLOW:
00332     fdct->do_dct[ci] = jpeg_fdct_islow;
00333     method = JDCT_ISLOW;
00334     break;
00335 #endif
00336 #ifdef DCT_IFAST_SUPPORTED
00337       case JDCT_IFAST:
00338     fdct->do_dct[ci] = jpeg_fdct_ifast;
00339     method = JDCT_IFAST;
00340     break;
00341 #endif
00342 #ifdef DCT_FLOAT_SUPPORTED
00343       case JDCT_FLOAT:
00344     fdct->do_float_dct[ci] = jpeg_fdct_float;
00345     method = JDCT_FLOAT;
00346     break;
00347 #endif
00348       default:
00349     ERREXIT(cinfo, JERR_NOT_COMPILED);
00350     break;
00351       }
00352       break;
00353     default:
00354       ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
00355            compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
00356       break;
00357     }
00358     qtblno = compptr->quant_tbl_no;
00359     /* Make sure specified quantization table is present */
00360     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
00361     cinfo->quant_tbl_ptrs[qtblno] == NULL)
00362       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
00363     qtbl = cinfo->quant_tbl_ptrs[qtblno];
00364     /* Create divisor table from quant table */
00365     switch (method) {
00366 #ifdef PROVIDE_ISLOW_TABLES
00367     case JDCT_ISLOW:
00368       /* For LL&M IDCT method, divisors are equal to raw quantization
00369        * coefficients multiplied by 8 (to counteract scaling).
00370        */
00371       dtbl = (DCTELEM *) compptr->dct_table;
00372       for (i = 0; i < DCTSIZE2; i++) {
00373     dtbl[i] =
00374       ((DCTELEM) qtbl->quantval[i]) << (compptr->component_needed ? 4 : 3);
00375       }
00376       fdct->pub.forward_DCT[ci] = forward_DCT;
00377       break;
00378 #endif
00379 #ifdef DCT_IFAST_SUPPORTED
00380     case JDCT_IFAST:
00381       {
00382     /* For AA&N IDCT method, divisors are equal to quantization
00383      * coefficients scaled by scalefactor[row]*scalefactor[col], where
00384      *   scalefactor[0] = 1
00385      *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
00386      * We apply a further scale factor of 8.
00387      */
00388 #define CONST_BITS 14
00389     static const INT16 aanscales[DCTSIZE2] = {
00390       /* precomputed values scaled up by 14 bits */
00391       16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
00392       22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
00393       21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
00394       19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
00395       16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
00396       12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
00397        8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
00398        4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
00399     };
00400     SHIFT_TEMPS
00401 
00402     dtbl = (DCTELEM *) compptr->dct_table;
00403     for (i = 0; i < DCTSIZE2; i++) {
00404       dtbl[i] = (DCTELEM)
00405         DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
00406                   (INT32) aanscales[i]),
00407             compptr->component_needed ? CONST_BITS-4 : CONST_BITS-3);
00408     }
00409       }
00410       fdct->pub.forward_DCT[ci] = forward_DCT;
00411       break;
00412 #endif
00413 #ifdef DCT_FLOAT_SUPPORTED
00414     case JDCT_FLOAT:
00415       {
00416     /* For float AA&N IDCT method, divisors are equal to quantization
00417      * coefficients scaled by scalefactor[row]*scalefactor[col], where
00418      *   scalefactor[0] = 1
00419      *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
00420      * We apply a further scale factor of 8.
00421      * What's actually stored is 1/divisor so that the inner loop can
00422      * use a multiplication rather than a division.
00423      */
00424     FAST_FLOAT * fdtbl = (FAST_FLOAT *) compptr->dct_table;
00425     int row, col;
00426     static const double aanscalefactor[DCTSIZE] = {
00427       1.0, 1.387039845, 1.306562965, 1.175875602,
00428       1.0, 0.785694958, 0.541196100, 0.275899379
00429     };
00430 
00431     i = 0;
00432     for (row = 0; row < DCTSIZE; row++) {
00433       for (col = 0; col < DCTSIZE; col++) {
00434         fdtbl[i] = (FAST_FLOAT)
00435           (1.0 / ((double) qtbl->quantval[i] *
00436               aanscalefactor[row] * aanscalefactor[col] *
00437               (compptr->component_needed ? 16.0 : 8.0)));
00438         i++;
00439       }
00440     }
00441       }
00442       fdct->pub.forward_DCT[ci] = forward_DCT_float;
00443       break;
00444 #endif
00445     default:
00446       ERREXIT(cinfo, JERR_NOT_COMPILED);
00447       break;
00448     }
00449   }
00450 }
00451 
00452 
00453 /*
00454  * Initialize FDCT manager.
00455  */
00456 
00457 GLOBAL(void)
00458 jinit_forward_dct (j_compress_ptr cinfo)
00459 {
00460   my_fdct_ptr fdct;
00461   int ci;
00462   jpeg_component_info *compptr;
00463 
00464   fdct = (my_fdct_ptr)
00465     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00466                 SIZEOF(my_fdct_controller));
00467   cinfo->fdct = &fdct->pub;
00468   fdct->pub.start_pass = start_pass_fdctmgr;
00469 
00470   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00471        ci++, compptr++) {
00472     /* Allocate a divisor table for each component */
00473     compptr->dct_table =
00474       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00475                   SIZEOF(divisor_table));
00476   }
00477 }