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.
8 * This file contains sample undifferencing (reconstruction) for lossless JPEG.
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.
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.
21 #define JPEG_INTERNALS
24 #include "jlossls.h" /* Private declarations for lossless codec */
27 #ifdef D_LOSSLESS_SUPPORTED
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))
32 /* Predictor for the first column of the remaining rows: Rb */
33 #define INITIAL_PREDICTOR2 GETJSAMPLE(prev_row[0])
37 * 1-Dimensional undifferencer routine.
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
44 * The reconstructed sample is supposed to be calculated modulo 2^16, so we
45 * logically AND the result with 0xFFFF.
48 #define UNDIFFERENCE_1D(INITIAL_PREDICTOR) \
49 unsigned int xindex; \
52 Ra = (diff_buf[0] + INITIAL_PREDICTOR) & 0xFFFF; \
55 for (xindex = 1; xindex < width; xindex++) { \
56 Ra = (diff_buf[xindex] + PREDICTOR1) & 0xFFFF; \
57 undiff_buf[xindex] = Ra; \
61 * 2-Dimensional undifferencer routine.
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.
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.
71 * The reconstructed sample is supposed to be calculated modulo 2^16, so we
72 * logically AND the result with 0xFFFF.
75 #define UNDIFFERENCE_2D(PREDICTOR) \
76 unsigned int xindex; \
79 Rb = GETJSAMPLE(prev_row[0]); \
80 Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF; \
83 for (xindex = 1; xindex < width; xindex++) { \
85 Rb = GETJSAMPLE(prev_row[xindex]); \
86 Ra = (diff_buf[xindex] + PREDICTOR) & 0xFFFF; \
87 undiff_buf[xindex] = Ra; \
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.
99 jpeg_undifference1(j_decompress_ptr cinfo, int comp_index,
100 JDIFFROW diff_buf, JDIFFROW prev_row,
101 JDIFFROW undiff_buf, JDIMENSION width)
103 UNDIFFERENCE_1D(INITIAL_PREDICTOR2);
104 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
108 jpeg_undifference2(j_decompress_ptr cinfo, int comp_index,
109 JDIFFROW diff_buf, JDIFFROW prev_row,
110 JDIFFROW undiff_buf, JDIMENSION width)
112 UNDIFFERENCE_2D(PREDICTOR2);
113 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
117 jpeg_undifference3(j_decompress_ptr cinfo, int comp_index,
118 JDIFFROW diff_buf, JDIFFROW prev_row,
119 JDIFFROW undiff_buf, JDIMENSION width)
121 UNDIFFERENCE_2D(PREDICTOR3);
122 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
126 jpeg_undifference4(j_decompress_ptr cinfo, int comp_index,
127 JDIFFROW diff_buf, JDIFFROW prev_row,
128 JDIFFROW undiff_buf, JDIMENSION width)
130 UNDIFFERENCE_2D(PREDICTOR4);
131 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
135 jpeg_undifference5(j_decompress_ptr cinfo, int comp_index,
136 JDIFFROW diff_buf, JDIFFROW prev_row,
137 JDIFFROW undiff_buf, JDIMENSION width)
140 UNDIFFERENCE_2D(PREDICTOR5);
141 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
145 jpeg_undifference6(j_decompress_ptr cinfo, int comp_index,
146 JDIFFROW diff_buf, JDIFFROW prev_row,
147 JDIFFROW undiff_buf, JDIMENSION width)
150 UNDIFFERENCE_2D(PREDICTOR6);
151 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
155 jpeg_undifference7(j_decompress_ptr cinfo, int comp_index,
156 JDIFFROW diff_buf, JDIFFROW prev_row,
157 JDIFFROW undiff_buf, JDIMENSION width)
160 UNDIFFERENCE_2D(PREDICTOR7);
161 (void)cinfo;(void)comp_index;(void)diff_buf;(void)prev_row;(void)undiff_buf;(void)width;
166 * Undifferencer for the first row in a scan or restart interval. The first
167 * sample in the row is undifferenced using the special predictor constant
168 * x=2^(P-Pt-1). The rest of the samples are undifferenced using the
169 * 1-D horizontal predictor (1).
173 jpeg_undifference_first_row(j_decompress_ptr cinfo, int comp_index,
174 JDIFFROW diff_buf, JDIFFROW prev_row,
175 JDIFFROW undiff_buf, JDIMENSION width)
177 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
179 UNDIFFERENCE_1D(INITIAL_PREDICTORx);
183 * Now that we have undifferenced the first row, we want to use the
184 * undifferencer which corresponds to the predictor specified in the
189 losslsd->predict_undifference[comp_index] = jpeg_undifference1;
192 losslsd->predict_undifference[comp_index] = jpeg_undifference2;
195 losslsd->predict_undifference[comp_index] = jpeg_undifference3;
198 losslsd->predict_undifference[comp_index] = jpeg_undifference4;
201 losslsd->predict_undifference[comp_index] = jpeg_undifference5;
204 losslsd->predict_undifference[comp_index] = jpeg_undifference6;
207 losslsd->predict_undifference[comp_index] = jpeg_undifference7;
214 * Initialize for an input processing pass.
218 predict_start_pass (j_decompress_ptr cinfo)
220 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
223 /* Check that the scan parameters Ss, Se, Ah, Al are OK for lossless JPEG.
225 * Ss is the predictor selection value (psv). Legal values for sequential
226 * lossless JPEG are: 1 <= psv <= 7.
228 * Se and Ah are not used and should be zero.
230 * Al specifies the point transform (Pt). Legal values are: 0 <= Pt <= 15.
232 if (cinfo->Ss < 1 || cinfo->Ss > 7 ||
233 cinfo->Se != 0 || cinfo->Ah != 0 ||
234 cinfo->Al > 15) /* need not check for < 0 */
235 ERREXIT4(cinfo, JERR_BAD_LOSSLESS,
236 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
238 /* Set undifference functions to first row function */
239 for (ci = 0; ci < cinfo->num_components; ci++)
240 losslsd->predict_undifference[ci] = jpeg_undifference_first_row;
245 * Module initialization routine for the undifferencer.
249 jinit_undifferencer (j_decompress_ptr cinfo)
251 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
253 losslsd->predict_start_pass = predict_start_pass;
254 losslsd->predict_process_restart = predict_start_pass;
257 #endif /* D_LOSSLESS_SUPPORTED */