| Index: jcarith.c
 | 
| diff --git a/jcarith.c b/jcarith.c
 | 
| index a9ca1c338c6284fe34e65ed8377e0a81abc52b8b..6d3b8af5b41dbeeccc009027b7aed89352af817e 100644
 | 
| --- a/jcarith.c
 | 
| +++ b/jcarith.c
 | 
| @@ -1,9 +1,12 @@
 | 
|  /*
 | 
|   * jcarith.c
 | 
|   *
 | 
| + * This file was part of the Independent JPEG Group's software:
 | 
|   * Developed 1997-2009 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.
 | 
| + * libjpeg-turbo Modifications:
 | 
| + * Copyright (C) 2015, D. R. Commander.
 | 
| + * For conditions of distribution and use, see the accompanying README.ijg
 | 
| + * 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).
 | 
| @@ -23,10 +26,10 @@
 | 
|  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 *
 | 
| +  JLONG c; /* C register, base of coding interval, layout as in sec. D.1.3 */
 | 
| +  JLONG a;               /* A register, normalized size of coding interval */
 | 
| +  JLONG sc;        /* counter for stacked 0xFF values which might overflow */
 | 
| +  JLONG 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 */
 | 
| @@ -34,18 +37,18 @@ typedef struct {
 | 
|    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) */
 | 
| +  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];
 | 
| +  unsigned char *dc_stats[NUM_ARITH_TBLS];
 | 
| +  unsigned char *ac_stats[NUM_ARITH_TBLS];
 | 
|  
 | 
|    /* Statistics bin for coding with fixed probability 0.5 */
 | 
|    unsigned char fixed_bin[4];
 | 
|  } arith_entropy_encoder;
 | 
|  
 | 
| -typedef arith_entropy_encoder * arith_entropy_ptr;
 | 
| +typedef arith_entropy_encoder *arith_entropy_ptr;
 | 
|  
 | 
|  /* The following two definitions specify the allocation chunk size
 | 
|   * for the statistics area.
 | 
| @@ -95,20 +98,20 @@ typedef arith_entropy_encoder * arith_entropy_ptr;
 | 
|  #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,
 | 
| +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
 | 
| + * We assume that int right shift is unsigned if JLONG right shift is,
 | 
|   * which should be safe.
 | 
|   */
 | 
|  
 | 
|  #ifdef RIGHT_SHIFT_IS_UNSIGNED
 | 
| -#define ISHIFT_TEMPS	int ishift_temp;
 | 
| +#define ISHIFT_TEMPS    int ishift_temp;
 | 
|  #define IRIGHT_SHIFT(x,shft)  \
 | 
| -	((ishift_temp = (x)) < 0 ? \
 | 
| -	 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
 | 
| -	 (ishift_temp >> (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))
 | 
| +#define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
 | 
|  #endif
 | 
|  
 | 
|  
 | 
| @@ -116,7 +119,7 @@ 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;
 | 
| +  struct jpeg_destination_mgr *dest = cinfo->dest;
 | 
|  
 | 
|    *dest->next_output_byte++ = (JOCTET) val;
 | 
|    if (--dest->free_in_buffer == 0)
 | 
| @@ -133,7 +136,7 @@ METHODDEF(void)
 | 
|  finish_pass (j_compress_ptr cinfo)
 | 
|  {
 | 
|    arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
 | 
| -  INT32 temp;
 | 
| +  JLONG temp;
 | 
|  
 | 
|    /* Section D.1.8: Termination of encoding */
 | 
|  
 | 
| @@ -149,11 +152,11 @@ finish_pass (j_compress_ptr cinfo)
 | 
|      /* One final overflow has to be handled */
 | 
|      if (e->buffer >= 0) {
 | 
|        if (e->zc)
 | 
| -	do emit_byte(0x00, cinfo);
 | 
| -	while (--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);
 | 
| +        emit_byte(0x00, cinfo);
 | 
|      }
 | 
|      e->zc += e->sc;  /* carry-over converts stacked 0xFF bytes to 0x00 */
 | 
|      e->sc = 0;
 | 
| @@ -162,17 +165,17 @@ finish_pass (j_compress_ptr cinfo)
 | 
|        ++e->zc;
 | 
|      else if (e->buffer >= 0) {
 | 
|        if (e->zc)
 | 
| -	do emit_byte(0x00, cinfo);
 | 
| -	while (--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(0x00, cinfo);
 | 
| +        while (--e->zc);
 | 
|        do {
 | 
| -	emit_byte(0xFF, cinfo);
 | 
| -	emit_byte(0x00, cinfo);
 | 
| +        emit_byte(0xFF, cinfo);
 | 
| +        emit_byte(0x00, cinfo);
 | 
|        } while (--e->sc);
 | 
|      }
 | 
|    }
 | 
| @@ -187,7 +190,7 @@ finish_pass (j_compress_ptr cinfo)
 | 
|      if (e->c & 0x7F800L) {
 | 
|        emit_byte((e->c >> 11) & 0xFF, cinfo);
 | 
|        if (((e->c >> 11) & 0xFF) == 0xFF)
 | 
| -	emit_byte(0x00, cinfo);
 | 
| +        emit_byte(0x00, cinfo);
 | 
|      }
 | 
|    }
 | 
|  }
 | 
