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