]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/lossless_d.c
COMP: Tons of warnings fix...cannot believe how poorly written this was
[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, psfix;
335   pixel Ra, Rb, Rc, Rd;
336   int SIGN;
337   int cont;
338
339   psfix = 0;
340
341
342   /**********************************************/
343   /* Do for all pixels in the row in 8-bit mode */
344   /**********************************************/
345   if (bpp16==FALSE)
346   {
347
348     Rc = psl[0];
349     Rb = psl[1];
350     Ra = sl[0];
351
352     i = 1;
353     do {
354       pixel Px;
355
356       Rd = psl[i + 1];
357
358       /* Quantize the gradient */
359       cont =  vLUT[0][Rd - Rb + LUTMAX8] +
360           vLUT[1][Rb - Rc + LUTMAX8] +
361           vLUT[2][Rc - Ra + LUTMAX8];
362
363       if ( cont == 0 )
364       {
365         /*********** RUN STATE **********/
366
367         register int n, m;
368
369         /* get length of the run */
370         /* arg is # of pixels left */
371         m = n = process_run_dec(no-i+1, color); 
372
373         if ( m > 0 )  {  /* run of nonzero length, otherwise
374                   we go directly to the end-of-run 
375                   state */
376           do {
377             sl[i++] = Ra;
378           } while(--n > 0);
379
380           if (i > no)
381             /* end of line */
382             return 0;
383
384           /* update context pixels */
385             Rb = psl[i];
386             Rd = psl[i + 1];
387         }
388
389         /* Do end of run encoding for LOSSLESS images */
390         Ra = lossless_end_of_run_d(Ra, Rb, (Ra==Rb));
391   
392       }       /* Run state block */ 
393       else 
394       {
395       /************ REGULAR CONTEXT **********/
396
397         predict(Rb, Ra, Rc);
398
399         /* map symmetric contexts */
400         cont = classmap[cont];
401
402         if (cont < 0) 
403         {
404           SIGN = -1;
405           cont = -cont;
406         }
407         else
408           SIGN = +1;
409
410         /* decode a Rice code of a given context */
411         Ra = lossless_regular_mode_d(cont, SIGN, Px);
412
413       }
414
415       sl[i] = Ra;
416       Rc = Rb;
417       Rb = Rd;
418       ++i;
419
420     } while (i <= no);
421
422   }
423   else
424   /***********************************************/
425   /* Do for all pixels in the row in 16-bit mode */
426   /***********************************************/
427   {
428
429     Rc = ENDIAN16(psl[0]);
430     Rb = ENDIAN16(psl[1]);
431     Ra = ENDIAN16(sl[0]);
432
433     i = 1;
434     do {
435
436       pixel Px;
437
438       Rd = ENDIAN16(psl[i + 1]);
439
440       /* Quantize the gradient */
441       {
442         register int diff;
443
444         /* Following segment assumes that T3 <= LUTMAX16 */
445         /* This condition should have been checked when the
446           lookup tables were built */
447         diff = Rd - Rb;
448         if (diff < 0)
449           cont = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
450         else 
451           cont = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
452
453         diff = Rb - Rc;
454         if (diff < 0)
455           cont += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
456         else 
457           cont += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
458         
459         diff = Rc - Ra;
460         if (diff < 0)
461           cont += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
462         else 
463           cont += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
464       }
465
466       if ( cont == 0 ) {
467
468         /********** RUN STATE **********/
469
470         register int n, m;
471
472         /* get length of the run */
473         /* arg is # of pixels left */
474         m = n = process_run_dec(no-i+1, color); 
475
476         if ( m > 0 )  {  /* run of nonzero length, otherwise
477                   we go directly to the end-of-run 
478                   state */
479           do {
480             sl[i++] = ENDIAN16(Ra);
481           }  while(--n > 0);
482
483           if (i > no)
484             /* end of line */
485             return 0;
486
487           /* update context pixels */
488             Rb = ENDIAN16(psl[i]);
489             Rd = ENDIAN16(psl[i + 1]);
490         }
491
492       
493         /* Do end of run encoding for LOSSLESS images */
494         Ra = lossless_end_of_run_d(Ra, Rb, (Ra==Rb));
495   
496       }
497       else 
498       {
499       /********** REGULAR CONTEXT **********/
500
501         predict(Rb, Ra, Rc);
502
503         /* map symmetric contexts */
504         cont = classmap[cont];
505
506         if (cont < 0) 
507         {
508           SIGN = -1;
509           cont = -cont;
510         }
511         else
512           SIGN = +1;
513
514         /* decode a Rice code of a given context */
515         Ra = lossless_regular_mode_d(cont, SIGN, Px);
516
517       }
518
519       sl[i] = ENDIAN16(Ra);
520       Rc = Rb;
521       Rb = Rd;
522       ++i;
523
524     } while (i <= no);
525
526   } /* End "if 8/16 bit" */
527
528   return 0;
529 }
530
531
532
533
534
535
536
537 /* For DEOCODING pixel interleavde mode for LOSSLESS images */
538
539 int lossless_undoscanline_pixel(pixel *psl,    /* previous scanline */
540                 pixel *sl,    /* current scanline */
541                 int no)      /* number of values in it */
542 /*** watch it! actual pixels in the scan line are numbered 1 to no .
543      pixels with indices < 1 or > no are dummy "border" pixels  */
544 {
545   int i, psfix, n_c, color, enter_run=0, was_in_run = 0,
546       test_run;
547   pixel Ra, Rb, Rc, Rd;
548   pixel c_aa[MAX_COMPONENTS],
549         c_bb[MAX_COMPONENTS],
550         c_cc[MAX_COMPONENTS],
551         c_dd[MAX_COMPONENTS],
552         c_xx[MAX_COMPONENTS];
553
554
555   int  SIGN;
556   int cont,c_cont[MAX_COMPONENTS];
557
558   psfix = 0;
559
560   /**********************************************/
561   /* Do for all pixels in the row in 8-bit mode */
562   /**********************************************/
563   if (bpp16==FALSE)
564   {
565
566     for (n_c=0; n_c<components; n_c++) {
567       c_cc[n_c] = psl[n_c];
568       c_bb[n_c] = psl[components+n_c];
569       c_aa[n_c] = sl[n_c];
570     }
571
572     i = components;
573     color = -1;
574
575     do {
576       pixel Px;
577
578       if (!was_in_run) color = (color+1)%components;
579       else color = 0;
580
581       if (color == 0)
582
583         for (n_c=0;n_c<components;n_c++) {
584           c_dd[n_c] = psl[i + components + n_c];
585
586           /* Quantize the gradient */       
587           c_cont[n_c] =  vLUT[0][c_dd[n_c] - c_bb[n_c] + LUTMAX8] +
588                   vLUT[1][c_bb[n_c] - c_cc[n_c] + LUTMAX8] +
589                   vLUT[2][c_cc[n_c] - c_aa[n_c] + LUTMAX8];
590         }
591
592       Ra=c_aa[color];
593       Rb=c_bb[color];
594       Rc=c_cc[color];
595       Rd=c_dd[color];
596       cont=c_cont[color];
597
598       enter_run = was_in_run = test_run = 0;
599     
600       if (color == 0) {
601         test_run = 1;
602         for (n_c=0;n_c<components;n_c++)
603           if (c_cont[n_c]!=0) {
604             test_run=0;
605             break;
606           }
607       }
608
609       if ( test_run ) {
610
611         /********** RUN STATE *********/
612
613         register int n, m;
614
615         enter_run = was_in_run = 1;
616
617         /* get length of the run */
618         /* arg is # of pixels left */
619         m = n = process_run_dec((no+components-1-i+1)/components, 0); 
620
621         if ( m > 0 )  {  /* run of nonzero length, otherwise
622                   we go directly to the end-of-run 
623                   state */
624           do {
625             for (n_c=0;n_c<components;n_c++) {
626               sl[i++] = c_aa[n_c];
627             }
628           } while(--n > 0);
629
630           if (i > no+components-1)
631               /* end of line */
632               return 0;
633
634           /* update context pixels */
635           for (n_c=0;n_c<components;n_c++) {
636               c_bb[n_c] = psl[i+n_c];
637               c_dd[n_c] = psl[i+components+n_c];
638           }
639         }
640
641         /* here we handle the "end-of-run" stat */
642
643         for (n_c=0;n_c<components;n_c++) {
644         /* The end of run is processed for each component */
645           Ra = c_aa[n_c];
646           Rb = c_bb[n_c];
647           c_aa[n_c] = c_xx[n_c] = lossless_end_of_run_d(Ra, Rb, 0);
648         }       /* Components loop */
649
650       }       /* Run state block */
651       else {
652
653       /******* REGULAR CONTEXT *******/
654
655         predict(Rb, Ra, Rc);
656     
657           cont = classmap[cont];
658         if (cont < 0) 
659         {
660           SIGN = -1;
661           cont = -cont;
662         }
663         else
664           SIGN = +1;
665
666         /* decode a Rice code of a given context */
667         c_aa[color] = Ra = lossless_regular_mode_d(cont, SIGN, Px);
668       }
669
670       if (!was_in_run) {
671         sl[i] = Ra;
672         c_cc[color] = Rb;
673         c_bb[color] = Rd;
674         i++;
675       }
676       else {
677         for (n_c=0;n_c<components;n_c++) {
678           sl[i+n_c] = c_aa[n_c];
679           c_cc[n_c] = c_bb[n_c];
680           c_bb[n_c] = c_dd[n_c];
681         }
682         i+=components;
683       }
684       
685     } while (i <= (no+components-1));
686
687   }
688   else
689   /***********************************************/
690   /* Do for all pixels in the row in 16-bit mode */
691   /***********************************************/
692   {
693
694     for (n_c=0; n_c<components; n_c++) {
695       c_cc[n_c] = ENDIAN16(psl[n_c]);
696       c_bb[n_c] = ENDIAN16(psl[components+n_c]);
697       c_aa[n_c] = ENDIAN16(sl[n_c]);
698     }
699
700     i = components;
701     color = -1;
702
703     do {
704       pixel Px;
705
706       if (!was_in_run) color = (color+1)%components;
707       else color = 0;
708
709       if (color == 0)
710         for (n_c=0;n_c<components;n_c++) {
711
712           c_dd[n_c] = ENDIAN16(psl[i + components + n_c]);
713
714           /* Quantize the gradient */
715           {
716             register int diff;
717
718             /* Following segment assumes that T3 <= LUTMAX16 */
719             /* This condition should have been checked when the
720             lookup tables were built */
721             diff = c_dd[n_c] - c_bb[n_c];
722             if (diff < 0)
723               c_cont[n_c] = (diff > -LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 7*CREGIONS*CREGIONS;
724             else 
725               c_cont[n_c] = (diff < LUTMAX16) ? vLUT[0][diff + LUTMAX16] : 8*CREGIONS*CREGIONS;
726
727             diff = c_bb[n_c] - c_cc[n_c];
728             if (diff < 0)
729               c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 7*CREGIONS;
730             else 
731               c_cont[n_c] += (diff < LUTMAX16) ? vLUT[1][diff + LUTMAX16] : 8*CREGIONS;
732
733             diff = c_cc[n_c] - c_aa[n_c];
734             if (diff < 0)
735               c_cont[n_c] += (diff > -LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 7;
736             else 
737               c_cont[n_c] += (diff < LUTMAX16) ? vLUT[2][diff + LUTMAX16] : 8;
738           }
739         }
740
741       Ra=c_aa[color];
742       Rb=c_bb[color];
743       Rc=c_cc[color];
744       Rd=c_dd[color];
745       cont=c_cont[color];
746
747       enter_run = was_in_run = test_run = 0;
748     
749       if (color == 0) {
750         test_run = 1;
751         for (n_c=0;n_c<components;n_c++)
752           if (c_cont[n_c]!=0) {
753             test_run=0;
754             break;
755           }
756       }
757
758       if ( test_run ) {
759
760         /********* RUN STATE *********/
761
762         register int n, m;
763
764         enter_run = was_in_run = 1;
765
766         /* get length of the run */
767         /* arg is # of pixels left */
768         m = n = process_run_dec((no+components-1-i+1)/components, 0); 
769
770         if ( m > 0 )  {  /* run of nonzero length, otherwise
771                   we go directly to the end-of-run 
772                   state */
773           do {
774             for (n_c=0;n_c<components;n_c++) {
775               sl[i++] = ENDIAN16(c_aa[n_c]);
776             }
777           } while(--n > 0);
778
779           if (i > no+components-1)
780             /* end of line */
781             return 0;
782
783           /* update context pixels */
784           for (n_c=0;n_c<components;n_c++) {
785               c_bb[n_c] = ENDIAN16(psl[i+n_c]);
786               c_dd[n_c] = ENDIAN16(psl[i+components+n_c]);
787           }
788         }
789
790         /* here we handle the "end-of-run" state */
791         for (n_c=0;n_c<components;n_c++) {
792           /* The end of run is processed for each component */
793           Ra = c_aa[n_c];
794           Rb = c_bb[n_c];
795
796           c_aa[n_c] = c_xx[n_c] = lossless_end_of_run_d(Ra, Rb, 0);
797         }       /* Components loop */
798
799       }       /* Run state block */
800       else {
801
802       /******** REGULAR CONTEXT ********/
803
804         predict(Rb, Ra, Rc);
805       
806         cont = classmap[cont];
807       
808         if (cont < 0) 
809         {
810           SIGN = -1;
811           cont = -cont;
812         }
813         else
814           SIGN = +1;
815
816         /* decode a Rice code of a given context */
817         c_aa[color] = Ra = lossless_regular_mode_d(cont, SIGN, Px);
818
819       }
820
821       if (!was_in_run) {
822         sl[i] = ENDIAN16(Ra);
823         c_cc[color] = Rb;
824         c_bb[color] = Rd;
825         i++;
826       }
827       else {
828         for (n_c=0;n_c<components;n_c++) {
829         sl[i+n_c] = ENDIAN16(c_aa[n_c]);
830         c_cc[n_c] = c_bb[n_c];
831         c_bb[n_c] = c_dd[n_c];
832         }
833         i+=components;
834       }
835
836     } while (i <= (no+components-1));
837
838   } /* ends "if 8/16 bit */
839
840   return 0;
841 }