Line data Source code
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 : /**
31 : * @file tif_compress.c
32 : * @brief Register, look up and select compression schemes, and provide the
33 : * default codec methods.
34 : */
35 :
36 : #include "tiffiop.h"
37 :
38 : /**
39 : * @brief Diagnose an unsupported encoder operation.
40 : * @ingroup libtiff_private_codecs
41 : *
42 : * Looks up the name and invokes handle diagnostics; no buffer access,
43 : * allocation or pixel I/O.
44 : *
45 : * @param[in] tif Non-NULL live handle; td_compression and tif_name are read
46 : * for the diagnostic and codec state is unchanged.
47 : * @param[in] method Borrowed method label used only during diagnostics.
48 : * @return Minus one unconditionally.
49 : */
50 0 : static int TIFFNoEncode(TIFF *tif, const char *method)
51 : {
52 0 : const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
53 :
54 0 : if (c)
55 : {
56 0 : TIFFErrorExtR(tif, tif->tif_name, "%s %s encoding is not implemented",
57 : c->name, method);
58 : }
59 : else
60 : {
61 0 : TIFFErrorExtR(tif, tif->tif_name,
62 : "Compression scheme %" PRIu16
63 : " %s encoding is not implemented",
64 0 : tif->tif_dir.td_compression, method);
65 : }
66 0 : return (-1);
67 : }
68 :
69 0 : int _TIFFNoRowEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
70 : {
71 : (void)pp;
72 : (void)cc;
73 : (void)s;
74 0 : return (TIFFNoEncode(tif, "scanline"));
75 : }
76 :
77 0 : int _TIFFNoStripEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
78 : {
79 : (void)pp;
80 : (void)cc;
81 : (void)s;
82 0 : return (TIFFNoEncode(tif, "strip"));
83 : }
84 :
85 0 : int _TIFFNoTileEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
86 : {
87 : (void)pp;
88 : (void)cc;
89 : (void)s;
90 0 : return (TIFFNoEncode(tif, "tile"));
91 : }
92 :
93 : /**
94 : * @brief Diagnose an unsupported decoder operation.
95 : * @ingroup libtiff_private_codecs
96 : *
97 : * Looks up the name and invokes handle diagnostics; no buffer access,
98 : * allocation or pixel I/O.
99 : *
100 : * @param[in] tif Non-NULL live handle; td_compression and tif_name are read
101 : * for the diagnostic and codec state is unchanged.
102 : * @param[in] method Borrowed method label used only during diagnostics.
103 : * @return Zero unconditionally.
104 : */
105 0 : static int TIFFNoDecode(TIFF *tif, const char *method)
106 : {
107 0 : const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
108 :
109 0 : if (c)
110 0 : TIFFErrorExtR(tif, tif->tif_name, "%s %s decoding is not implemented",
111 : c->name, method);
112 : else
113 0 : TIFFErrorExtR(tif, tif->tif_name,
114 : "Compression scheme %" PRIu16
115 : " %s decoding is not implemented",
116 0 : tif->tif_dir.td_compression, method);
117 0 : return (0);
118 : }
119 :
120 : /**
121 : * @brief Accept a no-op codec tag fixup.
122 : * @ingroup libtiff_private_codecs
123 : *
124 : * Ignores the handle; no state, allocation, I/O, diagnostics or ownership
125 : * effects.
126 : *
127 : * @param[in] tif Handle (ignored).
128 : * @return One unconditionally.
129 : */
130 74 : static int _TIFFNoFixupTags(TIFF *tif)
131 : {
132 : (void)tif;
133 74 : return (1);
134 : }
135 :
136 0 : int _TIFFNoRowDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
137 : {
138 : (void)pp;
139 : (void)cc;
140 : (void)s;
141 0 : return (TIFFNoDecode(tif, "scanline"));
142 : }
143 :
144 0 : int _TIFFNoStripDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
145 : {
146 : (void)pp;
147 : (void)cc;
148 : (void)s;
149 0 : return (TIFFNoDecode(tif, "strip"));
150 : }
151 :
152 0 : int _TIFFNoTileDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
153 : {
154 : (void)pp;
155 : (void)cc;
156 : (void)s;
157 0 : return (TIFFNoDecode(tif, "tile"));
158 : }
159 :
160 0 : int _TIFFNoSeek(TIFF *tif, uint32_t off)
161 : {
162 : (void)off;
163 0 : TIFFErrorExtR(tif, tif->tif_name,
164 : "Compression algorithm does not support random access");
165 0 : return (0);
166 : }
167 :
168 2088 : int _TIFFNoPreCode(TIFF *tif, uint16_t s)
169 : {
170 : (void)tif;
171 : (void)s;
172 2088 : return (1);
173 : }
174 :
175 : /**
176 : * @brief Accept a no-op codec setup or completion.
177 : * @ingroup libtiff_private_codecs
178 : *
179 : * Ignores the handle; no state, allocation, I/O, diagnostics or ownership
180 : * effects.
181 : *
182 : * @param[in] tif Handle (ignored).
183 : * @return One unconditionally.
184 : */
185 17585 : static int _TIFFtrue(TIFF *tif)
186 : {
187 : (void)tif;
188 17585 : return (1);
189 : }
190 : /**
191 : * @brief Perform no codec close or cleanup work.
192 : * @ingroup libtiff_private_codecs
193 : *
194 : * Ignores the handle; does not free codec state, flush, diagnose, allocate
195 : * or transfer ownership.
196 : *
197 : * @param[in] tif Handle (ignored).
198 : */
199 257978 : static void _TIFFvoid(TIFF *tif) { (void)tif; }
200 :
201 : /**
202 : * @brief Report an unknown codec compression ratio.
203 : * @ingroup libtiff_private_codecs
204 : *
205 : * No state, allocation, I/O, diagnostics or ownership effects.
206 : *
207 : * @param[in] tif Handle (ignored).
208 : * @return Zero for unknown.
209 : */
210 0 : static uint64_t _TIFFDefaultGetMaxCompressionRatio(TIFF *tif)
211 : {
212 : (void)tif;
213 0 : return 0; /* unknown */
214 : }
215 :
216 : /**
217 : * @brief Report the uncompressed ratio of one.
218 : * @ingroup libtiff_private_codecs
219 : *
220 : * No state, allocation, I/O, diagnostics or ownership effects.
221 : *
222 : * @param[in] tif Handle (ignored).
223 : * @return One unconditionally.
224 : */
225 0 : static uint64_t _TIFFGetMaxCompressionRatioOne(TIFF *tif)
226 : {
227 : (void)tif;
228 0 : return 1; /* no compression */
229 : }
230 :
231 287754 : void _TIFFSetDefaultCompressionState(TIFF *tif)
232 : {
233 287754 : tif->tif_fixuptags = _TIFFNoFixupTags;
234 287754 : tif->tif_decodestatus = TRUE;
235 287754 : tif->tif_setupdecode = _TIFFtrue;
236 287754 : tif->tif_predecode = _TIFFNoPreCode;
237 287754 : tif->tif_decoderow = _TIFFNoRowDecode;
238 287754 : tif->tif_decodestrip = _TIFFNoStripDecode;
239 287754 : tif->tif_decodetile = _TIFFNoTileDecode;
240 287754 : tif->tif_encodestatus = TRUE;
241 287754 : tif->tif_setupencode = _TIFFtrue;
242 287754 : tif->tif_preencode = _TIFFNoPreCode;
243 287754 : tif->tif_postencode = _TIFFtrue;
244 287754 : tif->tif_encoderow = _TIFFNoRowEncode;
245 287754 : tif->tif_encodestrip = _TIFFNoStripEncode;
246 287754 : tif->tif_encodetile = _TIFFNoTileEncode;
247 287754 : tif->tif_close = _TIFFvoid;
248 287754 : tif->tif_seek = _TIFFNoSeek;
249 287754 : tif->tif_cleanup = _TIFFvoid;
250 287754 : tif->tif_defstripsize = _TIFFDefaultStripSize;
251 287754 : tif->tif_deftilesize = _TIFFDefaultTileSize;
252 287754 : tif->tif_getmaxcompressionratio = _TIFFDefaultGetMaxCompressionRatio;
253 287754 : tif->tif_flags &= ~(TIFF_NOBITREV | TIFF_NOREADRAW | TIFF_UPSAMPLED);
254 287754 : }
255 :
256 177850 : int TIFFSetCompressionScheme(TIFF *tif, int scheme)
257 : {
258 177850 : const TIFFCodec *c = TIFFFindCODEC((uint16_t)scheme);
259 :
260 177861 : _TIFFSetDefaultCompressionState(tif);
261 177864 : if (scheme == COMPRESSION_NONE)
262 140211 : tif->tif_getmaxcompressionratio = _TIFFGetMaxCompressionRatioOne;
263 : /*
264 : * Don't treat an unknown compression scheme as an error.
265 : * This permits applications to open files with data that
266 : * the library does not have builtin support for, but which
267 : * may still be meaningful.
268 : */
269 177864 : return (c ? (*c->init)(tif, scheme) : 1);
270 : }
271 :
272 1 : uint64_t TIFFGetMaxCompressionRatio(TIFF *tif)
273 : {
274 1 : if (tif->tif_getmaxcompressionratio)
275 1 : return tif->tif_getmaxcompressionratio(tif);
276 0 : return 0;
277 : }
278 :
279 : /*
280 : * Other compression schemes may be registered. Registered
281 : * schemes can also override the builtin versions provided
282 : * by this library.
283 : */
284 : /**
285 : * @brief Link a dynamically registered codec allocation.
286 : * @ingroup libtiff_private_codecs
287 : *
288 : * The node, descriptor and copied name share one allocation released by
289 : * unregister. Global list operations require external serialisation.
290 : */
291 : typedef struct _codec
292 : {
293 : /** @brief Next older registered node, borrowed within the global list. */
294 : struct _codec *next;
295 : /** @brief Descriptor stored inside this node allocation. */
296 : TIFFCodec *info;
297 : } codec_t;
298 : /**
299 : * @brief Head the newest-first registered codec list.
300 : * @ingroup libtiff_private_codecs
301 : *
302 : * Process-global unsynchronised state; nodes are owned until unregister.
303 : */
304 : static codec_t *registeredCODECS = NULL;
305 :
306 274933 : const TIFFCodec *TIFFFindCODEC(uint16_t scheme)
307 : {
308 : const TIFFCodec *c;
309 : codec_t *cd;
310 :
311 671850 : for (cd = registeredCODECS; cd; cd = cd->next)
312 397819 : if (cd->info->scheme == scheme)
313 902 : return ((const TIFFCodec *)cd->info);
314 1420300 : for (c = _TIFFBuiltinCODECS; c->name; c++)
315 1420340 : if (c->scheme == scheme)
316 274071 : return (c);
317 0 : return NULL;
318 : }
319 :
320 1396 : TIFFCodec *TIFFRegisterCODEC(uint16_t scheme, const char *name,
321 : TIFFInitMethod init)
322 : {
323 1396 : size_t name_len = strlen(name) + 1;
324 1396 : codec_t *cd = (codec_t *)_TIFFmallocExt(
325 1396 : NULL, (tmsize_t)(sizeof(codec_t) + sizeof(TIFFCodec) + name_len));
326 :
327 1396 : if (cd != NULL)
328 : {
329 : char *codec_name;
330 1396 : cd->info = (TIFFCodec *)((uint8_t *)cd + sizeof(codec_t));
331 1396 : codec_name = (char *)((uint8_t *)cd->info + sizeof(TIFFCodec));
332 1396 : _TIFFmemcpy(codec_name, name, (tmsize_t)name_len);
333 1396 : cd->info->name = codec_name;
334 1396 : cd->info->scheme = scheme;
335 1396 : cd->info->init = init;
336 1396 : cd->next = registeredCODECS;
337 1396 : registeredCODECS = cd;
338 : }
339 : else
340 : {
341 0 : TIFFErrorExt(0, "TIFFRegisterCODEC",
342 : "No space to register compression scheme %s", name);
343 0 : return NULL;
344 : }
345 1396 : return (cd->info);
346 : }
347 :
348 1028 : void TIFFUnRegisterCODEC(TIFFCodec *c)
349 : {
350 : codec_t *cd;
351 : codec_t **pcd;
352 :
353 1542 : for (pcd = ®isteredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
354 1542 : if (cd->info == c)
355 : {
356 1028 : *pcd = cd->next;
357 1028 : _TIFFfreeExt(NULL, cd);
358 1028 : return;
359 : }
360 0 : TIFFErrorExt(0, "TIFFUnRegisterCODEC",
361 : "Cannot remove compression scheme %s; not registered",
362 : c->name);
363 : }
364 :
365 : /************************************************************************/
366 : /* TIFFGetConfiguredCODECs() */
367 : /************************************************************************/
368 :
369 : /* The public contract is documented with the declaration in tiffio.h. */
370 :
371 3861 : TIFFCodec *TIFFGetConfiguredCODECs(void)
372 : {
373 3861 : int i = 1;
374 : codec_t *cd;
375 : const TIFFCodec *c;
376 3861 : TIFFCodec *codecs = NULL;
377 : TIFFCodec *new_codecs;
378 :
379 4383 : for (cd = registeredCODECS; cd; cd = cd->next)
380 : {
381 522 : new_codecs = (TIFFCodec *)_TIFFreallocExt(
382 522 : NULL, codecs, (tmsize_t)((size_t)i * sizeof(TIFFCodec)));
383 522 : if (!new_codecs)
384 : {
385 0 : _TIFFfreeExt(NULL, codecs);
386 0 : return NULL;
387 : }
388 522 : codecs = new_codecs;
389 522 : _TIFFmemcpy(codecs + i - 1, cd->info, sizeof(TIFFCodec));
390 522 : i++;
391 : }
392 84942 : for (c = _TIFFBuiltinCODECS; c->name; c++)
393 : {
394 81081 : if (TIFFIsCODECConfigured(c->scheme))
395 : {
396 77220 : new_codecs = (TIFFCodec *)_TIFFreallocExt(
397 77220 : NULL, codecs, (tmsize_t)((size_t)i * sizeof(TIFFCodec)));
398 77220 : if (!new_codecs)
399 : {
400 0 : _TIFFfreeExt(NULL, codecs);
401 0 : return NULL;
402 : }
403 77220 : codecs = new_codecs;
404 77220 : _TIFFmemcpy(codecs + i - 1, c, sizeof(TIFFCodec));
405 77220 : i++;
406 : }
407 : }
408 :
409 3861 : new_codecs = (TIFFCodec *)_TIFFreallocExt(
410 3861 : NULL, codecs, (tmsize_t)((size_t)i * sizeof(TIFFCodec)));
411 3861 : if (!new_codecs)
412 : {
413 0 : _TIFFfreeExt(NULL, codecs);
414 0 : return NULL;
415 : }
416 3861 : codecs = new_codecs;
417 3861 : _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
418 :
419 3861 : return codecs;
420 : }
|