1b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC/*
2b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * jdcolext.c
3b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC *
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software:
5b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * Copyright (C) 1991-1997, Thomas G. Lane.
6a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications:
76eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * Copyright (C) 2009, 2011, 2015, D. R. Commander.
86eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * For conditions of distribution and use, see the accompanying README.ijg
96eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * file.
10b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC *
11b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * This file contains output colorspace conversion routines.
12b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC */
13b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
14b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
15b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC/* This file is included by jdcolor.c */
16b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
17b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
18b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC/*
19b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * Convert some rows of samples to the output colorspace.
20b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC *
21b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * Note that we change from noninterleaved, one-plane-per-component format
22b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * to interleaved-pixel format.  The output buffer is therefore three times
23b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * as wide as the input buffer.
24b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * A starting row offset is provided only for the input buffer.  The caller
25b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * can easily adjust the passed output_buf value to accommodate any row
26b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * offset required on that side.
27b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC */
28b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
29b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRCINLINE
30b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRCLOCAL(void)
31b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRCycc_rgb_convert_internal (j_decompress_ptr cinfo,
32b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                          JSAMPIMAGE input_buf, JDIMENSION input_row,
33b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                          JSAMPARRAY output_buf, int num_rows)
34b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC{
35b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
36b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register int y, cb, cr;
37b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register JSAMPROW outptr;
38b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register JSAMPROW inptr0, inptr1, inptr2;
39b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register JDIMENSION col;
40b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  JDIMENSION num_cols = cinfo->output_width;
41b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  /* copy these pointers into registers if possible */
42b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register JSAMPLE * range_limit = cinfo->sample_range_limit;
43b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register int * Crrtab = cconvert->Cr_r_tab;
44b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register int * Cbbtab = cconvert->Cb_b_tab;
456eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register JLONG * Crgtab = cconvert->Cr_g_tab;
466eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register JLONG * Cbgtab = cconvert->Cb_g_tab;
47b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  SHIFT_TEMPS
48b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
49b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  while (--num_rows >= 0) {
50b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    inptr0 = input_buf[0][input_row];
51b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    inptr1 = input_buf[1][input_row];
52b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    inptr2 = input_buf[2][input_row];
53b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    input_row++;
54b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    outptr = *output_buf++;
55b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    for (col = 0; col < num_cols; col++) {
56b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      y  = GETJSAMPLE(inptr0[col]);
57b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      cb = GETJSAMPLE(inptr1[col]);
58b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      cr = GETJSAMPLE(inptr2[col]);
59b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      /* Range-limiting is essential due to noise introduced by DCT losses. */
60b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      outptr[RGB_RED] =   range_limit[y + Crrtab[cr]];
61b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      outptr[RGB_GREEN] = range_limit[y +
62e5eaf37440b8e337ab150c017df7c03faf846c51DRC                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
63e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                                 SCALEBITS))];
64b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      outptr[RGB_BLUE] =  range_limit[y + Cbbtab[cb]];
65d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC      /* Set unused byte to 0xFF so it can be interpreted as an opaque */
66d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC      /* alpha channel value */
67d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#ifdef RGB_ALPHA
68d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC      outptr[RGB_ALPHA] = 0xFF;
69d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#endif
70b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      outptr += RGB_PIXELSIZE;
71b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    }
72b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  }
73b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC}
74b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
75b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
76b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC/*
77b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * Convert grayscale to RGB: just duplicate the graylevel three times.
78b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * This is provided to support applications that don't want to cope
79b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * with grayscale as a separate case.
80b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC */
81b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
82b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRCINLINE
83b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRCLOCAL(void)
84b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRCgray_rgb_convert_internal (j_decompress_ptr cinfo,
85b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                           JSAMPIMAGE input_buf, JDIMENSION input_row,
86b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                           JSAMPARRAY output_buf, int num_rows)
87b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC{
88b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register JSAMPROW inptr, outptr;
89b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  register JDIMENSION col;
90b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  JDIMENSION num_cols = cinfo->output_width;
91b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
92b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  while (--num_rows >= 0) {
93b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    inptr = input_buf[0][input_row++];
94b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    outptr = *output_buf++;
95b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    for (col = 0; col < num_cols; col++) {
96b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      /* We can dispense with GETJSAMPLE() here */
97b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      outptr[RGB_RED] = outptr[RGB_GREEN] = outptr[RGB_BLUE] = inptr[col];
98d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC      /* Set unused byte to 0xFF so it can be interpreted as an opaque */
99d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC      /* alpha channel value */
100d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#ifdef RGB_ALPHA
101d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC      outptr[RGB_ALPHA] = 0xFF;
102d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#endif
103b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      outptr += RGB_PIXELSIZE;
104b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    }
105b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  }
106b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC}
107a9b646c202a2a154a125545b4347cbfd70598c9eDRC
108a9b646c202a2a154a125545b4347cbfd70598c9eDRC
109a9b646c202a2a154a125545b4347cbfd70598c9eDRC/*
110a9b646c202a2a154a125545b4347cbfd70598c9eDRC * Convert RGB to extended RGB: just swap the order of source pixels
111a9b646c202a2a154a125545b4347cbfd70598c9eDRC */
112a9b646c202a2a154a125545b4347cbfd70598c9eDRC
113a9b646c202a2a154a125545b4347cbfd70598c9eDRCINLINE
114a9b646c202a2a154a125545b4347cbfd70598c9eDRCLOCAL(void)
115a9b646c202a2a154a125545b4347cbfd70598c9eDRCrgb_rgb_convert_internal (j_decompress_ptr cinfo,
116a9b646c202a2a154a125545b4347cbfd70598c9eDRC                          JSAMPIMAGE input_buf, JDIMENSION input_row,
117a9b646c202a2a154a125545b4347cbfd70598c9eDRC                          JSAMPARRAY output_buf, int num_rows)
118a9b646c202a2a154a125545b4347cbfd70598c9eDRC{
119a9b646c202a2a154a125545b4347cbfd70598c9eDRC  register JSAMPROW inptr0, inptr1, inptr2;
120a9b646c202a2a154a125545b4347cbfd70598c9eDRC  register JSAMPROW outptr;
121a9b646c202a2a154a125545b4347cbfd70598c9eDRC  register JDIMENSION col;
122a9b646c202a2a154a125545b4347cbfd70598c9eDRC  JDIMENSION num_cols = cinfo->output_width;
123a9b646c202a2a154a125545b4347cbfd70598c9eDRC
124a9b646c202a2a154a125545b4347cbfd70598c9eDRC  while (--num_rows >= 0) {
125a9b646c202a2a154a125545b4347cbfd70598c9eDRC    inptr0 = input_buf[0][input_row];
126a9b646c202a2a154a125545b4347cbfd70598c9eDRC    inptr1 = input_buf[1][input_row];
127a9b646c202a2a154a125545b4347cbfd70598c9eDRC    inptr2 = input_buf[2][input_row];
128a9b646c202a2a154a125545b4347cbfd70598c9eDRC    input_row++;
129a9b646c202a2a154a125545b4347cbfd70598c9eDRC    outptr = *output_buf++;
130a9b646c202a2a154a125545b4347cbfd70598c9eDRC    for (col = 0; col < num_cols; col++) {
131a9b646c202a2a154a125545b4347cbfd70598c9eDRC      /* We can dispense with GETJSAMPLE() here */
132a9b646c202a2a154a125545b4347cbfd70598c9eDRC      outptr[RGB_RED] = inptr0[col];
133a9b646c202a2a154a125545b4347cbfd70598c9eDRC      outptr[RGB_GREEN] = inptr1[col];
134a9b646c202a2a154a125545b4347cbfd70598c9eDRC      outptr[RGB_BLUE] = inptr2[col];
135a9b646c202a2a154a125545b4347cbfd70598c9eDRC      /* Set unused byte to 0xFF so it can be interpreted as an opaque */
136a9b646c202a2a154a125545b4347cbfd70598c9eDRC      /* alpha channel value */
137a9b646c202a2a154a125545b4347cbfd70598c9eDRC#ifdef RGB_ALPHA
138a9b646c202a2a154a125545b4347cbfd70598c9eDRC      outptr[RGB_ALPHA] = 0xFF;
139a9b646c202a2a154a125545b4347cbfd70598c9eDRC#endif
140a9b646c202a2a154a125545b4347cbfd70598c9eDRC      outptr += RGB_PIXELSIZE;
141a9b646c202a2a154a125545b4347cbfd70598c9eDRC    }
142a9b646c202a2a154a125545b4347cbfd70598c9eDRC  }
143a9b646c202a2a154a125545b4347cbfd70598c9eDRC}
144