8 * Copyright (C) 1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains sample undifferencing (reconstruction) for lossless JPEG.
14 * In order to avoid paying the performance penalty of having to check the
15 * predictor being used and the row being processed for each call of the
16 * undifferencer, and to promote optimization, we have separate undifferencing
17 * functions for each case.
19 * We are able to avoid duplicating source code by implementing the predictors
20 * and undifferencers as macros. Each of the undifferencing functions are
21 * simply wrappers around an UNDIFFERENCE macro with the appropriate PREDICTOR
22 * macro passed as an argument.
25 #define JPEG_INTERNALS
28 #include "jlossls.h" /* Private declarations for lossless codec */
31 #ifdef D_LOSSLESS_SUPPORTED
33 /* Predictor for the first column of the first row: 2^(P-Pt-1) */
34 #define INITIAL_PREDICTORx (1 << (cinfo->data_precision - cinfo->Al - 1))
36 /* Predictor for the first column of the remaining rows: Rb */
37 #define INITIAL_PREDICTOR2 GETJSAMPLE(prev_row[0])
41 * 1-Dimensional undifferencer routine.
43 * This macro implements the 1-D horizontal predictor (1). INITIAL_PREDICTOR
44 * is used as the special case predictor for the first column, which must be
45 * either INITIAL_PREDICTOR2 or INITIAL_PREDICTORx. The remaining samples
48 * The reconstructed sample is supposed to be calculated modulo 2^16, so we
49 * logically AND the result with 0xFFFF.
52 #define UNDIFFERENCE_1D(INITIAL_PREDICTOR) \
56 Ra = (diff_buf[0] + INITIAL_PREDICTOR) & 0xFFFF; \
59 for (xindex = 1; xindex < width; xindex++) { \
60 Ra = (diff_buf[xindex] + PREDICTOR1) & 0xFFFF; \
61 undiff_buf[xindex] = Ra; \
65 * 2-Dimensional undifferencer routine.
67 * This macro implements the 2-D horizontal predictors (#2-7). PREDICTOR2 is
68 * used as the special case predictor for the first column. The remaining
69 * samples use PREDICTOR, which is a function of Ra, Rb, Rc.
71 * Because prev_row and output_buf may point to the same storage area (in an
72 * interleaved image with Vi=1, for example), we must take care to buffer Rb/Rc
73 * before writing the current reconstructed sample value into output_buf.
75 * The reconstructed sample is supposed to be calculated modulo 2^16, so we
76 * logically AND the result with 0xFFFF.
79 #define UNDIFFERENCE_2D(PREDICTOR) \
83 Rb = GETJSAMPLE(prev_row[0]); \
84 Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF; \
87 for (xindex = 1; xindex < width; xindex++) { \
89 Rb = GETJSAMPLE(prev_row[xindex]); \
90 Ra = (diff_buf[xindex] + PREDICTOR) & 0xFFFF; \
91 undiff_buf[xindex] = Ra; \
96 * Undifferencers for the all rows but the first in a scan or restart interval.
97 * The first sample in the row is undifferenced using the vertical
98 * predictor (2). The rest of the samples are undifferenced using the
99 * predictor specified in the scan header.
103 jpeg_undifference1(j_decompress_ptr cinfo
, int comp_index
,
104 JDIFFROW diff_buf
, JDIFFROW prev_row
,
105 JDIFFROW undiff_buf
, JDIMENSION width
)
107 UNDIFFERENCE_1D(INITIAL_PREDICTOR2
);
111 jpeg_undifference2(j_decompress_ptr cinfo
, int comp_index
,
112 JDIFFROW diff_buf
, JDIFFROW prev_row
,
113 JDIFFROW undiff_buf
, JDIMENSION width
)
115 UNDIFFERENCE_2D(PREDICTOR2
);
119 jpeg_undifference3(j_decompress_ptr cinfo
, int comp_index
,
120 JDIFFROW diff_buf
, JDIFFROW prev_row
,
121 JDIFFROW undiff_buf
, JDIMENSION width
)
123 UNDIFFERENCE_2D(PREDICTOR3
);
127 jpeg_undifference4(j_decompress_ptr cinfo
, int comp_index
,
128 JDIFFROW diff_buf
, JDIFFROW prev_row
,
129 JDIFFROW undiff_buf
, JDIMENSION width
)
131 UNDIFFERENCE_2D(PREDICTOR4
);
135 jpeg_undifference5(j_decompress_ptr cinfo
, int comp_index
,
136 JDIFFROW diff_buf
, JDIFFROW prev_row
,
137 JDIFFROW undiff_buf
, JDIMENSION width
)
139 UNDIFFERENCE_2D(PREDICTOR5
);
143 jpeg_undifference6(j_decompress_ptr cinfo
, int comp_index
,
144 JDIFFROW diff_buf
, JDIFFROW prev_row
,
145 JDIFFROW undiff_buf
, JDIMENSION width
)
147 UNDIFFERENCE_2D(PREDICTOR6
);
151 jpeg_undifference7(j_decompress_ptr cinfo
, int comp_index
,
152 JDIFFROW diff_buf
, JDIFFROW prev_row
,
153 JDIFFROW undiff_buf
, JDIMENSION width
)
155 UNDIFFERENCE_2D(PREDICTOR7
);
160 * Undifferencer for the first row in a scan or restart interval. The first
161 * sample in the row is undifferenced using the special predictor constant
162 * x=2^(P-Pt-1). The rest of the samples are undifferenced using the
163 * 1-D horizontal predictor (1).
167 jpeg_undifference_first_row(j_decompress_ptr cinfo
, int comp_index
,
168 JDIFFROW diff_buf
, JDIFFROW prev_row
,
169 JDIFFROW undiff_buf
, JDIMENSION width
)
171 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
173 UNDIFFERENCE_1D(INITIAL_PREDICTORx
);
176 * Now that we have undifferenced the first row, we want to use the
177 * undifferencer which corresponds to the predictor specified in the
182 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference1
;
185 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference2
;
188 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference3
;
191 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference4
;
194 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference5
;
197 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference6
;
200 losslsd
->predict_undifference
[comp_index
] = jpeg_undifference7
;
207 * Initialize for an input processing pass.
211 predict_start_pass (j_decompress_ptr cinfo
)
213 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
216 /* Check that the scan parameters Ss, Se, Ah, Al are OK for lossless JPEG.
218 * Ss is the predictor selection value (psv). Legal values for sequential
219 * lossless JPEG are: 1 <= psv <= 7.
221 * Se and Ah are not used and should be zero.
223 * Al specifies the point transform (Pt). Legal values are: 0 <= Pt <= 15.
225 if (cinfo
->Ss
< 1 || cinfo
->Ss
> 7 ||
226 cinfo
->Se
!= 0 || cinfo
->Ah
!= 0 ||
227 cinfo
->Al
> 15) /* need not check for < 0 */
228 ERREXIT4(cinfo
, JERR_BAD_LOSSLESS
,
229 cinfo
->Ss
, cinfo
->Se
, cinfo
->Ah
, cinfo
->Al
);
231 /* Set undifference functions to first row function */
232 for (ci
= 0; ci
< cinfo
->num_components
; ci
++)
233 losslsd
->predict_undifference
[ci
] = jpeg_undifference_first_row
;
238 * Module initialization routine for the undifferencer.
242 jinit_undifferencer (j_decompress_ptr cinfo
)
244 j_lossless_d_ptr losslsd
= (j_lossless_d_ptr
) cinfo
->codec
;
246 losslsd
->predict_start_pass
= predict_start_pass
;
247 losslsd
->predict_process_restart
= predict_start_pass
;
250 #endif /* D_LOSSLESS_SUPPORTED */