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_e.c --- the main pipeline which processes a scanline by doing
43 * prediction, context computation, context quantization,
44 * and statistics gathering. (for LOSSY compression)
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 /*byte getk[65][3000];
68 /* Do Golomb statistics and ENCODING for LOSSY images */
69 inline void lossy_regular_mode(int Q, int SIGN, int Px, pixel *xp)
71 int At, Bt, Nt, absErrval, Errval, MErrval,
72 qErrval, iqErrval, Rx,
73 Ix = *xp; /* current pixel */
81 /* Estimate k - Golomb coding variable computation (A.5.1) */
84 for(k=0; nst < At; nst<<=1, k++);
88 /* Prediction correction (A.4.2), compute prediction error (A.4.3)
89 , and error quantization (A.4.4) */
91 Px = Px + (SIGN) * C[Q];
93 Errval = SIGN * (Ix - Px);
94 qErrval = qdiv[Errval];
95 iqErrval = qmul[qErrval];
96 Rx = Px + SIGN * iqErrval;
99 *xp = Rx; /* store reconstructed pixel in scan line */
102 /* Modulo reduction of predication error (A.4.5) */
104 qErrval += qbeta; /* qErrval is now in [0..qbeta-1] */
107 /* Do Rice mapping and compute magnitude of diff */
111 /* Error Mapping (A.5.2) */
112 temp = ( k==0 && NEAR==0 && ((Bt<<1) <= -Nt) );
113 if (qErrval >= ceil_half_qbeta) {
115 absErrval = -qErrval;
116 MErrval = 2*absErrval - 1 - temp;
119 MErrval = 2*qErrval + temp;
123 /* update bias stats (after correction of the difference) (A.6.1) */
125 Errval = qmul[qErrval]; /* convert back to alphabet space */
127 B[Q] = (Bt += Errval);
129 /* update Golomb stats */
132 /* check for reset */
134 /* reset for Golomb and bias cancelation at the same time */
141 /* Do bias estimation for NEXT pixel */
142 /* Bias cancelation tries to put error in (-1,0] (A.6.2)*/
154 } else if ( Bt > 0 ) {
167 /* Actually output the code: Mapped Error Encoding (A.5.3) */
168 unary = MErrval >> k;
169 if ( unary < limit ) {
171 putbits((1 << k) + (MErrval & ((1 << k) - 1)), k + 1);
175 putbits((1<<qbpp) + MErrval - 1, qbpp+1);
183 /* Do end of run encoding for LOSSY images - returns reconstructed value of Ix */
184 pixel lossy_end_of_run(pixel Ra, pixel Rb, pixel Ix, int RItype)
186 int qErrval, iqErrval, xpr,
195 int Rx; /* reconstructed pixel */
203 Errval = Ix - (xpr = Ra);
207 Errval = Ix - (xpr = Rb);
212 qErrval = qdiv[Errval];
213 iqErrval = qmul[qErrval];
215 if ( RItype || (Rb >= Ra) )
220 clip(Rx,alpha); /* reconstructed pixel */
224 for(k=0; Nt < At; Nt *=2, k++);
229 if( qErrval >= ceil_half_qbeta )
232 oldmap = ( k==0 && qErrval && 2*B[Q]<Nt );
235 Note: the Boolean variable 'oldmap' is not
236 identical to the variable 'map' in the
237 JPEG-LS draft. We have
238 oldmap = (qErrval<0) ? (1-map) : map;
241 /* Error mapping for run-interrupted sample (Figure A.22) */
243 MErrval = -2*qErrval-1-RItype+oldmap;
247 MErrval = 2*qErrval-RItype-oldmap;
249 absErrval = (MErrval+1-RItype)/2;
259 N[Q]++; /* for next pixel */
261 /* Do the actual Golomb encoding: */
262 eor_limit = limit - limit_reduce;
263 unary = MErrval >> k;
264 if ( unary < eor_limit ) {
266 putbits((1 << k) + (MErrval & ((1 << k) - 1)), k + 1);
269 put_zeros(eor_limit);
270 putbits((1<<qbpp) + MErrval-1, qbpp+1);
281 /* For line and plane interleaved mode in LOSSY mode */
283 void lossy_doscanline(pixel *psl, /* previous scanline */
284 pixel *sl, /* current scanline */
285 int no, int color) /* number of values in it */
287 /*** watch it! actual pixels in the scan line are numbered 1 to no .
288 pixels with indices < 1 or > no are dummy "border" pixels */
291 pixel Ra, Rb, Rc, Rd, /* context pixels */
292 Ix, /* current pixel */
293 Px; /* predicted current pixel */
294 int Rx; /* reconstructed current pixel */
296 int SIGN; /* sign of current context */
297 int cont; /* context */
301 i = 1; /* pixel indices in a scan line go from 1 to no */
303 /**********************************************/
304 /* Do for all pixels in the row in 8-bit mode */
305 /**********************************************/
313 /* For 8-bit Image */
321 /* Context determination */
323 /* Quantize the gradient */
324 /* partial context number: if (b-e) is used then its
325 contribution is added after determination of the run state.
326 Also, sign flipping, if any, occurs after run
327 state determination */
329 cont = vLUT[0][Rd - Rb + LUTMAX8] +
330 vLUT[1][Rb - Rc + LUTMAX8] +
331 vLUT[2][Rc - Ra + LUTMAX8];
334 if ( cont == 0 ) { /* Run state? */
335 /*************** RUN STATE ***************************/
337 register delta = Ix - Ra;
340 if ( delta <= NEAR && delta >= negNEAR )
349 /* Run-lenght coding when reach end of line (A.7.1.2) */
350 process_run(RUNcnt, EOLINE, color);
351 return; /* end of line */
357 if ( delta > NEAR || delta < negNEAR ) /* Run is broken */
361 break; /* out of while loop */
367 /* we only get here if the run is broken by
368 a non-matching symbol */
370 /* Run-lenght coding when end of line not reached (A.7.1.2) */
371 process_run(RUNcnt,NOEOLINE, color);
374 /* This is the END_OF_RUN state */
375 RItype = ((Rb-Ra)<=NEAR && (Rb-Ra)>=negNEAR);
376 Ix = lossy_end_of_run(Ra, Rb, Ix, RItype);
381 /*************** REGULAR CONTEXT *******************/
385 /* do symmetric context merging */
386 cont = classmap[cont];
394 /* output a rice code */
395 lossy_regular_mode(cont, SIGN, Px, &Ix);
398 /* context for next pixel: */
405 } else { /* 16 bit mode instead of 8 */
407 /***********************************************/
408 /* Do for all pixels in the row in 16-bit mode */
409 /***********************************************/
411 Rc = ENDIAN16(psl[0]);
412 Rb = ENDIAN16(psl[1]);
413 Ra = ENDIAN16(sl[0]);
415 /* For 16-bit Image */
420 Ix = ENDIAN16(sl[i]);
421 Rd = ENDIAN16(psl[i + 1]);
423 /* Context determination */
425 /* Quantize the gradient */
426 /* partial context number: if (b-e) is used then its
427 contribution is added after determination of the run state.
428 Also, sign flipping, if any, occurs after run
429 state determination */
434 /* Following segment assumes that Sc <= LUTMAX16 */
435 /* This condition should have been checked when the
436 lookup tables were built */
439 cont = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
441 cont = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
445 cont += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
447 cont += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
451 cont += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
453 cont += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
456 if ( cont == 0 ) { /* Run state? */
457 /*************** RUN STATE ***************************/
459 register delta = Ix - Ra;
462 if ( delta <= NEAR && delta >= negNEAR )
468 sl[i] = ENDIAN16(Ra);
471 /* Run-lenght coding when reach end of line (A.7.1.2) */
472 process_run(RUNcnt, EOLINE, color);
473 return; /* end of line */
476 Ix = ENDIAN16(sl[i]);
479 if ( delta > NEAR || delta < negNEAR ) /* Run is broken */
481 Rd = ENDIAN16(psl[i + 1]);
482 Rb = ENDIAN16(psl[i]);
483 break; /* out of while loop */
489 /* we only get here if the run is broken by
490 a non-matching symbol */
492 /* Run-lenght coding when end of line not reached (A.7.1.2) */
493 process_run(RUNcnt,NOEOLINE, color);
495 /* This is the END_OF_RUN state */
496 RItype = ((Rb-Ra)<=NEAR && (Rb-Ra)>=negNEAR);
497 Ix = lossy_end_of_run(Ra, Rb, Ix, RItype);
502 /*************** REGULAR CONTEXT *******************/
506 /* do symmetric context merging */
507 cont = classmap[cont];
515 /* output a rice code */
516 lossy_regular_mode(cont, SIGN, Px, &Ix);
519 /* context for next pixel: */
520 sl[i] = ENDIAN16(Ix);
526 } /* for "if" 16 or 8 bit mode */
535 /* For pixel interleavde mode for LOSSY encoding */
537 void lossy_doscanline_pixel(pixel *psl, /* previous scanline */
538 pixel *sl, /* current scanline */
539 int no) /* number of values in it */
540 /*** watch it! actual pixels in the scan line are numbered 1 to no .
541 pixels with indices < 1 or > no are dummy "border" pixels */
543 int i,n_c, enter_run=0, break_run, was_in_run=0, test_run;
544 int color; /* Index to the component, 0..COMPONENTS-1 */
545 pixel c_aa[MAX_COMPONENTS],
546 c_bb[MAX_COMPONENTS],
547 c_cc[MAX_COMPONENTS],
548 c_dd[MAX_COMPONENTS],
549 c_xx[MAX_COMPONENTS],
550 Ra, Rb, Rc, Rd, /* context pixels */
551 Ix, /* current pixel */
552 Px; /* predicted current pixel */
553 int SIGN; /* sign of current context */
554 int cont,c_cont[MAX_COMPONENTS]; /* context */
559 /**********************************************/
560 /* Do for all pixels in the row in 8-bit mode */
561 /**********************************************/
563 for (n_c=0; n_c<components; n_c++) {
564 c_cc[n_c] = psl[n_c];
565 c_bb[n_c] = psl[components+n_c];
569 i = components; /* pixel indices in a scan line go from COMPONENTS to no */
575 if (!was_in_run) color = (color+1)%components;
579 for (n_c=0;n_c<components;n_c++)
580 c_xx[n_c] = sl[i+n_c];
583 for (n_c=0;n_c<components;n_c++) {
585 c_dd[n_c] = psl[i+components+n_c];
587 /* Context determination */
589 /* Quantize the gradient */
590 /* partial context number: if (b-e) is used
591 then its contribution is added after
592 determination of the run state.
593 Also, sign flipping, if any, occurs after run
594 state determination */
596 c_cont[n_c] = vLUT[0][c_dd[n_c] - c_bb[n_c] + LUTMAX8] +
597 vLUT[1][c_bb[n_c] - c_cc[n_c] + LUTMAX8] +
598 vLUT[2][c_cc[n_c] - c_aa[n_c] + LUTMAX8];
607 enter_run = was_in_run = test_run = 0;
611 for (n_c=0;n_c<components;n_c++)
612 if (c_cont[n_c]!=0) {
619 /*************** RUN STATE ***************************/
621 int delta[MAX_COMPONENTS];
623 enter_run = was_in_run = 1;
624 for (n_c=0;n_c<components;n_c++) {
625 delta[n_c] = sl[i+n_c] - c_aa[n_c];
626 if (delta[n_c]>NEAR || delta[n_c]<negNEAR) enter_run=0;
635 for (n_c=0; n_c<components; n_c++)
636 sl[i+n_c] = c_aa[n_c];
638 if((i=i+components)>(no+components-1)){
639 process_run(RUNcnt, EOLINE, 0);
640 return; /* end of line */
643 for (n_c=0;n_c<components;n_c++)
644 c_xx[n_c] = sl[i+n_c];
647 for (n_c=0;n_c<components;n_c++) {
648 delta[n_c] = c_xx[n_c] - c_aa[n_c];
649 if(delta[n_c]>NEAR || delta[n_c]<negNEAR) break_run=1;
654 for(n_c=0; n_c<components; n_c++){
655 c_dd[n_c] = psl[i+components+n_c];
656 c_bb[n_c] = psl[i+n_c];
658 break; /* out of while loop */
664 /* we only get here if the run is broken by
665 a non-matching symbol */
667 process_run(RUNcnt, NOEOLINE, 0);
669 /* This is the END_OF_RUN state */
671 for (n_c=0;n_c<components;n_c++) {
672 /* The end of run is done for each component */
677 /* Handle two special EOR states */
678 c_xx[n_c] = Ix = lossy_end_of_run(Ra, Rb, Ix, 0);
680 } /* loop for components */
682 } /* Run state block */
685 /*************** REGULAR CONTEXT *******************/
688 cont = classmap[cont];
697 /* output a rice code */
698 lossy_regular_mode(cont, SIGN, Px, &Ix);
701 /* context for next pixel: */
705 sl[i] = Ix; /* store reconstructed x */
712 for(n_c=0;n_c<components;n_c++) {
713 c_aa[n_c] = c_xx[n_c];
714 sl[i+n_c] = c_xx[n_c];
716 c_cc[n_c] = c_bb[n_c];
717 c_bb[n_c] = c_dd[n_c];
722 } while (i <= (no+components-1));
728 /***********************************************/
729 /* Do for all pixels in the row in 16-bit mode */
730 /***********************************************/
732 for (n_c=0; n_c<components; n_c++) {
733 c_cc[n_c] = ENDIAN16(psl[n_c]);
734 c_bb[n_c] = ENDIAN16(psl[components+n_c]);
735 c_aa[n_c] = ENDIAN16(sl[n_c]);
738 i = components; /* pixel indices in a scan line go from COMPONENTS to no */
744 if (!was_in_run) color = (color+1)%components;
746 Ix = ENDIAN16(sl[i]);
748 for (n_c=0;n_c<components;n_c++)
749 c_xx[n_c] = ENDIAN16(sl[i+n_c]);
752 for (n_c=0;n_c<components;n_c++) {
754 c_dd[n_c] = ENDIAN16(psl[i+components+n_c]);
756 /* Context determination */
758 /* Quantize the gradient */
759 /* partial context number: if (b-e) is used
760 then its contribution is added after
761 determination of the run state.
762 Also, sign flipping, if any, occurs after run
763 state determination */
767 /* Following segment assumes that Sc <= LUTMAX16 */
768 /* This condition should have been checked when the
769 l ookup tables were built */
770 diff = c_dd[n_c] - c_bb[n_c];
772 c_cont[n_c] = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
774 c_cont[n_c] = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
775 diff = c_bb[n_c] - c_cc[n_c];
777 c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
779 c_cont[n_c] += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
780 diff = c_cc[n_c] - c_aa[n_c];
782 c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
784 c_cont[n_c] += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
795 enter_run = was_in_run = test_run = 0;
799 for (n_c=0;n_c<components;n_c++)
800 if (c_cont[n_c]!=0) {
807 /*************** RUN STATE ***************************/
809 int delta[MAX_COMPONENTS];
811 enter_run = was_in_run = 1;
812 for (n_c=0;n_c<components;n_c++) {
813 delta[n_c] = ENDIAN16(sl[i+n_c]) - c_aa[n_c];
814 if (delta[n_c]>NEAR || delta[n_c]<negNEAR) enter_run=0;
823 for (n_c=0; n_c<components; n_c++)
824 sl[i+n_c] = ENDIAN16(c_aa[n_c]);
826 if((i=i+components)>(no+components-1)){
827 process_run(RUNcnt, EOLINE, 0);
828 return; /* end of line */
831 for (n_c=0;n_c<components;n_c++)
832 c_xx[n_c] = ENDIAN16(sl[i+n_c]);
835 for (n_c=0;n_c<components;n_c++) {
836 delta[n_c] = c_xx[n_c] - c_aa[n_c];
837 if(delta[n_c]>NEAR || delta[n_c]<negNEAR) break_run=1;
842 for(n_c=0; n_c<components; n_c++){
843 c_dd[n_c] = ENDIAN16(psl[i+components+n_c]);
844 c_bb[n_c] = ENDIAN16(psl[i+n_c]);
846 break; /* out of while loop */
852 /* we only get here if the run is broken by
853 a non-matching symbol */
855 process_run(RUNcnt, NOEOLINE, 0);
857 /* This is the END_OF_RUN state */
859 for (n_c=0;n_c<components;n_c++) {
860 /* The end of run is done for each component */
865 /* Handle two special EOR states */
866 c_xx[n_c] = Ix = lossy_end_of_run(Ra, Rb, Ix, 0);
868 } /* loop for components */
870 } /* Run state block */
873 /*************** REGULAR CONTEXT *******************/
876 cont = classmap[cont];
885 /* output a rice code */
886 lossy_regular_mode(cont, SIGN, Px, &Ix);
889 /* context for next pixel: */
893 sl[i] = ENDIAN16(Ix); /* store reconstructed x */
900 for(n_c=0;n_c<components;n_c++) {
901 c_aa[n_c] = c_xx[n_c];
902 sl[i+n_c] = ENDIAN16(c_xx[n_c]);
904 c_cc[n_c] = c_bb[n_c];
905 c_bb[n_c] = c_dd[n_c];
910 } while (i <= (no+components-1));
912 } /* ends 'if' for 8 or 16 bit */