?? lossless_d.c
字號:
/* SPMG/JPEG-LS IMPLEMENTATION V.2.1 ===================================== These programs are Copyright (c) University of British Columbia. All rights reserved. They may be freely redistributed in their entirety provided that this copyright notice is not removed. THEY MAY NOT BE SOLD FOR PROFIT OR INCORPORATED IN COMMERCIAL PROGRAMS WITHOUT THE WRITTEN PERMISSION OF THE COPYRIGHT HOLDER. Each program is provided as is, without any express or implied warranty, without even the warranty of fitness for a particular purpose. ========================================================= THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls: ========================================================= LOCO-I/JPEG-LS IMPLEMENTATION V.0.90 ------------------------------------------------------------------------------- (c) COPYRIGHT HEWLETT-PACKARD COMPANY, 1995-1999. HEWLETT-PACKARD COMPANY ("HP") DOES NOT WARRANT THE ACCURACY OR COMPLETENESS OF THE INFORMATION GIVEN HERE. ANY USE MADE OF, OR RELIANCE ON, SUCH INFORMATION IS ENTIRELY AT USER'S OWN RISK. BY DOWNLOADING THE LOCO-I/JPEG-LS COMPRESSORS/DECOMPRESSORS ("THE SOFTWARE") YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS OF THIS LICENSING AGREEMENT. YOU MAY DOWNLOAD AND USE THE SOFTWARE FOR NON-COMMERCIAL PURPOSES FREE OF CHARGE OR FURTHER OBLIGATION. YOU MAY NOT, DIRECTLY OR INDIRECTLY, DISTRIBUTE THE SOFTWARE FOR A FEE, INCORPORATE THIS SOFTWARE INTO ANY PRODUCT OFFERED FOR SALE, OR USE THE SOFTWARE TO PROVIDE A SERVICE FOR WHICH A FEE IS CHARGED. YOU MAY MAKE COPIES OF THE SOFTWARE AND DISTRIBUTE SUCH COPIES TO OTHER PERSONS PROVIDED THAT SUCH COPIES ARE ACCOMPANIED BY HEWLETT-PACKARD'S COPYRIGHT NOTICE AND THIS AGREEMENT AND THAT SUCH OTHER PERSONS AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT. THE SOFTWARE IS NOT OF PRODUCT QUALITY AND MAY HAVE ERRORS OR DEFECTS. THE JPEG-LS STANDARD IS STILL UNDER DEVELOPMENT. THE SOFTWARE IS NOT A FINAL OR FULL IMPLEMENTATION OF THE STANDARD. HP GIVES NO EXPRESS OR IMPLIED WARRANTY OF ANY KIND AND ANY IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR PURPOSE ARE DISCLAIMED. HP SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE. -------------------------------------------------------------------------------*//* lossless_d.c --- the main pipeline which processes a scanline by doing * prediction, context computation, context quantization, * and statistics gathering. (for lossless mode) * * Initial code by Alex Jakulin, Aug. 1995 * * Modified and optimized: Gadiel Seroussi, October 1995 * * Modified and added Restart marker and input tables by: * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999 */#include "global.h"#include "bitio.h"#include <stdio.h>#include <math.h>static int eor_limit;/* Do Golomb-Rice statistics and DECODING for LOSSLESS images */__inline int lossless_regular_mode_d(int Q, int SIGN, int Px){ int At, Bt, Nt, Errval, absErrval; int current, k; /* This function is called only for regular contexts. End_of_run context is treated separately */ Nt = N[Q]; At = A[Q]; { /* Estimate k */ register nst = Nt; for(k=0; nst < At; nst *=2, k++); } /* Get the number of leading zeros */ absErrval = 0; do { int temp; temp = zeroLUT[reg >> 24]; absErrval += temp; if (temp != 8) { FILLBUFFER(temp + 1); break; } FILLBUFFER(8); } while (1); if ( absErrval < limit ) { /* now add the binary part of the Rice code */ if (k) { register unsigned long temp; absErrval <<= k; GETBITS(temp,k); absErrval += temp; } } else { /* the original unary would have been too long: (mapped value)-1 was sent verbatim */ GETBITS(absErrval, qbpp); absErrval ++; } /* Do the Rice mapping */ if ( absErrval & 1 ) { /* negative */ absErrval = (absErrval + 1) / 2; Errval = -absErrval; } else { absErrval /= 2; Errval = absErrval; } Bt = B[Q]; if ( k==0 && (2*Bt <= -Nt) ) { /* special case: see encoder side */ Errval = -(Errval+1); absErrval = (Errval<0)? (-Errval):Errval; } /* center, clip if necessary, and mask final error */ if ( SIGN == -1 ) { Px -= C[Q]; clip(Px, alpha); #if defined(POW2) /* this is valid if alpha is a power of 2 */ current = (Px - Errval)&(alpha-1);#else current = Px - Errval;#endif } else { Px += C[Q]; clip(Px,alpha); #if defined(POW2) /* valid if alpha is a power of 2 */ current = (Px + Errval)&(alpha-1);#else current = Px + Errval;#endif }#if !defined(POW2) /* reduce mod alpha, for arbitrary alpha */ if (current < 0) current += alpha; else if (current >= alpha) current -= alpha;#endif /* update bias stats */ B[Q] = (Bt += Errval); /* update Golomb-Rice stats */ A[Q] += absErrval; /* check reset (joint for Rice-Golomb and bias cancelation) */ if(Nt == reset) { N[Q] = (Nt >>= 1); A[Q] >>= 1; B[Q] = (Bt >>= 1); } /* Do bias estimation for NEXT pixel */ N[Q] = (++Nt); if ( Bt <= -Nt ) { if (C[Q] > MIN_C) --C[Q]; Bt = (B[Q] += Nt); if ( Bt <= -Nt ) B[Q] = -Nt+1; } else if ( Bt > 0 ) { if (C[Q] < MAX_C) ++C[Q]; Bt = (B[Q] -= Nt); if ( Bt > 0 ) B[Q] = 0; } return current;}/* Do end of run DECODING for LOSSLESS images */__inline pixel lossless_end_of_run_d(pixel Ra, pixel Rb, int RItype){ int Ix, Errval, absErrval, MErrval, k, Q, oldmap, Nt, At; Q = EOR_0 + RItype; Nt = N[Q], At = A[Q]; if ( RItype ) At += Nt/2; /* Estimate k */ for(k=0; Nt < At; Nt *=2, k++); /* read and decode the Golomb code */ /* Get the number of leading zeros */ MErrval = 0; do { int temp; temp = zeroLUT[reg >> 24]; MErrval += temp; if (temp != 8) { FILLBUFFER(temp + 1); break; } FILLBUFFER(8); } while (1); eor_limit = limit - limit_reduce; if ( MErrval < eor_limit ) { /* now add the binary part of the Golomb code */ if (k) { register unsigned long temp; MErrval <<= k; GETBITS(temp,k); MErrval += temp; } } else { /* the original unary would have been too long: (mapped value)-1 was sent verbatim */ GETBITS(MErrval, qbpp); MErrval ++; } oldmap = ( k==0 && (RItype||MErrval) && (2*B[Q]<Nt)); /* Note: the Boolean variable 'oldmap' is not identical to the variable 'map' in the JPEG-LS draft. We have oldmap = (qdiff<0) ? (1-map) : map; */ MErrval += ( RItype + oldmap ); if ( MErrval & 1 ) { /* negative */ Errval = oldmap - (MErrval+1)/2; absErrval = -Errval-RItype; B[Q]++; } else { /* nonnegative */ Errval = MErrval/2; absErrval = Errval-RItype; } if ( Rb < Ra )# ifdef POW2 Ix = ( Rb - Errval ) & (alpha-1);# else Ix = Rb - Errval;# endif else /* includes case a==b */# ifdef POW2 Ix = ( Rb + Errval ) & (alpha-1);# else Ix = Rb + Errval;# endif#if !defined(POW2) /* reduce mod alpha, for arbitrary alpha */ if (Ix < 0) Ix += alpha; else if (Ix >= alpha) Ix -= alpha;#endif /* update stats */ A[Q] += absErrval; if (N[Q] == reset) { N[Q] >>= 1; A[Q] >>= 1; B[Q] >>= 1; } N[Q]++; /* for next pixel */ return Ix; }/* For line and plane interleaved mode in LOSSLESS mode */int lossless_undoscanline( pixel *psl, /* previous scanline */ pixel *sl, /* current scanline */ int no, int color) /* number of values in it *//*** watch it! actual pixels in the scan line are numbered 1 to no . pixels with indices < 1 or > no are dummy "border" pixels */{ int i, psfix; pixel Ra, Rb, Rc, Rd; int SIGN; int cont; psfix = 0; /**********************************************/ /* Do for all pixels in the row in 8-bit mode */ /**********************************************/ if (bpp16==FALSE) { Rc = psl[0]; Rb = psl[1]; Ra = sl[0]; i = 1; do { pixel Px; Rd = psl[i + 1]; /* Quantize the gradient */ cont = vLUT[0][Rd - Rb + LUTMAX8] + vLUT[1][Rb - Rc + LUTMAX8] + vLUT[2][Rc - Ra + LUTMAX8]; if ( cont == 0 ) { /*********** RUN STATE **********/ register int n, m; /* get length of the run */ /* arg is # of pixels left */ m = n = process_run_dec(no-i+1, color); if ( m > 0 ) { /* run of nonzero length, otherwise we go directly to the end-of-run state */ do { sl[i++] = Ra; } while(--n > 0); if (i > no) /* end of line */ return 0; /* update context pixels */ Rb = psl[i]; Rd = psl[i + 1]; } /* Do end of run encoding for LOSSLESS images */ Ra = lossless_end_of_run_d(Ra, Rb, (Ra==Rb)); } /* Run state block */ else { /************ REGULAR CONTEXT **********/ predict(Rb, Ra, Rc); /* map symmetric contexts */ cont = classmap[cont]; if (cont < 0) { SIGN = -1; cont = -cont; } else SIGN = +1; /* decode a Rice code of a given context */ Ra = lossless_regular_mode_d(cont, SIGN, Px); } sl[i] = Ra; Rc = Rb; Rb = Rd; ++i; } while (i <= no);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -