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