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
56
59 unsigned int bpp;
///< bits per pixel
60 int compr;
///< compression level
70 int rps;
///< row per strip
78 uint32_t
dpi;
///< image resolution in DPI
80
81 /**
82 * Check free space in buffer.
83 *
84 * @param s Tiff context
85 * @param need Needed bytes
86 * @return 0 - ok, 1 - no free space
87 */
89 {
93 return 1;
94 }
95 return 0;
96 }
97
98 /**
99 * Put n values to buffer.
100 *
101 * @param p pointer to pointer to output buffer
102 * @param n number of values
103 * @param val pointer to values
104 * @param type type of values
105 * @param flip = 0 - normal copy, >0 - flip
106 */
109 {
110 int i;
111 #if HAVE_BIGENDIAN
112 flip ^= ((int[]) { 0, 0, 0, 1, 3, 3 })[type];
113 #endif
115 *(*p)++ = val[i ^
flip];
116 }
117
118 /**
119 * Add entry to directory in tiff header.
120 *
121 * @param s Tiff context
122 * @param tag tag that identifies the entry
123 * @param type entry type
124 * @param count the number of values
125 * @param ptr_val pointer to values
126 */
129 {
131
133
134 bytestream_put_le16(&entries_ptr, tag);
135 bytestream_put_le16(&entries_ptr, type);
136 bytestream_put_le32(&entries_ptr, count);
137
139 tnput(&entries_ptr, count, ptr_val, type, 0);
140 } else {
141 bytestream_put_le32(&entries_ptr, *s->
buf - s->
buf_start);
143 tnput(s->
buf, count, ptr_val, type, 0);
144 }
145
147 }
148
151 {
155 }
156
157 /**
158 * Encode one strip in tiff file.
159 *
160 * @param s Tiff context
161 * @param src input buffer
162 * @param dst output buffer
163 * @param n size of input buffer
164 * @param compr compression method
165 * @return number of output bytes. If an output error is encountered, -1 is returned
166 */
169 {
170 switch (compr) {
171 #if CONFIG_ZLIB
174 {
176 if (compress(dst, &zlen, src, n) != Z_OK) {
178 return -1;
179 }
180 return zlen;
181 }
182 #endif
185 return -1;
186 memcpy(dst, src, n);
190 src, 1, n, 2, 0xff, -1, 0);
193 default:
194 return -1;
195 }
196 }
197
199 {
201 int i, j, k;
206 for (i = 0; i < w; i++) {
211 *dst++ = *pu++;
213 }
214 }else{
215 for (i = 0; i < w; i++) {
220 *dst++ = *pu++;
222 }
223 }
224 }
225
227 {
229
234
235 return 0;
236 }
237
239 const AVFrame *pict,
int *got_packet)
240 {
244 int i;
247 uint32_t strips;
248 int bytes_per_row;
249 uint32_t
res[2] = { s->
dpi, 1 };
// image resolution (72/1)
250 uint16_t bpp_tab[4];
252 int is_yuv = 0,
alpha = 0;
253 int shift_h, shift_v;
254
255 *p = *pict;
256
261
265
273 break;
281 break;
284 break;
287 break;
298 is_yuv = 1;
299 break;
300 default:
302 "This colors format is not supported\n");
303 return -1;
304 }
305
308
312 // best choice for DEFLATE
314 else
315 // suggest size of strip
317 // round rps up
319
321
330
332 goto fail;
333
334 // write header
335 bytestream_put_le16(&ptr, 0x4949);
336 bytestream_put_le16(&ptr, 42);
337
338 offset = ptr;
339 bytestream_put_le32(&ptr, 0);
340
343
346 goto fail;
347 }
348
351 if (is_yuv) {
356 goto fail;
357 }
358 }
359
360 #if CONFIG_ZLIB
363 int zlen, zn;
364 int j;
365
366 zlen = bytes_per_row * s->
rps;
368 if (!zbuf) {
370 goto fail;
371 }
373 zn = 0;
374 for (j = 0; j < s->
rps; j++) {
375 if (is_yuv) {
377 memcpy(zbuf + zn, s->
yuv_line, bytes_per_row);
379 } else
380 memcpy(zbuf + j * bytes_per_row,
382 zn += bytes_per_row;
383 }
386 if (ret < 0) {
388 goto fail;
389 }
392 } else
393 #endif
394 {
399 goto fail;
400 }
401 }
402 for (i = 0; i < s->
height; i++) {
408 }
410 }
411 if (is_yuv) {
415 } else
417 ptr, bytes_per_row, s->
compr);
418 if (ret < 0) {
420 goto fail;
421 }
429 }
430 }
433 }
434
436
440
443
447
450
456
460
462 uint16_t pal[256 * 3];
463 for (i = 0; i < 256; i++) {
464 uint32_t rgb = *(uint32_t *) (p->
data[1] + i * 4);
465 pal[i] = ((rgb >> 16) & 0xff) * 257;
466 pal[i + 256] = ((rgb >> 8) & 0xff) * 257;
467 pal[i + 512] = (rgb & 0xff) * 257;
468 }
470 }
473 if (is_yuv) {
474 /** according to CCIR Recommendation 601.1 */
475 uint32_t refbw[12] = { 15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1 };
480 }
481 // write offset to dir
482 bytestream_put_le32(&offset, ptr - pkt->
data);
483
486 goto fail;
487 }
488 bytestream_put_le16(&ptr, s->
num_entries);
// write tag count
490 bytestream_put_le32(&ptr, 0);
491
494 *got_packet = 1;
495
496 fail:
497 return ret < 0 ? ret : 0;
498 }
499
501 {
503
507
508 return 0;
509 }
510
511 #define OFFSET(x) offsetof(TiffEncoderContext, x)
512 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
519 #if CONFIG_ZLIB
521 #endif
522 { NULL },
523 };
524
530 };
531
549 },
551 };