| @@ -216,20 +219,20 @@ finish_pass (j_compress_ptr cinfo)
 | 
|   */
 | 
|  
 | 
|  LOCAL(void)
 | 
| -arith_encode (j_compress_ptr cinfo, unsigned char *st, int val) 
 | 
| +arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
 | 
|  {
 | 
|    register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
 | 
|    register unsigned char nl, nm;
 | 
| -  register INT32 qe, temp;
 | 
| +  register JLONG 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 = jpeg_aritab[sv & 0x7F];	/* => Qe_Value */
 | 
| -  nl = qe & 0xFF; qe >>= 8;	/* Next_Index_LPS + Switch_MPS */
 | 
| -  nm = qe & 0xFF; qe >>= 8;	/* Next_Index_MPS */
 | 
| +  qe = jpeg_aritab[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;
 | 
| @@ -243,7 +246,7 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
 | 
|        e->c += e->a;
 | 
|        e->a = qe;
 | 
|      }
 | 
| -    *st = (sv & 0x80) ^ nl;	/* Estimate_after_LPS */
 | 
| +    *st = (sv & 0x80) ^ nl;     /* Estimate_after_LPS */
 | 
|    } else {
 | 
|      /* Encode the more probable symbol */
 | 
|      if (e->a >= 0x8000L)
 | 
| @@ -255,7 +258,7 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
 | 
|        e->c += e->a;
 | 
|        e->a = qe;
 | 
|      }
 | 
| -    *st = (sv & 0x80) ^ nm;	/* Estimate_after_MPS */
 | 
| +    *st = (sv & 0x80) ^ nm;     /* Estimate_after_MPS */
 | 
|    }
 | 
|  
 | 
|    /* Renormalization & data output per section D.1.6 */
 | 
| @@ -266,43 +269,43 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
 | 
|        /* 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 */
 | 
| +        /* 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) */
 | 
| +        ++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) */
 | 
| +        /* 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;
 | 
| @@ -320,7 +323,7 @@ emit_restart (j_compress_ptr cinfo, int restart_num)
 | 
|  {
 | 
|    arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
 | 
|    int ci;
 | 
| -  jpeg_component_info * compptr;
 | 
| +  jpeg_component_info *compptr;
 | 
|  
 | 
|    finish_pass(cinfo);
 | 
|  
 | 
| @@ -398,45 +401,45 @@ encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|      /* 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 */
 | 
| +      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 */
 | 
| +        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 */
 | 
| +        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, 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) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
 | 
| -	entropy->dc_context[ci] = 0;	/* zero diff category */
 | 
| +        entropy->dc_context[ci] = 0;    /* zero diff category */
 | 
|        else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
 | 
| -	entropy->dc_context[ci] += 8;	/* large diff category */
 | 
| +        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);
 | 
| +        arith_encode(cinfo, st, (m & v) ? 1 : 0);
 | 
|      }
 | 
|    }
 | 
|  
 | 
| @@ -491,21 +494,21 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|    /* 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 */
 | 
| +    arith_encode(cinfo, st, 0);         /* EOB decision */
 | 
|      for (;;) {
 | 
|        if ((v = (*block)[jpeg_natural_order[k]]) >= 0) {
 | 
| -	if (v >>= cinfo->Al) {
 | 
| -	  arith_encode(cinfo, st + 1, 1);
 | 
| -	  arith_encode(cinfo, entropy->fixed_bin, 0);
 | 
| -	  break;
 | 
| -	}
 | 
| +        if (v >>= cinfo->Al) {
 | 
| +          arith_encode(cinfo, st + 1, 1);
 | 
| +          arith_encode(cinfo, entropy->fixed_bin, 0);
 | 
| +          break;
 | 
| +        }
 | 
|        } else {
 | 
| -	v = -v;
 | 
| -	if (v >>= cinfo->Al) {
 | 
| -	  arith_encode(cinfo, st + 1, 1);
 | 
| -	  arith_encode(cinfo, entropy->fixed_bin, 1);
 | 
| -	  break;
 | 
| -	}
 | 
| +        v = -v;
 | 
| +        if (v >>= cinfo->Al) {
 | 
| +          arith_encode(cinfo, st + 1, 1);
 | 
| +          arith_encode(cinfo, entropy->fixed_bin, 1);
 | 
| +          break;
 | 
| +        }
 | 
|        }
 | 
|        arith_encode(cinfo, st + 1, 0); st += 3; k++;
 | 
|      }
 | 
| @@ -517,15 +520,15 @@ encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|        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, 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);
 | 
| @@ -566,7 +569,7 @@ encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|      entropy->restarts_to_go--;
 | 
|    }
 | 
|  
 | 
| -  st = entropy->fixed_bin;	/* use fixed probability estimation */
 | 
| +  st = entropy->fixed_bin;      /* use fixed probability estimation */
 | 
