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