Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jdpred.c
blob2f62a4820b28114dbe22890327c62e8e03d8c922
1 /*
2 $Id$
3 */
5 /*
6 * jdpred.c
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
26 #include "jinclude.h"
27 #include "jpeglib.h"
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
46 * use PREDICTOR1.
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) \
53 int xindex; \
54 int Ra; \
56 Ra = (diff_buf[0] + INITIAL_PREDICTOR) & 0xFFFF; \
57 undiff_buf[0] = Ra; \
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) \
80 int xindex; \
81 int Ra, Rb, Rc; \
83 Rb = GETJSAMPLE(prev_row[0]); \
84 Ra = (diff_buf[0] + PREDICTOR2) & 0xFFFF; \
85 undiff_buf[0] = Ra; \
87 for (xindex = 1; xindex < width; xindex++) { \
88 Rc = Rb; \
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.
102 METHODDEF(void)
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);
110 METHODDEF(void)
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);
118 METHODDEF(void)
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);
126 METHODDEF(void)
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);
134 METHODDEF(void)
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);
142 METHODDEF(void)
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);
150 METHODDEF(void)
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).
166 METHODDEF(void)
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
178 * scan header.
180 switch (cinfo->Ss) {
181 case 1:
182 losslsd->predict_undifference[comp_index] = jpeg_undifference1;
183 break;
184 case 2:
185 losslsd->predict_undifference[comp_index] = jpeg_undifference2;
186 break;
187 case 3:
188 losslsd->predict_undifference[comp_index] = jpeg_undifference3;
189 break;
190 case 4:
191 losslsd->predict_undifference[comp_index] = jpeg_undifference4;
192 break;
193 case 5:
194 losslsd->predict_undifference[comp_index] = jpeg_undifference5;
195 break;
196 case 6:
197 losslsd->predict_undifference[comp_index] = jpeg_undifference6;
198 break;
199 case 7:
200 losslsd->predict_undifference[comp_index] = jpeg_undifference7;
201 break;
207 * Initialize for an input processing pass.
210 METHODDEF(void)
211 predict_start_pass (j_decompress_ptr cinfo)
213 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
214 int ci;
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.
241 JGLOBAL(void)
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 */