blob: e13adb9f2122dfc0eeccc190d7f7e5a3100f39ff [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdmerge.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00006 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRCa6ef2822013-09-28 03:23:49 +00007 * libjpeg-turbo Modifications:
DRC72a3cc02014-08-30 20:37:50 +00008 * Copyright (C) 2009, 2011, 2014 D. R. Commander.
DRC78df2e62014-05-12 09:23:57 +00009 * Copyright (C) 2013, Linaro Limited.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 * For conditions of distribution and use, see the accompanying README file.
11 *
12 * This file contains code for merged upsampling/color conversion.
13 *
14 * This file combines functions from jdsample.c and jdcolor.c;
15 * read those files first to understand what's going on.
16 *
17 * When the chroma components are to be upsampled by simple replication
18 * (ie, box filtering), we can save some work in color conversion by
19 * calculating all the output pixels corresponding to a pair of chroma
20 * samples at one time. In the conversion equations
DRCe5eaf372014-05-09 18:00:32 +000021 * R = Y + K1 * Cr
22 * G = Y + K2 * Cb + K3 * Cr
23 * B = Y + K4 * Cb
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024 * only the Y term varies among the group of pixels corresponding to a pair
25 * of chroma samples, so the rest of the terms can be calculated just once.
26 * At typical sampling ratios, this eliminates half or three-quarters of the
27 * multiplications needed for color conversion.
28 *
29 * This file currently provides implementations for the following cases:
DRCe5eaf372014-05-09 18:00:32 +000030 * YCbCr => RGB color conversion only.
31 * Sampling ratios of 2h1v or 2h2v.
32 * No scaling needed at upsample time.
33 * Corner-aligned (non-CCIR601) sampling alignment.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034 * Other special cases could be added, but in most applications these are
35 * the only common cases. (For uncommon cases we fall back on the more
36 * general code in jdsample.c and jdcolor.c.)
37 */
38
39#define JPEG_INTERNALS
40#include "jinclude.h"
41#include "jpeglib.h"
Pierre Ossman59a39382009-03-09 13:15:56 +000042#include "jsimd.h"
DRCff6961f2014-04-20 09:17:11 +000043#include "jconfigint.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000044
45#ifdef UPSAMPLE_MERGING_SUPPORTED
46
47
48/* Private subobject */
49
50typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000051 struct jpeg_upsampler pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052
53 /* Pointer to routine to do actual upsampling/conversion of one row group */
DRCbc56b752014-05-16 10:43:44 +000054 void (*upmethod) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
55 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056
57 /* Private state for YCC->RGB conversion */
DRCe5eaf372014-05-09 18:00:32 +000058 int * Cr_r_tab; /* => table for Cr to R conversion */
59 int * Cb_b_tab; /* => table for Cb to B conversion */
60 INT32 * Cr_g_tab; /* => table for Cr to G conversion */
61 INT32 * Cb_g_tab; /* => table for Cb to G conversion */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062
63 /* For 2:1 vertical sampling, we produce two output rows at a time.
64 * We need a "spare" row buffer to hold the second output row if the
65 * application provides just a one-row buffer; we also use the spare
66 * to discard the dummy last row if the image height is odd.
67 */
68 JSAMPROW spare_row;
DRCe5eaf372014-05-09 18:00:32 +000069 boolean spare_full; /* T if spare buffer is occupied */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070
DRCe5eaf372014-05-09 18:00:32 +000071 JDIMENSION out_row_width; /* samples per output row */
72 JDIMENSION rows_to_go; /* counts rows remaining in image */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073} my_upsampler;
74
75typedef my_upsampler * my_upsample_ptr;
76
DRCe5eaf372014-05-09 18:00:32 +000077#define SCALEBITS 16 /* speediest right-shift on some machines */
78#define ONE_HALF ((INT32) 1 << (SCALEBITS-1))
79#define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080
81
DRCb4570bb2011-09-07 06:31:00 +000082/* Include inline routines for colorspace extensions */
83
84#include "jdmrgext.c"
85#undef RGB_RED
86#undef RGB_GREEN
87#undef RGB_BLUE
88#undef RGB_PIXELSIZE
89
90#define RGB_RED EXT_RGB_RED
91#define RGB_GREEN EXT_RGB_GREEN
92#define RGB_BLUE EXT_RGB_BLUE
93#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
94#define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
95#define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
96#include "jdmrgext.c"
97#undef RGB_RED
98#undef RGB_GREEN
99#undef RGB_BLUE
100#undef RGB_PIXELSIZE
101#undef h2v1_merged_upsample_internal
102#undef h2v2_merged_upsample_internal
103
104#define RGB_RED EXT_RGBX_RED
105#define RGB_GREEN EXT_RGBX_GREEN
106#define RGB_BLUE EXT_RGBX_BLUE
DRCcac10512012-03-16 14:37:36 +0000107#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000108#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
109#define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
110#define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
111#include "jdmrgext.c"
112#undef RGB_RED
113#undef RGB_GREEN
114#undef RGB_BLUE
DRCcac10512012-03-16 14:37:36 +0000115#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000116#undef RGB_PIXELSIZE
117#undef h2v1_merged_upsample_internal
118#undef h2v2_merged_upsample_internal
119
120#define RGB_RED EXT_BGR_RED
121#define RGB_GREEN EXT_BGR_GREEN
122#define RGB_BLUE EXT_BGR_BLUE
123#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
124#define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
125#define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
126#include "jdmrgext.c"
127#undef RGB_RED
128#undef RGB_GREEN
129#undef RGB_BLUE
130#undef RGB_PIXELSIZE
131#undef h2v1_merged_upsample_internal
132#undef h2v2_merged_upsample_internal
133
134#define RGB_RED EXT_BGRX_RED
135#define RGB_GREEN EXT_BGRX_GREEN
136#define RGB_BLUE EXT_BGRX_BLUE
DRCcac10512012-03-16 14:37:36 +0000137#define RGB_ALPHA 3
DRCb4570bb2011-09-07 06:31:00 +0000138#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
139#define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
140#define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
141#include "jdmrgext.c"
142#undef RGB_RED
143#undef RGB_GREEN
144#undef RGB_BLUE
DRCcac10512012-03-16 14:37:36 +0000145#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000146#undef RGB_PIXELSIZE
147#undef h2v1_merged_upsample_internal
148#undef h2v2_merged_upsample_internal
149
150#define RGB_RED EXT_XBGR_RED
151#define RGB_GREEN EXT_XBGR_GREEN
152#define RGB_BLUE EXT_XBGR_BLUE
DRCcac10512012-03-16 14:37:36 +0000153#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000154#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
155#define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
156#define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
157#include "jdmrgext.c"
158#undef RGB_RED
159#undef RGB_GREEN
160#undef RGB_BLUE
DRCcac10512012-03-16 14:37:36 +0000161#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000162#undef RGB_PIXELSIZE
163#undef h2v1_merged_upsample_internal
164#undef h2v2_merged_upsample_internal
165
166#define RGB_RED EXT_XRGB_RED
167#define RGB_GREEN EXT_XRGB_GREEN
168#define RGB_BLUE EXT_XRGB_BLUE
DRCcac10512012-03-16 14:37:36 +0000169#define RGB_ALPHA 0
DRCb4570bb2011-09-07 06:31:00 +0000170#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
171#define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
172#define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
173#include "jdmrgext.c"
174#undef RGB_RED
175#undef RGB_GREEN
176#undef RGB_BLUE
DRCcac10512012-03-16 14:37:36 +0000177#undef RGB_ALPHA
DRCb4570bb2011-09-07 06:31:00 +0000178#undef RGB_PIXELSIZE
179#undef h2v1_merged_upsample_internal
180#undef h2v2_merged_upsample_internal
181
182
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000183/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000184 * Initialize tables for YCC->RGB colorspace conversion.
185 * This is taken directly from jdcolor.c; see that file for more info.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 */
187
Thomas G. Lane489583f1996-02-07 00:00:00 +0000188LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000189build_ycc_rgb_table (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190{
191 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000192 int i;
193 INT32 x;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 SHIFT_TEMPS
195
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196 upsample->Cr_r_tab = (int *)
197 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000198 (MAXJSAMPLE+1) * sizeof(int));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000199 upsample->Cb_b_tab = (int *)
200 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000201 (MAXJSAMPLE+1) * sizeof(int));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 upsample->Cr_g_tab = (INT32 *)
203 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000204 (MAXJSAMPLE+1) * sizeof(INT32));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205 upsample->Cb_g_tab = (INT32 *)
206 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000207 (MAXJSAMPLE+1) * sizeof(INT32));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000209 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000211 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000212 /* Cr=>R value is nearest int to 1.40200 * x */
213 upsample->Cr_r_tab[i] = (int)
DRCe5eaf372014-05-09 18:00:32 +0000214 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 /* Cb=>B value is nearest int to 1.77200 * x */
216 upsample->Cb_b_tab[i] = (int)
DRCe5eaf372014-05-09 18:00:32 +0000217 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 /* Cr=>G value is scaled-up -0.71414 * x */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000219 upsample->Cr_g_tab[i] = (- FIX(0.71414)) * x;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 /* Cb=>G value is scaled-up -0.34414 * x */
221 /* We also add in ONE_HALF so that need not do it in inner loop */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000222 upsample->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 }
224}
225
226
227/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000228 * Initialize for an upsampling pass.
229 */
230
Thomas G. Lane489583f1996-02-07 00:00:00 +0000231METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000232start_pass_merged_upsample (j_decompress_ptr cinfo)
233{
234 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
235
236 /* Mark the spare buffer empty */
237 upsample->spare_full = FALSE;
238 /* Initialize total-height counter for detecting bottom of image */
239 upsample->rows_to_go = cinfo->output_height;
240}
241
242
243/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244 * Control routine to do upsampling (and color conversion).
245 *
246 * The control routine just handles the row buffering considerations.
247 */
248
Thomas G. Lane489583f1996-02-07 00:00:00 +0000249METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250merged_2v_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000251 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
252 JDIMENSION in_row_groups_avail,
253 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
254 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255/* 2:1 vertical sampling case: may need a spare row. */
256{
257 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
258 JSAMPROW work_ptrs[2];
DRCe5eaf372014-05-09 18:00:32 +0000259 JDIMENSION num_rows; /* number of rows returned to caller */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000260
261 if (upsample->spare_full) {
262 /* If we have a spare row saved from a previous cycle, just return it. */
DRC78df2e62014-05-12 09:23:57 +0000263 JDIMENSION size = upsample->out_row_width;
264 if (cinfo->out_color_space == JCS_RGB565)
265 size = cinfo->output_width * 2;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000266 jcopy_sample_rows(& upsample->spare_row, 0, output_buf + *out_row_ctr, 0,
DRC78df2e62014-05-12 09:23:57 +0000267 1, size);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000268 num_rows = 1;
269 upsample->spare_full = FALSE;
270 } else {
271 /* Figure number of rows to return to caller. */
272 num_rows = 2;
273 /* Not more than the distance to the end of the image. */
274 if (num_rows > upsample->rows_to_go)
275 num_rows = upsample->rows_to_go;
276 /* And not more than what the client can accept: */
277 out_rows_avail -= *out_row_ctr;
278 if (num_rows > out_rows_avail)
279 num_rows = out_rows_avail;
280 /* Create output pointer array for upsampler. */
281 work_ptrs[0] = output_buf[*out_row_ctr];
282 if (num_rows > 1) {
283 work_ptrs[1] = output_buf[*out_row_ctr + 1];
284 } else {
285 work_ptrs[1] = upsample->spare_row;
286 upsample->spare_full = TRUE;
287 }
288 /* Now do the upsampling. */
289 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr, work_ptrs);
290 }
291
292 /* Adjust counts */
293 *out_row_ctr += num_rows;
294 upsample->rows_to_go -= num_rows;
295 /* When the buffer is emptied, declare this input row group consumed */
296 if (! upsample->spare_full)
297 (*in_row_group_ctr)++;
298}
299
300
Thomas G. Lane489583f1996-02-07 00:00:00 +0000301METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000302merged_1v_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000303 JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr,
304 JDIMENSION in_row_groups_avail,
305 JSAMPARRAY output_buf, JDIMENSION *out_row_ctr,
306 JDIMENSION out_rows_avail)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000307/* 1:1 vertical sampling case: much easier, never need a spare row. */
308{
309 my_upsample_ptr upsample = (my_upsample_ptr) cinfo->upsample;
310
311 /* Just do the upsampling. */
312 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
DRCe5eaf372014-05-09 18:00:32 +0000313 output_buf + *out_row_ctr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 /* Adjust counts */
315 (*out_row_ctr)++;
316 (*in_row_group_ctr)++;
317}
318
319
320/*
321 * These are the routines invoked by the control routines to do
322 * the actual upsampling/conversion. One row group is processed per call.
323 *
324 * Note: since we may be writing directly into application-supplied buffers,
325 * we have to be honest about the output width; we can't assume the buffer
326 * has been rounded up to an even width.
327 */
328
329
330/*
331 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
332 */
333
Thomas G. Lane489583f1996-02-07 00:00:00 +0000334METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000335h2v1_merged_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000336 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
337 JSAMPARRAY output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000338{
DRCb4570bb2011-09-07 06:31:00 +0000339 switch (cinfo->out_color_space) {
340 case JCS_EXT_RGB:
341 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
342 output_buf);
343 break;
344 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000345 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000346 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
347 output_buf);
348 break;
349 case JCS_EXT_BGR:
350 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
351 output_buf);
352 break;
353 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000354 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000355 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
356 output_buf);
357 break;
358 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000359 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000360 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
361 output_buf);
362 break;
363 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000364 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000365 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
366 output_buf);
367 break;
368 default:
369 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
370 output_buf);
371 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000372 }
373}
374
375
376/*
377 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
378 */
379
Thomas G. Lane489583f1996-02-07 00:00:00 +0000380METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381h2v2_merged_upsample (j_decompress_ptr cinfo,
DRCe5eaf372014-05-09 18:00:32 +0000382 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
383 JSAMPARRAY output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384{
DRCb4570bb2011-09-07 06:31:00 +0000385 switch (cinfo->out_color_space) {
386 case JCS_EXT_RGB:
387 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
388 output_buf);
389 break;
390 case JCS_EXT_RGBX:
DRC67ce3b22011-12-19 02:21:03 +0000391 case JCS_EXT_RGBA:
DRCb4570bb2011-09-07 06:31:00 +0000392 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
393 output_buf);
394 break;
395 case JCS_EXT_BGR:
396 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
397 output_buf);
398 break;
399 case JCS_EXT_BGRX:
DRC67ce3b22011-12-19 02:21:03 +0000400 case JCS_EXT_BGRA:
DRCb4570bb2011-09-07 06:31:00 +0000401 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
402 output_buf);
403 break;
404 case JCS_EXT_XBGR:
DRC67ce3b22011-12-19 02:21:03 +0000405 case JCS_EXT_ABGR:
DRCb4570bb2011-09-07 06:31:00 +0000406 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
407 output_buf);
408 break;
409 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000410 case JCS_EXT_ARGB:
DRCb4570bb2011-09-07 06:31:00 +0000411 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
412 output_buf);
413 break;
414 default:
415 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
416 output_buf);
417 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418 }
419}
420
421
DRC72a3cc02014-08-30 20:37:50 +0000422/*
423 * RGB565 conversion
424 */
425
426#define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
427 (((g) << 3) & 0x7E0) | ((b) >> 3))
428#define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
429 (((g) << 11) & 0xE000) | \
430 (((b) << 5) & 0x1F00))
431
432#define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
433#define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
434
435#define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
436
437#define WRITE_TWO_PIXELS_LE(addr, pixels) { \
438 ((INT16*)(addr))[0] = (pixels); \
439 ((INT16*)(addr))[1] = (pixels) >> 16; \
440}
441#define WRITE_TWO_PIXELS_BE(addr, pixels) { \
442 ((INT16*)(addr))[1] = (pixels); \
443 ((INT16*)(addr))[0] = (pixels) >> 16; \
444}
445
DRC72a3cc02014-08-30 20:37:50 +0000446#define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
447#define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
448#define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
449
450
451/* Declarations for ordered dithering
452 *
453 * We use a 4x4 ordered dither array packed into 32 bits. This array is
454 * sufficent for dithering RGB888 to RGB565.
455 */
456
457#define DITHER_MASK 0x3
458#define DITHER_ROTATE(x) (((x) << 24) | (((x) >> 8) & 0x00FFFFFF))
459static const INT32 dither_matrix[4] = {
460 0x0008020A,
461 0x0C040E06,
462 0x030B0109,
463 0x0F070D05
464};
465
466
467/* Include inline routines for RGB565 conversion */
468
469#define PACK_SHORT_565 PACK_SHORT_565_LE
470#define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
471#define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
472#define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
473#define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
474#define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
475#define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
476#include "jdmrg565.c"
477#undef PACK_SHORT_565
478#undef PACK_TWO_PIXELS
479#undef WRITE_TWO_PIXELS
480#undef h2v1_merged_upsample_565_internal
481#undef h2v1_merged_upsample_565D_internal
482#undef h2v2_merged_upsample_565_internal
483#undef h2v2_merged_upsample_565D_internal
484
485#define PACK_SHORT_565 PACK_SHORT_565_BE
486#define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
487#define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
488#define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
489#define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
490#define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
491#define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
492#include "jdmrg565.c"
493#undef PACK_SHORT_565
494#undef PACK_TWO_PIXELS
495#undef WRITE_TWO_PIXELS
496#undef h2v1_merged_upsample_565_internal
497#undef h2v1_merged_upsample_565D_internal
498#undef h2v2_merged_upsample_565_internal
499#undef h2v2_merged_upsample_565D_internal
500
501
502static INLINE boolean is_big_endian(void)
503{
504 int test_value = 1;
505 if(*(char *)&test_value != 1)
506 return TRUE;
507 return FALSE;
508}
509
510
DRC78df2e62014-05-12 09:23:57 +0000511METHODDEF(void)
512h2v1_merged_upsample_565 (j_decompress_ptr cinfo,
513 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
514 JSAMPARRAY output_buf)
515{
DRC72a3cc02014-08-30 20:37:50 +0000516 if (is_big_endian())
517 h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
518 output_buf);
519 else
520 h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
521 output_buf);
DRC78df2e62014-05-12 09:23:57 +0000522 }
523
524
525METHODDEF(void)
526h2v1_merged_upsample_565D (j_decompress_ptr cinfo,
527 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
528 JSAMPARRAY output_buf)
529{
DRC72a3cc02014-08-30 20:37:50 +0000530 if (is_big_endian())
531 h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
532 output_buf);
533 else
534 h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
535 output_buf);
DRC78df2e62014-05-12 09:23:57 +0000536}
537
538
539METHODDEF(void)
540h2v2_merged_upsample_565 (j_decompress_ptr cinfo,
541 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
542 JSAMPARRAY output_buf)
543{
DRC72a3cc02014-08-30 20:37:50 +0000544 if (is_big_endian())
545 h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
546 output_buf);
547 else
548 h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
549 output_buf);
DRC78df2e62014-05-12 09:23:57 +0000550}
551
552
553METHODDEF(void)
554h2v2_merged_upsample_565D (j_decompress_ptr cinfo,
555 JSAMPIMAGE input_buf, JDIMENSION in_row_group_ctr,
556 JSAMPARRAY output_buf)
557{
DRC72a3cc02014-08-30 20:37:50 +0000558 if (is_big_endian())
559 h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
560 output_buf);
561 else
562 h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
563 output_buf);
DRC78df2e62014-05-12 09:23:57 +0000564}
565
566
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000567/*
568 * Module initialization routine for merged upsampling/color conversion.
569 *
570 * NB: this is called under the conditions determined by use_merged_upsample()
571 * in jdmaster.c. That routine MUST correspond to the actual capabilities
572 * of this module; no safety checks are made here.
573 */
574
Thomas G. Lane489583f1996-02-07 00:00:00 +0000575GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000576jinit_merged_upsampler (j_decompress_ptr cinfo)
577{
578 my_upsample_ptr upsample;
579
580 upsample = (my_upsample_ptr)
581 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000582 sizeof(my_upsampler));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583 cinfo->upsample = (struct jpeg_upsampler *) upsample;
584 upsample->pub.start_pass = start_pass_merged_upsample;
585 upsample->pub.need_context_rows = FALSE;
586
587 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
588
589 if (cinfo->max_v_samp_factor == 2) {
590 upsample->pub.upsample = merged_2v_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000591 if (jsimd_can_h2v2_merged_upsample())
592 upsample->upmethod = jsimd_h2v2_merged_upsample;
593 else
594 upsample->upmethod = h2v2_merged_upsample;
DRC78df2e62014-05-12 09:23:57 +0000595 if (cinfo->out_color_space == JCS_RGB565) {
596 if (cinfo->dither_mode != JDITHER_NONE) {
597 upsample->upmethod = h2v2_merged_upsample_565D;
598 } else {
599 upsample->upmethod = h2v2_merged_upsample_565;
600 }
601 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000602 /* Allocate a spare row buffer */
603 upsample->spare_row = (JSAMPROW)
604 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000605 (size_t) (upsample->out_row_width * sizeof(JSAMPLE)));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000606 } else {
607 upsample->pub.upsample = merged_1v_upsample;
Pierre Ossman59a39382009-03-09 13:15:56 +0000608 if (jsimd_can_h2v1_merged_upsample())
609 upsample->upmethod = jsimd_h2v1_merged_upsample;
610 else
611 upsample->upmethod = h2v1_merged_upsample;
DRC78df2e62014-05-12 09:23:57 +0000612 if (cinfo->out_color_space == JCS_RGB565) {
613 if (cinfo->dither_mode != JDITHER_NONE) {
614 upsample->upmethod = h2v1_merged_upsample_565D;
615 } else {
616 upsample->upmethod = h2v1_merged_upsample_565;
617 }
618 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000619 /* No spare row needed */
620 upsample->spare_row = NULL;
621 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000622
623 build_ycc_rgb_table(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000624}
625
626#endif /* UPSAMPLE_MERGING_SUPPORTED */