Adding upstream version 3.50~pre5.
[syslinux-debian/hramrach.git] / com32 / lib / jpeg / bgr24.c
blobffdcbdf9022c28559aab4bb113ed3d6b3ed5b772
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:
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 -> BGR24 (1x1)
68 * .---.
69 * | 1 |
70 * `---'
72 static void YCrCB_to_BGR24_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*3;
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;
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);
112 p += offset_to_next_row;
115 #undef SCALEBITS
116 #undef ONE_HALF
117 #undef FIX
123 * YCrCb -> BGR24 (2x1)
124 * .-------.
125 * | 1 | 2 |
126 * `-------'
128 static void YCrCB_to_BGR24_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*3;
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;
152 cb = *Cb++ - 128;
153 cr = *Cr++ - 128;
154 add_r = FIX(1.40200) * cr + ONE_HALF;
155 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
156 add_b = FIX(1.77200) * cb + ONE_HALF;
158 y = (*Y++) << SCALEBITS;
159 b = (y + add_b) >> SCALEBITS;
160 *p++ = clamp(b);
161 g = (y + add_g) >> SCALEBITS;
162 *p++ = clamp(g);
163 r = (y + add_r) >> SCALEBITS;
164 *p++ = clamp(r);
166 y = (*Y++) << SCALEBITS;
167 b = (y + add_b) >> SCALEBITS;
168 *p++ = clamp(b);
169 g = (y + add_g) >> SCALEBITS;
170 *p++ = clamp(g);
171 r = (y + add_r) >> SCALEBITS;
172 *p++ = clamp(r);
176 p += offset_to_next_row;
179 #undef SCALEBITS
180 #undef ONE_HALF
181 #undef FIX
186 * YCrCb -> BGR24 (1x2)
187 * .---.
188 * | 1 |
189 * |---|
190 * | 2 |
191 * `---'
193 static void YCrCB_to_BGR24_1x2(struct jdec_private *priv)
195 const unsigned char *Y, *Cb, *Cr;
196 unsigned char *p, *p2;
197 int i,j;
198 int offset_to_next_row;
200 #define SCALEBITS 10
201 #define ONE_HALF (1UL << (SCALEBITS-1))
202 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
204 p = priv->plane[0];
205 p2 = priv->plane[0] + priv->bytes_per_row[0];
206 Y = priv->Y;
207 Cb = priv->Cb;
208 Cr = priv->Cr;
209 offset_to_next_row = 2*priv->bytes_per_row[0] - 8*3;
210 for (i=0; i<8; i++) {
212 for (j=0; j<8; j++) {
214 int y, cb, cr;
215 int add_r, add_g, add_b;
216 int r, g , b;
218 cb = *Cb++ - 128;
219 cr = *Cr++ - 128;
220 add_r = FIX(1.40200) * cr + ONE_HALF;
221 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
222 add_b = FIX(1.77200) * cb + ONE_HALF;
224 y = (*Y++) << SCALEBITS;
225 b = (y + add_b) >> SCALEBITS;
226 *p++ = clamp(b);
227 g = (y + add_g) >> SCALEBITS;
228 *p++ = clamp(g);
229 r = (y + add_r) >> SCALEBITS;
230 *p++ = clamp(r);
232 y = (Y[8-1]) << SCALEBITS;
233 b = (y + add_b) >> SCALEBITS;
234 *p2++ = clamp(b);
235 g = (y + add_g) >> SCALEBITS;
236 *p2++ = clamp(g);
237 r = (y + add_r) >> SCALEBITS;
238 *p2++ = clamp(r);
241 Y += 8;
242 p += offset_to_next_row;
243 p2 += offset_to_next_row;
246 #undef SCALEBITS
247 #undef ONE_HALF
248 #undef FIX
254 * YCrCb -> BGR24 (2x2)
255 * .-------.
256 * | 1 | 2 |
257 * |---+---|
258 * | 3 | 4 |
259 * `-------'
261 static void YCrCB_to_BGR24_2x2(struct jdec_private *priv)
263 const unsigned char *Y, *Cb, *Cr;
264 unsigned char *p, *p2;
265 int i,j;
266 int offset_to_next_row;
268 #define SCALEBITS 10
269 #define ONE_HALF (1UL << (SCALEBITS-1))
270 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
272 p = priv->plane[0];
273 p2 = priv->plane[0] + priv->bytes_per_row[0];
274 Y = priv->Y;
275 Cb = priv->Cb;
276 Cr = priv->Cr;
277 offset_to_next_row = 2*priv->bytes_per_row[0] - 16*3;
278 for (i=0; i<8; i++) {
280 for (j=0;j<8;j++) {
282 int y, cb, cr;
283 int add_r, add_g, add_b;
284 int r, g , b;
286 cb = *Cb++ - 128;
287 cr = *Cr++ - 128;
288 add_r = FIX(1.40200) * cr + ONE_HALF;
289 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
290 add_b = FIX(1.77200) * cb + ONE_HALF;
292 y = (*Y++) << SCALEBITS;
293 b = (y + add_b) >> SCALEBITS;
294 *p++ = clamp(b);
295 g = (y + add_g) >> SCALEBITS;
296 *p++ = clamp(g);
297 r = (y + add_r) >> SCALEBITS;
298 *p++ = clamp(r);
300 y = (*Y++) << SCALEBITS;
301 b = (y + add_b) >> SCALEBITS;
302 *p++ = clamp(b);
303 g = (y + add_g) >> SCALEBITS;
304 *p++ = clamp(g);
305 r = (y + add_r) >> SCALEBITS;
306 *p++ = clamp(r);
308 y = (Y[16-2]) << SCALEBITS;
309 b = (y + add_b) >> SCALEBITS;
310 *p2++ = clamp(b);
311 g = (y + add_g) >> SCALEBITS;
312 *p2++ = clamp(g);
313 r = (y + add_r) >> SCALEBITS;
314 *p2++ = clamp(r);
316 y = (Y[16-1]) << SCALEBITS;
317 b = (y + add_b) >> SCALEBITS;
318 *p2++ = clamp(b);
319 g = (y + add_g) >> SCALEBITS;
320 *p2++ = clamp(g);
321 r = (y + add_r) >> SCALEBITS;
322 *p2++ = clamp(r);
324 Y += 16;
325 p += offset_to_next_row;
326 p2 += offset_to_next_row;
329 #undef SCALEBITS
330 #undef ONE_HALF
331 #undef FIX
335 static int initialize_bgr24(struct jdec_private *priv,
336 unsigned int *bytes_per_blocklines,
337 unsigned int *bytes_per_mcu)
339 if (priv->components[0] == NULL)
340 priv->components[0] = (uint8_t *)malloc(priv->width * priv->height * 3);
341 if (!priv->bytes_per_row[0])
342 priv->bytes_per_row[0] = priv->width * 3;
344 bytes_per_blocklines[0] = priv->bytes_per_row[0];
345 bytes_per_mcu[0] = 3*8;
347 return !priv->components[0];
350 static const struct tinyjpeg_colorspace format_bgr24 =
353 YCrCB_to_BGR24_1x1,
354 YCrCB_to_BGR24_1x2,
355 YCrCB_to_BGR24_2x1,
356 YCrCB_to_BGR24_2x2,
358 tinyjpeg_decode_mcu_3comp_table,
359 initialize_bgr24
362 const tinyjpeg_colorspace_t TINYJPEG_FMT_BGR24 = &format_bgr24;