jcdctmgr.c~

来自「JPEG Image compression using IJG standar」· C~ 代码 · 共 674 行 · 第 1/2 页

C~
674
字号
/* * jcdctmgr.c * * Copyright (C) 1994-1996, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains the forward-DCT management logic. * This code selects a particular DCT implementation to be used, * and it performs related housekeeping chores including coefficient * quantization. *//* ************************************************ * * $Log: jcdctmgr.c,v $ * Revision 1.1  2000/05/18 15:40:50  jliang * Initial revision * * * * ************************************************ *//************************************************************************* Modification History:* Date       Programmer   Description* --------   ----------   --------------------------------------------* 05/18/00   Jie          Modified jinit_forward_dct* 06/28/00   Jie          Added support for lossless compression: *                              No quantization is used, and perfect reconstruction is thus guaranteed by lifting.************************************************************************/#define JPEG_INTERNALS#include "jinclude.h"#include "jpeglib.h"#include "jdct.h"		/* Private declarations for DCT subsystem *//****************************** Defined in cjpeg, Jie 062800*****************************/extern boolean   lossless_codec;int first_blk = 1;/* Private subobject for this module */typedef struct {  struct jpeg_forward_dct pub;	/* public fields */  /* Pointer to the DCT routine actually in use */  forward_DCT_method_ptr do_dct;  /* The actual post-DCT divisors --- not identical to the quant table   * entries, because of scaling (especially for an unnormalized DCT).   * Each table is given in normal array order.   */  DCTELEM * divisors[NUM_QUANT_TBLS];#ifdef DCT_FLOAT_SUPPORTED  /* Same as above for the floating-point case. */  float_DCT_method_ptr do_float_dct;  FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];#endif} my_fdct_controller;typedef my_fdct_controller * my_fdct_ptr;/* * Initialize for a processing pass. * Verify that all referenced Q-tables are present, and set up * the divisor table for each one. * In the current implementation, DCT of all components is done during * the first pass, even if only some components will be output in the * first scan.  Hence all components should be examined here. */METHODDEF(void)start_pass_fdctmgr (j_compress_ptr cinfo){  my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;  int ci, qtblno, i;  jpeg_component_info *compptr;  JQUANT_TBL * qtbl;  DCTELEM * dtbl;  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;       ci++, compptr++) {    qtblno = compptr->quant_tbl_no;    /* Make sure specified quantization table is present */    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||	cinfo->quant_tbl_ptrs[qtblno] == NULL)      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);    qtbl = cinfo->quant_tbl_ptrs[qtblno];    /* Compute divisors for this quant table */    /* We may do this more than once for same table, but it's not a big deal */    switch (cinfo->dct_method) {#ifdef DCT_ISLOW_SUPPORTED    case JDCT_ISLOW:      /* For LL&M IDCT method, divisors are equal to raw quantization       * coefficients multiplied by 8 (to counteract scaling).       */      if (fdct->divisors[qtblno] == NULL) {	fdct->divisors[qtblno] = (DCTELEM *)	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				      DCTSIZE2 * SIZEOF(DCTELEM));      }      dtbl = fdct->divisors[qtblno];      for (i = 0; i < DCTSIZE2; i++) {	dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;      }      break;#endif#ifdef DCT_IFAST_SUPPORTED    case JDCT_IFAST:      {	/* For AA&N IDCT method, divisors are equal to quantization	 * coefficients scaled by scalefactor[row]*scalefactor[col], where	 *   scalefactor[0] = 1	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7	 * We apply a further scale factor of 8.	 */#define CONST_BITS 14	static const INT16 aanscales[DCTSIZE2] = {	  /* precomputed values scaled up by 14 bits */	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247	};	SHIFT_TEMPS	if (fdct->divisors[qtblno] == NULL) {	  fdct->divisors[qtblno] = (DCTELEM *)	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,					DCTSIZE2 * SIZEOF(DCTELEM));	}	dtbl = fdct->divisors[qtblno];	for (i = 0; i < DCTSIZE2; i++) {	  dtbl[i] = (DCTELEM)	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],				  (INT32) aanscales[i]),		    CONST_BITS-3);	}      }      break;#endif#ifdef DCT_FLOAT_SUPPORTED    case JDCT_FLOAT:      {	/* For float AA&N IDCT method, divisors are equal to quantization	 * coefficients scaled by scalefactor[row]*scalefactor[col], where	 *   scalefactor[0] = 1	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7	 * We apply a further scale factor of 8.	 * What's actually stored is 1/divisor so that the inner loop can	 * use a multiplication rather than a division.	 */	FAST_FLOAT * fdtbl;	int row, col;	static const double aanscalefactor[DCTSIZE] = {	  1.0, 1.387039845, 1.306562965, 1.175875602,	  1.0, 0.785694958, 0.541196100, 0.275899379	};	if (fdct->float_divisors[qtblno] == NULL) {	  fdct->float_divisors[qtblno] = (FAST_FLOAT *)	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,					DCTSIZE2 * SIZEOF(FAST_FLOAT));	}	fdtbl = fdct->float_divisors[qtblno];	i = 0;	for (row = 0; row < DCTSIZE; row++) {	  for (col = 0; col < DCTSIZE; col++) {	    fdtbl[i] = (FAST_FLOAT)	      (1.0 / (((double) qtbl->quantval[i] *		       aanscalefactor[row] * aanscalefactor[col] * 8.0)));	    i++;	  }	}      }      break;#endif	  /* Jie 05/18/00 */#ifdef DCT_BIN_A1_SUPPORTED       case JDCT_BIN_A1:      {		int row, col;		/* bin_a1 forward 2D transform result * scale[i] *scale[j] / 4 = True DCT.		   Hence the modified Q table is: (Q0 / scale[i] / scale[j] * 4). */		static const double bin_a1_scalefactor[DCTSIZE] = {	 	  0.707106781, 1.019591158, 1.0823922, 1.202689774,	 	  1.414213562, 0.831469612, 0.923879532, 0.98078528		};			if (!lossless_codec) {		  /* skip quantization if lossless is specified. */		  if (fdct->divisors[qtblno] == NULL) {			fdct->divisors[qtblno] = (DCTELEM *)			  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				      DCTSIZE2 * SIZEOF(DCTELEM));		  }		  dtbl = fdct->divisors[qtblno];		  i = 0;		  for (row = 0; row < DCTSIZE; row++) {			for (col = 0; col < DCTSIZE; col++) {			  dtbl[i] = (DCTELEM) ( (double) qtbl->quantval[i] / bin_a1_scalefactor[row] / bin_a1_scalefactor[col] * 4 + 0.5);			  i++;			}		  }		}      }      break;#endif#ifdef DCT_BIN_B1_SUPPORTED    case JDCT_BIN_B1:   {		 int row, col;#define B1_CONST_BITS 13	 /* max number in scalign is 2, so scale 13 bits, instead of 14 */	static const double bin_b1_scalefactor[DCTSIZE] = {	  0.707106781, 1.019591158, 1.0823922, 1.202689774,	  1.414213562, 0.831469612, 0.923879532, 0.98078528 	};	if (TRUE != lossless_codec) {    /* skip quantization if lossless is specified. */	  if (fdct->divisors[qtblno] == NULL) {		fdct->divisors[qtblno] = (DCTELEM *)		  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,					DCTSIZE2 * SIZEOF(DCTELEM));	  }	  dtbl = fdct->divisors[qtblno];	  i = 0;	  for (row = 0; row < DCTSIZE; row++) {		for ( col = 0; col < DCTSIZE; col++) {		  dtbl[i] = (DCTELEM) (qtbl->quantval[i] / bin_b1_scalefactor[row] / bin_b1_scalefactor[col] * 4 + 0.5);		  i++;		}	  }	}  }  break;#endif#ifdef DCT_BIN_C1_SUPPORTED    case JDCT_BIN_C1:      {		int row, col;		/* bin_a1 forward 2D transform result * scale[i] *scale[j] / 4 = True DCT.		   Hence the modified Q table is: (Q0 / scale[i] / scale[j] * 4). */		static const double bin_c1_scalefactor[DCTSIZE] = {	 	  0.707106781, 1.019591158, 1.0823922, 1.202689774,	 	  1.414213562, 0.831469612, 0.923879532, 0.98078528		};			if (!lossless_codec) {		  /* skip quantization if lossless is specified. */		  if (fdct->divisors[qtblno] == NULL) {			fdct->divisors[qtblno] = (DCTELEM *)			  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				      DCTSIZE2 * SIZEOF(DCTELEM));		  }		  dtbl = fdct->divisors[qtblno];		  i = 0;		  for (row = 0; row < DCTSIZE; row++) {			for (col = 0; col < DCTSIZE; col++) {			  dtbl[i] = (DCTELEM) ( (double) qtbl->quantval[i] / bin_c1_scalefactor[row] / bin_c1_scalefactor[col] * 4 + 0.5);			  i++;			}		  }		}      }      break;#endif#ifdef DCT_BIN_L1_SUPPORTED    case JDCT_BIN_L1:      {		int row, col;		static const double bin_l1_scalefactor[DCTSIZE] = {		  0.35355339, 0.35355339, 0.5411961, 0.5,		  0.70710678, 0.5,        0.4619398, 0.70710678		};			if (!lossless_codec) {		  /* skip quantization if lossless is specified. */		  if (fdct->divisors[qtblno] == NULL) {			fdct->divisors[qtblno] = (DCTELEM *)			  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,				      DCTSIZE2 * SIZEOF(DCTELEM));		  }		  dtbl = fdct->divisors[qtblno];		  i = 0;		  for (row = 0; row < DCTSIZE; row++) {			for (col = 0; col < DCTSIZE; col++) {			  dtbl[i] = (DCTELEM) ( (double) qtbl->quantval[i] / bin_l1_scalefactor[row] / bin_l1_scalefactor[col] + 0.5);			  i++;			}		  }		}      }      break;#endif    default:      ERREXIT(cinfo, JERR_NOT_COMPILED);      break;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?