Bsoft  1.9
Bernard's software package
tif_compress.c
Go to the documentation of this file.
1 /* $Id: tif_compress.c,v 1.13.2.1 2010-06-08 18:50:41 bfriesen 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, const char* method)
36 {
38 
39  if (c) {
41  "%s %s encoding is not implemented",
42  c->name, method);
43  } else {
45  "Compression scheme %u %s encoding is not implemented",
46  tif->tif_dir.td_compression, method);
47  }
48  return (-1);
49 }
50 
51 int
53 {
54  (void) pp; (void) cc; (void) s;
55  return (TIFFNoEncode(tif, "scanline"));
56 }
57 
58 int
60 {
61  (void) pp; (void) cc; (void) s;
62  return (TIFFNoEncode(tif, "strip"));
63 }
64 
65 int
67 {
68  (void) pp; (void) cc; (void) s;
69  return (TIFFNoEncode(tif, "tile"));
70 }
71 
72 static int
73 TIFFNoDecode(TIFF* tif, const char* method)
74 {
76 
77  if (c)
79  "%s %s decoding is not implemented",
80  c->name, method);
81  else
83  "Compression scheme %u %s decoding is not implemented",
84  tif->tif_dir.td_compression, method);
85  return (-1);
86 }
87 
88 int
90 {
91  (void) pp; (void) cc; (void) s;
92  return (TIFFNoDecode(tif, "scanline"));
93 }
94 
95 int
97 {
98  (void) pp; (void) cc; (void) s;
99  return (TIFFNoDecode(tif, "strip"));
100 }
101 
102 int
104 {
105  (void) pp; (void) cc; (void) s;
106  return (TIFFNoDecode(tif, "tile"));
107 }
108 
109 int
110 _TIFFNoSeek(TIFF* tif, uint32 off)
111 {
112  (void) off;
114  "Compression algorithm does not support random access");
115  return (0);
116 }
117 
118 int
120 {
121  (void) tif; (void) s;
122  return (1);
123 }
124 
125 static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
126 static void _TIFFvoid(TIFF* tif) { (void) tif; }
127 
128 void
130 {
131  tif->tif_decodestatus = TRUE;
132  tif->tif_setupdecode = _TIFFtrue;
137  tif->tif_encodestatus = TRUE;
138  tif->tif_setupencode = _TIFFtrue;
140  tif->tif_postencode = _TIFFtrue;
144  tif->tif_close = _TIFFvoid;
145  tif->tif_seek = _TIFFNoSeek;
146  tif->tif_cleanup = _TIFFvoid;
150 }
151 
152 int
154 {
155  const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
156 
158  /*
159  * Don't treat an unknown compression scheme as an error.
160  * This permits applications to open files with data that
161  * the library does not have builtin support for, but which
162  * may still be meaningful.
163  */
164  return (c ? (*c->init)(tif, scheme) : 1);
165 }
166 
167 /*
168  * Other compression schemes may be registered. Registered
169  * schemes can also override the builtin versions provided
170  * by this library.
171  */
172 typedef struct _codec {
173  struct _codec* next;
175 } codec_t;
176 static codec_t* registeredCODECS = NULL;
177 
178 const TIFFCodec*
180 {
181  const TIFFCodec* c;
182  codec_t* cd;
183 
184  for (cd = registeredCODECS; cd; cd = cd->next)
185  if (cd->info->scheme == scheme)
186  return ((const TIFFCodec*) cd->info);
187  for (c = _TIFFBuiltinCODECS; c->name; c++)
188  if (c->scheme == scheme)
189  return (c);
190  return ((const TIFFCodec*) 0);
191 }
192 
193 TIFFCodec*
194 TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
195 {
196  codec_t* cd = (codec_t*)
197  _TIFFmalloc(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1);
198 
199  if (cd != NULL) {
200  cd->info = (TIFFCodec*) ((tidata_t) cd + sizeof (codec_t));
201  cd->info->name = (char*)
202  ((tidata_t) cd->info + sizeof (TIFFCodec));
203  strcpy(cd->info->name, name);
204  cd->info->scheme = scheme;
205  cd->info->init = init;
206  cd->next = registeredCODECS;
207  registeredCODECS = cd;
208  } else {
209  TIFFErrorExt(0, "TIFFRegisterCODEC",
210  "No space to register compression scheme %s", name);
211  return NULL;
212  }
213  return (cd->info);
214 }
215 
216 void
218 {
219  codec_t* cd;
220  codec_t** pcd;
221 
222  for (pcd = &registeredCODECS; (cd = *pcd); pcd = &cd->next)
223  if (cd->info == c) {
224  *pcd = cd->next;
225  _TIFFfree(cd);
226  return;
227  }
228  TIFFErrorExt(0, "TIFFUnRegisterCODEC",
229  "Cannot remove compression scheme %s; not registered", c->name);
230 }
231 
232 /************************************************************************/
233 /* TIFFGetConfisuredCODECs() */
234 /************************************************************************/
235 
244 TIFFCodec*
246 {
247  int i = 1;
248  codec_t *cd;
249  const TIFFCodec *c;
250  TIFFCodec *codecs = NULL, *new_codecs;
251 
252  for (cd = registeredCODECS; cd; cd = cd->next) {
253  new_codecs = (TIFFCodec *)
254  _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
255  if (!new_codecs) {
256  _TIFFfree (codecs);
257  return NULL;
258  }
259  codecs = new_codecs;
260  _TIFFmemcpy(codecs + i - 1, cd, sizeof(TIFFCodec));
261  i++;
262  }
263  for (c = _TIFFBuiltinCODECS; c->name; c++) {
264  if (TIFFIsCODECConfigured(c->scheme)) {
265  new_codecs = (TIFFCodec *)
266  _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
267  if (!new_codecs) {
268  _TIFFfree (codecs);
269  return NULL;
270  }
271  codecs = new_codecs;
272  _TIFFmemcpy(codecs + i - 1, (const tdata_t)c, sizeof(TIFFCodec));
273  i++;
274  }
275  }
276 
277  new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
278  if (!new_codecs) {
279  _TIFFfree (codecs);
280  return NULL;
281  }
282  codecs = new_codecs;
283  _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
284 
285  return codecs;
286 }
287 
288 /* vim: set ts=8 sts=8 sw=8 noet: */
289 /*
290  * Local Variables:
291  * mode: c
292  * c-basic-offset: 8
293  * fill-column: 78
294  * End:
295  */
#define TRUE
Definition: twiddle.c:33
int32 tsize_t
Definition: tiffio.h:67
uint16 scheme
Definition: tiffio.h:242
uint16 tsample_t
Definition: tiffio.h:63
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:159
void * tdata_t
Definition: tiffio.h:68
int _TIFFNoStripEncode(TIFF *tif, tidata_t pp, tsize_t cc, tsample_t s)
Definition: tif_compress.c:59
TIFFPreMethod tif_predecode
Definition: tiffiop.h:154
TIFFCodec _TIFFBuiltinCODECS[]
Definition: tif_codec.c:79
TIFFCodeMethod tif_encodestrip
Definition: tiffiop.h:162
TIFFInitMethod init
Definition: tiffio.h:243
#define TIFF_NOREADRAW
Definition: tiffiop.h:124
TIFFCodeMethod tif_encoderow
Definition: tiffiop.h:160
TIFFVoidMethod tif_close
Definition: tiffiop.h:165
int(* TIFFInitMethod)(TIFF *, int)
Definition: tiffio.h:239
int _TIFFNoRowEncode(TIFF *tif, tidata_t pp, tsize_t cc, tsample_t s)
Definition: tif_compress.c:52
thandle_t tif_clientdata
Definition: tiffiop.h:185
int _TIFFNoSeek(TIFF *tif, uint32 off)
Definition: tif_compress.c:110
int _TIFFNoTileEncode(TIFF *tif, tidata_t pp, tsize_t cc, tsample_t s)
Definition: tif_compress.c:66
char * tif_name
Definition: tiffiop.h:103
Definition: tiffiop.h:102
void _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
Definition: tif_acorn.c:485
char * name
Definition: tiffio.h:241
TIFFCodec * TIFFRegisterCODEC(uint16 scheme, const char *name, TIFFInitMethod init)
Definition: tif_compress.c:194
int _TIFFNoStripDecode(TIFF *tif, tidata_t pp, tsize_t cc, tsample_t s)
Definition: tif_compress.c:96
uint32 tif_flags
Definition: tiffiop.h:106
int tif_encodestatus
Definition: tiffiop.h:156
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:161
TIFFCodeMethod tif_encodetile
Definition: tiffiop.h:164
TIFFSeekMethod tif_seek
Definition: tiffiop.h:166
int _TIFFNoPreCode(TIFF *tif, tsample_t s)
Definition: tif_compress.c:119
TIFFTileMethod tif_deftilesize
Definition: tiffiop.h:169
TIFFPreMethod tif_preencode
Definition: tiffiop.h:157
uint32 _TIFFDefaultStripSize(TIFF *tif, uint32 s)
Definition: tif_strip.c:204
unsigned short uint16
Definition: tiff.h:71
TIFFBoolMethod tif_postencode
Definition: tiffiop.h:158
int _TIFFNoTileDecode(TIFF *tif, tidata_t pp, tsize_t cc, tsample_t s)
Definition: tif_compress.c:103
void * _TIFFrealloc(tdata_t p, tsize_t s)
Definition: tif_acorn.c:473
int _TIFFNoRowDecode(TIFF *tif, tidata_t pp, tsize_t cc, tsample_t s)
Definition: tif_compress.c:89
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Definition: tif_error.c:63
TIFFCodec * TIFFGetConfiguredCODECs()
Definition: tif_compress.c:245
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:163
const TIFFCodec * TIFFFindCODEC(uint16 scheme)
Definition: tif_compress.c:179
#define TIFF_NOBITREV
Definition: tiffiop.h:114
TIFFVoidMethod tif_cleanup
Definition: tiffiop.h:167
void _TIFFmemset(tdata_t p, int v, tsize_t c)
Definition: tif_acorn.c:479
void _TIFFSetDefaultCompressionState(TIFF *tif)
Definition: tif_compress.c:129
void _TIFFDefaultTileSize(TIFF *tif, uint32 *tw, uint32 *th)
Definition: tif_tile.c:259
tidataval_t * tidata_t
Definition: tiffiop.h:91
void TIFFUnRegisterCODEC(TIFFCodec *c)
Definition: tif_compress.c:217
int tif_decodestatus
Definition: tiffiop.h:152
TIFFDirectory tif_dir
Definition: tiffiop.h:133
int TIFFSetCompressionScheme(TIFF *tif, int scheme)
Definition: tif_compress.c:153
struct _codec * next
Definition: tif_compress.c:173
uint16 td_compression
Definition: tif_dir.h:46
int TIFFIsCODECConfigured(uint16 scheme)
Definition: tif_codec.c:138
void * _TIFFmalloc(tsize_t s)
Definition: tif_acorn.c:461
struct _codec codec_t
void _TIFFfree(tdata_t p)
Definition: tif_acorn.c:467
TIFFBoolMethod tif_setupencode
Definition: tiffiop.h:155
TIFFStripMethod tif_defstripsize
Definition: tiffiop.h:168
TIFFCodec * info
Definition: tif_compress.c:174
TIFFBoolMethod tif_setupdecode
Definition: tiffiop.h:153