1 /* SPMG/JPEG-LS IMPLEMENTATION V.2.1
2 =====================================
3 These programs are Copyright (c) University of British Columbia. All rights reserved.
4 They may be freely redistributed in their entirety provided that this copyright
5 notice is not removed. THEY MAY NOT BE SOLD FOR PROFIT OR INCORPORATED IN
6 COMMERCIAL PROGRAMS WITHOUT THE WRITTEN PERMISSION OF THE COPYRIGHT HOLDER.
7 Each program is provided as is, without any express or implied warranty,
8 without even the warranty of fitness for a particular purpose.
10 =========================================================
11 THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:
12 =========================================================
14 LOCO-I/JPEG-LS IMPLEMENTATION V.0.90
15 -------------------------------------------------------------------------------
16 (c) COPYRIGHT HEWLETT-PACKARD COMPANY, 1995-1999.
17 HEWLETT-PACKARD COMPANY ("HP") DOES NOT WARRANT THE ACCURACY OR
18 COMPLETENESS OF THE INFORMATION GIVEN HERE. ANY USE MADE OF, OR
19 RELIANCE ON, SUCH INFORMATION IS ENTIRELY AT USER'S OWN RISK.
20 BY DOWNLOADING THE LOCO-I/JPEG-LS COMPRESSORS/DECOMPRESSORS
21 ("THE SOFTWARE") YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS
22 OF THIS LICENSING AGREEMENT.
23 YOU MAY DOWNLOAD AND USE THE SOFTWARE FOR NON-COMMERCIAL PURPOSES
24 FREE OF CHARGE OR FURTHER OBLIGATION. YOU MAY NOT, DIRECTLY OR
25 INDIRECTLY, DISTRIBUTE THE SOFTWARE FOR A FEE, INCORPORATE THIS
26 SOFTWARE INTO ANY PRODUCT OFFERED FOR SALE, OR USE THE SOFTWARE
27 TO PROVIDE A SERVICE FOR WHICH A FEE IS CHARGED.
28 YOU MAY MAKE COPIES OF THE SOFTWARE AND DISTRIBUTE SUCH COPIES TO
29 OTHER PERSONS PROVIDED THAT SUCH COPIES ARE ACCOMPANIED BY
30 HEWLETT-PACKARD'S COPYRIGHT NOTICE AND THIS AGREEMENT AND THAT
31 SUCH OTHER PERSONS AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT.
32 THE SOFTWARE IS NOT OF PRODUCT QUALITY AND MAY HAVE ERRORS OR DEFECTS.
33 THE JPEG-LS STANDARD IS STILL UNDER DEVELOPMENT. THE SOFTWARE IS NOT A
34 FINAL OR FULL IMPLEMENTATION OF THE STANDARD. HP GIVES NO EXPRESS OR
35 IMPLIED WARRANTY OF ANY KIND AND ANY IMPLIED WARRANTIES OF
36 MERCHANTABILITY AND FITNESS FOR PURPOSE ARE DISCLAIMED.
37 HP SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
38 OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE.
39 -------------------------------------------------------------------------------
42 /* lossy_d.c --- the main pipeline which processes a scanline by doing
43 * prediction, context computation, context quantization,
44 * and statistics gathering. (for LOSSY images)
46 * Initial code by Alex Jakulin, Aug. 1995
48 * Modified and optimized: Gadiel Seroussi, October 1995
50 * Modified and added Restart marker and input tables by:
51 * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
62 /* Do Golomb-Rice statistics and DECODING for LOSSY images*/
63 inline int lossy_regular_mode_d(int Q, int SIGN, int Px)
65 int At, Bt, Nt, Errval, absErrval;
68 /* This function is called only for regular contexts.
69 End_of_run context is treated separately */
76 for(k=0; nst < At; nst *=2, k++);
79 /* Get the number of leading zeros */
84 temp = zeroLUT[reg >> 24];
93 if ( absErrval < limit ) {
94 /* now add the binary part of the Rice code */
96 register unsigned long temp;
103 /* the original unary would have been too long:
104 (mapped value)-1 was sent verbatim */
105 GETBITS(absErrval, qbpp);
109 /* Do the Rice mapping */
110 if ( absErrval & 1 ) { /* negative */
111 absErrval = (absErrval + 1) / 2;
120 /* if ( k==0 && (2*Bt <= -qmul[Nt]) ) */
121 if ( k==0 && NEAR==0 && (2*Bt <= -Nt) )
123 /* special case: see encoder side */
124 Errval = -(Errval+1);
125 absErrval = (Errval<0)?(-Errval):Errval;
128 Errval = qmul[Errval]; /* dequantize prediction error */
130 /* center, clip if necessary, and mask final error */
134 current = Px - Errval;
139 current = Px + Errval;
142 /* first, we reduce mod beta in the range -NEAR <= x <= alpha-1+NEAR,
143 then we clip to [0,alpha] */
144 if (current < negNEAR)
146 else if (current > alpha1eps)
151 /* update bias stats */
152 B[Q] = (Bt += Errval);
154 /* update Golomb-Rice stats */
157 /* check reset (joint for Rice-Golomb and bias cancelation) */
164 /* Do bias estimation for NEXT pixel */
176 } else if ( Bt > 0 ) {
195 /* Do end of run DECODING for LOSSY images */
196 inline pixel lossy_end_of_run_d(pixel Ra, pixel Rb, int RItype)
217 for(k=0; Nt < At; Nt *=2, k++);
219 /* read and decode the Golomb code */
220 /* Get the number of leading zeros */
225 temp = zeroLUT[reg >> 24];
228 FILLBUFFER(temp + 1);
234 eor_limit = limit - limit_reduce;
236 if ( MErrval < eor_limit ) {
237 /* now add the binary part of the Golomb code */
239 register unsigned long temp;
246 /* the original unary would have been too long:
247 (mapped value)-1 was sent verbatim */
248 GETBITS(MErrval, qbpp);
252 oldmap = ( k==0 && (RItype||MErrval) && (2*B[Q]<Nt));
254 Note: the Boolean variable 'oldmap' is not
255 identical to the variable 'map' in the
256 JPEG-LS draft. We have
257 oldmap = (qdiff<0) ? (1-map) : map;
260 MErrval += ( RItype + oldmap );
262 if ( MErrval & 1 ) { /* negative */
263 Errval = oldmap - (MErrval+1)/2;
264 absErrval = -Errval-RItype;
267 else { /* nonnegative */
269 absErrval = Errval-RItype;
272 Errval = qmul[Errval]; /* de-quantize prediction error */
285 else if ( Ix > alpha1eps )
298 N[Q]++; /* for next pixel */
308 /* For DECODING line and plane interleaved mode for LOSSY images*/
309 int lossy_undoscanline( pixel *psl, /* previous scanline */
310 pixel *sl, /* current scanline */
311 int no, int color) /* number of values in it */
312 /*** watch it! actual pixels in the scan line are numbered 1 to no .
313 pixels with indices < 1 or > no are dummy "border" pixels */
316 pixel Ra, Rb, Rc, Rd;
323 /**********************************************/
324 /* Do for all pixels in the row in 8-bit mode */
325 /**********************************************/
338 /* Quantize the gradient */
339 cont = vLUT[0][Rd - Rb + LUTMAX8] +
340 vLUT[1][Rb - Rc + LUTMAX8] +
341 vLUT[2][Rc - Ra + LUTMAX8];
345 /********** RUN STATE *********/
349 /* get length of the run */
350 /* arg is # of pixels left */
351 m = n= process_run_dec(no-i+1, color);
353 if ( m > 0 ) { /* run of nonzero length, otherwise
354 we go directly to the end-of-run
364 /* update context pixels */
369 /* here we handle the "end-of-run" state */
370 run_int_type = ( (Rb-Ra) <= NEAR && (Rb-Ra) >= negNEAR);
371 Ra = lossy_end_of_run_d(Ra, Rb, run_int_type);
375 /****** REGULAR CONTEXT ******/
379 /* map symmetric contexts */
380 cont = classmap[cont];
390 /* decode a Rice code of a given context */
391 Ra = lossy_regular_mode_d(cont, SIGN, Px);
402 /***********************************************/
403 /* Do for all pixels in the row in 16-bit mode */
404 /***********************************************/
407 Rc = ENDIAN16(psl[0]);
408 Rb = ENDIAN16(psl[1]);
409 Ra = ENDIAN16(sl[0]);
415 Rd = ENDIAN16(psl[i + 1]);
417 /* Quantize the gradient */
421 /* Following segment assumes that T3 <= LUTMAX16 */
422 /* This condition should have been checked when the
423 lookup tables were built */
426 cont = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
428 cont = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
432 cont += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
434 cont += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
438 cont += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
440 cont += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
445 /********* RUN STATE *********/
449 /* get length of the run */
450 /* arg is # of pixels left */
451 m = n = process_run_dec(no-i+1, color);
453 if ( m > 0 ) { /* run of nonzero length, otherwise
454 we go directly to the end-of-run
457 sl[i++] = ENDIAN16(Ra);
464 /* update context pixels */
465 Rb = ENDIAN16(psl[i]);
466 Rd = ENDIAN16(psl[i + 1]);
469 /* here we handle the "end-of-run" state, which is
470 treated separately from regular states */
471 run_int_type = ( (Rb-Ra) <= NEAR && (Rb-Ra) >= negNEAR);
472 Ra = lossy_end_of_run_d(Ra, Rb, run_int_type);
477 /******REGULAR CONTEXT ******/
481 /* map symmetric contexts */
482 cont = classmap[cont];
492 /* decode a Rice code of a given context */
493 Ra = lossy_regular_mode_d(cont, SIGN, Px);
496 sl[i] = ENDIAN16(Ra);
503 } /* ends "if 8/16 bit" */
514 /* For DECODING pixel interleaved mode in LOSSY mode */
515 int lossy_undoscanline_pixel( pixel *psl, /* previous scanline */
516 pixel *sl, /* current scanline */
517 int no) /* number of values in it */
518 /*** watch it! actual pixels in the scan line are numbered 1 to no .
519 pixels with indices < 1 or > no are dummy "border" pixels */
521 int i, psfix, n_c, color, enter_run=0, break_run, was_in_run = 0,
523 pixel Ra, Rb, Rc, Rd;
524 pixel c_aa[MAX_COMPONENTS],
525 c_bb[MAX_COMPONENTS],
526 c_cc[MAX_COMPONENTS],
527 c_dd[MAX_COMPONENTS],
528 c_xx[MAX_COMPONENTS];
530 int cont,c_cont[MAX_COMPONENTS];
535 /**********************************************/
536 /* Do for all pixels in the row in 8-bit mode */
537 /**********************************************/
541 for (n_c=0; n_c<components; n_c++) {
542 c_cc[n_c] = psl[n_c];
543 c_bb[n_c] = psl[components+n_c];
553 if (!was_in_run) color = (color+1)%components;
557 for (n_c=0;n_c<components;n_c++) {
559 c_dd[n_c] = psl[i + components + n_c];
561 /* Quantize the gradient */
562 c_cont[n_c] = vLUT[0][c_dd[n_c] - c_bb[n_c] + LUTMAX8] +
563 vLUT[1][c_bb[n_c] - c_cc[n_c] + LUTMAX8] +
564 vLUT[2][c_cc[n_c] - c_aa[n_c] + LUTMAX8];
573 enter_run = was_in_run = test_run = 0;
577 for (n_c=0;n_c<components;n_c++)
578 if (c_cont[n_c]!=0) {
586 /********* RUN STATE *********/
590 enter_run = was_in_run = 1;
592 /* get length of the run */
593 /* arg is # of pixels left */
594 m = n = process_run_dec((no+components-1-i+1)/components, 0);
596 if ( m > 0 ) { /* run of nonzero length, otherwise
597 we go directly to the end-of-run
600 for (n_c=0;n_c<components;n_c++) {
605 if (i > no+components-1)
609 /* update context pixels */
610 for (n_c=0;n_c<components;n_c++) {
611 c_bb[n_c] = psl[i+n_c];
612 c_dd[n_c] = psl[i+components+n_c];
616 /* here we handle the "end-of-run" state */
617 for (n_c=0;n_c<components;n_c++) {
618 /* The end of run is processed for each component */
622 c_aa[n_c] = c_xx[n_c] = lossy_end_of_run_d(Ra, Rb, 0);
624 } /* Components loop */
628 /****** REGULAR CONTEXT *******/
632 cont = classmap[cont];
642 /* decode a Rice code of a given context */
643 c_aa[color] = Ra = lossy_regular_mode_d(cont, SIGN, Px);
654 for (n_c=0;n_c<components;n_c++) {
655 sl[i+n_c] = c_aa[n_c];
656 c_cc[n_c] = c_bb[n_c];
657 c_bb[n_c] = c_dd[n_c];
662 } while (i <= (no+components-1));
666 /***********************************************/
667 /* Do for all pixels in the row in 16-bit mode */
668 /***********************************************/
671 for (n_c=0; n_c<components; n_c++) {
672 c_cc[n_c] = ENDIAN16(psl[n_c]);
673 c_bb[n_c] = ENDIAN16(psl[components+n_c]);
674 c_aa[n_c] = ENDIAN16(sl[n_c]);
683 if (!was_in_run) color = (color+1)%components;
687 for (n_c=0;n_c<components;n_c++) {
689 c_dd[n_c] = ENDIAN16(psl[i + components + n_c]);
691 /* Quantize the gradient */
695 /* Following segment assumes that T3 <= LUTMAX16 */
696 /* This condition should have been checked when the
697 lookup tables were built */
698 diff = c_dd[n_c] - c_bb[n_c];
700 c_cont[n_c] = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
702 c_cont[n_c] = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
704 diff = c_bb[n_c] - c_cc[n_c];
706 c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
708 c_cont[n_c] += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
710 diff = c_cc[n_c] - c_aa[n_c];
712 c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
714 c_cont[n_c] += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
724 enter_run = was_in_run = test_run = 0;
728 for (n_c=0;n_c<components;n_c++)
729 if (c_cont[n_c]!=0) {
737 /********* RUN STATE *********/
741 enter_run = was_in_run = 1;
743 /* get length of the run */
744 /* arg is # of pixels left */
745 m = n = process_run_dec((no+components-1-i+1)/components, 0);
747 if ( m > 0 ) { /* run of nonzero length, otherwise
748 we go directly to the end-of-run
751 for (n_c=0;n_c<components;n_c++) {
752 sl[i++] = ENDIAN16(c_aa[n_c]);
756 if (i > no+components-1)
760 /* update context pixels */
761 for (n_c=0;n_c<components;n_c++) {
762 c_bb[n_c] = ENDIAN16(psl[i+n_c]);
763 c_dd[n_c] = ENDIAN16(psl[i+components+n_c]);
767 /* here we handle the "end-of-run" state */
768 for (n_c=0;n_c<components;n_c++) {
769 /* The end of run is processed for each component */
772 c_aa[n_c] = c_xx[n_c] = lossy_end_of_run_d(Ra, Rb, 0);
773 } /* Components loop */
778 /******* REGULAR CONTEXT *******/
782 cont = classmap[cont];
791 /* decode a Rice code of a given context */
792 c_aa[color] = Ra = lossy_regular_mode_d(cont, SIGN, Px);
796 sl[i] = ENDIAN16(Ra);
802 for (n_c=0;n_c<components;n_c++) {
803 sl[i+n_c] = ENDIAN16(c_aa[n_c]);
804 c_cc[n_c] = c_bb[n_c];
805 c_bb[n_c] = c_dd[n_c];
810 } while (i <= (no+components-1));
812 } /* for "if 8/16 bit" mode */