Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdsample.c
41149 views
1
/*
2
* reserved comment block
3
* DO NOT REMOVE OR ALTER!
4
*/
5
/*
6
* jdsample.c
7
*
8
* Copyright (C) 1991-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 upsampling routines.
13
*
14
* Upsampling input data is counted in "row groups". A row group
15
* is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
16
* sample rows of each component. Upsampling will normally produce
17
* max_v_samp_factor pixel rows from each row group (but this could vary
18
* if the upsampler is applying a scale factor of its own).
19
*
20
* An excellent reference for image resampling is
21
* Digital Image Warping, George Wolberg, 1990.
22
* Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
23
*/
24
25
#define JPEG_INTERNALS
26
#include "jinclude.h"
27
#include "jpeglib.h"
28
29
30
/* Pointer to routine to upsample a single component */
31
typedef JMETHOD(void, upsample1_ptr,
32
(j_decompress_ptr cinfo, jpeg_component_info * compptr,
33
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr));
34
35
/* Private subobject */
36
37
typedef struct {
38
struct jpeg_upsampler pub; /* public fields */
39
40
/* Color conversion buffer. When using separate upsampling and color
41
* conversion steps, this buffer holds one upsampled row group until it
42
* has been color converted and output.
43
* Note: we do not allocate any storage for component(s) which are full-size,
44
* ie do not need rescaling. The corresponding entry of color_buf[] is
45
* simply set to point to the input data array, thereby avoiding copying.
46
*/
47
JSAMPARRAY color_buf[MAX_COMPONENTS];
48
49
/* Per-component upsampling method pointers */
50
upsample1_ptr methods[MAX_COMPONENTS];
51
52
int next_row_out; /* counts rows emitted from color_buf */
53
JDIMENSION rows_to_go; /* counts rows remaining in image */
54
55
/* Height of an input row group for each component. */
56
int rowgroup_height[MAX_COMPONENTS];
57
58
/* These arrays save pixel expansion factors so that int_expand need not
59
* recompute them each time. They are unused for other upsampling methods.
60
*/
61
UINT8 h_expand[MAX_COMPONENTS];
62
UINT8 v_expand[MAX_COMPONENTS];
63
} my_upsampler;
64
65
typedef my_upsampler * my_upsample_ptr;
66
67
68
/*
69
* Initialize for an upsampling pass.
70
*/
71
72
METHODDEF(void)
73
start_pass_upsample (j_decompress_ptr cinfo)
74
{
75
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
76
77
/* Mark the conversion buffer empty */
78
upsample->next_row_out = cinfo->max_v_samp_factor;
79
/* Initialize total-height counter for detecting bottom of image */
80
upsample->rows_to_go = cinfo->output_height;
81
}
82
83
84
/*
85
* Control routine to do upsampling (and color conversion).
86
*
87
* In this version we upsample each component independently.
88
* We upsample one row group into the conversion buffer, then apply
89
* color conversion a row at a time.
90
*/
91
92
METHODDEF(void)
93
sep_upsample (j_decompress_ptr cinfo,
94
JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
95
JDIMENSION in_row_groups_avail,
96
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
97
JDIMENSION out_rows_avail)
98
{
99
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
100
int ci;
101
jpeg_component_info * compptr;
102
JDIMENSION num_rows;
103
104
/* Fill the conversion buffer, if it's empty */
105
if (upsample->next_row_out >= cinfo->max_v_samp_factor) {
106
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
107
ci++, compptr++) {
108
/* Invoke per-component upsample method. Notice we pass a POINTER
109
* to color_buf[ci], so that fullsize_upsample can change it.
110
*/
111
(*upsample->methods[ci]) (cinfo, compptr,
112
input_buf[ci] + (*in_row_group_ctr * upsample->rowgroup_height[ci]),
113
upsample->color_buf + ci);
114
}
115
upsample->next_row_out = 0;
116
}
117
118
/* Color-convert and emit rows */
119
120
/* How many we have in the buffer: */
121
num_rows = (JDIMENSION) (cinfo->max_v_samp_factor - upsample->next_row_out);
122
/* Not more than the distance to the end of the image. Need this test
123
* in case the image height is not a multiple of max_v_samp_factor:
124
*/
125
if (num_rows > upsample->rows_to_go)
126
num_rows = upsample->rows_to_go;
127
/* And not more than what the client can accept: */
128
out_rows_avail -= *out_row_ctr;
129
if (num_rows > out_rows_avail)
130
num_rows = out_rows_avail;
131
132
(*cinfo->cconvert->color_convert) (cinfo, upsample->color_buf,
133
(JDIMENSION) upsample->next_row_out,
134
output_buf + *out_row_ctr,
135
(int) num_rows);
136
137
/* Adjust counts */
138
*out_row_ctr += num_rows;
139
upsample->rows_to_go -= num_rows;
140
upsample->next_row_out += num_rows;
141
/* When the buffer is emptied, declare this input row group consumed */
142
if (upsample->next_row_out >= cinfo->max_v_samp_factor)
143
(*in_row_group_ctr)++;
144
}
145
146
147
/*
148
* These are the routines invoked by sep_upsample to upsample pixel values
149
* of a single component. One row group is processed per call.
150
*/
151
152
153
/*
154
* For full-size components, we just make color_buf[ci] point at the
155
* input buffer, and thus avoid copying any data. Note that this is
156
* safe only because sep_upsample doesn't declare the input row group
157
* "consumed" until we are done color converting and emitting it.
158
*/
159
160
METHODDEF(void)
161
fullsize_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
162
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
163
{
164
*output_data_ptr = input_data;
165
}
166
167
168
/*
169
* This is a no-op version used for "uninteresting" components.
170
* These components will not be referenced by color conversion.
171
*/
172
173
METHODDEF(void)
174
noop_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
175
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
176
{
177
*output_data_ptr = NULL; /* safety check */
178
}
179
180
181
/*
182
* This version handles any integral sampling ratios.
183
* This is not used for typical JPEG files, so it need not be fast.
184
* Nor, for that matter, is it particularly accurate: the algorithm is
185
* simple replication of the input pixel onto the corresponding output
186
* pixels. The hi-falutin sampling literature refers to this as a
187
* "box filter". A box filter tends to introduce visible artifacts,
188
* so if you are actually going to use 3:1 or 4:1 sampling ratios
189
* you would be well advised to improve this code.
190
*/
191
192
METHODDEF(void)
193
int_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
194
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
195
{
196
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
197
JSAMPARRAY output_data = *output_data_ptr;
198
register JSAMPROW inptr, outptr;
199
register JSAMPLE invalue;
200
register int h;
201
JSAMPROW outend;
202
int h_expand, v_expand;
203
int inrow, outrow;
204
205
h_expand = upsample->h_expand[compptr->component_index];
206
v_expand = upsample->v_expand[compptr->component_index];
207
208
inrow = outrow = 0;
209
while (outrow < cinfo->max_v_samp_factor) {
210
/* Generate one output row with proper horizontal expansion */
211
inptr = input_data[inrow];
212
outptr = output_data[outrow];
213
outend = outptr + cinfo->output_width;
214
while (outptr < outend) {
215
invalue = *inptr++; /* don't need GETJSAMPLE() here */
216
for (h = h_expand; h > 0; h--) {
217
*outptr++ = invalue;
218
}
219
}
220
/* Generate any additional output rows by duplicating the first one */
221
if (v_expand > 1) {
222
jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
223
v_expand-1, cinfo->output_width);
224
}
225
inrow++;
226
outrow += v_expand;
227
}
228
}
229
230
231
/*
232
* Fast processing for the common case of 2:1 horizontal and 1:1 vertical.
233
* It's still a box filter.
234
*/
235
236
METHODDEF(void)
237
h2v1_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
238
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
239
{
240
JSAMPARRAY output_data = *output_data_ptr;
241
register JSAMPROW inptr, outptr;
242
register JSAMPLE invalue;
243
JSAMPROW outend;
244
int inrow;
245
246
for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
247
inptr = input_data[inrow];
248
outptr = output_data[inrow];
249
outend = outptr + cinfo->output_width;
250
while (outptr < outend) {
251
invalue = *inptr++; /* don't need GETJSAMPLE() here */
252
*outptr++ = invalue;
253
*outptr++ = invalue;
254
}
255
}
256
}
257
258
259
/*
260
* Fast processing for the common case of 2:1 horizontal and 2:1 vertical.
261
* It's still a box filter.
262
*/
263
264
METHODDEF(void)
265
h2v2_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
266
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
267
{
268
JSAMPARRAY output_data = *output_data_ptr;
269
register JSAMPROW inptr, outptr;
270
register JSAMPLE invalue;
271
JSAMPROW outend;
272
int inrow, outrow;
273
274
inrow = outrow = 0;
275
while (outrow < cinfo->max_v_samp_factor) {
276
inptr = input_data[inrow];
277
outptr = output_data[outrow];
278
outend = outptr + cinfo->output_width;
279
while (outptr < outend) {
280
invalue = *inptr++; /* don't need GETJSAMPLE() here */
281
*outptr++ = invalue;
282
*outptr++ = invalue;
283
}
284
jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
285
1, cinfo->output_width);
286
inrow++;
287
outrow += 2;
288
}
289
}
290
291
292
/*
293
* Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.
294
*
295
* The upsampling algorithm is linear interpolation between pixel centers,
296
* also known as a "triangle filter". This is a good compromise between
297
* speed and visual quality. The centers of the output pixels are 1/4 and 3/4
298
* of the way between input pixel centers.
299
*
300
* A note about the "bias" calculations: when rounding fractional values to
301
* integer, we do not want to always round 0.5 up to the next integer.
302
* If we did that, we'd introduce a noticeable bias towards larger values.
303
* Instead, this code is arranged so that 0.5 will be rounded up or down at
304
* alternate pixel locations (a simple ordered dither pattern).
305
*/
306
307
METHODDEF(void)
308
h2v1_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
309
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
310
{
311
JSAMPARRAY output_data = *output_data_ptr;
312
register JSAMPROW inptr, outptr;
313
register int invalue;
314
register JDIMENSION colctr;
315
int inrow;
316
317
for (inrow = 0; inrow < cinfo->max_v_samp_factor; inrow++) {
318
inptr = input_data[inrow];
319
outptr = output_data[inrow];
320
/* Special case for first column */
321
invalue = GETJSAMPLE(*inptr++);
322
*outptr++ = (JSAMPLE) invalue;
323
*outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
324
325
for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
326
/* General case: 3/4 * nearer pixel + 1/4 * further pixel */
327
invalue = GETJSAMPLE(*inptr++) * 3;
328
*outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 1) >> 2);
329
*outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
330
}
331
332
/* Special case for last column */
333
invalue = GETJSAMPLE(*inptr);
334
*outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 1) >> 2);
335
*outptr++ = (JSAMPLE) invalue;
336
}
337
}
338
339
340
/*
341
* Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
342
* Again a triangle filter; see comments for h2v1 case, above.
343
*
344
* It is OK for us to reference the adjacent input rows because we demanded
345
* context from the main buffer controller (see initialization code).
346
*/
347
348
METHODDEF(void)
349
h2v2_fancy_upsample (j_decompress_ptr cinfo, jpeg_component_info * compptr,
350
JSAMPARRAY input_data, JSAMPARRAY * output_data_ptr)
351
{
352
JSAMPARRAY output_data = *output_data_ptr;
353
register JSAMPROW inptr0, inptr1, outptr;
354
#if BITS_IN_JSAMPLE == 8
355
register int thiscolsum, lastcolsum, nextcolsum;
356
#else
357
register INT32 thiscolsum, lastcolsum, nextcolsum;
358
#endif
359
register JDIMENSION colctr;
360
int inrow, outrow, v;
361
362
inrow = outrow = 0;
363
while (outrow < cinfo->max_v_samp_factor) {
364
for (v = 0; v < 2; v++) {
365
/* inptr0 points to nearest input row, inptr1 points to next nearest */
366
inptr0 = input_data[inrow];
367
if (v == 0) /* next nearest is row above */
368
inptr1 = input_data[inrow-1];
369
else /* next nearest is row below */
370
inptr1 = input_data[inrow+1];
371
outptr = output_data[outrow++];
372
373
/* Special case for first column */
374
thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
375
nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
376
*outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
377
*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
378
lastcolsum = thiscolsum; thiscolsum = nextcolsum;
379
380
for (colctr = compptr->downsampled_width - 2; colctr > 0; colctr--) {
381
/* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
382
/* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
383
nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
384
*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
385
*outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
386
lastcolsum = thiscolsum; thiscolsum = nextcolsum;
387
}
388
389
/* Special case for last column */
390
*outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
391
*outptr++ = (JSAMPLE) ((thiscolsum * 4 + 7) >> 4);
392
}
393
inrow++;
394
}
395
}
396
397
398
/*
399
* Module initialization routine for upsampling.
400
*/
401
402
GLOBAL(void)
403
jinit_upsampler (j_decompress_ptr cinfo)
404
{
405
my_upsample_ptr upsample;
406
int ci;
407
jpeg_component_info * compptr;
408
boolean need_buffer, do_fancy;
409
int h_in_group, v_in_group, h_out_group, v_out_group;
410
411
upsample = (my_upsample_ptr)
412
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
413
SIZEOF(my_upsampler));
414
cinfo->upsample = (struct jpeg_upsampler *) upsample;
415
upsample->pub.start_pass = start_pass_upsample;
416
upsample->pub.upsample = sep_upsample;
417
upsample->pub.need_context_rows = FALSE; /* until we find out differently */
418
419
if (cinfo->CCIR601_sampling) /* this isn't supported */
420
ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
421
422
/* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
423
* so don't ask for it.
424
*/
425
do_fancy = cinfo->do_fancy_upsampling && cinfo->min_DCT_scaled_size > 1;
426
427
/* Verify we can handle the sampling factors, select per-component methods,
428
* and create storage as needed.
429
*/
430
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
431
ci++, compptr++) {
432
/* Compute size of an "input group" after IDCT scaling. This many samples
433
* are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
434
*/
435
h_in_group = (compptr->h_samp_factor * compptr->DCT_scaled_size) /
436
cinfo->min_DCT_scaled_size;
437
v_in_group = (compptr->v_samp_factor * compptr->DCT_scaled_size) /
438
cinfo->min_DCT_scaled_size;
439
h_out_group = cinfo->max_h_samp_factor;
440
v_out_group = cinfo->max_v_samp_factor;
441
upsample->rowgroup_height[ci] = v_in_group; /* save for use later */
442
need_buffer = TRUE;
443
if (! compptr->component_needed) {
444
/* Don't bother to upsample an uninteresting component. */
445
upsample->methods[ci] = noop_upsample;
446
need_buffer = FALSE;
447
} else if (h_in_group == h_out_group && v_in_group == v_out_group) {
448
/* Fullsize components can be processed without any work. */
449
upsample->methods[ci] = fullsize_upsample;
450
need_buffer = FALSE;
451
} else if (h_in_group * 2 == h_out_group &&
452
v_in_group == v_out_group) {
453
/* Special cases for 2h1v upsampling */
454
if (do_fancy && compptr->downsampled_width > 2)
455
upsample->methods[ci] = h2v1_fancy_upsample;
456
else
457
upsample->methods[ci] = h2v1_upsample;
458
} else if (h_in_group * 2 == h_out_group &&
459
v_in_group * 2 == v_out_group) {
460
/* Special cases for 2h2v upsampling */
461
if (do_fancy && compptr->downsampled_width > 2) {
462
upsample->methods[ci] = h2v2_fancy_upsample;
463
upsample->pub.need_context_rows = TRUE;
464
} else
465
upsample->methods[ci] = h2v2_upsample;
466
} else if ((h_out_group % h_in_group) == 0 &&
467
(v_out_group % v_in_group) == 0) {
468
/* Generic integral-factors upsampling method */
469
upsample->methods[ci] = int_upsample;
470
upsample->h_expand[ci] = (UINT8) (h_out_group / h_in_group);
471
upsample->v_expand[ci] = (UINT8) (v_out_group / v_in_group);
472
} else
473
ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
474
if (need_buffer) {
475
upsample->color_buf[ci] = (*cinfo->mem->alloc_sarray)
476
((j_common_ptr) cinfo, JPOOL_IMAGE,
477
(JDIMENSION) jround_up((long) cinfo->output_width,
478
(long) cinfo->max_h_samp_factor),
479
(JDIMENSION) cinfo->max_v_samp_factor);
480
}
481
}
482
}
483
484