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