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