diff options
Diffstat (limited to 'src/libjpeg')
| -rw-r--r-- | src/libjpeg/jaricom.c | 148 | ||||
| -rw-r--r-- | src/libjpeg/jcarith.c | 921 | ||||
| -rw-r--r-- | src/libjpeg/jchuff.h | 47 | ||||
| -rw-r--r-- | src/libjpeg/jcphuff.c | 833 | ||||
| -rw-r--r-- | src/libjpeg/jdarith.c | 762 | ||||
| -rw-r--r-- | src/libjpeg/jdhuff.h | 201 | ||||
| -rw-r--r-- | src/libjpeg/jdphuff.c | 668 | ||||
| -rw-r--r-- | src/libjpeg/jidctred.c | 398 | 
8 files changed, 1831 insertions, 2147 deletions
| diff --git a/src/libjpeg/jaricom.c b/src/libjpeg/jaricom.c new file mode 100644 index 0000000..9e51ed5 --- /dev/null +++ b/src/libjpeg/jaricom.c @@ -0,0 +1,148 @@ +/* + * jaricom.c + * + * Developed 1997 by Guido Vollbeding. + * 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 probability estimation tables for common use in + * arithmetic entropy encoding and decoding routines. + * + * This data represents Table D.2 in the JPEG spec (ISO/IEC IS 10918-1 + * and CCITT Recommendation ITU-T T.81) and Table 24 in the JBIG spec + * (ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82). + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + +/* The following #define specifies the packing of the four components + * into the compact INT32 representation. + * Note that this formula must match the actual arithmetic encoder + * and decoder implementation.  The implementation has to be changed + * if this formula is changed. + * The current organization is leaned on Markus Kuhn's JBIG + * implementation (jbig_tab.c). + */ + +#define V(a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b) + +const INT32 jaritab[113] = { +/* + * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS + */ +/*   0 */  V( 0x5a1d,   1,   1, 1 ), +/*   1 */  V( 0x2586,  14,   2, 0 ), +/*   2 */  V( 0x1114,  16,   3, 0 ), +/*   3 */  V( 0x080b,  18,   4, 0 ), +/*   4 */  V( 0x03d8,  20,   5, 0 ), +/*   5 */  V( 0x01da,  23,   6, 0 ), +/*   6 */  V( 0x00e5,  25,   7, 0 ), +/*   7 */  V( 0x006f,  28,   8, 0 ), +/*   8 */  V( 0x0036,  30,   9, 0 ), +/*   9 */  V( 0x001a,  33,  10, 0 ), +/*  10 */  V( 0x000d,  35,  11, 0 ), +/*  11 */  V( 0x0006,   9,  12, 0 ), +/*  12 */  V( 0x0003,  10,  13, 0 ), +/*  13 */  V( 0x0001,  12,  13, 0 ), +/*  14 */  V( 0x5a7f,  15,  15, 1 ), +/*  15 */  V( 0x3f25,  36,  16, 0 ), +/*  16 */  V( 0x2cf2,  38,  17, 0 ), +/*  17 */  V( 0x207c,  39,  18, 0 ), +/*  18 */  V( 0x17b9,  40,  19, 0 ), +/*  19 */  V( 0x1182,  42,  20, 0 ), +/*  20 */  V( 0x0cef,  43,  21, 0 ), +/*  21 */  V( 0x09a1,  45,  22, 0 ), +/*  22 */  V( 0x072f,  46,  23, 0 ), +/*  23 */  V( 0x055c,  48,  24, 0 ), +/*  24 */  V( 0x0406,  49,  25, 0 ), +/*  25 */  V( 0x0303,  51,  26, 0 ), +/*  26 */  V( 0x0240,  52,  27, 0 ), +/*  27 */  V( 0x01b1,  54,  28, 0 ), +/*  28 */  V( 0x0144,  56,  29, 0 ), +/*  29 */  V( 0x00f5,  57,  30, 0 ), +/*  30 */  V( 0x00b7,  59,  31, 0 ), +/*  31 */  V( 0x008a,  60,  32, 0 ), +/*  32 */  V( 0x0068,  62,  33, 0 ), +/*  33 */  V( 0x004e,  63,  34, 0 ), +/*  34 */  V( 0x003b,  32,  35, 0 ), +/*  35 */  V( 0x002c,  33,   9, 0 ), +/*  36 */  V( 0x5ae1,  37,  37, 1 ), +/*  37 */  V( 0x484c,  64,  38, 0 ), +/*  38 */  V( 0x3a0d,  65,  39, 0 ), +/*  39 */  V( 0x2ef1,  67,  40, 0 ), +/*  40 */  V( 0x261f,  68,  41, 0 ), +/*  41 */  V( 0x1f33,  69,  42, 0 ), +/*  42 */  V( 0x19a8,  70,  43, 0 ), +/*  43 */  V( 0x1518,  72,  44, 0 ), +/*  44 */  V( 0x1177,  73,  45, 0 ), +/*  45 */  V( 0x0e74,  74,  46, 0 ), +/*  46 */  V( 0x0bfb,  75,  47, 0 ), +/*  47 */  V( 0x09f8,  77,  48, 0 ), +/*  48 */  V( 0x0861,  78,  49, 0 ), +/*  49 */  V( 0x0706,  79,  50, 0 ), +/*  50 */  V( 0x05cd,  48,  51, 0 ), +/*  51 */  V( 0x04de,  50,  52, 0 ), +/*  52 */  V( 0x040f,  50,  53, 0 ), +/*  53 */  V( 0x0363,  51,  54, 0 ), +/*  54 */  V( 0x02d4,  52,  55, 0 ), +/*  55 */  V( 0x025c,  53,  56, 0 ), +/*  56 */  V( 0x01f8,  54,  57, 0 ), +/*  57 */  V( 0x01a4,  55,  58, 0 ), +/*  58 */  V( 0x0160,  56,  59, 0 ), +/*  59 */  V( 0x0125,  57,  60, 0 ), +/*  60 */  V( 0x00f6,  58,  61, 0 ), +/*  61 */  V( 0x00cb,  59,  62, 0 ), +/*  62 */  V( 0x00ab,  61,  63, 0 ), +/*  63 */  V( 0x008f,  61,  32, 0 ), +/*  64 */  V( 0x5b12,  65,  65, 1 ), +/*  65 */  V( 0x4d04,  80,  66, 0 ), +/*  66 */  V( 0x412c,  81,  67, 0 ), +/*  67 */  V( 0x37d8,  82,  68, 0 ), +/*  68 */  V( 0x2fe8,  83,  69, 0 ), +/*  69 */  V( 0x293c,  84,  70, 0 ), +/*  70 */  V( 0x2379,  86,  71, 0 ), +/*  71 */  V( 0x1edf,  87,  72, 0 ), +/*  72 */  V( 0x1aa9,  87,  73, 0 ), +/*  73 */  V( 0x174e,  72,  74, 0 ), +/*  74 */  V( 0x1424,  72,  75, 0 ), +/*  75 */  V( 0x119c,  74,  76, 0 ), +/*  76 */  V( 0x0f6b,  74,  77, 0 ), +/*  77 */  V( 0x0d51,  75,  78, 0 ), +/*  78 */  V( 0x0bb6,  77,  79, 0 ), +/*  79 */  V( 0x0a40,  77,  48, 0 ), +/*  80 */  V( 0x5832,  80,  81, 1 ), +/*  81 */  V( 0x4d1c,  88,  82, 0 ), +/*  82 */  V( 0x438e,  89,  83, 0 ), +/*  83 */  V( 0x3bdd,  90,  84, 0 ), +/*  84 */  V( 0x34ee,  91,  85, 0 ), +/*  85 */  V( 0x2eae,  92,  86, 0 ), +/*  86 */  V( 0x299a,  93,  87, 0 ), +/*  87 */  V( 0x2516,  86,  71, 0 ), +/*  88 */  V( 0x5570,  88,  89, 1 ), +/*  89 */  V( 0x4ca9,  95,  90, 0 ), +/*  90 */  V( 0x44d9,  96,  91, 0 ), +/*  91 */  V( 0x3e22,  97,  92, 0 ), +/*  92 */  V( 0x3824,  99,  93, 0 ), +/*  93 */  V( 0x32b4,  99,  94, 0 ), +/*  94 */  V( 0x2e17,  93,  86, 0 ), +/*  95 */  V( 0x56a8,  95,  96, 1 ), +/*  96 */  V( 0x4f46, 101,  97, 0 ), +/*  97 */  V( 0x47e5, 102,  98, 0 ), +/*  98 */  V( 0x41cf, 103,  99, 0 ), +/*  99 */  V( 0x3c3d, 104, 100, 0 ), +/* 100 */  V( 0x375e,  99,  93, 0 ), +/* 101 */  V( 0x5231, 105, 102, 0 ), +/* 102 */  V( 0x4c0f, 106, 103, 0 ), +/* 103 */  V( 0x4639, 107, 104, 0 ), +/* 104 */  V( 0x415e, 103,  99, 0 ), +/* 105 */  V( 0x5627, 105, 106, 1 ), +/* 106 */  V( 0x50e7, 108, 107, 0 ), +/* 107 */  V( 0x4b85, 109, 103, 0 ), +/* 108 */  V( 0x5597, 110, 109, 0 ), +/* 109 */  V( 0x504f, 111, 107, 0 ), +/* 110 */  V( 0x5a10, 110, 111, 1 ), +/* 111 */  V( 0x5522, 112, 109, 0 ), +/* 112 */  V( 0x59eb, 112, 111, 1 ) +}; diff --git a/src/libjpeg/jcarith.c b/src/libjpeg/jcarith.c new file mode 100644 index 0000000..945a817 --- /dev/null +++ b/src/libjpeg/jcarith.c @@ -0,0 +1,921 @@ +/* + * jcarith.c + * + * Developed 1997 by Guido Vollbeding. + * 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 portable arithmetic entropy encoding routines for JPEG + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). + * + * Both sequential and progressive modes are supported in this single module. + * + * Suspension is not currently supported in this module. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Expanded entropy encoder object for arithmetic encoding. */ + +typedef struct { +  struct jpeg_entropy_encoder pub; /* public fields */ + +  INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */ +  INT32 a;               /* A register, normalized size of coding interval */ +  INT32 sc;        /* counter for stacked 0xFF values which might overflow */ +  INT32 zc;          /* counter for pending 0x00 output values which might * +                          * be discarded at the end ("Pacman" termination) */ +  int ct;  /* bit shift counter, determines when next byte will be written */ +  int buffer;                /* buffer for most recent output byte != 0xFF */ + +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ +  int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ + +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */ +  int next_restart_num;		/* next restart number to write (0-7) */ + +  /* Pointers to statistics areas (these workspaces have image lifespan) */ +  unsigned char * dc_stats[NUM_ARITH_TBLS]; +  unsigned char * ac_stats[NUM_ARITH_TBLS]; +} arith_entropy_encoder; + +typedef arith_entropy_encoder * arith_entropy_ptr; + +/* The following two definitions specify the allocation chunk size + * for the statistics area. + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least + * 49 statistics bins for DC, and 245 statistics bins for AC coding. + * Note that we use one additional AC bin for codings with fixed + * probability (0.5), thus the minimum number for AC is 246. + * + * We use a compact representation with 1 byte per statistics bin, + * thus the numbers directly represent byte sizes. + * This 1 byte per statistics bin contains the meaning of the MPS + * (more probable symbol) in the highest bit (mask 0x80), and the + * index into the probability estimation state machine table + * in the lower bits (mask 0x7F). + */ + +#define DC_STAT_BINS 64 +#define AC_STAT_BINS 256 + +/* NOTE: Uncomment the following #define if you want to use the + * given formula for calculating the AC conditioning parameter Kx + * for spectral selection progressive coding in section G.1.3.2 + * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4). + * Although the spec and P&M authors claim that this "has proven + * to give good results for 8 bit precision samples", I'm not + * convinced yet that this is really beneficial. + * Early tests gave only very marginal compression enhancements + * (a few - around 5 or so - bytes even for very large files), + * which would turn out rather negative if we'd suppress the + * DAC (Define Arithmetic Conditioning) marker segments for + * the default parameters in the future. + * Note that currently the marker writing module emits 12-byte + * DAC segments for a full-component scan in a color image. + * This is not worth worrying about IMHO. However, since the + * spec defines the default values to be used if the tables + * are omitted (unlike Huffman tables, which are required + * anyway), one might optimize this behaviour in the future, + * and then it would be disadvantageous to use custom tables if + * they don't provide sufficient gain to exceed the DAC size. + * + * On the other hand, I'd consider it as a reasonable result + * that the conditioning has no significant influence on the + * compression performance. This means that the basic + * statistical model is already rather stable. + * + * Thus, at the moment, we use the default conditioning values + * anyway, and do not use the custom formula. + * +#define CALCULATE_SPECTRAL_CONDITIONING + */ + +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. + * We assume that int right shift is unsigned if INT32 right shift is, + * which should be safe. + */ + +#ifdef RIGHT_SHIFT_IS_UNSIGNED +#define ISHIFT_TEMPS	int ishift_temp; +#define IRIGHT_SHIFT(x,shft)  \ +	((ishift_temp = (x)) < 0 ? \ +	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ +	 (ishift_temp >> (shft))) +#else +#define ISHIFT_TEMPS +#define IRIGHT_SHIFT(x,shft)	((x) >> (shft)) +#endif + + +LOCAL(void) +emit_byte (int val, j_compress_ptr cinfo) +/* Write next output byte; we do not support suspension in this module. */ +{ +  struct jpeg_destination_mgr * dest = cinfo->dest; + +  *dest->next_output_byte++ = (JOCTET) val; +  if (--dest->free_in_buffer == 0) +    if (! (*dest->empty_output_buffer) (cinfo)) +      ERREXIT(cinfo, JERR_CANT_SUSPEND); +} + + +/* + * Finish up at the end of an arithmetic-compressed scan. + */ + +METHODDEF(void) +finish_pass (j_compress_ptr cinfo) +{ +  arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; +  INT32 temp; + +  /* Section D.1.8: Termination of encoding */ + +  /* Find the e->c in the coding interval with the largest +   * number of trailing zero bits */ +  if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c) +    e->c = temp + 0x8000L; +  else +    e->c = temp; +  /* Send remaining bytes to output */ +  e->c <<= e->ct; +  if (e->c & 0xF8000000L) { +    /* One final overflow has to be handled */ +    if (e->buffer >= 0) { +      if (e->zc) +	do emit_byte(0x00, cinfo); +	while (--e->zc); +      emit_byte(e->buffer + 1, cinfo); +      if (e->buffer + 1 == 0xFF) +	emit_byte(0x00, cinfo); +    } +    e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */ +    e->sc = 0; +  } else { +    if (e->buffer == 0) +      ++e->zc; +    else if (e->buffer >= 0) { +      if (e->zc) +	do emit_byte(0x00, cinfo); +	while (--e->zc); +      emit_byte(e->buffer, cinfo); +    } +    if (e->sc) { +      if (e->zc) +	do emit_byte(0x00, cinfo); +	while (--e->zc); +      do { +	emit_byte(0xFF, cinfo); +	emit_byte(0x00, cinfo); +      } while (--e->sc); +    } +  } +  /* Output final bytes only if they are not 0x00 */ +  if (e->c & 0x7FFF800L) { +    if (e->zc)  /* output final pending zero bytes */ +      do emit_byte(0x00, cinfo); +      while (--e->zc); +    emit_byte((e->c >> 19) & 0xFF, cinfo); +    if (((e->c >> 19) & 0xFF) == 0xFF) +      emit_byte(0x00, cinfo); +    if (e->c & 0x7F800L) { +      emit_byte((e->c >> 11) & 0xFF, cinfo); +      if (((e->c >> 11) & 0xFF) == 0xFF) +	emit_byte(0x00, cinfo); +    } +  } +} + + +/* + * The core arithmetic encoding routine (common in JPEG and JBIG). + * This needs to go as fast as possible. + * Machine-dependent optimization facilities + * are not utilized in this portable implementation. + * However, this code should be fairly efficient and + * may be a good base for further optimizations anyway. + * + * Parameter 'val' to be encoded may be 0 or 1 (binary decision). + * + * Note: I've added full "Pacman" termination support to the + * byte output routines, which is equivalent to the optional + * Discard_final_zeros procedure (Figure D.15) in the spec. + * Thus, we always produce the shortest possible output + * stream compliant to the spec (no trailing zero bytes, + * except for FF stuffing). + * + * I've also introduced a new scheme for accessing + * the probability estimation state machine table, + * derived from Markus Kuhn's JBIG implementation. + */ + +LOCAL(void) +arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)  +{ +  extern const INT32 jaritab[]; +  register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; +  register unsigned char nl, nm; +  register INT32 qe, temp; +  register int sv; + +  /* Fetch values from our compact representation of Table D.2: +   * Qe values and probability estimation state machine +   */ +  sv = *st; +  qe = jaritab[sv & 0x7F];	/* => Qe_Value */ +  nl = qe & 0xFF; qe >>= 8;	/* Next_Index_LPS + Switch_MPS */ +  nm = qe & 0xFF; qe >>= 8;	/* Next_Index_MPS */ + +  /* Encode & estimation procedures per sections D.1.4 & D.1.5 */ +  e->a -= qe; +  if (val != (sv >> 7)) { +    /* Encode the less probable symbol */ +    if (e->a >= qe) { +      /* If the interval size (qe) for the less probable symbol (LPS) +       * is larger than the interval size for the MPS, then exchange +       * the two symbols for coding efficiency, otherwise code the LPS +       * as usual: */ +      e->c += e->a; +      e->a = qe; +    } +    *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */ +  } else { +    /* Encode the more probable symbol */ +    if (e->a >= 0x8000L) +      return;  /* A >= 0x8000 -> ready, no renormalization required */ +    if (e->a < qe) { +      /* If the interval size (qe) for the less probable symbol (LPS) +       * is larger than the interval size for the MPS, then exchange +       * the two symbols for coding efficiency: */ +      e->c += e->a; +      e->a = qe; +    } +    *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */ +  } + +  /* Renormalization & data output per section D.1.6 */ +  do { +    e->a <<= 1; +    e->c <<= 1; +    if (--e->ct == 0) { +      /* Another byte is ready for output */ +      temp = e->c >> 19; +      if (temp > 0xFF) { +	/* Handle overflow over all stacked 0xFF bytes */ +	if (e->buffer >= 0) { +	  if (e->zc) +	    do emit_byte(0x00, cinfo); +	    while (--e->zc); +	  emit_byte(e->buffer + 1, cinfo); +	  if (e->buffer + 1 == 0xFF) +	    emit_byte(0x00, cinfo); +	} +	e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */ +	e->sc = 0; +	/* Note: The 3 spacer bits in the C register guarantee +	 * that the new buffer byte can't be 0xFF here +	 * (see page 160 in the P&M JPEG book). */ +	e->buffer = temp & 0xFF;  /* new output byte, might overflow later */ +      } else if (temp == 0xFF) { +	++e->sc;  /* stack 0xFF byte (which might overflow later) */ +      } else { +	/* Output all stacked 0xFF bytes, they will not overflow any more */ +	if (e->buffer == 0) +	  ++e->zc; +	else if (e->buffer >= 0) { +	  if (e->zc) +	    do emit_byte(0x00, cinfo); +	    while (--e->zc); +	  emit_byte(e->buffer, cinfo); +	} +	if (e->sc) { +	  if (e->zc) +	    do emit_byte(0x00, cinfo); +	    while (--e->zc); +	  do { +	    emit_byte(0xFF, cinfo); +	    emit_byte(0x00, cinfo); +	  } while (--e->sc); +	} +	e->buffer = temp & 0xFF;  /* new output byte (can still overflow) */ +      } +      e->c &= 0x7FFFFL; +      e->ct += 8; +    } +  } while (e->a < 0x8000L); +} + + +/* + * Emit a restart marker & resynchronize predictions. + */ + +LOCAL(void) +emit_restart (j_compress_ptr cinfo, int restart_num) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  int ci; +  jpeg_component_info * compptr; + +  finish_pass(cinfo); + +  emit_byte(0xFF, cinfo); +  emit_byte(JPEG_RST0 + restart_num, cinfo); + +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { +    compptr = cinfo->cur_comp_info[ci]; +    /* Re-initialize statistics areas */ +    if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { +      MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); +      /* Reset DC predictions to 0 */ +      entropy->last_dc_val[ci] = 0; +      entropy->dc_context[ci] = 0; +    } +    if (cinfo->progressive_mode == 0 || cinfo->Ss) { +      MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); +    } +  } + +  /* Reset arithmetic encoding variables */ +  entropy->c = 0; +  entropy->a = 0x10000L; +  entropy->sc = 0; +  entropy->zc = 0; +  entropy->ct = 11; +  entropy->buffer = -1;  /* empty */ +} + + +/* + * MCU encoding for DC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  JBLOCKROW block; +  unsigned char *st; +  int blkn, ci, tbl; +  int v, v2, m; +  ISHIFT_TEMPS + +  /* Emit restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) { +      emit_restart(cinfo, entropy->next_restart_num); +      entropy->restarts_to_go = cinfo->restart_interval; +      entropy->next_restart_num++; +      entropy->next_restart_num &= 7; +    } +    entropy->restarts_to_go--; +  } + +  /* Encode the MCU data blocks */ +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { +    block = MCU_data[blkn]; +    ci = cinfo->MCU_membership[blkn]; +    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; + +    /* Compute the DC value after the required point transform by Al. +     * This is simply an arithmetic right shift. +     */ +    m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al); + +    /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ + +    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ +    st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + +    /* Figure F.4: Encode_DC_DIFF */ +    if ((v = m - entropy->last_dc_val[ci]) == 0) { +      arith_encode(cinfo, st, 0); +      entropy->dc_context[ci] = 0;	/* zero diff category */ +    } else { +      entropy->last_dc_val[ci] = m; +      arith_encode(cinfo, st, 1); +      /* Figure F.6: Encoding nonzero value v */ +      /* Figure F.7: Encoding the sign of v */ +      if (v > 0) { +	arith_encode(cinfo, st + 1, 0);	/* Table F.4: SS = S0 + 1 */ +	st += 2;			/* Table F.4: SP = S0 + 2 */ +	entropy->dc_context[ci] = 4;	/* small positive diff category */ +      } else { +	v = -v; +	arith_encode(cinfo, st + 1, 1);	/* Table F.4: SS = S0 + 1 */ +	st += 3;			/* Table F.4: SN = S0 + 3 */ +	entropy->dc_context[ci] = 8;	/* small negative diff category */ +      } +      /* Figure F.8: Encoding the magnitude category of v */ +      m = 0; +      if (v -= 1) { +	arith_encode(cinfo, st, 1); +	m = 1; +	v2 = v; +	st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ +	while (v2 >>= 1) { +	  arith_encode(cinfo, st, 1); +	  m <<= 1; +	  st += 1; +	} +      } +      arith_encode(cinfo, st, 0); +      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ +      if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) +	entropy->dc_context[ci] = 0;	/* zero diff category */ +      else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) +	entropy->dc_context[ci] += 8;	/* large diff category */ +      /* Figure F.9: Encoding the magnitude bit pattern of v */ +      st += 14; +      while (m >>= 1) +	arith_encode(cinfo, st, (m & v) ? 1 : 0); +    } +  } + +  return TRUE; +} + + +/* + * MCU encoding for AC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  JBLOCKROW block; +  unsigned char *st; +  int tbl, k, ke; +  int v, v2, m; + +  /* Emit restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) { +      emit_restart(cinfo, entropy->next_restart_num); +      entropy->restarts_to_go = cinfo->restart_interval; +      entropy->next_restart_num++; +      entropy->next_restart_num &= 7; +    } +    entropy->restarts_to_go--; +  } + +  /* Encode the MCU data block */ +  block = MCU_data[0]; +  tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + +  /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ + +  /* Establish EOB (end-of-block) index */ +  for (ke = cinfo->Se + 1; ke > 1; ke--) +    /* We must apply the point transform by Al.  For AC coefficients this +     * is an integer division with rounding towards 0.  To do this portably +     * in C, we shift after obtaining the absolute value. +     */ +    if ((v = (*block)[jpeg_natural_order[ke - 1]]) >= 0) { +      if (v >>= cinfo->Al) break; +    } else { +      v = -v; +      if (v >>= cinfo->Al) break; +    } + +  /* Figure F.5: Encode_AC_Coefficients */ +  for (k = cinfo->Ss; k < ke; k++) { +    st = entropy->ac_stats[tbl] + 3 * (k - 1); +    arith_encode(cinfo, st, 0);		/* EOB decision */ +    entropy->ac_stats[tbl][245] = 0; +    for (;;) { +      if ((v = (*block)[jpeg_natural_order[k]]) >= 0) { +	if (v >>= cinfo->Al) { +	  arith_encode(cinfo, st + 1, 1); +	  arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0); +	  break; +	} +      } else { +	v = -v; +	if (v >>= cinfo->Al) { +	  arith_encode(cinfo, st + 1, 1); +	  arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1); +	  break; +	} +      } +      arith_encode(cinfo, st + 1, 0); st += 3; k++; +    } +    st += 2; +    /* Figure F.8: Encoding the magnitude category of v */ +    m = 0; +    if (v -= 1) { +      arith_encode(cinfo, st, 1); +      m = 1; +      v2 = v; +      if (v2 >>= 1) { +	arith_encode(cinfo, st, 1); +	m <<= 1; +	st = entropy->ac_stats[tbl] + +	     (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); +	while (v2 >>= 1) { +	  arith_encode(cinfo, st, 1); +	  m <<= 1; +	  st += 1; +	} +      } +    } +    arith_encode(cinfo, st, 0); +    /* Figure F.9: Encoding the magnitude bit pattern of v */ +    st += 14; +    while (m >>= 1) +      arith_encode(cinfo, st, (m & v) ? 1 : 0); +  } +  /* Encode EOB decision only if k <= cinfo->Se */ +  if (k <= cinfo->Se) { +    st = entropy->ac_stats[tbl] + 3 * (k - 1); +    arith_encode(cinfo, st, 1); +  } + +  return TRUE; +} + + +/* + * MCU encoding for DC successive approximation refinement scan. + */ + +METHODDEF(boolean) +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  unsigned char st[4]; +  int Al, blkn; + +  /* Emit restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) { +      emit_restart(cinfo, entropy->next_restart_num); +      entropy->restarts_to_go = cinfo->restart_interval; +      entropy->next_restart_num++; +      entropy->next_restart_num &= 7; +    } +    entropy->restarts_to_go--; +  } + +  Al = cinfo->Al; + +  /* Encode the MCU data blocks */ +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { +    st[0] = 0;	/* use fixed probability estimation */ +    /* We simply emit the Al'th bit of the DC coefficient value. */ +    arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1); +  } + +  return TRUE; +} + + +/* + * MCU encoding for AC successive approximation refinement scan. + */ + +METHODDEF(boolean) +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  JBLOCKROW block; +  unsigned char *st; +  int tbl, k, ke, kex; +  int v; + +  /* Emit restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) { +      emit_restart(cinfo, entropy->next_restart_num); +      entropy->restarts_to_go = cinfo->restart_interval; +      entropy->next_restart_num++; +      entropy->next_restart_num &= 7; +    } +    entropy->restarts_to_go--; +  } + +  /* Encode the MCU data block */ +  block = MCU_data[0]; +  tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + +  /* Section G.1.3.3: Encoding of AC coefficients */ + +  /* Establish EOB (end-of-block) index */ +  for (ke = cinfo->Se + 1; ke > 1; ke--) +    /* We must apply the point transform by Al.  For AC coefficients this +     * is an integer division with rounding towards 0.  To do this portably +     * in C, we shift after obtaining the absolute value. +     */ +    if ((v = (*block)[jpeg_natural_order[ke - 1]]) >= 0) { +      if (v >>= cinfo->Al) break; +    } else { +      v = -v; +      if (v >>= cinfo->Al) break; +    } + +  /* Establish EOBx (previous stage end-of-block) index */ +  for (kex = ke; kex > 1; kex--) +    if ((v = (*block)[jpeg_natural_order[kex - 1]]) >= 0) { +      if (v >>= cinfo->Ah) break; +    } else { +      v = -v; +      if (v >>= cinfo->Ah) break; +    } + +  /* Figure G.10: Encode_AC_Coefficients_SA */ +  for (k = cinfo->Ss; k < ke; k++) { +    st = entropy->ac_stats[tbl] + 3 * (k - 1); +    if (k >= kex) +      arith_encode(cinfo, st, 0);	/* EOB decision */ +    entropy->ac_stats[tbl][245] = 0; +    for (;;) { +      if ((v = (*block)[jpeg_natural_order[k]]) >= 0) { +	if (v >>= cinfo->Al) { +	  if (v >> 1)		/* previously nonzero coef */ +	    arith_encode(cinfo, st + 2, (v & 1)); +	  else {		/* newly nonzero coef */ +	    arith_encode(cinfo, st + 1, 1); +	    arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0); +	  } +	  break; +	} +      } else { +	v = -v; +	if (v >>= cinfo->Al) { +	  if (v >> 1)		/* previously nonzero coef */ +	    arith_encode(cinfo, st + 2, (v & 1)); +	  else {		/* newly nonzero coef */ +	    arith_encode(cinfo, st + 1, 1); +	    arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1); +	  } +	  break; +	} +      } +      arith_encode(cinfo, st + 1, 0); st += 3; k++; +    } +  } +  /* Encode EOB decision only if k <= cinfo->Se */ +  if (k <= cinfo->Se) { +    st = entropy->ac_stats[tbl] + 3 * (k - 1); +    arith_encode(cinfo, st, 1); +  } + +  return TRUE; +} + + +/* + * Encode and output one MCU's worth of arithmetic-compressed coefficients. + */ + +METHODDEF(boolean) +encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  jpeg_component_info * compptr; +  JBLOCKROW block; +  unsigned char *st; +  int blkn, ci, tbl, k, ke; +  int v, v2, m; + +  /* Emit restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) { +      emit_restart(cinfo, entropy->next_restart_num); +      entropy->restarts_to_go = cinfo->restart_interval; +      entropy->next_restart_num++; +      entropy->next_restart_num &= 7; +    } +    entropy->restarts_to_go--; +  } + +  /* Encode the MCU data blocks */ +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { +    block = MCU_data[blkn]; +    ci = cinfo->MCU_membership[blkn]; +    compptr = cinfo->cur_comp_info[ci]; + +    /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ + +    tbl = compptr->dc_tbl_no; + +    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ +    st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + +    /* Figure F.4: Encode_DC_DIFF */ +    if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) { +      arith_encode(cinfo, st, 0); +      entropy->dc_context[ci] = 0;	/* zero diff category */ +    } else { +      entropy->last_dc_val[ci] = (*block)[0]; +      arith_encode(cinfo, st, 1); +      /* Figure F.6: Encoding nonzero value v */ +      /* Figure F.7: Encoding the sign of v */ +      if (v > 0) { +	arith_encode(cinfo, st + 1, 0);	/* Table F.4: SS = S0 + 1 */ +	st += 2;			/* Table F.4: SP = S0 + 2 */ +	entropy->dc_context[ci] = 4;	/* small positive diff category */ +      } else { +	v = -v; +	arith_encode(cinfo, st + 1, 1);	/* Table F.4: SS = S0 + 1 */ +	st += 3;			/* Table F.4: SN = S0 + 3 */ +	entropy->dc_context[ci] = 8;	/* small negative diff category */ +      } +      /* Figure F.8: Encoding the magnitude category of v */ +      m = 0; +      if (v -= 1) { +	arith_encode(cinfo, st, 1); +	m = 1; +	v2 = v; +	st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ +	while (v2 >>= 1) { +	  arith_encode(cinfo, st, 1); +	  m <<= 1; +	  st += 1; +	} +      } +      arith_encode(cinfo, st, 0); +      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ +      if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) +	entropy->dc_context[ci] = 0;	/* zero diff category */ +      else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) +	entropy->dc_context[ci] += 8;	/* large diff category */ +      /* Figure F.9: Encoding the magnitude bit pattern of v */ +      st += 14; +      while (m >>= 1) +	arith_encode(cinfo, st, (m & v) ? 1 : 0); +    } + +    /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ + +    tbl = compptr->ac_tbl_no; + +    /* Establish EOB (end-of-block) index */ +    for (ke = DCTSIZE2; ke > 1; ke--) +      if ((*block)[jpeg_natural_order[ke - 1]]) break; + +    /* Figure F.5: Encode_AC_Coefficients */ +    for (k = 1; k < ke; k++) { +      st = entropy->ac_stats[tbl] + 3 * (k - 1); +      arith_encode(cinfo, st, 0);	/* EOB decision */ +      while ((v = (*block)[jpeg_natural_order[k]]) == 0) { +	arith_encode(cinfo, st + 1, 0); st += 3; k++; +      } +      arith_encode(cinfo, st + 1, 1); +      /* Figure F.6: Encoding nonzero value v */ +      /* Figure F.7: Encoding the sign of v */ +      entropy->ac_stats[tbl][245] = 0; +      if (v > 0) { +	arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0); +      } else { +	v = -v; +	arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1); +      } +      st += 2; +      /* Figure F.8: Encoding the magnitude category of v */ +      m = 0; +      if (v -= 1) { +	arith_encode(cinfo, st, 1); +	m = 1; +	v2 = v; +	if (v2 >>= 1) { +	  arith_encode(cinfo, st, 1); +	  m <<= 1; +	  st = entropy->ac_stats[tbl] + +	       (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); +	  while (v2 >>= 1) { +	    arith_encode(cinfo, st, 1); +	    m <<= 1; +	    st += 1; +	  } +	} +      } +      arith_encode(cinfo, st, 0); +      /* Figure F.9: Encoding the magnitude bit pattern of v */ +      st += 14; +      while (m >>= 1) +	arith_encode(cinfo, st, (m & v) ? 1 : 0); +    } +    /* Encode EOB decision only if k < DCTSIZE2 */ +    if (k < DCTSIZE2) { +      st = entropy->ac_stats[tbl] + 3 * (k - 1); +      arith_encode(cinfo, st, 1); +    } +  } + +  return TRUE; +} + + +/* + * Initialize for an arithmetic-compressed scan. + */ + +METHODDEF(void) +start_pass (j_compress_ptr cinfo, boolean gather_statistics) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  int ci, tbl; +  jpeg_component_info * compptr; + +  if (gather_statistics) +    /* Make sure to avoid that in the master control logic! +     * We are fully adaptive here and need no extra +     * statistics gathering pass! +     */ +    ERREXIT(cinfo, JERR_NOT_COMPILED); + +  /* We assume jcmaster.c already validated the progressive scan parameters. */ + +  /* Select execution routines */ +  if (cinfo->progressive_mode) { +    if (cinfo->Ah == 0) { +      if (cinfo->Ss == 0) +	entropy->pub.encode_mcu = encode_mcu_DC_first; +      else +	entropy->pub.encode_mcu = encode_mcu_AC_first; +    } else { +      if (cinfo->Ss == 0) +	entropy->pub.encode_mcu = encode_mcu_DC_refine; +      else +	entropy->pub.encode_mcu = encode_mcu_AC_refine; +    } +  } else +    entropy->pub.encode_mcu = encode_mcu; + +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { +    compptr = cinfo->cur_comp_info[ci]; +    /* Allocate & initialize requested statistics areas */ +    if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { +      tbl = compptr->dc_tbl_no; +      if (tbl < 0 || tbl >= NUM_ARITH_TBLS) +	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); +      if (entropy->dc_stats[tbl] == NULL) +	entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); +      MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); +      /* Initialize DC predictions to 0 */ +      entropy->last_dc_val[ci] = 0; +      entropy->dc_context[ci] = 0; +    } +    if (cinfo->progressive_mode == 0 || cinfo->Ss) { +      tbl = compptr->ac_tbl_no; +      if (tbl < 0 || tbl >= NUM_ARITH_TBLS) +	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); +      if (entropy->ac_stats[tbl] == NULL) +	entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); +      MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); +#ifdef CALCULATE_SPECTRAL_CONDITIONING +      if (cinfo->progressive_mode) +	/* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */ +	cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4); +#endif +    } +  } + +  /* Initialize arithmetic encoding variables */ +  entropy->c = 0; +  entropy->a = 0x10000L; +  entropy->sc = 0; +  entropy->zc = 0; +  entropy->ct = 11; +  entropy->buffer = -1;  /* empty */ + +  /* Initialize restart stuff */ +  entropy->restarts_to_go = cinfo->restart_interval; +  entropy->next_restart_num = 0; +} + + +/* + * Module initialization routine for arithmetic entropy encoding. + */ + +GLOBAL(void) +jinit_arith_encoder (j_compress_ptr cinfo) +{ +  arith_entropy_ptr entropy; +  int i; + +  entropy = (arith_entropy_ptr) +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, +				SIZEOF(arith_entropy_encoder)); +  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; +  entropy->pub.start_pass = start_pass; +  entropy->pub.finish_pass = finish_pass; + +  /* Mark tables unallocated */ +  for (i = 0; i < NUM_ARITH_TBLS; i++) { +    entropy->dc_stats[i] = NULL; +    entropy->ac_stats[i] = NULL; +  } +} diff --git a/src/libjpeg/jchuff.h b/src/libjpeg/jchuff.h deleted file mode 100644 index a9599fc..0000000 --- a/src/libjpeg/jchuff.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * jchuff.h - * - * Copyright (C) 1991-1997, 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 declarations for Huffman entropy encoding routines - * that are shared between the sequential encoder (jchuff.c) and the - * progressive encoder (jcphuff.c).  No other modules need to see these. - */ - -/* The legal range of a DCT coefficient is - *  -1024 .. +1023  for 8-bit data; - * -16384 .. +16383 for 12-bit data. - * Hence the magnitude should always fit in 10 or 14 bits respectively. - */ - -#if BITS_IN_JSAMPLE == 8 -#define MAX_COEF_BITS 10 -#else -#define MAX_COEF_BITS 14 -#endif - -/* Derived data constructed for each Huffman table */ - -typedef struct { -  unsigned int ehufco[256];	/* code for each symbol */ -  char ehufsi[256];		/* length of code for each symbol */ -  /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */ -} c_derived_tbl; - -/* Short forms of external names for systems with brain-damaged linkers. */ - -#ifdef NEED_SHORT_EXTERNAL_NAMES -#define jpeg_make_c_derived_tbl	jMkCDerived -#define jpeg_gen_optimal_table	jGenOptTbl -#endif /* NEED_SHORT_EXTERNAL_NAMES */ - -/* Expand a Huffman table definition into the derived format */ -EXTERN(void) jpeg_make_c_derived_tbl -	JPP((j_compress_ptr cinfo, boolean isDC, int tblno, -	     c_derived_tbl ** pdtbl)); - -/* Generate an optimal table definition given the specified counts */ -EXTERN(void) jpeg_gen_optimal_table -	JPP((j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])); diff --git a/src/libjpeg/jcphuff.c b/src/libjpeg/jcphuff.c deleted file mode 100644 index 07f9178..0000000 --- a/src/libjpeg/jcphuff.c +++ /dev/null @@ -1,833 +0,0 @@ -/* - * jcphuff.c - * - * Copyright (C) 1995-1997, 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 Huffman entropy encoding routines for progressive JPEG. - * - * We do not support output suspension in this module, since the library - * currently does not allow multiple-scan files to be written with output - * suspension. - */ - -#define JPEG_INTERNALS -#include "jinclude.h" -#include "jpeglib.h" -#include "jchuff.h"		/* Declarations shared with jchuff.c */ - -#ifdef C_PROGRESSIVE_SUPPORTED - -/* Expanded entropy encoder object for progressive Huffman encoding. */ - -typedef struct { -  struct jpeg_entropy_encoder pub; /* public fields */ - -  /* Mode flag: TRUE for optimization, FALSE for actual data output */ -  boolean gather_statistics; - -  /* Bit-level coding status. -   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. -   */ -  JOCTET * next_output_byte;	/* => next byte to write in buffer */ -  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */ -  INT32 put_buffer;		/* current bit-accumulation buffer */ -  int put_bits;			/* # of bits now in it */ -  j_compress_ptr cinfo;		/* link to cinfo (needed for dump_buffer) */ - -  /* Coding status for DC components */ -  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ - -  /* Coding status for AC components */ -  int ac_tbl_no;		/* the table number of the single component */ -  unsigned int EOBRUN;		/* run length of EOBs */ -  unsigned int BE;		/* # of buffered correction bits before MCU */ -  char * bit_buffer;		/* buffer for correction bits (1 per char) */ -  /* packing correction bits tightly would save some space but cost time... */ - -  unsigned int restarts_to_go;	/* MCUs left in this restart interval */ -  int next_restart_num;		/* next restart number to write (0-7) */ - -  /* Pointers to derived tables (these workspaces have image lifespan). -   * Since any one scan codes only DC or only AC, we only need one set -   * of tables, not one for DC and one for AC. -   */ -  c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; - -  /* Statistics tables for optimization; again, one set is enough */ -  long * count_ptrs[NUM_HUFF_TBLS]; -} phuff_entropy_encoder; - -typedef phuff_entropy_encoder * phuff_entropy_ptr; - -/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit - * buffer can hold.  Larger sizes may slightly improve compression, but - * 1000 is already well into the realm of overkill. - * The minimum safe size is 64 bits. - */ - -#define MAX_CORR_BITS  1000	/* Max # of correction bits I can buffer */ - -/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. - * We assume that int right shift is unsigned if INT32 right shift is, - * which should be safe. - */ - -#ifdef RIGHT_SHIFT_IS_UNSIGNED -#define ISHIFT_TEMPS	int ishift_temp; -#define IRIGHT_SHIFT(x,shft)  \ -	((ishift_temp = (x)) < 0 ? \ -	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ -	 (ishift_temp >> (shft))) -#else -#define ISHIFT_TEMPS -#define IRIGHT_SHIFT(x,shft)	((x) >> (shft)) -#endif - -/* Forward declarations */ -METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, -					    JBLOCKROW *MCU_data)); -METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, -					    JBLOCKROW *MCU_data)); -METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, -					     JBLOCKROW *MCU_data)); -METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, -					     JBLOCKROW *MCU_data)); -METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); -METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); - - -/* - * Initialize for a Huffman-compressed scan using progressive JPEG. - */ - -METHODDEF(void) -start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) -{   -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  boolean is_DC_band; -  int ci, tbl; -  jpeg_component_info * compptr; - -  entropy->cinfo = cinfo; -  entropy->gather_statistics = gather_statistics; - -  is_DC_band = (cinfo->Ss == 0); - -  /* We assume jcmaster.c already validated the scan parameters. */ - -  /* Select execution routines */ -  if (cinfo->Ah == 0) { -    if (is_DC_band) -      entropy->pub.encode_mcu = encode_mcu_DC_first; -    else -      entropy->pub.encode_mcu = encode_mcu_AC_first; -  } else { -    if (is_DC_band) -      entropy->pub.encode_mcu = encode_mcu_DC_refine; -    else { -      entropy->pub.encode_mcu = encode_mcu_AC_refine; -      /* AC refinement needs a correction bit buffer */ -      if (entropy->bit_buffer == NULL) -	entropy->bit_buffer = (char *) -	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, -				      MAX_CORR_BITS * SIZEOF(char)); -    } -  } -  if (gather_statistics) -    entropy->pub.finish_pass = finish_pass_gather_phuff; -  else -    entropy->pub.finish_pass = finish_pass_phuff; - -  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 -   * for AC coefficients. -   */ -  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { -    compptr = cinfo->cur_comp_info[ci]; -    /* Initialize DC predictions to 0 */ -    entropy->last_dc_val[ci] = 0; -    /* Get table index */ -    if (is_DC_band) { -      if (cinfo->Ah != 0)	/* DC refinement needs no table */ -	continue; -      tbl = compptr->dc_tbl_no; -    } else { -      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; -    } -    if (gather_statistics) { -      /* Check for invalid table index */ -      /* (make_c_derived_tbl does this in the other path) */ -      if (tbl < 0 || tbl >= NUM_HUFF_TBLS) -        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); -      /* Allocate and zero the statistics tables */ -      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ -      if (entropy->count_ptrs[tbl] == NULL) -	entropy->count_ptrs[tbl] = (long *) -	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, -				      257 * SIZEOF(long)); -      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); -    } else { -      /* Compute derived values for Huffman table */ -      /* We may do this more than once for a table, but it's not expensive */ -      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, -			      & entropy->derived_tbls[tbl]); -    } -  } - -  /* Initialize AC stuff */ -  entropy->EOBRUN = 0; -  entropy->BE = 0; - -  /* Initialize bit buffer to empty */ -  entropy->put_buffer = 0; -  entropy->put_bits = 0; - -  /* Initialize restart stuff */ -  entropy->restarts_to_go = cinfo->restart_interval; -  entropy->next_restart_num = 0; -} - - -/* Outputting bytes to the file. - * NB: these must be called only when actually outputting, - * that is, entropy->gather_statistics == FALSE. - */ - -/* Emit a byte */ -#define emit_byte(entropy,val)  \ -	{ *(entropy)->next_output_byte++ = (JOCTET) (val);  \ -	  if (--(entropy)->free_in_buffer == 0)  \ -	    dump_buffer(entropy); } - - -LOCAL(void) -dump_buffer (phuff_entropy_ptr entropy) -/* Empty the output buffer; we do not support suspension in this module. */ -{ -  struct jpeg_destination_mgr * dest = entropy->cinfo->dest; - -  if (! (*dest->empty_output_buffer) (entropy->cinfo)) -    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); -  /* After a successful buffer dump, must reset buffer pointers */ -  entropy->next_output_byte = dest->next_output_byte; -  entropy->free_in_buffer = dest->free_in_buffer; -} - - -/* Outputting bits to the file */ - -/* Only the right 24 bits of put_buffer are used; the valid bits are - * left-justified in this part.  At most 16 bits can be passed to emit_bits - * in one call, and we never retain more than 7 bits in put_buffer - * between calls, so 24 bits are sufficient. - */ - -INLINE -LOCAL(void) -emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) -/* Emit some bits, unless we are in gather mode */ -{ -  /* This routine is heavily used, so it's worth coding tightly. */ -  register INT32 put_buffer = (INT32) code; -  register int put_bits = entropy->put_bits; - -  /* if size is 0, caller used an invalid Huffman table entry */ -  if (size == 0) -    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); - -  if (entropy->gather_statistics) -    return;			/* do nothing if we're only getting stats */ - -  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ -   -  put_bits += size;		/* new number of bits in buffer */ -   -  put_buffer <<= 24 - put_bits; /* align incoming bits */ - -  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ - -  while (put_bits >= 8) { -    int c = (int) ((put_buffer >> 16) & 0xFF); -     -    emit_byte(entropy, c); -    if (c == 0xFF) {		/* need to stuff a zero byte? */ -      emit_byte(entropy, 0); -    } -    put_buffer <<= 8; -    put_bits -= 8; -  } - -  entropy->put_buffer = put_buffer; /* update variables */ -  entropy->put_bits = put_bits; -} - - -LOCAL(void) -flush_bits (phuff_entropy_ptr entropy) -{ -  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */ -  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */ -  entropy->put_bits = 0; -} - - -/* - * Emit (or just count) a Huffman symbol. - */ - -INLINE -LOCAL(void) -emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) -{ -  if (entropy->gather_statistics) -    entropy->count_ptrs[tbl_no][symbol]++; -  else { -    c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; -    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); -  } -} - - -/* - * Emit bits from a correction bit buffer. - */ - -LOCAL(void) -emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, -		    unsigned int nbits) -{ -  if (entropy->gather_statistics) -    return;			/* no real work */ - -  while (nbits > 0) { -    emit_bits(entropy, (unsigned int) (*bufstart), 1); -    bufstart++; -    nbits--; -  } -} - - -/* - * Emit any pending EOBRUN symbol. - */ - -LOCAL(void) -emit_eobrun (phuff_entropy_ptr entropy) -{ -  register int temp, nbits; - -  if (entropy->EOBRUN > 0) {	/* if there is any pending EOBRUN */ -    temp = entropy->EOBRUN; -    nbits = 0; -    while ((temp >>= 1)) -      nbits++; -    /* safety check: shouldn't happen given limited correction-bit buffer */ -    if (nbits > 14) -      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); - -    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); -    if (nbits) -      emit_bits(entropy, entropy->EOBRUN, nbits); - -    entropy->EOBRUN = 0; - -    /* Emit any buffered correction bits */ -    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE); -    entropy->BE = 0; -  } -} - - -/* - * Emit a restart marker & resynchronize predictions. - */ - -LOCAL(void) -emit_restart (phuff_entropy_ptr entropy, int restart_num) -{ -  int ci; - -  emit_eobrun(entropy); - -  if (! entropy->gather_statistics) { -    flush_bits(entropy); -    emit_byte(entropy, 0xFF); -    emit_byte(entropy, JPEG_RST0 + restart_num); -  } - -  if (entropy->cinfo->Ss == 0) { -    /* Re-initialize DC predictions to 0 */ -    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++) -      entropy->last_dc_val[ci] = 0; -  } else { -    /* Re-initialize all AC-related fields to 0 */ -    entropy->EOBRUN = 0; -    entropy->BE = 0; -  } -} - - -/* - * MCU encoding for DC initial scan (either spectral selection, - * or first pass of successive approximation). - */ - -METHODDEF(boolean) -encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  register int temp, temp2; -  register int nbits; -  int blkn, ci; -  int Al = cinfo->Al; -  JBLOCKROW block; -  jpeg_component_info * compptr; -  ISHIFT_TEMPS - -  entropy->next_output_byte = cinfo->dest->next_output_byte; -  entropy->free_in_buffer = cinfo->dest->free_in_buffer; - -  /* Emit restart marker if needed */ -  if (cinfo->restart_interval) -    if (entropy->restarts_to_go == 0) -      emit_restart(entropy, entropy->next_restart_num); - -  /* Encode the MCU data blocks */ -  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { -    block = MCU_data[blkn]; -    ci = cinfo->MCU_membership[blkn]; -    compptr = cinfo->cur_comp_info[ci]; - -    /* Compute the DC value after the required point transform by Al. -     * This is simply an arithmetic right shift. -     */ -    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); - -    /* DC differences are figured on the point-transformed values. */ -    temp = temp2 - entropy->last_dc_val[ci]; -    entropy->last_dc_val[ci] = temp2; - -    /* Encode the DC coefficient difference per section G.1.2.1 */ -    temp2 = temp; -    if (temp < 0) { -      temp = -temp;		/* temp is abs value of input */ -      /* For a negative input, want temp2 = bitwise complement of abs(input) */ -      /* This code assumes we are on a two's complement machine */ -      temp2--; -    } -     -    /* Find the number of bits needed for the magnitude of the coefficient */ -    nbits = 0; -    while (temp) { -      nbits++; -      temp >>= 1; -    } -    /* Check for out-of-range coefficient values. -     * Since we're encoding a difference, the range limit is twice as much. -     */ -    if (nbits > MAX_COEF_BITS+1) -      ERREXIT(cinfo, JERR_BAD_DCT_COEF); -     -    /* Count/emit the Huffman-coded symbol for the number of bits */ -    emit_symbol(entropy, compptr->dc_tbl_no, nbits); -     -    /* Emit that number of bits of the value, if positive, */ -    /* or the complement of its magnitude, if negative. */ -    if (nbits)			/* emit_bits rejects calls with size 0 */ -      emit_bits(entropy, (unsigned int) temp2, nbits); -  } - -  cinfo->dest->next_output_byte = entropy->next_output_byte; -  cinfo->dest->free_in_buffer = entropy->free_in_buffer; - -  /* Update restart-interval state too */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) { -      entropy->restarts_to_go = cinfo->restart_interval; -      entropy->next_restart_num++; -      entropy->next_restart_num &= 7; -    } -    entropy->restarts_to_go--; -  } - -  return TRUE; -} - - -/* - * MCU encoding for AC initial scan (either spectral selection, - * or first pass of successive approximation). - */ - -METHODDEF(boolean) -encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  register int temp, temp2; -  register int nbits; -  register int r, k; -  int Se = cinfo->Se; -  int Al = cinfo->Al; -  JBLOCKROW block; - -  entropy->next_output_byte = cinfo->dest->next_output_byte; -  entropy->free_in_buffer = cinfo->dest->free_in_buffer; - -  /* Emit restart marker if needed */ -  if (cinfo->restart_interval) -    if (entropy->restarts_to_go == 0) -      emit_restart(entropy, entropy->next_restart_num); - -  /* Encode the MCU data block */ -  block = MCU_data[0]; - -  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ -   -  r = 0;			/* r = run length of zeros */ -    -  for (k = cinfo->Ss; k <= Se; k++) { -    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { -      r++; -      continue; -    } -    /* We must apply the point transform by Al.  For AC coefficients this -     * is an integer division with rounding towards 0.  To do this portably -     * in C, we shift after obtaining the absolute value; so the code is -     * interwoven with finding the abs value (temp) and output bits (temp2). -     */ -    if (temp < 0) { -      temp = -temp;		/* temp is abs value of input */ -      temp >>= Al;		/* apply the point transform */ -      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ -      temp2 = ~temp; -    } else { -      temp >>= Al;		/* apply the point transform */ -      temp2 = temp; -    } -    /* Watch out for case that nonzero coef is zero after point transform */ -    if (temp == 0) { -      r++; -      continue; -    } - -    /* Emit any pending EOBRUN */ -    if (entropy->EOBRUN > 0) -      emit_eobrun(entropy); -    /* if run length > 15, must emit special run-length-16 codes (0xF0) */ -    while (r > 15) { -      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); -      r -= 16; -    } - -    /* Find the number of bits needed for the magnitude of the coefficient */ -    nbits = 1;			/* there must be at least one 1 bit */ -    while ((temp >>= 1)) -      nbits++; -    /* Check for out-of-range coefficient values */ -    if (nbits > MAX_COEF_BITS) -      ERREXIT(cinfo, JERR_BAD_DCT_COEF); - -    /* Count/emit Huffman symbol for run length / number of bits */ -    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); - -    /* Emit that number of bits of the value, if positive, */ -    /* or the complement of its magnitude, if negative. */ -    emit_bits(entropy, (unsigned int) temp2, nbits); - -    r = 0;			/* reset zero run length */ -  } - -  if (r > 0) {			/* If there are trailing zeroes, */ -    entropy->EOBRUN++;		/* count an EOB */ -    if (entropy->EOBRUN == 0x7FFF) -      emit_eobrun(entropy);	/* force it out to avoid overflow */ -  } - -  cinfo->dest->next_output_byte = entropy->next_output_byte; -  cinfo->dest->free_in_buffer = entropy->free_in_buffer; - -  /* Update restart-interval state too */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) { -      entropy->restarts_to_go = cinfo->restart_interval; -      entropy->next_restart_num++; -      entropy->next_restart_num &= 7; -    } -    entropy->restarts_to_go--; -  } - -  return TRUE; -} - - -/* - * MCU encoding for DC successive approximation refinement scan. - * Note: we assume such scans can be multi-component, although the spec - * is not very clear on the point. - */ - -METHODDEF(boolean) -encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  register int temp; -  int blkn; -  int Al = cinfo->Al; -  JBLOCKROW block; - -  entropy->next_output_byte = cinfo->dest->next_output_byte; -  entropy->free_in_buffer = cinfo->dest->free_in_buffer; - -  /* Emit restart marker if needed */ -  if (cinfo->restart_interval) -    if (entropy->restarts_to_go == 0) -      emit_restart(entropy, entropy->next_restart_num); - -  /* Encode the MCU data blocks */ -  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { -    block = MCU_data[blkn]; - -    /* We simply emit the Al'th bit of the DC coefficient value. */ -    temp = (*block)[0]; -    emit_bits(entropy, (unsigned int) (temp >> Al), 1); -  } - -  cinfo->dest->next_output_byte = entropy->next_output_byte; -  cinfo->dest->free_in_buffer = entropy->free_in_buffer; - -  /* Update restart-interval state too */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) { -      entropy->restarts_to_go = cinfo->restart_interval; -      entropy->next_restart_num++; -      entropy->next_restart_num &= 7; -    } -    entropy->restarts_to_go--; -  } - -  return TRUE; -} - - -/* - * MCU encoding for AC successive approximation refinement scan. - */ - -METHODDEF(boolean) -encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  register int temp; -  register int r, k; -  int EOB; -  char *BR_buffer; -  unsigned int BR; -  int Se = cinfo->Se; -  int Al = cinfo->Al; -  JBLOCKROW block; -  int absvalues[DCTSIZE2]; - -  entropy->next_output_byte = cinfo->dest->next_output_byte; -  entropy->free_in_buffer = cinfo->dest->free_in_buffer; - -  /* Emit restart marker if needed */ -  if (cinfo->restart_interval) -    if (entropy->restarts_to_go == 0) -      emit_restart(entropy, entropy->next_restart_num); - -  /* Encode the MCU data block */ -  block = MCU_data[0]; - -  /* It is convenient to make a pre-pass to determine the transformed -   * coefficients' absolute values and the EOB position. -   */ -  EOB = 0; -  for (k = cinfo->Ss; k <= Se; k++) { -    temp = (*block)[jpeg_natural_order[k]]; -    /* We must apply the point transform by Al.  For AC coefficients this -     * is an integer division with rounding towards 0.  To do this portably -     * in C, we shift after obtaining the absolute value. -     */ -    if (temp < 0) -      temp = -temp;		/* temp is abs value of input */ -    temp >>= Al;		/* apply the point transform */ -    absvalues[k] = temp;	/* save abs value for main pass */ -    if (temp == 1) -      EOB = k;			/* EOB = index of last newly-nonzero coef */ -  } - -  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ -   -  r = 0;			/* r = run length of zeros */ -  BR = 0;			/* BR = count of buffered bits added now */ -  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ - -  for (k = cinfo->Ss; k <= Se; k++) { -    if ((temp = absvalues[k]) == 0) { -      r++; -      continue; -    } - -    /* Emit any required ZRLs, but not if they can be folded into EOB */ -    while (r > 15 && k <= EOB) { -      /* emit any pending EOBRUN and the BE correction bits */ -      emit_eobrun(entropy); -      /* Emit ZRL */ -      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); -      r -= 16; -      /* Emit buffered correction bits that must be associated with ZRL */ -      emit_buffered_bits(entropy, BR_buffer, BR); -      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ -      BR = 0; -    } - -    /* If the coef was previously nonzero, it only needs a correction bit. -     * NOTE: a straight translation of the spec's figure G.7 would suggest -     * that we also need to test r > 15.  But if r > 15, we can only get here -     * if k > EOB, which implies that this coefficient is not 1. -     */ -    if (temp > 1) { -      /* The correction bit is the next bit of the absolute value. */ -      BR_buffer[BR++] = (char) (temp & 1); -      continue; -    } - -    /* Emit any pending EOBRUN and the BE correction bits */ -    emit_eobrun(entropy); - -    /* Count/emit Huffman symbol for run length / number of bits */ -    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); - -    /* Emit output bit for newly-nonzero coef */ -    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; -    emit_bits(entropy, (unsigned int) temp, 1); - -    /* Emit buffered correction bits that must be associated with this code */ -    emit_buffered_bits(entropy, BR_buffer, BR); -    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ -    BR = 0; -    r = 0;			/* reset zero run length */ -  } - -  if (r > 0 || BR > 0) {	/* If there are trailing zeroes, */ -    entropy->EOBRUN++;		/* count an EOB */ -    entropy->BE += BR;		/* concat my correction bits to older ones */ -    /* We force out the EOB if we risk either: -     * 1. overflow of the EOB counter; -     * 2. overflow of the correction bit buffer during the next MCU. -     */ -    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) -      emit_eobrun(entropy); -  } - -  cinfo->dest->next_output_byte = entropy->next_output_byte; -  cinfo->dest->free_in_buffer = entropy->free_in_buffer; - -  /* Update restart-interval state too */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) { -      entropy->restarts_to_go = cinfo->restart_interval; -      entropy->next_restart_num++; -      entropy->next_restart_num &= 7; -    } -    entropy->restarts_to_go--; -  } - -  return TRUE; -} - - -/* - * Finish up at the end of a Huffman-compressed progressive scan. - */ - -METHODDEF(void) -finish_pass_phuff (j_compress_ptr cinfo) -{    -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; - -  entropy->next_output_byte = cinfo->dest->next_output_byte; -  entropy->free_in_buffer = cinfo->dest->free_in_buffer; - -  /* Flush out any buffered data */ -  emit_eobrun(entropy); -  flush_bits(entropy); - -  cinfo->dest->next_output_byte = entropy->next_output_byte; -  cinfo->dest->free_in_buffer = entropy->free_in_buffer; -} - - -/* - * Finish up a statistics-gathering pass and create the new Huffman tables. - */ - -METHODDEF(void) -finish_pass_gather_phuff (j_compress_ptr cinfo) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  boolean is_DC_band; -  int ci, tbl; -  jpeg_component_info * compptr; -  JHUFF_TBL **htblptr; -  boolean did[NUM_HUFF_TBLS]; - -  /* Flush out buffered data (all we care about is counting the EOB symbol) */ -  emit_eobrun(entropy); - -  is_DC_band = (cinfo->Ss == 0); - -  /* It's important not to apply jpeg_gen_optimal_table more than once -   * per table, because it clobbers the input frequency counts! -   */ -  MEMZERO(did, SIZEOF(did)); - -  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { -    compptr = cinfo->cur_comp_info[ci]; -    if (is_DC_band) { -      if (cinfo->Ah != 0)	/* DC refinement needs no table */ -	continue; -      tbl = compptr->dc_tbl_no; -    } else { -      tbl = compptr->ac_tbl_no; -    } -    if (! did[tbl]) { -      if (is_DC_band) -        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; -      else -        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; -      if (*htblptr == NULL) -        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); -      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]); -      did[tbl] = TRUE; -    } -  } -} - - -/* - * Module initialization routine for progressive Huffman entropy encoding. - */ - -GLOBAL(void) -jinit_phuff_encoder (j_compress_ptr cinfo) -{ -  phuff_entropy_ptr entropy; -  int i; - -  entropy = (phuff_entropy_ptr) -    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, -				SIZEOF(phuff_entropy_encoder)); -  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; -  entropy->pub.start_pass = start_pass_phuff; - -  /* Mark tables unallocated */ -  for (i = 0; i < NUM_HUFF_TBLS; i++) { -    entropy->derived_tbls[i] = NULL; -    entropy->count_ptrs[i] = NULL; -  } -  entropy->bit_buffer = NULL;	/* needed only in AC refinement scan */ -} - -#endif /* C_PROGRESSIVE_SUPPORTED */ diff --git a/src/libjpeg/jdarith.c b/src/libjpeg/jdarith.c new file mode 100644 index 0000000..702950f --- /dev/null +++ b/src/libjpeg/jdarith.c @@ -0,0 +1,762 @@ +/* + * jdarith.c + * + * Developed 1997 by Guido Vollbeding. + * 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 portable arithmetic entropy decoding routines for JPEG + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). + * + * Both sequential and progressive modes are supported in this single module. + * + * Suspension is not currently supported in this module. + */ + +#define JPEG_INTERNALS +#include "jinclude.h" +#include "jpeglib.h" + + +/* Expanded entropy decoder object for arithmetic decoding. */ + +typedef struct { +  struct jpeg_entropy_decoder pub; /* public fields */ + +  INT32 c;       /* C register, base of coding interval + input bit buffer */ +  INT32 a;               /* A register, normalized size of coding interval */ +  int ct;     /* bit shift counter, # of bits left in bit buffer part of C */ +                                                         /* init: ct = -16 */ +                                                         /* run: ct = 0..7 */ +                                                         /* error: ct = -1 */ +  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ +  int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ + +  unsigned int restarts_to_go;	/* MCUs left in this restart interval */ + +  /* Pointers to statistics areas (these workspaces have image lifespan) */ +  unsigned char * dc_stats[NUM_ARITH_TBLS]; +  unsigned char * ac_stats[NUM_ARITH_TBLS]; +} arith_entropy_decoder; + +typedef arith_entropy_decoder * arith_entropy_ptr; + +/* The following two definitions specify the allocation chunk size + * for the statistics area. + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least + * 49 statistics bins for DC, and 245 statistics bins for AC coding. + * Note that we use one additional AC bin for codings with fixed + * probability (0.5), thus the minimum number for AC is 246. + * + * We use a compact representation with 1 byte per statistics bin, + * thus the numbers directly represent byte sizes. + * This 1 byte per statistics bin contains the meaning of the MPS + * (more probable symbol) in the highest bit (mask 0x80), and the + * index into the probability estimation state machine table + * in the lower bits (mask 0x7F). + */ + +#define DC_STAT_BINS 64 +#define AC_STAT_BINS 256 + + +LOCAL(int) +get_byte (j_decompress_ptr cinfo) +/* Read next input byte; we do not support suspension in this module. */ +{ +  struct jpeg_source_mgr * src = cinfo->src; + +  if (src->bytes_in_buffer == 0) +    if (! (*src->fill_input_buffer) (cinfo)) +      ERREXIT(cinfo, JERR_CANT_SUSPEND); +  src->bytes_in_buffer--; +  return GETJOCTET(*src->next_input_byte++); +} + + +/* + * The core arithmetic decoding routine (common in JPEG and JBIG). + * This needs to go as fast as possible. + * Machine-dependent optimization facilities + * are not utilized in this portable implementation. + * However, this code should be fairly efficient and + * may be a good base for further optimizations anyway. + * + * Return value is 0 or 1 (binary decision). + * + * Note: I've changed the handling of the code base & bit + * buffer register C compared to other implementations + * based on the standards layout & procedures. + * While it also contains both the actual base of the + * coding interval (16 bits) and the next-bits buffer, + * the cut-point between these two parts is floating + * (instead of fixed) with the bit shift counter CT. + * Thus, we also need only one (variable instead of + * fixed size) shift for the LPS/MPS decision, and + * we can get away with any renormalization update + * of C (except for new data insertion, of course). + * + * I've also introduced a new scheme for accessing + * the probability estimation state machine table, + * derived from Markus Kuhn's JBIG implementation. + */ + +LOCAL(int) +arith_decode (j_decompress_ptr cinfo, unsigned char *st) +{ +  extern const INT32 jaritab[]; +  register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; +  register unsigned char nl, nm; +  register INT32 qe, temp; +  register int sv, data; + +  /* Renormalization & data input per section D.2.6 */ +  while (e->a < 0x8000L) { +    if (--e->ct < 0) { +      /* Need to fetch next data byte */ +      if (cinfo->unread_marker) +	data = 0;		/* stuff zero data */ +      else { +	data = get_byte(cinfo);	/* read next input byte */ +	if (data == 0xFF) {	/* zero stuff or marker code */ +	  do data = get_byte(cinfo); +	  while (data == 0xFF);	/* swallow extra 0xFF bytes */ +	  if (data == 0) +	    data = 0xFF;	/* discard stuffed zero byte */ +	  else { +	    /* Note: Different from the Huffman decoder, hitting +	     * a marker while processing the compressed data +	     * segment is legal in arithmetic coding. +	     * The convention is to supply zero data +	     * then until decoding is complete. +	     */ +	    cinfo->unread_marker = data; +	    data = 0; +	  } +	} +      } +      e->c = (e->c << 8) | data; /* insert data into C register */ +      if ((e->ct += 8) < 0)	 /* update bit shift counter */ +	/* Need more initial bytes */ +	if (++e->ct == 0) +	  /* Got 2 initial bytes -> re-init A and exit loop */ +	  e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ +    } +    e->a <<= 1; +  } + +  /* Fetch values from our compact representation of Table D.2: +   * Qe values and probability estimation state machine +   */ +  sv = *st; +  qe = jaritab[sv & 0x7F];	/* => Qe_Value */ +  nl = qe & 0xFF; qe >>= 8;	/* Next_Index_LPS + Switch_MPS */ +  nm = qe & 0xFF; qe >>= 8;	/* Next_Index_MPS */ + +  /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ +  temp = e->a - qe; +  e->a = temp; +  temp <<= e->ct; +  if (e->c >= temp) { +    e->c -= temp; +    /* Conditional LPS (less probable symbol) exchange */ +    if (e->a < qe) { +      e->a = qe; +      *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */ +    } else { +      e->a = qe; +      *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */ +      sv ^= 0x80;		/* Exchange LPS/MPS */ +    } +  } else if (e->a < 0x8000L) { +    /* Conditional MPS (more probable symbol) exchange */ +    if (e->a < qe) { +      *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */ +      sv ^= 0x80;		/* Exchange LPS/MPS */ +    } else { +      *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */ +    } +  } + +  return sv >> 7; +} + + +/* + * Check for a restart marker & resynchronize decoder. + */ + +LOCAL(void) +process_restart (j_decompress_ptr cinfo) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  int ci; +  jpeg_component_info * compptr; + +  /* Advance past the RSTn marker */ +  if (! (*cinfo->marker->read_restart_marker) (cinfo)) +    ERREXIT(cinfo, JERR_CANT_SUSPEND); + +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { +    compptr = cinfo->cur_comp_info[ci]; +    /* Re-initialize statistics areas */ +    if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { +      MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); +      /* Reset DC predictions to 0 */ +      entropy->last_dc_val[ci] = 0; +      entropy->dc_context[ci] = 0; +    } +    if (cinfo->progressive_mode == 0 || cinfo->Ss) { +      MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); +    } +  } + +  /* Reset arithmetic decoding variables */ +  entropy->c = 0; +  entropy->a = 0; +  entropy->ct = -16;	/* force reading 2 initial bytes to fill C */ + +  /* Reset restart counter */ +  entropy->restarts_to_go = cinfo->restart_interval; +} + + +/* + * Arithmetic MCU decoding. + * Each of these routines decodes and returns one MCU's worth of + * arithmetic-compressed coefficients. + * The coefficients are reordered from zigzag order into natural array order, + * but are not dequantized. + * + * The i'th block of the MCU is stored into the block pointed to by + * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. + */ + +/* + * MCU decoding for DC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  JBLOCKROW block; +  unsigned char *st; +  int blkn, ci, tbl, sign; +  int v, m; + +  /* Process restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) +      process_restart(cinfo); +    entropy->restarts_to_go--; +  } + +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */ + +  /* Outer loop handles each block in the MCU */ + +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { +    block = MCU_data[blkn]; +    ci = cinfo->MCU_membership[blkn]; +    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; + +    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ + +    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ +    st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + +    /* Figure F.19: Decode_DC_DIFF */ +    if (arith_decode(cinfo, st) == 0) +      entropy->dc_context[ci] = 0; +    else { +      /* Figure F.21: Decoding nonzero value v */ +      /* Figure F.22: Decoding the sign of v */ +      sign = arith_decode(cinfo, st + 1); +      st += 2; st += sign; +      /* Figure F.23: Decoding the magnitude category of v */ +      if ((m = arith_decode(cinfo, st)) != 0) { +	st = entropy->dc_stats[tbl] + 20;	/* Table F.4: X1 = 20 */ +	while (arith_decode(cinfo, st)) { +	  if ((m <<= 1) == 0x8000) { +	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	    entropy->ct = -1;			/* magnitude overflow */ +	    return TRUE; +	  } +	  st += 1; +	} +      } +      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ +      if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) +	entropy->dc_context[ci] = 0;		   /* zero diff category */ +      else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) +	entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ +      else +	entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */ +      v = m; +      /* Figure F.24: Decoding the magnitude bit pattern of v */ +      st += 14; +      while (m >>= 1) +	if (arith_decode(cinfo, st)) v |= m; +      v += 1; if (sign) v = -v; +      entropy->last_dc_val[ci] += v; +    } + +    /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ +    (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); +  } + +  return TRUE; +} + + +/* + * MCU decoding for AC initial scan (either spectral selection, + * or first pass of successive approximation). + */ + +METHODDEF(boolean) +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  JBLOCKROW block; +  unsigned char *st; +  int tbl, sign, k; +  int v, m; + +  /* Process restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) +      process_restart(cinfo); +    entropy->restarts_to_go--; +  } + +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */ + +  /* There is always only one block per MCU */ +  block = MCU_data[0]; +  tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + +  /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ + +  /* Figure F.20: Decode_AC_coefficients */ +  for (k = cinfo->Ss; k <= cinfo->Se; k++) { +    st = entropy->ac_stats[tbl] + 3 * (k - 1); +    if (arith_decode(cinfo, st)) break;		/* EOB flag */ +    while (arith_decode(cinfo, st + 1) == 0) { +      st += 3; k++; +      if (k > cinfo->Se) { +	WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	entropy->ct = -1;			/* spectral overflow */ +	return TRUE; +      } +    } +    /* Figure F.21: Decoding nonzero value v */ +    /* Figure F.22: Decoding the sign of v */ +    entropy->ac_stats[tbl][245] = 0; +    sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245); +    st += 2; +    /* Figure F.23: Decoding the magnitude category of v */ +    if ((m = arith_decode(cinfo, st)) != 0) { +      if (arith_decode(cinfo, st)) { +	m <<= 1; +	st = entropy->ac_stats[tbl] + +	     (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); +	while (arith_decode(cinfo, st)) { +	  if ((m <<= 1) == 0x8000) { +	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	    entropy->ct = -1;			/* magnitude overflow */ +	    return TRUE; +	  } +	  st += 1; +	} +      } +    } +    v = m; +    /* Figure F.24: Decoding the magnitude bit pattern of v */ +    st += 14; +    while (m >>= 1) +      if (arith_decode(cinfo, st)) v |= m; +    v += 1; if (sign) v = -v; +    /* Scale and output coefficient in natural (dezigzagged) order */ +    (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); +  } + +  return TRUE; +} + + +/* + * MCU decoding for DC successive approximation refinement scan. + */ + +METHODDEF(boolean) +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  unsigned char st[4]; +  int p1, blkn; + +  /* Process restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) +      process_restart(cinfo); +    entropy->restarts_to_go--; +  } + +  p1 = 1 << cinfo->Al;		/* 1 in the bit position being coded */ + +  /* Outer loop handles each block in the MCU */ + +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { +    st[0] = 0;	/* use fixed probability estimation */ +    /* Encoded data is simply the next bit of the two's-complement DC value */ +    if (arith_decode(cinfo, st)) +      MCU_data[blkn][0][0] |= p1; +  } + +  return TRUE; +} + + +/* + * MCU decoding for AC successive approximation refinement scan. + */ + +METHODDEF(boolean) +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  JBLOCKROW block; +  JCOEFPTR thiscoef; +  unsigned char *st; +  int tbl, k, kex; +  int p1, m1; + +  /* Process restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) +      process_restart(cinfo); +    entropy->restarts_to_go--; +  } + +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */ + +  /* There is always only one block per MCU */ +  block = MCU_data[0]; +  tbl = cinfo->cur_comp_info[0]->ac_tbl_no; + +  p1 = 1 << cinfo->Al;		/* 1 in the bit position being coded */ +  m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */ + +  /* Establish EOBx (previous stage end-of-block) index */ +  for (kex = cinfo->Se + 1; kex > 1; kex--) +    if ((*block)[jpeg_natural_order[kex - 1]]) break; + +  for (k = cinfo->Ss; k <= cinfo->Se; k++) { +    st = entropy->ac_stats[tbl] + 3 * (k - 1); +    if (k >= kex) +      if (arith_decode(cinfo, st)) break;	/* EOB flag */ +    for (;;) { +      thiscoef = *block + jpeg_natural_order[k]; +      if (*thiscoef) {				/* previously nonzero coef */ +	if (arith_decode(cinfo, st + 2)) { +	  if (*thiscoef < 0) +	    *thiscoef += m1; +	  else +	    *thiscoef += p1; +	} +	break; +      } +      if (arith_decode(cinfo, st + 1)) {	/* newly nonzero coef */ +	entropy->ac_stats[tbl][245] = 0; +	if (arith_decode(cinfo, entropy->ac_stats[tbl] + 245)) +	  *thiscoef = m1; +	else +	  *thiscoef = p1; +	break; +      } +      st += 3; k++; +      if (k > cinfo->Se) { +	WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	entropy->ct = -1;			/* spectral overflow */ +	return TRUE; +      } +    } +  } + +  return TRUE; +} + + +/* + * Decode one MCU's worth of arithmetic-compressed coefficients. + */ + +METHODDEF(boolean) +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  jpeg_component_info * compptr; +  JBLOCKROW block; +  unsigned char *st; +  int blkn, ci, tbl, sign, k; +  int v, m; + +  /* Process restart marker if needed */ +  if (cinfo->restart_interval) { +    if (entropy->restarts_to_go == 0) +      process_restart(cinfo); +    entropy->restarts_to_go--; +  } + +  if (entropy->ct == -1) return TRUE;	/* if error do nothing */ + +  /* Outer loop handles each block in the MCU */ + +  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { +    block = MCU_data[blkn]; +    ci = cinfo->MCU_membership[blkn]; +    compptr = cinfo->cur_comp_info[ci]; + +    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ + +    tbl = compptr->dc_tbl_no; + +    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ +    st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; + +    /* Figure F.19: Decode_DC_DIFF */ +    if (arith_decode(cinfo, st) == 0) +      entropy->dc_context[ci] = 0; +    else { +      /* Figure F.21: Decoding nonzero value v */ +      /* Figure F.22: Decoding the sign of v */ +      sign = arith_decode(cinfo, st + 1); +      st += 2; st += sign; +      /* Figure F.23: Decoding the magnitude category of v */ +      if ((m = arith_decode(cinfo, st)) != 0) { +	st = entropy->dc_stats[tbl] + 20;	/* Table F.4: X1 = 20 */ +	while (arith_decode(cinfo, st)) { +	  if ((m <<= 1) == 0x8000) { +	    WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	    entropy->ct = -1;			/* magnitude overflow */ +	    return TRUE; +	  } +	  st += 1; +	} +      } +      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ +      if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) +	entropy->dc_context[ci] = 0;		   /* zero diff category */ +      else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) +	entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ +      else +	entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */ +      v = m; +      /* Figure F.24: Decoding the magnitude bit pattern of v */ +      st += 14; +      while (m >>= 1) +	if (arith_decode(cinfo, st)) v |= m; +      v += 1; if (sign) v = -v; +      entropy->last_dc_val[ci] += v; +    } + +    (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; + +    /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ + +    tbl = compptr->ac_tbl_no; + +    /* Figure F.20: Decode_AC_coefficients */ +    for (k = 1; k < DCTSIZE2; k++) { +      st = entropy->ac_stats[tbl] + 3 * (k - 1); +      if (arith_decode(cinfo, st)) break;	/* EOB flag */ +      while (arith_decode(cinfo, st + 1) == 0) { +	st += 3; k++; +	if (k >= DCTSIZE2) { +	  WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	  entropy->ct = -1;			/* spectral overflow */ +	  return TRUE; +	} +      } +      /* Figure F.21: Decoding nonzero value v */ +      /* Figure F.22: Decoding the sign of v */ +      entropy->ac_stats[tbl][245] = 0; +      sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245); +      st += 2; +      /* Figure F.23: Decoding the magnitude category of v */ +      if ((m = arith_decode(cinfo, st)) != 0) { +	if (arith_decode(cinfo, st)) { +	  m <<= 1; +	  st = entropy->ac_stats[tbl] + +	       (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); +	  while (arith_decode(cinfo, st)) { +	    if ((m <<= 1) == 0x8000) { +	      WARNMS(cinfo, JWRN_ARITH_BAD_CODE); +	      entropy->ct = -1;			/* magnitude overflow */ +	      return TRUE; +	    } +	    st += 1; +	  } +	} +      } +      v = m; +      /* Figure F.24: Decoding the magnitude bit pattern of v */ +      st += 14; +      while (m >>= 1) +	if (arith_decode(cinfo, st)) v |= m; +      v += 1; if (sign) v = -v; +      (*block)[jpeg_natural_order[k]] = (JCOEF) v; +    } +  } + +  return TRUE; +} + + +/* + * Initialize for an arithmetic-compressed scan. + */ + +METHODDEF(void) +start_pass (j_decompress_ptr cinfo) +{ +  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; +  int ci, tbl; +  jpeg_component_info * compptr; + +  if (cinfo->progressive_mode) { +    /* Validate progressive scan parameters */ +    if (cinfo->Ss == 0) { +      if (cinfo->Se != 0) +	goto bad; +    } else { +      /* need not check Ss/Se < 0 since they came from unsigned bytes */ +      if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2) +	goto bad; +      /* AC scans may have only one component */ +      if (cinfo->comps_in_scan != 1) +	goto bad; +    } +    if (cinfo->Ah != 0) { +      /* Successive approximation refinement scan: must have Al = Ah-1. */ +      if (cinfo->Ah-1 != cinfo->Al) +	goto bad; +    } +    if (cinfo->Al > 13) {	/* need not check for < 0 */ +      bad: +      ERREXIT4(cinfo, JERR_BAD_PROGRESSION, +	       cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); +    } +    /* Update progression status, and verify that scan order is legal. +     * Note that inter-scan inconsistencies are treated as warnings +     * not fatal errors ... not clear if this is right way to behave. +     */ +    for (ci = 0; ci < cinfo->comps_in_scan; ci++) { +      int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; +      int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; +      if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ +	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); +      for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { +	int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; +	if (cinfo->Ah != expected) +	  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); +	coef_bit_ptr[coefi] = cinfo->Al; +      } +    } +    /* Select MCU decoding routine */ +    if (cinfo->Ah == 0) { +      if (cinfo->Ss == 0) +	entropy->pub.decode_mcu = decode_mcu_DC_first; +      else +	entropy->pub.decode_mcu = decode_mcu_AC_first; +    } else { +      if (cinfo->Ss == 0) +	entropy->pub.decode_mcu = decode_mcu_DC_refine; +      else +	entropy->pub.decode_mcu = decode_mcu_AC_refine; +    } +  } else { +    /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. +     * This ought to be an error condition, but we make it a warning because +     * there are some baseline files out there with all zeroes in these bytes. +     */ +    if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || +	cinfo->Ah != 0 || cinfo->Al != 0) +      WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); +    /* Select MCU decoding routine */ +    entropy->pub.decode_mcu = decode_mcu; +  } + +  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { +    compptr = cinfo->cur_comp_info[ci]; +    /* Allocate & initialize requested statistics areas */ +    if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { +      tbl = compptr->dc_tbl_no; +      if (tbl < 0 || tbl >= NUM_ARITH_TBLS) +	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); +      if (entropy->dc_stats[tbl] == NULL) +	entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); +      MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); +      /* Initialize DC predictions to 0 */ +      entropy->last_dc_val[ci] = 0; +      entropy->dc_context[ci] = 0; +    } +    if (cinfo->progressive_mode == 0 || cinfo->Ss) { +      tbl = compptr->ac_tbl_no; +      if (tbl < 0 || tbl >= NUM_ARITH_TBLS) +	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); +      if (entropy->ac_stats[tbl] == NULL) +	entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) +	  ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); +      MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); +    } +  } + +  /* Initialize arithmetic decoding variables */ +  entropy->c = 0; +  entropy->a = 0; +  entropy->ct = -16;	/* force reading 2 initial bytes to fill C */ + +  /* Initialize restart counter */ +  entropy->restarts_to_go = cinfo->restart_interval; +} + + +/* + * Module initialization routine for arithmetic entropy decoding. + */ + +GLOBAL(void) +jinit_arith_decoder (j_decompress_ptr cinfo) +{ +  arith_entropy_ptr entropy; +  int i; + +  entropy = (arith_entropy_ptr) +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, +				SIZEOF(arith_entropy_decoder)); +  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; +  entropy->pub.start_pass = start_pass; + +  /* Mark tables unallocated */ +  for (i = 0; i < NUM_ARITH_TBLS; i++) { +    entropy->dc_stats[i] = NULL; +    entropy->ac_stats[i] = NULL; +  } + +  if (cinfo->progressive_mode) { +    /* Create progression status table */ +    int *coef_bit_ptr, ci; +    cinfo->coef_bits = (int (*)[DCTSIZE2]) +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, +				  cinfo->num_components*DCTSIZE2*SIZEOF(int)); +    coef_bit_ptr = & cinfo->coef_bits[0][0]; +    for (ci = 0; ci < cinfo->num_components; ci++)  +      for (i = 0; i < DCTSIZE2; i++) +	*coef_bit_ptr++ = -1; +  } +} diff --git a/src/libjpeg/jdhuff.h b/src/libjpeg/jdhuff.h deleted file mode 100644 index ae19b6c..0000000 --- a/src/libjpeg/jdhuff.h +++ /dev/null @@ -1,201 +0,0 @@ -/* - * jdhuff.h - * - * Copyright (C) 1991-1997, 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 declarations for Huffman entropy decoding routines - * that are shared between the sequential decoder (jdhuff.c) and the - * progressive decoder (jdphuff.c).  No other modules need to see these. - */ - -/* Short forms of external names for systems with brain-damaged linkers. */ - -#ifdef NEED_SHORT_EXTERNAL_NAMES -#define jpeg_make_d_derived_tbl	jMkDDerived -#define jpeg_fill_bit_buffer	jFilBitBuf -#define jpeg_huff_decode	jHufDecode -#endif /* NEED_SHORT_EXTERNAL_NAMES */ - - -/* Derived data constructed for each Huffman table */ - -#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */ - -typedef struct { -  /* Basic tables: (element [0] of each array is unused) */ -  INT32 maxcode[18];		/* largest code of length k (-1 if none) */ -  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ -  INT32 valoffset[17];		/* huffval[] offset for codes of length k */ -  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less -   * the smallest code of length k; so given a code of length k, the -   * corresponding symbol is huffval[code + valoffset[k]] -   */ - -  /* Link to public Huffman table (needed only in jpeg_huff_decode) */ -  JHUFF_TBL *pub; - -  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of -   * the input data stream.  If the next Huffman code is no more -   * than HUFF_LOOKAHEAD bits long, we can obtain its length and -   * the corresponding symbol directly from these tables. -   */ -  int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */ -  UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */ -} d_derived_tbl; - -/* Expand a Huffman table definition into the derived format */ -EXTERN(void) jpeg_make_d_derived_tbl -	JPP((j_decompress_ptr cinfo, boolean isDC, int tblno, -	     d_derived_tbl ** pdtbl)); - - -/* - * Fetching the next N bits from the input stream is a time-critical operation - * for the Huffman decoders.  We implement it with a combination of inline - * macros and out-of-line subroutines.  Note that N (the number of bits - * demanded at one time) never exceeds 15 for JPEG use. - * - * We read source bytes into get_buffer and dole out bits as needed. - * If get_buffer already contains enough bits, they are fetched in-line - * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough - * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer - * as full as possible (not just to the number of bits needed; this - * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer). - * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension. - * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains - * at least the requested number of bits --- dummy zeroes are inserted if - * necessary. - */ - -typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */ -#define BIT_BUF_SIZE  32	/* size of buffer in bits */ - -/* If long is > 32 bits on your machine, and shifting/masking longs is - * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE - * appropriately should be a win.  Unfortunately we can't define the size - * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) - * because not all machines measure sizeof in 8-bit bytes. - */ - -typedef struct {		/* Bitreading state saved across MCUs */ -  bit_buf_type get_buffer;	/* current bit-extraction buffer */ -  int bits_left;		/* # of unused bits in it */ -} bitread_perm_state; - -typedef struct {		/* Bitreading working state within an MCU */ -  /* Current data source location */ -  /* We need a copy, rather than munging the original, in case of suspension */ -  const JOCTET * next_input_byte; /* => next byte to read from source */ -  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */ -  /* Bit input buffer --- note these values are kept in register variables, -   * not in this struct, inside the inner loops. -   */ -  bit_buf_type get_buffer;	/* current bit-extraction buffer */ -  int bits_left;		/* # of unused bits in it */ -  /* Pointer needed by jpeg_fill_bit_buffer. */ -  j_decompress_ptr cinfo;	/* back link to decompress master record */ -} bitread_working_state; - -/* Macros to declare and load/save bitread local variables. */ -#define BITREAD_STATE_VARS  \ -	register bit_buf_type get_buffer;  \ -	register int bits_left;  \ -	bitread_working_state br_state - -#define BITREAD_LOAD_STATE(cinfop,permstate)  \ -	br_state.cinfo = cinfop; \ -	br_state.next_input_byte = cinfop->src->next_input_byte; \ -	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \ -	get_buffer = permstate.get_buffer; \ -	bits_left = permstate.bits_left; - -#define BITREAD_SAVE_STATE(cinfop,permstate)  \ -	cinfop->src->next_input_byte = br_state.next_input_byte; \ -	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \ -	permstate.get_buffer = get_buffer; \ -	permstate.bits_left = bits_left - -/* - * These macros provide the in-line portion of bit fetching. - * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer - * before using GET_BITS, PEEK_BITS, or DROP_BITS. - * The variables get_buffer and bits_left are assumed to be locals, - * but the state struct might not be (jpeg_huff_decode needs this). - *	CHECK_BIT_BUFFER(state,n,action); - *		Ensure there are N bits in get_buffer; if suspend, take action. - *      val = GET_BITS(n); - *		Fetch next N bits. - *      val = PEEK_BITS(n); - *		Fetch next N bits without removing them from the buffer. - *	DROP_BITS(n); - *		Discard next N bits. - * The value N should be a simple variable, not an expression, because it - * is evaluated multiple times. - */ - -#define CHECK_BIT_BUFFER(state,nbits,action) \ -	{ if (bits_left < (nbits)) {  \ -	    if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \ -	      { action; }  \ -	    get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } - -#define GET_BITS(nbits) \ -	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1)) - -#define PEEK_BITS(nbits) \ -	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1)) - -#define DROP_BITS(nbits) \ -	(bits_left -= (nbits)) - -/* Load up the bit buffer to a depth of at least nbits */ -EXTERN(boolean) jpeg_fill_bit_buffer -	JPP((bitread_working_state * state, register bit_buf_type get_buffer, -	     register int bits_left, int nbits)); - - -/* - * Code for extracting next Huffman-coded symbol from input bit stream. - * Again, this is time-critical and we make the main paths be macros. - * - * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits - * without looping.  Usually, more than 95% of the Huffman codes will be 8 - * or fewer bits long.  The few overlength codes are handled with a loop, - * which need not be inline code. - * - * Notes about the HUFF_DECODE macro: - * 1. Near the end of the data segment, we may fail to get enough bits - *    for a lookahead.  In that case, we do it the hard way. - * 2. If the lookahead table contains no entry, the next code must be - *    more than HUFF_LOOKAHEAD bits long. - * 3. jpeg_huff_decode returns -1 if forced to suspend. - */ - -#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ -{ register int nb, look; \ -  if (bits_left < HUFF_LOOKAHEAD) { \ -    if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ -    get_buffer = state.get_buffer; bits_left = state.bits_left; \ -    if (bits_left < HUFF_LOOKAHEAD) { \ -      nb = 1; goto slowlabel; \ -    } \ -  } \ -  look = PEEK_BITS(HUFF_LOOKAHEAD); \ -  if ((nb = htbl->look_nbits[look]) != 0) { \ -    DROP_BITS(nb); \ -    result = htbl->look_sym[look]; \ -  } else { \ -    nb = HUFF_LOOKAHEAD+1; \ -slowlabel: \ -    if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \ -	{ failaction; } \ -    get_buffer = state.get_buffer; bits_left = state.bits_left; \ -  } \ -} - -/* Out-of-line case for Huffman code fetching */ -EXTERN(int) jpeg_huff_decode -	JPP((bitread_working_state * state, register bit_buf_type get_buffer, -	     register int bits_left, d_derived_tbl * htbl, int min_bits)); diff --git a/src/libjpeg/jdphuff.c b/src/libjpeg/jdphuff.c deleted file mode 100644 index 2267809..0000000 --- a/src/libjpeg/jdphuff.c +++ /dev/null @@ -1,668 +0,0 @@ -/* - * jdphuff.c - * - * Copyright (C) 1995-1997, 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 Huffman entropy decoding routines for progressive JPEG. - * - * Much of the complexity here has to do with supporting input suspension. - * If the data source module demands suspension, we want to be able to back - * up to the start of the current MCU.  To do this, we copy state variables - * into local working storage, and update them back to the permanent - * storage only upon successful completion of an MCU. - */ - -#define JPEG_INTERNALS -#include "jinclude.h" -#include "jpeglib.h" -#include "jdhuff.h"		/* Declarations shared with jdhuff.c */ - - -#ifdef D_PROGRESSIVE_SUPPORTED - -/* - * Expanded entropy decoder object for progressive Huffman decoding. - * - * The savable_state subrecord contains fields that change within an MCU, - * but must not be updated permanently until we complete the MCU. - */ - -typedef struct { -  unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */ -  int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */ -} savable_state; - -/* This macro is to work around compilers with missing or broken - * structure assignment.  You'll need to fix this code if you have - * such a compiler and you change MAX_COMPS_IN_SCAN. - */ - -#ifndef NO_STRUCT_ASSIGN -#define ASSIGN_STATE(dest,src)  ((dest) = (src)) -#else -#if MAX_COMPS_IN_SCAN == 4 -#define ASSIGN_STATE(dest,src)  \ -	((dest).EOBRUN = (src).EOBRUN, \ -	 (dest).last_dc_val[0] = (src).last_dc_val[0], \ -	 (dest).last_dc_val[1] = (src).last_dc_val[1], \ -	 (dest).last_dc_val[2] = (src).last_dc_val[2], \ -	 (dest).last_dc_val[3] = (src).last_dc_val[3]) -#endif -#endif - - -typedef struct { -  struct jpeg_entropy_decoder pub; /* public fields */ - -  /* These fields are loaded into local variables at start of each MCU. -   * In case of suspension, we exit WITHOUT updating them. -   */ -  bitread_perm_state bitstate;	/* Bit buffer at start of MCU */ -  savable_state saved;		/* Other state at start of MCU */ - -  /* These fields are NOT loaded into local working state. */ -  unsigned int restarts_to_go;	/* MCUs left in this restart interval */ - -  /* Pointers to derived tables (these workspaces have image lifespan) */ -  d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; - -  d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ -} phuff_entropy_decoder; - -typedef phuff_entropy_decoder * phuff_entropy_ptr; - -/* Forward declarations */ -METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo, -					    JBLOCKROW *MCU_data)); -METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo, -					    JBLOCKROW *MCU_data)); -METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo, -					     JBLOCKROW *MCU_data)); -METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, -					     JBLOCKROW *MCU_data)); - - -/* - * Initialize for a Huffman-compressed scan. - */ - -METHODDEF(void) -start_pass_phuff_decoder (j_decompress_ptr cinfo) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  boolean is_DC_band, bad; -  int ci, coefi, tbl; -  int *coef_bit_ptr; -  jpeg_component_info * compptr; - -  is_DC_band = (cinfo->Ss == 0); - -  /* Validate scan parameters */ -  bad = FALSE; -  if (is_DC_band) { -    if (cinfo->Se != 0) -      bad = TRUE; -  } else { -    /* need not check Ss/Se < 0 since they came from unsigned bytes */ -    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) -      bad = TRUE; -    /* AC scans may have only one component */ -    if (cinfo->comps_in_scan != 1) -      bad = TRUE; -  } -  if (cinfo->Ah != 0) { -    /* Successive approximation refinement scan: must have Al = Ah-1. */ -    if (cinfo->Al != cinfo->Ah-1) -      bad = TRUE; -  } -  if (cinfo->Al > 13)		/* need not check for < 0 */ -    bad = TRUE; -  /* Arguably the maximum Al value should be less than 13 for 8-bit precision, -   * but the spec doesn't say so, and we try to be liberal about what we -   * accept.  Note: large Al values could result in out-of-range DC -   * coefficients during early scans, leading to bizarre displays due to -   * overflows in the IDCT math.  But we won't crash. -   */ -  if (bad) -    ERREXIT4(cinfo, JERR_BAD_PROGRESSION, -	     cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); -  /* Update progression status, and verify that scan order is legal. -   * Note that inter-scan inconsistencies are treated as warnings -   * not fatal errors ... not clear if this is right way to behave. -   */ -  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { -    int cindex = cinfo->cur_comp_info[ci]->component_index; -    coef_bit_ptr = & cinfo->coef_bits[cindex][0]; -    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ -      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); -    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { -      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; -      if (cinfo->Ah != expected) -	WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); -      coef_bit_ptr[coefi] = cinfo->Al; -    } -  } - -  /* Select MCU decoding routine */ -  if (cinfo->Ah == 0) { -    if (is_DC_band) -      entropy->pub.decode_mcu = decode_mcu_DC_first; -    else -      entropy->pub.decode_mcu = decode_mcu_AC_first; -  } else { -    if (is_DC_band) -      entropy->pub.decode_mcu = decode_mcu_DC_refine; -    else -      entropy->pub.decode_mcu = decode_mcu_AC_refine; -  } - -  for (ci = 0; ci < cinfo->comps_in_scan; ci++) { -    compptr = cinfo->cur_comp_info[ci]; -    /* Make sure requested tables are present, and compute derived tables. -     * We may build same derived table more than once, but it's not expensive. -     */ -    if (is_DC_band) { -      if (cinfo->Ah == 0) {	/* DC refinement needs no table */ -	tbl = compptr->dc_tbl_no; -	jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, -				& entropy->derived_tbls[tbl]); -      } -    } else { -      tbl = compptr->ac_tbl_no; -      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, -			      & entropy->derived_tbls[tbl]); -      /* remember the single active table */ -      entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; -    } -    /* Initialize DC predictions to 0 */ -    entropy->saved.last_dc_val[ci] = 0; -  } - -  /* Initialize bitread state variables */ -  entropy->bitstate.bits_left = 0; -  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ -  entropy->pub.insufficient_data = FALSE; - -  /* Initialize private state variables */ -  entropy->saved.EOBRUN = 0; - -  /* Initialize restart counter */ -  entropy->restarts_to_go = cinfo->restart_interval; -} - - -/* - * Figure F.12: extend sign bit. - * On some machines, a shift and add will be faster than a table lookup. - */ - -#ifdef AVOID_TABLES - -#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) - -#else - -#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) - -static const int extend_test[16] =   /* entry n is 2**(n-1) */ -  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, -    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; - -static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ -  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, -    ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, -    ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, -    ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; - -#endif /* AVOID_TABLES */ - - -/* - * Check for a restart marker & resynchronize decoder. - * Returns FALSE if must suspend. - */ - -LOCAL(boolean) -process_restart (j_decompress_ptr cinfo) -{ -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  int ci; - -  /* Throw away any unused bits remaining in bit buffer; */ -  /* include any full bytes in next_marker's count of discarded bytes */ -  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; -  entropy->bitstate.bits_left = 0; - -  /* Advance past the RSTn marker */ -  if (! (*cinfo->marker->read_restart_marker) (cinfo)) -    return FALSE; - -  /* Re-initialize DC predictions to 0 */ -  for (ci = 0; ci < cinfo->comps_in_scan; ci++) -    entropy->saved.last_dc_val[ci] = 0; -  /* Re-init EOB run count, too */ -  entropy->saved.EOBRUN = 0; - -  /* Reset restart counter */ -  entropy->restarts_to_go = cinfo->restart_interval; - -  /* Reset out-of-data flag, unless read_restart_marker left us smack up -   * against a marker.  In that case we will end up treating the next data -   * segment as empty, and we can avoid producing bogus output pixels by -   * leaving the flag set. -   */ -  if (cinfo->unread_marker == 0) -    entropy->pub.insufficient_data = FALSE; - -  return TRUE; -} - - -/* - * Huffman MCU decoding. - * Each of these routines decodes and returns one MCU's worth of - * Huffman-compressed coefficients.  - * The coefficients are reordered from zigzag order into natural array order, - * but are not dequantized. - * - * The i'th block of the MCU is stored into the block pointed to by - * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. - * - * We return FALSE if data source requested suspension.  In that case no - * changes have been made to permanent state.  (Exception: some output - * coefficients may already have been assigned.  This is harmless for - * spectral selection, since we'll just re-assign them on the next call. - * Successive approximation AC refinement has to be more careful, however.) - */ - -/* - * MCU decoding for DC initial scan (either spectral selection, - * or first pass of successive approximation). - */ - -METHODDEF(boolean) -decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{    -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  int Al = cinfo->Al; -  register int s, r; -  int blkn, ci; -  JBLOCKROW block; -  BITREAD_STATE_VARS; -  savable_state state; -  d_derived_tbl * tbl; -  jpeg_component_info * compptr; - -  /* Process restart marker if needed; may have to suspend */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) -      if (! process_restart(cinfo)) -	return FALSE; -  } - -  /* If we've run out of data, just leave the MCU set to zeroes. -   * This way, we return uniform gray for the remainder of the segment. -   */ -  if (! entropy->pub.insufficient_data) { - -    /* Load up working state */ -    BITREAD_LOAD_STATE(cinfo,entropy->bitstate); -    ASSIGN_STATE(state, entropy->saved); - -    /* Outer loop handles each block in the MCU */ - -    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { -      block = MCU_data[blkn]; -      ci = cinfo->MCU_membership[blkn]; -      compptr = cinfo->cur_comp_info[ci]; -      tbl = entropy->derived_tbls[compptr->dc_tbl_no]; - -      /* Decode a single block's worth of coefficients */ - -      /* Section F.2.2.1: decode the DC coefficient difference */ -      HUFF_DECODE(s, br_state, tbl, return FALSE, label1); -      if (s) { -	CHECK_BIT_BUFFER(br_state, s, return FALSE); -	r = GET_BITS(s); -	s = HUFF_EXTEND(r, s); -      } - -      /* Convert DC difference to actual value, update last_dc_val */ -      s += state.last_dc_val[ci]; -      state.last_dc_val[ci] = s; -      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ -      (*block)[0] = (JCOEF) (s << Al); -    } - -    /* Completed MCU, so update state */ -    BITREAD_SAVE_STATE(cinfo,entropy->bitstate); -    ASSIGN_STATE(entropy->saved, state); -  } - -  /* Account for restart interval (no-op if not using restarts) */ -  entropy->restarts_to_go--; - -  return TRUE; -} - - -/* - * MCU decoding for AC initial scan (either spectral selection, - * or first pass of successive approximation). - */ - -METHODDEF(boolean) -decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{    -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  int Se = cinfo->Se; -  int Al = cinfo->Al; -  register int s, k, r; -  unsigned int EOBRUN; -  JBLOCKROW block; -  BITREAD_STATE_VARS; -  d_derived_tbl * tbl; - -  /* Process restart marker if needed; may have to suspend */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) -      if (! process_restart(cinfo)) -	return FALSE; -  } - -  /* If we've run out of data, just leave the MCU set to zeroes. -   * This way, we return uniform gray for the remainder of the segment. -   */ -  if (! entropy->pub.insufficient_data) { - -    /* Load up working state. -     * We can avoid loading/saving bitread state if in an EOB run. -     */ -    EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */ - -    /* There is always only one block per MCU */ - -    if (EOBRUN > 0)		/* if it's a band of zeroes... */ -      EOBRUN--;			/* ...process it now (we do nothing) */ -    else { -      BITREAD_LOAD_STATE(cinfo,entropy->bitstate); -      block = MCU_data[0]; -      tbl = entropy->ac_derived_tbl; - -      for (k = cinfo->Ss; k <= Se; k++) { -	HUFF_DECODE(s, br_state, tbl, return FALSE, label2); -	r = s >> 4; -	s &= 15; -	if (s) { -	  k += r; -	  CHECK_BIT_BUFFER(br_state, s, return FALSE); -	  r = GET_BITS(s); -	  s = HUFF_EXTEND(r, s); -	  /* Scale and output coefficient in natural (dezigzagged) order */ -	  (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al); -	} else { -	  if (r == 15) {	/* ZRL */ -	    k += 15;		/* skip 15 zeroes in band */ -	  } else {		/* EOBr, run length is 2^r + appended bits */ -	    EOBRUN = 1 << r; -	    if (r) {		/* EOBr, r > 0 */ -	      CHECK_BIT_BUFFER(br_state, r, return FALSE); -	      r = GET_BITS(r); -	      EOBRUN += r; -	    } -	    EOBRUN--;		/* this band is processed at this moment */ -	    break;		/* force end-of-band */ -	  } -	} -      } - -      BITREAD_SAVE_STATE(cinfo,entropy->bitstate); -    } - -    /* Completed MCU, so update state */ -    entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */ -  } - -  /* Account for restart interval (no-op if not using restarts) */ -  entropy->restarts_to_go--; - -  return TRUE; -} - - -/* - * MCU decoding for DC successive approximation refinement scan. - * Note: we assume such scans can be multi-component, although the spec - * is not very clear on the point. - */ - -METHODDEF(boolean) -decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{    -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */ -  int blkn; -  JBLOCKROW block; -  BITREAD_STATE_VARS; - -  /* Process restart marker if needed; may have to suspend */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) -      if (! process_restart(cinfo)) -	return FALSE; -  } - -  /* Not worth the cycles to check insufficient_data here, -   * since we will not change the data anyway if we read zeroes. -   */ - -  /* Load up working state */ -  BITREAD_LOAD_STATE(cinfo,entropy->bitstate); - -  /* Outer loop handles each block in the MCU */ - -  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { -    block = MCU_data[blkn]; - -    /* Encoded data is simply the next bit of the two's-complement DC value */ -    CHECK_BIT_BUFFER(br_state, 1, return FALSE); -    if (GET_BITS(1)) -      (*block)[0] |= p1; -    /* Note: since we use |=, repeating the assignment later is safe */ -  } - -  /* Completed MCU, so update state */ -  BITREAD_SAVE_STATE(cinfo,entropy->bitstate); - -  /* Account for restart interval (no-op if not using restarts) */ -  entropy->restarts_to_go--; - -  return TRUE; -} - - -/* - * MCU decoding for AC successive approximation refinement scan. - */ - -METHODDEF(boolean) -decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) -{    -  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; -  int Se = cinfo->Se; -  int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */ -  int m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */ -  register int s, k, r; -  unsigned int EOBRUN; -  JBLOCKROW block; -  JCOEFPTR thiscoef; -  BITREAD_STATE_VARS; -  d_derived_tbl * tbl; -  int num_newnz; -  int newnz_pos[DCTSIZE2]; - -  /* Process restart marker if needed; may have to suspend */ -  if (cinfo->restart_interval) { -    if (entropy->restarts_to_go == 0) -      if (! process_restart(cinfo)) -	return FALSE; -  } - -  /* If we've run out of data, don't modify the MCU. -   */ -  if (! entropy->pub.insufficient_data) { - -    /* Load up working state */ -    BITREAD_LOAD_STATE(cinfo,entropy->bitstate); -    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ - -    /* There is always only one block per MCU */ -    block = MCU_data[0]; -    tbl = entropy->ac_derived_tbl; - -    /* If we are forced to suspend, we must undo the assignments to any newly -     * nonzero coefficients in the block, because otherwise we'd get confused -     * next time about which coefficients were already nonzero. -     * But we need not undo addition of bits to already-nonzero coefficients; -     * instead, we can test the current bit to see if we already did it. -     */ -    num_newnz = 0; - -    /* initialize coefficient loop counter to start of band */ -    k = cinfo->Ss; - -    if (EOBRUN == 0) { -      for (; k <= Se; k++) { -	HUFF_DECODE(s, br_state, tbl, goto undoit, label3); -	r = s >> 4; -	s &= 15; -	if (s) { -	  if (s != 1)		/* size of new coef should always be 1 */ -	    WARNMS(cinfo, JWRN_HUFF_BAD_CODE); -	  CHECK_BIT_BUFFER(br_state, 1, goto undoit); -	  if (GET_BITS(1)) -	    s = p1;		/* newly nonzero coef is positive */ -	  else -	    s = m1;		/* newly nonzero coef is negative */ -	} else { -	  if (r != 15) { -	    EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */ -	    if (r) { -	      CHECK_BIT_BUFFER(br_state, r, goto undoit); -	      r = GET_BITS(r); -	      EOBRUN += r; -	    } -	    break;		/* rest of block is handled by EOB logic */ -	  } -	  /* note s = 0 for processing ZRL */ -	} -	/* Advance over already-nonzero coefs and r still-zero coefs, -	 * appending correction bits to the nonzeroes.  A correction bit is 1 -	 * if the absolute value of the coefficient must be increased. -	 */ -	do { -	  thiscoef = *block + jpeg_natural_order[k]; -	  if (*thiscoef != 0) { -	    CHECK_BIT_BUFFER(br_state, 1, goto undoit); -	    if (GET_BITS(1)) { -	      if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ -		if (*thiscoef >= 0) -		  *thiscoef += p1; -		else -		  *thiscoef += m1; -	      } -	    } -	  } else { -	    if (--r < 0) -	      break;		/* reached target zero coefficient */ -	  } -	  k++; -	} while (k <= Se); -	if (s) { -	  int pos = jpeg_natural_order[k]; -	  /* Output newly nonzero coefficient */ -	  (*block)[pos] = (JCOEF) s; -	  /* Remember its position in case we have to suspend */ -	  newnz_pos[num_newnz++] = pos; -	} -      } -    } - -    if (EOBRUN > 0) { -      /* Scan any remaining coefficient positions after the end-of-band -       * (the last newly nonzero coefficient, if any).  Append a correction -       * bit to each already-nonzero coefficient.  A correction bit is 1 -       * if the absolute value of the coefficient must be increased. -       */ -      for (; k <= Se; k++) { -	thiscoef = *block + jpeg_natural_order[k]; -	if (*thiscoef != 0) { -	  CHECK_BIT_BUFFER(br_state, 1, goto undoit); -	  if (GET_BITS(1)) { -	    if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ -	      if (*thiscoef >= 0) -		*thiscoef += p1; -	      else -		*thiscoef += m1; -	    } -	  } -	} -      } -      /* Count one block completed in EOB run */ -      EOBRUN--; -    } - -    /* Completed MCU, so update state */ -    BITREAD_SAVE_STATE(cinfo,entropy->bitstate); -    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ -  } - -  /* Account for restart interval (no-op if not using restarts) */ -  entropy->restarts_to_go--; - -  return TRUE; - -undoit: -  /* Re-zero any output coefficients that we made newly nonzero */ -  while (num_newnz > 0) -    (*block)[newnz_pos[--num_newnz]] = 0; - -  return FALSE; -} - - -/* - * Module initialization routine for progressive Huffman entropy decoding. - */ - -GLOBAL(void) -jinit_phuff_decoder (j_decompress_ptr cinfo) -{ -  phuff_entropy_ptr entropy; -  int *coef_bit_ptr; -  int ci, i; - -  entropy = (phuff_entropy_ptr) -    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, -				SIZEOF(phuff_entropy_decoder)); -  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; -  entropy->pub.start_pass = start_pass_phuff_decoder; - -  /* Mark derived tables unallocated */ -  for (i = 0; i < NUM_HUFF_TBLS; i++) { -    entropy->derived_tbls[i] = NULL; -  } - -  /* Create progression status table */ -  cinfo->coef_bits = (int (*)[DCTSIZE2]) -    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, -				cinfo->num_components*DCTSIZE2*SIZEOF(int)); -  coef_bit_ptr = & cinfo->coef_bits[0][0]; -  for (ci = 0; ci < cinfo->num_components; ci++)  -    for (i = 0; i < DCTSIZE2; i++) -      *coef_bit_ptr++ = -1; -} - -#endif /* D_PROGRESSIVE_SUPPORTED */ diff --git a/src/libjpeg/jidctred.c b/src/libjpeg/jidctred.c deleted file mode 100644 index 421f3c7..0000000 --- a/src/libjpeg/jidctred.c +++ /dev/null @@ -1,398 +0,0 @@ -/* - * jidctred.c - * - * Copyright (C) 1994-1998, 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 inverse-DCT routines that produce reduced-size output: - * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block. - * - * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M) - * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step - * with an 8-to-4 step that produces the four averages of two adjacent outputs - * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output). - * These steps were derived by computing the corresponding values at the end - * of the normal LL&M code, then simplifying as much as possible. - * - * 1x1 is trivial: just take the DC coefficient divided by 8. - * - * See jidctint.c for additional comments. - */ - -#define JPEG_INTERNALS -#include "jinclude.h" -#include "jpeglib.h" -#include "jdct.h"		/* Private declarations for DCT subsystem */ - -#ifdef IDCT_SCALING_SUPPORTED - - -/* - * This module is specialized to the case DCTSIZE = 8. - */ - -#if DCTSIZE != 8 -  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ -#endif - - -/* Scaling is the same as in jidctint.c. */ - -#if BITS_IN_JSAMPLE == 8 -#define CONST_BITS  13 -#define PASS1_BITS  2 -#else -#define CONST_BITS  13 -#define PASS1_BITS  1		/* lose a little precision to avoid overflow */ -#endif - -/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus - * causing a lot of useless floating-point operations at run time. - * To get around this we use the following pre-calculated constants. - * If you change CONST_BITS you may want to add appropriate values. - * (With a reasonable C compiler, you can just rely on the FIX() macro...) - */ - -#if CONST_BITS == 13 -#define FIX_0_211164243  ((INT32)  1730)	/* FIX(0.211164243) */ -#define FIX_0_509795579  ((INT32)  4176)	/* FIX(0.509795579) */ -#define FIX_0_601344887  ((INT32)  4926)	/* FIX(0.601344887) */ -#define FIX_0_720959822  ((INT32)  5906)	/* FIX(0.720959822) */ -#define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */ -#define FIX_0_850430095  ((INT32)  6967)	/* FIX(0.850430095) */ -#define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */ -#define FIX_1_061594337  ((INT32)  8697)	/* FIX(1.061594337) */ -#define FIX_1_272758580  ((INT32)  10426)	/* FIX(1.272758580) */ -#define FIX_1_451774981  ((INT32)  11893)	/* FIX(1.451774981) */ -#define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */ -#define FIX_2_172734803  ((INT32)  17799)	/* FIX(2.172734803) */ -#define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */ -#define FIX_3_624509785  ((INT32)  29692)	/* FIX(3.624509785) */ -#else -#define FIX_0_211164243  FIX(0.211164243) -#define FIX_0_509795579  FIX(0.509795579) -#define FIX_0_601344887  FIX(0.601344887) -#define FIX_0_720959822  FIX(0.720959822) -#define FIX_0_765366865  FIX(0.765366865) -#define FIX_0_850430095  FIX(0.850430095) -#define FIX_0_899976223  FIX(0.899976223) -#define FIX_1_061594337  FIX(1.061594337) -#define FIX_1_272758580  FIX(1.272758580) -#define FIX_1_451774981  FIX(1.451774981) -#define FIX_1_847759065  FIX(1.847759065) -#define FIX_2_172734803  FIX(2.172734803) -#define FIX_2_562915447  FIX(2.562915447) -#define FIX_3_624509785  FIX(3.624509785) -#endif - - -/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. - * For 8-bit samples with the recommended scaling, all the variable - * and constant values involved are no more than 16 bits wide, so a - * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. - * For 12-bit samples, a full 32-bit multiplication will be needed. - */ - -#if BITS_IN_JSAMPLE == 8 -#define MULTIPLY(var,const)  MULTIPLY16C16(var,const) -#else -#define MULTIPLY(var,const)  ((var) * (const)) -#endif - - -/* Dequantize a coefficient by multiplying it by the multiplier-table - * entry; produce an int result.  In this module, both inputs and result - * are 16 bits or less, so either int or short multiply will work. - */ - -#define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval)) - - -/* - * Perform dequantization and inverse DCT on one block of coefficients, - * producing a reduced-size 4x4 output block. - */ - -GLOBAL(void) -jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr, -	       JCOEFPTR coef_block, -	       JSAMPARRAY output_buf, JDIMENSION output_col) -{ -  INT32 tmp0, tmp2, tmp10, tmp12; -  INT32 z1, z2, z3, z4; -  JCOEFPTR inptr; -  ISLOW_MULT_TYPE * quantptr; -  int * wsptr; -  JSAMPROW outptr; -  JSAMPLE *range_limit = IDCT_range_limit(cinfo); -  int ctr; -  int workspace[DCTSIZE*4];	/* buffers data between passes */ -  SHIFT_TEMPS - -  /* Pass 1: process columns from input, store into work array. */ - -  inptr = coef_block; -  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; -  wsptr = workspace; -  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) { -    /* Don't bother to process column 4, because second pass won't use it */ -    if (ctr == DCTSIZE-4) -      continue; -    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && -	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 && -	inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) { -      /* AC terms all zero; we need not examine term 4 for 4x4 output */ -      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; -       -      wsptr[DCTSIZE*0] = dcval; -      wsptr[DCTSIZE*1] = dcval; -      wsptr[DCTSIZE*2] = dcval; -      wsptr[DCTSIZE*3] = dcval; -       -      continue; -    } -     -    /* Even part */ -     -    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); -    tmp0 <<= (CONST_BITS+1); -     -    z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); -    z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); - -    tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865); -     -    tmp10 = tmp0 + tmp2; -    tmp12 = tmp0 - tmp2; -     -    /* Odd part */ -     -    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); -    z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); -    z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); -    z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); -     -    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */ -	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */ -	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */ -	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */ -     -    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */ -	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */ -	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */ -	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */ - -    /* Final output stage */ -     -    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1); -    wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1); -    wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1); -    wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1); -  } -   -  /* Pass 2: process 4 rows from work array, store into output array. */ - -  wsptr = workspace; -  for (ctr = 0; ctr < 4; ctr++) { -    outptr = output_buf[ctr] + output_col; -    /* It's not clear whether a zero row test is worthwhile here ... */ - -#ifndef NO_ZERO_ROW_TEST -    if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && -	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { -      /* AC terms all zero */ -      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) -				  & RANGE_MASK]; -       -      outptr[0] = dcval; -      outptr[1] = dcval; -      outptr[2] = dcval; -      outptr[3] = dcval; -       -      wsptr += DCTSIZE;		/* advance pointer to next row */ -      continue; -    } -#endif -     -    /* Even part */ -     -    tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1); -     -    tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065) -	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865); -     -    tmp10 = tmp0 + tmp2; -    tmp12 = tmp0 - tmp2; -     -    /* Odd part */ -     -    z1 = (INT32) wsptr[7]; -    z2 = (INT32) wsptr[5]; -    z3 = (INT32) wsptr[3]; -    z4 = (INT32) wsptr[1]; -     -    tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */ -	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */ -	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */ -	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */ -     -    tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */ -	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */ -	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */ -	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */ - -    /* Final output stage */ -     -    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2, -					  CONST_BITS+PASS1_BITS+3+1) -			    & RANGE_MASK]; -    outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2, -					  CONST_BITS+PASS1_BITS+3+1) -			    & RANGE_MASK]; -    outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0, -					  CONST_BITS+PASS1_BITS+3+1) -			    & RANGE_MASK]; -    outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0, -					  CONST_BITS+PASS1_BITS+3+1) -			    & RANGE_MASK]; -     -    wsptr += DCTSIZE;		/* advance pointer to next row */ -  } -} - - -/* - * Perform dequantization and inverse DCT on one block of coefficients, - * producing a reduced-size 2x2 output block. - */ - -GLOBAL(void) -jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr, -	       JCOEFPTR coef_block, -	       JSAMPARRAY output_buf, JDIMENSION output_col) -{ -  INT32 tmp0, tmp10, z1; -  JCOEFPTR inptr; -  ISLOW_MULT_TYPE * quantptr; -  int * wsptr; -  JSAMPROW outptr; -  JSAMPLE *range_limit = IDCT_range_limit(cinfo); -  int ctr; -  int workspace[DCTSIZE*2];	/* buffers data between passes */ -  SHIFT_TEMPS - -  /* Pass 1: process columns from input, store into work array. */ - -  inptr = coef_block; -  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; -  wsptr = workspace; -  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) { -    /* Don't bother to process columns 2,4,6 */ -    if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6) -      continue; -    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 && -	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) { -      /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */ -      int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; -       -      wsptr[DCTSIZE*0] = dcval; -      wsptr[DCTSIZE*1] = dcval; -       -      continue; -    } -     -    /* Even part */ -     -    z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); -    tmp10 = z1 << (CONST_BITS+2); -     -    /* Odd part */ - -    z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); -    tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */ -    z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); -    tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */ -    z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); -    tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */ -    z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); -    tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */ - -    /* Final output stage */ -     -    wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2); -    wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2); -  } -   -  /* Pass 2: process 2 rows from work array, store into output array. */ - -  wsptr = workspace; -  for (ctr = 0; ctr < 2; ctr++) { -    outptr = output_buf[ctr] + output_col; -    /* It's not clear whether a zero row test is worthwhile here ... */ - -#ifndef NO_ZERO_ROW_TEST -    if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) { -      /* AC terms all zero */ -      JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3) -				  & RANGE_MASK]; -       -      outptr[0] = dcval; -      outptr[1] = dcval; -       -      wsptr += DCTSIZE;		/* advance pointer to next row */ -      continue; -    } -#endif -     -    /* Even part */ -     -    tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2); -     -    /* Odd part */ - -    tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */ -	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */ -	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */ -	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */ - -    /* Final output stage */ -     -    outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0, -					  CONST_BITS+PASS1_BITS+3+2) -			    & RANGE_MASK]; -    outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0, -					  CONST_BITS+PASS1_BITS+3+2) -			    & RANGE_MASK]; -     -    wsptr += DCTSIZE;		/* advance pointer to next row */ -  } -} - - -/* - * Perform dequantization and inverse DCT on one block of coefficients, - * producing a reduced-size 1x1 output block. - */ - -GLOBAL(void) -jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr, -	       JCOEFPTR coef_block, -	       JSAMPARRAY output_buf, JDIMENSION output_col) -{ -  int dcval; -  ISLOW_MULT_TYPE * quantptr; -  JSAMPLE *range_limit = IDCT_range_limit(cinfo); -  SHIFT_TEMPS - -  /* We hardly need an inverse DCT routine for this: just take the -   * average pixel value, which is one-eighth of the DC coefficient. -   */ -  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table; -  dcval = DEQUANTIZE(coef_block[0], quantptr[0]); -  dcval = (int) DESCALE((INT32) dcval, 3); - -  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK]; -} - -#endif /* IDCT_SCALING_SUPPORTED */ | 
