Mercurial > hg > forks > libbpg
annotate bpgenc.c @ 7:6dc09314d44b
Improve error handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2016 12:27:40 +0200 |
parents | 690bf78c1ce9 |
children | 5a1eec3c43cc |
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) |
7 | 931 { |
932 fprintf(stderr, "Could not open TIFF image '%s'\n", filename); | |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
933 return NULL; |
7 | 934 } |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
935 |
7 | 936 if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img_width) || |
937 !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img_height) || | |
938 !TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &img_spp) || | |
939 !TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &img_depth)) | |
940 { | |
941 fprintf(stderr, "TIFF file lacks basic fields!\n"); | |
942 goto err; | |
943 } | |
944 | |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
945 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
|
946 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
947 // Check basics |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
948 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
|
949 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
950 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
951 img_linesize = TIFFScanlineSize(tif); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
952 img_format = BPG_FORMAT_444; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
953 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
|
954 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
955 if (img_spp > 3) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
956 { |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
957 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
|
958 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
959 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
|
960 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
|
961 } |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
962 else |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
963 img_alpha = 0; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
964 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
965 // Allocate temporary image space |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
966 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
|
967 if (buf == NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
968 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
969 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
970 // Allocate target image |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
971 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
|
972 if (img == NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
973 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
974 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
975 img->limited_range = limited_range; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
976 img->premultiplied_alpha = premultiplied_alpha; |
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 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
|
979 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
980 int idx = img_depth == 16; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
981 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
|
982 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
983 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
|
984 { |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
985 // Read TIFF image to raster |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
986 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
|
987 goto err; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
988 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
989 // Convert |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
990 convert_func(&cvt, |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
991 (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
|
992 (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
|
993 (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
|
994 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
|
995 |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
996 if (img_alpha) |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
997 { |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
998 if (idx) |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
999 { |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1000 gray16_to_gray(&cvt, |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1001 (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
|
1002 ((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
|
1003 } |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1004 else |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1005 { |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1006 gray8_to_gray(&cvt, |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1007 (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
|
1008 ((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
|
1009 } |
690bf78c1ce9
Initial implementation of alpha channel support for TIFF images.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
1010 } |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1011 } |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1012 |
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 err: |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1015 if (buf != NULL) |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1016 _TIFFfree(buf); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1017 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1018 TIFFClose(tif); |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1019 return img; |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1020 } |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1021 |
0 | 1022 Image *read_png(BPGMetaData **pmd, |
1023 FILE *f, BPGColorSpaceEnum color_space, int out_bit_depth, | |
1024 int limited_range, int premultiplied_alpha) | |
1025 { | |
1026 png_structp png_ptr; | |
1027 png_infop info_ptr; | |
1028 int bit_depth, color_type; | |
1029 Image *img; | |
1030 uint8_t **rows; | |
1031 int y, has_alpha, linesize, bpp; | |
1032 BPGImageFormatEnum format; | |
1033 ColorConvertState cvt_s, *cvt = &cvt_s; | |
1034 BPGMetaData *md, **plast_md, *first_md; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1035 |
0 | 1036 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, |
1037 NULL, NULL, NULL); | |
1038 if (png_ptr == NULL) { | |
1039 return NULL; | |
1040 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1041 |
0 | 1042 info_ptr = png_create_info_struct(png_ptr); |
1043 if (info_ptr == NULL) { | |
1044 png_destroy_read_struct(&png_ptr, NULL, NULL); | |
1045 return NULL; | |
1046 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1047 |
0 | 1048 if (setjmp(png_jmpbuf(png_ptr))) { |
1049 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); | |
1050 return NULL; | |
1051 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1052 |
0 | 1053 png_init_io(png_ptr, f); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1054 |
0 | 1055 png_read_info(png_ptr, info_ptr); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1056 |
0 | 1057 bit_depth = png_get_bit_depth(png_ptr, info_ptr); |
1058 color_type = png_get_color_type(png_ptr, info_ptr); | |
1059 | |
1060 switch (color_type) { | |
1061 case PNG_COLOR_TYPE_PALETTE: | |
1062 png_set_palette_to_rgb(png_ptr); | |
1063 bit_depth = 8; | |
1064 break; | |
1065 case PNG_COLOR_TYPE_GRAY: | |
1066 case PNG_COLOR_TYPE_GRAY_ALPHA: | |
1067 if (bit_depth < 8) { | |
1068 png_set_expand_gray_1_2_4_to_8(png_ptr); | |
1069 bit_depth = 8; | |
1070 } | |
1071 break; | |
1072 } | |
1073 assert(bit_depth == 8 || bit_depth == 16); | |
1074 | |
1075 #if __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__ | |
1076 if (bit_depth == 16) { | |
1077 png_set_swap(png_ptr); | |
1078 } | |
1079 #endif | |
1080 | |
1081 if (color_type == PNG_COLOR_TYPE_GRAY || | |
1082 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { | |
1083 format = BPG_FORMAT_GRAY; | |
1084 color_space = BPG_CS_YCbCr; | |
1085 } else { | |
1086 format = BPG_FORMAT_444; | |
1087 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1088 |
0 | 1089 has_alpha = (color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
1090 color_type == PNG_COLOR_TYPE_RGB_ALPHA); | |
1091 | |
1092 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { | |
1093 png_set_tRNS_to_alpha(png_ptr); | |
1094 has_alpha = 1; | |
1095 } | |
1096 | |
1097 if (premultiplied_alpha) { | |
1098 png_set_alpha_mode(png_ptr, PNG_ALPHA_ASSOCIATED, PNG_GAMMA_LINEAR); | |
1099 } | |
1100 | |
1101 img = image_alloc(png_get_image_width(png_ptr, info_ptr), | |
1102 png_get_image_height(png_ptr, info_ptr), | |
1103 format, has_alpha, color_space, | |
1104 out_bit_depth); | |
1105 img->limited_range = limited_range; | |
1106 img->premultiplied_alpha = premultiplied_alpha; | |
1107 | |
1108 rows = malloc(sizeof(rows[0]) * img->h); | |
1109 if (format == BPG_FORMAT_GRAY) | |
1110 bpp = (1 + has_alpha) * (bit_depth / 8); | |
1111 else | |
1112 bpp = (3 + has_alpha) * (bit_depth / 8); | |
1113 linesize = bpp * img->w; | |
1114 for (y = 0; y < img->h; y++) { | |
1115 rows[y] = malloc(linesize); | |
1116 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1117 |
0 | 1118 png_read_image(png_ptr, rows); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1119 |
0 | 1120 convert_init(cvt, bit_depth, out_bit_depth, color_space, limited_range); |
1121 | |
1122 if (format != BPG_FORMAT_GRAY) { | |
1123 int idx; | |
1124 RGBConvertFunc *convert_func; | |
1125 | |
1126 idx = (bit_depth == 16); | |
1127 convert_func = rgb_to_cs[idx][color_space]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1128 |
0 | 1129 for (y = 0; y < img->h; y++) { |
1130 convert_func(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]), | |
1131 (PIXEL *)(img->data[1] + y * img->linesize[1]), | |
1132 (PIXEL *)(img->data[2] + y * img->linesize[2]), | |
1133 rows[y], img->w, 3 + has_alpha); | |
1134 if (has_alpha) { | |
1135 if (idx) { | |
1136 gray16_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]), | |
1137 (uint16_t *)rows[y] + 3, img->w, 4); | |
1138 } else { | |
1139 gray8_to_gray(cvt, (PIXEL *)(img->data[3] + y * img->linesize[3]), | |
1140 rows[y] + 3, img->w, 4); | |
1141 } | |
1142 } | |
1143 } | |
1144 } else { | |
1145 if (bit_depth == 16) { | |
1146 for (y = 0; y < img->h; y++) { | |
1147 luma16_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]), | |
1148 (uint16_t *)rows[y], img->w, 1 + has_alpha); | |
1149 if (has_alpha) { | |
1150 gray16_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]), | |
1151 (uint16_t *)rows[y] + 1, img->w, 2); | |
1152 } | |
1153 } | |
1154 } else { | |
1155 for (y = 0; y < img->h; y++) { | |
1156 luma8_to_gray(cvt, (PIXEL *)(img->data[0] + y * img->linesize[0]), | |
1157 rows[y], img->w, 1 + has_alpha); | |
1158 if (has_alpha) { | |
1159 gray8_to_gray(cvt, (PIXEL *)(img->data[1] + y * img->linesize[1]), | |
1160 rows[y] + 1, img->w, 2); | |
1161 } | |
1162 } | |
1163 } | |
1164 } | |
1165 | |
1166 for (y = 0; y < img->h; y++) { | |
1167 free(rows[y]); | |
1168 } | |
1169 free(rows); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1170 |
0 | 1171 png_read_end(png_ptr, info_ptr); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1172 |
0 | 1173 /* get the ICC profile if present */ |
1174 first_md = NULL; | |
1175 plast_md = &first_md; | |
1176 { | |
1177 png_charp name; | |
1178 int comp_type; | |
1179 png_bytep iccp_buf; | |
1180 png_uint_32 iccp_buf_len; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1181 |
0 | 1182 if (png_get_iCCP(png_ptr, info_ptr, |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1183 &name, &comp_type, &iccp_buf, &iccp_buf_len) == |
0 | 1184 PNG_INFO_iCCP) { |
1185 md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP); | |
1186 md->buf_len = iccp_buf_len; | |
1187 md->buf = malloc(iccp_buf_len); | |
1188 memcpy(md->buf, iccp_buf, iccp_buf_len); | |
1189 *plast_md = md; | |
1190 plast_md = &md->next; | |
1191 } | |
1192 } | |
1193 | |
1194 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1195 |
0 | 1196 *pmd = first_md; |
1197 return img; | |
1198 } | |
1199 | |
1200 static BPGMetaData *jpeg_get_metadata(jpeg_saved_marker_ptr first_marker) | |
1201 { | |
1202 static const char app1_exif[] = "Exif"; | |
1203 static const char app1_xmp[] = "http://ns.adobe.com/xap/1.0/"; | |
1204 static const char app2_iccp[] = "ICC_PROFILE"; | |
1205 jpeg_saved_marker_ptr marker; | |
1206 BPGMetaData *md, **plast_md, *first_md; | |
1207 int has_exif, has_xmp, l, iccp_chunk_count, i; | |
1208 jpeg_saved_marker_ptr iccp_chunks[256]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1209 |
0 | 1210 iccp_chunk_count = 0; |
1211 has_exif = 0; | |
1212 has_xmp = 0; | |
1213 first_md = NULL; | |
1214 plast_md = &first_md; | |
1215 for (marker = first_marker; marker != NULL; marker = marker->next) { | |
1216 #if 0 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1217 printf("marker=APP%d len=%d\n", |
0 | 1218 marker->marker - JPEG_APP0, marker->data_length); |
1219 #endif | |
1220 if (!has_exif && marker->marker == JPEG_APP0 + 1 && | |
1221 marker->data_length > sizeof(app1_exif) && | |
1222 !memcmp(marker->data, app1_exif, sizeof(app1_exif))) { | |
1223 md = bpg_md_alloc(BPG_EXTENSION_TAG_EXIF); | |
1224 l = sizeof(app1_exif); | |
1225 md->buf_len = marker->data_length - l; | |
1226 md->buf = malloc(md->buf_len); | |
1227 memcpy(md->buf, marker->data + l, md->buf_len); | |
1228 *plast_md = md; | |
1229 plast_md = &md->next; | |
1230 has_exif = 1; | |
1231 } else if (!has_xmp && marker->marker == JPEG_APP0 + 1 && | |
1232 marker->data_length > sizeof(app1_xmp) && | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1233 !memcmp(marker->data, app1_xmp, sizeof(app1_xmp)) && |
0 | 1234 !has_xmp) { |
1235 md = bpg_md_alloc(BPG_EXTENSION_TAG_XMP); | |
1236 l = sizeof(app1_xmp); | |
1237 md->buf_len = marker->data_length - l; | |
1238 md->buf = malloc(md->buf_len); | |
1239 memcpy(md->buf, marker->data + l, md->buf_len); | |
1240 *plast_md = md; | |
1241 plast_md = &md->next; | |
1242 has_xmp = 1; | |
1243 } else if (marker->marker == JPEG_APP0 + 2 && | |
1244 marker->data_length > (sizeof(app2_iccp) + 2) && | |
1245 !memcmp(marker->data, app2_iccp, sizeof(app2_iccp))) { | |
1246 int chunk_count, chunk_index; | |
1247 l = sizeof(app2_iccp); | |
1248 chunk_index = marker->data[l]; | |
1249 chunk_count = marker->data[l]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1250 if (chunk_index == 0 || chunk_count == 0) |
0 | 1251 continue; |
1252 if (iccp_chunk_count == 0) { | |
1253 iccp_chunk_count = chunk_count; | |
1254 for(i = 0; i < chunk_count; i++) { | |
1255 iccp_chunks[i] = NULL; | |
1256 } | |
1257 } else { | |
1258 if (chunk_count != iccp_chunk_count) | |
1259 continue; | |
1260 } | |
1261 if (chunk_index > iccp_chunk_count) | |
1262 continue; | |
1263 iccp_chunks[chunk_index - 1] = marker; | |
1264 } | |
1265 } | |
1266 | |
1267 if (iccp_chunk_count != 0) { | |
1268 int len, hlen, idx; | |
1269 /* check that no chunk are missing */ | |
1270 len = 0; | |
1271 hlen = sizeof(app2_iccp) + 2; | |
1272 for(i = 0; i < iccp_chunk_count; i++) { | |
1273 if (!iccp_chunks[i]) | |
1274 break; | |
1275 len += iccp_chunks[i]->data_length - hlen; | |
1276 } | |
1277 if (i == iccp_chunk_count) { | |
1278 md = bpg_md_alloc(BPG_EXTENSION_TAG_ICCP); | |
1279 md->buf_len = len; | |
1280 md->buf = malloc(md->buf_len); | |
1281 idx = 0; | |
1282 for(i = 0; i < iccp_chunk_count; i++) { | |
1283 l = iccp_chunks[i]->data_length - hlen; | |
1284 memcpy(md->buf + idx, iccp_chunks[i]->data + hlen, l); | |
1285 idx += l; | |
1286 } | |
1287 assert(idx == len); | |
1288 *plast_md = md; | |
1289 plast_md = &md->next; | |
1290 } | |
1291 } | |
1292 return first_md; | |
1293 } | |
1294 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1295 Image *read_jpeg(BPGMetaData **pmd, FILE *f, |
0 | 1296 int out_bit_depth) |
1297 { | |
1298 struct jpeg_decompress_struct cinfo; | |
1299 struct jpeg_error_mgr jerr; | |
1300 int w, h, w1, i, y_h, c_h, y, v_shift, c_w, y1, idx, c_idx, h_shift; | |
1301 int h1, plane_idx[4], has_alpha, has_w_plane; | |
1302 Image *img; | |
1303 BPGImageFormatEnum format; | |
1304 BPGColorSpaceEnum color_space; | |
1305 ColorConvertState cvt_s, *cvt = &cvt_s; | |
1306 BPGMetaData *first_md = NULL; | |
1307 uint32_t comp_hv; | |
1308 | |
1309 cinfo.err = jpeg_std_error(&jerr); | |
1310 jpeg_create_decompress(&cinfo); | |
1311 | |
1312 jpeg_save_markers(&cinfo, JPEG_APP0 + 1, 65535); | |
1313 jpeg_save_markers(&cinfo, JPEG_APP0 + 2, 65535); | |
1314 | |
1315 jpeg_stdio_src(&cinfo, f); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1316 |
0 | 1317 jpeg_read_header(&cinfo, TRUE); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1318 |
0 | 1319 cinfo.raw_data_out = TRUE; |
1320 cinfo.do_fancy_upsampling = TRUE; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1321 |
0 | 1322 w = cinfo.image_width; |
1323 h = cinfo.image_height; | |
1324 | |
1325 has_w_plane = 0; | |
1326 comp_hv = 0; | |
1327 if (cinfo.num_components < 1 || cinfo.num_components > 4) | |
1328 goto unsupported; | |
1329 for(i = 0; i < cinfo.num_components; i++) { | |
1330 comp_hv |= cinfo.comp_info[i].h_samp_factor << (i * 8 + 4); | |
1331 comp_hv |= cinfo.comp_info[i].v_samp_factor << (i * 8); | |
1332 } | |
1333 switch(cinfo.jpeg_color_space) { | |
1334 case JCS_GRAYSCALE: | |
1335 if (cinfo.num_components != 1 || comp_hv != 0x11) | |
1336 goto unsupported; | |
1337 format = BPG_FORMAT_GRAY; | |
1338 color_space = BPG_CS_YCbCr; | |
1339 break; | |
1340 case JCS_YCbCr: | |
1341 if (cinfo.num_components != 3) | |
1342 goto unsupported; | |
1343 switch(comp_hv) { | |
1344 case 0x111111: | |
1345 format = BPG_FORMAT_444; | |
1346 break; | |
1347 case 0x111121: | |
1348 format = BPG_FORMAT_422; | |
1349 break; | |
1350 case 0x111122: | |
1351 format = BPG_FORMAT_420; | |
1352 break; | |
1353 default: | |
1354 cinfo.raw_data_out = FALSE; | |
1355 format = BPG_FORMAT_444; | |
1356 cinfo.out_color_space = JCS_YCbCr; | |
1357 break; | |
1358 } | |
1359 color_space = BPG_CS_YCbCr; | |
1360 break; | |
1361 case JCS_RGB: | |
1362 if (cinfo.num_components != 3) | |
1363 goto unsupported; | |
1364 format = BPG_FORMAT_444; | |
1365 color_space = BPG_CS_RGB; | |
1366 cinfo.raw_data_out = FALSE; | |
1367 cinfo.out_color_space = JCS_RGB; | |
1368 break; | |
1369 case JCS_YCCK: | |
1370 if (cinfo.num_components != 4) | |
1371 goto unsupported; | |
1372 switch(comp_hv) { | |
1373 case 0x11111111: | |
1374 format = BPG_FORMAT_444; | |
1375 color_space = BPG_CS_YCbCr; | |
1376 break; | |
1377 case 0x22111121: | |
1378 format = BPG_FORMAT_422; | |
1379 color_space = BPG_CS_YCbCr; | |
1380 break; | |
1381 case 0x22111122: | |
1382 format = BPG_FORMAT_420; | |
1383 color_space = BPG_CS_YCbCr; | |
1384 break; | |
1385 default: | |
1386 cinfo.raw_data_out = FALSE; | |
1387 format = BPG_FORMAT_444; | |
1388 cinfo.out_color_space = JCS_CMYK; | |
1389 color_space = BPG_CS_RGB; | |
1390 break; | |
1391 } | |
1392 has_w_plane = 1; | |
1393 break; | |
1394 case JCS_CMYK: | |
1395 if (cinfo.num_components != 4) | |
1396 goto unsupported; | |
1397 format = BPG_FORMAT_444; | |
1398 color_space = BPG_CS_RGB; | |
1399 has_w_plane = 1; | |
1400 cinfo.raw_data_out = FALSE; | |
1401 cinfo.out_color_space = JCS_CMYK; | |
1402 break; | |
1403 default: | |
1404 unsupported: | |
1405 fprintf(stderr, "Unsupported JPEG parameters (cs=%d n_comp=%d comp_hv=%x)\n", | |
1406 cinfo.jpeg_color_space, cinfo.num_components, comp_hv); | |
1407 img = NULL; | |
1408 goto the_end; | |
1409 } | |
1410 | |
1411 v_shift = (format == BPG_FORMAT_420); | |
1412 h_shift = (format == BPG_FORMAT_422 || format == BPG_FORMAT_420); | |
1413 has_alpha = (cinfo.num_components == 4); | |
1414 img = image_alloc(w, h, format, has_alpha, color_space, out_bit_depth); | |
1415 img->has_w_plane = has_w_plane; | |
1416 | |
1417 convert_init(cvt, 8, out_bit_depth, color_space, 0); | |
1418 | |
1419 jpeg_start_decompress(&cinfo); | |
1420 | |
1421 if (color_space == BPG_CS_RGB) { | |
1422 plane_idx[0] = 2; | |
1423 plane_idx[1] = 0; | |
1424 plane_idx[2] = 1; | |
1425 } else { | |
1426 plane_idx[0] = 0; | |
1427 plane_idx[1] = 1; | |
1428 plane_idx[2] = 2; | |
1429 } | |
1430 plane_idx[3] = 3; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1431 |
0 | 1432 if (cinfo.raw_data_out) { |
1433 JSAMPROW rows[4][16]; | |
1434 JSAMPROW *plane_pointer[4]; | |
1435 | |
1436 y_h = 8 * cinfo.max_v_samp_factor; | |
1437 if (cinfo.num_components == 1) { | |
1438 c_h = 0; | |
1439 c_w = 0; | |
1440 } else { | |
1441 c_h = 8; | |
1442 if (h_shift) | |
1443 c_w = (w + 1) / 2; | |
1444 else | |
1445 c_w = w; | |
1446 } | |
1447 w1 = (w + 15) & ~15; | |
1448 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) { | |
1449 if (c_idx == 1 || c_idx == 2) { | |
1450 h1 = c_h; | |
1451 } else { | |
1452 h1 = y_h; | |
1453 } | |
1454 for(i = 0; i < h1; i++) { | |
1455 rows[c_idx][i] = malloc(w1); | |
1456 } | |
1457 plane_pointer[c_idx] = rows[c_idx]; | |
1458 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1459 |
0 | 1460 while (cinfo.output_scanline < cinfo.output_height) { |
1461 y = cinfo.output_scanline; | |
1462 jpeg_read_raw_data(&cinfo, plane_pointer, y_h); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1463 |
0 | 1464 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) { |
1465 if (c_idx == 1 || c_idx == 2) { | |
1466 h1 = c_h; | |
1467 w1 = c_w; | |
1468 y1 = (y >> v_shift); | |
1469 } else { | |
1470 h1 = y_h; | |
1471 w1 = img->w; | |
1472 y1 = y; | |
1473 } | |
1474 idx = plane_idx[c_idx]; | |
1475 for(i = 0; i < h1; i++) { | |
1476 PIXEL *ptr; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1477 ptr = (PIXEL *)(img->data[idx] + |
0 | 1478 img->linesize[idx] * (y1 + i)); |
1479 gray8_to_gray(cvt, ptr, rows[c_idx][i], w1, 1); | |
1480 if (color_space == BPG_CS_YCbCr && has_w_plane) { | |
1481 /* negate color */ | |
1482 if (c_idx == 0) { | |
1483 gray_one_minus(cvt, ptr, w1); | |
1484 } else if (c_idx <= 2) { | |
1485 gray_neg_c(cvt, ptr, w1); | |
1486 } | |
1487 } | |
1488 } | |
1489 } | |
1490 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1491 |
0 | 1492 for(c_idx = 0; c_idx < cinfo.num_components; c_idx++) { |
1493 if (c_idx == 1 || c_idx == 2) { | |
1494 h1 = c_h; | |
1495 } else { | |
1496 h1 = y_h; | |
1497 } | |
1498 for(i = 0; i < h1; i++) { | |
1499 free(rows[c_idx][i]); | |
1500 } | |
1501 } | |
1502 } else { | |
1503 JSAMPROW rows[1]; | |
1504 uint8_t *buf; | |
1505 int c_count; | |
1506 | |
1507 c_count = 3 + has_w_plane; | |
1508 buf = malloc(c_count * w); | |
1509 rows[0] = buf; | |
1510 while (cinfo.output_scanline < cinfo.output_height) { | |
1511 y = cinfo.output_scanline; | |
1512 jpeg_read_scanlines(&cinfo, rows, 1); | |
1513 | |
1514 for(c_idx = 0; c_idx < c_count; c_idx++) { | |
1515 idx = plane_idx[c_idx]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1516 gray8_to_gray(cvt, (PIXEL *)(img->data[idx] + |
0 | 1517 img->linesize[idx] * y), |
1518 buf + c_idx, w, c_count); | |
1519 } | |
1520 } | |
1521 free(buf); | |
1522 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1523 |
0 | 1524 first_md = jpeg_get_metadata(cinfo.marker_list); |
1525 | |
1526 the_end: | |
1527 jpeg_finish_decompress(&cinfo); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1528 |
0 | 1529 jpeg_destroy_decompress(&cinfo); |
1530 *pmd = first_md; | |
1531 return img; | |
1532 } | |
1533 | |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1534 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
|
1535 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
|
1536 #define PROBE_BUF_SIZE 8 |
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1537 |
0 | 1538 Image *load_image(BPGMetaData **pmd, const char *infilename, |
1539 BPGColorSpaceEnum color_space, int bit_depth, | |
1540 int limited_range, int premultiplied_alpha) | |
1541 { | |
1542 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
|
1543 Image *img = NULL; |
0 | 1544 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
|
1545 uint8_t buf[PROBE_BUF_SIZE]; |
0 | 1546 |
1547 *pmd = NULL; | |
1548 | |
3
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1549 if ((f = fopen(infilename, "rb")) == NULL) |
0 | 1550 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
|
1551 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1552 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
|
1553 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
|
1554 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1555 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
|
1556 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1557 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
|
1558 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
|
1559 else |
4
bbd61622856a
Initial implementation of TIFF format read support via libtiff.
Matti Hamalainen <ccr@tnsp.org>
parents:
3
diff
changeset
|
1560 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
|
1561 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
|
1562 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
|
1563 else |
0 | 1564 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
|
1565 |
ffcd1967fb5c
Clean up load_image() probing code a bit in preparation for TIFF support.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1566 err: |
0 | 1567 fclose(f); |
1568 *pmd = md; | |
1569 return img; | |
1570 } | |
1571 | |
1572 void save_yuv1(Image *img, FILE *f) | |
1573 { | |
1574 int c_w, c_h, i, c_count, y; | |
1575 | |
1576 if (img->format == BPG_FORMAT_GRAY) | |
1577 c_count = 1; | |
1578 else | |
1579 c_count = 3; | |
1580 for(i = 0; i < c_count; i++) { | |
1581 get_plane_res(img, &c_w, &c_h, i); | |
1582 for(y = 0; y < c_h; y++) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1583 fwrite(img->data[i] + y * img->linesize[i], |
0 | 1584 1, c_w << img->pixel_shift, f); |
1585 } | |
1586 } | |
1587 } | |
1588 | |
1589 void save_yuv(Image *img, const char *filename) | |
1590 { | |
1591 FILE *f; | |
1592 | |
1593 f = fopen(filename, "wb"); | |
1594 if (!f) { | |
1595 fprintf(stderr, "Could not open %s\n", filename); | |
1596 exit(1); | |
1597 } | |
1598 save_yuv1(img, f); | |
1599 fclose(f); | |
1600 } | |
1601 | |
1602 /* return the position of the end of the NAL or -1 if error */ | |
1603 static int find_nal_end(const uint8_t *buf, int buf_len) | |
1604 { | |
1605 int idx; | |
1606 | |
1607 idx = 0; | |
1608 if (buf_len >= 4 && | |
1609 buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 1) { | |
1610 idx = 4; | |
1611 } else if (buf_len >= 3 && | |
1612 buf[0] == 0 && buf[1] == 0 && buf[2] == 1) { | |
1613 idx = 3; | |
1614 } else { | |
1615 return -1; | |
1616 } | |
1617 /* NAL header */ | |
1618 if (idx + 2 > buf_len) | |
1619 return -1; | |
1620 /* find the last byte */ | |
1621 for(;;) { | |
1622 if (idx + 2 >= buf_len) { | |
1623 idx = buf_len; | |
1624 break; | |
1625 } | |
1626 if (buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 1) | |
1627 break; | |
1628 if (idx + 3 < buf_len && | |
1629 buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 0 && buf[idx + 3] == 1) | |
1630 break; | |
1631 idx++; | |
1632 } | |
1633 return idx; | |
1634 } | |
1635 | |
1636 /* 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
|
1637 static int extract_nal(uint8_t **pnal_buf, int *pnal_len, |
0 | 1638 const uint8_t *buf, int buf_len) |
1639 { | |
1640 int idx, start, end, len; | |
1641 uint8_t *nal_buf; | |
1642 int nal_len; | |
1643 | |
1644 end = find_nal_end(buf, buf_len); | |
1645 if (end < 0) | |
1646 return -1; | |
1647 if (buf[2] == 1) | |
1648 start = 3; | |
1649 else | |
1650 start = 4; | |
1651 len = end - start; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1652 |
0 | 1653 nal_buf = malloc(len); |
1654 nal_len = 0; | |
1655 idx = start; | |
1656 while (idx < end) { | |
1657 if (idx + 2 < end && buf[idx] == 0 && buf[idx + 1] == 0 && buf[idx + 2] == 3) { | |
1658 nal_buf[nal_len++] = 0; | |
1659 nal_buf[nal_len++] = 0; | |
1660 idx += 3; | |
1661 } else { | |
1662 nal_buf[nal_len++] = buf[idx++]; | |
1663 } | |
1664 } | |
1665 while (idx < end) { | |
1666 nal_buf[nal_len++] = buf[idx++]; | |
1667 } | |
1668 *pnal_buf = nal_buf; | |
1669 *pnal_len = nal_len; | |
1670 return idx; | |
1671 } | |
1672 | |
1673 /* big endian variable length 7 bit encoding */ | |
1674 static void put_ue(uint8_t **pp, uint32_t v) | |
1675 { | |
1676 uint8_t *p = *pp; | |
1677 int i, j; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1678 |
0 | 1679 for(i = 1; i < 5; i++) { |
1680 if (v < (1 << (7 * i))) | |
1681 break; | |
1682 } | |
1683 for(j = i - 1; j >= 1; j--) | |
1684 *p++ = ((v >> (7 * j)) & 0x7f) | 0x80; | |
1685 *p++ = v & 0x7f; | |
1686 *pp = p; | |
1687 } | |
1688 | |
1689 typedef struct { | |
1690 const uint8_t *buf; | |
1691 int idx; | |
1692 int buf_len; | |
1693 } GetBitState; | |
1694 | |
1695 static void init_get_bits(GetBitState *s, const uint8_t *buf, int buf_len) | |
1696 { | |
1697 s->buf = buf; | |
1698 s->buf_len = buf_len; | |
1699 s->idx = 0; | |
1700 } | |
1701 | |
1702 static void skip_bits(GetBitState *s, int n) | |
1703 { | |
1704 s->idx += n; | |
1705 } | |
1706 | |
1707 /* 1 <= n <= 25. return '0' bits if past the end of the buffer. */ | |
1708 static uint32_t get_bits(GetBitState *s, int n) | |
1709 { | |
1710 const uint8_t *buf = s->buf; | |
1711 int p, i; | |
1712 uint32_t v; | |
1713 | |
1714 p = s->idx >> 3; | |
1715 if ((p + 3) < s->buf_len) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1716 v = (buf[p] << 24) | (buf[p + 1] << 16) | |
0 | 1717 (buf[p + 2] << 8) | buf[p + 3]; |
1718 } else { | |
1719 v = 0; | |
1720 for(i = 0; i < 3; i++) { | |
1721 if ((p + i) < s->buf_len) | |
1722 v |= buf[p + i] << (24 - i * 8); | |
1723 } | |
1724 } | |
1725 v = (v >> (32 - (s->idx & 7) - n)) & ((1 << n) - 1); | |
1726 s->idx += n; | |
1727 return v; | |
1728 } | |
1729 | |
1730 /* 1 <= n <= 32 */ | |
1731 static uint32_t get_bits_long(GetBitState *s, int n) | |
1732 { | |
1733 uint32_t v; | |
1734 | |
1735 if (n <= 25) { | |
1736 v = get_bits(s, n); | |
1737 } else { | |
1738 n -= 16; | |
1739 v = get_bits(s, 16) << n; | |
1740 v |= get_bits(s, n); | |
1741 } | |
1742 return v; | |
1743 } | |
1744 | |
1745 /* at most 32 bits are supported */ | |
1746 static uint32_t get_ue_golomb(GetBitState *s) | |
1747 { | |
1748 int i; | |
1749 i = 0; | |
1750 for(;;) { | |
1751 if (get_bits(s, 1)) | |
1752 break; | |
1753 i++; | |
1754 if (i == 32) | |
1755 return 0xffffffff; | |
1756 } | |
1757 if (i == 0) | |
1758 return 0; | |
1759 else | |
1760 return ((1 << i) | get_bits_long(s, i)) - 1; | |
1761 } | |
1762 | |
1763 typedef struct { | |
1764 uint8_t *buf; | |
1765 int idx; | |
1766 } PutBitState; | |
1767 | |
1768 static void init_put_bits(PutBitState *s, uint8_t *buf) | |
1769 { | |
1770 s->buf = buf; | |
1771 s->idx = 0; | |
1772 } | |
1773 | |
1774 static void put_bit(PutBitState *s, int bit) | |
1775 { | |
1776 s->buf[s->idx >> 3] |= bit << (7 - (s->idx & 7)); | |
1777 s->idx++; | |
1778 } | |
1779 | |
1780 static void put_bits(PutBitState *s, int n, uint32_t v) | |
1781 { | |
1782 int i; | |
1783 | |
1784 for(i = 0; i < n; i++) { | |
1785 put_bit(s, (v >> (n - 1 - i)) & 1); | |
1786 } | |
1787 } | |
1788 | |
1789 static void put_ue_golomb(PutBitState *s, uint32_t v) | |
1790 { | |
1791 uint32_t a; | |
1792 int n; | |
1793 | |
1794 v++; | |
1795 n = 0; | |
1796 a = v; | |
1797 while (a != 0) { | |
1798 a >>= 1; | |
1799 n++; | |
1800 } | |
1801 if (n > 1) | |
1802 put_bits(s, n - 1, 0); | |
1803 put_bits(s, n, v); | |
1804 } | |
1805 | |
1806 typedef struct { | |
1807 uint8_t *buf; | |
1808 int size; | |
1809 int len; | |
1810 } DynBuf; | |
1811 | |
1812 static void dyn_buf_init(DynBuf *s) | |
1813 { | |
1814 s->buf = NULL; | |
1815 s->size = 0; | |
1816 s->len = 0; | |
1817 } | |
1818 | |
1819 static int dyn_buf_resize(DynBuf *s, int size) | |
1820 { | |
1821 int new_size; | |
1822 uint8_t *new_buf; | |
1823 | |
1824 if (size <= s->size) | |
1825 return 0; | |
1826 new_size = (s->size * 3) / 2; | |
1827 if (new_size < size) | |
1828 new_size = size; | |
1829 new_buf = realloc(s->buf, new_size); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1830 if (!new_buf) |
0 | 1831 return -1; |
1832 s->buf = new_buf; | |
1833 s->size = new_size; | |
1834 return 0; | |
1835 } | |
1836 | |
1837 /* suppress the VPS NAL and keep only the useful part of the SPS | |
1838 header. The decoder can rebuild a valid HEVC stream if needed. */ | |
1839 static int build_modified_sps(uint8_t **pout_buf, int *pout_buf_len, | |
1840 const uint8_t *buf, int buf_len) | |
1841 { | |
1842 int nal_unit_type, nal_len, idx, i, ret, msps_buf_len; | |
1843 int out_buf_len, out_buf_len_max; | |
1844 uint8_t *nal_buf, *msps_buf, *out_buf; | |
1845 GetBitState gb_s, *gb = &gb_s; | |
1846 PutBitState pb_s, *pb = &pb_s; | |
1847 uint8_t *p; | |
1848 | |
1849 idx = extract_nal(&nal_buf, &nal_len, buf, buf_len); | |
1850 if (idx < 0) | |
1851 return -1; | |
1852 if (nal_len < 2) { | |
1853 free(nal_buf); | |
1854 return -1; | |
1855 } | |
1856 nal_unit_type = (nal_buf[0] >> 1) & 0x3f; | |
1857 free(nal_buf); | |
1858 if (nal_unit_type != 32) { | |
1859 fprintf(stderr, "expecting VPS nal (%d)\n", nal_unit_type); | |
1860 return -1; /* expect VPS nal */ | |
1861 } | |
1862 | |
1863 ret = extract_nal(&nal_buf, &nal_len, buf + idx, buf_len); | |
1864 if (ret < 0) | |
1865 return -1; | |
1866 idx += ret; | |
1867 if (nal_len < 2) | |
1868 return -1; | |
1869 nal_unit_type = (nal_buf[0] >> 1) & 0x3f; | |
1870 if (nal_unit_type != 33) { | |
1871 fprintf(stderr, "expecting SPS nal (%d)\n", nal_unit_type); | |
1872 return -1; /* expect SPS nal */ | |
1873 } | |
1874 | |
1875 /* skip the initial part of the SPS up to and including | |
1876 log2_min_cb_size */ | |
1877 { | |
1878 int vps_id, max_sub_layers, profile_idc, sps_id; | |
1879 int chroma_format_idc, width, height, bit_depth_luma, bit_depth_chroma; | |
1880 int log2_max_poc_lsb, sublayer_ordering_info, log2_min_cb_size; | |
1881 int log2_diff_max_min_coding_block_size, log2_min_tb_size; | |
1882 int log2_diff_max_min_transform_block_size; | |
1883 int max_transform_hierarchy_depth_inter; | |
1884 int max_transform_hierarchy_depth_intra; | |
1885 int scaling_list_enable_flag, amp_enabled_flag, sao_enabled; | |
1886 int pcm_enabled_flag, nb_st_rps; | |
1887 int long_term_ref_pics_present_flag, sps_strong_intra_smoothing_enable_flag, vui_present; | |
1888 int sps_temporal_mvp_enabled_flag; | |
1889 int pcm_sample_bit_depth_luma_minus1; | |
1890 int pcm_sample_bit_depth_chroma_minus1; | |
1891 int log2_min_pcm_luma_coding_block_size_minus3; | |
1892 int log2_diff_max_min_pcm_luma_coding_block_size; | |
1893 int pcm_loop_filter_disabled_flag; | |
1894 int sps_extension_flag, sps_range_extension_flag, sps_extension_7bits; | |
1895 int sps_range_extension_flags; | |
1896 | |
1897 init_get_bits(gb, nal_buf, nal_len); | |
1898 skip_bits(gb, 16); /* nal header */ | |
1899 vps_id = get_bits(gb, 4); | |
1900 if (vps_id != 0) { | |
1901 fprintf(stderr, "VPS id 0 expected\n"); | |
1902 return -1; | |
1903 } | |
1904 max_sub_layers = get_bits(gb, 3); | |
1905 if (max_sub_layers != 0) { | |
1906 fprintf(stderr, "max_sub_layers == 0 expected\n"); | |
1907 return -1; | |
1908 } | |
1909 skip_bits(gb, 1); /* temporal_id_nesting_flag */ | |
1910 /* profile tier level */ | |
1911 skip_bits(gb, 2); /* profile_space */ | |
1912 skip_bits(gb, 1); /* tier_flag */ | |
1913 profile_idc = get_bits(gb, 5); | |
1914 for(i = 0; i < 32; i++) { | |
1915 skip_bits(gb, 1); /* profile_compatibility_flag */ | |
1916 } | |
1917 skip_bits(gb, 1); /* progressive_source_flag */ | |
1918 skip_bits(gb, 1); /* interlaced_source_flag */ | |
1919 skip_bits(gb, 1); /* non_packed_constraint_flag */ | |
1920 skip_bits(gb, 1); /* frame_only_constraint_flag */ | |
1921 skip_bits(gb, 44); /* XXX_reserved_zero_44 */ | |
1922 skip_bits(gb, 8); /* level_idc */ | |
1923 | |
1924 sps_id = get_ue_golomb(gb); | |
1925 if (sps_id != 0) { | |
1926 fprintf(stderr, "SPS id 0 expected (%d)\n", sps_id); | |
1927 return -1; | |
1928 } | |
1929 chroma_format_idc = get_ue_golomb(gb); | |
1930 if (chroma_format_idc == 3) { | |
1931 get_bits(gb, 1); /* separate_colour_plane_flag */ | |
1932 } | |
1933 width = get_ue_golomb(gb); | |
1934 height = get_ue_golomb(gb); | |
1935 /* pic conformance_flag */ | |
1936 if (get_bits(gb, 1)) { | |
1937 get_ue_golomb(gb); /* left_offset */ | |
1938 get_ue_golomb(gb); /* right_offset */ | |
1939 get_ue_golomb(gb); /* top_offset */ | |
1940 get_ue_golomb(gb); /* bottom_offset */ | |
1941 } | |
1942 bit_depth_luma = get_ue_golomb(gb) + 8; | |
1943 bit_depth_chroma = get_ue_golomb(gb) + 8; | |
1944 log2_max_poc_lsb = get_ue_golomb(gb) + 4; | |
1945 if (log2_max_poc_lsb != 8) { | |
1946 fprintf(stderr, "log2_max_poc_lsb must be 8 (%d)\n", log2_max_poc_lsb); | |
1947 return -1; | |
1948 } | |
1949 sublayer_ordering_info = get_bits(gb, 1); | |
1950 get_ue_golomb(gb); /* max_dec_pic_buffering */ | |
1951 get_ue_golomb(gb); /* num_reorder_pics */ | |
1952 get_ue_golomb(gb); /* max_latency_increase */ | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
1953 |
0 | 1954 log2_min_cb_size = get_ue_golomb(gb) + 3; |
1955 log2_diff_max_min_coding_block_size = get_ue_golomb(gb); | |
1956 log2_min_tb_size = get_ue_golomb(gb) + 2; | |
1957 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
|
1958 |
0 | 1959 max_transform_hierarchy_depth_inter = get_ue_golomb(gb); |
1960 max_transform_hierarchy_depth_intra = get_ue_golomb(gb); | |
1961 if (max_transform_hierarchy_depth_inter != max_transform_hierarchy_depth_intra) { | |
1962 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); | |
1963 return -1; | |
1964 } | |
1965 | |
1966 scaling_list_enable_flag = get_bits(gb, 1); | |
1967 if (scaling_list_enable_flag != 0) { | |
1968 fprintf(stderr, "scaling_list_enable_flag must be 0\n"); | |
1969 return -1; | |
1970 } | |
1971 amp_enabled_flag = get_bits(gb, 1); | |
1972 if (!amp_enabled_flag) { | |
1973 fprintf(stderr, "amp_enabled_flag must be set\n"); | |
1974 return -1; | |
1975 } | |
1976 sao_enabled = get_bits(gb, 1); | |
1977 pcm_enabled_flag = get_bits(gb, 1); | |
1978 if (pcm_enabled_flag) { | |
1979 pcm_sample_bit_depth_luma_minus1 = get_bits(gb, 4); | |
1980 pcm_sample_bit_depth_chroma_minus1 = get_bits(gb, 4); | |
1981 log2_min_pcm_luma_coding_block_size_minus3 = get_ue_golomb(gb); | |
1982 log2_diff_max_min_pcm_luma_coding_block_size = get_ue_golomb(gb); | |
1983 pcm_loop_filter_disabled_flag = get_bits(gb, 1); | |
1984 } | |
1985 nb_st_rps = get_ue_golomb(gb); | |
1986 if (nb_st_rps != 0) { | |
1987 fprintf(stderr, "nb_st_rps must be 0 (%d)\n", nb_st_rps); | |
1988 return -1; | |
1989 } | |
1990 long_term_ref_pics_present_flag = get_bits(gb, 1); | |
1991 if (long_term_ref_pics_present_flag) { | |
1992 fprintf(stderr, "nlong_term_ref_pics_present_flag must be 0 (%d)\n", nb_st_rps); | |
1993 return -1; | |
1994 } | |
1995 sps_temporal_mvp_enabled_flag = get_bits(gb, 1); | |
1996 if (!sps_temporal_mvp_enabled_flag) { | |
1997 fprintf(stderr, "sps_temporal_mvp_enabled_flag must be set\n"); | |
1998 return -1; | |
1999 } | |
2000 sps_strong_intra_smoothing_enable_flag = get_bits(gb, 1); | |
2001 vui_present = get_bits(gb, 1); | |
2002 if (vui_present) { | |
2003 int sar_present, sar_idx, overscan_info_present_flag; | |
2004 int video_signal_type_present_flag, chroma_loc_info_present_flag; | |
2005 int default_display_window_flag, vui_timing_info_present_flag; | |
2006 int vui_poc_proportional_to_timing_flag; | |
2007 int vui_hrd_parameters_present_flag, bitstream_restriction_flag; | |
2008 | |
2009 sar_present = get_bits(gb, 1); | |
2010 if (sar_present) { | |
2011 sar_idx = get_bits(gb, 8); | |
2012 if (sar_idx == 255) { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2013 skip_bits(gb, 16); /* sar_num */ |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2014 skip_bits(gb, 16); /* sar_den */ |
0 | 2015 } |
2016 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2017 |
0 | 2018 overscan_info_present_flag = get_bits(gb, 1); |
2019 if (overscan_info_present_flag) { | |
2020 skip_bits(gb, 1); /* overscan_appropriate_flag */ | |
2021 } | |
2022 | |
2023 video_signal_type_present_flag = get_bits(gb, 1); | |
2024 if (video_signal_type_present_flag) { | |
2025 fprintf(stderr, "video_signal_type_present_flag must be 0\n"); | |
2026 return -1; | |
2027 } | |
2028 chroma_loc_info_present_flag = get_bits(gb, 1); | |
2029 if (chroma_loc_info_present_flag) { | |
2030 get_ue_golomb(gb); | |
2031 get_ue_golomb(gb); | |
2032 } | |
2033 skip_bits(gb, 1); /* neutra_chroma_indication_flag */ | |
2034 skip_bits(gb, 1); | |
2035 skip_bits(gb, 1); | |
2036 default_display_window_flag = get_bits(gb, 1); | |
2037 if (default_display_window_flag) { | |
2038 fprintf(stderr, "default_display_window_flag must be 0\n"); | |
2039 return -1; | |
2040 } | |
2041 vui_timing_info_present_flag = get_bits(gb, 1); | |
2042 if (vui_timing_info_present_flag) { | |
2043 skip_bits(gb, 32); | |
2044 skip_bits(gb, 32); | |
2045 vui_poc_proportional_to_timing_flag = get_bits(gb, 1); | |
2046 if (vui_poc_proportional_to_timing_flag) { | |
2047 get_ue_golomb(gb); | |
2048 } | |
2049 vui_hrd_parameters_present_flag = get_bits(gb, 1); | |
2050 if (vui_hrd_parameters_present_flag) { | |
2051 fprintf(stderr, "vui_hrd_parameters_present_flag must be 0\n"); | |
2052 return -1; | |
2053 } | |
2054 } | |
2055 bitstream_restriction_flag = get_bits(gb, 1); | |
2056 if (bitstream_restriction_flag) { | |
2057 skip_bits(gb, 1); | |
2058 skip_bits(gb, 1); | |
2059 skip_bits(gb, 1); | |
2060 get_ue_golomb(gb); | |
2061 get_ue_golomb(gb); | |
2062 get_ue_golomb(gb); | |
2063 get_ue_golomb(gb); | |
2064 get_ue_golomb(gb); | |
2065 } | |
2066 } | |
2067 sps_extension_flag = get_bits(gb, 1); | |
2068 sps_range_extension_flag = 0; | |
2069 sps_range_extension_flags = 0; | |
2070 if (sps_extension_flag) { | |
2071 sps_range_extension_flag = get_bits(gb, 1); | |
2072 sps_extension_7bits = get_bits(gb, 7); | |
2073 if (sps_extension_7bits != 0) { | |
2074 fprintf(stderr, "sps_extension_7bits must be 0\n"); | |
2075 return -1; | |
2076 } | |
2077 if (sps_range_extension_flag) { | |
2078 sps_range_extension_flags = get_bits(gb, 9); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2079 if (sps_range_extension_flags & ((1 << (8 - 3)) | |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2080 (1 << (8 - 4)) | |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2081 (1 << (8 - 6)) | |
0 | 2082 (1 << (8 - 8)))) { |
2083 fprintf(stderr, "unsupported range extensions (0x%x)\n", | |
2084 sps_range_extension_flags); | |
2085 return -1; | |
2086 } | |
2087 } | |
2088 } | |
2089 | |
2090 /* build the modified SPS */ | |
2091 msps_buf = malloc(nal_len + 32); | |
2092 memset(msps_buf, 0, nal_len + 16); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2093 |
0 | 2094 init_put_bits(pb, msps_buf); |
2095 put_ue_golomb(pb, log2_min_cb_size - 3); | |
2096 put_ue_golomb(pb, log2_diff_max_min_coding_block_size); | |
2097 put_ue_golomb(pb, log2_min_tb_size - 2); | |
2098 put_ue_golomb(pb, log2_diff_max_min_transform_block_size); | |
2099 put_ue_golomb(pb, max_transform_hierarchy_depth_intra); | |
2100 put_bits(pb, 1, sao_enabled); | |
2101 put_bits(pb, 1, pcm_enabled_flag); | |
2102 if (pcm_enabled_flag) { | |
2103 put_bits(pb, 4, pcm_sample_bit_depth_luma_minus1); | |
2104 put_bits(pb, 4, pcm_sample_bit_depth_chroma_minus1); | |
2105 put_ue_golomb(pb, log2_min_pcm_luma_coding_block_size_minus3); | |
2106 put_ue_golomb(pb, log2_diff_max_min_pcm_luma_coding_block_size); | |
2107 put_bits(pb, 1, pcm_loop_filter_disabled_flag); | |
2108 } | |
2109 put_bits(pb, 1, sps_strong_intra_smoothing_enable_flag); | |
2110 put_bits(pb, 1, sps_extension_flag); | |
2111 if (sps_extension_flag) { | |
2112 put_bits(pb, 1, sps_range_extension_flag); | |
2113 put_bits(pb, 7, 0); | |
2114 if (sps_range_extension_flag) { | |
2115 put_bits(pb, 9, sps_range_extension_flags); | |
2116 } | |
2117 } | |
2118 msps_buf_len = (pb->idx + 7) >> 3; | |
2119 | |
2120 out_buf_len_max = 5 + msps_buf_len; | |
2121 out_buf = malloc(out_buf_len_max); | |
2122 | |
2123 // printf("msps_n_bits=%d\n", pb->idx); | |
2124 p = out_buf; | |
2125 put_ue(&p, msps_buf_len); /* header length */ | |
2126 | |
2127 memcpy(p, msps_buf, msps_buf_len); | |
2128 p += msps_buf_len; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2129 |
0 | 2130 out_buf_len = p - out_buf; |
2131 free(msps_buf); | |
2132 free(nal_buf); | |
2133 } | |
2134 *pout_buf = out_buf; | |
2135 *pout_buf_len = out_buf_len; | |
2136 return idx; | |
2137 } | |
2138 | |
2139 static int add_frame_duration_sei(DynBuf *out_buf, uint16_t frame_ticks) | |
2140 { | |
2141 uint8_t nal_buf[128], *q; | |
2142 int nut, nal_len; | |
2143 | |
2144 q = nal_buf; | |
2145 *q++ = 0x00; | |
2146 *q++ = 0x00; | |
2147 *q++ = 0x01; | |
2148 nut = 39; /* prefix SEI NUT */ | |
2149 *q++ = (nut << 1); | |
2150 *q++ = 1; | |
2151 *q++ = 0xff; /* payload_type = 257 */ | |
2152 *q++ = 0x02; | |
2153 *q++ = 2; /* payload_size = 2 */ | |
2154 *q++ = frame_ticks >> 8; | |
2155 *q++ = frame_ticks; | |
2156 *q++ = 0x80; /* extra '1' bit and align to byte */ | |
2157 /* Note: the 0x00 0x00 b pattern with b <= 3 cannot happen, so no | |
2158 need to escape */ | |
2159 nal_len = q - nal_buf; | |
2160 if (dyn_buf_resize(out_buf, out_buf->len + nal_len) < 0) | |
2161 return -1; | |
2162 memcpy(out_buf->buf + out_buf->len, nal_buf, nal_len); | |
2163 out_buf->len += nal_len; | |
2164 return 0; | |
2165 } | |
2166 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2167 static int build_modified_hevc(uint8_t **pout_buf, |
0 | 2168 const uint8_t *cbuf, int cbuf_len, |
2169 const uint8_t *abuf, int abuf_len, | |
2170 const uint16_t *frame_duration_tab, | |
2171 int frame_count) | |
2172 { | |
2173 DynBuf out_buf_s, *out_buf = &out_buf_s; | |
2174 uint8_t *msps; | |
2175 const uint8_t *nal_buf; | |
2176 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
|
2177 |
0 | 2178 dyn_buf_init(out_buf); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2179 |
0 | 2180 /* add alpha MSPS */ |
2181 aidx = 0; /* avoids warning */ | |
2182 if (abuf) { | |
2183 aidx = build_modified_sps(&msps, &msps_len, abuf, abuf_len); | |
2184 if (aidx < 0) | |
2185 goto fail; | |
2186 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0) | |
2187 goto fail; | |
2188 memcpy(out_buf->buf + out_buf->len, msps, msps_len); | |
2189 out_buf->len += msps_len; | |
2190 free(msps); | |
2191 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2192 |
0 | 2193 /* add color MSPS */ |
2194 cidx = build_modified_sps(&msps, &msps_len, cbuf, cbuf_len); | |
2195 if (cidx < 0) | |
2196 goto fail; | |
2197 if (dyn_buf_resize(out_buf, out_buf->len + msps_len) < 0) | |
2198 goto fail; | |
2199 memcpy(out_buf->buf + out_buf->len, msps, msps_len); | |
2200 out_buf->len += msps_len; | |
2201 free(msps); | |
2202 | |
2203 /* add the remaining NALs, alternating between alpha (if present) | |
2204 and color. */ | |
2205 is_alpha = (abuf != NULL); | |
2206 first_nal = 1; | |
2207 frame_num = 0; | |
2208 for(;;) { | |
2209 if (!is_alpha) { | |
2210 if (cidx >= cbuf_len) { | |
2211 if (abuf) { | |
2212 fprintf(stderr, "Incorrect number of alpha NALs\n"); | |
2213 goto fail; | |
2214 } | |
2215 break; | |
2216 } | |
2217 nal_buf = cbuf + cidx; | |
2218 nal_len = find_nal_end(nal_buf, cbuf_len - cidx); | |
2219 // printf("cidx=%d/%d nal_len=%d\n", cidx, cbuf_len, nal_len); | |
2220 if (nal_len < 0) | |
2221 goto fail; | |
2222 cidx += nal_len; | |
2223 } else { | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2224 if (aidx >= abuf_len) |
0 | 2225 break; |
2226 nal_buf = abuf + aidx; | |
2227 nal_len = find_nal_end(nal_buf, abuf_len - aidx); | |
2228 // printf("aidx=%d/%d nal_len=%d\n", aidx, abuf_len, nal_len); | |
2229 if (nal_len < 0) | |
2230 goto fail; | |
2231 aidx += nal_len; | |
2232 } | |
2233 start = 3 + (nal_buf[2] == 0); | |
2234 if (!is_alpha) { | |
2235 int nut; | |
2236 /* add SEI NAL for the frame duration (animation case) */ | |
2237 nut = (nal_buf[start] >> 1) & 0x3f; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2238 if ((nut <= 9 || (nut >= 16 && nut <= 21)) && |
0 | 2239 start + 2 < nal_len && (nal_buf[start + 2] & 0x80)) { |
2240 int frame_ticks; | |
2241 assert(frame_num < frame_count); | |
2242 frame_ticks = frame_duration_tab[frame_num]; | |
2243 if (frame_ticks > 1) { | |
2244 add_frame_duration_sei(out_buf, frame_ticks); | |
2245 } | |
2246 frame_num++; | |
2247 } | |
2248 } | |
2249 if (first_nal) { | |
2250 /* skip first start code */ | |
2251 l = start; | |
2252 } else { | |
2253 l = 0; | |
2254 } | |
2255 if (dyn_buf_resize(out_buf, out_buf->len + nal_len - l) < 0) | |
2256 goto fail; | |
2257 // printf("add nal len=%d\n", nal_len - l); | |
2258 memcpy(out_buf->buf + out_buf->len, nal_buf + l, nal_len - l); | |
2259 if (is_alpha) { | |
2260 /* set nul_layer_id of alpha to '1' */ | |
2261 out_buf->buf[out_buf->len + (start - l) + 1] |= 1 << 3; | |
2262 } | |
2263 out_buf->len += nal_len - l; | |
2264 | |
2265 if (abuf) { | |
2266 is_alpha ^= 1; | |
2267 } | |
2268 first_nal = 0; | |
2269 } | |
2270 *pout_buf = out_buf->buf; | |
2271 return out_buf->len; | |
2272 fail: | |
2273 free(out_buf->buf); | |
2274 return -1; | |
2275 } | |
2276 | |
2277 typedef enum { | |
2278 #if defined(USE_X265) | |
2279 HEVC_ENCODER_X265, | |
2280 #endif | |
2281 #if defined(USE_JCTVC) | |
2282 HEVC_ENCODER_JCTVC, | |
2283 #endif | |
2284 | |
2285 HEVC_ENCODER_COUNT, | |
2286 } HEVCEncoderEnum; | |
2287 | |
2288 static char *hevc_encoder_name[HEVC_ENCODER_COUNT] = { | |
2289 #if defined(USE_X265) | |
2290 "x265", | |
2291 #endif | |
2292 #if defined(USE_JCTVC) | |
2293 "jctvc", | |
2294 #endif | |
2295 }; | |
2296 | |
2297 static HEVCEncoder *hevc_encoder_tab[HEVC_ENCODER_COUNT] = { | |
2298 #if defined(USE_X265) | |
2299 &x265_hevc_encoder, | |
2300 #endif | |
2301 #if defined(USE_JCTVC) | |
2302 &jctvc_encoder, | |
2303 #endif | |
2304 }; | |
2305 | |
2306 #define IMAGE_HEADER_MAGIC 0x425047fb | |
2307 | |
2308 #define DEFAULT_OUTFILENAME "out.bpg" | |
2309 #define DEFAULT_QP 29 | |
2310 #define DEFAULT_BIT_DEPTH 8 | |
2311 | |
2312 #ifdef RExt__HIGH_BIT_DEPTH_SUPPORT | |
2313 #define BIT_DEPTH_MAX 14 | |
2314 #else | |
2315 #define BIT_DEPTH_MAX 12 | |
2316 #endif | |
2317 #define DEFAULT_COMPRESS_LEVEL 8 | |
2318 | |
2319 | |
2320 typedef struct BPGEncoderContext BPGEncoderContext; | |
2321 | |
2322 typedef struct BPGEncoderParameters { | |
2323 int qp; /* 0 ... 51 */ | |
2324 int alpha_qp; /* -1 ... 51. -1 means same as qp */ | |
2325 int lossless; /* true if lossless compression (qp and alpha_qp are | |
2326 ignored) */ | |
2327 BPGImageFormatEnum preferred_chroma_format; | |
2328 int sei_decoded_picture_hash; /* 0, 1 */ | |
2329 int compress_level; /* 1 ... 9 */ | |
2330 int verbose; | |
2331 HEVCEncoderEnum encoder_type; | |
2332 int animated; /* 0 ... 1: if true, encode as animated image */ | |
2333 uint16_t loop_count; /* animations: number of loops. 0=infinite */ | |
2334 /* animations: the frame delay is a multiple of | |
2335 frame_delay_num/frame_delay_den seconds */ | |
2336 uint16_t frame_delay_num; | |
2337 uint16_t frame_delay_den; | |
2338 } BPGEncoderParameters; | |
2339 | |
2340 typedef int BPGEncoderWriteFunc(void *opaque, const uint8_t *buf, int buf_len); | |
2341 | |
2342 struct BPGEncoderContext { | |
2343 BPGEncoderParameters params; | |
2344 BPGMetaData *first_md; | |
2345 HEVCEncoder *encoder; | |
2346 int frame_count; | |
2347 HEVCEncoderContext *enc_ctx; | |
2348 HEVCEncoderContext *alpha_enc_ctx; | |
2349 int frame_ticks; | |
2350 uint16_t *frame_duration_tab; | |
2351 int frame_duration_tab_size; | |
2352 }; | |
2353 | |
2354 void *mallocz(size_t size) | |
2355 { | |
2356 void *ptr; | |
2357 ptr = malloc(size); | |
2358 if (!ptr) | |
2359 return NULL; | |
2360 memset(ptr, 0, size); | |
2361 return ptr; | |
2362 } | |
2363 | |
2364 BPGEncoderParameters *bpg_encoder_param_alloc(void) | |
2365 { | |
2366 BPGEncoderParameters *p; | |
2367 p = mallocz(sizeof(BPGEncoderParameters)); | |
2368 if (!p) | |
2369 return NULL; | |
2370 p->qp = DEFAULT_QP; | |
2371 p->alpha_qp = -1; | |
2372 p->preferred_chroma_format = BPG_FORMAT_420; | |
2373 p->compress_level = DEFAULT_COMPRESS_LEVEL; | |
2374 p->frame_delay_num = 1; | |
2375 p->frame_delay_den = 25; | |
2376 p->loop_count = 0; | |
2377 return p; | |
2378 } | |
2379 | |
2380 void bpg_encoder_param_free(BPGEncoderParameters *p) | |
2381 { | |
2382 free(p); | |
2383 } | |
2384 | |
2385 BPGEncoderContext *bpg_encoder_open(BPGEncoderParameters *p) | |
2386 { | |
2387 BPGEncoderContext *s; | |
2388 | |
2389 s = mallocz(sizeof(BPGEncoderContext)); | |
2390 if (!s) | |
2391 return NULL; | |
2392 s->params = *p; | |
2393 s->encoder = hevc_encoder_tab[s->params.encoder_type]; | |
2394 s->frame_ticks = 1; | |
2395 return s; | |
2396 } | |
2397 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2398 void bpg_encoder_set_extension_data(BPGEncoderContext *s, |
0 | 2399 BPGMetaData *md) |
2400 { | |
2401 s->first_md = md; | |
2402 } | |
2403 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2404 static int bpg_encoder_encode_trailer(BPGEncoderContext *s, |
0 | 2405 BPGEncoderWriteFunc *write_func, |
2406 void *opaque) | |
2407 { | |
2408 uint8_t *out_buf, *alpha_buf, *hevc_buf; | |
2409 int out_buf_len, alpha_buf_len, hevc_buf_len; | |
2410 | |
2411 out_buf_len = s->encoder->close(s->enc_ctx, &out_buf); | |
2412 if (out_buf_len < 0) { | |
2413 fprintf(stderr, "Error while encoding picture\n"); | |
2414 exit(1); | |
2415 } | |
2416 s->enc_ctx = NULL; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2417 |
0 | 2418 alpha_buf = NULL; |
2419 alpha_buf_len = 0; | |
2420 if (s->alpha_enc_ctx) { | |
2421 alpha_buf_len = s->encoder->close(s->alpha_enc_ctx, &alpha_buf); | |
2422 if (alpha_buf_len < 0) { | |
2423 fprintf(stderr, "Error while encoding picture (alpha plane)\n"); | |
2424 exit(1); | |
2425 } | |
2426 s->alpha_enc_ctx = NULL; | |
2427 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2428 |
0 | 2429 hevc_buf = NULL; |
2430 hevc_buf_len = build_modified_hevc(&hevc_buf, out_buf, out_buf_len, | |
2431 alpha_buf, alpha_buf_len, | |
2432 s->frame_duration_tab, s->frame_count); | |
2433 if (hevc_buf_len < 0) { | |
2434 fprintf(stderr, "Error while creating HEVC data\n"); | |
2435 exit(1); | |
2436 } | |
2437 free(out_buf); | |
2438 free(alpha_buf); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2439 |
0 | 2440 if (write_func(opaque, hevc_buf, hevc_buf_len) != hevc_buf_len) { |
2441 fprintf(stderr, "Error while writing HEVC data\n"); | |
2442 exit(1); | |
2443 } | |
2444 free(hevc_buf); | |
2445 return 0; | |
2446 } | |
2447 | |
2448 int bpg_encoder_set_frame_duration(BPGEncoderContext *s, int frame_ticks) | |
2449 { | |
2450 if (frame_ticks >= 1 && frame_ticks <= 65535) { | |
2451 s->frame_ticks = frame_ticks; | |
2452 return 0; | |
2453 } else { | |
2454 return -1; | |
2455 } | |
2456 } | |
2457 | |
2458 /* Warning: currently 'img' is modified. When encoding animations, img | |
2459 = NULL indicates the end of the stream. */ | |
2460 int bpg_encoder_encode(BPGEncoderContext *s, Image *img, | |
2461 BPGEncoderWriteFunc *write_func, | |
2462 void *opaque) | |
2463 { | |
2464 const BPGEncoderParameters *p = &s->params; | |
2465 Image *img_alpha; | |
2466 HEVCEncodeParams ep_s, *ep = &ep_s; | |
2467 uint8_t *extension_buf; | |
2468 int extension_buf_len; | |
2469 int cb_size, width, height; | |
2470 | |
2471 if (p->animated && !img) { | |
2472 return bpg_encoder_encode_trailer(s, write_func, opaque); | |
2473 } | |
2474 | |
2475 /* extract the alpha plane */ | |
2476 if (img->has_alpha) { | |
2477 int c_idx; | |
2478 | |
2479 img_alpha = malloc(sizeof(Image)); | |
2480 memset(img_alpha, 0, sizeof(*img_alpha)); | |
2481 if (img->format == BPG_FORMAT_GRAY) | |
2482 c_idx = 1; | |
2483 else | |
2484 c_idx = 3; | |
2485 | |
2486 img_alpha->w = img->w; | |
2487 img_alpha->h = img->h; | |
2488 img_alpha->format = BPG_FORMAT_GRAY; | |
2489 img_alpha->has_alpha = 0; | |
2490 img_alpha->color_space = BPG_CS_YCbCr; | |
2491 img_alpha->bit_depth = img->bit_depth; | |
2492 img_alpha->pixel_shift = img->pixel_shift; | |
2493 img_alpha->data[0] = img->data[c_idx]; | |
2494 img_alpha->linesize[0] = img->linesize[c_idx]; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2495 |
0 | 2496 img->data[c_idx] = NULL; |
2497 img->has_alpha = 0; | |
2498 } else { | |
2499 img_alpha = NULL; | |
2500 } | |
2501 | |
2502 if (img->format == BPG_FORMAT_444 && img->color_space != BPG_CS_RGB) { | |
2503 if (p->preferred_chroma_format == BPG_FORMAT_420 || | |
2504 p->preferred_chroma_format == BPG_FORMAT_420_VIDEO) { | |
2505 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_420); | |
2506 if (image_ycc444_to_ycc420(img, c_h_phase) != 0) | |
2507 goto error_convert; | |
2508 } else if (p->preferred_chroma_format == BPG_FORMAT_422 || | |
2509 p->preferred_chroma_format == BPG_FORMAT_422_VIDEO) { | |
2510 int c_h_phase = (p->preferred_chroma_format == BPG_FORMAT_422); | |
2511 if (image_ycc444_to_ycc422(img, c_h_phase) != 0) { | |
2512 error_convert: | |
2513 fprintf(stderr, "Cannot convert image\n"); | |
2514 exit(1); | |
2515 } | |
2516 } | |
2517 } | |
2518 | |
2519 cb_size = 8; /* XXX: should make it configurable. We assume the | |
2520 HEVC encoder uses the same value */ | |
2521 width = img->w; | |
2522 height = img->h; | |
2523 image_pad(img, cb_size); | |
2524 if (img_alpha) | |
2525 image_pad(img_alpha, cb_size); | |
2526 | |
2527 /* convert to the allocated pixel width to 8 bit if needed by the | |
2528 HEVC encoder */ | |
2529 if (img->bit_depth == 8) { | |
2530 image_convert16to8(img); | |
2531 if (img_alpha) | |
2532 image_convert16to8(img_alpha); | |
2533 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2534 |
0 | 2535 if (s->frame_count == 0) { |
2536 memset(ep, 0, sizeof(*ep)); | |
2537 ep->qp = p->qp; | |
2538 ep->width = img->w; | |
2539 ep->height = img->h; | |
2540 ep->chroma_format = img->format; | |
2541 ep->bit_depth = img->bit_depth; | |
2542 ep->intra_only = !p->animated; | |
2543 ep->lossless = p->lossless; | |
2544 ep->sei_decoded_picture_hash = p->sei_decoded_picture_hash; | |
2545 ep->compress_level = p->compress_level; | |
2546 ep->verbose = p->verbose; | |
2547 | |
2548 s->enc_ctx = s->encoder->open(ep); | |
2549 if (!s->enc_ctx) { | |
2550 fprintf(stderr, "Error while opening encoder\n"); | |
2551 exit(1); | |
2552 } | |
2553 | |
2554 if (img_alpha) { | |
2555 if (p->alpha_qp < 0) | |
2556 ep->qp = p->qp; | |
2557 else | |
2558 ep->qp = p->alpha_qp; | |
2559 ep->chroma_format = 0; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2560 |
0 | 2561 s->alpha_enc_ctx = s->encoder->open(ep); |
2562 if (!s->alpha_enc_ctx) { | |
2563 fprintf(stderr, "Error while opening alpha encoder\n"); | |
2564 exit(1); | |
2565 } | |
2566 } | |
2567 | |
2568 /* prepare the extension data */ | |
2569 if (p->animated) { | |
2570 BPGMetaData *md; | |
2571 uint8_t buf[15], *q; | |
2572 | |
2573 md = bpg_md_alloc(BPG_EXTENSION_TAG_ANIM_CONTROL); | |
2574 q = buf; | |
2575 put_ue(&q, p->loop_count); | |
2576 put_ue(&q, p->frame_delay_num); | |
2577 put_ue(&q, p->frame_delay_den); | |
2578 md->buf_len = q - buf; | |
2579 md->buf = malloc(md->buf_len); | |
2580 memcpy(md->buf, buf, md->buf_len); | |
2581 md->next = s->first_md; | |
2582 s->first_md = md; | |
2583 } | |
2584 | |
2585 extension_buf = NULL; | |
2586 extension_buf_len = 0; | |
2587 if (s->first_md) { | |
2588 BPGMetaData *md1; | |
2589 int max_len; | |
2590 uint8_t *q; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2591 |
0 | 2592 max_len = 0; |
2593 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) { | |
2594 max_len += md1->buf_len + 5 * 2; | |
2595 } | |
2596 extension_buf = malloc(max_len); | |
2597 q = extension_buf; | |
2598 for(md1 = s->first_md; md1 != NULL; md1 = md1->next) { | |
2599 put_ue(&q, md1->tag); | |
2600 put_ue(&q, md1->buf_len); | |
2601 memcpy(q, md1->buf, md1->buf_len); | |
2602 q += md1->buf_len; | |
2603 } | |
2604 extension_buf_len = q - extension_buf; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2605 |
0 | 2606 bpg_md_free(s->first_md); |
2607 s->first_md = NULL; | |
2608 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2609 |
0 | 2610 { |
2611 uint8_t img_header[128], *q; | |
2612 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
|
2613 |
0 | 2614 has_alpha = (img_alpha != NULL); |
2615 has_extension = (extension_buf_len > 0); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2616 |
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2617 |
0 | 2618 if (has_alpha) { |
2619 if (img->has_w_plane) { | |
2620 alpha1_flag = 0; | |
2621 alpha2_flag = 1; | |
2622 } else { | |
2623 alpha1_flag = 1; | |
2624 alpha2_flag = img->premultiplied_alpha; | |
2625 } | |
2626 } else { | |
2627 alpha1_flag = 0; | |
2628 alpha2_flag = 0; | |
2629 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2630 |
0 | 2631 q = img_header; |
2632 *q++ = (IMAGE_HEADER_MAGIC >> 24) & 0xff; | |
2633 *q++ = (IMAGE_HEADER_MAGIC >> 16) & 0xff; | |
2634 *q++ = (IMAGE_HEADER_MAGIC >> 8) & 0xff; | |
2635 *q++ = (IMAGE_HEADER_MAGIC >> 0) & 0xff; | |
2636 | |
2637 if (img->c_h_phase == 0 && img->format == BPG_FORMAT_420) | |
2638 format = BPG_FORMAT_420_VIDEO; | |
2639 else if (img->c_h_phase == 0 && img->format == BPG_FORMAT_422) | |
2640 format = BPG_FORMAT_422_VIDEO; | |
2641 else | |
2642 format = img->format; | |
2643 v = (format << 5) | (alpha1_flag << 4) | (img->bit_depth - 8); | |
2644 *q++ = v; | |
2645 v = (img->color_space << 4) | (has_extension << 3) | | |
2646 (alpha2_flag << 2) | (img->limited_range << 1) | | |
2647 p->animated; | |
2648 *q++ = v; | |
2649 put_ue(&q, width); | |
2650 put_ue(&q, height); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2651 |
0 | 2652 put_ue(&q, 0); /* zero length means up to the end of the file */ |
2653 if (has_extension) { | |
2654 put_ue(&q, extension_buf_len); /* extension data length */ | |
2655 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2656 |
0 | 2657 write_func(opaque, img_header, q - img_header); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2658 |
0 | 2659 if (has_extension) { |
2660 if (write_func(opaque, extension_buf, extension_buf_len) != extension_buf_len) { | |
2661 fprintf(stderr, "Error while writing extension data\n"); | |
2662 exit(1); | |
2663 } | |
2664 free(extension_buf); | |
2665 } | |
2666 } | |
2667 } | |
2668 | |
2669 /* store the frame duration */ | |
2670 if ((s->frame_count + 1) > s->frame_duration_tab_size) { | |
2671 s->frame_duration_tab_size = (s->frame_duration_tab_size * 3) / 2; | |
2672 if (s->frame_duration_tab_size < (s->frame_count + 1)) | |
2673 s->frame_duration_tab_size = (s->frame_count + 1); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2674 s->frame_duration_tab = realloc(s->frame_duration_tab, |
0 | 2675 sizeof(s->frame_duration_tab) * s->frame_duration_tab_size); |
2676 } | |
2677 s->frame_duration_tab[s->frame_count] = s->frame_ticks; | |
2678 | |
2679 s->encoder->encode(s->enc_ctx, img); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2680 |
0 | 2681 if (img_alpha) { |
2682 s->encoder->encode(s->alpha_enc_ctx, img_alpha); | |
2683 image_free(img_alpha); | |
2684 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2685 |
0 | 2686 s->frame_count++; |
2687 | |
2688 if (!p->animated) | |
2689 bpg_encoder_encode_trailer(s, write_func, opaque); | |
2690 | |
2691 return 0; | |
2692 } | |
2693 | |
2694 void bpg_encoder_close(BPGEncoderContext *s) | |
2695 { | |
2696 free(s->frame_duration_tab); | |
2697 bpg_md_free(s->first_md); | |
2698 free(s); | |
2699 } | |
2700 | |
2701 static int my_write_func(void *opaque, const uint8_t *buf, int buf_len) | |
2702 { | |
2703 FILE *f = opaque; | |
2704 return fwrite(buf, 1, buf_len, f); | |
2705 } | |
2706 | |
2707 static int get_filename_num(char *buf, int buf_size, const char *str, int n) | |
2708 { | |
2709 const char *p, *r; | |
2710 char *q; | |
2711 int l, c; | |
2712 | |
2713 q = buf; | |
2714 p = str; | |
2715 for(;;) { | |
2716 c = *p++; | |
2717 if (c == '\0') | |
2718 break; | |
2719 if (c == '%') { | |
2720 r = p - 1; | |
2721 l = 0; | |
2722 for(;;) { | |
2723 c = *p; | |
2724 if (c < '0' || c > '9') | |
2725 break; | |
2726 l = l * 10 + (c - '0'); | |
2727 p++; | |
2728 } | |
2729 c = *p++; | |
2730 if (c == '%') { | |
2731 goto add_char; | |
2732 } else if (c != 'd') { | |
2733 return -1; | |
2734 } | |
2735 snprintf(q, buf + buf_size - q, "%0*u", l, n); | |
2736 q += strlen(q); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2737 |
0 | 2738 } else { |
2739 add_char: | |
2740 if ((q - buf) < buf_size - 1) | |
2741 *q++ = c; | |
2742 } | |
2743 } | |
2744 *q = '\0'; | |
2745 return 0; | |
2746 } | |
2747 | |
2748 void help(int is_full) | |
2749 { | |
2750 char hevc_encoders[128]; | |
2751 int i; | |
2752 | |
2753 hevc_encoders[0] = '\0'; | |
2754 for(i = 0; i < HEVC_ENCODER_COUNT; i++) { | |
2755 if (i != 0) | |
2756 strcat(hevc_encoders, " "); | |
2757 strcat(hevc_encoders, hevc_encoder_name[i]); | |
2758 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2759 |
0 | 2760 printf("BPG Image Encoder version " CONFIG_BPG_VERSION "\n" |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2761 "usage: bpgenc [options] infile.[jpg|png|tiff]\n" |
0 | 2762 "\n" |
2763 "Main options:\n" | |
2764 "-h show the full help (including the advanced options)\n" | |
2765 "-o outfile set output filename (default = %s)\n" | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2766 "-q qp set quantizer parameter (smaller gives better quality,\n" |
0 | 2767 " range: 0-51, default = %d)\n" |
2768 "-f cfmt set the preferred chroma format (420, 422, 444,\n" | |
2769 " default=420)\n" | |
2770 "-c color_space set the preferred color space (ycbcr, rgb, ycgco,\n" | |
2771 " ycbcr_bt709, ycbcr_bt2020, default=ycbcr)\n" | |
2772 "-b bit_depth set the bit depth (8 to %d, default = %d)\n" | |
2773 "-lossless enable lossless mode\n" | |
2774 "-e encoder select the HEVC encoder (%s, default = %s)\n" | |
2775 "-m level select the compression level (1=fast, 9=slow, default = %d)\n" | |
2776 "\n" | |
2777 "Animation options:\n" | |
2778 "-a generate animations from a sequence of images. Use %%d or\n" | |
2779 " %%Nd (N = number of digits) in the filename to specify the\n" | |
2780 " image index, starting from 0 or 1.\n" | |
2781 "-fps N set the frame rate (default = 25)\n" | |
2782 "-loop N set the number of times the animation is played. 0 means\n" | |
2783 " infinite (default = 0)\n" | |
2784 "-delayfile file text file containing one number per image giving the\n" | |
2785 " display delay per image in centiseconds.\n" | |
2786 , DEFAULT_OUTFILENAME, DEFAULT_QP, BIT_DEPTH_MAX, DEFAULT_BIT_DEPTH, | |
2787 hevc_encoders, hevc_encoder_name[0], DEFAULT_COMPRESS_LEVEL); | |
2788 | |
2789 if (is_full) { | |
2790 printf("\nAdvanced options:\n" | |
2791 "-alphaq set quantizer parameter for the alpha channel (default = same as -q value)\n" | |
2792 "-premul store the color with premultiplied alpha\n" | |
2793 "-limitedrange encode the color data with the limited range of video\n" | |
2794 "-hash include MD5 hash in HEVC bitstream\n" | |
2795 "-keepmetadata keep the metadata (from JPEG: EXIF, ICC profile, XMP, from PNG: ICC profile)\n" | |
2796 "-v show debug messages\n" | |
2797 ); | |
2798 } | |
2799 | |
2800 exit(1); | |
2801 } | |
2802 | |
2803 struct option long_opts[] = { | |
2804 { "hash", no_argument }, | |
2805 { "keepmetadata", no_argument }, | |
2806 { "alphaq", required_argument }, | |
2807 { "lossless", no_argument }, | |
2808 { "limitedrange", no_argument }, | |
2809 { "premul", no_argument }, | |
2810 { "loop", required_argument }, | |
2811 { "fps", required_argument }, | |
2812 { "delayfile", required_argument }, | |
2813 { NULL }, | |
2814 }; | |
2815 | |
2816 int main(int argc, char **argv) | |
2817 { | |
2818 const char *infilename, *outfilename, *frame_delay_file; | |
2819 Image *img; | |
2820 FILE *f; | |
2821 int c, option_index; | |
2822 int keep_metadata; | |
2823 int bit_depth, i, limited_range, premultiplied_alpha; | |
2824 BPGColorSpaceEnum color_space; | |
2825 BPGMetaData *md; | |
2826 BPGEncoderContext *enc_ctx; | |
2827 BPGEncoderParameters *p; | |
2828 | |
2829 p = bpg_encoder_param_alloc(); | |
2830 | |
2831 outfilename = DEFAULT_OUTFILENAME; | |
2832 color_space = BPG_CS_YCbCr; | |
2833 keep_metadata = 0; | |
2834 bit_depth = DEFAULT_BIT_DEPTH; | |
2835 limited_range = 0; | |
2836 premultiplied_alpha = 0; | |
2837 frame_delay_file = NULL; | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2838 |
0 | 2839 for(;;) { |
2840 c = getopt_long_only(argc, argv, "q:o:hf:c:vm:b:e:a", long_opts, &option_index); | |
2841 if (c == -1) | |
2842 break; | |
2843 switch(c) { | |
2844 case 0: | |
2845 switch(option_index) { | |
2846 case 0: | |
2847 p->sei_decoded_picture_hash = 1; | |
2848 break; | |
2849 case 1: | |
2850 keep_metadata = 1; | |
2851 break; | |
2852 case 2: | |
2853 p->alpha_qp = atoi(optarg); | |
2854 if (p->alpha_qp < 0 || p->alpha_qp > 51) { | |
2855 fprintf(stderr, "alpha_qp must be between 0 and 51\n"); | |
2856 exit(1); | |
2857 } | |
2858 break; | |
2859 case 3: | |
2860 p->lossless = 1; | |
2861 color_space = BPG_CS_RGB; | |
2862 p->preferred_chroma_format = BPG_FORMAT_444; | |
2863 bit_depth = 8; | |
2864 limited_range = 0; | |
2865 break; | |
2866 case 4: | |
2867 limited_range = 1; | |
2868 break; | |
2869 case 5: | |
2870 premultiplied_alpha = 1; | |
2871 break; | |
2872 case 6: | |
2873 p->loop_count = strtoul(optarg, NULL, 0); | |
2874 break; | |
2875 case 7: | |
2876 p->frame_delay_num = 1; | |
2877 p->frame_delay_den = strtoul(optarg, NULL, 0); | |
2878 if (p->frame_delay_den == 0) { | |
2879 fprintf(stderr, "invalid frame rate\n"); | |
2880 exit(1); | |
2881 } | |
2882 break; | |
2883 case 8: | |
2884 frame_delay_file = optarg; | |
2885 break; | |
2886 default: | |
2887 goto show_help; | |
2888 } | |
2889 break; | |
2890 case 'h': | |
2891 show_help: | |
2892 help(1); | |
2893 break; | |
2894 case 'q': | |
2895 p->qp = atoi(optarg); | |
2896 if (p->qp < 0 || p->qp > 51) { | |
2897 fprintf(stderr, "qp must be between 0 and 51\n"); | |
2898 exit(1); | |
2899 } | |
2900 break; | |
2901 case 'o': | |
2902 outfilename = optarg; | |
2903 break; | |
2904 case 'f': | |
2905 if (!strcmp(optarg, "420")) { | |
2906 p->preferred_chroma_format = BPG_FORMAT_420; | |
2907 } else if (!strcmp(optarg, "422")) { | |
2908 p->preferred_chroma_format = BPG_FORMAT_422; | |
2909 } else if (!strcmp(optarg, "444")) { | |
2910 p->preferred_chroma_format = BPG_FORMAT_444; | |
2911 } else if (!strcmp(optarg, "422_video")) { | |
2912 p->preferred_chroma_format = BPG_FORMAT_422_VIDEO; | |
2913 } else if (!strcmp(optarg, "420_video")) { | |
2914 p->preferred_chroma_format = BPG_FORMAT_420_VIDEO; | |
2915 } else { | |
2916 fprintf(stderr, "Invalid chroma format\n"); | |
2917 exit(1); | |
2918 } | |
2919 break; | |
2920 case 'c': | |
2921 if (!strcmp(optarg, "ycbcr")) { | |
2922 color_space = BPG_CS_YCbCr; | |
2923 } else if (!strcmp(optarg, "rgb")) { | |
2924 color_space = BPG_CS_RGB; | |
2925 p->preferred_chroma_format = BPG_FORMAT_444; | |
2926 } else if (!strcmp(optarg, "ycgco")) { | |
2927 color_space = BPG_CS_YCgCo; | |
2928 } else if (!strcmp(optarg, "ycbcr_bt709")) { | |
2929 color_space = BPG_CS_YCbCr_BT709; | |
2930 } else if (!strcmp(optarg, "ycbcr_bt2020")) { | |
2931 color_space = BPG_CS_YCbCr_BT2020; | |
2932 } else { | |
2933 fprintf(stderr, "Invalid color space format\n"); | |
2934 exit(1); | |
2935 } | |
2936 break; | |
2937 case 'm': | |
2938 p->compress_level = atoi(optarg); | |
2939 if (p->compress_level < 1) | |
2940 p->compress_level = 1; | |
2941 else if (p->compress_level > 9) | |
2942 p->compress_level = 9; | |
2943 break; | |
2944 case 'b': | |
2945 bit_depth = atoi(optarg); | |
2946 if (bit_depth < 8 || bit_depth > BIT_DEPTH_MAX) { | |
2947 fprintf(stderr, "Invalid bit depth (range: 8 to %d)\n", | |
2948 BIT_DEPTH_MAX); | |
2949 exit(1); | |
2950 } | |
2951 break; | |
2952 case 'v': | |
2953 p->verbose++; | |
2954 break; | |
2955 case 'e': | |
2956 for(i = 0; i < HEVC_ENCODER_COUNT; i++) { | |
2957 if (!strcmp(optarg, hevc_encoder_name[i])) | |
2958 break; | |
2959 } | |
2960 if (i == HEVC_ENCODER_COUNT) { | |
2961 fprintf(stderr, "Unsupported encoder. Available ones are:"); | |
2962 for(i = 0; i < HEVC_ENCODER_COUNT; i++) { | |
2963 fprintf(stderr, " %s", hevc_encoder_name[i]); | |
2964 } | |
2965 fprintf(stderr, "\n"); | |
2966 exit(1); | |
2967 } | |
2968 p->encoder_type = i; | |
2969 break; | |
2970 case 'a': | |
2971 p->animated = 1; | |
2972 break; | |
2973 default: | |
2974 exit(1); | |
2975 } | |
2976 } | |
2977 | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
2978 if (optind >= argc) |
0 | 2979 help(0); |
2980 infilename = argv[optind]; | |
2981 | |
2982 f = fopen(outfilename, "wb"); | |
2983 if (!f) { | |
2984 perror(outfilename); | |
2985 exit(1); | |
2986 } | |
2987 | |
2988 enc_ctx = bpg_encoder_open(p); | |
2989 if (!enc_ctx) { | |
2990 fprintf(stderr, "Could not open BPG encoder\n"); | |
2991 exit(1); | |
2992 } | |
2993 | |
2994 if (p->animated) { | |
2995 int frame_num, first_frame, frame_ticks; | |
2996 char filename[1024]; | |
2997 FILE *f1; | |
2998 | |
2999 if (frame_delay_file) { | |
3000 f1 = fopen(frame_delay_file, "r"); | |
3001 if (!f1) { | |
3002 fprintf(stderr, "Could not open '%s'\n", frame_delay_file); | |
3003 exit(1); | |
3004 } | |
3005 } else { | |
3006 f1 = NULL; | |
3007 } | |
3008 | |
3009 first_frame = 1; | |
3010 for(frame_num = 0; ; frame_num++) { | |
3011 if (get_filename_num(filename, sizeof(filename), infilename, frame_num) < 0) { | |
3012 fprintf(stderr, "Invalid filename syntax: '%s'\n", infilename); | |
3013 exit(1); | |
3014 } | |
3015 img = load_image(&md, filename, color_space, bit_depth, limited_range, | |
3016 premultiplied_alpha); | |
3017 if (!img) { | |
3018 if (frame_num == 0) | |
3019 continue; /* accept to start at 0 or 1 */ | |
3020 if (first_frame) { | |
3021 fprintf(stderr, "Could not read '%s'\n", filename); | |
3022 exit(1); | |
3023 } else { | |
3024 break; | |
3025 } | |
3026 } | |
3027 frame_ticks = 1; | |
3028 if (f1) { | |
3029 float fdelay; | |
3030 if (fscanf(f1, "%f", &fdelay) == 1) { | |
3031 frame_ticks = lrint(fdelay * p->frame_delay_den / (p->frame_delay_num * 100)); | |
3032 if (frame_ticks < 1) | |
3033 frame_ticks = 1; | |
3034 } | |
3035 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3036 |
0 | 3037 if (p->verbose) |
3038 printf("Encoding '%s' ticks=%d\n", filename, frame_ticks); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3039 |
0 | 3040 if (keep_metadata && first_frame) { |
3041 bpg_encoder_set_extension_data(enc_ctx, md); | |
3042 } else { | |
3043 bpg_md_free(md); | |
3044 } | |
3045 bpg_encoder_set_frame_duration(enc_ctx, frame_ticks); | |
3046 bpg_encoder_encode(enc_ctx, img, my_write_func, f); | |
3047 image_free(img); | |
3048 | |
3049 first_frame = 0; | |
3050 } | |
3051 if (f1) | |
3052 fclose(f1); | |
3053 /* end of stream */ | |
3054 bpg_encoder_encode(enc_ctx, NULL, my_write_func, f); | |
3055 } else { | |
3056 img = load_image(&md, infilename, color_space, bit_depth, limited_range, | |
3057 premultiplied_alpha); | |
3058 if (!img) { | |
3059 fprintf(stderr, "Could not read '%s'\n", infilename); | |
3060 exit(1); | |
3061 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3062 |
0 | 3063 if (!keep_metadata && md) { |
3064 bpg_md_free(md); | |
3065 md = NULL; | |
3066 } | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3067 |
0 | 3068 bpg_encoder_set_extension_data(enc_ctx, md); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3069 |
0 | 3070 bpg_encoder_encode(enc_ctx, img, my_write_func, f); |
3071 image_free(img); | |
3072 } | |
3073 | |
3074 fclose(f); | |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3075 |
0 | 3076 bpg_encoder_close(enc_ctx); |
5
524eae707ba4
Cosmetics: Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
3077 |
0 | 3078 bpg_encoder_param_free(p); |
3079 | |
3080 return 0; | |
3081 } |