blob: c813080c0b092469ba6ca958aaecf2b1d597c64e [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdmerge.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1994-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCb4570bb2011-09-07 06:31:00 +00006 * Copyright (C) 2009, 2011, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains code for merged upsampling/color conversion.
11 *
12 * This file combines functions from jdsample.c and jdcolor.c;
13 * read those files first to understand what's going on.
14 *
15 * When the chroma components are to be upsampled by simple replication
16 * (ie, box filtering), we can save some work in color conversion by
17 * calculating all the output pixels corresponding to a pair of chroma
18 * samples at one time. In the conversion equations
19 * R = Y + K1 * Cr
20 * G = Y + K2 * Cb + K3 * Cr
21 * B = Y + K4 * Cb
22 * only the Y term varies among the group of pixels corresponding to a pair
23 * of chroma samples, so the rest of the terms can be calculated just once.
24 * At typical sampling ratios, this eliminates half or three-quarters of the
25 * multiplications needed for color conversion.
26 *
27 * This file currently provides implementations for the following cases:
28 * YCbCr => RGB color conversion only.
29 * Sampling ratios of 2h1v or 2h2v.
30 * No scaling needed at upsample time.
31 * Corner-aligned (non-CCIR601) sampling alignment.
32 * Other special cases could be added, but in most applications these are
33 * the only common cases. (For uncommon cases we fall back on the more
34 * general code in jdsample.c and jdcolor.c.)
35 */
36
37#define JPEG_INTERNALS
38#include "jinclude.h"
39#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000040#include "jsimd.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041
42#ifdef UPSAMPLE_MERGING_SUPPORTED
43
44
45/* Private subobject */
46
47typedef struct {
48 struct jpeg_upsampler pub; /* public fields */
49
50 /* Pointer to routine to do actual upsampling/conversion of one row group */
51 JMETHOD(void, upmethod, (j_decompress_ptr cinfo,
52 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
53 JSAMPARRAY output_buf));
54
55 /* Private state for YCC->RGB conversion */
56 int * Cr_r_tab; /* => table for Cr to R conversion */
57 int * Cb_b_tab; /* => table for Cb to B conversion */
58 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
59 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
60
61 /* For 2:1 vertical sampling, we produce two output rows at a time.
62 * We need a "spare" row buffer to hold the second output row if the
63 * application provides just a one-row buffer; we also use the spare
64 * to discard the dummy last row if the image height is odd.
65 */
66 JSAMPROW spare_row;
67 boolean spare_full; /* T if spare buffer is occupied */
68
69 JDIMENSION out_row_width; /* samples per output row */
70 JDIMENSION rows_to_go; /* counts rows remaining in image */
71} my_upsampler;
72
73typedef my_upsampler * my_upsample_ptr;
74
75#define SCALEBITS 16 /* speediest right-shift on some machines */
76#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
77#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
78
79
DRCb4570bb2011-09-07 06:31:00 +000080/* Include inline routines for colorspace extensions */
81
82#include "jdmrgext.c"
83#undef RGB_RED
84#undef RGB_GREEN
85#undef RGB_BLUE
86#undef RGB_PIXELSIZE
87
88#define RGB_RED EXT_RGB_RED
89#define RGB_GREEN EXT_RGB_GREEN
90#define RGB_BLUE EXT_RGB_BLUE
91#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
92#define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
93#define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
94#include "jdmrgext.c"
95#undef RGB_RED
96#undef RGB_GREEN
97#undef RGB_BLUE
98#undef RGB_PIXELSIZE
99#undef h2v1_merged_upsample_internal
100#undef h2v2_merged_upsample_internal
101
102#define RGB_RED EXT_RGBX_RED
103#define RGB_GREEN EXT_RGBX_GREEN
104#define RGB_BLUE EXT_RGBX_BLUE
105#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
106#define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
107#define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
108#include "jdmrgext.c"
109#undef RGB_RED
110#undef RGB_GREEN
111#undef RGB_BLUE
112#undef RGB_PIXELSIZE
113#undef h2v1_merged_upsample_internal
114#undef h2v2_merged_upsample_internal
115
116#define RGB_RED EXT_BGR_RED
117#define RGB_GREEN EXT_BGR_GREEN
118#define RGB_BLUE EXT_BGR_BLUE
119#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
120#define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
121#define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
122#include "jdmrgext.c"
123#undef RGB_RED
124#undef RGB_GREEN
125#undef RGB_BLUE
126#undef RGB_PIXELSIZE
127#undef h2v1_merged_upsample_internal
128#undef h2v2_merged_upsample_internal
129
130#define RGB_RED EXT_BGRX_RED
131#define RGB_GREEN EXT_BGRX_GREEN
132#define RGB_BLUE EXT_BGRX_BLUE
133#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
134#define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
135#define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
136#include "jdmrgext.c"
137#undef RGB_RED
138#undef RGB_GREEN
139#undef RGB_BLUE
140#undef RGB_PIXELSIZE
141#undef h2v1_merged_upsample_internal
142#undef h2v2_merged_upsample_internal
143
144#define RGB_RED EXT_XBGR_RED
145#define RGB_GREEN EXT_XBGR_GREEN
146#define RGB_BLUE EXT_XBGR_BLUE
147#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
148#define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
149#define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
150#include "jdmrgext.c"
151#undef RGB_RED
152#undef RGB_GREEN
153#undef RGB_BLUE
154#undef RGB_PIXELSIZE
155#undef h2v1_merged_upsample_internal
156#undef h2v2_merged_upsample_internal
157
158#define RGB_RED EXT_XRGB_RED
159#define RGB_GREEN EXT_XRGB_GREEN
160#define RGB_BLUE EXT_XRGB_BLUE
161#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
162#define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
163#define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
164#include "jdmrgext.c"
165#undef RGB_RED
166#undef RGB_GREEN
167#undef RGB_BLUE
168#undef RGB_PIXELSIZE
169#undef h2v1_merged_upsample_internal
170#undef h2v2_merged_upsample_internal
171
172
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000174 * Initialize tables for YCC->RGB colorspace conversion.
175 * This is taken directly from jdcolor.c; see that file for more info.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176 */
177
Thomas G. Lane489583f1996-02-07 00:00:00 +0000178LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000179build_ycc_rgb_table (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180{
181 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000182 int i;
183 INT32 x;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000184 SHIFT_TEMPS
185
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 upsample->Cr_r_tab = (int *)
187 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
188 (MAXJSAMPLE+1) * SIZEOF(int));
189 upsample->Cb_b_tab = (int *)
190 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
191 (MAXJSAMPLE+1) * SIZEOF(int));
192 upsample->Cr_g_tab = (INT32 *)
193 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
194 (MAXJSAMPLE+1) * SIZEOF(INT32));
195 upsample->Cb_g_tab = (INT32 *)
196 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
197 (MAXJSAMPLE+1) * SIZEOF(INT32));
198
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000199 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000201 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 /* Cr=>R value is nearest int to 1.40200 * x */
203 upsample->Cr_r_tab[i] = (int)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000204 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205 /* Cb=>B value is nearest int to 1.77200 * x */
206 upsample->Cb_b_tab[i] = (int)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000207 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 /* Cr=>G value is scaled-up -0.71414 * x */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000209 upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210 /* Cb=>G value is scaled-up -0.34414 * x */
211 /* We also add in ONE_HALF so that need not do it in inner loop */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000212 upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 }
214}
215
216
217/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000218 * Initialize for an upsampling pass.
219 */
220
Thomas G. Lane489583f1996-02-07 00:00:00 +0000221METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000222start_pass_merged_upsample (j_decompress_ptr cinfo)
223{
224 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
225
226 /* Mark the spare buffer empty */
227 upsample->spare_full = FALSE;
228 /* Initialize total-height counter for detecting bottom of image */
229 upsample->rows_to_go = cinfo->output_height;
230}
231
232
233/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000234 * Control routine to do upsampling (and color conversion).
235 *
236 * The control routine just handles the row buffering considerations.
237 */
238
Thomas G. Lane489583f1996-02-07 00:00:00 +0000239METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240merged_2v_upsample (j_decompress_ptr cinfo,
241 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
242 JDIMENSION in_row_groups_avail,
243 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
244 JDIMENSION out_rows_avail)
245/* 2:1 vertical sampling case: may need a spare row. */
246{
247 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
248 JSAMPROW work_ptrs[2];
249 JDIMENSION num_rows; /* number of rows returned to caller */
250
251 if (upsample->spare_full) {
252 /* If we have a spare row saved from a previous cycle, just return it. */
253 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
254 1, upsample->out_row_width);
255 num_rows = 1;
256 upsample->spare_full = FALSE;
257 } else {
258 /* Figure number of rows to return to caller. */
259 num_rows = 2;
260 /* Not more than the distance to the end of the image. */
261 if (num_rows > upsample->rows_to_go)
262 num_rows = upsample->rows_to_go;
263 /* And not more than what the client can accept: */
264 out_rows_avail -= *out_row_ctr;
265 if (num_rows > out_rows_avail)
266 num_rows = out_rows_avail;
267 /* Create output pointer array for upsampler. */
268 work_ptrs[0] = output_buf[*out_row_ctr];
269 if (num_rows > 1) {
270 work_ptrs[1] = output_buf[*out_row_ctr + 1];
271 } else {
272 work_ptrs[1] = upsample->spare_row;
273 upsample->spare_full = TRUE;
274 }
275 /* Now do the upsampling. */
276 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
277 }
278
279 /* Adjust counts */
280 *out_row_ctr += num_rows;
281 upsample->rows_to_go -= num_rows;
282 /* When the buffer is emptied, declare this input row group consumed */
283 if (! upsample->spare_full)
284 (*in_row_group_ctr)++;
285}
286
287
Thomas G. Lane489583f1996-02-07 00:00:00 +0000288METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289merged_1v_upsample (j_decompress_ptr cinfo,
290 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
291 JDIMENSION in_row_groups_avail,
292 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
293 JDIMENSION out_rows_avail)
294/* 1:1 vertical sampling case: much easier, never need a spare row. */
295{
296 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
297
298 /* Just do the upsampling. */
299 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
300 output_buf + *out_row_ctr);
301 /* Adjust counts */
302 (*out_row_ctr)++;
303 (*in_row_group_ctr)++;
304}
305
306
307/*
308 * These are the routines invoked by the control routines to do
309 * the actual upsampling/conversion. One row group is processed per call.
310 *
311 * Note: since we may be writing directly into application-supplied buffers,
312 * we have to be honest about the output width; we can't assume the buffer
313 * has been rounded up to an even width.
314 */
315
316
317/*
318 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
319 */
320
Thomas G. Lane489583f1996-02-07 00:00:00 +0000321METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322h2v1_merged_upsample (j_decompress_ptr cinfo,
323 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
324 JSAMPARRAY output_buf)
325{
DRCb4570bb2011-09-07 06:31:00 +0000326 switch (cinfo->out_color_space) {
327 case JCS_EXT_RGB:
328 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
329 output_buf);
330 break;
331 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000332 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000333 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
334 output_buf);
335 break;
336 case JCS_EXT_BGR:
337 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
338 output_buf);
339 break;
340 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000341 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000342 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
343 output_buf);
344 break;
345 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000346 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000347 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
348 output_buf);
349 break;
350 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000351 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000352 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
353 output_buf);
354 break;
355 default:
356 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
357 output_buf);
358 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 }
360}
361
362
363/*
364 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
365 */
366
Thomas G. Lane489583f1996-02-07 00:00:00 +0000367METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368h2v2_merged_upsample (j_decompress_ptr cinfo,
369 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
370 JSAMPARRAY output_buf)
371{
DRCb4570bb2011-09-07 06:31:00 +0000372 switch (cinfo->out_color_space) {
373 case JCS_EXT_RGB:
374 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
375 output_buf);
376 break;
377 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000378 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000379 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
380 output_buf);
381 break;
382 case JCS_EXT_BGR:
383 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
384 output_buf);
385 break;
386 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000387 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000388 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
389 output_buf);
390 break;
391 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000392 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000393 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
394 output_buf);
395 break;
396 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000397 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000398 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
399 output_buf);
400 break;
401 default:
402 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
403 output_buf);
404 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405 }
406}
407
408
409/*
410 * Module initialization routine for merged upsampling/color conversion.
411 *
412 * NB: this is called under the conditions determined by use_merged_upsample()
413 * in jdmaster.c. That routine MUST correspond to the actual capabilities
414 * of this module; no safety checks are made here.
415 */
416
Thomas G. Lane489583f1996-02-07 00:00:00 +0000417GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418jinit_merged_upsampler (j_decompress_ptr cinfo)
419{
420 my_upsample_ptr upsample;
421
422 upsample = (my_upsample_ptr)
423 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
424 SIZEOF(my_upsampler));
425 cinfo->upsample = (struct jpeg_upsampler *) upsample;
426 upsample->pub.start_pass = start_pass_merged_upsample;
427 upsample->pub.need_context_rows = FALSE;
428
429 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
430
431 if (cinfo->max_v_samp_factor == 2) {
432 upsample->pub.upsample = merged_2v_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000433 if (jsimd_can_h2v2_merged_upsample())
434 upsample->upmethod = jsimd_h2v2_merged_upsample;
435 else
436 upsample->upmethod = h2v2_merged_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000437 /* Allocate a spare row buffer */
438 upsample->spare_row = (JSAMPROW)
439 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
440 (size_t) (upsample->out_row_width * SIZEOF(JSAMPLE)));
441 } else {
442 upsample->pub.upsample = merged_1v_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000443 if (jsimd_can_h2v1_merged_upsample())
444 upsample->upmethod = jsimd_h2v1_merged_upsample;
445 else
446 upsample->upmethod = h2v1_merged_upsample;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000447 /* No spare row needed */
448 upsample->spare_row = NULL;
449 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000450
451 build_ycc_rgb_table(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000452}
453
454#endif /* UPSAMPLE_MERGING_SUPPORTED */