| 1 | /* $Header$ */ |
| 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, char* method) |
| 36 | { |
| 37 | const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression); |
| 38 | |
| 39 | if (c) |
| 40 | TIFFError(tif->tif_name, "%s %s encoding is not implemented", |
| 41 | c->name, method); |
| 42 | else |
| 43 | TIFFError(tif->tif_name, |
| 44 | "Compression scheme %u %s encoding is not implemented", |
| 45 | tif->tif_dir.td_compression, method); |
| 46 | return (-1); |
| 47 | } |
| 48 | |
| 49 | int |
| 50 | _TIFFNoRowEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
| 51 | { |
| 52 | (void) pp; (void) cc; (void) s; |
| 53 | return (TIFFNoEncode(tif, "scanline")); |
| 54 | } |
| 55 | |
| 56 | int |
| 57 | _TIFFNoStripEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
| 58 | { |
| 59 | (void) pp; (void) cc; (void) s; |
| 60 | return (TIFFNoEncode(tif, "strip")); |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | _TIFFNoTileEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
| 65 | { |
| 66 | (void) pp; (void) cc; (void) s; |
| 67 | return (TIFFNoEncode(tif, "tile")); |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | TIFFNoDecode(TIFF* tif, char* method) |
| 72 | { |
| 73 | const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression); |
| 74 | |
| 75 | if (c) |
| 76 | TIFFError(tif->tif_name, "%s %s decoding is not implemented", |
| 77 | c->name, method); |
| 78 | else |
| 79 | TIFFError(tif->tif_name, |
| 80 | "Compression scheme %u %s decoding is not implemented", |
| 81 | tif->tif_dir.td_compression, method); |
| 82 | return (-1); |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | _TIFFNoRowDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
| 87 | { |
| 88 | (void) pp; (void) cc; (void) s; |
| 89 | return (TIFFNoDecode(tif, "scanline")); |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | _TIFFNoStripDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
| 94 | { |
| 95 | (void) pp; (void) cc; (void) s; |
| 96 | return (TIFFNoDecode(tif, "strip")); |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | _TIFFNoTileDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) |
| 101 | { |
| 102 | (void) pp; (void) cc; (void) s; |
| 103 | return (TIFFNoDecode(tif, "tile")); |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | _TIFFNoSeek(TIFF* tif, uint32 off) |
| 108 | { |
| 109 | (void) off; |
| 110 | TIFFError(tif->tif_name, |
| 111 | "Compression algorithm does not support random access"); |
| 112 | return (0); |
| 113 | } |
| 114 | |
| 115 | int |
| 116 | _TIFFNoPreCode(TIFF* tif, tsample_t s) |
| 117 | { |
| 118 | (void) tif; (void) s; |
| 119 | return (1); |
| 120 | } |
| 121 | |
| 122 | static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); } |
| 123 | static void _TIFFvoid(TIFF* tif) { (void) tif; } |
| 124 | |
| 125 | void LINKAGEMODE |
| 126 | _TIFFSetDefaultCompressionState(TIFF* tif) |
| 127 | { |
| 128 | tif->tif_setupdecode = _TIFFtrue; |
| 129 | tif->tif_predecode = _TIFFNoPreCode; |
| 130 | tif->tif_decoderow = _TIFFNoRowDecode; |
| 131 | tif->tif_decodestrip = _TIFFNoStripDecode; |
| 132 | tif->tif_decodetile = _TIFFNoTileDecode; |
| 133 | tif->tif_setupencode = _TIFFtrue; |
| 134 | tif->tif_preencode = _TIFFNoPreCode; |
| 135 | tif->tif_postencode = _TIFFtrue; |
| 136 | tif->tif_encoderow = _TIFFNoRowEncode; |
| 137 | tif->tif_encodestrip = _TIFFNoStripEncode; |
| 138 | tif->tif_encodetile = _TIFFNoTileEncode; |
| 139 | tif->tif_close = _TIFFvoid; |
| 140 | tif->tif_seek = _TIFFNoSeek; |
| 141 | tif->tif_cleanup = _TIFFvoid; |
| 142 | tif->tif_defstripsize = _TIFFDefaultStripSize; |
| 143 | tif->tif_deftilesize = _TIFFDefaultTileSize; |
| 144 | tif->tif_flags &= ~TIFF_NOBITREV; |
| 145 | } |
| 146 | |
| 147 | int |
| 148 | TIFFSetCompressionScheme(TIFF* tif, int scheme) |
| 149 | { |
| 150 | const TIFFCodec *c = TIFFFindCODEC(scheme); |
| 151 | |
| 152 | _TIFFSetDefaultCompressionState(tif); |
| 153 | /* |
| 154 | * Don't treat an unknown compression scheme as an error. |
| 155 | * This permits applications to open files with data that |
| 156 | * the library does not have builtin support for, but which |
| 157 | * may still be meaningful. |
| 158 | */ |
| 159 | return (c ? (*c->init)(tif, scheme) : 1); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Other compression schemes may be registered. Registered |
| 164 | * schemes can also override the builtin versions provided |
| 165 | * by this library. |
| 166 | */ |
| 167 | typedef struct _codec { |
| 168 | struct _codec* next; |
| 169 | TIFFCodec* info; |
| 170 | } codec_t; |
| 171 | static codec_t* registeredCODECS = NULL; |
| 172 | |
| 173 | const TIFFCodec* |
| 174 | TIFFFindCODEC(uint16 scheme) |
| 175 | { |
| 176 | const TIFFCodec* c; |
| 177 | codec_t* cd; |
| 178 | |
| 179 | for (cd = registeredCODECS; cd; cd = cd->next) |
| 180 | if (cd->info->scheme == scheme) |
| 181 | return ((const TIFFCodec*) cd->info); |
| 182 | for (c = _TIFFBuiltinCODECS; c->name; c++) |
| 183 | if (c->scheme == scheme) |
| 184 | return (c); |
| 185 | return ((const TIFFCodec*) 0); |
| 186 | } |
| 187 | |
| 188 | TIFFCodec* |
| 189 | TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init) |
| 190 | { |
| 191 | codec_t* cd = (codec_t*) |
| 192 | _TIFFmalloc(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1); |
| 193 | |
| 194 | if (cd != NULL) { |
| 195 | cd->info = (TIFFCodec*) ((tidata_t) cd + sizeof (codec_t)); |
| 196 | cd->info->name = (char*) |
| 197 | ((tidata_t) cd->info + sizeof (TIFFCodec)); |
| 198 | strcpy(cd->info->name, name); |
| 199 | cd->info->scheme = scheme; |
| 200 | cd->info->init = init; |
| 201 | cd->next = registeredCODECS; |
| 202 | registeredCODECS = cd; |
| 203 | } else |
| 204 | TIFFError("TIFFRegisterCODEC", |
| 205 | "No space to register compression scheme %s", name); |
| 206 | return (cd->info); |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | TIFFUnRegisterCODEC(TIFFCodec* c) |
| 211 | { |
| 212 | codec_t* cd; |
| 213 | codec_t** pcd; |
| 214 | |
| 215 | for (pcd = ®isteredCODECS; (cd = *pcd); pcd = &cd->next) |
| 216 | if (cd->info == c) { |
| 217 | *pcd = cd->next; |
| 218 | _TIFFfree(cd); |
| 219 | return; |
| 220 | } |
| 221 | TIFFError("TIFFUnRegisterCODEC", |
| 222 | "Cannot remove compression scheme %s; not registered", c->name); |
| 223 | } |