1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "jpeg_internal.h"
#include "jpeg.h"
#include <liboil/liboil.h>
#include <liboil/liboildebug.h>
#include <liboil/liboilcolorspace.h>
#define CLAMP(x,a,b) ((x)<(a) ? (a) : ((x)>(b) ? (b) : (x)))
static int16_t jfif_matrix[24] = {
0, 0, -8192, -8192,
16384, 0, 0, 0,
0, 16384, 16384, 16384,
0, 0, -5638, 29032,
0, 22970, -11700, 0,
0, 0, 0, 0
};
unsigned char * get_argb_444 (JpegDecoder *dec);
unsigned char * get_argb_422 (JpegDecoder *dec);
unsigned char * get_argb_422v (JpegDecoder *dec);
unsigned char * get_argb_420 (JpegDecoder *dec);
#if 0
static void imagescale2h_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2v_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int src_rowstride, int width, int height);
static void imagescale2h2v_u8 (unsigned char *dest, int d_rowstride,
unsigned char *src, int src_rowstride, int width, int height);
static void scanlinescale2_u8 (unsigned char *dest, unsigned char *src,
int len);
#endif
int jpeg_decode_argb (uint8_t *data, int length, uint32_t **image,
int *width, int *height)
{
JpegDecoder *dec;
int ret;
dec = jpeg_decoder_new();
jpeg_decoder_addbits (dec, data, length);
ret = jpeg_decoder_decode(dec);
if (!ret) return FALSE;
jpeg_decoder_get_image_size (dec, width, height);
*image = (uint32_t *)jpeg_decoder_get_argb_image (dec);
return TRUE;
}
unsigned char *
jpeg_decoder_get_argb_image (JpegDecoder *dec)
{
if (dec->n_components == 3) {
if (dec->components[0].h_subsample == 1 &&
dec->components[0].v_subsample == 1 &&
dec->components[1].h_subsample == dec->components[2].h_subsample &&
dec->components[1].v_subsample == dec->components[2].v_subsample) {
if (dec->components[1].h_subsample == 1 &&
dec->components[1].v_subsample == 1) {
return get_argb_444 (dec);
} else if (dec->components[1].h_subsample == 2 &&
dec->components[1].v_subsample == 1) {
return get_argb_422 (dec);
} else if (dec->components[1].h_subsample == 1 &&
dec->components[1].v_subsample == 2) {
return get_argb_422v (dec);
} else if (dec->components[1].h_subsample == 2 &&
dec->components[1].v_subsample == 2) {
return get_argb_420 (dec);
}
}
}
return NULL;
}
static void
yuv_mux (uint32_t *dest, uint8_t *src_y, uint8_t *src_u, uint8_t *src_v,
int n)
{
int i;
for (i = 0; i < n; i++) {
dest[i] = oil_argb(255, src_y[i], src_u[i], src_v[i]);
}
}
static void
upsample (uint8_t *d, uint8_t *s, int n)
{
int i;
d[0] = s[0];
for (i = 0; i < n-3; i+=2) {
d[i + 1] = (3*s[i/2] + s[i/2+1] + 2)>>2;
d[i + 2] = (s[i/2] + 3*s[i/2+1] + 2)>>2;
}
if (n&1) {
i = n-3;
d[n-2] = s[n/2];
d[n-1] = s[n/2];
} else {
d[n-1] = s[n/2-1];
}
}
unsigned char *
get_argb_444 (JpegDecoder *dec)
{
uint32_t *tmp;
uint32_t *argb_image;
uint8_t *yp, *up, *vp;
uint32_t *argbp;
int j;
tmp = malloc (4 * dec->width * dec->height);
argb_image = malloc (4 * dec->width * dec->height);
yp = dec->components[0].image;
up = dec->components[1].image;
vp = dec->components[2].image;
argbp = argb_image;
for(j=0;j<dec->height;j++){
yuv_mux (tmp, yp, up, vp, dec->width);
oil_colorspace_argb(argbp, tmp, jfif_matrix, dec->width);
yp += dec->components[0].rowstride;
up += dec->components[1].rowstride;
vp += dec->components[2].rowstride;
argbp += dec->width;
}
free(tmp);
return (unsigned char *)argb_image;
}
unsigned char *
get_argb_422 (JpegDecoder *dec)
{
uint32_t *tmp;
uint8_t *tmp_u;
uint8_t *tmp_v;
uint32_t *argb_image;
uint8_t *yp, *up, *vp;
uint32_t *argbp;
int j;
tmp = malloc (4 * dec->width * dec->height);
tmp_u = malloc (dec->width);
tmp_v = malloc (dec->width);
argb_image = malloc (4 * dec->width * dec->height);
yp = dec->components[0].image;
up = dec->components[1].image;
vp = dec->components[2].image;
argbp = argb_image;
for(j=0;j<dec->height;j++){
upsample (tmp_u, up, dec->width);
upsample (tmp_v, vp, dec->width);
yuv_mux (tmp, yp, tmp_u, tmp_v, dec->width);
oil_colorspace_argb(argbp, tmp, jfif_matrix, dec->width);
yp += dec->components[0].rowstride;
up += dec->components[1].rowstride;
vp += dec->components[2].rowstride;
argbp += dec->width;
}
free(tmp);
free(tmp_u);
free(tmp_v);
return (unsigned char *)argb_image;
}
unsigned char *
get_argb_422v (JpegDecoder *dec)
{
uint32_t *tmp;
uint8_t *tmp_u;
uint8_t *tmp_v;
uint32_t *argb_image;
uint8_t *yp, *up, *vp;
uint32_t *argbp;
int halfheight;
int j;
OIL_ERROR("got here");
tmp = malloc (4 * dec->width * dec->height);
tmp_u = malloc (dec->width);
tmp_v = malloc (dec->width);
argb_image = malloc (4 * dec->width * dec->height);
yp = dec->components[0].image;
up = dec->components[1].image;
vp = dec->components[2].image;
argbp = argb_image;
halfheight = (dec->height+1)>>1;
for(j=0;j<dec->height;j++){
uint32_t weight = 192 - 128*(j&1);
oil_merge_linear_u8(tmp_u,
up + dec->components[1].rowstride * CLAMP((j-1)/2,0,halfheight-1),
up + dec->components[1].rowstride * CLAMP((j+1)/2,0,halfheight-1),
&weight, dec->width);
oil_merge_linear_u8(tmp_v,
vp + dec->components[2].rowstride * CLAMP((j-1)/2,0,halfheight-1),
vp + dec->components[2].rowstride * CLAMP((j+1)/2,0,halfheight-1),
&weight, dec->width);
yuv_mux (tmp, yp, tmp_u, tmp_v, dec->width);
oil_colorspace_argb(argbp, tmp, jfif_matrix, dec->width);
yp += dec->components[0].rowstride;
argbp += dec->width;
}
free(tmp);
free(tmp_u);
free(tmp_v);
return (unsigned char *)argb_image;
}
unsigned char *
get_argb_420 (JpegDecoder *dec)
{
uint32_t *tmp;
uint8_t *tmp_u;
uint8_t *tmp_v;
uint8_t *tmp1;
uint32_t *argb_image;
uint8_t *yp, *up, *vp;
uint32_t *argbp;
int j;
int halfwidth;
int halfheight;
halfwidth = (dec->width + 1)>>1;
tmp = malloc (4 * dec->width * dec->height);
tmp_u = malloc (dec->width);
tmp_v = malloc (dec->width);
tmp1 = malloc (halfwidth);
argb_image = malloc (4 * dec->width * dec->height);
yp = dec->components[0].image;
up = dec->components[1].image;
vp = dec->components[2].image;
argbp = argb_image;
halfheight = (dec->height+1)>>1;
for(j=0;j<dec->height;j++){
uint32_t weight = 192 - 128*(j&1);
oil_merge_linear_u8(tmp1,
up + dec->components[1].rowstride * CLAMP((j-1)/2,0,halfheight-1),
up + dec->components[1].rowstride * CLAMP((j+1)/2,0,halfheight-1),
&weight, halfwidth);
upsample (tmp_u, tmp1, dec->width);
oil_merge_linear_u8(tmp1,
vp + dec->components[2].rowstride * CLAMP((j-1)/2,0,halfheight-1),
vp + dec->components[2].rowstride * CLAMP((j+1)/2,0,halfheight-1),
&weight, halfwidth);
upsample (tmp_v, tmp1, dec->width);
yuv_mux (tmp, yp, tmp_u, tmp_v, dec->width);
oil_colorspace_argb(argbp, tmp, jfif_matrix, dec->width);
yp += dec->components[0].rowstride;
argbp += dec->width;
}
free(tmp);
free(tmp_u);
free(tmp_v);
free(tmp1);
return (unsigned char *)argb_image;
}
#if 0
int
jpeg_rgb_decoder_get_image (JpegRGBDecoder * rgbdec,
unsigned char **image, int *rowstride, int *width, int *height)
{
int i;
jpeg_decoder_get_image_size (rgbdec->dec, &rgbdec->width, &rgbdec->height);
for (i = 0; i < 3; i++) {
jpeg_decoder_get_component_ptr (rgbdec->dec, i + 1,
&rgbdec->component[i].image, &rgbdec->component[i].rowstride);
jpeg_decoder_get_component_subsampling (rgbdec->dec, i + 1,
&rgbdec->component[i].h_subsample, &rgbdec->component[i].v_subsample);
rgbdec->component[i].alloc = 0;
if (rgbdec->component[i].h_subsample > 1 ||
rgbdec->component[i].v_subsample > 1) {
unsigned char *dest;
dest = malloc (rgbdec->width * rgbdec->height);
if (rgbdec->component[i].v_subsample > 1) {
if (rgbdec->component[i].h_subsample > 1) {
imagescale2h2v_u8 (dest,
rgbdec->width,
rgbdec->component[i].image,
rgbdec->component[i].rowstride, rgbdec->width, rgbdec->height);
} else {
imagescale2v_u8 (dest,
rgbdec->width,
rgbdec->component[i].image,
rgbdec->component[i].rowstride, rgbdec->width, rgbdec->height);
}
} else {
imagescale2h_u8 (dest,
rgbdec->width,
rgbdec->component[i].image,
rgbdec->component[i].rowstride, rgbdec->width, rgbdec->height);
}
rgbdec->component[i].alloc = 1;
rgbdec->component[i].image = dest;
rgbdec->component[i].rowstride = rgbdec->width;
rgbdec->component[i].h_subsample = 1;
rgbdec->component[i].v_subsample = 1;
}
}
rgbdec->image = malloc (rgbdec->width * rgbdec->height * 4);
convert (rgbdec);
if (image)
*image = rgbdec->image;
if (rowstride)
*rowstride = rgbdec->width * 4;
if (width)
*width = rgbdec->width;
if (height)
*height = rgbdec->height;
return 0;
}
#endif
|