4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1996, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2009-2012, 2015, 2022, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
11 * This file contains input colorspace conversion routines.
15 /* This file is included by jccolor.c */
19 * Convert some rows of samples to the JPEG colorspace.
21 * Note that we change from the application's interleaved-pixel format
22 * to our internal noninterleaved, one-plane-per-component format.
23 * The input buffer is therefore three times as wide as the output buffer.
25 * A starting row offset is provided only for the output buffer. The caller
26 * can easily adjust the passed input_buf value to accommodate any row
27 * offset required on that side.
32 rgb_ycc_convert_internal(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
33 _JSAMPIMAGE output_buf, JDIMENSION output_row,
36 #if BITS_IN_JSAMPLE != 16
37 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
39 register JLONG *ctab = cconvert->rgb_ycc_tab;
40 register _JSAMPROW inptr;
41 register _JSAMPROW outptr0, outptr1, outptr2;
42 register JDIMENSION col;
43 JDIMENSION num_cols = cinfo->image_width;
45 while (--num_rows >= 0) {
47 outptr0 = output_buf[0][output_row];
48 outptr1 = output_buf[1][output_row];
49 outptr2 = output_buf[2][output_row];
51 for (col = 0; col < num_cols; col++) {
52 r = RANGE_LIMIT(inptr[RGB_RED]);
53 g = RANGE_LIMIT(inptr[RGB_GREEN]);
54 b = RANGE_LIMIT(inptr[RGB_BLUE]);
55 inptr += RGB_PIXELSIZE;
56 /* If the inputs are 0.._MAXJSAMPLE, the outputs of these equations
57 * must be too; we do not need an explicit range-limiting operation.
58 * Hence the value being shifted is never negative, and we don't
59 * need the general RIGHT_SHIFT macro.
62 outptr0[col] = (_JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
63 ctab[b + B_Y_OFF]) >> SCALEBITS);
65 outptr1[col] = (_JSAMPLE)((ctab[r + R_CB_OFF] + ctab[g + G_CB_OFF] +
66 ctab[b + B_CB_OFF]) >> SCALEBITS);
68 outptr2[col] = (_JSAMPLE)((ctab[r + R_CR_OFF] + ctab[g + G_CR_OFF] +
69 ctab[b + B_CR_OFF]) >> SCALEBITS);
73 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
78 /**************** Cases other than RGB -> YCbCr **************/
82 * Convert some rows of samples to the JPEG colorspace.
83 * This version handles RGB->grayscale conversion, which is the same
84 * as the RGB->Y portion of RGB->YCbCr.
85 * We assume rgb_ycc_start has been called (we only use the Y tables).
90 rgb_gray_convert_internal(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
91 _JSAMPIMAGE output_buf, JDIMENSION output_row,
94 #if BITS_IN_JSAMPLE != 16
95 my_cconvert_ptr cconvert = (my_cconvert_ptr)cinfo->cconvert;
97 register JLONG *ctab = cconvert->rgb_ycc_tab;
98 register _JSAMPROW inptr;
99 register _JSAMPROW outptr;
100 register JDIMENSION col;
101 JDIMENSION num_cols = cinfo->image_width;
103 while (--num_rows >= 0) {
104 inptr = *input_buf++;
105 outptr = output_buf[0][output_row];
107 for (col = 0; col < num_cols; col++) {
108 r = RANGE_LIMIT(inptr[RGB_RED]);
109 g = RANGE_LIMIT(inptr[RGB_GREEN]);
110 b = RANGE_LIMIT(inptr[RGB_BLUE]);
111 inptr += RGB_PIXELSIZE;
113 outptr[col] = (_JSAMPLE)((ctab[r + R_Y_OFF] + ctab[g + G_Y_OFF] +
114 ctab[b + B_Y_OFF]) >> SCALEBITS);
118 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
124 * Convert some rows of samples to the JPEG colorspace.
125 * This version handles extended RGB->plain RGB conversion
130 rgb_rgb_convert_internal(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
131 _JSAMPIMAGE output_buf, JDIMENSION output_row,
134 register _JSAMPROW inptr;
135 register _JSAMPROW outptr0, outptr1, outptr2;
136 register JDIMENSION col;
137 JDIMENSION num_cols = cinfo->image_width;
139 while (--num_rows >= 0) {
140 inptr = *input_buf++;
141 outptr0 = output_buf[0][output_row];
142 outptr1 = output_buf[1][output_row];
143 outptr2 = output_buf[2][output_row];
145 for (col = 0; col < num_cols; col++) {
146 outptr0[col] = inptr[RGB_RED];
147 outptr1[col] = inptr[RGB_GREEN];
148 outptr2[col] = inptr[RGB_BLUE];
149 inptr += RGB_PIXELSIZE;