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