LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_compress.c
Go to the documentation of this file.
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 */
36#include "tiffiop.h"
37
50static int TIFFNoEncode(TIFF *tif, const char *method)
51{
53
54 if (c)
55 {
56 TIFFErrorExtR(tif, tif->tif_name, "%s %s encoding is not implemented",
57 c->name, method);
58 }
59 else
60 {
61 TIFFErrorExtR(tif, tif->tif_name,
62 "Compression scheme %" PRIu16
63 " %s encoding is not implemented",
64 tif->tif_dir.td_compression, method);
65 }
66 return (-1);
67}
68
69int _TIFFNoRowEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
70{
71 (void)pp;
72 (void)cc;
73 (void)s;
74 return (TIFFNoEncode(tif, "scanline"));
75}
76
77int _TIFFNoStripEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
78{
79 (void)pp;
80 (void)cc;
81 (void)s;
82 return (TIFFNoEncode(tif, "strip"));
83}
84
85int _TIFFNoTileEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
86{
87 (void)pp;
88 (void)cc;
89 (void)s;
90 return (TIFFNoEncode(tif, "tile"));
91}
92
105static int TIFFNoDecode(TIFF *tif, const char *method)
106{
108
109 if (c)
110 TIFFErrorExtR(tif, tif->tif_name, "%s %s decoding is not implemented",
111 c->name, method);
112 else
113 TIFFErrorExtR(tif, tif->tif_name,
114 "Compression scheme %" PRIu16
115 " %s decoding is not implemented",
116 tif->tif_dir.td_compression, method);
117 return (0);
118}
119
130static int _TIFFNoFixupTags(TIFF *tif)
131{
132 (void)tif;
133 return (1);
134}
135
136int _TIFFNoRowDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
137{
138 (void)pp;
139 (void)cc;
140 (void)s;
141 return (TIFFNoDecode(tif, "scanline"));
142}
143
144int _TIFFNoStripDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
145{
146 (void)pp;
147 (void)cc;
148 (void)s;
149 return (TIFFNoDecode(tif, "strip"));
150}
151
152int _TIFFNoTileDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
153{
154 (void)pp;
155 (void)cc;
156 (void)s;
157 return (TIFFNoDecode(tif, "tile"));
158}
159
160int _TIFFNoSeek(TIFF *tif, uint32_t off)
161{
162 (void)off;
163 TIFFErrorExtR(tif, tif->tif_name,
164 "Compression algorithm does not support random access");
165 return (0);
166}
167
168int _TIFFNoPreCode(TIFF *tif, uint16_t s)
169{
170 (void)tif;
171 (void)s;
172 return (1);
173}
174
185static int _TIFFtrue(TIFF *tif)
186{
187 (void)tif;
188 return (1);
189}
199static void _TIFFvoid(TIFF *tif) { (void)tif; }
200
211{
212 (void)tif;
213 return 0; /* unknown */
214}
215
226{
227 (void)tif;
228 return 1; /* no compression */
229}
230
255
256int TIFFSetCompressionScheme(TIFF *tif, int scheme)
257{
258 const TIFFCodec *c = TIFFFindCODEC((uint16_t)scheme);
259
261 if (scheme == COMPRESSION_NONE)
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 return (c ? (*c->init)(tif, scheme) : 1);
270}
271
273{
275 return tif->tif_getmaxcompressionratio(tif);
276 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 */
291typedef struct _codec
292{
294 struct _codec *next;
305
306const TIFFCodec *TIFFFindCODEC(uint16_t scheme)
307{
308 const TIFFCodec *c;
309 codec_t *cd;
310
311 for (cd = registeredCODECS; cd; cd = cd->next)
312 if (cd->info->scheme == scheme)
313 return ((const TIFFCodec *)cd->info);
314 for (c = _TIFFBuiltinCODECS; c->name; c++)
315 if (c->scheme == scheme)
316 return (c);
317 return NULL;
318}
319
320TIFFCodec *TIFFRegisterCODEC(uint16_t scheme, const char *name,
321 TIFFInitMethod init)
322{
323 size_t name_len = strlen(name) + 1;
325 NULL, (tmsize_t)(sizeof(codec_t) + sizeof(TIFFCodec) + name_len));
326
327 if (cd != NULL)
328 {
329 char *codec_name;
330 cd->info = (TIFFCodec *)((uint8_t *)cd + sizeof(codec_t));
331 codec_name = (char *)((uint8_t *)cd->info + sizeof(TIFFCodec));
332 _TIFFmemcpy(codec_name, name, (tmsize_t)name_len);
333 cd->info->name = codec_name;
334 cd->info->scheme = scheme;
335 cd->info->init = init;
337 registeredCODECS = cd;
338 }
339 else
340 {
341 TIFFErrorExt(0, "TIFFRegisterCODEC",
342 "No space to register compression scheme %s", name);
343 return NULL;
344 }
345 return (cd->info);
346}
347
349{
350 codec_t *cd;
351 codec_t **pcd;
352
353 for (pcd = &registeredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
354 if (cd->info == c)
355 {
356 *pcd = cd->next;
357 _TIFFfreeExt(NULL, cd);
358 return;
359 }
360 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
372{
373 int i = 1;
374 codec_t *cd;
375 const TIFFCodec *c;
376 TIFFCodec *codecs = NULL;
377 TIFFCodec *new_codecs;
378
379 for (cd = registeredCODECS; cd; cd = cd->next)
380 {
381 new_codecs = (TIFFCodec *)_TIFFreallocExt(
382 NULL, codecs, (tmsize_t)((size_t)i * sizeof(TIFFCodec)));
383 if (!new_codecs)
384 {
385 _TIFFfreeExt(NULL, codecs);
386 return NULL;
387 }
388 codecs = new_codecs;
389 _TIFFmemcpy(codecs + i - 1, cd->info, sizeof(TIFFCodec));
390 i++;
391 }
392 for (c = _TIFFBuiltinCODECS; c->name; c++)
393 {
395 {
396 new_codecs = (TIFFCodec *)_TIFFreallocExt(
397 NULL, codecs, (tmsize_t)((size_t)i * sizeof(TIFFCodec)));
398 if (!new_codecs)
399 {
400 _TIFFfreeExt(NULL, codecs);
401 return NULL;
402 }
403 codecs = new_codecs;
404 _TIFFmemcpy(codecs + i - 1, c, sizeof(TIFFCodec));
405 i++;
406 }
407 }
408
409 new_codecs = (TIFFCodec *)_TIFFreallocExt(
410 NULL, codecs, (tmsize_t)((size_t)i * sizeof(TIFFCodec)));
411 if (!new_codecs)
412 {
413 _TIFFfreeExt(NULL, codecs);
414 return NULL;
415 }
416 codecs = new_codecs;
417 _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
418
419 return codecs;
420}
static void _TIFFvoid(TIFF *tif)
Perform no codec close or cleanup work.
int TIFFSetCompressionScheme(TIFF *tif, int scheme)
Reset methods and install the selected compression implementation.
int _TIFFNoTileEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
Report unsupported codec encoding.
static int TIFFNoEncode(TIFF *tif, const char *method)
Diagnose an unsupported encoder operation.
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
static int _TIFFtrue(TIFF *tif)
Accept a no-op codec setup or completion.
int _TIFFNoTileDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
Report unsupported codec decoding.
struct _codec codec_t
Link a dynamically registered codec allocation.
static codec_t * registeredCODECS
Head the newest-first registered codec list.
static uint64_t _TIFFDefaultGetMaxCompressionRatio(TIFF *tif)
Report an unknown codec compression ratio.
static int _TIFFNoFixupTags(TIFF *tif)
Accept a no-op codec tag fixup.
int _TIFFNoRowDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
Report unsupported codec decoding.
int _TIFFNoRowEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
Report unsupported codec encoding.
static uint64_t _TIFFGetMaxCompressionRatioOne(TIFF *tif)
Report the uncompressed ratio of one.
int _TIFFNoStripEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
Report unsupported codec encoding.
static int TIFFNoDecode(TIFF *tif, const char *method)
Diagnose an unsupported decoder operation.
int _TIFFNoStripDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
Report unsupported codec decoding.
const TIFFCodec _TIFFBuiltinCODECS[]
Describe built-in compression names and initialisers.
Definition tif_codec.c:91
int _TIFFNoPreCode(TIFF *tif, uint16_t s)
Accept a no-op codec preparation.
int _TIFFNoSeek(TIFF *tif, uint32_t off)
Report unsupported codec seeking.
void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
Supply default tile dimensions and round them to multiples of sixteen.
Definition tif_tile.c:326
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
Choose a default strip height near STRIP_SIZE_DEFAULT bytes.
Definition tif_strip.c:263
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Resize a handle-aware block or release it for a nonpositive size.
Definition tif_open.c:257
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
#define TIFF_UPSAMPLED
Mark codec output that has already been upsampled.
Definition tiffiop.h:481
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define TIFF_NOREADRAW
Suppress the normal raw-data read path.
Definition tiffiop.h:496
#define TIFF_NOBITREV
Suppress automatic bit reversal.
Definition tiffiop.h:446
TIFFCodec * TIFFRegisterCODEC(uint16_t scheme, const char *name, TIFFInitMethod init)
Register a codec descriptor ahead of built-in and older entries.
const TIFFCodec * TIFFFindCODEC(uint16_t scheme)
Find the highest-priority descriptor for a compression scheme.
int(* TIFFInitMethod)(TIFF *tif, int scheme)
Install compression methods and private state on a handle.
Definition tiffio.h:697
int TIFFIsCODECConfigured(uint16_t scheme)
Check whether the selected descriptor has a configured initialiser.
Definition tif_codec.c:171
TIFFCodec * TIFFGetConfiguredCODECs(void)
Allocate a terminated snapshot of registered and configured codec descriptors.
void TIFFUnRegisterCODEC(TIFFCodec *c)
Remove and free an exact registered codec descriptor.
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:68
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
uint64_t TIFFGetMaxCompressionRatio(TIFF *tif)
Query the codec's allocation-recovery compression-ratio heuristic.
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
#define COMPRESSION_NONE
Dump mode.
Definition tiff.h:302
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
Describe a named compression scheme and its initialiser.
Definition tiffio.h:706
const char * name
Codec name; storage lifetime follows the API supplying this descriptor.
Definition tiffio.h:711
TIFFInitMethod init
Callback that installs the codec on a TIFF handle.
Definition tiffio.h:715
uint16_t scheme
Compression identifier, such as COMPRESSION_LZW.
Definition tiffio.h:713
uint16_t td_compression
COMPRESSION_* codec identifier.
Definition tif_dir.h:172
Link a dynamically registered codec allocation.
TIFFCodec * info
Descriptor stored inside this node allocation.
struct _codec * next
Next older registered node, borrowed within the global list.
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFTileMethod tif_deftilesize
Codec callback computing or constraining default tile dimensions.
Definition tiffiop.h:667
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
TIFFSeekMethod tif_seek
Codec callback advancing within a strip.
Definition tiffiop.h:659
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
Definition tiffiop.h:653
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
Definition tiffiop.h:639
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
Definition tiffiop.h:626
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
Definition tiffiop.h:630
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
Definition tiffiop.h:670
TIFFStripMethod tif_defstripsize
Codec callback computing or constraining a default strip size.
Definition tiffiop.h:663
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
Definition tiffiop.h:394
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
Definition tiffiop.h:645
TIFFVoidMethod tif_close
Codec close callback; distinct from the client I/O close callback.
Definition tiffiop.h:657
int tif_decodestatus
Nonzero unless the selected scheme is a known but unconfigured codec; not a setup result.
Definition tiffiop.h:624
int tif_encodestatus
Nonzero unless the selected scheme is a known but unconfigured codec; never read.
Definition tiffiop.h:637
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
Definition tiffiop.h:661
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Definition tiffiop.h:651
Declare private library state, callbacks, and implementation helpers.