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