1 | /* $Header: /afs/dev.mit.edu/source/repository/third/tiff/libtiff/tif_compress.c,v 1.1.1.1 2002-12-26 02:37:59 ghudson Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1988-1997 Sam Leffler |
---|
5 | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
---|
6 | * |
---|
7 | * Permission to use, copy, modify, distribute, and sell this software and |
---|
8 | * its documentation for any purpose is hereby granted without fee, provided |
---|
9 | * that (i) the above copyright notices and this permission notice appear in |
---|
10 | * all copies of the software and related documentation, and (ii) the names of |
---|
11 | * Sam Leffler and Silicon Graphics may not be used in any advertising or |
---|
12 | * publicity relating to the software without the specific, prior written |
---|
13 | * permission of Sam Leffler and Silicon Graphics. |
---|
14 | * |
---|
15 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
---|
16 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
---|
17 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
---|
18 | * |
---|
19 | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
---|
20 | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
---|
21 | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
---|
22 | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
---|
23 | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
---|
24 | * OF THIS SOFTWARE. |
---|
25 | */ |
---|
26 | |
---|
27 | /* |
---|
28 | * TIFF Library |
---|
29 | * |
---|
30 | * Compression Scheme Configuration Support. |
---|
31 | */ |
---|
32 | #include "tiffiop.h" |
---|
33 | |
---|
34 | static int |
---|
35 | TIFFNoEncode(TIFF* tif, char* method) |
---|
36 | { |
---|
37 | const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression); |
---|
38 | |
---|
39 | if (c) { |
---|
40 | if (! strncmp(c->name, "LZW", 3) ){ |
---|
41 | TIFFError(tif->tif_name, |
---|
42 | "%s %s encoding is no longer implemented due to Unisys patent enforcement", |
---|
43 | c->name, method); |
---|
44 | } else { |
---|
45 | TIFFError(tif->tif_name, "%s %s encoding is not implemented", |
---|
46 | c->name, method); |
---|
47 | } |
---|
48 | } |
---|
49 | else { |
---|
50 | TIFFError(tif->tif_name, |
---|
51 | "Compression scheme %u %s encoding is not implemented", |
---|
52 | tif->tif_dir.td_compression, method); |
---|
53 | } |
---|
54 | return (-1); |
---|
55 | } |
---|
56 | |
---|
57 | int |
---|
58 | _TIFFNoRowEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
---|
59 | { |
---|
60 | (void) pp; (void) cc; (void) s; |
---|
61 | return (TIFFNoEncode(tif, "scanline")); |
---|
62 | } |
---|
63 | |
---|
64 | int |
---|
65 | _TIFFNoStripEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
---|
66 | { |
---|
67 | (void) pp; (void) cc; (void) s; |
---|
68 | return (TIFFNoEncode(tif, "strip")); |
---|
69 | } |
---|
70 | |
---|
71 | int |
---|
72 | _TIFFNoTileEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
---|
73 | { |
---|
74 | (void) pp; (void) cc; (void) s; |
---|
75 | return (TIFFNoEncode(tif, "tile")); |
---|
76 | } |
---|
77 | |
---|
78 | static int |
---|
79 | TIFFNoDecode(TIFF* tif, char* method) |
---|
80 | { |
---|
81 | const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression); |
---|
82 | |
---|
83 | if (c) |
---|
84 | TIFFError(tif->tif_name, "%s %s decoding is not implemented", |
---|
85 | c->name, method); |
---|
86 | else |
---|
87 | TIFFError(tif->tif_name, |
---|
88 | "Compression scheme %u %s decoding is not implemented", |
---|
89 | tif->tif_dir.td_compression, method); |
---|
90 | return (-1); |
---|
91 | } |
---|
92 | |
---|
93 | int |
---|
94 | _TIFFNoRowDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
---|
95 | { |
---|
96 | (void) pp; (void) cc; (void) s; |
---|
97 | return (TIFFNoDecode(tif, "scanline")); |
---|
98 | } |
---|
99 | |
---|
100 | int |
---|
101 | _TIFFNoStripDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
---|
102 | { |
---|
103 | (void) pp; (void) cc; (void) s; |
---|
104 | return (TIFFNoDecode(tif, "strip")); |
---|
105 | } |
---|
106 | |
---|
107 | int |
---|
108 | _TIFFNoTileDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
---|
109 | { |
---|
110 | (void) pp; (void) cc; (void) s; |
---|
111 | return (TIFFNoDecode(tif, "tile")); |
---|
112 | } |
---|
113 | |
---|
114 | int |
---|
115 | _TIFFNoSeek(TIFF* tif, uint32 off) |
---|
116 | { |
---|
117 | (void) off; |
---|
118 | TIFFError(tif->tif_name, |
---|
119 | "Compression algorithm does not support random access"); |
---|
120 | return (0); |
---|
121 | } |
---|
122 | |
---|
123 | int |
---|
124 | _TIFFNoPreCode(TIFF* tif, tsample_t s) |
---|
125 | { |
---|
126 | (void) tif; (void) s; |
---|
127 | return (1); |
---|
128 | } |
---|
129 | |
---|
130 | static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); } |
---|
131 | static void _TIFFvoid(TIFF* tif) { (void) tif; } |
---|
132 | |
---|
133 | void |
---|
134 | _TIFFSetDefaultCompressionState(TIFF* tif) |
---|
135 | { |
---|
136 | tif->tif_setupdecode = _TIFFtrue; |
---|
137 | tif->tif_predecode = _TIFFNoPreCode; |
---|
138 | tif->tif_decoderow = _TIFFNoRowDecode; |
---|
139 | tif->tif_decodestrip = _TIFFNoStripDecode; |
---|
140 | tif->tif_decodetile = _TIFFNoTileDecode; |
---|
141 | tif->tif_setupencode = _TIFFtrue; |
---|
142 | tif->tif_preencode = _TIFFNoPreCode; |
---|
143 | tif->tif_postencode = _TIFFtrue; |
---|
144 | tif->tif_encoderow = _TIFFNoRowEncode; |
---|
145 | tif->tif_encodestrip = _TIFFNoStripEncode; |
---|
146 | tif->tif_encodetile = _TIFFNoTileEncode; |
---|
147 | tif->tif_close = _TIFFvoid; |
---|
148 | tif->tif_seek = _TIFFNoSeek; |
---|
149 | tif->tif_cleanup = _TIFFvoid; |
---|
150 | tif->tif_defstripsize = _TIFFDefaultStripSize; |
---|
151 | tif->tif_deftilesize = _TIFFDefaultTileSize; |
---|
152 | tif->tif_flags &= ~TIFF_NOBITREV; |
---|
153 | } |
---|
154 | |
---|
155 | int |
---|
156 | TIFFSetCompressionScheme(TIFF* tif, int scheme) |
---|
157 | { |
---|
158 | const TIFFCodec *c = TIFFFindCODEC((uint16) scheme); |
---|
159 | |
---|
160 | _TIFFSetDefaultCompressionState(tif); |
---|
161 | /* |
---|
162 | * Don't treat an unknown compression scheme as an error. |
---|
163 | * This permits applications to open files with data that |
---|
164 | * the library does not have builtin support for, but which |
---|
165 | * may still be meaningful. |
---|
166 | */ |
---|
167 | return (c ? (*c->init)(tif, scheme) : 1); |
---|
168 | } |
---|
169 | |
---|
170 | /* |
---|
171 | * Other compression schemes may be registered. Registered |
---|
172 | * schemes can also override the builtin versions provided |
---|
173 | * by this library. |
---|
174 | */ |
---|
175 | typedef struct _codec { |
---|
176 | struct _codec* next; |
---|
177 | TIFFCodec* info; |
---|
178 | } codec_t; |
---|
179 | static codec_t* registeredCODECS = NULL; |
---|
180 | |
---|
181 | const TIFFCodec* |
---|
182 | TIFFFindCODEC(uint16 scheme) |
---|
183 | { |
---|
184 | const TIFFCodec* c; |
---|
185 | codec_t* cd; |
---|
186 | |
---|
187 | for (cd = registeredCODECS; cd; cd = cd->next) |
---|
188 | if (cd->info->scheme == scheme) |
---|
189 | return ((const TIFFCodec*) cd->info); |
---|
190 | for (c = _TIFFBuiltinCODECS; c->name; c++) |
---|
191 | if (c->scheme == scheme) |
---|
192 | return (c); |
---|
193 | return ((const TIFFCodec*) 0); |
---|
194 | } |
---|
195 | |
---|
196 | TIFFCodec* |
---|
197 | TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init) |
---|
198 | { |
---|
199 | codec_t* cd = (codec_t*) |
---|
200 | _TIFFmalloc(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1); |
---|
201 | |
---|
202 | if (cd != NULL) { |
---|
203 | cd->info = (TIFFCodec*) ((tidata_t) cd + sizeof (codec_t)); |
---|
204 | cd->info->name = (char*) |
---|
205 | ((tidata_t) cd->info + sizeof (TIFFCodec)); |
---|
206 | strcpy(cd->info->name, name); |
---|
207 | cd->info->scheme = scheme; |
---|
208 | cd->info->init = init; |
---|
209 | cd->next = registeredCODECS; |
---|
210 | registeredCODECS = cd; |
---|
211 | } else |
---|
212 | TIFFError("TIFFRegisterCODEC", |
---|
213 | "No space to register compression scheme %s", name); |
---|
214 | return (cd->info); |
---|
215 | } |
---|
216 | |
---|
217 | void |
---|
218 | TIFFUnRegisterCODEC(TIFFCodec* c) |
---|
219 | { |
---|
220 | codec_t* cd; |
---|
221 | codec_t** pcd; |
---|
222 | |
---|
223 | for (pcd = ®isteredCODECS; (cd = *pcd); pcd = &cd->next) |
---|
224 | if (cd->info == c) { |
---|
225 | *pcd = cd->next; |
---|
226 | _TIFFfree(cd); |
---|
227 | return; |
---|
228 | } |
---|
229 | TIFFError("TIFFUnRegisterCODEC", |
---|
230 | "Cannot remove compression scheme %s; not registered", c->name); |
---|
231 | } |
---|