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