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 -> RGBA32 (1x1)
72 static void YCrCB_to_RGBA32_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*4;
88 for (i
= sy
; i
> 0; i
--) {
89 for (j
= sx
; j
> 0; j
--) {
92 int add_r
, add_g
, add_b
;
95 y
= Y
[0] << SCALEBITS
;
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 r
= (y
+ add_r
) >> SCALEBITS
;
104 g
= (y
+ add_g
) >> SCALEBITS
;
106 b
= (y
+ add_b
) >> SCALEBITS
;
114 p
+= offset_to_next_row
;
124 * YCrCb -> RGBA32 (2x1)
129 static void YCrCB_to_RGBA32_2x1(struct jdec_private
*priv
, int sx
, int sy
)
131 const unsigned char *Y
, *Cb
, *Cr
;
134 int offset_to_next_row
;
137 #define ONE_HALF (1UL << (SCALEBITS-1))
138 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
144 offset_to_next_row
= priv
->bytes_per_row
[0] - 16*4;
145 for (i
= sy
; i
> 0; i
--) {
146 for (j
= sx
; j
> 0; j
-= 2) {
149 int add_r
, add_g
, add_b
;
152 y
= Y
[0] << SCALEBITS
;
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
;
161 g
= (y
+ add_g
) >> SCALEBITS
;
163 b
= (y
+ add_b
) >> SCALEBITS
;
169 y
= Y
[1] << SCALEBITS
;
170 r
= (y
+ add_r
) >> SCALEBITS
;
172 g
= (y
+ add_g
) >> SCALEBITS
;
174 b
= (y
+ add_b
) >> SCALEBITS
;
183 p
+= offset_to_next_row
;
194 * YCrCb -> RGBA32 (1x2)
201 static void YCrCB_to_RGBA32_1x2(struct jdec_private
*priv
, int sx
, int sy
)
203 const unsigned char *Y
, *Cb
, *Cr
;
204 unsigned char *p
, *p2
;
206 int offset_to_next_row
;
209 #define ONE_HALF (1UL << (SCALEBITS-1))
210 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
213 p2
= priv
->plane
[0] + priv
->bytes_per_row
[0];
217 offset_to_next_row
= 2*priv
->bytes_per_row
[0] - 8*4;
218 for (i
= sy
; i
> 0; i
-= 2) {
219 for (j
= sx
; j
> 0; j
--) {
222 int add_r
, add_g
, add_b
;
227 add_r
= FIX(1.40200) * cr
+ ONE_HALF
;
228 add_g
= - FIX(0.34414) * cb
- FIX(0.71414) * cr
+ ONE_HALF
;
229 add_b
= FIX(1.77200) * cb
+ ONE_HALF
;
231 y
= Y
[0] << SCALEBITS
;
232 r
= (y
+ add_r
) >> SCALEBITS
;
234 g
= (y
+ add_g
) >> SCALEBITS
;
236 b
= (y
+ add_b
) >> SCALEBITS
;
242 y
= Y
[8] << SCALEBITS
;
243 r
= (y
+ add_r
) >> SCALEBITS
;
245 g
= (y
+ add_g
) >> SCALEBITS
;
247 b
= (y
+ add_b
) >> SCALEBITS
;
256 p
+= offset_to_next_row
;
257 p2
+= offset_to_next_row
;
267 * YCrCb -> RGBA32 (2x2)
274 static void YCrCB_to_RGBA32_2x2(struct jdec_private
*priv
, int sx
, int sy
)
276 const unsigned char *Y
, *Cb
, *Cr
;
277 unsigned char *p
, *p2
;
279 int offset_to_next_row
;
282 #define ONE_HALF (1UL << (SCALEBITS-1))
283 #define FIX(x) ((int)((x) * (1UL<<SCALEBITS) + 0.5))
286 p2
= priv
->plane
[0] + priv
->bytes_per_row
[0];
290 offset_to_next_row
= 2*priv
->bytes_per_row
[0] - 16*4;
291 for (i
= sy
; i
> 0; i
-= 2) {
292 for (j
= sx
; i
> 0; j
-= 2) {
295 int add_r
, add_g
, add_b
;
300 add_r
= FIX(1.40200) * cr
+ ONE_HALF
;
301 add_g
= - FIX(0.34414) * cb
- FIX(0.71414) * cr
+ ONE_HALF
;
302 add_b
= FIX(1.77200) * cb
+ ONE_HALF
;
304 y
= Y
[0] << SCALEBITS
;
305 r
= (y
+ add_r
) >> SCALEBITS
;
307 g
= (y
+ add_g
) >> SCALEBITS
;
309 b
= (y
+ add_b
) >> SCALEBITS
;
315 y
= Y
[1] << SCALEBITS
;
316 r
= (y
+ add_r
) >> SCALEBITS
;
318 g
= (y
+ add_g
) >> SCALEBITS
;
320 b
= (y
+ add_b
) >> SCALEBITS
;
327 y
= Y
[16+0] << SCALEBITS
;
328 r
= (y
+ add_r
) >> SCALEBITS
;
330 g
= (y
+ add_g
) >> SCALEBITS
;
332 b
= (y
+ add_b
) >> SCALEBITS
;
338 y
= Y
[16+1] << SCALEBITS
;
339 r
= (y
+ add_r
) >> SCALEBITS
;
341 g
= (y
+ add_g
) >> SCALEBITS
;
343 b
= (y
+ add_b
) >> SCALEBITS
;
353 p
+= offset_to_next_row
;
354 p2
+= offset_to_next_row
;
363 static int initialize_rgba32(struct jdec_private
*priv
,
364 unsigned int *bytes_per_blocklines
,
365 unsigned int *bytes_per_mcu
)
367 if (!priv
->bytes_per_row
[0])
368 priv
->bytes_per_row
[0] = priv
->width
* 4;
369 if (!priv
->components
[0])
370 priv
->components
[0] = malloc(priv
->height
* priv
->bytes_per_row
[0]);
372 bytes_per_blocklines
[0] = priv
->bytes_per_row
[0] << 3;
373 bytes_per_mcu
[0] = 4*8;
375 return !priv
->components
[0];
378 static const struct tinyjpeg_colorspace format_rgba32
=
386 tinyjpeg_decode_mcu_3comp_table
,
390 const tinyjpeg_colorspace_t TINYJPEG_FMT_RGBA32
= &format_rgba32
;