12cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
22cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * jdcolor.c
32cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software:
55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * Copyright (C) 1991-1997, Thomas G. Lane.
65829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * Modified 2011 by Guido Vollbeding.
7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications:
8cf763c0cd8116ac225bd2d36a71da33a3c7fcc46DRC * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
90e94025ac72a481ca822b9c876dcada6db977d8bDRC * Copyright (C) 2009, 2011-2012, 2014-2015, D. R. Commander.
1078df2e6115b0e579432d01cb034132cd4402a1baDRC * Copyright (C) 2013, Linaro Limited.
116eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * For conditions of distribution and use, see the accompanying README.ijg
126eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * file.
132cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane *
142cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * This file contains output colorspace conversion routines.
152cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
162cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS
182cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane#include "jinclude.h"
1936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h"
2059a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman#include "jsimd.h"
21ff6961f3d22060adc13cc3e60d42fc480de007e5DRC#include "jconfigint.h"
2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Private subobject */
2536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef struct {
2736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  struct jpeg_color_deconverter pub; /* public fields */
2836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Private state for YCC->RGB conversion */
306eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  int *Cr_r_tab;                /* => table for Cr to R conversion */
316eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  int *Cb_b_tab;                /* => table for Cb to B conversion */
326eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JLONG *Cr_g_tab;              /* => table for Cr to G conversion */
336eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JLONG *Cb_g_tab;              /* => table for Cb to G conversion */
345829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
355829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  /* Private state for RGB->Y conversion */
366eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JLONG *rgb_y_tab;             /* => table for RGB to Y conversion */
3736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} my_color_deconverter;
3836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
396eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidistypedef my_color_deconverter *my_cconvert_ptr;
402cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
412cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/**************** YCbCr -> RGB conversion: most common case **************/
435829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding/****************   RGB -> Y   conversion: less common case **************/
444a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/*
464a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
474a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
484a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * The conversion equations to be implemented are therefore
495829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding *
50e5eaf37440b8e337ab150c017df7c03faf846c51DRC *      R = Y                + 1.40200 * Cr
51e5eaf37440b8e337ab150c017df7c03faf846c51DRC *      G = Y - 0.34414 * Cb - 0.71414 * Cr
52e5eaf37440b8e337ab150c017df7c03faf846c51DRC *      B = Y + 1.77200 * Cb
535829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding *
54e5eaf37440b8e337ab150c017df7c03faf846c51DRC *      Y = 0.29900 * R + 0.58700 * G + 0.11400 * B
555829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding *
569ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane * where Cb and Cr represent the incoming values less CENTERJSAMPLE.
5788aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
584a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
594a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * To avoid floating-point arithmetic, we represent the fractional constants
6088aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
6188aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane * the products by 2^16, with appropriate rounding, to get the correct answer.
624a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * Notice that Y, being an integral input, does not contribute any fraction
634a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * so it need not participate in the rounding.
644a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane *
654a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * For even more speed, we avoid doing any multiplications in the inner loop
664a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * by precalculating the constants times Cb and Cr for all possible values.
6788aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
6888aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane * for 12-bit samples it is still acceptable.  It's not very reasonable for
6988aeed428fd820659e3ae00292cb84ecfc05dd23Thomas G. Lane * 16-bit samples, but if you want lossless storage you shouldn't be changing
704a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * colorspace anyway.
714a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
724a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * values for the G calculation are left scaled up, since we must add them
734a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane * together before rounding.
744a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */
754a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
76e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define SCALEBITS       16      /* speediest right-shift on some machines */
776eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis#define ONE_HALF        ((JLONG) 1 << (SCALEBITS-1))
786eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis#define FIX(x)          ((JLONG) ((x) * (1L<<SCALEBITS) + 0.5))
794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
805829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding/* We allocate one big table for RGB->Y conversion and divide it up into
815829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * three parts, instead of doing three alloc_small requests.  This lets us
825829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * use a single table base address, which can be held in a register in the
835829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * inner loops on many machines (more than can hold all three addresses,
845829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * anyway).
855829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding */
865829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
87e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define R_Y_OFF         0                       /* offset to R => Y section */
88e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define G_Y_OFF         (1*(MAXJSAMPLE+1))      /* offset to G => Y section */
89e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define B_Y_OFF         (2*(MAXJSAMPLE+1))      /* etc. */
90e5eaf37440b8e337ab150c017df7c03faf846c51DRC#define TABLE_SIZE      (3*(MAXJSAMPLE+1))
915829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
924a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
93b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC/* Include inline routines for colorspace extensions */
94b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
95b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
96b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
97b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
98b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
99b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
100b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
101b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_RED EXT_RGB_RED
102b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_GREEN EXT_RGB_GREEN
103b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_BLUE EXT_RGB_BLUE
104b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_PIXELSIZE EXT_RGB_PIXELSIZE
105b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define ycc_rgb_convert_internal ycc_extrgb_convert_internal
106b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define gray_rgb_convert_internal gray_extrgb_convert_internal
107a9b646c202a2a154a125545b4347cbfd70598c9eDRC#define rgb_rgb_convert_internal rgb_extrgb_convert_internal
108b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
109b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
110b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
111b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
112b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
113b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef ycc_rgb_convert_internal
114b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef gray_rgb_convert_internal
115a9b646c202a2a154a125545b4347cbfd70598c9eDRC#undef rgb_rgb_convert_internal
116b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
117b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_RED EXT_RGBX_RED
118b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_GREEN EXT_RGBX_GREEN
119b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_BLUE EXT_RGBX_BLUE
120d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#define RGB_ALPHA 3
121b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_PIXELSIZE EXT_RGBX_PIXELSIZE
122b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define ycc_rgb_convert_internal ycc_extrgbx_convert_internal
123b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define gray_rgb_convert_internal gray_extrgbx_convert_internal
124a9b646c202a2a154a125545b4347cbfd70598c9eDRC#define rgb_rgb_convert_internal rgb_extrgbx_convert_internal
125b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
126b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
127b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
128b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
129d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#undef RGB_ALPHA
130b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
131b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef ycc_rgb_convert_internal
132b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef gray_rgb_convert_internal
133a9b646c202a2a154a125545b4347cbfd70598c9eDRC#undef rgb_rgb_convert_internal
134b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
135b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_RED EXT_BGR_RED
136b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_GREEN EXT_BGR_GREEN
137b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_BLUE EXT_BGR_BLUE
138b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_PIXELSIZE EXT_BGR_PIXELSIZE
139b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define ycc_rgb_convert_internal ycc_extbgr_convert_internal
140b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define gray_rgb_convert_internal gray_extbgr_convert_internal
141a9b646c202a2a154a125545b4347cbfd70598c9eDRC#define rgb_rgb_convert_internal rgb_extbgr_convert_internal
142b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
143b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
144b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
145b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
146b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
147b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef ycc_rgb_convert_internal
148b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef gray_rgb_convert_internal
149a9b646c202a2a154a125545b4347cbfd70598c9eDRC#undef rgb_rgb_convert_internal
150b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
151b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_RED EXT_BGRX_RED
152b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_GREEN EXT_BGRX_GREEN
153b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_BLUE EXT_BGRX_BLUE
154d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#define RGB_ALPHA 3
155b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_PIXELSIZE EXT_BGRX_PIXELSIZE
156b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define ycc_rgb_convert_internal ycc_extbgrx_convert_internal
157b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define gray_rgb_convert_internal gray_extbgrx_convert_internal
158a9b646c202a2a154a125545b4347cbfd70598c9eDRC#define rgb_rgb_convert_internal rgb_extbgrx_convert_internal
159b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
160b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
161b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
162b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
163d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#undef RGB_ALPHA
164b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
165b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef ycc_rgb_convert_internal
166b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef gray_rgb_convert_internal
167a9b646c202a2a154a125545b4347cbfd70598c9eDRC#undef rgb_rgb_convert_internal
168b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
169b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_RED EXT_XBGR_RED
170b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_GREEN EXT_XBGR_GREEN
171b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_BLUE EXT_XBGR_BLUE
172d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#define RGB_ALPHA 0
173b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_PIXELSIZE EXT_XBGR_PIXELSIZE
174b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define ycc_rgb_convert_internal ycc_extxbgr_convert_internal
175b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define gray_rgb_convert_internal gray_extxbgr_convert_internal
176a9b646c202a2a154a125545b4347cbfd70598c9eDRC#define rgb_rgb_convert_internal rgb_extxbgr_convert_internal
177b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
178b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
179b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
180b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
181d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#undef RGB_ALPHA
182b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
183b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef ycc_rgb_convert_internal
184b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef gray_rgb_convert_internal
185a9b646c202a2a154a125545b4347cbfd70598c9eDRC#undef rgb_rgb_convert_internal
186b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
187b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_RED EXT_XRGB_RED
188b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_GREEN EXT_XRGB_GREEN
189b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_BLUE EXT_XRGB_BLUE
190d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#define RGB_ALPHA 0
191b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define RGB_PIXELSIZE EXT_XRGB_PIXELSIZE
192b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define ycc_rgb_convert_internal ycc_extxrgb_convert_internal
193b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#define gray_rgb_convert_internal gray_extxrgb_convert_internal
194a9b646c202a2a154a125545b4347cbfd70598c9eDRC#define rgb_rgb_convert_internal rgb_extxrgb_convert_internal
195b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#include "jdcolext.c"
196b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_RED
197b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_GREEN
198b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_BLUE
199d89e01f5ce9f5bf05b6a8d8ef51a4bddcf201383DRC#undef RGB_ALPHA
200b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef RGB_PIXELSIZE
201b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef ycc_rgb_convert_internal
202b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC#undef gray_rgb_convert_internal
203a9b646c202a2a154a125545b4347cbfd70598c9eDRC#undef rgb_rgb_convert_internal
204b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
205b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC
2062cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
207bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Initialize tables for YCC->RGB colorspace conversion.
2082cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
2092cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
210489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void)
211bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanebuild_ycc_rgb_table (j_decompress_ptr cinfo)
2122cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
21336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
214bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int i;
2156eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JLONG x;
2164a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  SHIFT_TEMPS
2174a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
21836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cconvert->Cr_r_tab = (int *)
21936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
2205de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                (MAXJSAMPLE+1) * sizeof(int));
22136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cconvert->Cb_b_tab = (int *)
22236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
2235de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                (MAXJSAMPLE+1) * sizeof(int));
2246eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  cconvert->Cr_g_tab = (JLONG *)
22536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
2266eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis                                (MAXJSAMPLE+1) * sizeof(JLONG));
2276eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  cconvert->Cb_g_tab = (JLONG *)
22836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
2296eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis                                (MAXJSAMPLE+1) * sizeof(JLONG));
2304a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2319ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane  for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
2324a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
2339ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane    /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
2344a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* Cr=>R value is nearest int to 1.40200 * x */
23536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cconvert->Cr_r_tab[i] = (int)
236e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    RIGHT_SHIFT(FIX(1.40200) * x + ONE_HALF, SCALEBITS);
2374a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* Cb=>B value is nearest int to 1.77200 * x */
23836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cconvert->Cb_b_tab[i] = (int)
239e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    RIGHT_SHIFT(FIX(1.77200) * x + ONE_HALF, SCALEBITS);
2404a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* Cr=>G value is scaled-up -0.71414 * x */
2419ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane    cconvert->Cr_g_tab[i] = (- FIX(0.71414)) * x;
2424a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* Cb=>G value is scaled-up -0.34414 * x */
2434a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    /* We also add in ONE_HALF so that need not do it in inner loop */
2449ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane    cconvert->Cb_g_tab[i] = (- FIX(0.34414)) * x + ONE_HALF;
2454a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  }
2462cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
2472cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2482cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2492cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
2502cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane * Convert some rows of samples to the output colorspace.
2512cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
2522cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
253489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
25436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneycc_rgb_convert (j_decompress_ptr cinfo,
255e5eaf37440b8e337ab150c017df7c03faf846c51DRC                 JSAMPIMAGE input_buf, JDIMENSION input_row,
256e5eaf37440b8e337ab150c017df7c03faf846c51DRC                 JSAMPARRAY output_buf, int num_rows)
2572cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
258b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  switch (cinfo->out_color_space) {
259b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_RGB:
260b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
261b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                  num_rows);
262b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
263b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_RGBX:
26467ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_RGBA:
265b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
266b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                   num_rows);
267b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
268b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_BGR:
269b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
270b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                  num_rows);
271b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
272b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_BGRX:
27367ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_BGRA:
274b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
275b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                   num_rows);
276b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
277b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_XBGR:
27867ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_ABGR:
279b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
280b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                   num_rows);
281b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
282b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_XRGB:
28367ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_ARGB:
284b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
285b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                   num_rows);
286b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
287b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    default:
288b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      ycc_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
289b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                               num_rows);
290b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
2912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
2922cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
2932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2942cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
2954a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/**************** Cases other than YCbCr -> RGB **************/
2964a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2974a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
2984a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/*
2995829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * Initialize for RGB->grayscale colorspace conversion.
3005829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding */
3015829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3025829cb23983cd241c48abd8ed70ff3627560c453Guido VollbedingLOCAL(void)
3035829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbedingbuild_rgb_y_table (j_decompress_ptr cinfo)
3045829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding{
3055829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
3066eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JLONG *rgb_y_tab;
3076eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JLONG i;
3085829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3095829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  /* Allocate and fill in the conversion tables. */
3106eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  cconvert->rgb_y_tab = rgb_y_tab = (JLONG *)
3115829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3126eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis                                (TABLE_SIZE * sizeof(JLONG)));
3135829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3145829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  for (i = 0; i <= MAXJSAMPLE; i++) {
3155829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    rgb_y_tab[i+R_Y_OFF] = FIX(0.29900) * i;
3165829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    rgb_y_tab[i+G_Y_OFF] = FIX(0.58700) * i;
3175829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    rgb_y_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF;
3185829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  }
3195829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding}
3205829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3215829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3225829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding/*
3235829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding * Convert RGB to grayscale.
3245829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding */
3255829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3265829cb23983cd241c48abd8ed70ff3627560c453Guido VollbedingMETHODDEF(void)
3275829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbedingrgb_gray_convert (j_decompress_ptr cinfo,
328e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPIMAGE input_buf, JDIMENSION input_row,
329e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPARRAY output_buf, int num_rows)
3305829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding{
3315829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
3325829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  register int r, g, b;
3336eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register JLONG *ctab = cconvert->rgb_y_tab;
3345829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  register JSAMPROW outptr;
3355829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  register JSAMPROW inptr0, inptr1, inptr2;
3365829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  register JDIMENSION col;
3375829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  JDIMENSION num_cols = cinfo->output_width;
3385829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3395829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  while (--num_rows >= 0) {
3405829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    inptr0 = input_buf[0][input_row];
3415829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    inptr1 = input_buf[1][input_row];
3425829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    inptr2 = input_buf[2][input_row];
3435829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    input_row++;
3445829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    outptr = *output_buf++;
3455829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    for (col = 0; col < num_cols; col++) {
3465829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      r = GETJSAMPLE(inptr0[col]);
3475829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      g = GETJSAMPLE(inptr1[col]);
3485829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      b = GETJSAMPLE(inptr2[col]);
3495829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      /* Y */
3505829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      outptr[col] = (JSAMPLE)
351e5eaf37440b8e337ab150c017df7c03faf846c51DRC                ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
352e5eaf37440b8e337ab150c017df7c03faf846c51DRC                 >> SCALEBITS);
3535829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    }
3545829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding  }
3555829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding}
3565829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3575829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding
3585829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding/*
35936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Color conversion for no colorspace change: just copy the data,
36036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * converting from separate-planes to interleaved representation.
3614a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane */
3624a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
363489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
36436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanenull_convert (j_decompress_ptr cinfo,
365e5eaf37440b8e337ab150c017df7c03faf846c51DRC              JSAMPIMAGE input_buf, JDIMENSION input_row,
366e5eaf37440b8e337ab150c017df7c03faf846c51DRC              JSAMPARRAY output_buf, int num_rows)
3674a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane{
3680e94025ac72a481ca822b9c876dcada6db977d8bDRC  register JSAMPROW inptr, inptr0, inptr1, inptr2, inptr3, outptr;
3690e94025ac72a481ca822b9c876dcada6db977d8bDRC  register JDIMENSION col;
370bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  register int num_components = cinfo->num_components;
37136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JDIMENSION num_cols = cinfo->output_width;
37236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
37336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
3740e94025ac72a481ca822b9c876dcada6db977d8bDRC  if (num_components == 3) {
3750e94025ac72a481ca822b9c876dcada6db977d8bDRC    while (--num_rows >= 0) {
3760e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr0 = input_buf[0][input_row];
3770e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr1 = input_buf[1][input_row];
3780e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr2 = input_buf[2][input_row];
3790e94025ac72a481ca822b9c876dcada6db977d8bDRC      input_row++;
3800e94025ac72a481ca822b9c876dcada6db977d8bDRC      outptr = *output_buf++;
3810e94025ac72a481ca822b9c876dcada6db977d8bDRC      for (col = 0; col < num_cols; col++) {
3820e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr0[col];
3830e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr1[col];
3840e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr2[col];
38536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
38636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
3870e94025ac72a481ca822b9c876dcada6db977d8bDRC  } else if (num_components == 4) {
3880e94025ac72a481ca822b9c876dcada6db977d8bDRC    while (--num_rows >= 0) {
3890e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr0 = input_buf[0][input_row];
3900e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr1 = input_buf[1][input_row];
3910e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr2 = input_buf[2][input_row];
3920e94025ac72a481ca822b9c876dcada6db977d8bDRC      inptr3 = input_buf[3][input_row];
3930e94025ac72a481ca822b9c876dcada6db977d8bDRC      input_row++;
3940e94025ac72a481ca822b9c876dcada6db977d8bDRC      outptr = *output_buf++;
3950e94025ac72a481ca822b9c876dcada6db977d8bDRC      for (col = 0; col < num_cols; col++) {
3960e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr0[col];
3970e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr1[col];
3980e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr2[col];
3990e94025ac72a481ca822b9c876dcada6db977d8bDRC        *outptr++ = inptr3[col];
4000e94025ac72a481ca822b9c876dcada6db977d8bDRC      }
4010e94025ac72a481ca822b9c876dcada6db977d8bDRC    }
4020e94025ac72a481ca822b9c876dcada6db977d8bDRC  } else {
4030e94025ac72a481ca822b9c876dcada6db977d8bDRC    while (--num_rows >= 0) {
4040e94025ac72a481ca822b9c876dcada6db977d8bDRC      for (ci = 0; ci < num_components; ci++) {
4050e94025ac72a481ca822b9c876dcada6db977d8bDRC        inptr = input_buf[ci][input_row];
4060e94025ac72a481ca822b9c876dcada6db977d8bDRC        outptr = *output_buf;
4070e94025ac72a481ca822b9c876dcada6db977d8bDRC        for (col = 0; col < num_cols; col++) {
4080e94025ac72a481ca822b9c876dcada6db977d8bDRC          outptr[ci] = inptr[col];
4090e94025ac72a481ca822b9c876dcada6db977d8bDRC          outptr += num_components;
4100e94025ac72a481ca822b9c876dcada6db977d8bDRC        }
4110e94025ac72a481ca822b9c876dcada6db977d8bDRC      }
4120e94025ac72a481ca822b9c876dcada6db977d8bDRC      output_buf++;
4130e94025ac72a481ca822b9c876dcada6db977d8bDRC      input_row++;
4140e94025ac72a481ca822b9c876dcada6db977d8bDRC    }
41536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
4164a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane}
4174a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
4184a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane
4194a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane/*
42036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Color conversion for grayscale: just copy the data.
42136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This also works for YCbCr -> grayscale conversion, in which
42236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * we just copy the Y (luminance) component and ignore chrominance.
4232cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
4242cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
425489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
42636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanegrayscale_convert (j_decompress_ptr cinfo,
427e5eaf37440b8e337ab150c017df7c03faf846c51DRC                   JSAMPIMAGE input_buf, JDIMENSION input_row,
428e5eaf37440b8e337ab150c017df7c03faf846c51DRC                   JSAMPARRAY output_buf, int num_rows)
4292cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
43036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jcopy_sample_rows(input_buf[0], (int) input_row, output_buf, 0,
431e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    num_rows, cinfo->output_width);
4322cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
4332cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4342cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
4352cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
436b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC * Convert grayscale to RGB
4375ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane */
4385ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
4395ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. LaneMETHODDEF(void)
4405ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lanegray_rgb_convert (j_decompress_ptr cinfo,
441e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPIMAGE input_buf, JDIMENSION input_row,
442e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPARRAY output_buf, int num_rows)
4435ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane{
444b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC  switch (cinfo->out_color_space) {
445b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_RGB:
446b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
447b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                   num_rows);
448b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
449b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_RGBX:
45067ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_RGBA:
451b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
452b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                    num_rows);
453b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
454b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_BGR:
455b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
456b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                   num_rows);
457b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
458b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_BGRX:
45967ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_BGRA:
460b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
461b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                    num_rows);
462b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
463b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_XBGR:
46467ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_ABGR:
465b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
466b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                    num_rows);
467b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
468b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    case JCS_EXT_XRGB:
46967ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    case JCS_EXT_ARGB:
470b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
471b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                    num_rows);
472b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
473b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC    default:
474b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      gray_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
475b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC                                num_rows);
476b4570bbf8cb3045e2b3cbf3d6a80d31735871d10DRC      break;
4775ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  }
4785ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane}
4795ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
4805ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
4815ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane/*
482a9b646c202a2a154a125545b4347cbfd70598c9eDRC * Convert plain RGB to extended RGB
483a9b646c202a2a154a125545b4347cbfd70598c9eDRC */
484a9b646c202a2a154a125545b4347cbfd70598c9eDRC
485a9b646c202a2a154a125545b4347cbfd70598c9eDRCMETHODDEF(void)
486a9b646c202a2a154a125545b4347cbfd70598c9eDRCrgb_rgb_convert (j_decompress_ptr cinfo,
487e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPIMAGE input_buf, JDIMENSION input_row,
488e5eaf37440b8e337ab150c017df7c03faf846c51DRC                  JSAMPARRAY output_buf, int num_rows)
489a9b646c202a2a154a125545b4347cbfd70598c9eDRC{
490a9b646c202a2a154a125545b4347cbfd70598c9eDRC  switch (cinfo->out_color_space) {
491a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_RGB:
492a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_extrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
493a9b646c202a2a154a125545b4347cbfd70598c9eDRC                                  num_rows);
494a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
495a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_RGBX:
496a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_RGBA:
497a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_extrgbx_convert_internal(cinfo, input_buf, input_row, output_buf,
498a9b646c202a2a154a125545b4347cbfd70598c9eDRC                                   num_rows);
499a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
500a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_BGR:
501a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_extbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
502a9b646c202a2a154a125545b4347cbfd70598c9eDRC                                  num_rows);
503a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
504a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_BGRX:
505a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_BGRA:
506a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_extbgrx_convert_internal(cinfo, input_buf, input_row, output_buf,
507a9b646c202a2a154a125545b4347cbfd70598c9eDRC                                   num_rows);
508a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
509a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_XBGR:
510a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_ABGR:
511a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_extxbgr_convert_internal(cinfo, input_buf, input_row, output_buf,
512a9b646c202a2a154a125545b4347cbfd70598c9eDRC                                   num_rows);
513a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
514a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_XRGB:
515a9b646c202a2a154a125545b4347cbfd70598c9eDRC    case JCS_EXT_ARGB:
516a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_extxrgb_convert_internal(cinfo, input_buf, input_row, output_buf,
517a9b646c202a2a154a125545b4347cbfd70598c9eDRC                                   num_rows);
518a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
519a9b646c202a2a154a125545b4347cbfd70598c9eDRC    default:
520a9b646c202a2a154a125545b4347cbfd70598c9eDRC      rgb_rgb_convert_internal(cinfo, input_buf, input_row, output_buf,
521a9b646c202a2a154a125545b4347cbfd70598c9eDRC                               num_rows);
522a9b646c202a2a154a125545b4347cbfd70598c9eDRC      break;
523a9b646c202a2a154a125545b4347cbfd70598c9eDRC  }
524a9b646c202a2a154a125545b4347cbfd70598c9eDRC}
525a9b646c202a2a154a125545b4347cbfd70598c9eDRC
5265ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
527a9b646c202a2a154a125545b4347cbfd70598c9eDRC/*
52836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Adobe-style YCCK->CMYK conversion.
52936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same
53036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * conversion as above, while passing K (black) unchanged.
531bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * We assume build_ycc_rgb_table has been called.
5322cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
5332cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
534489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
53536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Laneycck_cmyk_convert (j_decompress_ptr cinfo,
536e5eaf37440b8e337ab150c017df7c03faf846c51DRC                   JSAMPIMAGE input_buf, JDIMENSION input_row,
537e5eaf37440b8e337ab150c017df7c03faf846c51DRC                   JSAMPARRAY output_buf, int num_rows)
5382cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
53936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
54036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  register int y, cb, cr;
54136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  register JSAMPROW outptr;
54236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  register JSAMPROW inptr0, inptr1, inptr2, inptr3;
54336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  register JDIMENSION col;
54436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JDIMENSION num_cols = cinfo->output_width;
54536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* copy these pointers into registers if possible */
5466eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register JSAMPLE *range_limit = cinfo->sample_range_limit;
5476eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register int *Crrtab = cconvert->Cr_r_tab;
5486eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register int *Cbbtab = cconvert->Cb_b_tab;
5496eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register JLONG *Crgtab = cconvert->Cr_g_tab;
5506eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  register JLONG *Cbgtab = cconvert->Cb_g_tab;
55136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  SHIFT_TEMPS
55236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
55336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  while (--num_rows >= 0) {
55436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    inptr0 = input_buf[0][input_row];
55536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    inptr1 = input_buf[1][input_row];
55636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    inptr2 = input_buf[2][input_row];
55736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    inptr3 = input_buf[3][input_row];
55836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    input_row++;
55936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    outptr = *output_buf++;
56036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    for (col = 0; col < num_cols; col++) {
56136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      y  = GETJSAMPLE(inptr0[col]);
56236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cb = GETJSAMPLE(inptr1[col]);
56336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cr = GETJSAMPLE(inptr2[col]);
5649ba2f5ed3649fb6de83d3c16e4dba1443aaca983Thomas G. Lane      /* Range-limiting is essential due to noise introduced by DCT losses. */
565e5eaf37440b8e337ab150c017df7c03faf846c51DRC      outptr[0] = range_limit[MAXJSAMPLE - (y + Crrtab[cr])];   /* red */
566e5eaf37440b8e337ab150c017df7c03faf846c51DRC      outptr[1] = range_limit[MAXJSAMPLE - (y +                 /* green */
567e5eaf37440b8e337ab150c017df7c03faf846c51DRC                              ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
568e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                                 SCALEBITS)))];
569e5eaf37440b8e337ab150c017df7c03faf846c51DRC      outptr[2] = range_limit[MAXJSAMPLE - (y + Cbbtab[cb])];   /* blue */
57036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      /* K passes through unchanged */
571e5eaf37440b8e337ab150c017df7c03faf846c51DRC      outptr[3] = inptr3[col];  /* don't need GETJSAMPLE here */
57236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      outptr += 4;
57336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
57436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
5752cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
5762cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
5772cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
57872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC/*
57972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC * RGB565 conversion
58072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC */
58172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
58272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_SHORT_565_LE(r, g, b)   ((((r) << 8) & 0xF800) |  \
58372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                                      (((g) << 3) & 0x7E0) | ((b) >> 3))
58472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_SHORT_565_BE(r, g, b)   (((r) & 0xF8) | ((g) >> 5) |  \
58572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                                      (((g) << 11) & 0xE000) |  \
58672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                                      (((b) << 5) & 0x1F00))
58772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
58872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_TWO_PIXELS_LE(l, r)     ((r << 16) | l)
58972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_TWO_PIXELS_BE(l, r)     ((l << 16) | r)
59072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
59172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_NEED_ALIGNMENT(ptr)     (((size_t)(ptr)) & 3)
59272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
593bbd9b7117192e590b5fdec1aa0a8f0b11244a4ccDRC#define WRITE_TWO_ALIGNED_PIXELS(addr, pixels)  ((*(int *)(addr)) = pixels)
59472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
59572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define DITHER_565_R(r, dither)  ((r) + ((dither) & 0xFF))
59672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define DITHER_565_G(g, dither)  ((g) + (((dither) & 0xFF) >> 1))
59772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define DITHER_565_B(b, dither)  ((b) + ((dither) & 0xFF))
59872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
59972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
60072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC/* Declarations for ordered dithering
60172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC *
60272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC * We use a 4x4 ordered dither array packed into 32 bits.  This array is
60372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC * sufficent for dithering RGB888 to RGB565.
60472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC */
60572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
60672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define DITHER_MASK       0x3
6076eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis#define DITHER_ROTATE(x)  ((((x) & 0xFF) << 24) | (((x) >> 8) & 0x00FFFFFF))
6086eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidisstatic const JLONG dither_matrix[4] = {
60972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  0x0008020A,
61072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  0x0C040E06,
61172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  0x030B0109,
61272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  0x0F070D05
61372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC};
61472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
61572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
61672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCstatic INLINE boolean is_big_endian(void)
61772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
61872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  int test_value = 1;
619bd7903e2a5584fe8d4c1103da25dff429e77c304Leon Scroggins III  if (*(char *)&test_value != 1)
62072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    return TRUE;
62172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  return FALSE;
62272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
62372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
62472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
62572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC/* Include inline routines for RGB565 conversion */
62672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
62772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_SHORT_565 PACK_SHORT_565_LE
62872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_TWO_PIXELS PACK_TWO_PIXELS_LE
62972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define ycc_rgb565_convert_internal ycc_rgb565_convert_le
63072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define ycc_rgb565D_convert_internal ycc_rgb565D_convert_le
63172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define rgb_rgb565_convert_internal rgb_rgb565_convert_le
63272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define rgb_rgb565D_convert_internal rgb_rgb565D_convert_le
63372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define gray_rgb565_convert_internal gray_rgb565_convert_le
63472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define gray_rgb565D_convert_internal gray_rgb565D_convert_le
63578df2e6115b0e579432d01cb034132cd4402a1baDRC#include "jdcol565.c"
63672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef PACK_SHORT_565
63772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef PACK_TWO_PIXELS
63872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef ycc_rgb565_convert_internal
63972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef ycc_rgb565D_convert_internal
64072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef rgb_rgb565_convert_internal
64172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef rgb_rgb565D_convert_internal
64272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef gray_rgb565_convert_internal
64372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef gray_rgb565D_convert_internal
64472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
64572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_SHORT_565 PACK_SHORT_565_BE
64672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define PACK_TWO_PIXELS PACK_TWO_PIXELS_BE
64772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define ycc_rgb565_convert_internal ycc_rgb565_convert_be
64872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define ycc_rgb565D_convert_internal ycc_rgb565D_convert_be
64972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define rgb_rgb565_convert_internal rgb_rgb565_convert_be
65072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define rgb_rgb565D_convert_internal rgb_rgb565D_convert_be
65172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define gray_rgb565_convert_internal gray_rgb565_convert_be
65272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#define gray_rgb565D_convert_internal gray_rgb565D_convert_be
65372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#include "jdcol565.c"
65472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef PACK_SHORT_565
65572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef PACK_TWO_PIXELS
65672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef ycc_rgb565_convert_internal
65772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef ycc_rgb565D_convert_internal
65872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef rgb_rgb565_convert_internal
65972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef rgb_rgb565D_convert_internal
66072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef gray_rgb565_convert_internal
66172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC#undef gray_rgb565D_convert_internal
66272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
66372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
66472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCMETHODDEF(void)
66572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCycc_rgb565_convert (j_decompress_ptr cinfo,
66672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                    JSAMPIMAGE input_buf, JDIMENSION input_row,
66772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                    JSAMPARRAY output_buf, int num_rows)
66872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
66972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  if (is_big_endian())
67072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    ycc_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
67172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  else
67272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    ycc_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
67372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
67472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
67572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
67672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCMETHODDEF(void)
67772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCycc_rgb565D_convert (j_decompress_ptr cinfo,
67872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                     JSAMPIMAGE input_buf, JDIMENSION input_row,
67972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                     JSAMPARRAY output_buf, int num_rows)
68072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
68172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  if (is_big_endian())
68272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    ycc_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
68372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  else
68472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    ycc_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
68572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
68672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
68772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
68872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCMETHODDEF(void)
68972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCrgb_rgb565_convert (j_decompress_ptr cinfo,
69072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                    JSAMPIMAGE input_buf, JDIMENSION input_row,
69172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                    JSAMPARRAY output_buf, int num_rows)
69272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
69372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  if (is_big_endian())
69472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    rgb_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
69572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  else
69672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    rgb_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
69772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
69872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
69972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
70072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCMETHODDEF(void)
70172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCrgb_rgb565D_convert (j_decompress_ptr cinfo,
70272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                     JSAMPIMAGE input_buf, JDIMENSION input_row,
70372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                     JSAMPARRAY output_buf, int num_rows)
70472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
70572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  if (is_big_endian())
70672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    rgb_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
70772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  else
70872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    rgb_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
70972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
71072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
71172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
71272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCMETHODDEF(void)
71372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCgray_rgb565_convert (j_decompress_ptr cinfo,
71472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                     JSAMPIMAGE input_buf, JDIMENSION input_row,
71572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                     JSAMPARRAY output_buf, int num_rows)
71672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
71772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  if (is_big_endian())
71872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    gray_rgb565_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
71972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  else
72072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    gray_rgb565_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
72172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
72272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
72372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC
72472a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCMETHODDEF(void)
72572a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRCgray_rgb565D_convert (j_decompress_ptr cinfo,
72672a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                      JSAMPIMAGE input_buf, JDIMENSION input_row,
72772a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC                      JSAMPARRAY output_buf, int num_rows)
72872a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC{
72972a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  if (is_big_endian())
73072a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    gray_rgb565D_convert_be(cinfo, input_buf, input_row, output_buf, num_rows);
73172a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC  else
73272a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC    gray_rgb565D_convert_le(cinfo, input_buf, input_row, output_buf, num_rows);
73372a3cc0ecd83bffad1c6e5dae90a8b405dd22c64DRC}
73478df2e6115b0e579432d01cb034132cd4402a1baDRC
73578df2e6115b0e579432d01cb034132cd4402a1baDRC
7362cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
73736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Empty method for start_pass.
7382cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
7392cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
740489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
741bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanestart_pass_dcolor (j_decompress_ptr cinfo)
7422cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
7432cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* no work needed */
7442cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
7452cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
7462cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
7472cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane/*
74836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Module initialization routine for output colorspace conversion.
7492cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane */
7502cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
751489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
75236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejinit_color_deconverter (j_decompress_ptr cinfo)
7532cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane{
75436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_cconvert_ptr cconvert;
755cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane  int ci;
756cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane
75736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cconvert = (my_cconvert_ptr)
75836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
7595de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                sizeof(my_color_deconverter));
76036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->cconvert = (struct jpeg_color_deconverter *) cconvert;
761bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cconvert->pub.start_pass = start_pass_dcolor;
76236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7632cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  /* Make sure num_components agrees with jpeg_color_space */
7642cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  switch (cinfo->jpeg_color_space) {
76536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_GRAYSCALE:
7662cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    if (cinfo->num_components != 1)
76736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
7682cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
7692cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
77036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_RGB:
77136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCbCr:
7722cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    if (cinfo->num_components != 3)
77336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
7742cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
7752cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
77636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_CMYK:
77736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_YCCK:
7782cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    if (cinfo->num_components != 4)
77936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
7802cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
7812cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
782e5eaf37440b8e337ab150c017df7c03faf846c51DRC  default:                      /* JCS_UNKNOWN can be anything */
78336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->num_components < 1)
78436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
7852cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
7862cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
7872cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
78836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  /* Set out_color_components and conversion method based on requested space.
78936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * Also clear the component_needed flags for any unused components,
79036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   * so that earlier pipeline stages can avoid useless computation.
79136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane   */
792cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane
7932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  switch (cinfo->out_color_space) {
79436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_GRAYSCALE:
79536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->out_color_components = 1;
79636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->jpeg_color_space == JCS_GRAYSCALE ||
797e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->jpeg_color_space == JCS_YCbCr) {
79836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cconvert->pub.color_convert = grayscale_convert;
799cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      /* For color->grayscale conversion, only the Y (0) component is needed */
800cc7150e281999ac2642e21f13e2c160f68b1d675Thomas G. Lane      for (ci = 1; ci < cinfo->num_components; ci++)
801e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->comp_info[ci].component_needed = FALSE;
8025829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding    } else if (cinfo->jpeg_color_space == JCS_RGB) {
8035829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      cconvert->pub.color_convert = rgb_gray_convert;
8045829cb23983cd241c48abd8ed70ff3627560c453Guido Vollbeding      build_rgb_y_table(cinfo);
80536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    } else
80636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
80736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    break;
80836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
80936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_RGB:
810f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_RGB:
811f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_RGBX:
812f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_BGR:
813f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_BGRX:
814f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_XBGR:
815f25c071eb745268452206bb633561b770c4d62eaDRC  case JCS_EXT_XRGB:
81667ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_RGBA:
81767ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_BGRA:
81867ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_ABGR:
81967ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC  case JCS_EXT_ARGB:
820f25c071eb745268452206bb633561b770c4d62eaDRC    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
82136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->jpeg_color_space == JCS_YCbCr) {
82259a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      if (jsimd_can_ycc_rgb())
82359a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        cconvert->pub.color_convert = jsimd_ycc_rgb_convert;
82459a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      else {
82559a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        cconvert->pub.color_convert = ycc_rgb_convert;
82659a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        build_ycc_rgb_table(cinfo);
82759a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      }
8285ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
8295ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane      cconvert->pub.color_convert = gray_rgb_convert;
830a9b646c202a2a154a125545b4347cbfd70598c9eDRC    } else if (cinfo->jpeg_color_space == JCS_RGB) {
831a9b646c202a2a154a125545b4347cbfd70598c9eDRC      if (rgb_red[cinfo->out_color_space] == 0 &&
832a9b646c202a2a154a125545b4347cbfd70598c9eDRC          rgb_green[cinfo->out_color_space] == 1 &&
833a9b646c202a2a154a125545b4347cbfd70598c9eDRC          rgb_blue[cinfo->out_color_space] == 2 &&
834a9b646c202a2a154a125545b4347cbfd70598c9eDRC          rgb_pixelsize[cinfo->out_color_space] == 3)
835a9b646c202a2a154a125545b4347cbfd70598c9eDRC        cconvert->pub.color_convert = null_convert;
836a9b646c202a2a154a125545b4347cbfd70598c9eDRC      else
837a9b646c202a2a154a125545b4347cbfd70598c9eDRC        cconvert->pub.color_convert = rgb_rgb_convert;
8384a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else
83936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
8402cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
8412cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
84278df2e6115b0e579432d01cb034132cd4402a1baDRC  case JCS_RGB565:
84378df2e6115b0e579432d01cb034132cd4402a1baDRC    cinfo->out_color_components = 3;
84478df2e6115b0e579432d01cb034132cd4402a1baDRC    if (cinfo->dither_mode == JDITHER_NONE) {
84578df2e6115b0e579432d01cb034132cd4402a1baDRC      if (cinfo->jpeg_color_space == JCS_YCbCr) {
846d729f4da9c86b7212912a7d59e49d061d0e61d5fDRC         if (jsimd_can_ycc_rgb565())
847d729f4da9c86b7212912a7d59e49d061d0e61d5fDRC           cconvert->pub.color_convert = jsimd_ycc_rgb565_convert;
848d729f4da9c86b7212912a7d59e49d061d0e61d5fDRC         else {
849d729f4da9c86b7212912a7d59e49d061d0e61d5fDRC           cconvert->pub.color_convert = ycc_rgb565_convert;
850d729f4da9c86b7212912a7d59e49d061d0e61d5fDRC           build_ycc_rgb_table(cinfo);
851d729f4da9c86b7212912a7d59e49d061d0e61d5fDRC        }
85278df2e6115b0e579432d01cb034132cd4402a1baDRC      } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
85378df2e6115b0e579432d01cb034132cd4402a1baDRC        cconvert->pub.color_convert = gray_rgb565_convert;
85478df2e6115b0e579432d01cb034132cd4402a1baDRC      } else if (cinfo->jpeg_color_space == JCS_RGB) {
85578df2e6115b0e579432d01cb034132cd4402a1baDRC        cconvert->pub.color_convert = rgb_rgb565_convert;
85678df2e6115b0e579432d01cb034132cd4402a1baDRC      } else
85778df2e6115b0e579432d01cb034132cd4402a1baDRC        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
85878df2e6115b0e579432d01cb034132cd4402a1baDRC    } else {
85978df2e6115b0e579432d01cb034132cd4402a1baDRC      /* only ordered dithering is supported */
86078df2e6115b0e579432d01cb034132cd4402a1baDRC      if (cinfo->jpeg_color_space == JCS_YCbCr) {
86178df2e6115b0e579432d01cb034132cd4402a1baDRC        cconvert->pub.color_convert = ycc_rgb565D_convert;
86278df2e6115b0e579432d01cb034132cd4402a1baDRC        build_ycc_rgb_table(cinfo);
86378df2e6115b0e579432d01cb034132cd4402a1baDRC      } else if (cinfo->jpeg_color_space == JCS_GRAYSCALE) {
86478df2e6115b0e579432d01cb034132cd4402a1baDRC        cconvert->pub.color_convert = gray_rgb565D_convert;
86578df2e6115b0e579432d01cb034132cd4402a1baDRC      } else if (cinfo->jpeg_color_space == JCS_RGB) {
86678df2e6115b0e579432d01cb034132cd4402a1baDRC        cconvert->pub.color_convert = rgb_rgb565D_convert;
86778df2e6115b0e579432d01cb034132cd4402a1baDRC      } else
86878df2e6115b0e579432d01cb034132cd4402a1baDRC        ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
86978df2e6115b0e579432d01cb034132cd4402a1baDRC    }
87078df2e6115b0e579432d01cb034132cd4402a1baDRC    break;
87178df2e6115b0e579432d01cb034132cd4402a1baDRC
87236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  case JCS_CMYK:
87336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->out_color_components = 4;
87436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    if (cinfo->jpeg_color_space == JCS_YCCK) {
87536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cconvert->pub.color_convert = ycck_cmyk_convert;
876bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      build_ycc_rgb_table(cinfo);
87736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    } else if (cinfo->jpeg_color_space == JCS_CMYK) {
87836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cconvert->pub.color_convert = null_convert;
8794a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    } else
88036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
8812cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
8822cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
8834a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane  default:
88436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    /* Permit null conversion to same output space */
8854a6b7303643714d495b9d26742d8a156fd120936Thomas G. Lane    if (cinfo->out_color_space == cinfo->jpeg_color_space) {
88636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cinfo->out_color_components = cinfo->num_components;
88736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      cconvert->pub.color_convert = null_convert;
888e5eaf37440b8e337ab150c017df7c03faf846c51DRC    } else                      /* unsupported non-null conversion */
88936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
8902cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane    break;
8912cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  }
8922cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane
8932cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  if (cinfo->quantize_colors)
89436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->output_components = 1; /* single colormapped output component */
8952cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane  else
89636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    cinfo->output_components = cinfo->out_color_components;
8972cbeb8abd92d5ad8a1bd415b51b3816213b15f3Thomas G. Lane}
898