Adding upstream version 6.03~pre1+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / jpeg / bgr24.c
blob855b855fb11a3d18e16ae64a959c1957c5947567
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, int sx, int sy)
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 = sy; i > 0; i--) {
89 for (j = sx; j > 0; j--) {
91 int y, cb, cr;
92 int add_r, add_g, add_b;
93 int r, g , b;
95 y = Y[0] << SCALEBITS;
96 cb = *Cb++ - 128;
97 cr = *Cr++ - 128;
98 add_r = FIX(1.40200) * cr + ONE_HALF;
99 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
100 add_b = FIX(1.77200) * cb + ONE_HALF;
102 b = (y + add_b) >> SCALEBITS;
103 *p++ = clamp(b);
104 g = (y + add_g) >> SCALEBITS;
105 *p++ = clamp(g);
106 r = (y + add_r) >> SCALEBITS;
107 *p++ = clamp(r);
109 Y++;
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, int sx, int sy)
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 = sy; i > 0; i--) {
145 for (j = sx; j > 0; j -= 2) {
147 int y, cb, cr;
148 int add_r, add_g, add_b;
149 int r, g , b;
151 cb = *Cb++ - 128;
152 cr = *Cr++ - 128;
153 add_r = FIX(1.40200) * cr + ONE_HALF;
154 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
155 add_b = FIX(1.77200) * cb + ONE_HALF;
157 y = Y[0] << SCALEBITS;
158 b = (y + add_b) >> SCALEBITS;
159 *p++ = clamp(b);
160 g = (y + add_g) >> SCALEBITS;
161 *p++ = clamp(g);
162 r = (y + add_r) >> SCALEBITS;
163 *p++ = clamp(r);
165 if (j > 1) {
166 y = Y[1] << 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);
175 Y += 2;
178 p += offset_to_next_row;
181 #undef SCALEBITS
182 #undef ONE_HALF
183 #undef FIX
188 * YCrCb -> BGR24 (1x2)
189 * .---.
190 * | 1 |
191 * |---|
192 * | 2 |
193 * `---'
195 static void YCrCB_to_BGR24_1x2(struct jdec_private *priv, int sx, int sy)
197 const unsigned char *Y, *Cb, *Cr;
198 unsigned char *p, *p2;
199 int i,j;
200 int offset_to_next_row;
202 #define SCALEBITS 10
203 #define ONE_HALF (1UL << (SCALEBITS-1))
204 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
206 p = priv->plane[0];
207 p2 = priv->plane[0] + priv->bytes_per_row[0];
208 Y = priv->Y;
209 Cb = priv->Cb;
210 Cr = priv->Cr;
211 offset_to_next_row = 2*priv->bytes_per_row[0] - 8*3;
212 for (i = sy; i > 0; i -= 2) {
213 for (j = sx; j > 0 ; j--) {
215 int y, cb, cr;
216 int add_r, add_g, add_b;
217 int r, g , b;
219 cb = *Cb++ - 128;
220 cr = *Cr++ - 128;
221 add_r = FIX(1.40200) * cr + ONE_HALF;
222 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
223 add_b = FIX(1.77200) * cb + ONE_HALF;
225 y = Y[0] << SCALEBITS;
226 b = (y + add_b) >> SCALEBITS;
227 *p++ = clamp(b);
228 g = (y + add_g) >> SCALEBITS;
229 *p++ = clamp(g);
230 r = (y + add_r) >> SCALEBITS;
231 *p++ = clamp(r);
233 if (i > 1) {
234 y = Y[8] << SCALEBITS;
235 b = (y + add_b) >> SCALEBITS;
236 *p2++ = clamp(b);
237 g = (y + add_g) >> SCALEBITS;
238 *p2++ = clamp(g);
239 r = (y + add_r) >> SCALEBITS;
240 *p2++ = clamp(r);
243 Y++;
245 Y += 8;
246 p += offset_to_next_row;
247 p2 += offset_to_next_row;
250 #undef SCALEBITS
251 #undef ONE_HALF
252 #undef FIX
258 * YCrCb -> BGR24 (2x2)
259 * .-------.
260 * | 1 | 2 |
261 * |---+---|
262 * | 3 | 4 |
263 * `-------'
265 static void YCrCB_to_BGR24_2x2(struct jdec_private *priv, int sx, int sy)
267 const unsigned char *Y, *Cb, *Cr;
268 unsigned char *p, *p2;
269 int i,j;
270 int offset_to_next_row;
272 #define SCALEBITS 10
273 #define ONE_HALF (1UL << (SCALEBITS-1))
274 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
276 p = priv->plane[0];
277 p2 = priv->plane[0] + priv->bytes_per_row[0];
278 Y = priv->Y;
279 Cb = priv->Cb;
280 Cr = priv->Cr;
281 offset_to_next_row = 2*priv->bytes_per_row[0] - 16*3;
282 for (i = sy; i > 0; i -= 2) {
283 for (j = sx; j > 0; j -= 2) {
285 int y, cb, cr;
286 int add_r, add_g, add_b;
287 int r, g , b;
289 cb = *Cb++ - 128;
290 cr = *Cr++ - 128;
291 add_r = FIX(1.40200) * cr + ONE_HALF;
292 add_g = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;
293 add_b = FIX(1.77200) * cb + ONE_HALF;
295 y = Y[0] << SCALEBITS;
296 b = (y + add_b) >> SCALEBITS;
297 *p++ = clamp(b);
298 g = (y + add_g) >> SCALEBITS;
299 *p++ = clamp(g);
300 r = (y + add_r) >> SCALEBITS;
301 *p++ = clamp(r);
303 if (j > 1) {
304 y = Y[1] << SCALEBITS;
305 b = (y + add_b) >> SCALEBITS;
306 *p++ = clamp(b);
307 g = (y + add_g) >> SCALEBITS;
308 *p++ = clamp(g);
309 r = (y + add_r) >> SCALEBITS;
310 *p++ = clamp(r);
313 if (i > 1) {
314 y = Y[16+0] << SCALEBITS;
315 b = (y + add_b) >> SCALEBITS;
316 *p2++ = clamp(b);
317 g = (y + add_g) >> SCALEBITS;
318 *p2++ = clamp(g);
319 r = (y + add_r) >> SCALEBITS;
320 *p2++ = clamp(r);
322 if (j > 1) {
323 y = Y[16+1] << SCALEBITS;
324 b = (y + add_b) >> SCALEBITS;
325 *p2++ = clamp(b);
326 g = (y + add_g) >> SCALEBITS;
327 *p2++ = clamp(g);
328 r = (y + add_r) >> SCALEBITS;
329 *p2++ = clamp(r);
333 Y += 2;
335 Y += 16;
336 p += offset_to_next_row;
337 p2 += offset_to_next_row;
340 #undef SCALEBITS
341 #undef ONE_HALF
342 #undef FIX
346 static int initialize_bgr24(struct jdec_private *priv,
347 unsigned int *bytes_per_blocklines,
348 unsigned int *bytes_per_mcu)
350 if (!priv->bytes_per_row[0])
351 priv->bytes_per_row[0] = priv->width * 3;
352 if (!priv->components[0])
353 priv->components[0] = malloc(priv->height * priv->bytes_per_row[0]);
355 bytes_per_blocklines[0] = priv->bytes_per_row[0] << 3;
356 bytes_per_mcu[0] = 3*8;
358 return !priv->components[0];
361 static const struct tinyjpeg_colorspace format_bgr24 =
364 YCrCB_to_BGR24_1x1,
365 YCrCB_to_BGR24_1x2,
366 YCrCB_to_BGR24_2x1,
367 YCrCB_to_BGR24_2x2,
369 tinyjpeg_decode_mcu_3comp_table,
370 initialize_bgr24
373 const tinyjpeg_colorspace_t TINYJPEG_FMT_BGR24 = &format_bgr24;