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
153
154
155
156
157
158
161
162
163
164
165
166
167
168
171
172
173
174
175
176
177
178
179
180
183
184
185
186
187
188
189
190
191
192
193
194
195
196
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
220
221
225
230
233
234
235
236
237
238
239
242
243
244
245
246
247
248
249
250
251
252
253
254
255
261
262
263
264
265
266
267
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
288
291
294
295
298
299
300
301
302
305
306
307
337
338
339
343
344
345
346
347
348
349
350
351
352
353
358
359
360
361
365
366
367
368
369
370
371
372
376
377
378
379
383
384
385
386
387
388
389
390
391
392
399
400
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
455
456
457
469
470
471
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
494
502
503
504
505
506
507
508
509
510
511
512
/* ... */
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
/* ... */
typedef struct {
struct jpeg_d_main_controller pub;
JSAMPARRAY buffer[MAX_COMPONENTS];
boolean buffer_full;
JDIMENSION rowgroup_ctr;
JSAMPIMAGE xbuffer[2];
int whichptr;
int context_state;
JDIMENSION rowgroups_avail;
JDIMENSION iMCU_row_ctr;
...} my_main_controller;
typedef my_main_controller * my_main_ptr;
#define CTX_PREPARE_FOR_IMCU 0
#define CTX_PROCESS_IMCU 1
#define CTX_POSTPONED_ROW 2
METHODDEF(void) process_data_simple_main
JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
METHODDEF(void) process_data_context_main
JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));
#ifdef QUANT_2PASS_SUPPORTED
METHODDEF(void) process_data_crank_post
JPP((j_decompress_ptr cinfo, JSAMPARRAY output_buf,
JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail));/* ... */
#endif
LOCAL(void)
alloc_funny_pointers (j_decompress_ptr cinfo)
/* ... */
{
my_main_ptr main = (my_main_ptr) cinfo->main;
int ci, rgroup;
int M = cinfo->min_DCT_v_scaled_size;
jpeg_component_info *compptr;
JSAMPARRAY xbuf;
/* ... */
main->xbuffer[0] = (JSAMPIMAGE)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
cinfo->min_DCT_v_scaled_size;
/* ... */
xbuf = (JSAMPARRAY)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
xbuf += rgroup;
main->xbuffer[0][ci] = xbuf;
xbuf += rgroup * (M + 4);
main->xbuffer[1][ci] = xbuf;
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
...}
LOCAL(void)
make_funny_pointers (j_decompress_ptr cinfo)
/* ... */
{
my_main_ptr main = (my_main_ptr) cinfo->main;
int ci, i, rgroup;
int M = cinfo->min_DCT_v_scaled_size;
jpeg_component_info *compptr;
JSAMPARRAY buf, xbuf0, xbuf1;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
cinfo->min_DCT_v_scaled_size;
xbuf0 = main->xbuffer[0][ci];
xbuf1 = main->xbuffer[1][ci];
buf = main->buffer[ci];
for (i = 0; i < rgroup * (M + 2); i++) {
xbuf0[i] = xbuf1[i] = buf[i];
}for (i = 0; i < rgroup * (M + 2); i++) { ... }
for (i = 0; i < rgroup * 2; i++) {
xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
}for (i = 0; i < rgroup * 2; i++) { ... }
/* ... */
for (i = 0; i < rgroup; i++) {
xbuf0[i - rgroup] = xbuf0[0];
}for (i = 0; i < rgroup; i++) { ... }
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
...}
LOCAL(void)
set_wraparound_pointers (j_decompress_ptr cinfo)
/* ... */
{
my_main_ptr main = (my_main_ptr) cinfo->main;
int ci, i, rgroup;
int M = cinfo->min_DCT_v_scaled_size;
jpeg_component_info *compptr;
JSAMPARRAY xbuf0, xbuf1;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
cinfo->min_DCT_v_scaled_size;
xbuf0 = main->xbuffer[0][ci];
xbuf1 = main->xbuffer[1][ci];
for (i = 0; i < rgroup; i++) {
xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
xbuf0[rgroup*(M+2) + i] = xbuf0[i];
xbuf1[rgroup*(M+2) + i] = xbuf1[i];
}for (i = 0; i < rgroup; i++) { ... }
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
...}
LOCAL(void)
set_bottom_pointers (j_decompress_ptr cinfo)
/* ... */
{
my_main_ptr main = (my_main_ptr) cinfo->main;
int ci, i, rgroup, iMCUheight, rows_left;
jpeg_component_info *compptr;
JSAMPARRAY xbuf;
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
iMCUheight = compptr->v_samp_factor * compptr->DCT_v_scaled_size;
rgroup = iMCUheight / cinfo->min_DCT_v_scaled_size;
rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
if (rows_left == 0) rows_left = iMCUheight;
/* ... */
if (ci == 0) {
main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
}if (ci == 0) { ... }
/* ... */
xbuf = main->xbuffer[main->whichptr][ci];
for (i = 0; i < rgroup * 2; i++) {
xbuf[rows_left + i] = xbuf[rows_left-1];
}for (i = 0; i < rgroup * 2; i++) { ... }
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
...}
/* ... */
METHODDEF(void)
start_pass_main (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
{
my_main_ptr main = (my_main_ptr) cinfo->main;
switch (pass_mode) {
case JBUF_PASS_THRU:
if (cinfo->upsample->need_context_rows) {
main->pub.process_data = process_data_context_main;
make_funny_pointers(cinfo);
main->whichptr = 0;
main->context_state = CTX_PREPARE_FOR_IMCU;
main->iMCU_row_ctr = 0;
}if (cinfo->upsample->need_context_rows) { ... } else {
main->pub.process_data = process_data_simple_main;
}else { ... }
main->buffer_full = FALSE;
main->rowgroup_ctr = 0;
break;
#ifdef QUANT_2PASS_SUPPORTEDcase JBUF_PASS_THRU:
case JBUF_CRANK_DEST:
main->pub.process_data = process_data_crank_post;
break;/* ... */
#endifcase JBUF_CRANK_DEST:
default:
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
break;default
}switch (pass_mode) { ... }
}{ ... }
/* ... */
METHODDEF(void)
process_data_simple_main (j_decompress_ptr cinfo,
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
JDIMENSION out_rows_avail)
{
my_main_ptr main = (my_main_ptr) cinfo->main;
JDIMENSION rowgroups_avail;
if (! main->buffer_full) {
if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
return;
main->buffer_full = TRUE;
}if (! main->buffer_full) { ... }
rowgroups_avail = (JDIMENSION) cinfo->min_DCT_v_scaled_size;
/* ... */
(*cinfo->post->post_process_data) (cinfo, main->buffer,
&main->rowgroup_ctr, rowgroups_avail,
output_buf, out_row_ctr, out_rows_avail);
if (main->rowgroup_ctr >= rowgroups_avail) {
main->buffer_full = FALSE;
main->rowgroup_ctr = 0;
}if (main->rowgroup_ctr >= rowgroups_avail) { ... }
}{ ... }
/* ... */
METHODDEF(void)
process_data_context_main (j_decompress_ptr cinfo,
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
JDIMENSION out_rows_avail)
{
my_main_ptr main = (my_main_ptr) cinfo->main;
if (! main->buffer_full) {
if (! (*cinfo->coef->decompress_data) (cinfo,
main->xbuffer[main->whichptr]))
return;
main->buffer_full = TRUE;
main->iMCU_row_ctr++;
}if (! main->buffer_full) { ... }
/* ... */
switch (main->context_state) {
case CTX_POSTPONED_ROW:
(*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
&main->rowgroup_ctr, main->rowgroups_avail,
output_buf, out_row_ctr, out_rows_avail);
if (main->rowgroup_ctr < main->rowgroups_avail)
return;
main->context_state = CTX_PREPARE_FOR_IMCU;
if (*out_row_ctr >= out_rows_avail)
return;
case CTX_POSTPONED_ROW:
case CTX_PREPARE_FOR_IMCU:
main->rowgroup_ctr = 0;
main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_v_scaled_size - 1);
/* ... */
if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
set_bottom_pointers(cinfo);
main->context_state = CTX_PROCESS_IMCU;
case CTX_PREPARE_FOR_IMCU:
case CTX_PROCESS_IMCU:
(*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
&main->rowgroup_ctr, main->rowgroups_avail,
output_buf, out_row_ctr, out_rows_avail);
if (main->rowgroup_ctr < main->rowgroups_avail)
return;
if (main->iMCU_row_ctr == 1)
set_wraparound_pointers(cinfo);
main->whichptr ^= 1;
main->buffer_full = FALSE;
main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_v_scaled_size + 1);
main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_v_scaled_size + 2);
main->context_state = CTX_POSTPONED_ROW;case CTX_PROCESS_IMCU:
}switch (main->context_state) { ... }
}{ ... }
/* ... */
#ifdef QUANT_2PASS_SUPPORTED
METHODDEF(void)
process_data_crank_post (j_decompress_ptr cinfo,
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
JDIMENSION out_rows_avail)
{
(*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
(JDIMENSION *) NULL, (JDIMENSION) 0,
output_buf, out_row_ctr, out_rows_avail);
}{ ... }
/* ... */#endif
/* ... */
GLOBAL(void)
jinit_d_main_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
{
my_main_ptr main;
int ci, rgroup, ngroups;
jpeg_component_info *compptr;
main = (my_main_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(my_main_controller));
cinfo->main = (struct jpeg_d_main_controller *) main;
main->pub.start_pass = start_pass_main;
if (need_full_buffer)
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
/* ... */
if (cinfo->upsample->need_context_rows) {
if (cinfo->min_DCT_v_scaled_size < 2)
ERREXIT(cinfo, JERR_NOTIMPL);
alloc_funny_pointers(cinfo);
ngroups = cinfo->min_DCT_v_scaled_size + 2;
}if (cinfo->upsample->need_context_rows) { ... } else {
ngroups = cinfo->min_DCT_v_scaled_size;
}else { ... }
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
rgroup = (compptr->v_samp_factor * compptr->DCT_v_scaled_size) /
cinfo->min_DCT_v_scaled_size;
main->buffer[ci] = (*cinfo->mem->alloc_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE,
compptr->width_in_blocks * compptr->DCT_h_scaled_size,
(JDIMENSION) (rgroup * ngroups));
}for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; ci++, compptr++) { ... }
}{ ... }