]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/lossy_d.c
7046e3ee7479795ad98d3e69305ceed98bad677e
[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 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 xpr,
199     Ix,
200     Errval,
201     absErrval,
202     MErrval,
203     k,
204     Q,
205     oldmap, 
206     Nt,
207     At;
208
209   Q = EOR_0 + RItype;
210   Nt = N[Q], 
211   At = A[Q];
212
213   if ( RItype )
214     At += Nt/2;
215
216   /* Estimate k */
217   for(k=0; Nt < At; Nt *=2, k++);
218
219   /* read and decode the Golomb code */
220   /* Get the number of leading zeros */
221   MErrval = 0;
222   do {
223     int temp;
224
225     temp = zeroLUT[reg >> 24];
226     MErrval += temp;
227     if (temp != 8) {
228       FILLBUFFER(temp + 1);
229       break;
230     }
231     FILLBUFFER(8);
232   } while (1);
233
234   eor_limit = limit - limit_reduce;
235
236   if ( MErrval < eor_limit ) {
237     /* now add the binary part of the Golomb code */
238     if (k) {
239       register unsigned long temp;
240       MErrval <<= k;
241       GETBITS(temp,k);
242       MErrval += temp;
243       }
244   }
245   else {
246       /* the original unary would have been too long:
247          (mapped value)-1 was sent verbatim */
248       GETBITS(MErrval, qbpp);
249       MErrval ++;
250   }
251
252   oldmap = ( k==0 && (RItype||MErrval) && (2*B[Q]<Nt));
253   /* 
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;
258   */
259
260   MErrval += ( RItype + oldmap );
261
262   if ( MErrval & 1 ) { /* negative */
263       Errval = oldmap - (MErrval+1)/2;
264       absErrval = -Errval-RItype;
265       B[Q]++;
266   }
267   else { /* nonnegative */
268       Errval = MErrval/2;
269       absErrval = Errval-RItype;
270   }
271
272   Errval = qmul[Errval];   /* de-quantize prediction error */
273   if ( RItype ) {
274       Ix = Ra + Errval;
275   }
276   else  {
277       if ( Rb < Ra )
278       Ix = Rb - Errval;
279       else
280       Ix = Rb + Errval;
281   }
282
283   if ( Ix < negNEAR )
284     Ix += beta;
285   else if ( Ix > alpha1eps )
286     Ix -= beta;
287
288   clip(Ix,alpha);
289
290   /* update stats */
291   A[Q] += absErrval;
292   if (N[Q] == reset) {
293       N[Q] >>= 1;
294       A[Q] >>= 1;
295       B[Q] >>= 1;
296   }
297
298   N[Q]++;  /* for next pixel */
299
300   return Ix;
301       
302 }
303
304
305
306
307
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  */
314 {
315   int i, psfix;
316   pixel Ra, Rb, Rc, Rd;
317   int SIGN;
318   int cont;
319   int run_int_type;
320
321   psfix = 0;
322
323   /**********************************************/
324   /* Do for all pixels in the row in 8-bit mode */
325   /**********************************************/
326   if (bpp16==FALSE)
327   {
328     Rc = psl[0];
329     Rb = psl[1];
330     Ra = sl[0];
331
332     i = 1;
333
334     do {
335       pixel Px;
336       Rd = psl[i + 1];
337
338       /* Quantize the gradient */
339       cont =  vLUT[0][Rd - Rb + LUTMAX8] +
340           vLUT[1][Rb - Rc + LUTMAX8] +
341           vLUT[2][Rc - Ra + LUTMAX8];
342
343       if ( cont == 0 ) {
344
345         /********** RUN STATE *********/
346
347         register int n, m;
348
349         /* get length of the run */
350         /* arg is # of pixels left */
351         m = n= process_run_dec(no-i+1, color); 
352
353         if ( m > 0 )  {  /* run of nonzero length, otherwise
354                   we go directly to the end-of-run 
355                   state */
356           do {
357             sl[i++] = Ra;
358           } while(--n > 0);
359
360           if (i > no)
361             /* end of line */
362             return 0;
363
364           /* update context pixels */
365           Rb = psl[i];
366           Rd = psl[i + 1];
367         }
368
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);
372       }
373       else {
374
375         /****** REGULAR CONTEXT ******/
376
377         predict(Rb, Ra, Rc);
378
379         /* map symmetric contexts */
380         cont = classmap[cont];
381
382         if (cont < 0) 
383         {
384           SIGN = -1;
385           cont = -cont;
386         }
387         else
388           SIGN = +1;
389
390         /* decode a Rice code of a given context */
391         Ra = lossy_regular_mode_d(cont, SIGN, Px);
392       }
393
394       sl[i] = Ra;
395       Rc = Rb;
396       Rb = Rd;
397       ++i;
398
399     } while (i <= no);
400
401   } else
402   /***********************************************/
403   /* Do for all pixels in the row in 16-bit mode */
404   /***********************************************/
405   {
406
407     Rc = ENDIAN16(psl[0]);
408     Rb = ENDIAN16(psl[1]);
409     Ra = ENDIAN16(sl[0]);
410     i = 1;
411
412     do {
413       pixel Px;
414
415       Rd = ENDIAN16(psl[i + 1]);
416
417       /* Quantize the gradient */
418       {
419         register int diff;
420
421         /* Following segment assumes that T3 <= LUTMAX16 */
422         /* This condition should have been checked when the
423            lookup tables were built */
424         diff = Rd - Rb;
425         if (diff < 0)
426           cont = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
427         else 
428           cont = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
429
430         diff = Rb - Rc;
431         if (diff < 0)
432           cont += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
433         else 
434           cont += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
435
436         diff = Rc - Ra;
437         if (diff < 0)
438           cont += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
439         else 
440           cont += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
441       }
442
443       if ( cont == 0 ) {
444
445         /********* RUN STATE *********/
446
447         register int n, m;
448
449         /* get length of the run */
450         /* arg is # of pixels left */
451         m = n = process_run_dec(no-i+1, color); 
452
453           if ( m > 0 )  {  /* run of nonzero length, otherwise
454                   we go directly to the end-of-run 
455                   state */
456           do {
457             sl[i++] = ENDIAN16(Ra);
458           } while(--n > 0);
459
460           if (i > no)
461               /* end of line */
462             return 0;
463
464           /* update context pixels */
465             Rb = ENDIAN16(psl[i]);
466             Rd = ENDIAN16(psl[i + 1]);
467         }
468
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);
473
474       }
475       else {
476
477       /******REGULAR CONTEXT ******/
478
479         predict(Rb, Ra, Rc);
480
481         /* map symmetric contexts */
482         cont = classmap[cont];
483
484         if (cont < 0) 
485         {
486           SIGN = -1;
487           cont = -cont;
488         }
489         else
490           SIGN = +1;
491
492         /* decode a Rice code of a given context */
493           Ra = lossy_regular_mode_d(cont, SIGN, Px);
494       }
495
496       sl[i] = ENDIAN16(Ra);
497       Rc = Rb;
498       Rb = Rd;
499       ++i;
500
501     } while (i <= no);
502
503   } /* ends "if 8/16 bit" */
504
505   return 0;
506 }
507
508
509
510
511
512
513
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  */
520 {
521   int i, psfix, n_c, color, enter_run=0, break_run, was_in_run = 0,
522       test_run;
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];
529   int  SIGN;
530   int cont,c_cont[MAX_COMPONENTS];
531
532   psfix = 0;
533
534
535   /**********************************************/
536   /* Do for all pixels in the row in 8-bit mode */
537   /**********************************************/
538   if (bpp16==FALSE)
539   {
540
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];
544       c_aa[n_c] = sl[n_c];
545     }
546
547     i = components;
548     color = -1;
549
550     do {
551       pixel Px;
552
553       if (!was_in_run) color = (color+1)%components;
554       else color = 0;
555
556       if (color == 0)
557       for (n_c=0;n_c<components;n_c++) {
558
559         c_dd[n_c] = psl[i + components + n_c];
560
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];
565       }
566
567       Ra=c_aa[color];
568       Rb=c_bb[color];
569       Rc=c_cc[color];
570       Rd=c_dd[color];
571       cont=c_cont[color];
572
573       enter_run = was_in_run = test_run = 0;
574     
575       if (color == 0) {
576         test_run = 1;
577         for (n_c=0;n_c<components;n_c++)
578           if (c_cont[n_c]!=0) {
579             test_run=0;
580             break;
581           }
582       }
583
584       if ( test_run ) {
585
586         /********* RUN STATE *********/
587
588         register int n, m;
589
590         enter_run = was_in_run = 1;
591
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); 
595
596         if ( m > 0 )  {  /* run of nonzero length, otherwise
597                   we go directly to the end-of-run 
598                   state */
599           do {
600             for (n_c=0;n_c<components;n_c++) {
601               sl[i++] = c_aa[n_c];
602             }
603           } while(--n > 0);
604
605           if (i > no+components-1)
606             /* end of line */
607             return 0;
608
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];
613           }
614         }
615
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 */
619           Ra = c_aa[n_c];
620           Rb = c_bb[n_c];
621
622           c_aa[n_c] = c_xx[n_c] = lossy_end_of_run_d(Ra, Rb, 0);
623
624         }       /* Components loop */
625       } 
626       else {
627
628       /****** REGULAR CONTEXT *******/
629
630         predict(Rb, Ra, Rc);
631
632         cont = classmap[cont];
633
634         if (cont < 0) 
635         {
636           SIGN = -1;
637           cont = -cont;
638         }
639         else
640           SIGN = +1;
641
642         /* decode a Rice code of a given context */
643           c_aa[color] = Ra = lossy_regular_mode_d(cont, SIGN, Px);
644
645       }
646
647       if (!was_in_run) {
648         sl[i] = Ra;
649         c_cc[color] = Rb;
650         c_bb[color] = Rd;
651         i++;
652       }
653       else {
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];
658         }
659         i+=components;
660       }
661   
662     } while (i <= (no+components-1));
663
664   } else
665
666   /***********************************************/
667   /* Do for all pixels in the row in 16-bit mode */
668   /***********************************************/
669   {
670
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]);
675     }
676
677     i = components;
678     color = -1;
679
680     do {
681       pixel Px;
682
683       if (!was_in_run) color = (color+1)%components;
684       else color = 0;
685
686       if (color == 0)
687         for (n_c=0;n_c<components;n_c++) {
688
689           c_dd[n_c] = ENDIAN16(psl[i + components + n_c]);
690
691           /* Quantize the gradient */
692           {
693             register int diff;
694
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];
699             if (diff < 0)
700               c_cont[n_c] = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
701             else 
702               c_cont[n_c] = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
703
704             diff = c_bb[n_c] - c_cc[n_c];
705             if (diff < 0)
706               c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
707             else 
708               c_cont[n_c] += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
709
710             diff = c_cc[n_c] - c_aa[n_c];
711             if (diff < 0)
712               c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
713             else 
714               c_cont[n_c] += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
715           }
716         }
717
718       Ra=c_aa[color];
719       Rb=c_bb[color];
720       Rc=c_cc[color];
721       Rd=c_dd[color];
722       cont=c_cont[color];
723
724       enter_run = was_in_run = test_run = 0;
725     
726       if (color == 0) {
727         test_run = 1;
728         for (n_c=0;n_c<components;n_c++)
729           if (c_cont[n_c]!=0) {
730             test_run=0;
731             break;
732           }
733       }
734
735       if ( test_run ) {
736
737         /********* RUN STATE *********/
738
739         register int n, m;
740
741         enter_run = was_in_run = 1;
742
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); 
746
747         if ( m > 0 )  {  /* run of nonzero length, otherwise
748                   we go directly to the end-of-run 
749                   state */
750           do {
751             for (n_c=0;n_c<components;n_c++) {
752               sl[i++] = ENDIAN16(c_aa[n_c]);
753             }
754           } while(--n > 0);
755
756           if (i > no+components-1)
757             /* end of line */
758             return 0;
759
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]);
764           }
765         }
766
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 */
770           Ra = c_aa[n_c];
771           Rb = c_bb[n_c];
772           c_aa[n_c] = c_xx[n_c] = lossy_end_of_run_d(Ra, Rb, 0);
773         }       /* Components loop */
774
775       }
776       else {
777
778       /******* REGULAR CONTEXT *******/
779
780           predict(Rb, Ra, Rc);
781         
782         cont = classmap[cont];
783         if (cont < 0) 
784         {
785           SIGN = -1;
786           cont = -cont;
787         }
788         else
789           SIGN = +1;
790
791         /* decode a Rice code of a given context */
792           c_aa[color] = Ra = lossy_regular_mode_d(cont, SIGN, Px);
793       }
794
795       if (!was_in_run) {
796         sl[i] = ENDIAN16(Ra);
797         c_cc[color] = Rb;
798         c_bb[color] = Rd;
799         i++;
800       }
801       else {
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];
806         }
807         i+=components;
808       }
809
810     } while (i <= (no+components-1));
811
812   } /* for "if 8/16 bit" mode */
813
814   return 0;
815 }