Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/jpeg/jdmerge.c
4389 views
1
/*
2
* jdmerge.c
3
*
4
* Copyright (C) 1994-1996, Thomas G. Lane.
5
* Modified 2013-2022 by Guido Vollbeding.
6
* This file is part of the Independent JPEG Group's software.
7
* For conditions of distribution and use, see the accompanying README file.
8
*
9
* This file contains code for merged upsampling/color conversion.
10
*
11
* This file combines functions from jdsample.c and jdcolor.c;
12
* read those files first to understand what's going on.
13
*
14
* When the chroma components are to be upsampled by simple replication
15
* (ie, box filtering), we can save some work in color conversion by
16
* calculating all the output pixels corresponding to a pair of chroma
17
* samples at one time. In the conversion equations
18
* R = Y + K1 * Cr
19
* G = Y + K2 * Cb + K3 * Cr
20
* B = Y + K4 * Cb
21
* only the Y term varies among the group of pixels corresponding to a pair
22
* of chroma samples, so the rest of the terms can be calculated just once.
23
* At typical sampling ratios, this eliminates half or three-quarters
24
* of the multiplications needed for color conversion.
25
*
26
* This file currently provides implementations for the following cases:
27
* YCC => RGB color conversion only (YCbCr or BG_YCC).
28
* Sampling ratios of 2h1v or 2h2v.
29
* No scaling needed at upsample time.
30
* Corner-aligned (non-CCIR601) sampling alignment.
31
* Other special cases could be added, but in most applications these
32
* are the only common cases. (For uncommon cases we fall back on
33
* the more general code in jdsample.c and jdcolor.c.)
34
*/
35
36
#define JPEG_INTERNALS
37
#include "jinclude.h"
38
#include "jpeglib.h"
39
40
#ifdef UPSAMPLE_MERGING_SUPPORTED
41
42
43
#if RANGE_BITS < 2
44
/* Deliberate syntax err */
45
Sorry, this code requires 2 or more range extension bits.
46
#endif
47
48
49
/* Private subobject */
50
51
typedef struct {
52
struct jpeg_upsampler pub; /* public fields */
53
54
/* Pointer to routine to do actual upsampling/conversion of one row group */
55
JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
56
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
57
JSAMPARRAY output_buf));
58
59
/* Private state for YCC->RGB conversion */
60
int * Cr_r_tab; /* => table for Cr to R conversion */
61
int * Cb_b_tab; /* => table for Cb to B conversion */
62
INT32 * Cr_g_tab; /* => table for Cr to G conversion */
63
INT32 * Cb_g_tab; /* => table for Cb to G conversion */
64
65
/* For 2:1 vertical sampling, we produce two output rows at a time.
66
* We need a "spare" row buffer to hold the second output row if the
67
* application provides just a one-row buffer; we also use the spare
68
* to discard the dummy last row if the image height is odd.
69
*/
70
JSAMPROW spare_row;
71
boolean spare_full; /* T if spare buffer is occupied */
72
73
JDIMENSION out_row_width; /* samples per output row */
74
JDIMENSION rows_to_go; /* counts rows remaining in image */
75
} my_upsampler;
76
77
typedef my_upsampler * my_upsample_ptr;
78
79
#define SCALEBITS 16 /* speediest right-shift on some machines */
80
#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
81
#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
82
83
84
/*
85
* Initialize tables for YCbCr->RGB and BG_YCC->RGB colorspace conversion.
86
* This is taken directly from jdcolor.c; see that file for more info.
87
*/
88
89
LOCAL(void)
90
build_ycc_rgb_table (j_decompress_ptr cinfo)
91
/* Normal case, sYCC */
92
{
93
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
94
int i;
95
INT32 x;
96
SHIFT_TEMPS
97
98
upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
99
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
100
upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
101
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
102
upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
103
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
104
upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
105
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
106
107
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
108
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
109
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
110
/* Cr=>R value is nearest int to 1.402 * x */
111
upsample->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
112
/* Cb=>B value is nearest int to 1.772 * x */
113
upsample->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
114
/* Cr=>G value is scaled-up -0.714136286 * x */
115
upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
116
/* Cb=>G value is scaled-up -0.344136286 * x */
117
/* We also add in ONE_HALF so that need not do it in inner loop */
118
upsample->Cb_g_tab[i] = (- FIX(0.344136286)) * x + ONE_HALF;
119
}
120
}
121
122
123
LOCAL(void)
124
build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
125
/* Wide gamut case, bg-sYCC */
126
{
127
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
128
int i;
129
INT32 x;
130
SHIFT_TEMPS
131
132
upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
133
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
134
upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
135
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
136
upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
137
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
138
upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
139
((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
140
141
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
142
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
143
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
144
/* Cr=>R value is nearest int to 2.804 * x */
145
upsample->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
146
/* Cb=>B value is nearest int to 3.544 * x */
147
upsample->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
148
/* Cr=>G value is scaled-up -1.428272572 * x */
149
upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
150
/* Cb=>G value is scaled-up -0.688272572 * x */
151
/* We also add in ONE_HALF so that need not do it in inner loop */
152
upsample->Cb_g_tab[i] = (- FIX(0.688272572)) * x + ONE_HALF;
153
}
154
}
155
156
157
/*
158
* Initialize for an upsampling pass.
159
*/
160
161
METHODDEF(void)
162
start_pass_merged_upsample (j_decompress_ptr cinfo)
163
{
164
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
165
166
/* Mark the spare buffer empty */
167
upsample->spare_full = FALSE;
168
/* Initialize total-height counter for detecting bottom of image */
169
upsample->rows_to_go = cinfo->output_height;
170
}
171
172
173
/*
174
* Control routine to do upsampling (and color conversion).
175
*
176
* The control routine just handles the row buffering considerations.
177
*/
178
179
METHODDEF(void)
180
merged_2v_upsample (j_decompress_ptr cinfo,
181
JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
182
JDIMENSION in_row_groups_avail,
183
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
184
JDIMENSION out_rows_avail)
185
/* 2:1 vertical sampling case: may need a spare row. */
186
{
187
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
188
JSAMPROW work_ptrs[2];
189
JDIMENSION num_rows; /* number of rows returned to caller */
190
191
if (upsample->spare_full) {
192
/* If we have a spare row saved from a previous cycle, just return it. */
193
jcopy_sample_rows(& upsample->spare_row, output_buf + *out_row_ctr,
194
1, upsample->out_row_width);
195
num_rows = 1;
196
upsample->spare_full = FALSE;
197
} else {
198
/* Figure number of rows to return to caller. */
199
num_rows = 2;
200
/* Not more than the distance to the end of the image. */
201
if (num_rows > upsample->rows_to_go)
202
num_rows = upsample->rows_to_go;
203
/* And not more than what the client can accept: */
204
out_rows_avail -= *out_row_ctr;
205
if (num_rows > out_rows_avail)
206
num_rows = out_rows_avail;
207
/* Create output pointer array for upsampler. */
208
work_ptrs[0] = output_buf[*out_row_ctr];
209
if (num_rows > 1) {
210
work_ptrs[1] = output_buf[*out_row_ctr + 1];
211
} else {
212
work_ptrs[1] = upsample->spare_row;
213
upsample->spare_full = TRUE;
214
}
215
/* Now do the upsampling. */
216
(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
217
}
218
219
/* Adjust counts */
220
*out_row_ctr += num_rows;
221
upsample->rows_to_go -= num_rows;
222
/* When the buffer is emptied, declare this input row group consumed */
223
if (! upsample->spare_full)
224
(*in_row_group_ctr)++;
225
}
226
227
228
METHODDEF(void)
229
merged_1v_upsample (j_decompress_ptr cinfo,
230
JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
231
JDIMENSION in_row_groups_avail,
232
JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
233
JDIMENSION out_rows_avail)
234
/* 1:1 vertical sampling case: much easier, never need a spare row. */
235
{
236
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
237
238
/* Just do the upsampling. */
239
(*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
240
output_buf + *out_row_ctr);
241
/* Adjust counts */
242
(*out_row_ctr)++;
243
(*in_row_group_ctr)++;
244
}
245
246
247
/*
248
* These are the routines invoked by the control routines to do
249
* the actual upsampling/conversion. One row group is processed per call.
250
*
251
* Note: since we may be writing directly into application-supplied buffers,
252
* we have to be honest about the output width; we can't assume the buffer
253
* has been rounded up to an even width.
254
*/
255
256
257
/*
258
* Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
259
*/
260
261
METHODDEF(void)
262
h2v1_merged_upsample (j_decompress_ptr cinfo,
263
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
264
JSAMPARRAY output_buf)
265
{
266
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
267
register int y, cred, cgreen, cblue;
268
int cb, cr;
269
register JSAMPROW outptr;
270
JSAMPROW inptr0, inptr1, inptr2;
271
JDIMENSION col;
272
/* copy these pointers into registers if possible */
273
register JSAMPLE * range_limit = cinfo->sample_range_limit;
274
int * Crrtab = upsample->Cr_r_tab;
275
int * Cbbtab = upsample->Cb_b_tab;
276
INT32 * Crgtab = upsample->Cr_g_tab;
277
INT32 * Cbgtab = upsample->Cb_g_tab;
278
SHIFT_TEMPS
279
280
inptr0 = input_buf[0][in_row_group_ctr];
281
inptr1 = input_buf[1][in_row_group_ctr];
282
inptr2 = input_buf[2][in_row_group_ctr];
283
outptr = output_buf[0];
284
/* Loop for each pair of output pixels */
285
for (col = cinfo->output_width >> 1; col > 0; col--) {
286
/* Do the chroma part of the calculation */
287
cb = GETJSAMPLE(*inptr1++);
288
cr = GETJSAMPLE(*inptr2++);
289
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
290
cblue = Cbbtab[cb];
291
cred = Crrtab[cr];
292
/* Fetch 2 Y values and emit 2 pixels */
293
y = GETJSAMPLE(*inptr0++);
294
outptr[RGB_RED] = range_limit[y + cred];
295
outptr[RGB_GREEN] = range_limit[y + cgreen];
296
outptr[RGB_BLUE] = range_limit[y + cblue];
297
outptr += RGB_PIXELSIZE;
298
y = GETJSAMPLE(*inptr0++);
299
outptr[RGB_RED] = range_limit[y + cred];
300
outptr[RGB_GREEN] = range_limit[y + cgreen];
301
outptr[RGB_BLUE] = range_limit[y + cblue];
302
outptr += RGB_PIXELSIZE;
303
}
304
/* If image width is odd, do the last output column separately */
305
if (cinfo->output_width & 1) {
306
y = GETJSAMPLE(*inptr0);
307
cb = GETJSAMPLE(*inptr1);
308
cr = GETJSAMPLE(*inptr2);
309
outptr[RGB_RED] = range_limit[y + Crrtab[cr]];
310
outptr[RGB_GREEN] = range_limit[y +
311
((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
312
SCALEBITS))];
313
outptr[RGB_BLUE] = range_limit[y + Cbbtab[cb]];
314
}
315
}
316
317
318
/*
319
* Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
320
*/
321
322
METHODDEF(void)
323
h2v2_merged_upsample (j_decompress_ptr cinfo,
324
JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
325
JSAMPARRAY output_buf)
326
{
327
my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
328
register int y, cred, cgreen, cblue;
329
int cb, cr;
330
register JSAMPROW outptr0, outptr1;
331
JSAMPROW inptr00, inptr01, inptr1, inptr2;
332
JDIMENSION col;
333
/* copy these pointers into registers if possible */
334
register JSAMPLE * range_limit = cinfo->sample_range_limit;
335
int * Crrtab = upsample->Cr_r_tab;
336
int * Cbbtab = upsample->Cb_b_tab;
337
INT32 * Crgtab = upsample->Cr_g_tab;
338
INT32 * Cbgtab = upsample->Cb_g_tab;
339
SHIFT_TEMPS
340
341
inptr00 = input_buf[0][in_row_group_ctr*2];
342
inptr01 = input_buf[0][in_row_group_ctr*2 + 1];
343
inptr1 = input_buf[1][in_row_group_ctr];
344
inptr2 = input_buf[2][in_row_group_ctr];
345
outptr0 = output_buf[0];
346
outptr1 = output_buf[1];
347
/* Loop for each group of output pixels */
348
for (col = cinfo->output_width >> 1; col > 0; col--) {
349
/* Do the chroma part of the calculation */
350
cb = GETJSAMPLE(*inptr1++);
351
cr = GETJSAMPLE(*inptr2++);
352
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
353
cblue = Cbbtab[cb];
354
cred = Crrtab[cr];
355
/* Fetch 4 Y values and emit 4 pixels */
356
y = GETJSAMPLE(*inptr00++);
357
outptr0[RGB_RED] = range_limit[y + cred];
358
outptr0[RGB_GREEN] = range_limit[y + cgreen];
359
outptr0[RGB_BLUE] = range_limit[y + cblue];
360
outptr0 += RGB_PIXELSIZE;
361
y = GETJSAMPLE(*inptr00++);
362
outptr0[RGB_RED] = range_limit[y + cred];
363
outptr0[RGB_GREEN] = range_limit[y + cgreen];
364
outptr0[RGB_BLUE] = range_limit[y + cblue];
365
outptr0 += RGB_PIXELSIZE;
366
y = GETJSAMPLE(*inptr01++);
367
outptr1[RGB_RED] = range_limit[y + cred];
368
outptr1[RGB_GREEN] = range_limit[y + cgreen];
369
outptr1[RGB_BLUE] = range_limit[y + cblue];
370
outptr1 += RGB_PIXELSIZE;
371
y = GETJSAMPLE(*inptr01++);
372
outptr1[RGB_RED] = range_limit[y + cred];
373
outptr1[RGB_GREEN] = range_limit[y + cgreen];
374
outptr1[RGB_BLUE] = range_limit[y + cblue];
375
outptr1 += RGB_PIXELSIZE;
376
}
377
/* If image width is odd, do the last output column separately */
378
if (cinfo->output_width & 1) {
379
cb = GETJSAMPLE(*inptr1);
380
cr = GETJSAMPLE(*inptr2);
381
cgreen = (int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr], SCALEBITS);
382
cblue = Cbbtab[cb];
383
cred = Crrtab[cr];
384
y = GETJSAMPLE(*inptr00);
385
outptr0[RGB_RED] = range_limit[y + cred];
386
outptr0[RGB_GREEN] = range_limit[y + cgreen];
387
outptr0[RGB_BLUE] = range_limit[y + cblue];
388
y = GETJSAMPLE(*inptr01);
389
outptr1[RGB_RED] = range_limit[y + cred];
390
outptr1[RGB_GREEN] = range_limit[y + cgreen];
391
outptr1[RGB_BLUE] = range_limit[y + cblue];
392
}
393
}
394
395
396
/*
397
* Module initialization routine for merged upsampling/color conversion.
398
*
399
* NB: this is called under the conditions determined by use_merged_upsample()
400
* in jdmaster.c. That routine MUST correspond to the actual capabilities
401
* of this module; no safety checks are made here.
402
*/
403
404
GLOBAL(void)
405
jinit_merged_upsampler (j_decompress_ptr cinfo)
406
{
407
my_upsample_ptr upsample;
408
409
upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
410
((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
411
cinfo->upsample = &upsample->pub;
412
upsample->pub.start_pass = start_pass_merged_upsample;
413
upsample->pub.need_context_rows = FALSE;
414
415
upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
416
417
if (cinfo->max_v_samp_factor == 2) {
418
upsample->pub.upsample = merged_2v_upsample;
419
upsample->upmethod = h2v2_merged_upsample;
420
/* Allocate a spare row buffer */
421
upsample->spare_row = (JSAMPROW) (*cinfo->mem->alloc_large)
422
((j_common_ptr) cinfo, JPOOL_IMAGE,
423
(size_t) upsample->out_row_width * SIZEOF(JSAMPLE));
424
} else {
425
upsample->pub.upsample = merged_1v_upsample;
426
upsample->upmethod = h2v1_merged_upsample;
427
/* No spare row needed */
428
upsample->spare_row = NULL;
429
}
430
431
if (cinfo->jpeg_color_space == JCS_BG_YCC)
432
build_bg_ycc_rgb_table(cinfo);
433
else
434
build_ycc_rgb_table(cinfo);
435
}
436
437
#endif /* UPSAMPLE_MERGING_SUPPORTED */
438
439