1 /*
2  * jdpred.c
3  *
4  * Copyright (C) 1998, Thomas G. Lane.
5  * This file is part of the Independent JPEG Group's software.
6  * For conditions of distribution and use, see the accompanying README file.
7  *
8  * This file contains sample undifferencing (reconstruction) for lossless JPEG.
9  *
10  * In order to avoid paying the performance penalty of having to check the
11  * predictor being used and the row being processed for each call of the
12  * undifferencer, and to promote optimization, we have separate undifferencing
13  * functions for each case.
14  *
15  * We are able to avoid duplicating source code by implementing the predictors
16  * and undifferencers as macros.  Each of the undifferencing functions are
17  * simply wrappers around an UNDIFFERENCE macro with the appropriate PREDICTOR
18  * macro passed as an argument.
19  */
20 
21 #define JPEG_INTERNALS
22 #include "jinclude.h"
23 #include "jpeglib.h"
24 #include "jlossls.h"    /* Private declarations for lossless codec */
25 
26 
27 #ifdef D_LOSSLESS_SUPPORTED
28 
29 /* Predictor for the first column of the first row: 2^(P-Pt-1) */
30 #define INITIAL_PREDICTORx  (1 << (cinfo->data_precision - cinfo->Al - 1))
31 
32 /* Predictor for the first column of the remaining rows: Rb */
33 #define INITIAL_PREDICTOR2  GETJSAMPLE(prev_row[0])
34 
35 
36 /*
37  * 1-Dimensional undifferencer routine.
38  *
39  * This macro implements the 1-D horizontal predictor (1).  INITIAL_PREDICTOR
40  * is used as the special case predictor for the first column, which must be
41  * either INITIAL_PREDICTOR2 or INITIAL_PREDICTORx.  The remaining samples
42  * use PREDICTOR1.
43  *
44  * The reconstructed sample is supposed to be calculated modulo 2^16, so we
45  * logically AND the result with 0xFFFF.
46 */
47 
48 #define UNDIFFERENCE_1D(INITIAL_PREDICTOR) \
49   unsigned int xindex; \
50   int Ra; \
51  \
52   Ra = (diff_buf[0] + INITIAL_PREDICTOR) & 0xFFFF; \
53   undiff_buf[0] = Ra; \
54  \
55   for (xindex = 1; xindex < width; xindex++) { \
56     Ra = (diff_buf[xindex] + PREDICTOR1) & 0xFFFF; \
57     undiff_buf[xindex] = Ra; \
58   }
59 
60 /*
61  * 2-Dimensional undifferencer routine.
62  *
63  * This macro implements the 2-D horizontal predictors (#2-7).  PREDICTOR2 is
64  * used as the special case predictor for the first column.  The remaining
65  * samples use PREDICTOR, which is a function of Ra, Rb, Rc.
66  *
67  * Because prev_row and output_buf may point to the same storage area (in an
68  * interleaved image with Vi=1, for example), we must take care to buffer Rb/Rc
69  * before writing the current reconstructed sample value into output_buf.
70  *
71  * The reconstructed sample is supposed to be calculated modulo 2^16, so we
72  * logically AND the result with 0xFFFF.
73  */
74 
75 #define UNDIFFERENCE_2D_BUG(PREDICTOR) \
76   Rb = GETJSAMPLE(prev_row[0]); \
77   Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF; \
78   undiff_buf[0] = Ra; \
79  \
80   for (xindex = 1; xindex < width; xindex++) { \
81     Rc = Rb; \
82     Rb = GETJSAMPLE(prev_row[xindex]); \
83     Ra = (diff_buf[xindex] + PREDICTOR) & 0xFFFF; \
84     undiff_buf[xindex] = Ra; \
85   }
86 
87 #define UNDIFFERENCE_2D(PREDICTOR) \
88   unsigned int xindex; \
89   int Ra, Rb, Rc; \
90  \
91   Rb = GETJSAMPLE(prev_row[0]); \
92   Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF; \
93   undiff_buf[0] = Ra; \
94  \
95   for (xindex = 1; xindex < width; xindex++) { \
96     Rc = Rb; \
97     Rb = GETJSAMPLE(prev_row[xindex]); \
98     Ra = (diff_buf[xindex] + PREDICTOR) & 0xFFFF; \
99     undiff_buf[xindex] = Ra; \
100   }
101 
102 
103 /*
104  * Undifferencers for the all rows but the first in a scan or restart interval.
105  * The first sample in the row is undifferenced using the vertical
106  * predictor (2).  The rest of the samples are undifferenced using the
107  * predictor specified in the scan header.
108  */
109 
110 METHODDEF(void)
jpeg_undifference1(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)111 jpeg_undifference1(j_decompress_ptr cinfo, int comp_index,
112        JDIFFROW diff_buf, JDIFFROW prev_row,
113        JDIFFROW undiff_buf, JDIMENSION width)
114 {
115   UNDIFFERENCE_1D(INITIAL_PREDICTOR2);
116   (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
117 }
118 
119 METHODDEF(void)
jpeg_undifference2(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)120 jpeg_undifference2(j_decompress_ptr cinfo, int comp_index,
121        JDIFFROW diff_buf, JDIFFROW prev_row,
122        JDIFFROW undiff_buf, JDIMENSION width)
123 {
124   UNDIFFERENCE_2D(PREDICTOR2);
125   (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
126 }
127 
128 METHODDEF(void)
jpeg_undifference3(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)129 jpeg_undifference3(j_decompress_ptr cinfo, int comp_index,
130        JDIFFROW diff_buf, JDIFFROW prev_row,
131        JDIFFROW undiff_buf, JDIMENSION width)
132 {
133   UNDIFFERENCE_2D(PREDICTOR3);
134   (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
135 }
136 
137 METHODDEF(void)
jpeg_undifference4(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)138 jpeg_undifference4(j_decompress_ptr cinfo, int comp_index,
139        JDIFFROW diff_buf, JDIFFROW prev_row,
140        JDIFFROW undiff_buf, JDIMENSION width)
141 {
142   UNDIFFERENCE_2D(PREDICTOR4);
143   (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
144 }
145 
146 METHODDEF(void)
jpeg_undifference5(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)147 jpeg_undifference5(j_decompress_ptr cinfo, int comp_index,
148        JDIFFROW diff_buf, JDIFFROW prev_row,
149        JDIFFROW undiff_buf, JDIMENSION width)
150 {
151   SHIFT_TEMPS
152   UNDIFFERENCE_2D(PREDICTOR5);
153   (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
154 }
155 
156 #ifdef SUPPORT_DICOMOBJECTS_BUG
157 /* uninitialized */
158 static int dicomobjectsbug = -1; /* 0 == nobug, 1 == bug */
159 #endif
160 
161 METHODDEF(void)
jpeg_undifference6(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)162 jpeg_undifference6(j_decompress_ptr cinfo, int comp_index,
163        JDIFFROW diff_buf, JDIFFROW prev_row,
164        JDIFFROW undiff_buf, JDIMENSION width)
165 {
166 #ifdef SUPPORT_DICOMOBJECTS_BUG
167   unsigned int xindex;
168   int Ra, Rb, Rc;
169   int min, max, temp;
170   SHIFT_TEMPS
171   if( dicomobjectsbug == -1 )
172     {
173     dicomobjectsbug = 0; /* no bug by default */
174 
175     Rb = GETJSAMPLE(prev_row[0]);
176     Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF;
177     undiff_buf[0] = Ra;
178     temp = min = max = undiff_buf[0];
179 
180     for (xindex = 1; xindex < width; xindex++) {
181       Rc = Rb;
182       Rb = GETJSAMPLE(prev_row[xindex]);
183       Ra = (diff_buf[xindex] + PREDICTOR6) & 0xFFFF;
184       temp = Ra;
185       min = temp < min ? temp : min;
186       max = temp > max ? temp : max;
187     }
188     if( (max - min) > 50000) /* magic number */
189       {
190       dicomobjectsbug = 1;
191       WARNMS(cinfo, JWRN_SIGNED_ARITH);
192       }
193     }
194   if(dicomobjectsbug)
195     {
196     UNDIFFERENCE_2D_BUG(PREDICTOR6_BUG);
197     }
198   else
199     {
200     UNDIFFERENCE_2D_BUG(PREDICTOR6);
201     }
202 #else
203   SHIFT_TEMPS
204   UNDIFFERENCE_2D(PREDICTOR6);
205 #endif
206   (void)comp_index;(void)cinfo;
207 }
208 
209 METHODDEF(void)
jpeg_undifference7(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)210 jpeg_undifference7(j_decompress_ptr cinfo, int comp_index,
211        JDIFFROW diff_buf, JDIFFROW prev_row,
212        JDIFFROW undiff_buf, JDIMENSION width)
213 {
214   SHIFT_TEMPS
215   UNDIFFERENCE_2D(PREDICTOR7);
216   (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
217 }
218 
219 
220 /*
221  * Undifferencer for the first row in a scan or restart interval.  The first
222  * sample in the row is undifferenced using the special predictor constant
223  * x=2^(P-Pt-1).  The rest of the samples are undifferenced using the
224  * 1-D horizontal predictor (1).
225  */
226 
227 METHODDEF(void)
jpeg_undifference_first_row(j_decompress_ptr cinfo,int comp_index,JDIFFROW diff_buf,JDIFFROW prev_row,JDIFFROW undiff_buf,JDIMENSION width)228 jpeg_undifference_first_row(j_decompress_ptr cinfo, int comp_index,
229           JDIFFROW diff_buf, JDIFFROW prev_row,
230           JDIFFROW undiff_buf, JDIMENSION width)
231 {
232   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
233 
234   UNDIFFERENCE_1D(INITIAL_PREDICTORx);
235   (void)prev_row;
236 
237   /*
238    * Now that we have undifferenced the first row, we want to use the
239    * undifferencer which corresponds to the predictor specified in the
240    * scan header.
241    */
242   switch (cinfo->Ss) {
243   case 1:
244     losslsd->predict_undifference[comp_index] = jpeg_undifference1;
245     break;
246   case 2:
247     losslsd->predict_undifference[comp_index] = jpeg_undifference2;
248     break;
249   case 3:
250     losslsd->predict_undifference[comp_index] = jpeg_undifference3;
251     break;
252   case 4:
253     losslsd->predict_undifference[comp_index] = jpeg_undifference4;
254     break;
255   case 5:
256     losslsd->predict_undifference[comp_index] = jpeg_undifference5;
257     break;
258   case 6:
259     losslsd->predict_undifference[comp_index] = jpeg_undifference6;
260     break;
261   case 7:
262     losslsd->predict_undifference[comp_index] = jpeg_undifference7;
263     break;
264   }
265 }
266 
267 
268 /*
269  * Initialize for an input processing pass.
270  */
271 
272 METHODDEF(void)
predict_start_pass(j_decompress_ptr cinfo)273 predict_start_pass (j_decompress_ptr cinfo)
274 {
275   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
276   int ci;
277 
278   /* Check that the scan parameters Ss, Se, Ah, Al are OK for lossless JPEG.
279    *
280    * Ss is the predictor selection value (psv).  Legal values for sequential
281    * lossless JPEG are: 1 <= psv <= 7.
282    *
283    * Se and Ah are not used and should be zero.
284    *
285    * Al specifies the point transform (Pt).  Legal values are: 0 <= Pt <= 15.
286    */
287   if (cinfo->Ss < 1 || cinfo->Ss > 7 ||
288       cinfo->Se != 0 || cinfo->Ah != 0 ||
289       cinfo->Al > 15)        /* need not check for < 0 */
290     ERREXIT4(cinfo, JERR_BAD_LOSSLESS,
291        cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
292 
293   /* Set undifference functions to first row function */
294   for (ci = 0; ci < cinfo->num_components; ci++)
295     losslsd->predict_undifference[ci] = jpeg_undifference_first_row;
296 }
297 
298 
299 /*
300  * Module initialization routine for the undifferencer.
301  */
302 
303 GLOBAL(void)
jinit_undifferencer(j_decompress_ptr cinfo)304 jinit_undifferencer (j_decompress_ptr cinfo)
305 {
306   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
307 
308   losslsd->predict_start_pass = predict_start_pass;
309   losslsd->predict_process_restart = predict_start_pass;
310 }
311 
312 #endif /* D_LOSSLESS_SUPPORTED */
313