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
44
45
46
47
48
49
50
51
52
53
54
55
58
59
60
61
62
63
64
65
68
69
70
79
80
81
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
114
115
116
117
118
119
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
148
149
150
156
157
158
159
163
164
165
169
170
171
172
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
217
218
222
223
224
225
226
227
228
232
233
234
235
236
237
238
239
240
241
242
243
253
254
255
256
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
281
282
283
284
285
286
287
288
289
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
316
317
318
321
322
323
324
325
326
327
328
329
352
353
360
361
/* ... */
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
typedef JMETHOD(void, upsample1_ptr,
(j_decompress_ptr cinfo, jpeg_component_info * compptr,
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
typedef struct {
struct jpeg_upsampler pub;
/* ... */
JSAMPARRAY color_buf[MAX_COMPONENTS];
upsample1_ptr methods[MAX_COMPONENTS];
int next_row_out;
JDIMENSION rows_to_go;
int rowgroup_height[MAX_COMPONENTS];
/* ... */
UINT8 h_expand[MAX_COMPONENTS];
UINT8 v_expand[MAX_COMPONENTS];
...} my_upsampler;
typedef my_upsampler * my_upsample_ptr;
/* ... */
METHODDEF(void)
start_pass_upsample (j_decompress_ptr cinfo)
{
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
upsample->next_row_out = cinfo->max_v_samp_factor;
upsample->rows_to_go = cinfo->output_height;
}{ ... }
/* ... */
METHODDEF(void)
sep_upsample (j_decompress_ptr cinfo,
JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
JDIMENSION in_row_groups_avail,
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
JDIMENSION out_rows_avail)
{
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
int ci;
jpeg_component_info * compptr;
JDIMENSION num_rows;
if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
/* ... */
(*upsample->methods[ci]) (cinfo, compptr,
input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
upsample->color_buf + ci);
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
upsample->next_row_out = 0;
}if (upsample->next_row_out >= cinfo->max_v_samp_factor) { ... }
num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
/* ... */
if (num_rows > upsample->rows_to_go)
num_rows = upsample->rows_to_go;
out_rows_avail -= *out_row_ctr;
if (num_rows > out_rows_avail)
num_rows = out_rows_avail;
(*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
(JDIMENSION) upsample->next_row_out,
output_buf + *out_row_ctr,
(int) num_rows);
*out_row_ctr += num_rows;
upsample->rows_to_go -= num_rows;
upsample->next_row_out += num_rows;
if (upsample->next_row_out >= cinfo->max_v_samp_factor)
(*in_row_group_ctr)++;
}{ ... }
/* ... */
/* ... */
METHODDEF(void)
fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
*output_data_ptr = input_data;
}{ ... }
/* ... */
METHODDEF(void)
noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
*output_data_ptr = NULL;
}{ ... }
/* ... */
METHODDEF(void)
int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
JSAMPARRAY output_data = *output_data_ptr;
register JSAMPROW inptr, outptr;
register JSAMPLE invalue;
register int h;
JSAMPROW outend;
int h_expand, v_expand;
int inrow, outrow;
h_expand = upsample->h_expand[compptr->component_index];
v_expand = upsample->v_expand[compptr->component_index];
inrow = outrow = 0;
while (outrow < cinfo->max_v_samp_factor) {
inptr = input_data[inrow];
outptr = output_data[outrow];
outend = outptr + cinfo->output_width;
while (outptr < outend) {
invalue = *inptr++;
for (h = h_expand; h > 0; h--) {
*outptr++ = invalue;
}for (h = h_expand; h > 0; h--) { ... }
}while (outptr < outend) { ... }
if (v_expand > 1) {
jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
v_expand-1, cinfo->output_width);
}if (v_expand > 1) { ... }
inrow++;
outrow += v_expand;
}while (outrow < cinfo->max_v_samp_factor) { ... }
}{ ... }
/* ... */
METHODDEF(void)
h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
JSAMPARRAY output_data = *output_data_ptr;
register JSAMPROW inptr, outptr;
register JSAMPLE invalue;
JSAMPROW outend;
int outrow;
for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) {
inptr = input_data[outrow];
outptr = output_data[outrow];
outend = outptr + cinfo->output_width;
while (outptr < outend) {
invalue = *inptr++;
*outptr++ = invalue;
*outptr++ = invalue;
}while (outptr < outend) { ... }
}for (outrow = 0; outrow < cinfo->max_v_samp_factor; outrow++) { ... }
}{ ... }
/* ... */
METHODDEF(void)
h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
{
JSAMPARRAY output_data = *output_data_ptr;
register JSAMPROW inptr, outptr;
register JSAMPLE invalue;
JSAMPROW outend;
int inrow, outrow;
inrow = outrow = 0;
while (outrow < cinfo->max_v_samp_factor) {
inptr = input_data[inrow];
outptr = output_data[outrow];
outend = outptr + cinfo->output_width;
while (outptr < outend) {
invalue = *inptr++;
*outptr++ = invalue;
*outptr++ = invalue;
}while (outptr < outend) { ... }
jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
1, cinfo->output_width);
inrow++;
outrow += 2;
}while (outrow < cinfo->max_v_samp_factor) { ... }
}{ ... }
/* ... */
GLOBAL(void)
jinit_upsampler (j_decompress_ptr cinfo)
{
my_upsample_ptr upsample;
int ci;
jpeg_component_info * compptr;
boolean need_buffer;
int h_in_group, v_in_group, h_out_group, v_out_group;
upsample = (my_upsample_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(my_upsampler));
cinfo->upsample = (struct jpeg_upsampler *) upsample;
upsample->pub.start_pass = start_pass_upsample;
upsample->pub.upsample = sep_upsample;
upsample->pub.need_context_rows = FALSE;
if (cinfo->CCIR601_sampling)
ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
/* ... */
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
/* ... */
h_in_group = (compptr->h_samp_factor * compptr->DCT_h_scaled_size) /
cinfo->min_DCT_h_scaled_size;
v_in_group = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
cinfo->min_DCT_v_scaled_size;
h_out_group = cinfo->max_h_samp_factor;
v_out_group = cinfo->max_v_samp_factor;
upsample->rowgroup_height[ci] = v_in_group;
need_buffer = TRUE;
if (! compptr->component_needed) {
upsample->methods[ci] = noop_upsample;
need_buffer = FALSE;
}if (! compptr->component_needed) { ... } else if (h_in_group == h_out_group && v_in_group == v_out_group) {
upsample->methods[ci] = fullsize_upsample;
need_buffer = FALSE;
}else if (h_in_group == h_out_group && v_in_group == v_out_group) { ... } else if (h_in_group * 2 == h_out_group &&
v_in_group == v_out_group) {
upsample->methods[ci] = h2v1_upsample;
}else if (h_in_group * 2 == h_out_group && v_in_group == v_out_group) { ... } else if (h_in_group * 2 == h_out_group &&
v_in_group * 2 == v_out_group) {
upsample->methods[ci] = h2v2_upsample;
}else if (h_in_group * 2 == h_out_group && v_in_group * 2 == v_out_group) { ... } else if ((h_out_group % h_in_group) == 0 &&
(v_out_group % v_in_group) == 0) {
upsample->methods[ci] = int_upsample;
upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
}else if ((h_out_group % h_in_group) == 0 && (v_out_group % v_in_group) == 0) { ... } else
ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
if (need_buffer) {
upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE,
(JDIMENSION) jround_up((long) cinfo->output_width,
(long) cinfo->max_h_samp_factor),
(JDIMENSION) cinfo->max_v_samp_factor);
}if (need_buffer) { ... }
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
}{ ... }