Mercurial > libavcodec.hg
annotate tiffenc.c @ 4777:536588c41db6 libavcodec
typo
author | michael |
---|---|
date | Tue, 03 Apr 2007 13:53:25 +0000 |
parents | 0860efc2f02b |
children | bca8924ed36c |
rev | line source |
---|---|
4774
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
1 /* |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
2 * TIFF image encoder |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
3 * Copyright (c) 2007 Bartlomiej Wolowiec |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
4 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
5 * This file is part of FFmpeg. |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
6 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
9 * License as published by the Free Software Foundation; either |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
11 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
15 * Lesser General Public License for more details. |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
16 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
20 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
21 */ |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
22 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
23 #include "avcodec.h" |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
24 #ifdef CONFIG_ZLIB |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
25 #include <zlib.h> |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
26 #endif |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
27 #include "bytestream.h" |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
28 #include "tiff.h" |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
29 #include "rle.h" |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
30 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
31 #define TIFF_MAX_ENTRY 32 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
32 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
33 /** sizes of various TIFF field types (string size = 1)*/ |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
34 static const uint8_t type_sizes2[6] = { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
35 0, 1, 1, 2, 4, 8 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
36 }; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
37 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
38 typedef struct TiffEncoderContext { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
39 AVCodecContext *avctx; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
40 AVFrame picture; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
41 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
42 int width; ///< picture width |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
43 int height; ///< picture height |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
44 unsigned int bpp; ///< bits per pixel |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
45 int compr; ///< compression level |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
46 int bpp_tab_size; ///< bpp_tab size |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
47 int invert; ///< photometric interpretation |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
48 int strips; ///< number of strips |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
49 int rps; ///< row per strip |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
50 uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
51 int num_entries; ///< number of entires |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
52 uint8_t **buf; ///< actual position in buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
53 uint8_t *buf_start; ///< pointer to first byte in buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
54 int buf_size; ///< buffer size |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
55 } TiffEncoderContext; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
56 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
57 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
58 /** |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
59 * Check free space in buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
60 * @param s Tiff context |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
61 * @param need Needed bytes |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
62 * @return 0 - ok, 1 - no free space |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
63 */ |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
64 inline static int check_size(TiffEncoderContext * s, uint64_t need) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
65 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
66 if (s->buf_size < *s->buf - s->buf_start + need) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
67 *s->buf = s->buf_start + s->buf_size + 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
68 av_log(s->avctx, AV_LOG_ERROR, "Buffer is too small\n"); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
69 return 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
70 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
71 return 0; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
72 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
73 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
74 /** |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
75 * Put n values to buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
76 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
77 * @param p Pointer to pointer to output buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
78 * @param n Number of values |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
79 * @param val Pointer to values |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
80 * @param type Type of values |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
81 * @param flip =0 - normal copy, >0 - flip |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
82 */ |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
83 static void tnput(uint8_t ** p, int n, const uint8_t * val, enum TiffTypes type, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
84 int flip) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
85 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
86 int i; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
87 #ifdef WORDS_BIGENDIAN |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
88 flip ^= ((int[]) {0, 0, 0, 1, 3, 3})[type]; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
89 #endif |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
90 for (i = 0; i < n * type_sizes2[type]; i++) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
91 *(*p)++ = val[i ^ flip]; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
92 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
93 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
94 /** |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
95 * Add entry to directory in tiff header. |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
96 * @param s Tiff context |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
97 * @param tag Tag that identifies the entry |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
98 * @param type Entry type |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
99 * @param count The number of values |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
100 * @param ptr_val Pointer to values |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
101 */ |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
102 static void add_entry(TiffEncoderContext * s, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
103 enum TiffTags tag, enum TiffTypes type, int count, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
104 const void *ptr_val) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
105 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
106 uint8_t *entries_ptr = s->entries + 12 * s->num_entries; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
107 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
108 assert(s->num_entries < TIFF_MAX_ENTRY); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
109 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
110 bytestream_put_le16(&entries_ptr, tag); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
111 bytestream_put_le16(&entries_ptr, type); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
112 bytestream_put_le32(&entries_ptr, count); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
113 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
114 if (type_sizes[type] * count <= 4) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
115 tnput(&entries_ptr, count, ptr_val, type, 0); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
116 } else { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
117 bytestream_put_le32(&entries_ptr, *s->buf - s->buf_start); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
118 check_size(s, count * type_sizes2[type]); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
119 tnput(s->buf, count, ptr_val, type, 0); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
120 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
121 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
122 s->num_entries++; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
123 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
124 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
125 /** |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
126 * Encode one strip in tiff file |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
127 * |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
128 * @param s Tiff context |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
129 * @param src Input buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
130 * @param dst Output buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
131 * @param n Size of input buffer |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
132 * @param compr Compression method |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
133 * @return Number of output bytes. If an output error is encountered, -1 returned |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
134 */ |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
135 static int encode_strip(TiffEncoderContext * s, const int8_t * src, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
136 uint8_t * dst, int n, int compr) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
137 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
138 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
139 switch (compr) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
140 #ifdef CONFIG_ZLIB |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
141 case TIFF_DEFLATE: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
142 case TIFF_ADOBE_DEFLATE: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
143 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
144 unsigned long zlen = s->buf_size - (*s->buf - s->buf_start); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
145 if (compress(dst, &zlen, src, n) != Z_OK) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
146 av_log(s->avctx, AV_LOG_ERROR, "Compressing failed\n"); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
147 return -1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
148 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
149 return zlen; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
150 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
151 #endif |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
152 case TIFF_RAW: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
153 if (check_size(s, n)) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
154 return -1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
155 memcpy(dst, src, n); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
156 return n; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
157 case TIFF_PACKBITS: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
158 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
159 default: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
160 return -1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
161 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
162 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
163 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
164 static int encode_frame(AVCodecContext * avctx, unsigned char *buf, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
165 int buf_size, void *data) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
166 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
167 TiffEncoderContext *s = avctx->priv_data; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
168 AVFrame *pict = data; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
169 AVFrame *const p = (AVFrame *) & s->picture; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
170 int i; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
171 int n; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
172 uint8_t *ptr = buf; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
173 uint8_t *offset; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
174 uint32_t strips; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
175 uint32_t *strip_sizes = NULL; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
176 uint32_t *strip_offsets = NULL; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
177 int bytes_per_row; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
178 uint32_t res[2] = { 72, 1 }; // image resolution (72/1) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
179 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 }; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
180 int ret = -1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
181 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
182 s->buf_start = buf; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
183 s->buf = &ptr; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
184 s->buf_size = buf_size; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
185 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
186 *p = *pict; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
187 p->pict_type = FF_I_TYPE; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
188 p->key_frame = 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
189 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
190 s->compr = TIFF_PACKBITS; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
191 if (avctx->compression_level == 0) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
192 s->compr = TIFF_RAW; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
193 #ifdef CONFIG_ZLIB |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
194 } else if ((avctx->compression_level > 2)) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
195 s->compr = TIFF_DEFLATE; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
196 #endif |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
197 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
198 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
199 s->width = avctx->width; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
200 s->height = avctx->height; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
201 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
202 switch (avctx->pix_fmt) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
203 case PIX_FMT_RGB24: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
204 s->bpp = 24; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
205 s->invert = 2; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
206 break; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
207 case PIX_FMT_GRAY8: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
208 s->bpp = 8; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
209 s->invert = 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
210 break; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
211 case PIX_FMT_PAL8: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
212 s->bpp = 8; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
213 s->invert = 3; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
214 break; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
215 case PIX_FMT_MONOBLACK: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
216 s->bpp = 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
217 s->invert = 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
218 break; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
219 case PIX_FMT_MONOWHITE: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
220 s->bpp = 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
221 s->invert = 0; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
222 break; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
223 default: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
224 av_log(s->avctx, AV_LOG_ERROR, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
225 "This colors format is not supported\n"); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
226 return -1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
227 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
228 s->bpp_tab_size = (s->bpp >> 3); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
229 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
230 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
231 //best choose for DEFLATE |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
232 s->rps = s->height; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
233 else |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
234 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
235 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
236 strips = (s->height - 1) / s->rps + 1; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
237 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
238 if (check_size(s, 8)) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
239 goto fail; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
240 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
241 // write header |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
242 bytestream_put_le16(&ptr, 0x4949); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
243 bytestream_put_le16(&ptr, 42); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
244 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
245 offset = ptr; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
246 bytestream_put_le32(&ptr, 0); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
247 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
248 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
249 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
250 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
251 bytes_per_row = (s->width * s->bpp + 7) >> 3; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
252 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
253 #ifdef CONFIG_ZLIB |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
254 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
255 uint8_t *zbuf; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
256 int zlen, zn; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
257 int j; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
258 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
259 zlen = bytes_per_row * s->rps; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
260 zbuf = av_malloc(zlen); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
261 strip_offsets[0] = ptr - buf; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
262 zn = 0; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
263 for (j = 0; j < s->rps; j++) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
264 memcpy(zbuf + j * bytes_per_row, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
265 p->data[0] + j * p->linesize[0], bytes_per_row); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
266 zn += bytes_per_row; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
267 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
268 n = encode_strip(s, zbuf, ptr, zn, s->compr); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
269 av_free(zbuf); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
270 if (n<0) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
271 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n"); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
272 goto fail; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
273 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
274 ptr += n; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
275 strip_sizes[0] = ptr - buf - strip_offsets[0]; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
276 } else |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
277 #endif |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
278 { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
279 for (i = 0; i < s->height; i++) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
280 if (strip_sizes[i / s->rps] == 0) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
281 strip_offsets[i / s->rps] = ptr - buf; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
282 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
283 if ((n = encode_strip(s, p->data[0] + i * p->linesize[0] |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
284 , ptr, bytes_per_row, s->compr)) < 0) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
285 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n"); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
286 goto fail; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
287 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
288 strip_sizes[i / s->rps] += n; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
289 ptr += n; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
290 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
291 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
292 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
293 s->num_entries = 0; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
294 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
295 add_entry(s, TIFF_SUBFILE, TIFF_LONG, 1, (uint32_t[]) {0}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
296 add_entry(s, TIFF_WIDTH, TIFF_LONG, 1, (uint32_t[]) {s->width}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
297 add_entry(s, TIFF_HEIGHT, TIFF_LONG, 1, (uint32_t[]) {s->height}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
298 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
299 if (s->bpp_tab_size) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
300 add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
301 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
302 add_entry(s, TIFF_COMPR, TIFF_SHORT, 1, (uint16_t[]) {s->compr}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
303 add_entry(s, TIFF_INVERT, TIFF_SHORT, 1, (uint16_t[]) {s->invert}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
304 add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
305 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
306 if (s->bpp_tab_size) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
307 add_entry(s, TIFF_SAMPLES_PER_PIXEL, TIFF_SHORT, 1, (uint16_t[]) {s->bpp_tab_size}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
308 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
309 add_entry(s, TIFF_ROWSPERSTRIP, TIFF_LONG, 1, (uint32_t[]) {s->rps}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
310 add_entry(s, TIFF_STRIP_SIZE, TIFF_LONG, strips, strip_sizes); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
311 add_entry(s, TIFF_XRES, TIFF_RATIONAL, 1, res); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
312 add_entry(s, TIFF_YRES, TIFF_RATIONAL, 1, res); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
313 add_entry(s, TIFF_RES_UNIT, TIFF_SHORT, 1, (uint16_t[]) {2}); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
314 add_entry(s, TIFF_SOFTWARE_NAME, TIFF_STRING, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
315 strlen(LIBAVCODEC_IDENT) + 1, LIBAVCODEC_IDENT); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
316 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
317 if (avctx->pix_fmt == PIX_FMT_PAL8) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
318 uint16_t pal[256 * 3]; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
319 for (i = 0; i < 256; i++) { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
320 uint32_t rgb = *(uint32_t *) (p->data[1] + i * 4); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
321 pal[i] = ((rgb >> 16) & 0xff) * 257; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
322 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
323 pal[i + 512] = ( rgb & 0xff) * 257; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
324 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
325 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
326 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
327 bytestream_put_le32(&offset, ptr - buf); // write offset to dir |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
328 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
329 if (check_size(s, 6 + s->num_entries * 12)) |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
330 goto fail; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
331 bytestream_put_le16(&ptr, s->num_entries); // write tag count |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
332 bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
333 bytestream_put_le32(&ptr, 0); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
334 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
335 ret = ptr - buf; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
336 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
337 fail: |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
338 av_free(strip_sizes); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
339 av_free(strip_offsets); |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
340 return ret; |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
341 } |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
342 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
343 AVCodec tiff_encoder = { |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
344 "tiff", |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
345 CODEC_TYPE_VIDEO, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
346 CODEC_ID_TIFF, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
347 sizeof(TiffEncoderContext), |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
348 NULL, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
349 encode_frame, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
350 NULL, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
351 NULL, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
352 0, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
353 NULL, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
354 .pix_fmts = |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
355 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
356 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE, |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
357 -1} |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
358 |
0860efc2f02b
tiff encoder by (Bartlomiej Wolowiec b.wolowiec students mimuw edu pl)
michael
parents:
diff
changeset
|
359 }; |