Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcprepct.c
41149 views
1
/*
2
* reserved comment block
3
* DO NOT REMOVE OR ALTER!
4
*/
5
/*
6
* jcprepct.c
7
*
8
* Copyright (C) 1994-1996, 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.
11
*
12
* This file contains the compression preprocessing controller.
13
* This controller manages the color conversion, downsampling,
14
* and edge expansion steps.
15
*
16
* Most of the complexity here is associated with buffering input rows
17
* as required by the downsampler. See the comments at the head of
18
* jcsample.c for the downsampler's needs.
19
*/
20
21
#define JPEG_INTERNALS
22
#include "jinclude.h"
23
#include "jpeglib.h"
24
25
26
/* At present, jcsample.c can request context rows only for smoothing.
27
* In the future, we might also need context rows for CCIR601 sampling
28
* or other more-complex downsampling procedures. The code to support
29
* context rows should be compiled only if needed.
30
*/
31
#ifdef INPUT_SMOOTHING_SUPPORTED
32
#define CONTEXT_ROWS_SUPPORTED
33
#endif
34
35
36
/*
37
* For the simple (no-context-row) case, we just need to buffer one
38
* row group's worth of pixels for the downsampling step. At the bottom of
39
* the image, we pad to a full row group by replicating the last pixel row.
40
* The downsampler's last output row is then replicated if needed to pad
41
* out to a full iMCU row.
42
*
43
* When providing context rows, we must buffer three row groups' worth of
44
* pixels. Three row groups are physically allocated, but the row pointer
45
* arrays are made five row groups high, with the extra pointers above and
46
* below "wrapping around" to point to the last and first real row groups.
47
* This allows the downsampler to access the proper context rows.
48
* At the top and bottom of the image, we create dummy context rows by
49
* copying the first or last real pixel row. This copying could be avoided
50
* by pointer hacking as is done in jdmainct.c, but it doesn't seem worth the
51
* trouble on the compression side.
52
*/
53
54
55
/* Private buffer controller object */
56
57
typedef struct {
58
struct jpeg_c_prep_controller pub; /* public fields */
59
60
/* Downsampling input buffer. This buffer holds color-converted data
61
* until we have enough to do a downsample step.
62
*/
63
JSAMPARRAY color_buf[MAX_COMPONENTS];
64
65
JDIMENSION rows_to_go; /* counts rows remaining in source image */
66
int next_buf_row; /* index of next row to store in color_buf */
67
68
#ifdef CONTEXT_ROWS_SUPPORTED /* only needed for context case */
69
int this_row_group; /* starting row index of group to process */
70
int next_buf_stop; /* downsample when we reach this index */
71
#endif
72
} my_prep_controller;
73
74
typedef my_prep_controller * my_prep_ptr;
75
76
77
/*
78
* Initialize for a processing pass.
79
*/
80
81
METHODDEF(void)
82
start_pass_prep (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
83
{
84
my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
85
86
if (pass_mode != JBUF_PASS_THRU)
87
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
88
89
/* Initialize total-height counter for detecting bottom of image */
90
prep->rows_to_go = cinfo->image_height;
91
/* Mark the conversion buffer empty */
92
prep->next_buf_row = 0;
93
#ifdef CONTEXT_ROWS_SUPPORTED
94
/* Preset additional state variables for context mode.
95
* These aren't used in non-context mode, so we needn't test which mode.
96
*/
97
prep->this_row_group = 0;
98
/* Set next_buf_stop to stop after two row groups have been read in. */
99
prep->next_buf_stop = 2 * cinfo->max_v_samp_factor;
100
#endif
101
}
102
103
104
/*
105
* Expand an image vertically from height input_rows to height output_rows,
106
* by duplicating the bottom row.
107
*/
108
109
LOCAL(void)
110
expand_bottom_edge (JSAMPARRAY image_data, JDIMENSION num_cols,
111
int input_rows, int output_rows)
112
{
113
register int row;
114
115
for (row = input_rows; row < output_rows; row++) {
116
jcopy_sample_rows(image_data, input_rows-1, image_data, row,
117
1, num_cols);
118
}
119
}
120
121
122
/*
123
* Process some data in the simple no-context case.
124
*
125
* Preprocessor output data is counted in "row groups". A row group
126
* is defined to be v_samp_factor sample rows of each component.
127
* Downsampling will produce this much data from each max_v_samp_factor
128
* input rows.
129
*/
130
131
METHODDEF(void)
132
pre_process_data (j_compress_ptr cinfo,
133
JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
134
JDIMENSION in_rows_avail,
135
JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
136
JDIMENSION out_row_groups_avail)
137
{
138
my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
139
int numrows, ci;
140
JDIMENSION inrows;
141
jpeg_component_info * compptr;
142
143
while (*in_row_ctr < in_rows_avail &&
144
*out_row_group_ctr < out_row_groups_avail) {
145
/* Do color conversion to fill the conversion buffer. */
146
inrows = in_rows_avail - *in_row_ctr;
147
numrows = cinfo->max_v_samp_factor - prep->next_buf_row;
148
numrows = (int) MIN((JDIMENSION) numrows, inrows);
149
(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
150
prep->color_buf,
151
(JDIMENSION) prep->next_buf_row,
152
numrows);
153
*in_row_ctr += numrows;
154
prep->next_buf_row += numrows;
155
prep->rows_to_go -= numrows;
156
/* If at bottom of image, pad to fill the conversion buffer. */
157
if (prep->rows_to_go == 0 &&
158
prep->next_buf_row < cinfo->max_v_samp_factor) {
159
for (ci = 0; ci < cinfo->num_components; ci++) {
160
expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
161
prep->next_buf_row, cinfo->max_v_samp_factor);
162
}
163
prep->next_buf_row = cinfo->max_v_samp_factor;
164
}
165
/* If we've filled the conversion buffer, empty it. */
166
if (prep->next_buf_row == cinfo->max_v_samp_factor) {
167
(*cinfo->downsample->downsample) (cinfo,
168
prep->color_buf, (JDIMENSION) 0,
169
output_buf, *out_row_group_ctr);
170
prep->next_buf_row = 0;
171
(*out_row_group_ctr)++;
172
}
173
/* If at bottom of image, pad the output to a full iMCU height.
174
* Note we assume the caller is providing a one-iMCU-height output buffer!
175
*/
176
if (prep->rows_to_go == 0 &&
177
*out_row_group_ctr < out_row_groups_avail) {
178
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
179
ci++, compptr++) {
180
expand_bottom_edge(output_buf[ci],
181
compptr->width_in_blocks * DCTSIZE,
182
(int) (*out_row_group_ctr * compptr->v_samp_factor),
183
(int) (out_row_groups_avail * compptr->v_samp_factor));
184
}
185
*out_row_group_ctr = out_row_groups_avail;
186
break; /* can exit outer loop without test */
187
}
188
}
189
}
190
191
192
#ifdef CONTEXT_ROWS_SUPPORTED
193
194
/*
195
* Process some data in the context case.
196
*/
197
198
METHODDEF(void)
199
pre_process_context (j_compress_ptr cinfo,
200
JSAMPARRAY input_buf, JDIMENSION *in_row_ctr,
201
JDIMENSION in_rows_avail,
202
JSAMPIMAGE output_buf, JDIMENSION *out_row_group_ctr,
203
JDIMENSION out_row_groups_avail)
204
{
205
my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
206
int numrows, ci;
207
int buf_height = cinfo->max_v_samp_factor * 3;
208
JDIMENSION inrows;
209
210
while (*out_row_group_ctr < out_row_groups_avail) {
211
if (*in_row_ctr < in_rows_avail) {
212
/* Do color conversion to fill the conversion buffer. */
213
inrows = in_rows_avail - *in_row_ctr;
214
numrows = prep->next_buf_stop - prep->next_buf_row;
215
numrows = (int) MIN((JDIMENSION) numrows, inrows);
216
(*cinfo->cconvert->color_convert) (cinfo, input_buf + *in_row_ctr,
217
prep->color_buf,
218
(JDIMENSION) prep->next_buf_row,
219
numrows);
220
/* Pad at top of image, if first time through */
221
if (prep->rows_to_go == cinfo->image_height) {
222
for (ci = 0; ci < cinfo->num_components; ci++) {
223
int row;
224
for (row = 1; row <= cinfo->max_v_samp_factor; row++) {
225
jcopy_sample_rows(prep->color_buf[ci], 0,
226
prep->color_buf[ci], -row,
227
1, cinfo->image_width);
228
}
229
}
230
}
231
*in_row_ctr += numrows;
232
prep->next_buf_row += numrows;
233
prep->rows_to_go -= numrows;
234
} else {
235
/* Return for more data, unless we are at the bottom of the image. */
236
if (prep->rows_to_go != 0)
237
break;
238
/* When at bottom of image, pad to fill the conversion buffer. */
239
if (prep->next_buf_row < prep->next_buf_stop) {
240
for (ci = 0; ci < cinfo->num_components; ci++) {
241
expand_bottom_edge(prep->color_buf[ci], cinfo->image_width,
242
prep->next_buf_row, prep->next_buf_stop);
243
}
244
prep->next_buf_row = prep->next_buf_stop;
245
}
246
}
247
/* If we've gotten enough data, downsample a row group. */
248
if (prep->next_buf_row == prep->next_buf_stop) {
249
(*cinfo->downsample->downsample) (cinfo,
250
prep->color_buf,
251
(JDIMENSION) prep->this_row_group,
252
output_buf, *out_row_group_ctr);
253
(*out_row_group_ctr)++;
254
/* Advance pointers with wraparound as necessary. */
255
prep->this_row_group += cinfo->max_v_samp_factor;
256
if (prep->this_row_group >= buf_height)
257
prep->this_row_group = 0;
258
if (prep->next_buf_row >= buf_height)
259
prep->next_buf_row = 0;
260
prep->next_buf_stop = prep->next_buf_row + cinfo->max_v_samp_factor;
261
}
262
}
263
}
264
265
266
/*
267
* Create the wrapped-around downsampling input buffer needed for context mode.
268
*/
269
270
LOCAL(void)
271
create_context_buffer (j_compress_ptr cinfo)
272
{
273
my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
274
int rgroup_height = cinfo->max_v_samp_factor;
275
int ci, i;
276
jpeg_component_info * compptr;
277
JSAMPARRAY true_buffer, fake_buffer;
278
279
/* Grab enough space for fake row pointers for all the components;
280
* we need five row groups' worth of pointers for each component.
281
*/
282
fake_buffer = (JSAMPARRAY)
283
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
284
(cinfo->num_components * 5 * rgroup_height) *
285
SIZEOF(JSAMPROW));
286
287
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
288
ci++, compptr++) {
289
/* Allocate the actual buffer space (3 row groups) for this component.
290
* We make the buffer wide enough to allow the downsampler to edge-expand
291
* horizontally within the buffer, if it so chooses.
292
*/
293
true_buffer = (*cinfo->mem->alloc_sarray)
294
((j_common_ptr) cinfo, JPOOL_IMAGE,
295
(JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
296
cinfo->max_h_samp_factor) / compptr->h_samp_factor),
297
(JDIMENSION) (3 * rgroup_height));
298
/* Copy true buffer row pointers into the middle of the fake row array */
299
MEMCOPY(fake_buffer + rgroup_height, true_buffer,
300
3 * rgroup_height * SIZEOF(JSAMPROW));
301
/* Fill in the above and below wraparound pointers */
302
for (i = 0; i < rgroup_height; i++) {
303
fake_buffer[i] = true_buffer[2 * rgroup_height + i];
304
fake_buffer[4 * rgroup_height + i] = true_buffer[i];
305
}
306
prep->color_buf[ci] = fake_buffer + rgroup_height;
307
fake_buffer += 5 * rgroup_height; /* point to space for next component */
308
}
309
}
310
311
#endif /* CONTEXT_ROWS_SUPPORTED */
312
313
314
/*
315
* Initialize preprocessing controller.
316
*/
317
318
GLOBAL(void)
319
jinit_c_prep_controller (j_compress_ptr cinfo, boolean need_full_buffer)
320
{
321
my_prep_ptr prep;
322
int ci;
323
jpeg_component_info * compptr;
324
325
if (need_full_buffer) /* safety check */
326
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
327
328
prep = (my_prep_ptr)
329
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
330
SIZEOF(my_prep_controller));
331
cinfo->prep = (struct jpeg_c_prep_controller *) prep;
332
prep->pub.start_pass = start_pass_prep;
333
334
/* Allocate the color conversion buffer.
335
* We make the buffer wide enough to allow the downsampler to edge-expand
336
* horizontally within the buffer, if it so chooses.
337
*/
338
if (cinfo->downsample->need_context_rows) {
339
/* Set up to provide context rows */
340
#ifdef CONTEXT_ROWS_SUPPORTED
341
prep->pub.pre_process_data = pre_process_context;
342
create_context_buffer(cinfo);
343
#else
344
ERREXIT(cinfo, JERR_NOT_COMPILED);
345
#endif
346
} else {
347
/* No context, just make it tall enough for one row group */
348
prep->pub.pre_process_data = pre_process_data;
349
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
350
ci++, compptr++) {
351
prep->color_buf[ci] = (*cinfo->mem->alloc_sarray)
352
((j_common_ptr) cinfo, JPOOL_IMAGE,
353
(JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
354
cinfo->max_h_samp_factor) / compptr->h_samp_factor),
355
(JDIMENSION) cinfo->max_v_samp_factor);
356
}
357
}
358
}
359
360