Allow specifying * instead of any of the MENU COLOR fields.
[syslinux.git] / com32 / lib / jpeg / bgra32.c
blobfe6b813af0adb82959f76c3120cb832409650494
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 -> BGRA32 (1x1)
68 * .---.
69 * | 1 |
70 * `---'
72 static void YCrCB_to_BGRA32_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 b = (y + add_b) >> SCALEBITS;
104 *p++ = clamp(b);
105 g = (y + add_g) >> SCALEBITS;
106 *p++ = clamp(g);
107 r = (y + add_r) >> SCALEBITS;
108 *p++ = clamp(r);
109 a = 255;
110 *p++ = a;
114 p += offset_to_next_row;
117 #undef SCALEBITS
118 #undef ONE_HALF
119 #undef FIX
125 * YCrCb -> BGRA32 (2x1)
126 * .-------.
127 * | 1 | 2 |
128 * `-------'
130 static void YCrCB_to_BGRA32_2x1(struct jdec_private *priv)
132 const unsigned char *Y, *Cb, *Cr;
133 unsigned char *p;
134 int i,j;
135 int offset_to_next_row;
137 #define SCALEBITS 10
138 #define ONE_HALF (1UL << (SCALEBITS-1))
139 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
141 p = priv->plane[0];
142 Y = priv->Y;
143 Cb = priv->Cb;
144 Cr = priv->Cr;
145 offset_to_next_row = priv->bytes_per_row[0] - 16*4;
146 for (i=0; i<8; i++) {
148 for (j=0; j<8; j++) {
150 int y, cb, cr;
151 int add_r, add_g, add_b;
152 int r, g , b, a;
154 cb = *Cb++ - 128;
155 cr = *Cr++ - 128;
156 add_r = FIX(1.40200) * cr + ONE_HALF;
157 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
158 add_b = FIX(1.77200) * cb + ONE_HALF;
160 y = (*Y++) << SCALEBITS;
161 b = (y + add_b) >> SCALEBITS;
162 *p++ = clamp(b);
163 g = (y + add_g) >> SCALEBITS;
164 *p++ = clamp(g);
165 r = (y + add_r) >> SCALEBITS;
166 *p++ = clamp(r);
167 a = 255;
168 *p++ = a;
170 y = (*Y++) << SCALEBITS;
171 b = (y + add_b) >> SCALEBITS;
172 *p++ = clamp(b);
173 g = (y + add_g) >> SCALEBITS;
174 *p++ = clamp(g);
175 r = (y + add_r) >> SCALEBITS;
176 *p++ = clamp(r);
177 a = 255;
178 *p++ = a;
182 p += offset_to_next_row;
185 #undef SCALEBITS
186 #undef ONE_HALF
187 #undef FIX
192 * YCrCb -> BGRA32 (1x2)
193 * .---.
194 * | 1 |
195 * |---|
196 * | 2 |
197 * `---'
199 static void YCrCB_to_BGRA32_1x2(struct jdec_private *priv)
201 const unsigned char *Y, *Cb, *Cr;
202 unsigned char *p, *p2;
203 int i,j;
204 int offset_to_next_row;
206 #define SCALEBITS 10
207 #define ONE_HALF (1UL << (SCALEBITS-1))
208 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
210 p = priv->plane[0];
211 p2 = priv->plane[0] + priv->bytes_per_row[0];
212 Y = priv->Y;
213 Cb = priv->Cb;
214 Cr = priv->Cr;
215 offset_to_next_row = 2*priv->bytes_per_row[0] - 8*4;
216 for (i=0; i<8; i++) {
218 for (j=0; j<8; j++) {
220 int y, cb, cr;
221 int add_r, add_g, add_b;
222 int r, g , b, a;
224 cb = *Cb++ - 128;
225 cr = *Cr++ - 128;
226 add_r = FIX(1.40200) * cr + ONE_HALF;
227 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
228 add_b = FIX(1.77200) * cb + ONE_HALF;
230 y = (*Y++) << SCALEBITS;
231 b = (y + add_b) >> SCALEBITS;
232 *p++ = clamp(b);
233 g = (y + add_g) >> SCALEBITS;
234 *p++ = clamp(g);
235 r = (y + add_r) >> SCALEBITS;
236 *p++ = clamp(r);
237 a = 255;
238 *p++ = a;
240 y = (Y[8-1]) << SCALEBITS;
241 b = (y + add_b) >> SCALEBITS;
242 *p2++ = clamp(b);
243 g = (y + add_g) >> SCALEBITS;
244 *p2++ = clamp(g);
245 r = (y + add_r) >> SCALEBITS;
246 *p2++ = clamp(r);
247 a = 255;
248 *p2++ = a;
251 Y += 8;
252 p += offset_to_next_row;
253 p2 += offset_to_next_row;
256 #undef SCALEBITS
257 #undef ONE_HALF
258 #undef FIX
264 * YCrCb -> BGRA32 (2x2)
265 * .-------.
266 * | 1 | 2 |
267 * |---+---|
268 * | 3 | 4 |
269 * `-------'
271 static void YCrCB_to_BGRA32_2x2(struct jdec_private *priv)
273 const unsigned char *Y, *Cb, *Cr;
274 unsigned char *p, *p2;
275 int i,j;
276 int offset_to_next_row;
278 #define SCALEBITS 10
279 #define ONE_HALF (1UL << (SCALEBITS-1))
280 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
282 p = priv->plane[0];
283 p2 = priv->plane[0] + priv->bytes_per_row[0];
284 Y = priv->Y;
285 Cb = priv->Cb;
286 Cr = priv->Cr;
287 offset_to_next_row = 2*priv->bytes_per_row[0] - 16*4;
288 for (i=0; i<8; i++) {
290 for (j=0;j<8;j++) {
292 int y, cb, cr;
293 int add_r, add_g, add_b;
294 int r, g , b, a;
296 cb = *Cb++ - 128;
297 cr = *Cr++ - 128;
298 add_r = FIX(1.40200) * cr + ONE_HALF;
299 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
300 add_b = FIX(1.77200) * cb + ONE_HALF;
302 y = (*Y++) << SCALEBITS;
303 b = (y + add_b) >> SCALEBITS;
304 *p++ = clamp(b);
305 g = (y + add_g) >> SCALEBITS;
306 *p++ = clamp(g);
307 r = (y + add_r) >> SCALEBITS;
308 *p++ = clamp(r);
309 a = 255;
310 *p++ = a;
312 y = (*Y++) << SCALEBITS;
313 b = (y + add_b) >> SCALEBITS;
314 *p++ = clamp(b);
315 g = (y + add_g) >> SCALEBITS;
316 *p++ = clamp(g);
317 r = (y + add_r) >> SCALEBITS;
318 *p++ = clamp(r);
319 a = 255;
320 *p++ = a;
322 y = (Y[16-2]) << SCALEBITS;
323 b = (y + add_b) >> SCALEBITS;
324 *p2++ = clamp(b);
325 g = (y + add_g) >> SCALEBITS;
326 *p2++ = clamp(g);
327 r = (y + add_r) >> SCALEBITS;
328 *p2++ = clamp(r);
329 a = 255;
330 *p2++ = a;
332 y = (Y[16-1]) << SCALEBITS;
333 b = (y + add_b) >> SCALEBITS;
334 *p2++ = clamp(b);
335 g = (y + add_g) >> SCALEBITS;
336 *p2++ = clamp(g);
337 r = (y + add_r) >> SCALEBITS;
338 *p2++ = clamp(r);
339 a = 255;
340 *p2++ = a;
342 Y += 16;
343 p += offset_to_next_row;
344 p2 += offset_to_next_row;
347 #undef SCALEBITS
348 #undef ONE_HALF
349 #undef FIX
353 static int initialize_bgra32(struct jdec_private *priv,
354 unsigned int *bytes_per_blocklines,
355 unsigned int *bytes_per_mcu)
357 if (priv->components[0] == NULL)
358 priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 4);
359 if (!priv->bytes_per_row[0])
360 priv->bytes_per_row[0] = priv->width * 4;
362 bytes_per_blocklines[0] = priv->bytes_per_row[0];
363 bytes_per_mcu[0] = 4*8;
365 return !priv->components[0];
368 static const struct tinyjpeg_colorspace format_bgra32 =
371 YCrCB_to_BGRA32_1x1,
372 YCrCB_to_BGRA32_1x2,
373 YCrCB_to_BGRA32_2x1,
374 YCrCB_to_BGRA32_2x2,
376 tinyjpeg_decode_mcu_3comp_table,
377 initialize_bgra32
380 const tinyjpeg_colorspace_t TINYJPEG_FMT_BGRA32 = &format_bgra32;