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.
44 #include "tinyjpeg-internal.h"
47 * YCrCb -> YUV420P (1x1)
52 static void YCrCB_to_YUV420P_1x1(struct jdec_private
*priv
)
54 const unsigned char *s
, *y
;
63 p
+= priv
->bytes_per_row
[0];
71 for (j
=0; j
<8; j
+=2, s
+=2)
73 s
+= 8; /* Skip one line */
74 p
+= priv
->bytes_per_row
[1] - 4;
81 for (j
=0; j
<8; j
+=2, s
+=2)
83 s
+= 8; /* Skip one line */
84 p
+= priv
->bytes_per_row
[2] - 4;
89 * YCrCb -> YUV420P (2x1)
94 static void YCrCB_to_YUV420P_2x1(struct jdec_private
*priv
)
97 const unsigned char *s
, *y1
;
105 p
+= priv
->bytes_per_row
[0];
113 for (j
=0; j
<8; j
+=1, s
+=1)
115 s
+= 8; /* Skip one line */
116 p
+= priv
->bytes_per_row
[1] - 8;
123 for (j
=0; j
<8; j
+=1, s
+=1)
125 s
+= 8; /* Skip one line */
126 p
+= priv
->bytes_per_row
[2] - 8;
132 * YCrCb -> YUV420P (1x2)
139 static void YCrCB_to_YUV420P_1x2(struct jdec_private
*priv
)
141 const unsigned char *s
, *y
;
150 p
+=priv
->bytes_per_row
[0];
158 for (j
=0; j
<8; j
+=2, s
+=2)
160 p
+= priv
->bytes_per_row
[1] - 4;
167 for (j
=0; j
<8; j
+=2, s
+=2)
169 p
+= priv
->bytes_per_row
[2] - 4;
174 * YCrCb -> YUV420P (2x2)
181 static void YCrCB_to_YUV420P_2x2(struct jdec_private
*priv
)
184 const unsigned char *s
, *y1
;
192 p
+= priv
->bytes_per_row
[0];
202 p
+= priv
->bytes_per_row
[1];
211 p
+= priv
->bytes_per_row
[2];
215 static int initialize_yuv420p(struct jdec_private
*priv
,
216 unsigned int *bytes_per_blocklines
,
217 unsigned int *bytes_per_mcu
)
219 if (priv
->components
[0] == NULL
)
220 priv
->components
[0] = (uint8_t *)malloc(priv
->width
* priv
->height
);
221 if (priv
->components
[1] == NULL
)
222 priv
->components
[1] = (uint8_t *)malloc(priv
->width
* priv
->height
/4);
223 if (priv
->components
[2] == NULL
)
224 priv
->components
[2] = (uint8_t *)malloc(priv
->width
* priv
->height
/4);
225 if (!priv
->bytes_per_row
[0])
226 priv
->bytes_per_row
[0] = priv
->width
;
227 if (!priv
->bytes_per_row
[1])
228 priv
->bytes_per_row
[1] = priv
->width
/2;
229 if (!priv
->bytes_per_row
[2])
230 priv
->bytes_per_row
[2] = priv
->width
/2;
232 bytes_per_blocklines
[0] = priv
->bytes_per_row
[0];
233 bytes_per_blocklines
[1] = priv
->bytes_per_row
[1]/2;
234 bytes_per_blocklines
[2] = priv
->bytes_per_row
[2]/2;
235 bytes_per_mcu
[0] = 8;
236 bytes_per_mcu
[1] = 4;
237 bytes_per_mcu
[2] = 4;
239 /* Return nonzero on failure */
240 return !priv
->components
[0] || !priv
->components
[1] || !priv
->components
[2];
243 static const struct tinyjpeg_colorspace format_yuv420p
=
246 YCrCB_to_YUV420P_1x1
,
247 YCrCB_to_YUV420P_1x2
,
248 YCrCB_to_YUV420P_2x1
,
249 YCrCB_to_YUV420P_2x2
,
251 tinyjpeg_decode_mcu_3comp_table
,
255 const tinyjpeg_colorspace_t TINYJPEG_FMT_YUV420P
= &format_yuv420p
;