Allow specifying * instead of any of the MENU COLOR fields.
[syslinux.git] / com32 / lib / jpeg / rgba32.c
blob2ad91125b2fe434ccc05f639c474c28961b46d8e
1 /*
2 * Small jpeg decoder library
4 * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
5 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * - Neither the name of the author nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
39 #include "tinyjpeg.h"
40 #include "tinyjpeg-internal.h"
42 /*******************************************************************************
44 * Colorspace conversion routine
47 * Note:
48 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
49 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
50 * The conversion equations to be implemented are therefore
51 * R = Y + 1.40200 * Cr
52 * G = Y - 0.34414 * Cb - 0.71414 * Cr
53 * B = Y + 1.77200 * Cb
55 ******************************************************************************/
56 static unsigned char clamp(int i)
58 if (i<0)
59 return 0;
60 else if (i>255)
61 return 255;
62 else
63 return i;
66 /**
67 * YCrCb -> RGBA32 (1x1)
68 * .---.
69 * | 1 |
70 * `---'
72 static void YCrCB_to_RGBA32_1x1(struct jdec_private *priv)
74 const unsigned char *Y, *Cb, *Cr;
75 unsigned char *p;
76 int i,j;
77 int offset_to_next_row;
79 #define SCALEBITS 10
80 #define ONE_HALF (1UL << (SCALEBITS-1))
81 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
83 p = priv->plane[0];
84 Y = priv->Y;
85 Cb = priv->Cb;
86 Cr = priv->Cr;
87 offset_to_next_row = priv->bytes_per_row[0] - 8*4;
88 for (i=0; i<8; i++) {
90 for (j=0;j<8;j++) {
92 int y, cb, cr;
93 int add_r, add_g, add_b;
94 int r, g , b, a;
96 y = (*Y++) << SCALEBITS;
97 cb = *Cb++ - 128;
98 cr = *Cr++ - 128;
99 add_r = FIX(1.40200) * cr + ONE_HALF;
100 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
101 add_b = FIX(1.77200) * cb + ONE_HALF;
103 r = (y + add_r) >> SCALEBITS;
104 *p++ = clamp(r);
105 g = (y + add_g) >> SCALEBITS;
106 *p++ = clamp(g);
107 b = (y + add_b) >> SCALEBITS;
108 *p++ = clamp(b);
109 a = 255;
110 *p++ = a;
113 p += offset_to_next_row;
116 #undef SCALEBITS
117 #undef ONE_HALF
118 #undef FIX
123 * YCrCb -> RGBA32 (2x1)
124 * .-------.
125 * | 1 | 2 |
126 * `-------'
128 static void YCrCB_to_RGBA32_2x1(struct jdec_private *priv)
130 const unsigned char *Y, *Cb, *Cr;
131 unsigned char *p;
132 int i,j;
133 int offset_to_next_row;
135 #define SCALEBITS 10
136 #define ONE_HALF (1UL << (SCALEBITS-1))
137 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
139 p = priv->plane[0];
140 Y = priv->Y;
141 Cb = priv->Cb;
142 Cr = priv->Cr;
143 offset_to_next_row = priv->bytes_per_row[0] - 16*4;
144 for (i=0; i<8; i++) {
146 for (j=0; j<8; j++) {
148 int y, cb, cr;
149 int add_r, add_g, add_b;
150 int r, g , b, a;
152 y = (*Y++) << SCALEBITS;
153 cb = *Cb++ - 128;
154 cr = *Cr++ - 128;
155 add_r = FIX(1.40200) * cr + ONE_HALF;
156 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
157 add_b = FIX(1.77200) * cb + ONE_HALF;
159 r = (y + add_r) >> SCALEBITS;
160 *p++ = clamp(r);
161 g = (y + add_g) >> SCALEBITS;
162 *p++ = clamp(g);
163 b = (y + add_b) >> SCALEBITS;
164 *p++ = clamp(b);
165 a = 255;
166 *p++ = a;
168 y = (*Y++) << SCALEBITS;
169 r = (y + add_r) >> SCALEBITS;
170 *p++ = clamp(r);
171 g = (y + add_g) >> SCALEBITS;
172 *p++ = clamp(g);
173 b = (y + add_b) >> SCALEBITS;
174 *p++ = clamp(b);
175 a = 255;
176 *p++ = a;
179 p += offset_to_next_row;
182 #undef SCALEBITS
183 #undef ONE_HALF
184 #undef FIX
190 * YCrCb -> RGBA32 (1x2)
191 * .---.
192 * | 1 |
193 * |---|
194 * | 2 |
195 * `---'
197 static void YCrCB_to_RGBA32_1x2(struct jdec_private *priv)
199 const unsigned char *Y, *Cb, *Cr;
200 unsigned char *p, *p2;
201 int i,j;
202 int offset_to_next_row;
204 #define SCALEBITS 10
205 #define ONE_HALF (1UL << (SCALEBITS-1))
206 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
208 p = priv->plane[0];
209 p2 = priv->plane[0] + priv->bytes_per_row[0];
210 Y = priv->Y;
211 Cb = priv->Cb;
212 Cr = priv->Cr;
213 offset_to_next_row = 2*priv->bytes_per_row[0] - 8*4;
214 for (i=0; i<8; i++) {
216 for (j=0; j<8; j++) {
218 int y, cb, cr;
219 int add_r, add_g, add_b;
220 int r, g , b, a;
222 cb = *Cb++ - 128;
223 cr = *Cr++ - 128;
224 add_r = FIX(1.40200) * cr + ONE_HALF;
225 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
226 add_b = FIX(1.77200) * cb + ONE_HALF;
228 y = (*Y++) << SCALEBITS;
229 r = (y + add_r) >> SCALEBITS;
230 *p++ = clamp(r);
231 g = (y + add_g) >> SCALEBITS;
232 *p++ = clamp(g);
233 b = (y + add_b) >> SCALEBITS;
234 *p++ = clamp(b);
235 a = 255;
236 *p++ = a;
238 y = (Y[8-1]) << SCALEBITS;
239 r = (y + add_r) >> SCALEBITS;
240 *p2++ = clamp(r);
241 g = (y + add_g) >> SCALEBITS;
242 *p2++ = clamp(g);
243 b = (y + add_b) >> SCALEBITS;
244 *p2++ = clamp(b);
245 a = 255;
246 *p2++ = a;
249 Y += 8;
250 p += offset_to_next_row;
251 p2 += offset_to_next_row;
254 #undef SCALEBITS
255 #undef ONE_HALF
256 #undef FIX
261 * YCrCb -> RGBA32 (2x2)
262 * .-------.
263 * | 1 | 2 |
264 * |---+---|
265 * | 3 | 4 |
266 * `-------'
268 static void YCrCB_to_RGBA32_2x2(struct jdec_private *priv)
270 const unsigned char *Y, *Cb, *Cr;
271 unsigned char *p, *p2;
272 int i,j;
273 int offset_to_next_row;
275 #define SCALEBITS 10
276 #define ONE_HALF (1UL << (SCALEBITS-1))
277 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
279 p = priv->plane[0];
280 p2 = priv->plane[0] + priv->bytes_per_row[0];
281 Y = priv->Y;
282 Cb = priv->Cb;
283 Cr = priv->Cr;
284 offset_to_next_row = 2*priv->bytes_per_row[0] - 16*4;
285 for (i=0; i<8; i++) {
287 for (j=0;j<8;j++) {
289 int y, cb, cr;
290 int add_r, add_g, add_b;
291 int r, g , b, a;
293 cb = *Cb++ - 128;
294 cr = *Cr++ - 128;
295 add_r = FIX(1.40200) * cr + ONE_HALF;
296 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
297 add_b = FIX(1.77200) * cb + ONE_HALF;
299 y = (*Y++) << SCALEBITS;
300 r = (y + add_r) >> SCALEBITS;
301 *p++ = clamp(r);
302 g = (y + add_g) >> SCALEBITS;
303 *p++ = clamp(g);
304 b = (y + add_b) >> SCALEBITS;
305 *p++ = clamp(b);
306 a = 255;
307 *p++ = a;
309 y = (*Y++) << SCALEBITS;
310 r = (y + add_r) >> SCALEBITS;
311 *p++ = clamp(r);
312 g = (y + add_g) >> SCALEBITS;
313 *p++ = clamp(g);
314 b = (y + add_b) >> SCALEBITS;
315 *p++ = clamp(b);
316 a = 255;
317 *p++ = a;
319 y = (Y[16-2]) << SCALEBITS;
320 r = (y + add_r) >> SCALEBITS;
321 *p2++ = clamp(r);
322 g = (y + add_g) >> SCALEBITS;
323 *p2++ = clamp(g);
324 b = (y + add_b) >> SCALEBITS;
325 *p2++ = clamp(b);
326 a = 255;
327 *p2++ = a;
329 y = (Y[16-1]) << SCALEBITS;
330 r = (y + add_r) >> SCALEBITS;
331 *p2++ = clamp(r);
332 g = (y + add_g) >> SCALEBITS;
333 *p2++ = clamp(g);
334 b = (y + add_b) >> SCALEBITS;
335 *p2++ = clamp(b);
336 a = 255;
337 *p2++ = a;
339 Y += 16;
340 p += offset_to_next_row;
341 p2 += offset_to_next_row;
344 #undef SCALEBITS
345 #undef ONE_HALF
346 #undef FIX
350 static int initialize_rgba32(struct jdec_private *priv,
351 unsigned int *bytes_per_blocklines,
352 unsigned int *bytes_per_mcu)
354 if (priv->components[0] == NULL)
355 priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 4);
356 if (!priv->bytes_per_row[0])
357 priv->bytes_per_row[0] = priv->width * 4;
359 bytes_per_blocklines[0] = priv->bytes_per_row[0];
360 bytes_per_mcu[0] = 4*8;
362 return !priv->components[0];
365 static const struct tinyjpeg_colorspace format_rgba32 =
368 YCrCB_to_RGBA32_1x1,
369 YCrCB_to_RGBA32_1x2,
370 YCrCB_to_RGBA32_2x1,
371 YCrCB_to_RGBA32_2x2,
373 tinyjpeg_decode_mcu_3comp_table,
374 initialize_rgba32
377 const tinyjpeg_colorspace_t TINYJPEG_FMT_RGBA32 = &format_rgba32;