|    Al = cinfo->Al;
 | 
|  
 | 
|    /* Encode the MCU data blocks */
 | 
| @@ -635,29 +638,29 @@ encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|    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 */
 | 
| +      arith_encode(cinfo, st, 0);       /* EOB decision */
 | 
|      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->fixed_bin, 0);
 | 
| -	  }
 | 
| -	  break;
 | 
| -	}
 | 
| +        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->fixed_bin, 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->fixed_bin, 1);
 | 
| -	  }
 | 
| -	  break;
 | 
| -	}
 | 
| +        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->fixed_bin, 1);
 | 
| +          }
 | 
| +          break;
 | 
| +        }
 | 
|        }
 | 
|        arith_encode(cinfo, st + 1, 0); st += 3; k++;
 | 
|      }
 | 
| @@ -680,7 +683,7 @@ METHODDEF(boolean)
 | 
|  encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|  {
 | 
|    arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
 | 
| -  jpeg_component_info * compptr;
 | 
| +  jpeg_component_info *compptr;
 | 
|    JBLOCKROW block;
 | 
|    unsigned char *st;
 | 
|    int blkn, ci, tbl, k, ke;
 | 
| @@ -713,45 +716,45 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|      /* 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 */
 | 
| +      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 */
 | 
| +        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 */
 | 
| +        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, 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) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
 | 
| -	entropy->dc_context[ci] = 0;	/* zero diff category */
 | 
| +        entropy->dc_context[ci] = 0;    /* zero diff category */
 | 
|        else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
 | 
| -	entropy->dc_context[ci] += 8;	/* large diff category */
 | 
| +        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);
 | 
| +        arith_encode(cinfo, st, (m & v) ? 1 : 0);
 | 
|      }
 | 
|  
 | 
|      /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */
 | 
| @@ -765,43 +768,43 @@ encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 | 
|      /* 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 */
 | 
| +      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, 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 */
 | 
|        if (v > 0) {
 | 
| -	arith_encode(cinfo, entropy->fixed_bin, 0);
 | 
| +        arith_encode(cinfo, entropy->fixed_bin, 0);
 | 
|        } else {
 | 
| -	v = -v;
 | 
| -	arith_encode(cinfo, entropy->fixed_bin, 1);
 | 
| +        v = -v;
 | 
| +        arith_encode(cinfo, entropy->fixed_bin, 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, 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);
 | 
| +        arith_encode(cinfo, st, (m & v) ? 1 : 0);
 | 
|      }
 | 
|      /* Encode EOB decision only if k <= DCTSIZE2 - 1 */
 | 
|      if (k <= DCTSIZE2 - 1) {
 | 
| @@ -823,7 +826,7 @@ 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;
 | 
| +  jpeg_component_info *compptr;
 | 
|  
 | 
|    if (gather_statistics)
 | 
|      /* Make sure to avoid that in the master control logic!
 | 
| @@ -838,14 +841,14 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
 | 
|    if (cinfo->progressive_mode) {
 | 
|      if (cinfo->Ah == 0) {
 | 
|        if (cinfo->Ss == 0)
 | 
| -	entropy->pub.encode_mcu = encode_mcu_DC_first;
 | 
| +        entropy->pub.encode_mcu = encode_mcu_DC_first;
 | 
|        else
 | 
| -	entropy->pub.encode_mcu = encode_mcu_AC_first;
 | 
| +        entropy->pub.encode_mcu = encode_mcu_AC_first;
 | 
|      } else {
 | 
|        if (cinfo->Ss == 0)
 | 
| -	entropy->pub.encode_mcu = encode_mcu_DC_refine;
 | 
| +        entropy->pub.encode_mcu = encode_mcu_DC_refine;
 | 
|        else
 | 
| -	entropy->pub.encode_mcu = encode_mcu_AC_refine;
 | 
| +        entropy->pub.encode_mcu = encode_mcu_AC_refine;
 | 
|      }
 | 
|    } else
 | 
|      entropy->pub.encode_mcu = encode_mcu;
 | 
| @@ -857,10 +860,10 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
 | 
|      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);
 | 
| +        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);
 | 
| +        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;
 | 
| @@ -870,15 +873,15 @@ start_pass (j_compress_ptr cinfo, boolean gather_statistics)
 | 
|      if (cinfo->progressive_mode == 0 || cinfo->Se) {
 | 
|        tbl = compptr->ac_tbl_no;
 | 
|        if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
 | 
| -	ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
 | 
| +        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);
 | 
| +        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);
 | 
| +        /* 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
 | 
|      }
 | 
|    }
 | 
| @@ -909,7 +912,7 @@ jinit_arith_encoder (j_compress_ptr cinfo)
 | 
|  
 | 
|    entropy = (arith_entropy_ptr)
 | 
|      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 | 
| -				SIZEOF(arith_entropy_encoder));
 | 
| +                                sizeof(arith_entropy_encoder));
 | 
|    cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
 | 
|    entropy->pub.start_pass = start_pass;
 | 
|    entropy->pub.finish_pass = finish_pass;
 | 
| 
 |