1 /* PDFlib GmbH cvsid:
2 * $Id: tif_compress.c,v 1.12 2005/12/21 14:12:52 rjs Exp $ */
3
4 /*
5 * Copyright (c) 1988-1997 Sam Leffler
6 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and
9 * its documentation for any purpose is hereby granted without fee, provided
10 * that (i) the above copyright notices and this permission notice appear in
11 * all copies of the software and related documentation, and (ii) the names of
12 * Sam Leffler and Silicon Graphics may not be used in any advertising or
13 * publicity relating to the software without the specific, prior written
14 * permission of Sam Leffler and Silicon Graphics.
15 *
16 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
21 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 * OF THIS SOFTWARE.
26 */
27
28 /*
29 * TIFF Library
30 *
31 * Compression Scheme Configuration Support.
32 */
33 #include "tiffiop.h"
34
35 static int
TIFFNoEncode(TIFF * tif,const char * method)36 TIFFNoEncode(TIFF* tif, const char* method)
37 {
38 const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
39
40 if (c) {
41 _TIFFError(tif, tif->tif_name,
42 "%s %s encoding is not implemented",
43 c->name, method);
44 } else {
45 _TIFFError(tif, tif->tif_name,
46 "Compression scheme %u %s encoding is not implemented",
47 tif->tif_dir.td_compression, method);
48 }
49 return (-1);
50 }
51
52 int
_TIFFNoRowEncode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)53 _TIFFNoRowEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
54 {
55 (void) pp; (void) cc; (void) s;
56 return (TIFFNoEncode(tif, "scanline"));
57 }
58
59 int
_TIFFNoStripEncode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)60 _TIFFNoStripEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
61 {
62 (void) pp; (void) cc; (void) s;
63 return (TIFFNoEncode(tif, "strip"));
64 }
65
66 int
_TIFFNoTileEncode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)67 _TIFFNoTileEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
68 {
69 (void) pp; (void) cc; (void) s;
70 return (TIFFNoEncode(tif, "tile"));
71 }
72
73 static int
TIFFNoDecode(TIFF * tif,const char * method)74 TIFFNoDecode(TIFF* tif, const char* method)
75 {
76 const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
77
78 if (c)
79 _TIFFError(tif, tif->tif_name,
80 "%s %s decoding is not implemented",
81 c->name, method);
82 else
83 _TIFFError(tif, tif->tif_name,
84 "Compression scheme %u %s decoding is not implemented",
85 tif->tif_dir.td_compression, method);
86 return (-1);
87 }
88
89 int
_TIFFNoRowDecode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)90 _TIFFNoRowDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
91 {
92 (void) pp; (void) cc; (void) s;
93 return (TIFFNoDecode(tif, "scanline"));
94 }
95
96 int
_TIFFNoStripDecode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)97 _TIFFNoStripDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
98 {
99 (void) pp; (void) cc; (void) s;
100 return (TIFFNoDecode(tif, "strip"));
101 }
102
103 int
_TIFFNoTileDecode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)104 _TIFFNoTileDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
105 {
106 (void) pp; (void) cc; (void) s;
107 return (TIFFNoDecode(tif, "tile"));
108 }
109
110 int
_TIFFNoSeek(TIFF * tif,uint32 off)111 _TIFFNoSeek(TIFF* tif, uint32 off)
112 {
113 (void) off;
114 _TIFFError(tif, tif->tif_name,
115 "Compression algorithm does not support random access");
116 return (0);
117 }
118
119 int
_TIFFNoPreCode(TIFF * tif,tsample_t s)120 _TIFFNoPreCode(TIFF* tif, tsample_t s)
121 {
122 (void) tif; (void) s;
123 return (1);
124 }
125
_TIFFtrue(TIFF * tif)126 static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
_TIFFvoid(TIFF * tif)127 static void _TIFFvoid(TIFF* tif) { (void) tif; }
128
129 void
_TIFFSetDefaultCompressionState(TIFF * tif)130 _TIFFSetDefaultCompressionState(TIFF* tif)
131 {
132 tif->tif_decodestatus = TRUE;
133 tif->tif_setupdecode = _TIFFtrue;
134 tif->tif_predecode = _TIFFNoPreCode;
135 tif->tif_decoderow = _TIFFNoRowDecode;
136 tif->tif_decodestrip = _TIFFNoStripDecode;
137 tif->tif_decodetile = _TIFFNoTileDecode;
138 tif->tif_encodestatus = TRUE;
139 tif->tif_setupencode = _TIFFtrue;
140 tif->tif_preencode = _TIFFNoPreCode;
141 tif->tif_postencode = _TIFFtrue;
142 tif->tif_encoderow = _TIFFNoRowEncode;
143 tif->tif_encodestrip = _TIFFNoStripEncode;
144 tif->tif_encodetile = _TIFFNoTileEncode;
145 tif->tif_close = _TIFFvoid;
146 tif->tif_seek = _TIFFNoSeek;
147 tif->tif_cleanup = _TIFFvoid;
148 tif->tif_defstripsize = _TIFFDefaultStripSize;
149 tif->tif_deftilesize = _TIFFDefaultTileSize;
150 tif->tif_flags &= ~TIFF_NOBITREV;
151 }
152
153 int
TIFFSetCompressionScheme(TIFF * tif,int scheme)154 TIFFSetCompressionScheme(TIFF* tif, int scheme)
155 {
156 const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
157
158 _TIFFSetDefaultCompressionState(tif);
159 /*
160 * Don't treat an unknown compression scheme as an error.
161 * This permits applications to open files with data that
162 * the library does not have builtin support for, but which
163 * may still be meaningful.
164 */
165 return (c ? (*c->init)(tif, scheme) : 1);
166 }
167
168 /*
169 * Other compression schemes may be registered. Registered
170 * schemes can also override the builtin versions provided
171 * by this library.
172 */
173 typedef struct _codec {
174 struct _codec* next;
175 TIFFCodec* info;
176 } codec_t;
177 static codec_t* registeredCODECS = NULL;
178
179 const TIFFCodec*
TIFFFindCODEC(uint16 scheme)180 TIFFFindCODEC(uint16 scheme)
181 {
182 const TIFFCodec* c;
183 codec_t* cd;
184
185 for (cd = registeredCODECS; cd; cd = cd->next)
186 if (cd->info->scheme == scheme)
187 return ((const TIFFCodec*) cd->info);
188 for (c = _TIFFBuiltinCODECS; c->name; c++)
189 if (c->scheme == scheme)
190 return (c);
191 return ((const TIFFCodec*) 0);
192 }
193
194 #ifdef PDF_used
195 TIFFCodec*
TIFFRegisterCODEC(uint16 scheme,const char * name,TIFFInitMethod init)196 TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
197 {
198 codec_t* cd = (codec_t*) _TIFFmalloc(
199 sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1);
200
201 if (cd != NULL) {
202 cd->info = (TIFFCodec*) ((tidata_t) cd + sizeof (codec_t));
203 cd->info->name = (char*)
204 ((tidata_t) cd->info + sizeof (TIFFCodec));
205 strcpy(cd->info->name, name);
206 cd->info->scheme = scheme;
207 cd->info->init = init;
208 cd->next = registeredCODECS;
209 registeredCODECS = cd;
210 } else {
211 _TIFFError(tif, "TIFFRegisterCODEC",
212 "No space to register compression scheme %s", name);
213 return NULL;
214 }
215 return (cd->info);
216 }
217 #endif
218
219 #ifdef PDF_used
220 void
TIFFUnRegisterCODEC(TIFFCodec * c)221 TIFFUnRegisterCODEC(TIFFCodec* c)
222 {
223 codec_t* cd;
224 codec_t** pcd;
225
226 for (pcd = ®isteredCODECS; (cd = *pcd); pcd = &cd->next)
227 if (cd->info == c) {
228 *pcd = cd->next;
229 _TIFFfree(cd);
230 return;
231 }
232 _TIFFError(tif, "TIFFUnRegisterCODEC",
233 "Cannot remove compression scheme %s; not registered", c->name);
234 }
235 #endif
236
237 /************************************************************************/
238 /* TIFFGetConfisuredCODECs() */
239 /************************************************************************/
240
241 /**
242 * Get list of configured codecs, both built-in and registered by user.
243 * Caller is responsible to free this structure.
244 *
245 * @return returns array of TIFFCodec records (the last record should be NULL)
246 * or NULL if function failed.
247 */
248
249 TIFFCodec*
TIFFGetConfiguredCODECs(TIFF * tif)250 TIFFGetConfiguredCODECs(TIFF *tif)
251 {
252 int i = 1;
253 codec_t *cd;
254 const TIFFCodec *c;
255 TIFFCodec *codecs = NULL, *new_codecs;
256
257 for (cd = registeredCODECS; cd; cd = cd->next) {
258 new_codecs = (TIFFCodec *)
259 _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
260 if (!new_codecs) {
261 _TIFFfree (codecs);
262 return NULL;
263 }
264 codecs = new_codecs;
265 _TIFFmemcpy(codecs + i - 1, cd, sizeof(TIFFCodec));
266 i++;
267 }
268 for (c = _TIFFBuiltinCODECS; c->name; c++) {
269 if (TIFFIsCODECConfigured(c->scheme)) {
270 new_codecs = (TIFFCodec *)
271 _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
272 if (!new_codecs) {
273 _TIFFfree (codecs);
274 return NULL;
275 }
276 codecs = new_codecs;
277 _TIFFmemcpy(codecs + i - 1, (const tdata_t)c,
278 sizeof(TIFFCodec));
279 i++;
280 }
281 }
282
283 new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
284 if (!new_codecs) {
285 _TIFFfree (codecs);
286 return NULL;
287 }
288 codecs = new_codecs;
289 _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
290
291 return codecs;
292 }
293
294 /* vim: set ts=8 sts=8 sw=8 noet: */
295