cscript fiddling.
[openjpeg.git] / thirdparty / libtiff / tif_compress.c
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 {
37         const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
38
39         if (c) { 
40                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
41                              "%s %s encoding is not implemented",
42                              c->name, method);
43         } else { 
44                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
45                         "Compression scheme %u %s encoding is not implemented",
46                              tif->tif_dir.td_compression, method);
47         }
48         return (-1);
49 }
50
51 int
52 _TIFFNoRowEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
53 {
54         (void) pp; (void) cc; (void) s;
55         return (TIFFNoEncode(tif, "scanline"));
56 }
57
58 int
59 _TIFFNoStripEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
60 {
61         (void) pp; (void) cc; (void) s;
62         return (TIFFNoEncode(tif, "strip"));
63 }
64
65 int
66 _TIFFNoTileEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
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 {
75         const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
76
77         if (c)
78                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
79                              "%s %s decoding is not implemented",
80                              c->name, method);
81         else
82                 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
83                              "Compression scheme %u %s decoding is not implemented",
84                              tif->tif_dir.td_compression, method);
85         return (-1);
86 }
87
88 int
89 _TIFFNoRowDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
90 {
91         (void) pp; (void) cc; (void) s;
92         return (TIFFNoDecode(tif, "scanline"));
93 }
94
95 int
96 _TIFFNoStripDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
97 {
98         (void) pp; (void) cc; (void) s;
99         return (TIFFNoDecode(tif, "strip"));
100 }
101
102 int
103 _TIFFNoTileDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
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;
113         TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
114                      "Compression algorithm does not support random access");
115         return (0);
116 }
117
118 int
119 _TIFFNoPreCode(TIFF* tif, tsample_t s)
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
129 _TIFFSetDefaultCompressionState(TIFF* tif)
130 {
131         tif->tif_decodestatus = TRUE;
132         tif->tif_setupdecode = _TIFFtrue;
133         tif->tif_predecode = _TIFFNoPreCode;
134         tif->tif_decoderow = _TIFFNoRowDecode;
135         tif->tif_decodestrip = _TIFFNoStripDecode;
136         tif->tif_decodetile = _TIFFNoTileDecode;
137         tif->tif_encodestatus = TRUE;
138         tif->tif_setupencode = _TIFFtrue;
139         tif->tif_preencode = _TIFFNoPreCode;
140         tif->tif_postencode = _TIFFtrue;
141         tif->tif_encoderow = _TIFFNoRowEncode;
142         tif->tif_encodestrip = _TIFFNoStripEncode;
143         tif->tif_encodetile = _TIFFNoTileEncode;
144         tif->tif_close = _TIFFvoid;
145         tif->tif_seek = _TIFFNoSeek;
146         tif->tif_cleanup = _TIFFvoid;
147         tif->tif_defstripsize = _TIFFDefaultStripSize;
148         tif->tif_deftilesize = _TIFFDefaultTileSize;
149         tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
150 }
151
152 int
153 TIFFSetCompressionScheme(TIFF* tif, int scheme)
154 {
155         const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
156
157         _TIFFSetDefaultCompressionState(tif);
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;
174         TIFFCodec*      info;
175 } codec_t;
176 static  codec_t* registeredCODECS = NULL;
177
178 const TIFFCodec*
179 TIFFFindCODEC(uint16 scheme)
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
217 TIFFUnRegisterCODEC(TIFFCodec* c)
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
236 /**
237  * Get list of configured codecs, both built-in and registered by user.
238  * Caller is responsible to free this structure.
239  * 
240  * @return returns array of TIFFCodec records (the last record should be NULL)
241  * or NULL if function failed.
242  */
243
244 TIFFCodec*
245 TIFFGetConfiguredCODECs()
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  */