]> Creatis software - gdcm.git/blob - src/jpeg/libijg/jdpred.c
ENH: Update the jpeg library. This patch is the result of the ijg lib + ls-patch...
[gdcm.git] / src / jpeg / libijg / jdpred.c
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(PREDICTOR) \
76   unsigned int xindex; \
77   int Ra, Rb, Rc; \
78  \
79   Rb = GETJSAMPLE(prev_row[0]); \
80   Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF; \
81   undiff_buf[0] = Ra; \
82  \
83   for (xindex = 1; xindex < width; xindex++) { \
84     Rc = Rb; \
85     Rb = GETJSAMPLE(prev_row[xindex]); \
86     Ra = (diff_buf[xindex] + PREDICTOR) & 0xFFFF; \
87     undiff_buf[xindex] = Ra; \
88   }
89
90
91 /*
92  * Undifferencers for the all rows but the first in a scan or restart interval.
93  * The first sample in the row is undifferenced using the vertical
94  * predictor (2).  The rest of the samples are undifferenced using the
95  * predictor specified in the scan header.
96  */
97
98 METHODDEF(void)
99 jpeg_undifference1(j_decompress_ptr cinfo, int comp_index,
100        JDIFFROW diff_buf, JDIFFROW prev_row,
101        JDIFFROW undiff_buf, JDIMENSION width)
102 {
103   UNDIFFERENCE_1D(INITIAL_PREDICTOR2);
104 }
105
106 METHODDEF(void)
107 jpeg_undifference2(j_decompress_ptr cinfo, int comp_index,
108        JDIFFROW diff_buf, JDIFFROW prev_row,
109        JDIFFROW undiff_buf, JDIMENSION width)
110 {
111   UNDIFFERENCE_2D(PREDICTOR2);
112 }
113
114 METHODDEF(void)
115 jpeg_undifference3(j_decompress_ptr cinfo, int comp_index,
116        JDIFFROW diff_buf, JDIFFROW prev_row,
117        JDIFFROW undiff_buf, JDIMENSION width)
118 {
119   UNDIFFERENCE_2D(PREDICTOR3);
120 }
121
122 METHODDEF(void)
123 jpeg_undifference4(j_decompress_ptr cinfo, int comp_index,
124        JDIFFROW diff_buf, JDIFFROW prev_row,
125        JDIFFROW undiff_buf, JDIMENSION width)
126 {
127   UNDIFFERENCE_2D(PREDICTOR4);
128 }
129
130 METHODDEF(void)
131 jpeg_undifference5(j_decompress_ptr cinfo, int comp_index,
132        JDIFFROW diff_buf, JDIFFROW prev_row,
133        JDIFFROW undiff_buf, JDIMENSION width)
134 {
135   SHIFT_TEMPS
136   UNDIFFERENCE_2D(PREDICTOR5);
137 }
138
139 METHODDEF(void)
140 jpeg_undifference6(j_decompress_ptr cinfo, int comp_index,
141        JDIFFROW diff_buf, JDIFFROW prev_row,
142        JDIFFROW undiff_buf, JDIMENSION width)
143 {
144   SHIFT_TEMPS
145   UNDIFFERENCE_2D(PREDICTOR6);
146 }
147
148 METHODDEF(void)
149 jpeg_undifference7(j_decompress_ptr cinfo, int comp_index,
150        JDIFFROW diff_buf, JDIFFROW prev_row,
151        JDIFFROW undiff_buf, JDIMENSION width)
152 {
153   SHIFT_TEMPS
154   UNDIFFERENCE_2D(PREDICTOR7);
155 }
156
157
158 /*
159  * Undifferencer for the first row in a scan or restart interval.  The first
160  * sample in the row is undifferenced using the special predictor constant
161  * x=2^(P-Pt-1).  The rest of the samples are undifferenced using the
162  * 1-D horizontal predictor (1).
163  */
164
165 METHODDEF(void)
166 jpeg_undifference_first_row(j_decompress_ptr cinfo, int comp_index,
167           JDIFFROW diff_buf, JDIFFROW prev_row,
168           JDIFFROW undiff_buf, JDIMENSION width)
169 {
170   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
171
172   UNDIFFERENCE_1D(INITIAL_PREDICTORx);
173
174   /*
175    * Now that we have undifferenced the first row, we want to use the
176    * undifferencer which corresponds to the predictor specified in the
177    * scan header.
178    */
179   switch (cinfo->Ss) {
180   case 1:
181     losslsd->predict_undifference[comp_index] = jpeg_undifference1;
182     break;
183   case 2:
184     losslsd->predict_undifference[comp_index] = jpeg_undifference2;
185     break;
186   case 3:
187     losslsd->predict_undifference[comp_index] = jpeg_undifference3;
188     break;
189   case 4:
190     losslsd->predict_undifference[comp_index] = jpeg_undifference4;
191     break;
192   case 5:
193     losslsd->predict_undifference[comp_index] = jpeg_undifference5;
194     break;
195   case 6:
196     losslsd->predict_undifference[comp_index] = jpeg_undifference6;
197     break;
198   case 7:
199     losslsd->predict_undifference[comp_index] = jpeg_undifference7;
200     break;
201   }
202 }
203
204
205 /*
206  * Initialize for an input processing pass.
207  */
208
209 METHODDEF(void)
210 predict_start_pass (j_decompress_ptr cinfo)
211 {
212   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
213   int ci;
214
215   /* Check that the scan parameters Ss, Se, Ah, Al are OK for lossless JPEG.
216    *
217    * Ss is the predictor selection value (psv).  Legal values for sequential
218    * lossless JPEG are: 1 <= psv <= 7.
219    *
220    * Se and Ah are not used and should be zero.
221    *
222    * Al specifies the point transform (Pt).  Legal values are: 0 <= Pt <= 15.
223    */
224   if (cinfo->Ss < 1 || cinfo->Ss > 7 ||
225       cinfo->Se != 0 || cinfo->Ah != 0 ||
226       cinfo->Al > 15)        /* need not check for < 0 */
227     ERREXIT4(cinfo, JERR_BAD_LOSSLESS,
228        cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
229
230   /* Set undifference functions to first row function */
231   for (ci = 0; ci < cinfo->num_components; ci++)
232     losslsd->predict_undifference[ci] = jpeg_undifference_first_row;
233 }
234
235
236 /*
237  * Module initialization routine for the undifferencer.
238  */
239
240 GLOBAL(void)
241 jinit_undifferencer (j_decompress_ptr cinfo)
242 {
243   j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
244
245   losslsd->predict_start_pass = predict_start_pass;
246   losslsd->predict_process_restart = predict_start_pass;
247 }
248
249 #endif /* D_LOSSLESS_SUPPORTED */
250