2 * Small jpeg decoder library
4 * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
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.
40 #include "tinyjpeg-internal.h"
42 /*******************************************************************************
44 * Colorspace conversion routine
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
)
67 * YCrCb -> RGB24 (1x1)
72 static void YCrCB_to_RGB24_1x1(struct jdec_private
*priv
, int sx
, int sy
)
74 const unsigned char *Y
, *Cb
, *Cr
;
77 int offset_to_next_row
;
80 #define ONE_HALF (1UL << (SCALEBITS-1))
81 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
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 add_r
, add_g
, add_b
;
94 y
= Y
[0] << SCALEBITS
;
97 add_r
= FIX(1.40200) * cr
+ ONE_HALF
;
98 add_g
= - FIX(0.34414) * cb
- FIX(0.71414) * cr
+ ONE_HALF
;
99 add_b
= FIX(1.77200) * cb
+ ONE_HALF
;
101 r
= (y
+ add_r
) >> SCALEBITS
;
103 g
= (y
+ add_g
) >> SCALEBITS
;
105 b
= (y
+ add_b
) >> SCALEBITS
;
111 p
+= offset_to_next_row
;
121 * YCrCb -> RGB24 (2x1)
126 static void YCrCB_to_RGB24_2x1(struct jdec_private
*priv
, int sx
, int sy
)
128 const unsigned char *Y
, *Cb
, *Cr
;
131 int offset_to_next_row
;
134 #define ONE_HALF (1UL << (SCALEBITS-1))
135 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
141 offset_to_next_row
= priv
->bytes_per_row
[0] - 16*3;
142 for (i
= sy
; i
> 0; i
--) {
144 for (j
= sx
; j
> 0; j
-= 2) {
147 int add_r
, add_g
, add_b
;
150 y
= Y
[0] << SCALEBITS
;
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 r
= (y
+ add_r
) >> SCALEBITS
;
159 g
= (y
+ add_g
) >> SCALEBITS
;
161 b
= (y
+ add_b
) >> SCALEBITS
;
165 y
= Y
[1] << SCALEBITS
;
166 r
= (y
+ add_r
) >> SCALEBITS
;
168 g
= (y
+ add_g
) >> SCALEBITS
;
170 b
= (y
+ add_b
) >> SCALEBITS
;
177 p
+= offset_to_next_row
;
188 * YCrCb -> RGB24 (1x2)
195 static void YCrCB_to_RGB24_1x2(struct jdec_private
*priv
, int sx
, int sy
)
197 const unsigned char *Y
, *Cb
, *Cr
;
198 unsigned char *p
, *p2
;
200 int offset_to_next_row
;
203 #define ONE_HALF (1UL << (SCALEBITS-1))
204 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
207 p2
= priv
->plane
[0] + priv
->bytes_per_row
[0];
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
--) {
216 int add_r
, add_g
, add_b
;
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 r
= (y
+ add_r
) >> SCALEBITS
;
228 g
= (y
+ add_g
) >> SCALEBITS
;
230 b
= (y
+ add_b
) >> SCALEBITS
;
234 y
= Y
[8] << SCALEBITS
;
235 r
= (y
+ add_r
) >> SCALEBITS
;
237 g
= (y
+ add_g
) >> SCALEBITS
;
239 b
= (y
+ add_b
) >> SCALEBITS
;
246 p
+= offset_to_next_row
;
247 p2
+= offset_to_next_row
;
257 * YCrCb -> RGB24 (2x2)
264 static void YCrCB_to_RGB24_2x2(struct jdec_private
*priv
, int sx
, int sy
)
266 const unsigned char *Y
, *Cb
, *Cr
;
267 unsigned char *p
, *p2
;
269 int offset_to_next_row
;
272 #define ONE_HALF (1UL << (SCALEBITS-1))
273 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
276 p2
= priv
->plane
[0] + priv
->bytes_per_row
[0];
280 offset_to_next_row
= 2*priv
->bytes_per_row
[0] - 16*3;
281 for (i
= sy
; i
> 0; i
-= 2) {
282 for (j
= sx
; j
> 0; j
-= 2) {
285 int add_r
, add_g
, add_b
;
290 add_r
= FIX(1.40200) * cr
+ ONE_HALF
;
291 add_g
= - FIX(0.34414) * cb
- FIX(0.71414) * cr
+ ONE_HALF
;
292 add_b
= FIX(1.77200) * cb
+ ONE_HALF
;
294 y
= Y
[0] << SCALEBITS
;
295 r
= (y
+ add_r
) >> SCALEBITS
;
297 g
= (y
+ add_g
) >> SCALEBITS
;
299 b
= (y
+ add_b
) >> SCALEBITS
;
303 y
= Y
[1] << SCALEBITS
;
304 r
= (y
+ add_r
) >> SCALEBITS
;
306 g
= (y
+ add_g
) >> SCALEBITS
;
308 b
= (y
+ add_b
) >> SCALEBITS
;
313 y
= Y
[16+0] << SCALEBITS
;
314 r
= (y
+ add_r
) >> SCALEBITS
;
316 g
= (y
+ add_g
) >> SCALEBITS
;
318 b
= (y
+ add_b
) >> SCALEBITS
;
322 y
= Y
[16+1] << SCALEBITS
;
323 r
= (y
+ add_r
) >> SCALEBITS
;
325 g
= (y
+ add_g
) >> SCALEBITS
;
327 b
= (y
+ add_b
) >> SCALEBITS
;
335 p
+= offset_to_next_row
;
336 p2
+= offset_to_next_row
;
345 static int initialize_rgb24(struct jdec_private
*priv
,
346 unsigned int *bytes_per_blocklines
,
347 unsigned int *bytes_per_mcu
)
349 if (!priv
->bytes_per_row
[0])
350 priv
->bytes_per_row
[0] = priv
->width
* 3;
351 if (priv
->components
[0] == NULL
)
352 priv
->components
[0] = malloc(priv
->height
* priv
->bytes_per_row
[0]);
354 bytes_per_blocklines
[0] = priv
->bytes_per_row
[0] << 3;
355 bytes_per_mcu
[0] = 3*8;
357 return !priv
->components
[0];
360 static const struct tinyjpeg_colorspace format_rgb24
=
368 tinyjpeg_decode_mcu_3comp_table
,
372 const tinyjpeg_colorspace_t TINYJPEG_FMT_RGB24
= &format_rgb24
;