1/*
2 * jdmerge.c
3 *
4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1996, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8 * Copyright (C) 2009, 2011, 2014-2015, D. R. Commander.
9 * Copyright (C) 2013, Linaro Limited.
10 * For conditions of distribution and use, see the accompanying README.ijg
11 * file.
12 *
13 * This file contains code for merged upsampling/color conversion.
14 *
15 * This file combines functions from jdsample.c and jdcolor.c;
16 * read those files first to understand what's going on.
17 *
18 * When the chroma components are to be upsampled by simple replication
19 * (ie, box filtering), we can save some work in color conversion by
20 * calculating all the output pixels corresponding to a pair of chroma
21 * samples at one time. In the conversion equations
22 * R = Y + K1 * Cr
23 * G = Y + K2 * Cb + K3 * Cr
24 * B = Y + K4 * Cb
25 * only the Y term varies among the group of pixels corresponding to a pair
26 * of chroma samples, so the rest of the terms can be calculated just once.
27 * At typical sampling ratios, this eliminates half or three-quarters of the
28 * multiplications needed for color conversion.
29 *
30 * This file currently provides implementations for the following cases:
31 * YCbCr => RGB color conversion only.
32 * Sampling ratios of 2h1v or 2h2v.
33 * No scaling needed at upsample time.
34 * Corner-aligned (non-CCIR601) sampling alignment.
35 * Other special cases could be added, but in most applications these are
36 * the only common cases. (For uncommon cases we fall back on the more
37 * general code in jdsample.c and jdcolor.c.)
38 */
39
40#define JPEG_INTERNALS
41#include "jinclude.h"
42#include "jpeglib.h"
43#include "jsimd.h"
44#include "jconfigint.h"
45
46#ifdef UPSAMPLE_MERGING_SUPPORTED
47
48
49/* Private subobject */
50
51typedef struct {
52 struct jpeg_upsampler pub; /* public fields */
53
54 /* Pointer to routine to do actual upsampling/conversion of one row group */
55 void (*upmethod) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
56 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf);
57
58 /* Private state for YCC->RGB conversion */
59 int *Cr_r_tab; /* => table for Cr to R conversion */
60 int *Cb_b_tab; /* => table for Cb to B conversion */
61 JLONG *Cr_g_tab; /* => table for Cr to G conversion */
62 JLONG *Cb_g_tab; /* => table for Cb to G conversion */
63
64 /* For 2:1 vertical sampling, we produce two output rows at a time.
65 * We need a "spare" row buffer to hold the second output row if the
66 * application provides just a one-row buffer; we also use the spare
67 * to discard the dummy last row if the image height is odd.
68 */
69 JSAMPROW spare_row;
70 boolean spare_full; /* T if spare buffer is occupied */
71
72 JDIMENSION out_row_width; /* samples per output row */
73 JDIMENSION rows_to_go; /* counts rows remaining in image */
74} my_upsampler;
75
76typedef my_upsampler *my_upsample_ptr;
77
78#define SCALEBITS 16 /* speediest right-shift on some machines */
79#define ONE_HALF ((JLONG)1 << (SCALEBITS - 1))
80#define FIX(x) ((JLONG)((x) * (1L << SCALEBITS) + 0.5))
81
82
83/* Include inline routines for colorspace extensions */
84
85#include "jdmrgext.c"
86#undef RGB_RED
87#undef RGB_GREEN
88#undef RGB_BLUE
89#undef RGB_PIXELSIZE
90
91#define RGB_RED EXT_RGB_RED
92#define RGB_GREEN EXT_RGB_GREEN
93#define RGB_BLUE EXT_RGB_BLUE
94#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
95#define h2v1_merged_upsample_internal extrgb_h2v1_merged_upsample_internal
96#define h2v2_merged_upsample_internal extrgb_h2v2_merged_upsample_internal
97#include "jdmrgext.c"
98#undef RGB_RED
99#undef RGB_GREEN
100#undef RGB_BLUE
101#undef RGB_PIXELSIZE
102#undef h2v1_merged_upsample_internal
103#undef h2v2_merged_upsample_internal
104
105#define RGB_RED EXT_RGBX_RED
106#define RGB_GREEN EXT_RGBX_GREEN
107#define RGB_BLUE EXT_RGBX_BLUE
108#define RGB_ALPHA 3
109#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
110#define h2v1_merged_upsample_internal extrgbx_h2v1_merged_upsample_internal
111#define h2v2_merged_upsample_internal extrgbx_h2v2_merged_upsample_internal
112#include "jdmrgext.c"
113#undef RGB_RED
114#undef RGB_GREEN
115#undef RGB_BLUE
116#undef RGB_ALPHA
117#undef RGB_PIXELSIZE
118#undef h2v1_merged_upsample_internal
119#undef h2v2_merged_upsample_internal
120
121#define RGB_RED EXT_BGR_RED
122#define RGB_GREEN EXT_BGR_GREEN
123#define RGB_BLUE EXT_BGR_BLUE
124#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
125#define h2v1_merged_upsample_internal extbgr_h2v1_merged_upsample_internal
126#define h2v2_merged_upsample_internal extbgr_h2v2_merged_upsample_internal
127#include "jdmrgext.c"
128#undef RGB_RED
129#undef RGB_GREEN
130#undef RGB_BLUE
131#undef RGB_PIXELSIZE
132#undef h2v1_merged_upsample_internal
133#undef h2v2_merged_upsample_internal
134
135#define RGB_RED EXT_BGRX_RED
136#define RGB_GREEN EXT_BGRX_GREEN
137#define RGB_BLUE EXT_BGRX_BLUE
138#define RGB_ALPHA 3
139#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
140#define h2v1_merged_upsample_internal extbgrx_h2v1_merged_upsample_internal
141#define h2v2_merged_upsample_internal extbgrx_h2v2_merged_upsample_internal
142#include "jdmrgext.c"
143#undef RGB_RED
144#undef RGB_GREEN
145#undef RGB_BLUE
146#undef RGB_ALPHA
147#undef RGB_PIXELSIZE
148#undef h2v1_merged_upsample_internal
149#undef h2v2_merged_upsample_internal
150
151#define RGB_RED EXT_XBGR_RED
152#define RGB_GREEN EXT_XBGR_GREEN
153#define RGB_BLUE EXT_XBGR_BLUE
154#define RGB_ALPHA 0
155#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
156#define h2v1_merged_upsample_internal extxbgr_h2v1_merged_upsample_internal
157#define h2v2_merged_upsample_internal extxbgr_h2v2_merged_upsample_internal
158#include "jdmrgext.c"
159#undef RGB_RED
160#undef RGB_GREEN
161#undef RGB_BLUE
162#undef RGB_ALPHA
163#undef RGB_PIXELSIZE
164#undef h2v1_merged_upsample_internal
165#undef h2v2_merged_upsample_internal
166
167#define RGB_RED EXT_XRGB_RED
168#define RGB_GREEN EXT_XRGB_GREEN
169#define RGB_BLUE EXT_XRGB_BLUE
170#define RGB_ALPHA 0
171#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
172#define h2v1_merged_upsample_internal extxrgb_h2v1_merged_upsample_internal
173#define h2v2_merged_upsample_internal extxrgb_h2v2_merged_upsample_internal
174#include "jdmrgext.c"
175#undef RGB_RED
176#undef RGB_GREEN
177#undef RGB_BLUE
178#undef RGB_ALPHA
179#undef RGB_PIXELSIZE
180#undef h2v1_merged_upsample_internal
181#undef h2v2_merged_upsample_internal
182
183
184/*
185 * Initialize tables for YCC->RGB colorspace conversion.
186 * This is taken directly from jdcolor.c; see that file for more info.
187 */
188
189LOCAL(void)
190build_ycc_rgb_table(j_decompress_ptr cinfo)
191{
192 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
193 int i;
194 JLONG x;
195 SHIFT_TEMPS
196
197 upsample->Cr_r_tab = (int *)
198 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
199 (MAXJSAMPLE + 1) * sizeof(int));
200 upsample->Cb_b_tab = (int *)
201 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
202 (MAXJSAMPLE + 1) * sizeof(int));
203 upsample->Cr_g_tab = (JLONG *)
204 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
205 (MAXJSAMPLE + 1) * sizeof(JLONG));
206 upsample->Cb_g_tab = (JLONG *)
207 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
208 (MAXJSAMPLE + 1) * sizeof(JLONG));
209
210 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
211 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
212 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
213 /* Cr=>R value is nearest int to 1.40200 * x */
214 upsample->Cr_r_tab[i] = (int)
215 RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
216 /* Cb=>B value is nearest int to 1.77200 * x */
217 upsample->Cb_b_tab[i] = (int)
218 RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
219 /* Cr=>G value is scaled-up -0.71414 * x */
220 upsample->Cr_g_tab[i] = (-FIX(0.71414)) * x;
221 /* Cb=>G value is scaled-up -0.34414 * x */
222 /* We also add in ONE_HALF so that need not do it in inner loop */
223 upsample->Cb_g_tab[i] = (-FIX(0.34414)) * x + ONE_HALF;
224 }
225}
226
227
228/*
229 * Initialize for an upsampling pass.
230 */
231
232METHODDEF(void)
233start_pass_merged_upsample(j_decompress_ptr cinfo)
234{
235 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
236
237 /* Mark the spare buffer empty */
238 upsample->spare_full = FALSE;
239 /* Initialize total-height counter for detecting bottom of image */
240 upsample->rows_to_go = cinfo->output_height;
241}
242
243
244/*
245 * Control routine to do upsampling (and color conversion).
246 *
247 * The control routine just handles the row buffering considerations.
248 */
249
250METHODDEF(void)
251merged_2v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
252 JDIMENSION *in_row_group_ctr,
253 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
254 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
255/* 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];
259 JDIMENSION num_rows; /* number of rows returned to caller */
260
261 if (upsample->spare_full) {
262 /* If we have a spare row saved from a previous cycle, just return it. */
263 JDIMENSION size = upsample->out_row_width;
264 if (cinfo->out_color_space == JCS_RGB565)
265 size = cinfo->output_width * 2;
266 jcopy_sample_rows(&upsample->spare_row, 0, output_buf + *out_row_ctr, 0, 1,
267 size);
268 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
301METHODDEF(void)
302merged_1v_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
303 JDIMENSION *in_row_group_ctr,
304 JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
305 JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
306/* 1:1 vertical sampling case: much easier, never need a spare row. */
307{
308 my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
309
310 /* Just do the upsampling. */
311 (*upsample->upmethod) (cinfo, input_buf, *in_row_group_ctr,
312 output_buf + *out_row_ctr);
313 /* Adjust counts */
314 (*out_row_ctr)++;
315 (*in_row_group_ctr)++;
316}
317
318
319/*
320 * These are the routines invoked by the control routines to do
321 * the actual upsampling/conversion. One row group is processed per call.
322 *
323 * Note: since we may be writing directly into application-supplied buffers,
324 * we have to be honest about the output width; we can't assume the buffer
325 * has been rounded up to an even width.
326 */
327
328
329/*
330 * Upsample and color convert for the case of 2:1 horizontal and 1:1 vertical.
331 */
332
333METHODDEF(void)
334h2v1_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
335 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
336{
337 switch (cinfo->out_color_space) {
338 case JCS_EXT_RGB:
339 extrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
340 output_buf);
341 break;
342 case JCS_EXT_RGBX:
343 case JCS_EXT_RGBA:
344 extrgbx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
345 output_buf);
346 break;
347 case JCS_EXT_BGR:
348 extbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
349 output_buf);
350 break;
351 case JCS_EXT_BGRX:
352 case JCS_EXT_BGRA:
353 extbgrx_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
354 output_buf);
355 break;
356 case JCS_EXT_XBGR:
357 case JCS_EXT_ABGR:
358 extxbgr_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
359 output_buf);
360 break;
361 case JCS_EXT_XRGB:
362 case JCS_EXT_ARGB:
363 extxrgb_h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
364 output_buf);
365 break;
366 default:
367 h2v1_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
368 output_buf);
369 break;
370 }
371}
372
373
374/*
375 * Upsample and color convert for the case of 2:1 horizontal and 2:1 vertical.
376 */
377
378METHODDEF(void)
379h2v2_merged_upsample(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
380 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
381{
382 switch (cinfo->out_color_space) {
383 case JCS_EXT_RGB:
384 extrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
385 output_buf);
386 break;
387 case JCS_EXT_RGBX:
388 case JCS_EXT_RGBA:
389 extrgbx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
390 output_buf);
391 break;
392 case JCS_EXT_BGR:
393 extbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
394 output_buf);
395 break;
396 case JCS_EXT_BGRX:
397 case JCS_EXT_BGRA:
398 extbgrx_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
399 output_buf);
400 break;
401 case JCS_EXT_XBGR:
402 case JCS_EXT_ABGR:
403 extxbgr_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
404 output_buf);
405 break;
406 case JCS_EXT_XRGB:
407 case JCS_EXT_ARGB:
408 extxrgb_h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
409 output_buf);
410 break;
411 default:
412 h2v2_merged_upsample_internal(cinfo, input_buf, in_row_group_ctr,
413 output_buf);
414 break;
415 }
416}
417
418
419/*
420 * RGB565 conversion
421 */
422
423#define PACK_SHORT_565_LE(r, g, b) ((((r) << 8) & 0xF800) | \
424 (((g) << 3) & 0x7E0) | ((b) >> 3))
425#define PACK_SHORT_565_BE(r, g, b) (((r) & 0xF8) | ((g) >> 5) | \
426 (((g) << 11) & 0xE000) | \
427 (((b) << 5) & 0x1F00))
428
429#define PACK_TWO_PIXELS_LE(l, r) ((r << 16) | l)
430#define PACK_TWO_PIXELS_BE(l, r) ((l << 16) | r)
431
432#define PACK_NEED_ALIGNMENT(ptr) (((size_t)(ptr)) & 3)
433
434#define WRITE_TWO_PIXELS_LE(addr, pixels) { \
435 ((INT16 *)(addr))[0] = (INT16)(pixels); \
436 ((INT16 *)(addr))[1] = (INT16)((pixels) >> 16); \
437}
438#define WRITE_TWO_PIXELS_BE(addr, pixels) { \
439 ((INT16 *)(addr))[1] = (INT16)(pixels); \
440 ((INT16 *)(addr))[0] = (INT16)((pixels) >> 16); \
441}
442
443#define DITHER_565_R(r, dither) ((r) + ((dither) & 0xFF))
444#define DITHER_565_G(g, dither) ((g) + (((dither) & 0xFF) >> 1))
445#define DITHER_565_B(b, dither) ((b) + ((dither) & 0xFF))
446
447
448/* Declarations for ordered dithering
449 *
450 * We use a 4x4 ordered dither array packed into 32 bits. This array is
451 * sufficent for dithering RGB888 to RGB565.
452 */
453
454#define DITHER_MASK 0x3
455#define DITHER_ROTATE(x) ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
456static const JLONG dither_matrix[4] = {
457 0x0008020A,
458 0x0C040E06,
459 0x030B0109,
460 0x0F070D05
461};
462
463
464/* Include inline routines for RGB565 conversion */
465
466#define PACK_SHORT_565 PACK_SHORT_565_LE
467#define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
468#define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_LE
469#define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_le
470#define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_le
471#define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_le
472#define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_le
473#include "jdmrg565.c"
474#undef PACK_SHORT_565
475#undef PACK_TWO_PIXELS
476#undef WRITE_TWO_PIXELS
477#undef h2v1_merged_upsample_565_internal
478#undef h2v1_merged_upsample_565D_internal
479#undef h2v2_merged_upsample_565_internal
480#undef h2v2_merged_upsample_565D_internal
481
482#define PACK_SHORT_565 PACK_SHORT_565_BE
483#define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
484#define WRITE_TWO_PIXELS WRITE_TWO_PIXELS_BE
485#define h2v1_merged_upsample_565_internal h2v1_merged_upsample_565_be
486#define h2v1_merged_upsample_565D_internal h2v1_merged_upsample_565D_be
487#define h2v2_merged_upsample_565_internal h2v2_merged_upsample_565_be
488#define h2v2_merged_upsample_565D_internal h2v2_merged_upsample_565D_be
489#include "jdmrg565.c"
490#undef PACK_SHORT_565
491#undef PACK_TWO_PIXELS
492#undef WRITE_TWO_PIXELS
493#undef h2v1_merged_upsample_565_internal
494#undef h2v1_merged_upsample_565D_internal
495#undef h2v2_merged_upsample_565_internal
496#undef h2v2_merged_upsample_565D_internal
497
498
499static INLINE boolean is_big_endian(void)
500{
501 int test_value = 1;
502 if (*(char *)&test_value != 1)
503 return TRUE;
504 return FALSE;
505}
506
507
508METHODDEF(void)
509h2v1_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
510 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
511{
512 if (is_big_endian())
513 h2v1_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
514 output_buf);
515 else
516 h2v1_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
517 output_buf);
518}
519
520
521METHODDEF(void)
522h2v1_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
523 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
524{
525 if (is_big_endian())
526 h2v1_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
527 output_buf);
528 else
529 h2v1_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
530 output_buf);
531}
532
533
534METHODDEF(void)
535h2v2_merged_upsample_565(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
536 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
537{
538 if (is_big_endian())
539 h2v2_merged_upsample_565_be(cinfo, input_buf, in_row_group_ctr,
540 output_buf);
541 else
542 h2v2_merged_upsample_565_le(cinfo, input_buf, in_row_group_ctr,
543 output_buf);
544}
545
546
547METHODDEF(void)
548h2v2_merged_upsample_565D(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
549 JDIMENSION in_row_group_ctr, JSAMPARRAY output_buf)
550{
551 if (is_big_endian())
552 h2v2_merged_upsample_565D_be(cinfo, input_buf, in_row_group_ctr,
553 output_buf);
554 else
555 h2v2_merged_upsample_565D_le(cinfo, input_buf, in_row_group_ctr,
556 output_buf);
557}
558
559
560/*
561 * Module initialization routine for merged upsampling/color conversion.
562 *
563 * NB: this is called under the conditions determined by use_merged_upsample()
564 * in jdmaster.c. That routine MUST correspond to the actual capabilities
565 * of this module; no safety checks are made here.
566 */
567
568GLOBAL(void)
569jinit_merged_upsampler(j_decompress_ptr cinfo)
570{
571 my_upsample_ptr upsample;
572
573 upsample = (my_upsample_ptr)
574 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
575 sizeof(my_upsampler));
576 cinfo->upsample = (struct jpeg_upsampler *)upsample;
577 upsample->pub.start_pass = start_pass_merged_upsample;
578 upsample->pub.need_context_rows = FALSE;
579
580 upsample->out_row_width = cinfo->output_width * cinfo->out_color_components;
581
582 if (cinfo->max_v_samp_factor == 2) {
583 upsample->pub.upsample = merged_2v_upsample;
584 if (jsimd_can_h2v2_merged_upsample())
585 upsample->upmethod = jsimd_h2v2_merged_upsample;
586 else
587 upsample->upmethod = h2v2_merged_upsample;
588 if (cinfo->out_color_space == JCS_RGB565) {
589 if (cinfo->dither_mode != JDITHER_NONE) {
590 upsample->upmethod = h2v2_merged_upsample_565D;
591 } else {
592 upsample->upmethod = h2v2_merged_upsample_565;
593 }
594 }
595 /* Allocate a spare row buffer */
596 upsample->spare_row = (JSAMPROW)
597 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
598 (size_t)(upsample->out_row_width * sizeof(JSAMPLE)));
599 } else {
600 upsample->pub.upsample = merged_1v_upsample;
601 if (jsimd_can_h2v1_merged_upsample())
602 upsample->upmethod = jsimd_h2v1_merged_upsample;
603 else
604 upsample->upmethod = h2v1_merged_upsample;
605 if (cinfo->out_color_space == JCS_RGB565) {
606 if (cinfo->dither_mode != JDITHER_NONE) {
607 upsample->upmethod = h2v1_merged_upsample_565D;
608 } else {
609 upsample->upmethod = h2v1_merged_upsample_565;
610 }
611 }
612 /* No spare row needed */
613 upsample->spare_row = NULL;
614 }
615
616 build_ycc_rgb_table(cinfo);
617}
618
619#endif /* UPSAMPLE_MERGING_SUPPORTED */
620