1 /*
2 * TIFF image encoder
3 * Copyright (c) 2007 Bartlomiej Wolowiec
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * TIFF image encoder
25 * @author Bartlomiej Wolowiec
26 */
27
28 #include "config.h"
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif
32
44
45 #define TIFF_MAX_ENTRY 32
46
47 /** sizes of various TIFF field types (string size = 1)*/
49 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4
50 };
51
55
58 unsigned int bpp;
///< bits per pixel
59 int compr;
///< compression level
69 int rps;
///< row per strip
77 uint32_t
dpi;
///< image resolution in DPI
79
80 /**
81 * Check free space in buffer.
82 *
83 * @param s Tiff context
84 * @param need Needed bytes
85 * @return 0 - ok, 1 - no free space
86 */
88 {
92 return 1;
93 }
94 return 0;
95 }
96
97 /**
98 * Put n values to buffer.
99 *
100 * @param p pointer to pointer to output buffer
101 * @param n number of values
102 * @param val pointer to values
103 * @param type type of values
104 * @param flip = 0 - normal copy, >0 - flip
105 */
108 {
109 int i;
110 #if HAVE_BIGENDIAN
111 flip ^= ((int[]) { 0, 0, 0, 1, 3, 3 })[type];
112 #endif
114 *(*p)++ = val[i ^
flip];
115 }
116
117 /**
118 * Add entry to directory in tiff header.
119 *
120 * @param s Tiff context
121 * @param tag tag that identifies the entry
122 * @param type entry type
123 * @param count the number of values
124 * @param ptr_val pointer to values
125 */
128 {
130
132
133 bytestream_put_le16(&entries_ptr, tag);
134 bytestream_put_le16(&entries_ptr, type);
135 bytestream_put_le32(&entries_ptr, count);
136
138 tnput(&entries_ptr, count, ptr_val, type, 0);
139 } else {
140 bytestream_put_le32(&entries_ptr, *s->
buf - s->
buf_start);
143 tnput(s->
buf, count, ptr_val, type, 0);
144 }
145
147 return 0;
148 }
149
152 {
156 type ==
TIFF_SHORT ? (
void *)&w : (
void *)&dw);
157 }
158
159 /**
160 * Encode one strip in tiff file.
161 *
162 * @param s Tiff context
163 * @param src input buffer
164 * @param dst output buffer
165 * @param n size of input buffer
166 * @param compr compression method
167 * @return number of output bytes. If an output error is encountered, -1 is returned
168 */
171 {
172 switch (compr) {
173 #if CONFIG_ZLIB
176 {
178 if (compress(dst, &zlen, src, n) != Z_OK) {
180 return -1;
181 }
182 return zlen;
183 }
184 #endif
187 return -1;
188 memcpy(dst, src, n);
192 src, 1, n, 2, 0xff, -1, 0);
195 default:
196 return -1;
197 }
198 }
199
202 {
203 int i, j, k;
208 for (i = 0; i < w; i++) {
213 *dst++ = *pu++;
215 }
216 }else{
217 for (i = 0; i < w; i++) {
222 *dst++ = *pu++;
224 }
225 }
226 }
227
228 #define ADD_ENTRY(s, tag, type, count, ptr_val) \
229 do { \
230 ret = add_entry(s, tag, type, count, ptr_val); \
231 if (ret < 0) \
232 goto fail; \
233 } while (0)
234
235 #define ADD_ENTRY1(s, tag, type, val) \
236 do { \
237 ret = add_entry1(s, tag, type, val); \
238 if (ret < 0) \
239 goto fail; \
240 } while (0)
241
243 const AVFrame *pict,
int *got_packet)
244 {
248 int i;
251 uint32_t strips;
252 int bytes_per_row;
253 uint32_t res[2] = { s->
dpi, 1 };
// image resolution (72/1)
254 uint16_t bpp_tab[4];
256 int is_yuv = 0,
alpha = 0;
257 int shift_h, shift_v;
258 int packet_size;
259
264
268
276 break;
285 break;
288 break;
291 break;
302 is_yuv = 1;
303 break;
304 default:
306 "This colors format is not supported\n");
307 return -1;
308 }
309
312
316 // best choice for DEFLATE
318 else
319 // suggest size of strip
321 // round rps up
323
325
328 packet_size = avctx->
height * bytes_per_row * 2 +
330
337
340 goto fail;
341 }
342
343 // write header
344 bytestream_put_le16(&ptr, 0x4949);
345 bytestream_put_le16(&ptr, 42);
346
347 offset = ptr;
348 bytestream_put_le32(&ptr, 0);
349
352 goto fail;
353 }
356
359 goto fail;
360 }
361
362 if (is_yuv) {
367 goto fail;
368 }
369 }
370
371 #if CONFIG_ZLIB
374 int zlen, zn;
375 int j;
376
377 zlen = bytes_per_row * s->
rps;
379 if (!zbuf) {
381 goto fail;
382 }
384 zn = 0;
385 for (j = 0; j < s->
rps; j++) {
386 if (is_yuv) {
388 memcpy(zbuf + zn, s->
yuv_line, bytes_per_row);
390 } else
391 memcpy(zbuf + j * bytes_per_row,
393 zn += bytes_per_row;
394 }
397 if (ret < 0) {
399 goto fail;
400 }
403 } else
404 #endif
405 {
410 goto fail;
411 }
412 }
413 for (i = 0; i < s->
height; i++) {
419 }
421 }
422 if (is_yuv) {
426 } else
428 ptr, bytes_per_row, s->
compr);
429 if (ret < 0) {
431 goto fail;
432 }
440 }
441 }
444 }
445
447
451
454
458
461
471 }
474
478
480 uint16_t pal[256 * 3];
481 for (i = 0; i < 256; i++) {
482 uint32_t rgb = *(uint32_t *) (p->
data[1] + i * 4);
483 pal[i] = ((rgb >> 16) & 0xff) * 257;
484 pal[i + 256] = ((rgb >> 8) & 0xff) * 257;
485 pal[i + 512] = (rgb & 0xff) * 257;
486 }
488 }
491 if (is_yuv) {
492 /** according to CCIR Recommendation 601.1 */
493 uint32_t refbw[12] = { 15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1 };
498 }
499 // write offset to dir
500 bytestream_put_le32(&offset, ptr - pkt->
data);
501
504 goto fail;
505 }
506 bytestream_put_le16(&ptr, s->
num_entries);
// write tag count
508 bytestream_put_le32(&ptr, 0);
509
512 *got_packet = 1;
513
514 fail:
515 return ret < 0 ? ret : 0;
516 }
517
519 {
521
525
529
530 return 0;
531 }
532
534 {
536
541
542 return 0;
543 }
544
545 #define OFFSET(x) offsetof(TiffEncoderContext, x)
546 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
553 #if CONFIG_ZLIB
555 #endif
557 };
558
564 };
565
584 },
586 };