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