Mercurial > hg > forks > libbpg
annotate bpgenc.c @ 6:690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2016 11:23:39 +0200 |
parents | 524eae707ba4 |
children | 6dc09314d44b |
rev | line source |
---|---|
0 | 1 /* |
2 * BPG encoder | |
3 * | |
4 * Copyright (c) 2014 Fabrice Bellard | |
5 * | |
6 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 * of this software and associated documentation files (the "Software"), to deal | |
8 * in the Software without restriction, including without limitation the rights | |
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 * copies of the Software, and to permit persons to whom the Software is | |
11 * furnished to do so, subject to the following conditions: | |
12 * | |
13 * The above copyright notice and this permission notice shall be included in | |
14 * all copies or substantial portions of the Software. | |
15 * | |
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 * THE SOFTWARE. | |
23 */ | |
24 #include <stdlib.h> | |
25 #include <stdio.h> | |
26 #include <string.h> | |
27 #include <inttypes.h> | |
28 #include <getopt.h> | |
29 #include <math.h> | |
30 #include <assert.h> | |
31 | |
32 #include <png.h> | |
33 #include <jpeglib.h> | |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
34 #include <tiffio.h> |
0 | 35 |
36 #include "bpgenc.h" | |
37 | |
38 typedef uint16_t PIXEL; | |
39 | |
40 static void put_ue(uint8_t **pp, uint32_t v); | |
41 | |
42 static inline int clamp_pix(int a, int pixel_max) | |
43 { | |
44 if (a < 0) | |
45 return 0; | |
46 else if (a > pixel_max) | |
47 return pixel_max; | |
48 else | |
49 return a; | |
50 } | |
51 | |
52 static inline int sub_mod_int(int a, int b, int m) | |
53 { | |
54 a -= b; | |
55 if (a < 0) | |
56 a += m; | |
57 return a; | |
58 } | |
59 | |
60 static inline int add_mod_int(int a, int b, int m) | |
61 { | |
62 a += b; | |
63 if (a >= m) | |
64 a -= m; | |
65 return a; | |
66 } | |
67 | |
68 typedef struct { | |
69 int c_shift; | |
70 int c_rnd; | |
71 int c_0_25, c_0_5, c_one; | |
72 int rgb_to_ycc[3 * 3]; | |
73 int y_one; | |
74 int y_offset; | |
75 int bit_depth; | |
76 int pixel_max; | |
77 int c_center; | |
78 } ColorConvertState; | |
79 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
80 static void convert_init(ColorConvertState *s, int in_bit_depth, |
0 | 81 int out_bit_depth, BPGColorSpaceEnum color_space, |
82 int limited_range) | |
83 { | |
84 double k_r, k_b, mult, mult_y, mult_c; | |
85 int in_pixel_max, out_pixel_max, c_shift, i; | |
86 double rgb_to_ycc[3 * 3]; | |
87 | |
88 /* XXX: could use one more bit */ | |
89 c_shift = 31 - out_bit_depth; | |
90 in_pixel_max = (1 << in_bit_depth) - 1; | |
91 out_pixel_max = (1 << out_bit_depth) - 1; | |
92 mult = (double)out_pixel_max * (1 << c_shift) / (double)in_pixel_max; | |
93 // printf("mult=%f c_shift=%d\n", mult, c_shift); | |
94 if (limited_range) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
95 mult_y = (double)(219 << (out_bit_depth - 8)) * (1 << c_shift) / |
0 | 96 (double)in_pixel_max; |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
97 mult_c = (double)(224 << (out_bit_depth - 8)) * (1 << c_shift) / |
0 | 98 (double)in_pixel_max; |
99 } else { | |
100 mult_y = mult; | |
101 mult_c = mult; | |
102 } | |
103 switch(color_space) { | |
104 case BPG_CS_YCbCr: | |
105 k_r = 0.299; | |
106 k_b = 0.114; | |
107 goto convert_ycc; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
108 |
0 | 109 case BPG_CS_YCbCr_BT709: |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
110 k_r = 0.2126; |
0 | 111 k_b = 0.0722; |
112 goto convert_ycc; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
113 |
0 | 114 case BPG_CS_YCbCr_BT2020: |
115 k_r = 0.2627; | |
116 k_b = 0.0593; | |
117 convert_ycc: | |
118 rgb_to_ycc[0] = k_r; | |
119 rgb_to_ycc[1] = 1 - k_r - k_b; | |
120 rgb_to_ycc[2] = k_b; | |
121 rgb_to_ycc[3] = -0.5 * k_r / (1 - k_b); | |
122 rgb_to_ycc[4] = -0.5 * (1 - k_r - k_b) / (1 - k_b); | |
123 rgb_to_ycc[5] = 0.5; | |
124 rgb_to_ycc[6] = 0.5; | |
125 rgb_to_ycc[7] = -0.5 * (1 - k_r - k_b) / (1 - k_r); | |
126 rgb_to_ycc[8] = -0.5 * k_b / (1 - k_r); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
127 |
0 | 128 for(i = 0; i < 3; i++) |
129 s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_y); | |
130 for(i = 3; i < 9; i++) | |
131 s->rgb_to_ycc[i] = lrint(rgb_to_ycc[i] * mult_c); | |
132 break; | |
133 case BPG_CS_YCgCo: | |
134 s->c_0_25 = lrint(0.25 * mult_y); | |
135 s->c_0_5 = lrint(0.5 * mult_y); | |
136 break; | |
137 default: | |
138 break; | |
139 } | |
140 | |
141 s->c_one = lrint(mult); | |
142 s->c_shift = c_shift; | |
143 s->c_rnd = (1 << (c_shift - 1)); | |
144 if (limited_range) { | |
145 s->y_offset = s->c_rnd + (16 << (c_shift + out_bit_depth - 8)); | |
146 s->y_one = lrint(mult_y); | |
147 } else { | |
148 s->y_offset = s->c_rnd; | |
149 s->y_one = s->c_one; | |
150 } | |
151 | |
152 s->bit_depth = out_bit_depth; | |
153 s->c_center = 1 << (out_bit_depth - 1); | |
154 s->pixel_max = out_pixel_max; | |
155 } | |
156 | |
157 /* 8 bit input */ | |
158 static void rgb24_to_ycc(ColorConvertState *s, | |
159 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, | |
160 const void *src1, int n, int incr) | |
161 { | |
162 const uint8_t *src = src1; | |
163 int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center; | |
164 int pixel_max, y_offset; | |
165 | |
166 c0 = s->rgb_to_ycc[0]; | |
167 c1 = s->rgb_to_ycc[1]; | |
168 c2 = s->rgb_to_ycc[2]; | |
169 c3 = s->rgb_to_ycc[3]; | |
170 c4 = s->rgb_to_ycc[4]; | |
171 c5 = s->rgb_to_ycc[5]; | |
172 c6 = s->rgb_to_ycc[6]; | |
173 c7 = s->rgb_to_ycc[7]; | |
174 c8 = s->rgb_to_ycc[8]; | |
175 shift = s->c_shift; | |
176 rnd = s->c_rnd; | |
177 y_offset = s->y_offset; | |
178 center = s->c_center; | |
179 pixel_max = s->pixel_max; | |
180 for(i = 0; i < n; i++) { | |
181 r = src[0]; | |
182 g = src[1]; | |
183 b = src[2]; | |
184 y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b + | |
185 y_offset) >> shift, pixel_max); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
186 cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b + |
0 | 187 rnd) >> shift) + center, pixel_max); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
188 cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b + |
0 | 189 rnd) >> shift) + center, pixel_max); |
190 src += incr; | |
191 } | |
192 } | |
193 | |
194 static void rgb24_to_rgb(ColorConvertState *s, | |
195 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, | |
196 const void *src1, int n, int incr) | |
197 { | |
198 const uint8_t *src = src1; | |
199 int i, r, g, b, c, shift, rnd; | |
200 | |
201 c = s->y_one; | |
202 shift = s->c_shift; | |
203 rnd = s->y_offset; | |
204 for(i = 0; i < n; i++) { | |
205 r = src[0]; | |
206 g = src[1]; | |
207 b = src[2]; | |
208 y_ptr[i] = (c * g + rnd) >> shift; | |
209 cb_ptr[i] = (c * b + rnd) >> shift; | |
210 cr_ptr[i] = (c * r + rnd) >> shift; | |
211 src += incr; | |
212 } | |
213 } | |
214 | |
215 static void rgb24_to_ycgco(ColorConvertState *s, | |
216 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, | |
217 const void *src1, int n, int incr) | |
218 { | |
219 const uint8_t *src = src1; | |
220 int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center; | |
221 int y_offset; | |
222 | |
223 c_0_25 = s->c_0_25; | |
224 c_0_5 = s->c_0_5; | |
225 rnd = s->c_rnd; | |
226 shift = s->c_shift; | |
227 pixel_max = s->pixel_max; | |
228 center = s->c_center; | |
229 y_offset = s->y_offset; | |
230 for(i = 0; i < n; i++) { | |
231 r = src[0]; | |
232 g = src[1]; | |
233 b = src[2]; | |
234 t1 = c_0_5 * g; | |
235 t2 = c_0_25 * (r + b); | |
236 y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
237 cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center, |
0 | 238 pixel_max); |
239 cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) + | |
240 rnd) >> shift) + center, pixel_max); | |
241 src += incr; | |
242 } | |
243 } | |
244 | |
245 /* Note: used for alpha/W so no limited range */ | |
246 static void gray8_to_gray(ColorConvertState *s, | |
247 PIXEL *y_ptr, const uint8_t *src, int n, int incr) | |
248 { | |
249 int i, g, c, shift, rnd; | |
250 | |
251 c = s->c_one; | |
252 shift = s->c_shift; | |
253 rnd = s->c_rnd; | |
254 for(i = 0; i < n; i++) { | |
255 g = src[0]; | |
256 y_ptr[i] = (c * g + rnd) >> shift; | |
257 src += incr; | |
258 } | |
259 } | |
260 | |
261 static void luma8_to_gray(ColorConvertState *s, | |
262 PIXEL *y_ptr, const uint8_t *src, int n, int incr) | |
263 { | |
264 int i, g, c, shift, rnd; | |
265 | |
266 c = s->y_one; | |
267 shift = s->c_shift; | |
268 rnd = s->y_offset; | |
269 for(i = 0; i < n; i++) { | |
270 g = src[0]; | |
271 y_ptr[i] = (c * g + rnd) >> shift; | |
272 src += incr; | |
273 } | |
274 } | |
275 | |
276 /* 16 bit input */ | |
277 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
278 static void rgb48_to_ycc(ColorConvertState *s, |
0 | 279 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, |
280 const void *src1, int n, int incr) | |
281 { | |
282 const uint16_t *src = src1; | |
283 int i, r, g, b, c0, c1, c2, c3, c4, c5, c6, c7, c8, shift, rnd, center; | |
284 int pixel_max, y_offset; | |
285 | |
286 c0 = s->rgb_to_ycc[0]; | |
287 c1 = s->rgb_to_ycc[1]; | |
288 c2 = s->rgb_to_ycc[2]; | |
289 c3 = s->rgb_to_ycc[3]; | |
290 c4 = s->rgb_to_ycc[4]; | |
291 c5 = s->rgb_to_ycc[5]; | |
292 c6 = s->rgb_to_ycc[6]; | |
293 c7 = s->rgb_to_ycc[7]; | |
294 c8 = s->rgb_to_ycc[8]; | |
295 shift = s->c_shift; | |
296 rnd = s->c_rnd; | |
297 y_offset = s->y_offset; | |
298 center = s->c_center; | |
299 pixel_max = s->pixel_max; | |
300 for(i = 0; i < n; i++) { | |
301 r = src[0]; | |
302 g = src[1]; | |
303 b = src[2]; | |
304 y_ptr[i] = clamp_pix((c0 * r + c1 * g + c2 * b + | |
305 y_offset) >> shift, pixel_max); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
306 cb_ptr[i] = clamp_pix(((c3 * r + c4 * g + c5 * b + |
0 | 307 rnd) >> shift) + center, pixel_max); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
308 cr_ptr[i] = clamp_pix(((c6 * r + c7 * g + c8 * b + |
0 | 309 rnd) >> shift) + center, pixel_max); |
310 src += incr; | |
311 } | |
312 } | |
313 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
314 static void rgb48_to_ycgco(ColorConvertState *s, |
0 | 315 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, |
316 const void *src1, int n, int incr) | |
317 { | |
318 const uint16_t *src = src1; | |
319 int i, r, g, b, t1, t2, pixel_max, c_0_5, c_0_25, rnd, shift, center; | |
320 int y_offset; | |
321 | |
322 c_0_25 = s->c_0_25; | |
323 c_0_5 = s->c_0_5; | |
324 rnd = s->c_rnd; | |
325 y_offset = s->y_offset; | |
326 shift = s->c_shift; | |
327 pixel_max = s->pixel_max; | |
328 center = s->c_center; | |
329 for(i = 0; i < n; i++) { | |
330 r = src[0]; | |
331 g = src[1]; | |
332 b = src[2]; | |
333 t1 = c_0_5 * g; | |
334 t2 = c_0_25 * (r + b); | |
335 y_ptr[i] = clamp_pix((t1 + t2 + y_offset) >> shift, pixel_max); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
336 cb_ptr[i] = clamp_pix(((t1 - t2 + rnd) >> shift) + center, |
0 | 337 pixel_max); |
338 cr_ptr[i] = clamp_pix(((c_0_5 * (r - b) + | |
339 rnd) >> shift) + center, pixel_max); | |
340 src += incr; | |
341 } | |
342 } | |
343 | |
344 /* Note: use for alpha/W so no limited range */ | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
345 static void gray16_to_gray(ColorConvertState *s, |
0 | 346 PIXEL *y_ptr, const uint16_t *src, int n, int incr) |
347 { | |
348 int i, g, c, shift, rnd; | |
349 | |
350 c = s->c_one; | |
351 shift = s->c_shift; | |
352 rnd = s->c_rnd; | |
353 for(i = 0; i < n; i++) { | |
354 g = src[0]; | |
355 y_ptr[i] = (c * g + rnd) >> shift; | |
356 src += incr; | |
357 } | |
358 } | |
359 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
360 static void luma16_to_gray(ColorConvertState *s, |
0 | 361 PIXEL *y_ptr, const uint16_t *src, int n, int incr) |
362 { | |
363 int i, g, c, shift, rnd; | |
364 | |
365 c = s->y_one; | |
366 shift = s->c_shift; | |
367 rnd = s->y_offset; | |
368 for(i = 0; i < n; i++) { | |
369 g = src[0]; | |
370 y_ptr[i] = (c * g + rnd) >> shift; | |
371 src += incr; | |
372 } | |
373 } | |
374 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
375 static void rgb48_to_rgb(ColorConvertState *s, |
0 | 376 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, |
377 const void *src1, int n, int incr) | |
378 { | |
379 const uint16_t *src = src1; | |
380 | |
381 luma16_to_gray(s, y_ptr, src + 1, n, incr); | |
382 luma16_to_gray(s, cb_ptr, src + 2, n, incr); | |
383 luma16_to_gray(s, cr_ptr, src + 0, n, incr); | |
384 } | |
385 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
386 typedef void RGBConvertFunc(ColorConvertState *s, |
0 | 387 PIXEL *y_ptr, PIXEL *cb_ptr, PIXEL *cr_ptr, |
388 const void *src, int n, int incr); | |
389 | |
390 static RGBConvertFunc *rgb_to_cs[2][BPG_CS_COUNT] = { | |
391 { | |
392 rgb24_to_ycc, | |
393 rgb24_to_rgb, | |
394 rgb24_to_ycgco, | |
395 rgb24_to_ycc, | |
396 rgb24_to_ycc, | |
397 }, | |
398 { | |
399 rgb48_to_ycc, | |
400 rgb48_to_rgb, | |
401 rgb48_to_ycgco, | |
402 rgb48_to_ycc, | |
403 rgb48_to_ycc, | |
404 } | |
405 }; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
406 |
0 | 407 /* val = 1.0 - val */ |
408 static void gray_one_minus(ColorConvertState *s, PIXEL *y_ptr, int n) | |
409 { | |
410 int pixel_max = s->pixel_max; | |
411 int i; | |
412 | |
413 for(i = 0; i < n; i++) { | |
414 y_ptr[i] = pixel_max - y_ptr[i]; | |
415 } | |
416 } | |
417 | |
418 /* val = -val for chroma */ | |
419 static void gray_neg_c(ColorConvertState *s, PIXEL *y_ptr, int n) | |
420 { | |
421 int pixel_max = s->pixel_max; | |
422 int i, v; | |
423 | |
424 for(i = 0; i < n; i++) { | |
425 v = y_ptr[i]; | |
426 if (v == 0) | |
427 v = pixel_max; | |
428 else | |
429 v = pixel_max + 1 - v; | |
430 y_ptr[i] = v; | |
431 } | |
432 } | |
433 | |
434 | |
435 /* decimation */ | |
436 | |
437 /* phase = 0 */ | |
438 #define DP0TAPS2 7 | |
439 #define DP0TAPS (2 * DP0TAPS + 1) | |
440 #define DP0C0 64 | |
441 #define DP0C1 40 | |
442 #define DP0C3 (-11) | |
443 #define DP0C5 4 | |
444 #define DP0C7 (-1) | |
445 | |
446 /* phase = 0.5 */ | |
447 #define DP1TAPS2 5 | |
448 #define DP1TAPS (2 * DP1TAPS2) | |
449 #define DP1C0 57 | |
450 #define DP1C1 17 | |
451 #define DP1C2 (-8) | |
452 #define DP1C3 (-4) | |
453 #define DP1C4 2 | |
454 | |
455 #define DTAPS_MAX 7 | |
456 | |
457 /* chroma aligned with luma samples */ | |
458 static void decimate2p0_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth) | |
459 { | |
460 int n2, i, pixel_max; | |
461 pixel_max = (1 << bit_depth) - 1; | |
462 n2 = (n + 1) / 2; | |
463 for(i = 0; i < n2; i++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
464 dst[i] = clamp_pix(((src[-7] + src[7]) * DP0C7 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
465 (src[-5] + src[5]) * DP0C5 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
466 (src[-3] + src[3]) * DP0C3 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
467 (src[-1] + src[1]) * DP0C1 + |
0 | 468 src[0] * DP0C0 + 64) >> 7, pixel_max); |
469 src += 2; | |
470 } | |
471 } | |
472 | |
473 /* same with more precision and no saturation */ | |
474 static void decimate2p0_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth) | |
475 { | |
476 int n2, i, shift, rnd; | |
477 shift = bit_depth - 7; | |
478 rnd = 1 << (shift - 1); | |
479 n2 = (n + 1) / 2; | |
480 for(i = 0; i < n2; i++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
481 dst[i] = ((src[-7] + src[7]) * DP0C7 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
482 (src[-5] + src[5]) * DP0C5 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
483 (src[-3] + src[3]) * DP0C3 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
484 (src[-1] + src[1]) * DP0C1 + |
0 | 485 src[0] * DP0C0 + rnd) >> shift; |
486 src += 2; | |
487 } | |
488 } | |
489 | |
490 | |
491 /* chroma half way between luma samples */ | |
492 static void decimate2p1_simple(PIXEL *dst, PIXEL *src, int n, int bit_depth) | |
493 { | |
494 int n2, i, pixel_max; | |
495 pixel_max = (1 << bit_depth) - 1; | |
496 n2 = (n + 1) / 2; | |
497 for(i = 0; i < n2; i++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
498 dst[i] = clamp_pix(((src[-4] + src[5]) * DP1C4 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
499 (src[-3] + src[4]) * DP1C3 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
500 (src[-2] + src[3]) * DP1C2 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
501 (src[-1] + src[2]) * DP1C1 + |
0 | 502 (src[0] + src[1]) * DP1C0 + 64) >> 7, pixel_max); |
503 src += 2; | |
504 } | |
505 } | |
506 | |
507 /* same with more precision and no saturation */ | |
508 static void decimate2p1_simple16(int16_t *dst, PIXEL *src, int n, int bit_depth) | |
509 { | |
510 int n2, i, shift, rnd; | |
511 shift = bit_depth - 7; | |
512 rnd = 1 << (shift - 1); | |
513 n2 = (n + 1) / 2; | |
514 for(i = 0; i < n2; i++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
515 dst[i] = ((src[-4] + src[5]) * DP1C4 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
516 (src[-3] + src[4]) * DP1C3 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
517 (src[-2] + src[3]) * DP1C2 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
518 (src[-1] + src[2]) * DP1C1 + |
0 | 519 (src[0] + src[1]) * DP1C0 + rnd) >> shift; |
520 src += 2; | |
521 } | |
522 } | |
523 | |
524 static void decimate2_h(PIXEL *dst, PIXEL *src, int n, int bit_depth, int phase) | |
525 { | |
526 PIXEL *src1, v; | |
527 int d, i; | |
528 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
529 if (phase == 0) |
0 | 530 d = DP0TAPS2; |
531 else | |
532 d = DP1TAPS2; | |
533 /* add edge pixels */ | |
534 src1 = malloc(sizeof(PIXEL) * (n + 2 * d)); | |
535 v = src[0]; | |
536 for(i = 0; i < d; i++) | |
537 src1[i] = v; | |
538 memcpy(src1 + d, src, n * sizeof(PIXEL)); | |
539 v = src[n - 1]; | |
540 for(i = 0; i < d; i++) | |
541 src1[d + n + i] = v; | |
542 if (phase == 0) | |
543 decimate2p0_simple(dst, src1 + d, n, bit_depth); | |
544 else | |
545 decimate2p1_simple(dst, src1 + d, n, bit_depth); | |
546 free(src1); | |
547 } | |
548 | |
549 /* src1 is a temporary buffer of length n + 2 * DTAPS */ | |
550 static void decimate2_h16(int16_t *dst, PIXEL *src, int n, PIXEL *src1, | |
551 int bit_depth, int phase) | |
552 { | |
553 PIXEL v; | |
554 int d, i; | |
555 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
556 if (phase == 0) |
0 | 557 d = DP0TAPS2; |
558 else | |
559 d = DP1TAPS2; | |
560 /* add edge pixels */ | |
561 v = src[0]; | |
562 for(i = 0; i < d; i++) | |
563 src1[i] = v; | |
564 memcpy(src1 + d, src, n * sizeof(PIXEL)); | |
565 v = src[n - 1]; | |
566 for(i = 0; i < d; i++) | |
567 src1[d + n + i] = v; | |
568 if (phase == 0) | |
569 decimate2p0_simple16(dst, src1 + d, n, bit_depth); | |
570 else | |
571 decimate2p1_simple16(dst, src1 + d, n, bit_depth); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
572 |
0 | 573 } |
574 | |
575 static void decimate2_v(PIXEL *dst, int16_t **src, int pos, int n, | |
576 int bit_depth) | |
577 { | |
578 int16_t *src0, *src1, *src2, *src3, *src4, *src5, *srcm1, *srcm2, *srcm3, *srcm4; | |
579 int i, shift, offset, pixel_max; | |
580 | |
581 pos = sub_mod_int(pos, 4, DP1TAPS); | |
582 srcm4 = src[pos]; | |
583 pos = add_mod_int(pos, 1, DP1TAPS); | |
584 srcm3 = src[pos]; | |
585 pos = add_mod_int(pos, 1, DP1TAPS); | |
586 srcm2 = src[pos]; | |
587 pos = add_mod_int(pos, 1, DP1TAPS); | |
588 srcm1 = src[pos]; | |
589 pos = add_mod_int(pos, 1, DP1TAPS); | |
590 src0 = src[pos]; | |
591 pos = add_mod_int(pos, 1, DP1TAPS); | |
592 src1 = src[pos]; | |
593 pos = add_mod_int(pos, 1, DP1TAPS); | |
594 src2 = src[pos]; | |
595 pos = add_mod_int(pos, 1, DP1TAPS); | |
596 src3 = src[pos]; | |
597 pos = add_mod_int(pos, 1, DP1TAPS); | |
598 src4 = src[pos]; | |
599 pos = add_mod_int(pos, 1, DP1TAPS); | |
600 src5 = src[pos]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
601 |
0 | 602 shift = 21 - bit_depth; |
603 offset = 1 << (shift - 1); | |
604 pixel_max = (1 << bit_depth) - 1; | |
605 for(i = 0; i < n; i++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
606 dst[i] = clamp_pix(((srcm4[i] + src5[i]) * DP1C4 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
607 (srcm3[i] + src4[i]) * DP1C3 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
608 (srcm2[i] + src3[i]) * DP1C2 + |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
609 (srcm1[i] + src2[i]) * DP1C1 + |
0 | 610 (src0[i] + src1[i]) * DP1C0 + offset) >> shift, pixel_max); |
611 } | |
612 } | |
613 | |
614 /* Note: we do the horizontal decimation first to use less CPU cache */ | |
615 static void decimate2_hv(uint8_t *dst, int dst_linesize, | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
616 uint8_t *src, int src_linesize, |
0 | 617 int w, int h, int bit_depth, int h_phase) |
618 { | |
619 PIXEL *buf1; | |
620 int16_t *buf2[DP1TAPS]; | |
621 int w2, pos, i, y, y1, y2; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
622 |
0 | 623 w2 = (w + 1) / 2; |
624 | |
625 buf1 = malloc(sizeof(PIXEL) * (w + 2 * DTAPS_MAX)); | |
626 /* init line buffer */ | |
627 for(i = 0; i < DP1TAPS; i++) { | |
628 buf2[i] = malloc(sizeof(int16_t) * w2); | |
629 y = i; | |
630 if (y > DP1TAPS2) | |
631 y -= DP1TAPS; | |
632 if (y < 0) { | |
633 /* copy from first line */ | |
634 memcpy(buf2[i], buf2[0], sizeof(int16_t) * w2); | |
635 } else if (y >= h) { | |
636 /* copy from last line (only happens for small height) */ | |
637 memcpy(buf2[i], buf2[h - 1], sizeof(int16_t) * w2); | |
638 } else { | |
639 decimate2_h16(buf2[i], (PIXEL *)(src + src_linesize * y), w, | |
640 buf1, bit_depth, h_phase); | |
641 } | |
642 } | |
643 | |
644 for(y = 0; y < h; y++) { | |
645 pos = y % DP1TAPS; | |
646 if ((y & 1) == 0) { | |
647 /* filter one line */ | |
648 y2 = y >> 1; | |
649 decimate2_v((PIXEL *)(dst + y2 * dst_linesize), buf2, | |
650 pos, w2, bit_depth); | |
651 } | |
652 /* add a new line in the buffer */ | |
653 y1 = y + DP1TAPS2 + 1; | |
654 pos = add_mod_int(pos, DP1TAPS2 + 1, DP1TAPS); | |
655 if (y1 >= h) { | |
656 /* copy last line */ | |
657 memcpy(buf2[pos], buf2[sub_mod_int(pos, 1, DP1TAPS)], | |
658 sizeof(int16_t) * w2); | |
659 } else { | |
660 /* horizontally decimate new line */ | |
661 decimate2_h16(buf2[pos], (PIXEL *)(src + src_linesize * y1), w, | |
662 buf1, bit_depth, h_phase); | |
663 } | |
664 } | |
665 | |
666 for(i = 0; i < DP1TAPS; i++) | |
667 free(buf2[i]); | |
668 free(buf1); | |
669 } | |
670 | |
671 static void get_plane_res(Image *img, int *pw, int *ph, int i) | |
672 { | |
673 if (img->format == BPG_FORMAT_420 && (i == 1 || i == 2)) { | |
674 *pw = (img->w + 1) / 2; | |
675 *ph = (img->h + 1) / 2; | |
676 } else if (img->format == BPG_FORMAT_422 && (i == 1 || i == 2)) { | |
677 *pw = (img->w + 1) / 2; | |
678 *ph = img->h; | |
679 } else { | |
680 *pw = img->w; | |
681 *ph = img->h; | |
682 } | |
683 } | |
684 | |
685 #define W_PAD 16 | |
686 | |
687 Image *image_alloc(int w, int h, BPGImageFormatEnum format, int has_alpha, | |
688 BPGColorSpaceEnum color_space, int bit_depth) | |
689 { | |
690 Image *img; | |
691 int i, linesize, w1, h1, c_count; | |
692 | |
693 img = malloc(sizeof(Image)); | |
694 memset(img, 0, sizeof(*img)); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
695 |
0 | 696 img->w = w; |
697 img->h = h; | |
698 img->format = format; | |
699 img->has_alpha = has_alpha; | |
700 img->bit_depth = bit_depth; | |
701 img->color_space = color_space; | |
702 img->pixel_shift = 1; | |
703 img->c_h_phase = 1; | |
704 | |
705 if (img->format == BPG_FORMAT_GRAY) | |
706 c_count = 1; | |
707 else | |
708 c_count = 3; | |
709 if (has_alpha) | |
710 c_count++; | |
711 for(i = 0; i < c_count; i++) { | |
712 get_plane_res(img, &w1, &h1, i); | |
713 /* multiple of 16 pixels to add borders */ | |
714 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1); | |
715 h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
716 |
0 | 717 linesize = w1 << img->pixel_shift; |
718 img->data[i] = malloc(linesize * h1); | |
719 img->linesize[i] = linesize; | |
720 } | |
721 return img; | |
722 } | |
723 | |
724 void image_free(Image *img) | |
725 { | |
726 int i, c_count; | |
727 if (img->format == BPG_FORMAT_GRAY) | |
728 c_count = 1; | |
729 else | |
730 c_count = 3; | |
731 if (img->has_alpha) | |
732 c_count++; | |
733 for(i = 0; i < c_count; i++) | |
734 free(img->data[i]); | |
735 free(img); | |
736 } | |
737 | |
738 int image_ycc444_to_ycc422(Image *img, int h_phase) | |
739 { | |
740 uint8_t *data1; | |
741 int w1, h1, bpp, linesize1, i, y; | |
742 | |
743 if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1) | |
744 return -1; | |
745 bpp = 2; | |
746 w1 = (img->w + 1) / 2; | |
747 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1); | |
748 h1 = (img->h + (W_PAD - 1)) & ~(W_PAD - 1); | |
749 linesize1 = bpp * w1; | |
750 for(i = 1; i <= 2; i++) { | |
751 data1 = malloc(linesize1 * h1); | |
752 for(y = 0; y < img->h; y++) { | |
753 decimate2_h((PIXEL *)(data1 + y * linesize1), | |
754 (PIXEL *)(img->data[i] + y * img->linesize[i]), | |
755 img->w, img->bit_depth, h_phase); | |
756 } | |
757 free(img->data[i]); | |
758 img->data[i] = data1; | |
759 img->linesize[i] = linesize1; | |
760 } | |
761 img->format = BPG_FORMAT_422; | |
762 img->c_h_phase = h_phase; | |
763 return 0; | |
764 } | |
765 | |
766 int image_ycc444_to_ycc420(Image *img, int h_phase) | |
767 { | |
768 uint8_t *data1; | |
769 int w1, h1, bpp, linesize1, i; | |
770 | |
771 if (img->format != BPG_FORMAT_444 || img->pixel_shift != 1) | |
772 return -1; | |
773 bpp = 2; | |
774 w1 = (img->w + 1) / 2; | |
775 h1 = (img->h + 1) / 2; | |
776 w1 = (w1 + (W_PAD - 1)) & ~(W_PAD - 1); | |
777 h1 = (h1 + (W_PAD - 1)) & ~(W_PAD - 1); | |
778 linesize1 = bpp * w1; | |
779 for(i = 1; i <= 2; i++) { | |
780 data1 = malloc(linesize1 * h1); | |
781 decimate2_hv(data1, linesize1, | |
782 img->data[i], img->linesize[i], | |
783 img->w, img->h, img->bit_depth, h_phase); | |
784 free(img->data[i]); | |
785 img->data[i] = data1; | |
786 img->linesize[i] = linesize1; | |
787 } | |
788 img->format = BPG_FORMAT_420; | |
789 img->c_h_phase = h_phase; | |
790 return 0; | |
791 } | |
792 | |
793 /* duplicate right and bottom samples so that the image has a width | |
794 and height multiple of cb_size (power of two) */ | |
795 void image_pad(Image *img, int cb_size) | |
796 { | |
797 int w1, h1, x, y, c_count, c_w, c_h, c_w1, c_h1, h_shift, v_shift, c_idx; | |
798 PIXEL *ptr, v, *ptr1; | |
799 | |
800 assert(img->pixel_shift == 1); | |
801 if (cb_size <= 1) | |
802 return; | |
803 w1 = (img->w + cb_size - 1) & ~(cb_size - 1); | |
804 h1 = (img->h + cb_size - 1) & ~(cb_size - 1); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
805 |
0 | 806 if (img->format == BPG_FORMAT_GRAY) |
807 c_count = 1; | |
808 else | |
809 c_count = 3; | |
810 if (img->has_alpha) | |
811 c_count++; | |
812 for(c_idx = 0; c_idx < c_count; c_idx++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
813 if (img->format == BPG_FORMAT_420 && |
0 | 814 (c_idx == 1 || c_idx == 2)) { |
815 h_shift = 1; | |
816 v_shift = 1; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
817 } else if (img->format == BPG_FORMAT_422 && |
0 | 818 (c_idx == 1 || c_idx == 2)) { |
819 h_shift = 1; | |
820 v_shift = 0; | |
821 } else { | |
822 h_shift = 0; | |
823 v_shift = 0; | |
824 } | |
825 | |
826 c_w = (img->w + h_shift) >> h_shift; | |
827 c_h = (img->h + v_shift) >> v_shift; | |
828 c_w1 = w1 >> h_shift; | |
829 c_h1 = h1 >> v_shift; | |
830 | |
831 /* pad horizontally */ | |
832 for(y = 0; y < c_h; y++) { | |
833 ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y); | |
834 v = ptr[c_w - 1]; | |
835 for(x = c_w; x < c_w1; x++) { | |
836 ptr[x] = v; | |
837 } | |
838 } | |
839 | |
840 /* pad vertically */ | |
841 ptr1 = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * (c_h - 1)); | |
842 for(y = c_h; y < c_h1; y++) { | |
843 ptr = (PIXEL *)(img->data[c_idx] + img->linesize[c_idx] * y); | |
844 memcpy(ptr, ptr1, c_w1 * sizeof(PIXEL)); | |
845 } | |
846 } | |
847 img->w = w1; | |
848 img->h = h1; | |
849 } | |
850 | |
851 /* convert the 16 bit components to 8 bits */ | |
852 void image_convert16to8(Image *img) | |
853 { | |
854 int w, h, stride, y, x, c_count, i; | |
855 uint8_t *plane; | |
856 | |
857 if (img->bit_depth > 8 || img->pixel_shift != 1) | |
858 return; | |
859 if (img->format == BPG_FORMAT_GRAY) | |
860 c_count = 1; | |
861 else | |
862 c_count = 3; | |
863 if (img->has_alpha) | |
864 c_count++; | |
865 for(i = 0; i < c_count; i++) { | |
866 get_plane_res(img, &w, &h, i); | |
867 stride = w; | |
868 plane = malloc(stride * h); | |
869 for(y = 0; y < h; y++) { | |
870 const uint16_t *src; | |
871 uint8_t *dst; | |
872 dst = plane + stride * y; | |
873 src = (uint16_t *)(img->data[i] + img->linesize[i] * y); | |
874 for(x = 0; x < w; x++) | |
875 dst[x] = src[x]; | |
876 } | |
877 free(img->data[i]); | |
878 img->data[i] = plane; | |
879 img->linesize[i] = stride; | |
880 } | |
881 img->pixel_shift = 0; | |
882 } | |
883 | |
884 typedef struct BPGMetaData { | |
885 uint32_t tag; | |
886 uint8_t *buf; | |
887 int buf_len; | |
888 struct BPGMetaData *next; | |
889 } BPGMetaData; | |
890 | |
891 BPGMetaData *bpg_md_alloc(uint32_t tag) | |
892 { | |
893 BPGMetaData *md; | |
894 md = malloc(sizeof(BPGMetaData)); | |
895 memset(md, 0, sizeof(*md)); | |
896 md->tag = tag; | |
897 return md; | |
898 } | |
899 | |
900 void bpg_md_free(BPGMetaData *md) | |
901 { | |
902 BPGMetaData *md_next; | |
903 | |
904 while (md != NULL) { | |
905 md_next = md->next; | |
906 free(md->buf); | |
907 free(md); | |
908 md = md_next; | |
909 } | |
910 } | |
911 | |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
912 Image *read_tiff(BPGMetaData **pmd, const char *filename, |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
913 BPGColorSpaceEnum color_space, int out_bit_depth, |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
914 int limited_range, int premultiplied_alpha) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
915 { |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
916 TIFF *tif; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
917 Image *img = NULL; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
918 BPGImageFormatEnum img_format; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
919 uint8_t *buf = NULL; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
920 uint32_t img_width, img_height; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
921 int img_alpha, img_bpp; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
922 size_t img_linesize; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
923 uint16_t img_depth, img_spp, img_sfmt, img_pconfig; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
924 ColorConvertState cvt; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
925 RGBConvertFunc *convert_func; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
926 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
927 *pmd = NULL; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
928 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
929 // Open and read TIFF header etc. |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
930 if ((tif = TIFFOpen(filename, "rb")) == NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
931 return NULL; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
932 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
933 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img_width); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
934 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img_height); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
935 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &img_spp); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
936 TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &img_depth); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
937 TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &img_sfmt); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
938 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &img_pconfig); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
939 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
940 // Check basics |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
941 if ((img_depth != 8 && img_depth != 16) || img_spp > 4) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
942 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
943 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
944 img_linesize = TIFFScanlineSize(tif); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
945 img_format = BPG_FORMAT_444; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
946 img_bpp = sizeof(uint32_t) * img_depth / 8; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
947 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
948 if (img_spp > 3) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
949 { |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
950 uint16_t img_esmp, *img_esmp_types; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
951 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
952 TIFFGetField(tif, TIFFTAG_EXTRASAMPLES, &img_esmp, &img_esmp_types); |
6
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
953 img_alpha = img_esmp == 1 && img_esmp_types[0] == EXTRASAMPLE_ASSOCALPHA; |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
954 } |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
955 else |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
956 img_alpha = 0; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
957 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
958 // Allocate temporary image space |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
959 buf = (uint8_t *) _TIFFmalloc(img_linesize * img_spp); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
960 if (buf == NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
961 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
962 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
963 // Allocate target image |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
964 img = image_alloc(img_width, img_height, img_format, img_alpha, color_space, out_bit_depth); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
965 if (img == NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
966 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
967 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
968 img->limited_range = limited_range; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
969 img->premultiplied_alpha = premultiplied_alpha; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
970 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
971 convert_init(&cvt, img_depth, out_bit_depth, color_space, limited_range); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
972 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
973 int idx = img_depth == 16; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
974 convert_func = rgb_to_cs[idx][color_space]; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
975 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
976 for (int y = 0; y < img->h; y++) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
977 { |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
978 // Read TIFF image to raster |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
979 if (!TIFFReadScanline(tif, buf, y, 0)) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
980 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
981 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
982 // Convert |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
983 convert_func(&cvt, |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
984 (PIXEL *)(img->data[0] + y * img->linesize[0]), |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
985 (PIXEL *)(img->data[1] + y * img->linesize[1]), |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
986 (PIXEL *)(img->data[2] + y * img->linesize[2]), |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
987 buf, img->w, img_spp); |
6
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
988 |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
989 if (img_alpha) |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
990 { |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
991 if (idx) |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
992 { |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
993 gray16_to_gray(&cvt, |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
994 (PIXEL *)(img->data[3] + y * img->linesize[3]), |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
995 ((uint16_t *) buf) + 3, img->w, 4); |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
996 } |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
997 else |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
998 { |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
999 gray8_to_gray(&cvt, |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1000 (PIXEL *)(img->data[3] + y * img->linesize[3]), |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1001 ((uint8_t *) buf) + 3, img->w, 4); |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1002 } |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1003 } |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1004 } |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1005 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1006 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1007 err: |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1008 if (buf != NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1009 _TIFFfree(buf); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1010 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1011 TIFFClose(tif); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1012 return img; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1013 } |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1014 |
0 | 1015 Image *read_png(BPGMetaData **pmd, |
1016 FILE *f, BPGColorSpaceEnum color_space, int out_bit_depth, | |
1017 int limited_range, int premultiplied_alpha) | |
1018 { | |
1019 png_structp png_ptr; | |
1020 png_infop info_ptr; | |
1021 int bit_depth, color_type; | |
1022 Image *img; | |
1023 uint8_t **rows; | |
1024 int y, has_alpha, linesize, bpp; | |
1025 BPGImageFormatEnum format; | |
1026 ColorConvertState cvt_s, *cvt = &cvt_s; | |
1027 BPGMetaData *md, **plast_md, *first_md; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1028 |
0 | 1029 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, |
1030 NULL, NULL, NULL); | |
1031 if (png_ptr == NULL) { | |
1032 return NULL; | |
1033 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1034 |
0 | 1035 info_ptr = png_create_info_struct(png_ptr); |
1036 if (info_ptr == NULL) { | |
1037 png_destroy_read_struct(&png_ptr, NULL, NULL); | |
1038 return NULL; | |
1039 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1040 |
0 | 1041 if (setjmp(png_jmpbuf(png_ptr))) { |
1042 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); | |
1043 return NULL; | |
1044 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1045 |
0 | 1046 png_init_io(png_ptr, f); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1047 |
0 | 1048 png_read_info(png_ptr, info_ptr); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1049 |
0 | 1050 bit_depth = png_get_bit_depth(png_ptr, info_ptr); |
1051 color_type = png_get_color_type(png_ptr, info_ptr); | |
1052 | |
1053 switch (color_type) { | |
1054 case PNG_COLOR_TYPE_PALETTE: | |
1055 png_set_palette_to_rgb(png_ptr); | |
1056 bit_depth = 8; | |
1057 break; | |
1058 case PNG_COLOR_TYPE_GRAY: | |
1059 case PNG_COLOR_TYPE_GRAY_ALPHA: | |
1060 if (bit_depth < 8) { | |
1061 png_set_expand_gray_1_2_4_to_8(png_ptr); | |
1062 bit_depth = 8; | |
1063 } | |
1064 break; | |
1065 } | |
1066 assert(bit_depth == 8 || bit_depth == 16); | |
1067 | |
1068 #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__ | |
1069 if (bit_depth == 16) { | |
1070 png_set_swap(png_ptr); | |
1071 } | |
1072 #endif | |
1073 | |
1074 if (color_type == PNG_COLOR_TYPE_GRAY || | |
1075 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { | |
1076 format = BPG_FORMAT_GRAY; | |
1077 color_space = BPG_CS_YCbCr; | |
1078 } else { | |
1079 format = BPG_FORMAT_444; | |
1080 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1081 |
0 | 1082 has_alpha = (color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
1083 color_type == PNG_COLOR_TYPE_RGB_ALPHA); | |
1084 | |
1085 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { | |
1086 png_set_tRNS_to_alpha(png_ptr); | |
1087 has_alpha = 1; | |
1088 } | |
1089 | |
1090 if (premultiplied_alpha) { | |
1091 png_set_alpha_mode(png_ptr, PNG_ALPHA_ASSOCIATED, PNG_GAMMA_LINEAR); | |
1092 } | |
1093 | |
1094 img = image_alloc(png_get_image_width(png_ptr, info_ptr), | |
1095 png_get_image_height(png_ptr, info_ptr), | |
1096 format, has_alpha, color_space, | |
1097 out_bit_depth); | |
1098 img->limited_range = limited_range; | |
1099 img->premultiplied_alpha = premultiplied_alpha; | |
1100 | |
1101 rows = malloc(sizeof(rows[0]) * img->h); | |
1102 if (format == BPG_FORMAT_GRAY) | |
1103 bpp = (1 + has_alpha) * (bit_depth / 8); | |
1104 else | |
1105 bpp = (3 + has_alpha) * (bit_depth / 8); | |
1106 linesize = bpp * img->w; | |
1107 for (y = 0; y < img->h; y++) { | |
1108 rows[y] = malloc(linesize); | |
1109 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1110 |
0 | 1111 png_read_image(png_ptr, rows); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1112 |
0 | 1113 convert_init(cvt, bit_depth, out_bit_depth, color_space, limited_range); |
1114 | |
1115 if (format != BPG_FORMAT_GRAY) { | |
1116 int idx; | |
1117 RGBConvertFunc *convert_func; | |
1118 | |
1119 idx = (bit_depth == 16); | |
1120 convert_func = rgb_to_cs[idx][color_space]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1121 |
0 | 1122 for (y = 0; y < img->h; y++) { |
1123 convert_func(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]), | |
1124 (PIXEL *)(img->data[1] + y * img->linesize[1]), | |
1125 (PIXEL *)(img->data[2] + y * img->linesize[2]), | |
1126 rows[y], img->w, 3 + has_alpha); | |
1127 if (has_alpha) { | |
1128 if (idx) { | |
1129 gray16_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]), | |
1130 (uint16_t *)rows[y] + 3, img->w, 4); | |
1131 } else { | |
1132 gray8_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]), | |
1133 rows[y] + 3, img->w, 4); | |
1134 } | |
1135 } | |
1136 } | |
1137 } else { | |
1138 if (bit_depth == 16) { | |
1139 for (y = 0; y < img->h; y++) { | |
1140 luma16_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]), | |
1141 (uint16_t *)rows[y], img->w, 1 + has_alpha); | |
1142 if (has_alpha) { | |
1143 gray16_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]), | |
1144 (uint16_t *)rows[y] + 1, img->w, 2); | |
1145 } | |
1146 } | |
1147 } else { | |
1148 for (y = 0; y < img->h; y++) { | |
1149 luma8_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]), | |
1150 rows[y], img->w, 1 + has_alpha); | |
1151 if (has_alpha) { | |
1152 gray8_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]), | |
1153 rows[y] + 1, img->w, 2); | |
1154 } | |
1155 } | |
1156 } | |
1157 } | |
1158 | |
1159 for (y = 0; y < img->h; y++) { | |
1160 free(rows[y]); | |
1161 } | |
1162 free(rows); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1163 |
0 | 1164 png_read_end(png_ptr, info_ptr); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1165 |
0 | 1166 /* get the ICC profile if present */ |
1167 first_md = NULL; | |
1168 plast_md = &first_md; | |
1169 { | |
1170 png_charp name; | |
1171 int comp_type; | |
1172 png_bytep iccp_buf; | |
1173 png_uint_32 iccp_buf_len; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1174 |
0 | 1175 if (png_get_iCCP(png_ptr, info_ptr, |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1176 &name, &comp_type, &iccp_buf, &iccp_buf_len) == |
0 | 1177 PNG_INFO_iCCP) { |
1178 md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP); | |
1179 md->buf_len = iccp_buf_len; | |
1180 md->buf = malloc(iccp_buf_len); | |
1181 memcpy(md->buf, iccp_buf, iccp_buf_len); | |
1182 *plast_md = md; | |
1183 plast_md = &md->next; | |
1184 } | |
1185 } | |
1186 | |
1187 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1188 |
0 | 1189 *pmd = first_md; |
1190 return img; | |
1191 } | |
1192 | |
1193 static BPGMetaData *jpeg_get_metadata(jpeg_saved_marker_ptr first_marker) | |
1194 { | |
1195 static const char app1_exif[] = "Exif"; | |
1196 static const char app1_xmp[] = "http://ns.adobe.com/xap/1.0/"; | |
1197 static const char app2_iccp[] = "ICC_PROFILE"; | |
1198 jpeg_saved_marker_ptr marker; | |
1199 BPGMetaData *md, **plast_md, *first_md; | |
1200 int has_exif, has_xmp, l, iccp_chunk_count, i; | |
1201 jpeg_saved_marker_ptr iccp_chunks[256]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1202 |
0 | 1203 iccp_chunk_count = 0; |
1204 has_exif = 0; | |
1205 has_xmp = 0; | |
1206 first_md = NULL; | |
1207 plast_md = &first_md; | |
1208 for (marker = first_marker; marker != NULL; marker = marker->next) { | |
1209 #if 0 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1210 printf("marker=APP%d len=%d\n", |
0 | 1211 marker->marker - JPEG_APP0, marker->data_length); |
1212 #endif | |
1213 if (!has_exif && marker->marker == JPEG_APP0 + 1 && | |
1214 marker->data_length > sizeof(app1_exif) && | |
1215 !memcmp(marker->data, app1_exif, sizeof(app1_exif))) { | |
1216 md = bpg_md_alloc(BPG_EXTENSION_TAG_EXIF); | |
1217 l = sizeof(app1_exif); | |
1218 md->buf_len = marker->data_length - l; | |
1219 md->buf = malloc(md->buf_len); | |
1220 memcpy(md->buf, marker->data + l, md->buf_len); | |
1221 *plast_md = md; | |
1222 plast_md = &md->next; | |
1223 has_exif = 1; | |
1224 } else if (!has_xmp && marker->marker == JPEG_APP0 + 1 && | |
1225 marker->data_length > sizeof(app1_xmp) && | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1226 !memcmp(marker->data, app1_xmp, sizeof(app1_xmp)) && |
0 | 1227 !has_xmp) { |
1228 md = bpg_md_alloc(BPG_EXTENSION_TAG_XMP); | |
1229 l = sizeof(app1_xmp); | |
1230 md->buf_len = marker->data_length - l; | |
1231 md->buf = malloc(md->buf_len); | |
1232 memcpy(md->buf, marker->data + l, md->buf_len); | |
1233 *plast_md = md; | |
1234 plast_md = &md->next; | |
1235 has_xmp = 1; | |
1236 } else if (marker->marker == JPEG_APP0 + 2 && | |
1237 marker->data_length > (sizeof(app2_iccp) + 2) && | |
1238 !memcmp(marker->data, app2_iccp, sizeof(app2_iccp))) { | |
1239 int chunk_count, chunk_index; | |
1240 l = sizeof(app2_iccp); | |
1241 chunk_index = marker->data[l]; | |
1242 chunk_count = marker->data[l]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1243 if (chunk_index == 0 || chunk_count == 0) |
0 | 1244 continue; |
1245 if (iccp_chunk_count == 0) { | |
1246 iccp_chunk_count = chunk_count; | |
1247 for(i = 0; i < chunk_count; i++) { | |
1248 iccp_chunks[i] = NULL; | |
1249 } | |
1250 } else { | |
1251 if (chunk_count != iccp_chunk_count) | |
1252 continue; | |
1253 } | |
1254 if (chunk_index > iccp_chunk_count) | |
1255 continue; | |
1256 iccp_chunks[chunk_index - 1] = marker; | |
1257 } | |
1258 } | |
1259 | |
1260 if (iccp_chunk_count != 0) { | |
1261 int len, hlen, idx; | |
1262 /* check that no chunk are missing */ | |
1263 len = 0; | |
1264 hlen = sizeof(app2_iccp) + 2; | |
1265 for(i = 0; i < iccp_chunk_count; i++) { | |
1266 if (!iccp_chunks[i]) | |
1267 break; | |
1268 len += iccp_chunks[i]->data_length - hlen; | |
1269 } | |
1270 if (i == iccp_chunk_count) { | |
1271 md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP); | |
1272 md->buf_len = len; | |
1273 md->buf = malloc(md->buf_len); | |
1274 idx = 0; | |
1275 for(i = 0; i < iccp_chunk_count; i++) { | |
1276 l = iccp_chunks[i]->data_length - hlen; | |
1277 memcpy(md->buf + idx, iccp_chunks[i]->data + hlen, l); | |
1278 idx += l; | |
1279 } | |
1280 assert(idx == len); | |
1281 *plast_md = md; | |
1282 plast_md = &md->next; | |
1283 } | |
1284 } | |
1285 return first_md; | |
1286 } | |
1287 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1288 Image *read_jpeg(BPGMetaData **pmd, FILE *f, |
0 | 1289 int out_bit_depth) |
1290 { | |
1291 struct jpeg_decompress_struct cinfo; | |
1292 struct jpeg_error_mgr jerr; | |
1293 int w, h, w1, i, y_h, c_h, y, v_shift, c_w, y1, idx, c_idx, h_shift; | |
1294 int h1, plane_idx[4], has_alpha, has_w_plane; | |
1295 Image *img; | |
1296 BPGImageFormatEnum format; | |
1297 BPGColorSpaceEnum color_space; | |
1298 ColorConvertState cvt_s, *cvt = &cvt_s; | |
1299 BPGMetaData *first_md = NULL; | |
1300 uint32_t comp_hv; | |
1301 | |
1302 cinfo.err = jpeg_std_error(&jerr); | |
1303 jpeg_create_decompress(&cinfo); | |
1304 | |
1305 jpeg_save_markers(&cinfo, JPEG_APP0 + 1, 65535); | |
1306 jpeg_save_markers(&cinfo, JPEG_APP0 + 2, 65535); | |
1307 | |
1308 jpeg_stdio_src(&cinfo, f); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1309 |
0 | 1310 jpeg_read_header(&cinfo, TRUE); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1311 |
0 | 1312 cinfo.raw_data_out = TRUE; |
1313 cinfo.do_fancy_upsampling = TRUE; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1314 |
0 | 1315 w = cinfo.image_width; |
1316 h = cinfo.image_height; | |
1317 | |
1318 has_w_plane = 0; | |
1319 comp_hv = 0; | |
1320 if (cinfo.num_components < 1 || cinfo.num_components > 4) | |
1321 goto unsupported; | |
1322 for(i = 0; i < cinfo.num_components; i++) { | |
1323 comp_hv |= cinfo.comp_info[i].h_samp_factor << (i * 8 + 4); | |
1324 comp_hv |= cinfo.comp_info[i].v_samp_factor << (i * 8); | |
1325 } | |
1326 switch(cinfo.jpeg_color_space) { | |
1327 case JCS_GRAYSCALE: | |
1328 if (cinfo.num_components != 1 || comp_hv != 0x11) | |
1329 goto unsupported; | |
1330 format = BPG_FORMAT_GRAY; | |
1331 color_space = BPG_CS_YCbCr; | |
1332 break; | |
1333 case JCS_YCbCr: | |
1334 if (cinfo.num_components != 3) | |
1335 goto unsupported; | |
1336 switch(comp_hv) { | |
1337 case 0x111111: | |
1338 format = BPG_FORMAT_444; | |
1339 break; | |
1340 case 0x111121: | |
1341 format = BPG_FORMAT_422; | |
1342 break; | |
1343 case 0x111122: | |
1344 format = BPG_FORMAT_420; | |
1345 break; | |
1346 default: | |
1347 cinfo.raw_data_out = FALSE; | |
1348 format = BPG_FORMAT_444; | |
1349 cinfo.out_color_space = JCS_YCbCr; | |
1350 break; | |
1351 } | |
1352 color_space = BPG_CS_YCbCr; | |
1353 break; | |
1354 case JCS_RGB: | |
1355 if (cinfo.num_components != 3) | |
1356 goto unsupported; | |
1357 format = BPG_FORMAT_444; | |
1358 color_space = BPG_CS_RGB; | |
1359 cinfo.raw_data_out = FALSE; | |
1360 cinfo.out_color_space = JCS_RGB; | |
1361 break; | |
1362 case JCS_YCCK: | |
1363 if (cinfo.num_components != 4) | |
1364 goto unsupported; | |
1365 switch(comp_hv) { | |
1366 case 0x11111111: | |
1367 format = BPG_FORMAT_444; | |
1368 color_space = BPG_CS_YCbCr; | |
1369 break; | |
1370 case 0x22111121: | |
1371 format = BPG_FORMAT_422; | |
1372 color_space = BPG_CS_YCbCr; | |
1373 break; | |
1374 case 0x22111122: | |
1375 format = BPG_FORMAT_420; | |
1376 color_space = BPG_CS_YCbCr; | |
1377 break; | |
1378 default: | |
1379 cinfo.raw_data_out = FALSE; | |
1380 format = BPG_FORMAT_444; | |
1381 cinfo.out_color_space = JCS_CMYK; | |
1382 color_space = BPG_CS_RGB; | |
1383 break; | |
1384 } | |
1385 has_w_plane = 1; | |
1386 break; | |
1387 case JCS_CMYK: | |
1388 if (cinfo.num_components != 4) | |
1389 goto unsupported; | |
1390 format = BPG_FORMAT_444; | |
1391 color_space = BPG_CS_RGB; | |
1392 has_w_plane = 1; | |
1393 cinfo.raw_data_out = FALSE; | |
1394 cinfo.out_color_space = JCS_CMYK; | |
1395 break; | |
1396 default: | |
1397 unsupported: | |
1398 fprintf(stderr, "Unsupported JPEG parameters (cs=%d n_comp=%d comp_hv=%x)\n", | |
1399 cinfo.jpeg_color_space, cinfo.num_components, comp_hv); | |
1400 img = NULL; | |
1401 goto the_end; | |
1402 } | |
1403 | |
1404 v_shift = (format == BPG_FORMAT_420); | |
1405 h_shift = (format == BPG_FORMAT_422 || format == BPG_FORMAT_420); | |
1406 has_alpha = (cinfo.num_components == 4); | |
1407 img = image_alloc(w, h, format, has_alpha, color_space, out_bit_depth); | |
1408 img->has_w_plane = has_w_plane; | |
1409 | |
1410 convert_init(cvt, 8, out_bit_depth, color_space, 0); | |
1411 | |
1412 jpeg_start_decompress(&cinfo); | |
1413 | |
1414 if (color_space == BPG_CS_RGB) { | |
1415 plane_idx[0] = 2; | |
1416 plane_idx[1] = 0; | |
1417 plane_idx[2] = 1; | |
1418 } else { | |
1419 plane_idx[0] = 0; | |
1420 plane_idx[1] = 1; | |
1421 plane_idx[2] = 2; | |
1422 } | |
1423 plane_idx[3] = 3; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1424 |
0 | 1425 if (cinfo.raw_data_out) { |
1426 JSAMPROW rows[4][16]; | |
1427 JSAMPROW *plane_pointer[4]; | |
1428 | |
1429 y_h = 8 * cinfo.max_v_samp_factor; | |
1430 if (cinfo.num_components == 1) { | |
1431 c_h = 0; | |
1432 c_w = 0; | |
1433 } else { | |
1434 c_h = 8; | |
1435 if (h_shift) | |
1436 c_w = (w + 1) / 2; | |
1437 else | |
1438 c_w = w; | |
1439 } | |
1440 w1 = (w + 15) & ~15; | |
1441 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) { | |
1442 if (c_idx == 1 || c_idx == 2) { | |
1443 h1 = c_h; | |
1444 } else { | |
1445 h1 = y_h; | |
1446 } | |
1447 for(i = 0; i < h1; i++) { | |
1448 rows[c_idx][i] = malloc(w1); | |
1449 } | |
1450 plane_pointer[c_idx] = rows[c_idx]; | |
1451 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1452 |
0 | 1453 while (cinfo.output_scanline < cinfo.output_height) { |
1454 y = cinfo.output_scanline; | |
1455 jpeg_read_raw_data(&cinfo, plane_pointer, y_h); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1456 |
0 | 1457 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) { |
1458 if (c_idx == 1 || c_idx == 2) { | |
1459 h1 = c_h; | |
1460 w1 = c_w; | |
1461 y1 = (y >> v_shift); | |
1462 } else { | |
1463 h1 = y_h; | |
1464 w1 = img->w; | |
1465 y1 = y; | |
1466 } | |
1467 idx = plane_idx[c_idx]; | |
1468 for(i = 0; i < h1; i++) { | |
1469 PIXEL *ptr; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1470 ptr = (PIXEL *)(img->data[idx] + |
0 | 1471 img->linesize[idx] * (y1 + i)); |
1472 gray8_to_gray(cvt, ptr, rows[c_idx][i], w1, 1); | |
1473 if (color_space == BPG_CS_YCbCr && has_w_plane) { | |
1474 /* negate color */ | |
1475 if (c_idx == 0) { | |
1476 gray_one_minus(cvt, ptr, w1); | |
1477 } else if (c_idx <= 2) { | |
1478 gray_neg_c(cvt, ptr, w1); | |
1479 } | |
1480 } | |
1481 } | |
1482 } | |
1483 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1484 |
0 | 1485 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) { |
1486 if (c_idx == 1 || c_idx == 2) { | |
1487 h1 = c_h; | |
1488 } else { | |
1489 h1 = y_h; | |
1490 } | |
1491 for(i = 0; i < h1; i++) { | |
1492 free(rows[c_idx][i]); | |
1493 } | |
1494 } | |
1495 } else { | |
1496 JSAMPROW rows[1]; | |
1497 uint8_t *buf; | |
1498 int c_count; | |
1499 | |
1500 c_count = 3 + has_w_plane; | |
1501 buf = malloc(c_count * w); | |
1502 rows[0] = buf; | |
1503 while (cinfo.output_scanline < cinfo.output_height) { | |
1504 y = cinfo.output_scanline; | |
1505 jpeg_read_scanlines(&cinfo, rows, 1); | |
1506 | |
1507 for(c_idx = 0; c_idx < c_count; c_idx++) { | |
1508 idx = plane_idx[c_idx]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1509 gray8_to_gray(cvt, (PIXEL *)(img->data[idx] + |
0 | 1510 img->linesize[idx] * y), |
1511 buf + c_idx, w, c_count); | |
1512 } | |
1513 } | |
1514 free(buf); | |
1515 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1516 |
0 | 1517 first_md = jpeg_get_metadata(cinfo.marker_list); |
1518 | |
1519 the_end: | |
1520 jpeg_finish_decompress(&cinfo); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1521 |
0 | 1522 jpeg_destroy_decompress(&cinfo); |
1523 *pmd = first_md; | |
1524 return img; | |
1525 } | |
1526 | |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1527 static const char PROBE_TIFF_MAGIC_LE[] = { 'I', 'I', 42, 0 }; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1528 static const char PROBE_TIFF_MAGIC_BE[] = { 'M', 'M', 0, 42 }; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1529 #define PROBE_BUF_SIZE 8 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1530 |
0 | 1531 Image *load_image(BPGMetaData **pmd, const char *infilename, |
1532 BPGColorSpaceEnum color_space, int bit_depth, | |
1533 int limited_range, int premultiplied_alpha) | |
1534 { | |
1535 FILE *f; | |
3
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1536 Image *img = NULL; |
0 | 1537 BPGMetaData *md; |
3
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1538 uint8_t buf[PROBE_BUF_SIZE]; |
0 | 1539 |
1540 *pmd = NULL; | |
1541 | |
3
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1542 if ((f = fopen(infilename, "rb")) == NULL) |
0 | 1543 return NULL; |
3
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1544 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1545 if (fread(buf, 1, PROBE_BUF_SIZE, f) != PROBE_BUF_SIZE) |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1546 goto err; |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1547 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1548 fseek(f, 0, SEEK_SET); |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1549 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1550 if (png_sig_cmp(buf, 0, PROBE_BUF_SIZE) == 0) |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1551 img = read_png(&md, f, color_space, bit_depth, limited_range, premultiplied_alpha); |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1552 else |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1553 if (memcmp(buf, PROBE_TIFF_MAGIC_LE, sizeof(PROBE_TIFF_MAGIC_LE)) == 0 || |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1554 memcmp(buf, PROBE_TIFF_MAGIC_BE, sizeof(PROBE_TIFF_MAGIC_BE)) == 0) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1555 img = read_tiff(&md, infilename, color_space, bit_depth, limited_range, premultiplied_alpha); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1556 else |
0 | 1557 img = read_jpeg(&md, f, bit_depth); |
3
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1558 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1559 err: |
0 | 1560 fclose(f); |
1561 *pmd = md; | |
1562 return img; | |
1563 } | |
1564 | |
1565 void save_yuv1(Image *img, FILE *f) | |
1566 { | |
1567 int c_w, c_h, i, c_count, y; | |
1568 | |
1569 if (img->format == BPG_FORMAT_GRAY) | |
1570 c_count = 1; | |
1571 else | |
1572 c_count = 3; | |
1573 for(i = 0; i < c_count; i++) { | |
1574 get_plane_res(img, &c_w, &c_h, i); | |
1575 for(y = 0; y < c_h; y++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1576 fwrite(img->data[i] + y * img->linesize[i], |
0 | 1577 1, c_w << img->pixel_shift, f); |
1578 } | |
1579 } | |
1580 } | |
1581 | |
1582 void save_yuv(Image *img, const char *filename) | |
1583 { | |
1584 FILE *f; | |
1585 | |
1586 f = fopen(filename, "wb"); | |
1587 if (!f) { | |
1588 fprintf(stderr, "Could not open %s\n", filename); | |
1589 exit(1); | |
1590 } | |
1591 save_yuv1(img, f); | |
1592 fclose(f); | |
1593 } | |
1594 | |
1595 /* return the position of the end of the NAL or -1 if error */ | |
1596 static int find_nal_end(const uint8_t *buf, int buf_len) | |
1597 { | |
1598 int idx; | |
1599 | |
1600 idx = 0; | |
1601 if (buf_len >= 4 && | |
1602 buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 1) { | |
1603 idx = 4; | |
1604 } else if (buf_len >= 3 && | |
1605 buf[0] == 0 && buf[1] == 0 && buf[2] == 1) { | |
1606 idx = 3; | |
1607 } else { | |
1608 return -1; | |
1609 } | |
1610 /* NAL header */ | |
1611 if (idx + 2 > buf_len) | |
1612 return -1; | |
1613 /* find the last byte */ | |
1614 for(;;) { | |
1615 if (idx + 2 >= buf_len) { | |
1616 idx = buf_len; | |
1617 break; | |
1618 } | |
1619 if (buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 1) | |
1620 break; | |
1621 if (idx + 3 < buf_len && | |
1622 buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0 && buf[idx + 3] == 1) | |
1623 break; | |
1624 idx++; | |
1625 } | |
1626 return idx; | |
1627 } | |
1628 | |
1629 /* return the position of the end of the NAL or -1 if error */ | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1630 static int extract_nal(uint8_t **pnal_buf, int *pnal_len, |
0 | 1631 const uint8_t *buf, int buf_len) |
1632 { | |
1633 int idx, start, end, len; | |
1634 uint8_t *nal_buf; | |
1635 int nal_len; | |
1636 | |
1637 end = find_nal_end(buf, buf_len); | |
1638 if (end < 0) | |
1639 return -1; | |
1640 if (buf[2] == 1) | |
1641 start = 3; | |
1642 else | |
1643 start = 4; | |
1644 len = end - start; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1645 |
0 | 1646 nal_buf = malloc(len); |
1647 nal_len = 0; | |
1648 idx = start; | |
1649 while (idx < end) { | |
1650 if (idx + 2 < end && buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 3) { | |
1651 nal_buf[nal_len++] = 0; | |
1652 nal_buf[nal_len++] = 0; | |
1653 idx += 3; | |
1654 } else { | |
1655 nal_buf[nal_len++] = buf[idx++]; | |
1656 } | |
1657 } | |
1658 while (idx < end) { | |
1659 nal_buf[nal_len++] = buf[idx++]; | |
1660 } | |
1661 *pnal_buf = nal_buf; | |
1662 *pnal_len = nal_len; | |
1663 return idx; | |
1664 } | |
1665 | |
1666 /* big endian variable length 7 bit encoding */ | |
1667 static void put_ue(uint8_t **pp, uint32_t v) | |
1668 { | |
1669 uint8_t *p = *pp; | |
1670 int i, j; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1671 |
0 | 1672 for(i = 1; i < 5; i++) { |
1673 if (v < (1 << (7 * i))) | |
1674 break; | |
1675 } | |
1676 for(j = i - 1; j >= 1; j--) | |
1677 *p++ = ((v >> (7 * j)) & 0x7f) | 0x80; | |
1678 *p++ = v & 0x7f; | |
1679 *pp = p; | |
1680 } | |
1681 | |
1682 typedef struct { | |
1683 const uint8_t *buf; | |
1684 int idx; | |
1685 int buf_len; | |
1686 } GetBitState; | |
1687 | |
1688 static void init_get_bits(GetBitState *s, const uint8_t *buf, int buf_len) | |
1689 { | |
1690 s->buf = buf; | |
1691 s->buf_len = buf_len; | |
1692 s->idx = 0; | |
1693 } | |
1694 | |
1695 static void skip_bits(GetBitState *s, int n) | |
1696 { | |
1697 s->idx += n; | |
1698 } | |
1699 | |
1700 /* 1 <= n <= 25. return '0' bits if past the end of the buffer. */ | |
1701 static uint32_t get_bits(GetBitState *s, int n) | |
1702 { | |
1703 const uint8_t *buf = s->buf; | |
1704 int p, i; | |
1705 uint32_t v; | |
1706 | |
1707 p = s->idx >> 3; | |
1708 if ((p + 3) < s->buf_len) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1709 v = (buf[p] << 24) | (buf[p + 1] << 16) | |
0 | 1710 (buf[p + 2] << 8) | buf[p + 3]; |
1711 } else { | |
1712 v = 0; | |
1713 for(i = 0; i < 3; i++) { | |
1714 if ((p + i) < s->buf_len) | |
1715 v |= buf[p + i] << (24 - i * 8); | |
1716 } | |
1717 } | |
1718 v = (v >> (32 - (s->idx & 7) - n)) & ((1 << n) - 1); | |
1719 s->idx += n; | |
1720 return v; | |
1721 } | |
1722 | |
1723 /* 1 <= n <= 32 */ | |
1724 static uint32_t get_bits_long(GetBitState *s, int n) | |
1725 { | |
1726 uint32_t v; | |
1727 | |
1728 if (n <= 25) { | |
1729 v = get_bits(s, n); | |
1730 } else { | |
1731 n -= 16; | |
1732 v = get_bits(s, 16) << n; | |
1733 v |= get_bits(s, n); | |
1734 } | |
1735 return v; | |
1736 } | |
1737 | |
1738 /* at most 32 bits are supported */ | |
1739 static uint32_t get_ue_golomb(GetBitState *s) | |
1740 { | |
1741 int i; | |
1742 i = 0; | |
1743 for(;;) { | |
1744 if (get_bits(s, 1)) | |
1745 break; | |
1746 i++; | |
1747 if (i == 32) | |
1748 return 0xffffffff; | |
1749 } | |
1750 if (i == 0) | |
1751 return 0; | |
1752 else | |
1753 return ((1 << i) | get_bits_long(s, i)) - 1; | |
1754 } | |
1755 | |
1756 typedef struct { | |
1757 uint8_t *buf; | |
1758 int idx; | |
1759 } PutBitState; | |
1760 | |
1761 static void init_put_bits(PutBitState *s, uint8_t *buf) | |
1762 { | |
1763 s->buf = buf; | |
1764 s->idx = 0; | |
1765 } | |
1766 | |
1767 static void put_bit(PutBitState *s, int bit) | |
1768 { | |
1769 s->buf[s->idx >> 3] |= bit << (7 - (s->idx & 7)); | |
1770 s->idx++; | |
1771 } | |
1772 | |
1773 static void put_bits(PutBitState *s, int n, uint32_t v) | |
1774 { | |
1775 int i; | |
1776 | |
1777 for(i = 0; i < n; i++) { | |
1778 put_bit(s, (v >> (n - 1 - i)) & 1); | |
1779 } | |
1780 } | |
1781 | |
1782 static void put_ue_golomb(PutBitState *s, uint32_t v) | |
1783 { | |
1784 uint32_t a; | |
1785 int n; | |
1786 | |
1787 v++; | |
1788 n = 0; | |
1789 a = v; | |
1790 while (a != 0) { | |
1791 a >>= 1; | |
1792 n++; | |
1793 } | |
1794 if (n > 1) | |
1795 put_bits(s, n - 1, 0); | |
1796 put_bits(s, n, v); | |
1797 } | |
1798 | |
1799 typedef struct { | |
1800 uint8_t *buf; | |
1801 int size; | |
1802 int len; | |
1803 } DynBuf; | |
1804 | |
1805 static void dyn_buf_init(DynBuf *s) | |
1806 { | |
1807 s->buf = NULL; | |
1808 s->size = 0; | |
1809 s->len = 0; | |
1810 } | |
1811 | |
1812 static int dyn_buf_resize(DynBuf *s, int size) | |
1813 { | |
1814 int new_size; | |
1815 uint8_t *new_buf; | |
1816 | |
1817 if (size <= s->size) | |
1818 return 0; | |
1819 new_size = (s->size * 3) / 2; | |
1820 if (new_size < size) | |
1821 new_size = size; | |
1822 new_buf = realloc(s->buf, new_size); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1823 if (!new_buf) |
0 | 1824 return -1; |
1825 s->buf = new_buf; | |
1826 s->size = new_size; | |
1827 return 0; | |
1828 } | |
1829 | |
1830 /* suppress the VPS NAL and keep only the useful part of the SPS | |
1831 header. The decoder can rebuild a valid HEVC stream if needed. */ | |
1832 static int build_modified_sps(uint8_t **pout_buf, int *pout_buf_len, | |
1833 const uint8_t *buf, int buf_len) | |
1834 { | |
1835 int nal_unit_type, nal_len, idx, i, ret, msps_buf_len; | |
1836 int out_buf_len, out_buf_len_max; | |
1837 uint8_t *nal_buf, *msps_buf, *out_buf; | |
1838 GetBitState gb_s, *gb = &gb_s; | |
1839 PutBitState pb_s, *pb = &pb_s; | |
1840 uint8_t *p; | |
1841 | |
1842 idx = extract_nal(&nal_buf, &nal_len, buf, buf_len); | |
1843 if (idx < 0) | |
1844 return -1; | |
1845 if (nal_len < 2) { | |
1846 free(nal_buf); | |
1847 return -1; | |
1848 } | |
1849 nal_unit_type = (nal_buf[0] >> 1) & 0x3f; | |
1850 free(nal_buf); | |
1851 if (nal_unit_type != 32) { | |
1852 fprintf(stderr, "expecting VPS nal (%d)\n", nal_unit_type); | |
1853 return -1; /* expect VPS nal */ | |
1854 } | |
1855 | |
1856 ret = extract_nal(&nal_buf, &nal_len, buf + idx, buf_len); | |
1857 if (ret < 0) | |
1858 return -1; | |
1859 idx += ret; | |
1860 if (nal_len < 2) | |
1861 return -1; | |
1862 nal_unit_type = (nal_buf[0] >> 1) & 0x3f; | |
1863 if (nal_unit_type != 33) { | |
1864 fprintf(stderr, "expecting SPS nal (%d)\n", nal_unit_type); | |
1865 return -1; /* expect SPS nal */ | |
1866 } | |
1867 | |
1868 /* skip the initial part of the SPS up to and including | |
1869 log2_min_cb_size */ | |
1870 { | |
1871 int vps_id, max_sub_layers, profile_idc, sps_id; | |
1872 int chroma_format_idc, width, height, bit_depth_luma, bit_depth_chroma; | |
1873 int log2_max_poc_lsb, sublayer_ordering_info, log2_min_cb_size; | |
1874 int log2_diff_max_min_coding_block_size, log2_min_tb_size; | |
1875 int log2_diff_max_min_transform_block_size; | |
1876 int max_transform_hierarchy_depth_inter; | |
1877 int max_transform_hierarchy_depth_intra; | |
1878 int scaling_list_enable_flag, amp_enabled_flag, sao_enabled; | |
1879 int pcm_enabled_flag, nb_st_rps; | |
1880 int long_term_ref_pics_present_flag, sps_strong_intra_smoothing_enable_flag, vui_present; | |
1881 int sps_temporal_mvp_enabled_flag; | |
1882 int pcm_sample_bit_depth_luma_minus1; | |
1883 int pcm_sample_bit_depth_chroma_minus1; | |
1884 int log2_min_pcm_luma_coding_block_size_minus3; | |
1885 int log2_diff_max_min_pcm_luma_coding_block_size; | |
1886 int pcm_loop_filter_disabled_flag; | |
1887 int sps_extension_flag, sps_range_extension_flag, sps_extension_7bits; | |
1888 int sps_range_extension_flags; | |
1889 | |
1890 init_get_bits(gb, nal_buf, nal_len); | |
1891 skip_bits(gb, 16); /* nal header */ | |
1892 vps_id = get_bits(gb, 4); | |
1893 if (vps_id != 0) { | |
1894 fprintf(stderr, "VPS id 0 expected\n"); | |
1895 return -1; | |
1896 } | |
1897 max_sub_layers = get_bits(gb, 3); | |
1898 if (max_sub_layers != 0) { | |
1899 fprintf(stderr, "max_sub_layers == 0 expected\n"); | |
1900 return -1; | |
1901 } | |
1902 skip_bits(gb, 1); /* temporal_id_nesting_flag */ | |
1903 /* profile tier level */ | |
1904 skip_bits(gb, 2); /* profile_space */ | |
1905 skip_bits(gb, 1); /* tier_flag */ | |
1906 profile_idc = get_bits(gb, 5); | |
1907 for(i = 0; i < 32; i++) { | |
1908 skip_bits(gb, 1); /* profile_compatibility_flag */ | |
1909 } | |
1910 skip_bits(gb, 1); /* progressive_source_flag */ | |
1911 skip_bits(gb, 1); /* interlaced_source_flag */ | |
1912 skip_bits(gb, 1); /* non_packed_constraint_flag */ | |
1913 skip_bits(gb, 1); /* frame_only_constraint_flag */ | |
1914 skip_bits(gb, 44); /* XXX_reserved_zero_44 */ | |
1915 skip_bits(gb, 8); /* level_idc */ | |
1916 | |
1917 sps_id = get_ue_golomb(gb); | |
1918 if (sps_id != 0) { | |
1919 fprintf(stderr, "SPS id 0 expected (%d)\n", sps_id); | |
1920 return -1; | |
1921 } | |
1922 chroma_format_idc = get_ue_golomb(gb); | |
1923 if (chroma_format_idc == 3) { | |
1924 get_bits(gb, 1); /* separate_colour_plane_flag */ | |
1925 } | |
1926 width = get_ue_golomb(gb); | |
1927 height = get_ue_golomb(gb); | |
1928 /* pic conformance_flag */ | |
1929 if (get_bits(gb, 1)) { | |
1930 get_ue_golomb(gb); /* left_offset */ | |
1931 get_ue_golomb(gb); /* right_offset */ | |
1932 get_ue_golomb(gb); /* top_offset */ | |
1933 get_ue_golomb(gb); /* bottom_offset */ | |
1934 } | |
1935 bit_depth_luma = get_ue_golomb(gb) + 8; | |
1936 bit_depth_chroma = get_ue_golomb(gb) + 8; | |
1937 log2_max_poc_lsb = get_ue_golomb(gb) + 4; | |
1938 if (log2_max_poc_lsb != 8) { | |
1939 fprintf(stderr, "log2_max_poc_lsb must be 8 (%d)\n", log2_max_poc_lsb); | |
1940 return -1; | |
1941 } | |
1942 sublayer_ordering_info = get_bits(gb, 1); | |
1943 get_ue_golomb(gb); /* max_dec_pic_buffering */ | |
1944 get_ue_golomb(gb); /* num_reorder_pics */ | |
1945 get_ue_golomb(gb); /* max_latency_increase */ | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1946 |
0 | 1947 log2_min_cb_size = get_ue_golomb(gb) + 3; |
1948 log2_diff_max_min_coding_block_size = get_ue_golomb(gb); | |
1949 log2_min_tb_size = get_ue_golomb(gb) + 2; | |
1950 log2_diff_max_min_transform_block_size = get_ue_golomb(gb); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1951 |
0 | 1952 max_transform_hierarchy_depth_inter = get_ue_golomb(gb); |
1953 max_transform_hierarchy_depth_intra = get_ue_golomb(gb); | |
1954 if (max_transform_hierarchy_depth_inter != max_transform_hierarchy_depth_intra) { | |
1955 fprintf(stderr, "max_transform_hierarchy_depth_inter must be the same as max_transform_hierarchy_depth_intra (%d %d)\n", max_transform_hierarchy_depth_inter, max_transform_hierarchy_depth_intra); | |
1956 return -1; | |
1957 } | |
1958 | |
1959 scaling_list_enable_flag = get_bits(gb, 1); | |
1960 if (scaling_list_enable_flag != 0) { | |
1961 fprintf(stderr, "scaling_list_enable_flag must be 0\n"); | |
1962 return -1; | |
1963 } | |
1964 amp_enabled_flag = get_bits(gb, 1); | |
1965 if (!amp_enabled_flag) { | |
1966 fprintf(stderr, "amp_enabled_flag must be set\n"); | |
1967 return -1; | |
1968 } | |
1969 sao_enabled = get_bits(gb, 1); | |
1970 pcm_enabled_flag = get_bits(gb, 1); | |
1971 if (pcm_enabled_flag) { | |
1972 pcm_sample_bit_depth_luma_minus1 = get_bits(gb, 4); | |
1973 pcm_sample_bit_depth_chroma_minus1 = get_bits(gb, 4); | |
1974 log2_min_pcm_luma_coding_block_size_minus3 = get_ue_golomb(gb); | |
1975 log2_diff_max_min_pcm_luma_coding_block_size = get_ue_golomb(gb); | |
1976 pcm_loop_filter_disabled_flag = get_bits(gb, 1); | |
1977 } | |
1978 nb_st_rps = get_ue_golomb(gb); | |
1979 if (nb_st_rps != 0) { | |
1980 fprintf(stderr, "nb_st_rps must be 0 (%d)\n", nb_st_rps); | |
1981 return -1; | |
1982 } | |
1983 long_term_ref_pics_present_flag = get_bits(gb, 1); | |
1984 if (long_term_ref_pics_present_flag) { | |
1985 fprintf(stderr, "nlong_term_ref_pics_present_flag must be 0 (%d)\n", nb_st_rps); | |
1986 return -1; | |
1987 } | |
1988 sps_temporal_mvp_enabled_flag = get_bits(gb, 1); | |
1989 if (!sps_temporal_mvp_enabled_flag) { | |
1990 fprintf(stderr, "sps_temporal_mvp_enabled_flag must be set\n"); | |
1991 return -1; | |
1992 } | |
1993 sps_strong_intra_smoothing_enable_flag = get_bits(gb, 1); | |
1994 vui_present = get_bits(gb, 1); | |
1995 if (vui_present) { | |
1996 int sar_present, sar_idx, overscan_info_present_flag; | |
1997 int video_signal_type_present_flag, chroma_loc_info_present_flag; | |
1998 int default_display_window_flag, vui_timing_info_present_flag; | |
1999 int vui_poc_proportional_to_timing_flag; | |
2000 int vui_hrd_parameters_present_flag, bitstream_restriction_flag; | |
2001 | |
2002 sar_present = get_bits(gb, 1); | |
2003 if (sar_present) { | |
2004 sar_idx = get_bits(gb, 8); | |
2005 if (sar_idx == 255) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2006 skip_bits(gb, 16); /* sar_num */ |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2007 skip_bits(gb, 16); /* sar_den */ |
0 | 2008 } |
2009 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2010 |
0 | 2011 overscan_info_present_flag = get_bits(gb, 1); |
2012 if (overscan_info_present_flag) { | |
2013 skip_bits(gb, 1); /* overscan_appropriate_flag */ | |
2014 } | |
2015 | |
2016 video_signal_type_present_flag = get_bits(gb, 1); | |
2017 if (video_signal_type_present_flag) { | |
2018 fprintf(stderr, "video_signal_type_present_flag must be 0\n"); | |
2019 return -1; | |
2020 } | |
2021 chroma_loc_info_present_flag = get_bits(gb, 1); | |
2022 if (chroma_loc_info_present_flag) { | |
2023 get_ue_golomb(gb); | |
2024 get_ue_golomb(gb); | |
2025 } | |
2026 skip_bits(gb, 1); /* neutra_chroma_indication_flag */ | |
2027 skip_bits(gb, 1); | |
2028 skip_bits(gb, 1); | |
2029 default_display_window_flag = get_bits(gb, 1); | |
2030 if (default_display_window_flag) { | |
2031 fprintf(stderr, "default_display_window_flag must be 0\n"); | |
2032 return -1; | |
2033 } | |
2034 vui_timing_info_present_flag = get_bits(gb, 1); | |
2035 if (vui_timing_info_present_flag) { | |
2036 skip_bits(gb, 32); | |
2037 skip_bits(gb, 32); | |
2038 vui_poc_proportional_to_timing_flag = get_bits(gb, 1); | |
2039 if (vui_poc_proportional_to_timing_flag) { | |
2040 get_ue_golomb(gb); | |
2041 } | |
2042 vui_hrd_parameters_present_flag = get_bits(gb, 1); | |
2043 if (vui_hrd_parameters_present_flag) { | |
2044 fprintf(stderr, "vui_hrd_parameters_present_flag must be 0\n"); | |
2045 return -1; | |
2046 } | |
2047 } | |
2048 bitstream_restriction_flag = get_bits(gb, 1); | |
2049 if (bitstream_restriction_flag) { | |
2050 skip_bits(gb, 1); | |
2051 skip_bits(gb, 1); | |
2052 skip_bits(gb, 1); | |
2053 get_ue_golomb(gb); | |
2054 get_ue_golomb(gb); | |
2055 get_ue_golomb(gb); | |
2056 get_ue_golomb(gb); | |
2057 get_ue_golomb(gb); | |
2058 } | |
2059 } | |
2060 sps_extension_flag = get_bits(gb, 1); | |
2061 sps_range_extension_flag = 0; | |
2062 sps_range_extension_flags = 0; | |
2063 if (sps_extension_flag) { | |
2064 sps_range_extension_flag = get_bits(gb, 1); | |
2065 sps_extension_7bits = get_bits(gb, 7); | |
2066 if (sps_extension_7bits != 0) { | |
2067 fprintf(stderr, "sps_extension_7bits must be 0\n"); | |
2068 return -1; | |
2069 } | |
2070 if (sps_range_extension_flag) { | |
2071 sps_range_extension_flags = get_bits(gb, 9); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2072 if (sps_range_extension_flags & ((1 << (8 - 3)) | |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2073 (1 << (8 - 4)) | |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2074 (1 << (8 - 6)) | |
0 | 2075 (1 << (8 - 8)))) { |
2076 fprintf(stderr, "unsupported range extensions (0x%x)\n", | |
2077 sps_range_extension_flags); | |
2078 return -1; | |
2079 } | |
2080 } | |
2081 } | |
2082 | |
2083 /* build the modified SPS */ | |
2084 msps_buf = malloc(nal_len + 32); | |
2085 memset(msps_buf, 0, nal_len + 16); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2086 |
0 | 2087 init_put_bits(pb, msps_buf); |
2088 put_ue_golomb(pb, log2_min_cb_size - 3); | |
2089 put_ue_golomb(pb, log2_diff_max_min_coding_block_size); | |
2090 put_ue_golomb(pb, log2_min_tb_size - 2); | |
2091 put_ue_golomb(pb, log2_diff_max_min_transform_block_size); | |
2092 put_ue_golomb(pb, max_transform_hierarchy_depth_intra); | |
2093 put_bits(pb, 1, sao_enabled); | |
2094 put_bits(pb, 1, pcm_enabled_flag); | |
2095 if (pcm_enabled_flag) { | |
2096 put_bits(pb, 4, pcm_sample_bit_depth_luma_minus1); | |
2097 put_bits(pb, 4, pcm_sample_bit_depth_chroma_minus1); | |
2098 put_ue_golomb(pb, log2_min_pcm_luma_coding_block_size_minus3); | |
2099 put_ue_golomb(pb, log2_diff_max_min_pcm_luma_coding_block_size); | |
2100 put_bits(pb, 1, pcm_loop_filter_disabled_flag); | |
2101 } | |
2102 put_bits(pb, 1, sps_strong_intra_smoothing_enable_flag); | |
2103 put_bits(pb, 1, sps_extension_flag); | |
2104 if (sps_extension_flag) { | |
2105 put_bits(pb, 1, sps_range_extension_flag); | |
2106 put_bits(pb, 7, 0); | |
2107 if (sps_range_extension_flag) { | |
2108 put_bits(pb, 9, sps_range_extension_flags); | |
2109 } | |
2110 } | |
2111 msps_buf_len = (pb->idx + 7) >> 3; | |
2112 | |
2113 out_buf_len_max = 5 + msps_buf_len; | |
2114 out_buf = malloc(out_buf_len_max); | |
2115 | |
2116 // printf("msps_n_bits=%d\n", pb->idx); | |
2117 p = out_buf; | |
2118 put_ue(&p, msps_buf_len); /* header length */ | |
2119 | |
2120 memcpy(p, msps_buf, msps_buf_len); | |
2121 p += msps_buf_len; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2122 |
0 | 2123 out_buf_len = p - out_buf; |
2124 free(msps_buf); | |
2125 free(nal_buf); | |
2126 } | |
2127 *pout_buf = out_buf; | |
2128 *pout_buf_len = out_buf_len; | |
2129 return idx; | |
2130 } | |
2131 | |
2132 static int add_frame_duration_sei(DynBuf *out_buf, uint16_t frame_ticks) | |
2133 { | |
2134 uint8_t nal_buf[128], *q; | |
2135 int nut, nal_len; | |
2136 | |
2137 q = nal_buf; | |
2138 *q++ = 0x00; | |
2139 *q++ = 0x00; | |
2140 *q++ = 0x01; | |
2141 nut = 39; /* prefix SEI NUT */ | |
2142 *q++ = (nut << 1); | |
2143 *q++ = 1; | |
2144 *q++ = 0xff; /* payload_type = 257 */ | |
2145 *q++ = 0x02; | |
2146 *q++ = 2; /* payload_size = 2 */ | |
2147 *q++ = frame_ticks >> 8; | |
2148 *q++ = frame_ticks; | |
2149 *q++ = 0x80; /* extra '1' bit and align to byte */ | |
2150 /* Note: the 0x00 0x00 b pattern with b <= 3 cannot happen, so no | |
2151 need to escape */ | |
2152 nal_len = q - nal_buf; | |
2153 if (dyn_buf_resize(out_buf, out_buf->len + nal_len) < 0) | |
2154 return -1; | |
2155 memcpy(out_buf->buf + out_buf->len, nal_buf, nal_len); | |
2156 out_buf->len += nal_len; | |
2157 return 0; | |
2158 } | |
2159 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2160 static int build_modified_hevc(uint8_t **pout_buf, |
0 | 2161 const uint8_t *cbuf, int cbuf_len, |
2162 const uint8_t *abuf, int abuf_len, | |
2163 const uint16_t *frame_duration_tab, | |
2164 int frame_count) | |
2165 { | |
2166 DynBuf out_buf_s, *out_buf = &out_buf_s; | |
2167 uint8_t *msps; | |
2168 const uint8_t *nal_buf; | |
2169 int msps_len, cidx, aidx, is_alpha, nal_len, first_nal, start, l, frame_num; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2170 |
0 | 2171 dyn_buf_init(out_buf); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2172 |
0 | 2173 /* add alpha MSPS */ |
2174 aidx = 0; /* avoids warning */ | |
2175 if (abuf) { | |
2176 aidx = build_modified_sps(&msps, &msps_len, abuf, abuf_len); | |
2177 if (aidx < 0) | |
2178 goto fail; | |
2179 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0) | |
2180 goto fail; | |
2181 memcpy(out_buf->buf + out_buf->len, msps, msps_len); | |
2182 out_buf->len += msps_len; | |
2183 free(msps); | |
2184 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2185 |
0 | 2186 /* add color MSPS */ |
2187 cidx = build_modified_sps(&msps, &msps_len, cbuf, cbuf_len); | |
2188 if (cidx < 0) | |
2189 goto fail; | |
2190 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0) | |
2191 goto fail; | |
2192 memcpy(out_buf->buf + out_buf->len, msps, msps_len); | |
2193 out_buf->len += msps_len; | |
2194 free(msps); | |
2195 | |
2196 /* add the remaining NALs, alternating between alpha (if present) | |
2197 and color. */ | |
2198 is_alpha = (abuf != NULL); | |
2199 first_nal = 1; | |
2200 frame_num = 0; | |
2201 for(;;) { | |
2202 if (!is_alpha) { | |
2203 if (cidx >= cbuf_len) { | |
2204 if (abuf) { | |
2205 fprintf(stderr, "Incorrect number of alpha NALs\n"); | |
2206 goto fail; | |
2207 } | |
2208 break; | |
2209 } | |
2210 nal_buf = cbuf + cidx; | |
2211 nal_len = find_nal_end(nal_buf, cbuf_len - cidx); | |
2212 // printf("cidx=%d/%d nal_len=%d\n", cidx, cbuf_len, nal_len); | |
2213 if (nal_len < 0) | |
2214 goto fail; | |
2215 cidx += nal_len; | |
2216 } else { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2217 if (aidx >= abuf_len) |
0 | 2218 break; |
2219 nal_buf = abuf + aidx; | |
2220 nal_len = find_nal_end(nal_buf, abuf_len - aidx); | |
2221 // printf("aidx=%d/%d nal_len=%d\n", aidx, abuf_len, nal_len); | |
2222 if (nal_len < 0) | |
2223 goto fail; | |
2224 aidx += nal_len; | |
2225 } | |
2226 start = 3 + (nal_buf[2] == 0); | |
2227 if (!is_alpha) { | |
2228 int nut; | |
2229 /* add SEI NAL for the frame duration (animation case) */ | |
2230 nut = (nal_buf[start] >> 1) & 0x3f; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2231 if ((nut <= 9 || (nut >= 16 && nut <= 21)) && |
0 | 2232 start + 2 < nal_len && (nal_buf[start + 2] & 0x80)) { |
2233 int frame_ticks; | |
2234 assert(frame_num < frame_count); | |
2235 frame_ticks = frame_duration_tab[frame_num]; | |
2236 if (frame_ticks > 1) { | |
2237 add_frame_duration_sei(out_buf, frame_ticks); | |
2238 } | |
2239 frame_num++; | |
2240 } | |
2241 } | |
2242 if (first_nal) { | |
2243 /* skip first start code */ | |
2244 l = start; | |
2245 } else { | |
2246 l = 0; | |
2247 } | |
2248 if (dyn_buf_resize(out_buf, out_buf->len + nal_len - l) < 0) | |
2249 goto fail; | |
2250 // printf("add nal len=%d\n", nal_len - l); | |
2251 memcpy(out_buf->buf + out_buf->len, nal_buf + l, nal_len - l); | |
2252 if (is_alpha) { | |
2253 /* set nul_layer_id of alpha to '1' */ | |
2254 out_buf->buf[out_buf->len + (start - l) + 1] |= 1 << 3; | |
2255 } | |
2256 out_buf->len += nal_len - l; | |
2257 | |
2258 if (abuf) { | |
2259 is_alpha ^= 1; | |
2260 } | |
2261 first_nal = 0; | |
2262 } | |
2263 *pout_buf = out_buf->buf; | |
2264 return out_buf->len; | |
2265 fail: | |
2266 free(out_buf->buf); | |
2267 return -1; | |
2268 } | |
2269 | |
2270 typedef enum { | |
2271 #if defined(USE_X265) | |
2272 HEVC_ENCODER_X265, | |
2273 #endif | |
2274 #if defined(USE_JCTVC) | |
2275 HEVC_ENCODER_JCTVC, | |
2276 #endif | |
2277 | |
2278 HEVC_ENCODER_COUNT, | |
2279 } HEVCEncoderEnum; | |
2280 | |
2281 static char *hevc_encoder_name[HEVC_ENCODER_COUNT] = { | |
2282 #if defined(USE_X265) | |
2283 "x265", | |
2284 #endif | |
2285 #if defined(USE_JCTVC) | |
2286 "jctvc", | |
2287 #endif | |
2288 }; | |
2289 | |
2290 static HEVCEncoder *hevc_encoder_tab[HEVC_ENCODER_COUNT] = { | |
2291 #if defined(USE_X265) | |
2292 &x265_hevc_encoder, | |
2293 #endif | |
2294 #if defined(USE_JCTVC) | |
2295 &jctvc_encoder, | |
2296 #endif | |
2297 }; | |
2298 | |
2299 #define IMAGE_HEADER_MAGIC 0x425047fb | |
2300 | |
2301 #define DEFAULT_OUTFILENAME "out.bpg" | |
2302 #define DEFAULT_QP 29 | |
2303 #define DEFAULT_BIT_DEPTH 8 | |
2304 | |
2305 #ifdef RExt__HIGH_BIT_DEPTH_SUPPORT | |
2306 #define BIT_DEPTH_MAX 14 | |
2307 #else | |
2308 #define BIT_DEPTH_MAX 12 | |
2309 #endif | |
2310 #define DEFAULT_COMPRESS_LEVEL 8 | |
2311 | |
2312 | |
2313 typedef struct BPGEncoderContext BPGEncoderContext; | |
2314 | |
2315 typedef struct BPGEncoderParameters { | |
2316 int qp; /* 0 ... 51 */ | |
2317 int alpha_qp; /* -1 ... 51. -1 means same as qp */ | |
2318 int lossless; /* true if lossless compression (qp and alpha_qp are | |
2319 ignored) */ | |
2320 BPGImageFormatEnum preferred_chroma_format; | |
2321 int sei_decoded_picture_hash; /* 0, 1 */ | |
2322 int compress_level; /* 1 ... 9 */ | |
2323 int verbose; | |
2324 HEVCEncoderEnum encoder_type; | |
2325 int animated; /* 0 ... 1: if true, encode as animated image */ | |
2326 uint16_t loop_count; /* animations: number of loops. 0=infinite */ | |
2327 /* animations: the frame delay is a multiple of | |
2328 frame_delay_num/frame_delay_den seconds */ | |
2329 uint16_t frame_delay_num; | |
2330 uint16_t frame_delay_den; | |
2331 } BPGEncoderParameters; | |
2332 | |
2333 typedef int BPGEncoderWriteFunc(void *opaque, const uint8_t *buf, int buf_len); | |
2334 | |
2335 struct BPGEncoderContext { | |
2336 BPGEncoderParameters params; | |
2337 BPGMetaData *first_md; | |
2338 HEVCEncoder *encoder; | |
2339 int frame_count; | |
2340 HEVCEncoderContext *enc_ctx; | |
2341 HEVCEncoderContext *alpha_enc_ctx; | |
2342 int frame_ticks; | |
2343 uint16_t *frame_duration_tab; | |
2344 int frame_duration_tab_size; | |
2345 }; | |
2346 | |
2347 void *mallocz(size_t size) | |
2348 { | |
2349 void *ptr; | |
2350 ptr = malloc(size); | |
2351 if (!ptr) | |
2352 return NULL; | |
2353 memset(ptr, 0, size); | |
2354 return ptr; | |
2355 } | |
2356 | |
2357 BPGEncoderParameters *bpg_encoder_param_alloc(void) | |
2358 { | |
2359 BPGEncoderParameters *p; | |
2360 p = mallocz(sizeof(BPGEncoderParameters)); | |
2361 if (!p) | |
2362 return NULL; | |
2363 p->qp = DEFAULT_QP; | |
2364 p->alpha_qp = -1; | |
2365 p->preferred_chroma_format = BPG_FORMAT_420; | |
2366 p->compress_level = DEFAULT_COMPRESS_LEVEL; | |
2367 p->frame_delay_num = 1; | |
2368 p->frame_delay_den = 25; | |
2369 p->loop_count = 0; | |
2370 return p; | |
2371 } | |
2372 | |
2373 void bpg_encoder_param_free(BPGEncoderParameters *p) | |
2374 { | |
2375 free(p); | |
2376 } | |
2377 | |
2378 BPGEncoderContext *bpg_encoder_open(BPGEncoderParameters *p) | |
2379 { | |
2380 BPGEncoderContext *s; | |
2381 | |
2382 s = mallocz(sizeof(BPGEncoderContext)); | |
2383 if (!s) | |
2384 return NULL; | |
2385 s->params = *p; | |
2386 s->encoder = hevc_encoder_tab[s->params.encoder_type]; | |
2387 s->frame_ticks = 1; | |
2388 return s; | |
2389 } | |
2390 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2391 void bpg_encoder_set_extension_data(BPGEncoderContext *s, |
0 | 2392 BPGMetaData *md) |
2393 { | |
2394 s->first_md = md; | |
2395 } | |
2396 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2397 static int bpg_encoder_encode_trailer(BPGEncoderContext *s, |
0 | 2398 BPGEncoderWriteFunc *write_func, |
2399 void *opaque) | |
2400 { | |
2401 uint8_t *out_buf, *alpha_buf, *hevc_buf; | |
2402 int out_buf_len, alpha_buf_len, hevc_buf_len; | |
2403 | |
2404 out_buf_len = s->encoder->close(s->enc_ctx, &out_buf); | |
2405 if (out_buf_len < 0) { | |
2406 fprintf(stderr, "Error while encoding picture\n"); | |
2407 exit(1); | |
2408 } | |
2409 s->enc_ctx = NULL; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2410 |
0 | 2411 alpha_buf = NULL; |
2412 alpha_buf_len = 0; | |
2413 if (s->alpha_enc_ctx) { | |
2414 alpha_buf_len = s->encoder->close(s->alpha_enc_ctx, &alpha_buf); | |
2415 if (alpha_buf_len < 0) { | |
2416 fprintf(stderr, "Error while encoding picture (alpha plane)\n"); | |
2417 exit(1); | |
2418 } | |
2419 s->alpha_enc_ctx = NULL; | |
2420 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2421 |
0 | 2422 hevc_buf = NULL; |
2423 hevc_buf_len = build_modified_hevc(&hevc_buf, out_buf, out_buf_len, | |
2424 alpha_buf, alpha_buf_len, | |
2425 s->frame_duration_tab, s->frame_count); | |
2426 if (hevc_buf_len < 0) { | |
2427 fprintf(stderr, "Error while creating HEVC data\n"); | |
2428 exit(1); | |
2429 } | |
2430 free(out_buf); | |
2431 free(alpha_buf); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2432 |
0 | 2433 if (write_func(opaque, hevc_buf, hevc_buf_len) != hevc_buf_len) { |
2434 fprintf(stderr, "Error while writing HEVC data\n"); | |
2435 exit(1); | |
2436 } | |
2437 free(hevc_buf); | |
2438 return 0; | |
2439 } | |
2440 | |
2441 int bpg_encoder_set_frame_duration(BPGEncoderContext *s, int frame_ticks) | |
2442 { | |
2443 if (frame_ticks >= 1 && frame_ticks <= 65535) { | |
2444 s->frame_ticks = frame_ticks; | |
2445 return 0; | |
2446 } else { | |
2447 return -1; | |
2448 } | |
2449 } | |
2450 | |
2451 /* Warning: currently 'img' is modified. When encoding animations, img | |
2452 = NULL indicates the end of the stream. */ | |
2453 int bpg_encoder_encode(BPGEncoderContext *s, Image *img, | |
2454 BPGEncoderWriteFunc *write_func, | |
2455 void *opaque) | |
2456 { | |
2457 const BPGEncoderParameters *p = &s->params; | |
2458 Image *img_alpha; | |
2459 HEVCEncodeParams ep_s, *ep = &ep_s; | |
2460 uint8_t *extension_buf; | |
2461 int extension_buf_len; | |
2462 int cb_size, width, height; | |
2463 | |
2464 if (p->animated && !img) { | |
2465 return bpg_encoder_encode_trailer(s, write_func, opaque); | |
2466 } | |
2467 | |
2468 /* extract the alpha plane */ | |
2469 if (img->has_alpha) { | |
2470 int c_idx; | |
2471 | |
2472 img_alpha = malloc(sizeof(Image)); | |
2473 memset(img_alpha, 0, sizeof(*img_alpha)); | |
2474 if (img->format == BPG_FORMAT_GRAY) | |
2475 c_idx = 1; | |
2476 else | |
2477 c_idx = 3; | |
2478 | |
2479 img_alpha->w = img->w; | |
2480 img_alpha->h = img->h; | |
2481 img_alpha->format = BPG_FORMAT_GRAY; | |
2482 img_alpha->has_alpha = 0; | |
2483 img_alpha->color_space = BPG_CS_YCbCr; | |
2484 img_alpha->bit_depth = img->bit_depth; | |
2485 img_alpha->pixel_shift = img->pixel_shift; | |
2486 img_alpha->data[0] = img->data[c_idx]; | |
2487 img_alpha->linesize[0] = img->linesize[c_idx]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2488 |
0 | 2489 img->data[c_idx] = NULL; |
2490 img->has_alpha = 0; | |
2491 } else { | |
2492 img_alpha = NULL; | |
2493 } | |
2494 | |
2495 if (img->format == BPG_FORMAT_444 && img->color_space != BPG_CS_RGB) { | |
2496 if (p->preferred_chroma_format == BPG_FORMAT_420 || | |
2497 p->preferred_chroma_format == BPG_FORMAT_420_VIDEO) { | |
2498 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_420); | |
2499 if (image_ycc444_to_ycc420(img, c_h_phase) != 0) | |
2500 goto error_convert; | |
2501 } else if (p->preferred_chroma_format == BPG_FORMAT_422 || | |
2502 p->preferred_chroma_format == BPG_FORMAT_422_VIDEO) { | |
2503 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_422); | |
2504 if (image_ycc444_to_ycc422(img, c_h_phase) != 0) { | |
2505 error_convert: | |
2506 fprintf(stderr, "Cannot convert image\n"); | |
2507 exit(1); | |
2508 } | |
2509 } | |
2510 } | |
2511 | |
2512 cb_size = 8; /* XXX: should make it configurable. We assume the | |
2513 HEVC encoder uses the same value */ | |
2514 width = img->w; | |
2515 height = img->h; | |
2516 image_pad(img, cb_size); | |
2517 if (img_alpha) | |
2518 image_pad(img_alpha, cb_size); | |
2519 | |
2520 /* convert to the allocated pixel width to 8 bit if needed by the | |
2521 HEVC encoder */ | |
2522 if (img->bit_depth == 8) { | |
2523 image_convert16to8(img); | |
2524 if (img_alpha) | |
2525 image_convert16to8(img_alpha); | |
2526 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2527 |
0 | 2528 if (s->frame_count == 0) { |
2529 memset(ep, 0, sizeof(*ep)); | |
2530 ep->qp = p->qp; | |
2531 ep->width = img->w; | |
2532 ep->height = img->h; | |
2533 ep->chroma_format = img->format; | |
2534 ep->bit_depth = img->bit_depth; | |
2535 ep->intra_only = !p->animated; | |
2536 ep->lossless = p->lossless; | |
2537 ep->sei_decoded_picture_hash = p->sei_decoded_picture_hash; | |
2538 ep->compress_level = p->compress_level; | |
2539 ep->verbose = p->verbose; | |
2540 | |
2541 s->enc_ctx = s->encoder->open(ep); | |
2542 if (!s->enc_ctx) { | |
2543 fprintf(stderr, "Error while opening encoder\n"); | |
2544 exit(1); | |
2545 } | |
2546 | |
2547 if (img_alpha) { | |
2548 if (p->alpha_qp < 0) | |
2549 ep->qp = p->qp; | |
2550 else | |
2551 ep->qp = p->alpha_qp; | |
2552 ep->chroma_format = 0; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2553 |
0 | 2554 s->alpha_enc_ctx = s->encoder->open(ep); |
2555 if (!s->alpha_enc_ctx) { | |
2556 fprintf(stderr, "Error while opening alpha encoder\n"); | |
2557 exit(1); | |
2558 } | |
2559 } | |
2560 | |
2561 /* prepare the extension data */ | |
2562 if (p->animated) { | |
2563 BPGMetaData *md; | |
2564 uint8_t buf[15], *q; | |
2565 | |
2566 md = bpg_md_alloc(BPG_EXTENSION_TAG_ANIM_CONTROL); | |
2567 q = buf; | |
2568 put_ue(&q, p->loop_count); | |
2569 put_ue(&q, p->frame_delay_num); | |
2570 put_ue(&q, p->frame_delay_den); | |
2571 md->buf_len = q - buf; | |
2572 md->buf = malloc(md->buf_len); | |
2573 memcpy(md->buf, buf, md->buf_len); | |
2574 md->next = s->first_md; | |
2575 s->first_md = md; | |
2576 } | |
2577 | |
2578 extension_buf = NULL; | |
2579 extension_buf_len = 0; | |
2580 if (s->first_md) { | |
2581 BPGMetaData *md1; | |
2582 int max_len; | |
2583 uint8_t *q; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2584 |
0 | 2585 max_len = 0; |
2586 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) { | |
2587 max_len += md1->buf_len + 5 * 2; | |
2588 } | |
2589 extension_buf = malloc(max_len); | |
2590 q = extension_buf; | |
2591 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) { | |
2592 put_ue(&q, md1->tag); | |
2593 put_ue(&q, md1->buf_len); | |
2594 memcpy(q, md1->buf, md1->buf_len); | |
2595 q += md1->buf_len; | |
2596 } | |
2597 extension_buf_len = q - extension_buf; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2598 |
0 | 2599 bpg_md_free(s->first_md); |
2600 s->first_md = NULL; | |
2601 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2602 |
0 | 2603 { |
2604 uint8_t img_header[128], *q; | |
2605 int v, has_alpha, has_extension, alpha2_flag, alpha1_flag, format; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2606 |
0 | 2607 has_alpha = (img_alpha != NULL); |
2608 has_extension = (extension_buf_len > 0); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2609 |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2610 |
0 | 2611 if (has_alpha) { |
2612 if (img->has_w_plane) { | |
2613 alpha1_flag = 0; | |
2614 alpha2_flag = 1; | |
2615 } else { | |
2616 alpha1_flag = 1; | |
2617 alpha2_flag = img->premultiplied_alpha; | |
2618 } | |
2619 } else { | |
2620 alpha1_flag = 0; | |
2621 alpha2_flag = 0; | |
2622 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2623 |
0 | 2624 q = img_header; |
2625 *q++ = (IMAGE_HEADER_MAGIC >> 24) & 0xff; | |
2626 *q++ = (IMAGE_HEADER_MAGIC >> 16) & 0xff; | |
2627 *q++ = (IMAGE_HEADER_MAGIC >> 8) & 0xff; | |
2628 *q++ = (IMAGE_HEADER_MAGIC >> 0) & 0xff; | |
2629 | |
2630 if (img->c_h_phase == 0 && img->format == BPG_FORMAT_420) | |
2631 format = BPG_FORMAT_420_VIDEO; | |
2632 else if (img->c_h_phase == 0 && img->format == BPG_FORMAT_422) | |
2633 format = BPG_FORMAT_422_VIDEO; | |
2634 else | |
2635 format = img->format; | |
2636 v = (format << 5) | (alpha1_flag << 4) | (img->bit_depth - 8); | |
2637 *q++ = v; | |
2638 v = (img->color_space << 4) | (has_extension << 3) | | |
2639 (alpha2_flag << 2) | (img->limited_range << 1) | | |
2640 p->animated; | |
2641 *q++ = v; | |
2642 put_ue(&q, width); | |
2643 put_ue(&q, height); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2644 |
0 | 2645 put_ue(&q, 0); /* zero length means up to the end of the file */ |
2646 if (has_extension) { | |
2647 put_ue(&q, extension_buf_len); /* extension data length */ | |
2648 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2649 |
0 | 2650 write_func(opaque, img_header, q - img_header); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2651 |
0 | 2652 if (has_extension) { |
2653 if (write_func(opaque, extension_buf, extension_buf_len) != extension_buf_len) { | |
2654 fprintf(stderr, "Error while writing extension data\n"); | |
2655 exit(1); | |
2656 } | |
2657 free(extension_buf); | |
2658 } | |
2659 } | |
2660 } | |
2661 | |
2662 /* store the frame duration */ | |
2663 if ((s->frame_count + 1) > s->frame_duration_tab_size) { | |
2664 s->frame_duration_tab_size = (s->frame_duration_tab_size * 3) / 2; | |
2665 if (s->frame_duration_tab_size < (s->frame_count + 1)) | |
2666 s->frame_duration_tab_size = (s->frame_count + 1); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2667 s->frame_duration_tab = realloc(s->frame_duration_tab, |
0 | 2668 sizeof(s->frame_duration_tab) * s->frame_duration_tab_size); |
2669 } | |
2670 s->frame_duration_tab[s->frame_count] = s->frame_ticks; | |
2671 | |
2672 s->encoder->encode(s->enc_ctx, img); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2673 |
0 | 2674 if (img_alpha) { |
2675 s->encoder->encode(s->alpha_enc_ctx, img_alpha); | |
2676 image_free(img_alpha); | |
2677 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2678 |
0 | 2679 s->frame_count++; |
2680 | |
2681 if (!p->animated) | |
2682 bpg_encoder_encode_trailer(s, write_func, opaque); | |
2683 | |
2684 return 0; | |
2685 } | |
2686 | |
2687 void bpg_encoder_close(BPGEncoderContext *s) | |
2688 { | |
2689 free(s->frame_duration_tab); | |
2690 bpg_md_free(s->first_md); | |
2691 free(s); | |
2692 } | |
2693 | |
2694 static int my_write_func(void *opaque, const uint8_t *buf, int buf_len) | |
2695 { | |
2696 FILE *f = opaque; | |
2697 return fwrite(buf, 1, buf_len, f); | |
2698 } | |
2699 | |
2700 static int get_filename_num(char *buf, int buf_size, const char *str, int n) | |
2701 { | |
2702 const char *p, *r; | |
2703 char *q; | |
2704 int l, c; | |
2705 | |
2706 q = buf; | |
2707 p = str; | |
2708 for(;;) { | |
2709 c = *p++; | |
2710 if (c == '\0') | |
2711 break; | |
2712 if (c == '%') { | |
2713 r = p - 1; | |
2714 l = 0; | |
2715 for(;;) { | |
2716 c = *p; | |
2717 if (c < '0' || c > '9') | |
2718 break; | |
2719 l = l * 10 + (c - '0'); | |
2720 p++; | |
2721 } | |
2722 c = *p++; | |
2723 if (c == '%') { | |
2724 goto add_char; | |
2725 } else if (c != 'd') { | |
2726 return -1; | |
2727 } | |
2728 snprintf(q, buf + buf_size - q, "%0*u", l, n); | |
2729 q += strlen(q); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2730 |
0 | 2731 } else { |
2732 add_char: | |
2733 if ((q - buf) < buf_size - 1) | |
2734 *q++ = c; | |
2735 } | |
2736 } | |
2737 *q = '\0'; | |
2738 return 0; | |
2739 } | |
2740 | |
2741 void help(int is_full) | |
2742 { | |
2743 char hevc_encoders[128]; | |
2744 int i; | |
2745 | |
2746 hevc_encoders[0] = '\0'; | |
2747 for(i = 0; i < HEVC_ENCODER_COUNT; i++) { | |
2748 if (i != 0) | |
2749 strcat(hevc_encoders, " "); | |
2750 strcat(hevc_encoders, hevc_encoder_name[i]); | |
2751 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2752 |
0 | 2753 printf("BPG Image Encoder version " CONFIG_BPG_VERSION "\n" |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2754 "usage: bpgenc [options] infile.[jpg|png|tiff]\n" |
0 | 2755 "\n" |
2756 "Main options:\n" | |
2757 "-h show the full help (including the advanced options)\n" | |
2758 "-o outfile set output filename (default = %s)\n" | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2759 "-q qp set quantizer parameter (smaller gives better quality,\n" |
0 | 2760 " range: 0-51, default = %d)\n" |
2761 "-f cfmt set the preferred chroma format (420, 422, 444,\n" | |
2762 " default=420)\n" | |
2763 "-c color_space set the preferred color space (ycbcr, rgb, ycgco,\n" | |
2764 " ycbcr_bt709, ycbcr_bt2020, default=ycbcr)\n" | |
2765 "-b bit_depth set the bit depth (8 to %d, default = %d)\n" | |
2766 "-lossless enable lossless mode\n" | |
2767 "-e encoder select the HEVC encoder (%s, default = %s)\n" | |
2768 "-m level select the compression level (1=fast, 9=slow, default = %d)\n" | |
2769 "\n" | |
2770 "Animation options:\n" | |
2771 "-a generate animations from a sequence of images. Use %%d or\n" | |
2772 " %%Nd (N = number of digits) in the filename to specify the\n" | |
2773 " image index, starting from 0 or 1.\n" | |
2774 "-fps N set the frame rate (default = 25)\n" | |
2775 "-loop N set the number of times the animation is played. 0 means\n" | |
2776 " infinite (default = 0)\n" | |
2777 "-delayfile file text file containing one number per image giving the\n" | |
2778 " display delay per image in centiseconds.\n" | |
2779 , DEFAULT_OUTFILENAME, DEFAULT_QP, BIT_DEPTH_MAX, DEFAULT_BIT_DEPTH, | |
2780 hevc_encoders, hevc_encoder_name[0], DEFAULT_COMPRESS_LEVEL); | |
2781 | |
2782 if (is_full) { | |
2783 printf("\nAdvanced options:\n" | |
2784 "-alphaq set quantizer parameter for the alpha channel (default = same as -q value)\n" | |
2785 "-premul store the color with premultiplied alpha\n" | |
2786 "-limitedrange encode the color data with the limited range of video\n" | |
2787 "-hash include MD5 hash in HEVC bitstream\n" | |
2788 "-keepmetadata keep the metadata (from JPEG: EXIF, ICC profile, XMP, from PNG: ICC profile)\n" | |
2789 "-v show debug messages\n" | |
2790 ); | |
2791 } | |
2792 | |
2793 exit(1); | |
2794 } | |
2795 | |
2796 struct option long_opts[] = { | |
2797 { "hash", no_argument }, | |
2798 { "keepmetadata", no_argument }, | |
2799 { "alphaq", required_argument }, | |
2800 { "lossless", no_argument }, | |
2801 { "limitedrange", no_argument }, | |
2802 { "premul", no_argument }, | |
2803 { "loop", required_argument }, | |
2804 { "fps", required_argument }, | |
2805 { "delayfile", required_argument }, | |
2806 { NULL }, | |
2807 }; | |
2808 | |
2809 int main(int argc, char **argv) | |
2810 { | |
2811 const char *infilename, *outfilename, *frame_delay_file; | |
2812 Image *img; | |
2813 FILE *f; | |
2814 int c, option_index; | |
2815 int keep_metadata; | |
2816 int bit_depth, i, limited_range, premultiplied_alpha; | |
2817 BPGColorSpaceEnum color_space; | |
2818 BPGMetaData *md; | |
2819 BPGEncoderContext *enc_ctx; | |
2820 BPGEncoderParameters *p; | |
2821 | |
2822 p = bpg_encoder_param_alloc(); | |
2823 | |
2824 outfilename = DEFAULT_OUTFILENAME; | |
2825 color_space = BPG_CS_YCbCr; | |
2826 keep_metadata = 0; | |
2827 bit_depth = DEFAULT_BIT_DEPTH; | |
2828 limited_range = 0; | |
2829 premultiplied_alpha = 0; | |
2830 frame_delay_file = NULL; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2831 |
0 | 2832 for(;;) { |
2833 c = getopt_long_only(argc, argv, "q:o:hf:c:vm:b:e:a", long_opts, &option_index); | |
2834 if (c == -1) | |
2835 break; | |
2836 switch(c) { | |
2837 case 0: | |
2838 switch(option_index) { | |
2839 case 0: | |
2840 p->sei_decoded_picture_hash = 1; | |
2841 break; | |
2842 case 1: | |
2843 keep_metadata = 1; | |
2844 break; | |
2845 case 2: | |
2846 p->alpha_qp = atoi(optarg); | |
2847 if (p->alpha_qp < 0 || p->alpha_qp > 51) { | |
2848 fprintf(stderr, "alpha_qp must be between 0 and 51\n"); | |
2849 exit(1); | |
2850 } | |
2851 break; | |
2852 case 3: | |
2853 p->lossless = 1; | |
2854 color_space = BPG_CS_RGB; | |
2855 p->preferred_chroma_format = BPG_FORMAT_444; | |
2856 bit_depth = 8; | |
2857 limited_range = 0; | |
2858 break; | |
2859 case 4: | |
2860 limited_range = 1; | |
2861 break; | |
2862 case 5: | |
2863 premultiplied_alpha = 1; | |
2864 break; | |
2865 case 6: | |
2866 p->loop_count = strtoul(optarg, NULL, 0); | |
2867 break; | |
2868 case 7: | |
2869 p->frame_delay_num = 1; | |
2870 p->frame_delay_den = strtoul(optarg, NULL, 0); | |
2871 if (p->frame_delay_den == 0) { | |
2872 fprintf(stderr, "invalid frame rate\n"); | |
2873 exit(1); | |
2874 } | |
2875 break; | |
2876 case 8: | |
2877 frame_delay_file = optarg; | |
2878 break; | |
2879 default: | |
2880 goto show_help; | |
2881 } | |
2882 break; | |
2883 case 'h': | |
2884 show_help: | |
2885 help(1); | |
2886 break; | |
2887 case 'q': | |
2888 p->qp = atoi(optarg); | |
2889 if (p->qp < 0 || p->qp > 51) { | |
2890 fprintf(stderr, "qp must be between 0 and 51\n"); | |
2891 exit(1); | |
2892 } | |
2893 break; | |
2894 case 'o': | |
2895 outfilename = optarg; | |
2896 break; | |
2897 case 'f': | |
2898 if (!strcmp(optarg, "420")) { | |
2899 p->preferred_chroma_format = BPG_FORMAT_420; | |
2900 } else if (!strcmp(optarg, "422")) { | |
2901 p->preferred_chroma_format = BPG_FORMAT_422; | |
2902 } else if (!strcmp(optarg, "444")) { | |
2903 p->preferred_chroma_format = BPG_FORMAT_444; | |
2904 } else if (!strcmp(optarg, "422_video")) { | |
2905 p->preferred_chroma_format = BPG_FORMAT_422_VIDEO; | |
2906 } else if (!strcmp(optarg, "420_video")) { | |
2907 p->preferred_chroma_format = BPG_FORMAT_420_VIDEO; | |
2908 } else { | |
2909 fprintf(stderr, "Invalid chroma format\n"); | |
2910 exit(1); | |
2911 } | |
2912 break; | |
2913 case 'c': | |
2914 if (!strcmp(optarg, "ycbcr")) { | |
2915 color_space = BPG_CS_YCbCr; | |
2916 } else if (!strcmp(optarg, "rgb")) { | |
2917 color_space = BPG_CS_RGB; | |
2918 p->preferred_chroma_format = BPG_FORMAT_444; | |
2919 } else if (!strcmp(optarg, "ycgco")) { | |
2920 color_space = BPG_CS_YCgCo; | |
2921 } else if (!strcmp(optarg, "ycbcr_bt709")) { | |
2922 color_space = BPG_CS_YCbCr_BT709; | |
2923 } else if (!strcmp(optarg, "ycbcr_bt2020")) { | |
2924 color_space = BPG_CS_YCbCr_BT2020; | |
2925 } else { | |
2926 fprintf(stderr, "Invalid color space format\n"); | |
2927 exit(1); | |
2928 } | |
2929 break; | |
2930 case 'm': | |
2931 p->compress_level = atoi(optarg); | |
2932 if (p->compress_level < 1) | |
2933 p->compress_level = 1; | |
2934 else if (p->compress_level > 9) | |
2935 p->compress_level = 9; | |
2936 break; | |
2937 case 'b': | |
2938 bit_depth = atoi(optarg); | |
2939 if (bit_depth < 8 || bit_depth > BIT_DEPTH_MAX) { | |
2940 fprintf(stderr, "Invalid bit depth (range: 8 to %d)\n", | |
2941 BIT_DEPTH_MAX); | |
2942 exit(1); | |
2943 } | |
2944 break; | |
2945 case 'v': | |
2946 p->verbose++; | |
2947 break; | |
2948 case 'e': | |
2949 for(i = 0; i < HEVC_ENCODER_COUNT; i++) { | |
2950 if (!strcmp(optarg, hevc_encoder_name[i])) | |
2951 break; | |
2952 } | |
2953 if (i == HEVC_ENCODER_COUNT) { | |
2954 fprintf(stderr, "Unsupported encoder. Available ones are:"); | |
2955 for(i = 0; i < HEVC_ENCODER_COUNT; i++) { | |
2956 fprintf(stderr, " %s", hevc_encoder_name[i]); | |
2957 } | |
2958 fprintf(stderr, "\n"); | |
2959 exit(1); | |
2960 } | |
2961 p->encoder_type = i; | |
2962 break; | |
2963 case 'a': | |
2964 p->animated = 1; | |
2965 break; | |
2966 default: | |
2967 exit(1); | |
2968 } | |
2969 } | |
2970 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2971 if (optind >= argc) |
0 | 2972 help(0); |
2973 infilename = argv[optind]; | |
2974 | |
2975 f = fopen(outfilename, "wb"); | |
2976 if (!f) { | |
2977 perror(outfilename); | |
2978 exit(1); | |
2979 } | |
2980 | |
2981 enc_ctx = bpg_encoder_open(p); | |
2982 if (!enc_ctx) { | |
2983 fprintf(stderr, "Could not open BPG encoder\n"); | |
2984 exit(1); | |
2985 } | |
2986 | |
2987 if (p->animated) { | |
2988 int frame_num, first_frame, frame_ticks; | |
2989 char filename[1024]; | |
2990 FILE *f1; | |
2991 | |
2992 if (frame_delay_file) { | |
2993 f1 = fopen(frame_delay_file, "r"); | |
2994 if (!f1) { | |
2995 fprintf(stderr, "Could not open '%s'\n", frame_delay_file); | |
2996 exit(1); | |
2997 } | |
2998 } else { | |
2999 f1 = NULL; | |
3000 } | |
3001 | |
3002 first_frame = 1; | |
3003 for(frame_num = 0; ; frame_num++) { | |
3004 if (get_filename_num(filename, sizeof(filename), infilename, frame_num) < 0) { | |
3005 fprintf(stderr, "Invalid filename syntax: '%s'\n", infilename); | |
3006 exit(1); | |
3007 } | |
3008 img = load_image(&md, filename, color_space, bit_depth, limited_range, | |
3009 premultiplied_alpha); | |
3010 if (!img) { | |
3011 if (frame_num == 0) | |
3012 continue; /* accept to start at 0 or 1 */ | |
3013 if (first_frame) { | |
3014 fprintf(stderr, "Could not read '%s'\n", filename); | |
3015 exit(1); | |
3016 } else { | |
3017 break; | |
3018 } | |
3019 } | |
3020 frame_ticks = 1; | |
3021 if (f1) { | |
3022 float fdelay; | |
3023 if (fscanf(f1, "%f", &fdelay) == 1) { | |
3024 frame_ticks = lrint(fdelay * p->frame_delay_den / (p->frame_delay_num * 100)); | |
3025 if (frame_ticks < 1) | |
3026 frame_ticks = 1; | |
3027 } | |
3028 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3029 |
0 | 3030 if (p->verbose) |
3031 printf("Encoding '%s' ticks=%d\n", filename, frame_ticks); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3032 |
0 | 3033 if (keep_metadata && first_frame) { |
3034 bpg_encoder_set_extension_data(enc_ctx, md); | |
3035 } else { | |
3036 bpg_md_free(md); | |
3037 } | |
3038 bpg_encoder_set_frame_duration(enc_ctx, frame_ticks); | |
3039 bpg_encoder_encode(enc_ctx, img, my_write_func, f); | |
3040 image_free(img); | |
3041 | |
3042 first_frame = 0; | |
3043 } | |
3044 if (f1) | |
3045 fclose(f1); | |
3046 /* end of stream */ | |
3047 bpg_encoder_encode(enc_ctx, NULL, my_write_func, f); | |
3048 } else { | |
3049 img = load_image(&md, infilename, color_space, bit_depth, limited_range, | |
3050 premultiplied_alpha); | |
3051 if (!img) { | |
3052 fprintf(stderr, "Could not read '%s'\n", infilename); | |
3053 exit(1); | |
3054 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3055 |
0 | 3056 if (!keep_metadata && md) { |
3057 bpg_md_free(md); | |
3058 md = NULL; | |
3059 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3060 |
0 | 3061 bpg_encoder_set_extension_data(enc_ctx, md); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3062 |
0 | 3063 bpg_encoder_encode(enc_ctx, img, my_write_func, f); |
3064 image_free(img); | |
3065 } | |
3066 | |
3067 fclose(f); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3068 |
0 | 3069 bpg_encoder_close(enc_ctx); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3070 |
0 | 3071 bpg_encoder_param_free(p); |
3072 | |
3073 return 0; | |
3074 } |