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, 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,
33 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
34 JDIMENSION output_row, int num_rows)
36 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
38 register JLONG * ctab = cconvert->rgb_ycc_tab;
39 register JSAMPROW inptr;
40 register JSAMPROW outptr0, outptr1, outptr2;
41 register JDIMENSION col;
42 JDIMENSION num_cols = cinfo->image_width;
44 while (--num_rows >= 0) {
46 outptr0 = output_buf[0][output_row];
47 outptr1 = output_buf[1][output_row];
48 outptr2 = output_buf[2][output_row];
50 for (col = 0; col < num_cols; col++) {
51 r = GETJSAMPLE(inptr[RGB_RED]);
52 g = GETJSAMPLE(inptr[RGB_GREEN]);
53 b = GETJSAMPLE(inptr[RGB_BLUE]);
54 inptr += RGB_PIXELSIZE;
55 /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
56 * must be too; we do not need an explicit range-limiting operation.
57 * Hence the value being shifted is never negative, and we don't
58 * need the general RIGHT_SHIFT macro.
61 outptr0[col] = (JSAMPLE)
62 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
65 outptr1[col] = (JSAMPLE)
66 ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF])
69 outptr2[col] = (JSAMPLE)
70 ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
77 /**************** Cases other than RGB -> YCbCr **************/
81 * Convert some rows of samples to the JPEG colorspace.
82 * This version handles RGB->grayscale conversion, which is the same
83 * as the RGB->Y portion of RGB->YCbCr.
84 * We assume rgb_ycc_start has been called (we only use the Y tables).
89 rgb_gray_convert_internal (j_compress_ptr cinfo,
90 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
91 JDIMENSION output_row, int num_rows)
93 my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
95 register JLONG * ctab = cconvert->rgb_ycc_tab;
96 register JSAMPROW inptr;
97 register JSAMPROW outptr;
98 register JDIMENSION col;
99 JDIMENSION num_cols = cinfo->image_width;
101 while (--num_rows >= 0) {
102 inptr = *input_buf++;
103 outptr = output_buf[0][output_row];
105 for (col = 0; col < num_cols; col++) {
106 r = GETJSAMPLE(inptr[RGB_RED]);
107 g = GETJSAMPLE(inptr[RGB_GREEN]);
108 b = GETJSAMPLE(inptr[RGB_BLUE]);
109 inptr += RGB_PIXELSIZE;
111 outptr[col] = (JSAMPLE)
112 ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
120 * Convert some rows of samples to the JPEG colorspace.
121 * This version handles extended RGB->plain RGB conversion
126 rgb_rgb_convert_internal (j_compress_ptr cinfo,
127 JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
128 JDIMENSION output_row, int num_rows)
130 register JSAMPROW inptr;
131 register JSAMPROW outptr0, outptr1, outptr2;
132 register JDIMENSION col;
133 JDIMENSION num_cols = cinfo->image_width;
135 while (--num_rows >= 0) {
136 inptr = *input_buf++;
137 outptr0 = output_buf[0][output_row];
138 outptr1 = output_buf[1][output_row];
139 outptr2 = output_buf[2][output_row];
141 for (col = 0; col < num_cols; col++) {
142 outptr0[col] = GETJSAMPLE(inptr[RGB_RED]);
143 outptr1[col] = GETJSAMPLE(inptr[RGB_GREEN]);
144 outptr2[col] = GETJSAMPLE(inptr[RGB_BLUE]);
145 inptr += RGB_PIXELSIZE;