]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/lossy_d.c
Add Gianni Lazzaretto in contributors list
[gdcm.git] / src / gdcmjpegls / Decoder / lossy_d.c
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.
9
10    =========================================================
11    THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:
12    =========================================================
13
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    -------------------------------------------------------------------------------
40 */
41
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)
45  *
46  * Initial code by Alex Jakulin,  Aug. 1995
47  *
48  * Modified and optimized: Gadiel Seroussi, October 1995
49  *
50  * Modified and added Restart marker and input tables by:
51  * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
52  */
53
54 #include "global.h"
55 #include "bitio.h"
56
57 #include <stdio.h>
58 #include <math.h>
59
60 static int eor_limit;
61
62 /* Do Golomb-Rice statistics and DECODING for LOSSY images*/
63 inline int lossy_regular_mode_d(int Q, int SIGN, int Px)
64 {
65   int At, Bt, Nt, Errval, absErrval;
66   int current, k;
67
68   /* This function is called only for regular contexts. 
69      End_of_run context is treated separately */
70
71   Nt = N[Q];
72   At = A[Q];
73   /* Estimate k */
74   {
75       register int nst = Nt;
76       for(k=0; nst < At; nst *=2, k++);
77   }
78   
79   /* Get the number of leading zeros */
80   absErrval = 0;
81   do {
82     int temp;
83
84     temp = zeroLUT[reg >> 24];
85     absErrval += temp;
86     if (temp != 8) {
87       FILLBUFFER(temp + 1);
88       break;
89     }
90     FILLBUFFER(8);
91   } while (1);
92
93   if ( absErrval < limit ) {
94     /* now add the binary part of the Rice code */
95     if (k) {
96       register unsigned long temp;
97       absErrval <<= k;
98       GETBITS(temp,k);
99       absErrval += temp;
100     }
101   }
102   else {
103     /* the original unary would have been too long:
104       (mapped value)-1 was sent verbatim */
105     GETBITS(absErrval, qbpp);
106     absErrval ++;
107   }
108
109   /* Do the Rice mapping */
110   if ( absErrval & 1 ) {        /* negative */
111     absErrval = (absErrval + 1) / 2;
112     Errval = -absErrval;
113   } else {
114     absErrval /= 2;
115     Errval = absErrval;
116   }
117
118   Bt = B[Q];
119
120   /* if ( k==0 && (2*Bt <= -qmul[Nt]) ) */
121   if ( k==0 && NEAR==0 && (2*Bt <= -Nt) ) 
122   {
123   /* special case: see encoder side */
124     Errval = -(Errval+1);
125     absErrval = (Errval<0)?(-Errval):Errval;
126   }
127
128   Errval = qmul[Errval];  /* dequantize prediction error */
129
130   /* center, clip if necessary, and mask final error */
131   if ( SIGN == -1 ) {
132       Px -= C[Q];
133       clip(Px, alpha);
134       current = Px - Errval;
135   }
136   else {
137       Px += C[Q];
138       clip(Px,alpha);
139       current = Px + Errval;
140   }
141
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)
145     current += beta;
146   else if (current > alpha1eps)
147     current -= beta;
148
149   clip(current,alpha);
150
151   /* update bias stats */
152   B[Q] = (Bt += Errval);
153
154   /* update Golomb-Rice stats */
155   A[Q] += absErrval;
156
157   /* check reset (joint for Rice-Golomb and bias cancelation) */
158   if(Nt == reset) {
159     N[Q] = (Nt >>= 1);
160     A[Q] >>= 1;
161     B[Q] = (Bt >>= 1);
162   }
163
164   /* Do bias estimation for NEXT pixel */
165   N[Q] = (++Nt);
166   if  ( Bt <= -Nt ) {
167
168       if (C[Q] > MIN_C)
169       --C[Q];
170
171       Bt = (B[Q] += Nt);
172
173       if ( Bt <= -Nt ) 
174       B[Q] = -Nt+1;
175
176   } else if ( Bt > 0 ) {
177
178       if (C[Q] < MAX_C)
179       ++C[Q];
180
181       Bt = (B[Q] -= Nt);
182
183       if ( Bt > 0 )
184       B[Q] = 0;
185   }
186
187   return current;
188 }
189
190
191
192
193
194
195 /* Do end of run DECODING for LOSSY images */
196 inline pixel lossy_end_of_run_d(pixel Ra, pixel Rb, int RItype)
197 {
198   int Ix,
199     Errval,
200     absErrval,
201     MErrval,
202     k,
203     Q,
204     oldmap, 
205     Nt,
206     At;
207
208   Q = EOR_0 + RItype;
209   Nt = N[Q], 
210   At = A[Q];
211
212   if ( RItype )
213     At += Nt/2;
214
215   /* Estimate k */
216   for(k=0; Nt < At; Nt *=2, k++);
217
218   /* read and decode the Golomb code */
219   /* Get the number of leading zeros */
220   MErrval = 0;
221   do {
222     int temp;
223
224     temp = zeroLUT[reg >> 24];
225     MErrval += temp;
226     if (temp != 8) {
227       FILLBUFFER(temp + 1);
228       break;
229     }
230     FILLBUFFER(8);
231   } while (1);
232
233   eor_limit = limit - limit_reduce;
234
235   if ( MErrval < eor_limit ) {
236     /* now add the binary part of the Golomb code */
237     if (k) {
238       register unsigned long temp;
239       MErrval <<= k;
240       GETBITS(temp,k);
241       MErrval += temp;
242       }
243   }
244   else {
245       /* the original unary would have been too long:
246          (mapped value)-1 was sent verbatim */
247       GETBITS(MErrval, qbpp);
248       MErrval ++;
249   }
250
251   oldmap = ( k==0 && (RItype||MErrval) && (2*B[Q]<Nt));
252   /* 
253      Note: the Boolean variable 'oldmap' is not 
254      identical to the variable 'map' in the
255      JPEG-LS draft. We have
256        oldmap = (qdiff<0) ? (1-map) : map;
257   */
258
259   MErrval += ( RItype + oldmap );
260
261   if ( MErrval & 1 ) { /* negative */
262       Errval = oldmap - (MErrval+1)/2;
263       absErrval = -Errval-RItype;
264       B[Q]++;
265   }
266   else { /* nonnegative */
267       Errval = MErrval/2;
268       absErrval = Errval-RItype;
269   }
270
271   Errval = qmul[Errval];   /* de-quantize prediction error */
272   if ( RItype ) {
273       Ix = Ra + Errval;
274   }
275   else  {
276       if ( Rb < Ra )
277       Ix = Rb - Errval;
278       else
279       Ix = Rb + Errval;
280   }
281
282   if ( Ix < negNEAR )
283     Ix += beta;
284   else if ( Ix > alpha1eps )
285     Ix -= beta;
286
287   clip(Ix,alpha);
288
289   /* update stats */
290   A[Q] += absErrval;
291   if (N[Q] == reset) {
292       N[Q] >>= 1;
293       A[Q] >>= 1;
294       B[Q] >>= 1;
295   }
296
297   N[Q]++;  /* for next pixel */
298
299   return Ix;
300       
301 }
302
303
304
305
306
307 /* For DECODING line and plane interleaved mode for LOSSY images*/
308 int lossy_undoscanline(  pixel *psl,      /* previous scanline */
309             pixel *sl,      /* current scanline */
310             int no, int color)  /* number of values in it */
311 /*** watch it! actual pixels in the scan line are numbered 1 to no .
312      pixels with indices < 1 or > no are dummy "border" pixels  */
313 {
314   int i;
315   pixel Ra, Rb, Rc, Rd;
316   int SIGN;
317   int cont;
318   int run_int_type;
319
320   /**********************************************/
321   /* Do for all pixels in the row in 8-bit mode */
322   /**********************************************/
323   if (bpp16==FALSE)
324   {
325     Rc = psl[0];
326     Rb = psl[1];
327     Ra = sl[0];
328
329     i = 1;
330
331     do {
332       pixel Px;
333       Rd = psl[i + 1];
334
335       /* Quantize the gradient */
336       cont =  vLUT[0][Rd - Rb + LUTMAX8] +
337           vLUT[1][Rb - Rc + LUTMAX8] +
338           vLUT[2][Rc - Ra + LUTMAX8];
339
340       if ( cont == 0 ) {
341
342         /********** RUN STATE *********/
343
344         register int n, m;
345
346         /* get length of the run */
347         /* arg is # of pixels left */
348         m = n= process_run_dec(no-i+1, color); 
349
350         if ( m > 0 )  {  /* run of nonzero length, otherwise
351                   we go directly to the end-of-run 
352                   state */
353           do {
354             sl[i++] = Ra;
355           } while(--n > 0);
356
357           if (i > no)
358             /* end of line */
359             return 0;
360
361           /* update context pixels */
362           Rb = psl[i];
363           Rd = psl[i + 1];
364         }
365
366         /* here we handle the "end-of-run" state */
367         run_int_type = ( (Rb-Ra) <= NEAR && (Rb-Ra) >= negNEAR);
368         Ra = lossy_end_of_run_d(Ra, Rb, run_int_type);
369       }
370       else {
371
372         /****** REGULAR CONTEXT ******/
373
374         predict(Rb, Ra, Rc);
375
376         /* map symmetric contexts */
377         cont = classmap[cont];
378
379         if (cont < 0) 
380         {
381           SIGN = -1;
382           cont = -cont;
383         }
384         else
385           SIGN = +1;
386
387         /* decode a Rice code of a given context */
388         Ra = lossy_regular_mode_d(cont, SIGN, Px);
389       }
390
391       sl[i] = Ra;
392       Rc = Rb;
393       Rb = Rd;
394       ++i;
395
396     } while (i <= no);
397
398   } else
399   /***********************************************/
400   /* Do for all pixels in the row in 16-bit mode */
401   /***********************************************/
402   {
403
404     Rc = ENDIAN16(psl[0]);
405     Rb = ENDIAN16(psl[1]);
406     Ra = ENDIAN16(sl[0]);
407     i = 1;
408
409     do {
410       pixel Px;
411
412       Rd = ENDIAN16(psl[i + 1]);
413
414       /* Quantize the gradient */
415       {
416         register int diff;
417
418         /* Following segment assumes that T3 <= LUTMAX16 */
419         /* This condition should have been checked when the
420            lookup tables were built */
421         diff = Rd - Rb;
422         if (diff < 0)
423           cont = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
424         else 
425           cont = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
426
427         diff = Rb - Rc;
428         if (diff < 0)
429           cont += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
430         else 
431           cont += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
432
433         diff = Rc - Ra;
434         if (diff < 0)
435           cont += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
436         else 
437           cont += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
438       }
439
440       if ( cont == 0 ) {
441
442         /********* RUN STATE *********/
443
444         register int n, m;
445
446         /* get length of the run */
447         /* arg is # of pixels left */
448         m = n = process_run_dec(no-i+1, color); 
449
450           if ( m > 0 )  {  /* run of nonzero length, otherwise
451                   we go directly to the end-of-run 
452                   state */
453           do {
454             sl[i++] = ENDIAN16(Ra);
455           } while(--n > 0);
456
457           if (i > no)
458               /* end of line */
459             return 0;
460
461           /* update context pixels */
462             Rb = ENDIAN16(psl[i]);
463             Rd = ENDIAN16(psl[i + 1]);
464         }
465
466         /* here we handle the "end-of-run" state, which is 
467           treated separately from regular states */
468         run_int_type = ( (Rb-Ra) <= NEAR && (Rb-Ra) >= negNEAR);
469         Ra = lossy_end_of_run_d(Ra, Rb, run_int_type);
470
471       }
472       else {
473
474       /******REGULAR CONTEXT ******/
475
476         predict(Rb, Ra, Rc);
477
478         /* map symmetric contexts */
479         cont = classmap[cont];
480
481         if (cont < 0) 
482         {
483           SIGN = -1;
484           cont = -cont;
485         }
486         else
487           SIGN = +1;
488
489         /* decode a Rice code of a given context */
490           Ra = lossy_regular_mode_d(cont, SIGN, Px);
491       }
492
493       sl[i] = ENDIAN16(Ra);
494       Rc = Rb;
495       Rb = Rd;
496       ++i;
497
498     } while (i <= no);
499
500   } /* ends "if 8/16 bit" */
501
502   return 0;
503 }
504
505
506
507
508
509
510
511 /* For DECODING pixel interleaved mode in LOSSY mode */
512 int lossy_undoscanline_pixel(  pixel *psl,    /* previous scanline */
513                 pixel *sl,    /* current scanline */
514                 int no)      /* number of values in it */
515 /*** watch it! actual pixels in the scan line are numbered 1 to no .
516      pixels with indices < 1 or > no are dummy "border" pixels  */
517 {
518   int i, n_c, color, was_in_run = 0,
519       test_run;
520   pixel Ra, Rb, Rc, Rd;
521   pixel c_aa[MAX_COMPONENTS],
522         c_bb[MAX_COMPONENTS],
523         c_cc[MAX_COMPONENTS],
524         c_dd[MAX_COMPONENTS],
525         c_xx[MAX_COMPONENTS];
526   int  SIGN;
527   int cont,c_cont[MAX_COMPONENTS];
528
529
530   /**********************************************/
531   /* Do for all pixels in the row in 8-bit mode */
532   /**********************************************/
533   if (bpp16==FALSE)
534   {
535
536     for (n_c=0; n_c<components; n_c++) {
537       c_cc[n_c] = psl[n_c];
538       c_bb[n_c] = psl[components+n_c];
539       c_aa[n_c] = sl[n_c];
540     }
541
542     i = components;
543     color = -1;
544
545     do {
546       pixel Px;
547
548       if (!was_in_run) color = (color+1)%components;
549       else color = 0;
550
551       if (color == 0)
552       for (n_c=0;n_c<components;n_c++) {
553
554         c_dd[n_c] = psl[i + components + n_c];
555
556         /* Quantize the gradient */
557         c_cont[n_c] =  vLUT[0][c_dd[n_c] - c_bb[n_c] + LUTMAX8] +
558                 vLUT[1][c_bb[n_c] - c_cc[n_c] + LUTMAX8] +
559                 vLUT[2][c_cc[n_c] - c_aa[n_c] + LUTMAX8];
560       }
561
562       Ra=c_aa[color];
563       Rb=c_bb[color];
564       Rc=c_cc[color];
565       Rd=c_dd[color];
566       cont=c_cont[color];
567
568       was_in_run = test_run = 0;
569     
570       if (color == 0) {
571         test_run = 1;
572         for (n_c=0;n_c<components;n_c++)
573           if (c_cont[n_c]!=0) {
574             test_run=0;
575             break;
576           }
577       }
578
579       if ( test_run ) {
580
581         /********* RUN STATE *********/
582
583         register int n, m;
584
585         was_in_run = 1;
586
587         /* get length of the run */
588         /* arg is # of pixels left */
589         m = n = process_run_dec((no+components-1-i+1)/components, 0); 
590
591         if ( m > 0 )  {  /* run of nonzero length, otherwise
592                   we go directly to the end-of-run 
593                   state */
594           do {
595             for (n_c=0;n_c<components;n_c++) {
596               sl[i++] = c_aa[n_c];
597             }
598           } while(--n > 0);
599
600           if (i > no+components-1)
601             /* end of line */
602             return 0;
603
604           /* update context pixels */
605           for (n_c=0;n_c<components;n_c++) {
606               c_bb[n_c] = psl[i+n_c];
607               c_dd[n_c] = psl[i+components+n_c];
608           }
609         }
610
611         /* here we handle the "end-of-run" state */
612         for (n_c=0;n_c<components;n_c++) {
613           /* The end of run is processed for each component */
614           Ra = c_aa[n_c];
615           Rb = c_bb[n_c];
616
617           c_aa[n_c] = c_xx[n_c] = lossy_end_of_run_d(Ra, Rb, 0);
618
619         }       /* Components loop */
620       } 
621       else {
622
623       /****** REGULAR CONTEXT *******/
624
625         predict(Rb, Ra, Rc);
626
627         cont = classmap[cont];
628
629         if (cont < 0) 
630         {
631           SIGN = -1;
632           cont = -cont;
633         }
634         else
635           SIGN = +1;
636
637         /* decode a Rice code of a given context */
638           c_aa[color] = Ra = lossy_regular_mode_d(cont, SIGN, Px);
639
640       }
641
642       if (!was_in_run) {
643         sl[i] = Ra;
644         c_cc[color] = Rb;
645         c_bb[color] = Rd;
646         i++;
647       }
648       else {
649         for (n_c=0;n_c<components;n_c++) {
650           sl[i+n_c] = c_aa[n_c];
651           c_cc[n_c] = c_bb[n_c];
652           c_bb[n_c] = c_dd[n_c];
653         }
654         i+=components;
655       }
656   
657     } while (i <= (no+components-1));
658
659   } else
660
661   /***********************************************/
662   /* Do for all pixels in the row in 16-bit mode */
663   /***********************************************/
664   {
665
666     for (n_c=0; n_c<components; n_c++) {
667       c_cc[n_c] = ENDIAN16(psl[n_c]);
668       c_bb[n_c] = ENDIAN16(psl[components+n_c]);
669       c_aa[n_c] = ENDIAN16(sl[n_c]);
670     }
671
672     i = components;
673     color = -1;
674
675     do {
676       pixel Px;
677
678       if (!was_in_run) color = (color+1)%components;
679       else color = 0;
680
681       if (color == 0)
682         for (n_c=0;n_c<components;n_c++) {
683
684           c_dd[n_c] = ENDIAN16(psl[i + components + n_c]);
685
686           /* Quantize the gradient */
687           {
688             register int diff;
689
690             /* Following segment assumes that T3 <= LUTMAX16 */
691             /* This condition should have been checked when the
692               lookup tables were built */
693             diff = c_dd[n_c] - c_bb[n_c];
694             if (diff < 0)
695               c_cont[n_c] = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
696             else 
697               c_cont[n_c] = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
698
699             diff = c_bb[n_c] - c_cc[n_c];
700             if (diff < 0)
701               c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
702             else 
703               c_cont[n_c] += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
704
705             diff = c_cc[n_c] - c_aa[n_c];
706             if (diff < 0)
707               c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
708             else 
709               c_cont[n_c] += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
710           }
711         }
712
713       Ra=c_aa[color];
714       Rb=c_bb[color];
715       Rc=c_cc[color];
716       Rd=c_dd[color];
717       cont=c_cont[color];
718
719       was_in_run = test_run = 0;
720     
721       if (color == 0) {
722         test_run = 1;
723         for (n_c=0;n_c<components;n_c++)
724           if (c_cont[n_c]!=0) {
725             test_run=0;
726             break;
727           }
728       }
729
730       if ( test_run ) {
731
732         /********* RUN STATE *********/
733
734         register int n, m;
735
736         was_in_run = 1;
737
738         /* get length of the run */
739         /* arg is # of pixels left */
740         m = n = process_run_dec((no+components-1-i+1)/components, 0); 
741
742         if ( m > 0 )  {  /* run of nonzero length, otherwise
743                   we go directly to the end-of-run 
744                   state */
745           do {
746             for (n_c=0;n_c<components;n_c++) {
747               sl[i++] = ENDIAN16(c_aa[n_c]);
748             }
749           } while(--n > 0);
750
751           if (i > no+components-1)
752             /* end of line */
753             return 0;
754
755           /* update context pixels */
756           for (n_c=0;n_c<components;n_c++) {
757               c_bb[n_c] = ENDIAN16(psl[i+n_c]);
758               c_dd[n_c] = ENDIAN16(psl[i+components+n_c]);
759           }
760         }
761
762         /* here we handle the "end-of-run" state */
763           for (n_c=0;n_c<components;n_c++) {
764             /* The end of run is processed for each component */
765           Ra = c_aa[n_c];
766           Rb = c_bb[n_c];
767           c_aa[n_c] = c_xx[n_c] = lossy_end_of_run_d(Ra, Rb, 0);
768         }       /* Components loop */
769
770       }
771       else {
772
773       /******* REGULAR CONTEXT *******/
774
775           predict(Rb, Ra, Rc);
776         
777         cont = classmap[cont];
778         if (cont < 0) 
779         {
780           SIGN = -1;
781           cont = -cont;
782         }
783         else
784           SIGN = +1;
785
786         /* decode a Rice code of a given context */
787           c_aa[color] = Ra = lossy_regular_mode_d(cont, SIGN, Px);
788       }
789
790       if (!was_in_run) {
791         sl[i] = ENDIAN16(Ra);
792         c_cc[color] = Rb;
793         c_bb[color] = Rd;
794         i++;
795       }
796       else {
797         for (n_c=0;n_c<components;n_c++) {
798           sl[i+n_c] = ENDIAN16(c_aa[n_c]);
799           c_cc[n_c] = c_bb[n_c];
800           c_bb[n_c] = c_dd[n_c];
801         }
802         i+=components;
803       }
804
805     } while (i <= (no+components-1));
806
807   } /* for "if 8/16 bit" mode */
808
809   return 0;
810 }