Adding upstream version 3.30~pre4.
[syslinux-debian/hramrach.git] / com32 / lib / jpeg / yuv420p.c
blob6d2bd05618f95db62ebaa54100706b6c85fef302
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:
8 *
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.
35 * yuv420p.c
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdint.h>
43 #include "tinyjpeg.h"
44 #include "tinyjpeg-internal.h"
46 /**
47 * YCrCb -> YUV420P (1x1)
48 * .---.
49 * | 1 |
50 * `---'
52 static void YCrCB_to_YUV420P_1x1(struct jdec_private *priv)
54 const unsigned char *s, *y;
55 unsigned char *p;
56 int i,j;
58 p = priv->plane[0];
59 y = priv->Y;
60 for (i=0; i<8; i++)
62 memcpy(p, y, 8);
63 p += priv->bytes_per_row[0];
64 y += 8;
67 p = priv->plane[1];
68 s = priv->Cb;
69 for (i=0; i<8; i+=2)
71 for (j=0; j<8; j+=2, s+=2)
72 *p++ = *s;
73 s += 8; /* Skip one line */
74 p += priv->bytes_per_row[1] - 4;
77 p = priv->plane[2];
78 s = priv->Cr;
79 for (i=0; i<8; i+=2)
81 for (j=0; j<8; j+=2, s+=2)
82 *p++ = *s;
83 s += 8; /* Skip one line */
84 p += priv->bytes_per_row[2] - 4;
88 /**
89 * YCrCb -> YUV420P (2x1)
90 * .-------.
91 * | 1 | 2 |
92 * `-------'
94 static void YCrCB_to_YUV420P_2x1(struct jdec_private *priv)
96 unsigned char *p;
97 const unsigned char *s, *y1;
98 int i,j;
100 p = priv->plane[0];
101 y1 = priv->Y;
102 for (i=0; i<8; i++)
104 memcpy(p, y1, 16);
105 p += priv->bytes_per_row[0];
106 y1 += 16;
109 p = priv->plane[1];
110 s = priv->Cb;
111 for (i=0; i<8; i+=2)
113 for (j=0; j<8; j+=1, s+=1)
114 *p++ = *s;
115 s += 8; /* Skip one line */
116 p += priv->bytes_per_row[1] - 8;
119 p = priv->plane[2];
120 s = priv->Cr;
121 for (i=0; i<8; i+=2)
123 for (j=0; j<8; j+=1, s+=1)
124 *p++ = *s;
125 s += 8; /* Skip one line */
126 p += priv->bytes_per_row[2] - 8;
132 * YCrCb -> YUV420P (1x2)
133 * .---.
134 * | 1 |
135 * |---|
136 * | 2 |
137 * `---'
139 static void YCrCB_to_YUV420P_1x2(struct jdec_private *priv)
141 const unsigned char *s, *y;
142 unsigned char *p;
143 int i,j;
145 p = priv->plane[0];
146 y = priv->Y;
147 for (i=0; i<16; i++)
149 memcpy(p, y, 8);
150 p+=priv->bytes_per_row[0];
151 y+=8;
154 p = priv->plane[1];
155 s = priv->Cb;
156 for (i=0; i<8; i++)
158 for (j=0; j<8; j+=2, s+=2)
159 *p++ = *s;
160 p += priv->bytes_per_row[1] - 4;
163 p = priv->plane[2];
164 s = priv->Cr;
165 for (i=0; i<8; i++)
167 for (j=0; j<8; j+=2, s+=2)
168 *p++ = *s;
169 p += priv->bytes_per_row[2] - 4;
174 * YCrCb -> YUV420P (2x2)
175 * .-------.
176 * | 1 | 2 |
177 * |---+---|
178 * | 3 | 4 |
179 * `-------'
181 static void YCrCB_to_YUV420P_2x2(struct jdec_private *priv)
183 unsigned char *p;
184 const unsigned char *s, *y1;
185 int i;
187 p = priv->plane[0];
188 y1 = priv->Y;
189 for (i=0; i<16; i++)
191 memcpy(p, y1, 16);
192 p += priv->bytes_per_row[0];
193 y1 += 16;
196 p = priv->plane[1];
197 s = priv->Cb;
198 for (i=0; i<8; i++)
200 memcpy(p, s, 8);
201 s += 8;
202 p += priv->bytes_per_row[1];
205 p = priv->plane[2];
206 s = priv->Cr;
207 for (i=0; i<8; i++)
209 memcpy(p, s, 8);
210 s += 8;
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,
252 initialize_yuv420p
255 const tinyjpeg_colorspace_t TINYJPEG_FMT_YUV420P = &format_yuv420p;