tif_compress.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. * TIFF Library
  26. *
  27. * Compression Scheme Configuration Support.
  28. */
  29. #include "tiffiop.h"
  30. static int
  31. TIFFNoEncode(TIFF* tif, const char* method)
  32. {
  33. const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
  34. if (c) {
  35. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  36. "%s %s encoding is not implemented",
  37. c->name, method);
  38. } else {
  39. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  40. "Compression scheme %u %s encoding is not implemented",
  41. tif->tif_dir.td_compression, method);
  42. }
  43. return (-1);
  44. }
  45. int
  46. _TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  47. {
  48. (void) pp; (void) cc; (void) s;
  49. return (TIFFNoEncode(tif, "scanline"));
  50. }
  51. int
  52. _TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  53. {
  54. (void) pp; (void) cc; (void) s;
  55. return (TIFFNoEncode(tif, "strip"));
  56. }
  57. int
  58. _TIFFNoTileEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  59. {
  60. (void) pp; (void) cc; (void) s;
  61. return (TIFFNoEncode(tif, "tile"));
  62. }
  63. static int
  64. TIFFNoDecode(TIFF* tif, const char* method)
  65. {
  66. const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
  67. if (c)
  68. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  69. "%s %s decoding is not implemented",
  70. c->name, method);
  71. else
  72. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  73. "Compression scheme %u %s decoding is not implemented",
  74. tif->tif_dir.td_compression, method);
  75. return (0);
  76. }
  77. static int
  78. _TIFFNoFixupTags(TIFF* tif)
  79. {
  80. (void) tif;
  81. return (1);
  82. }
  83. int
  84. _TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  85. {
  86. (void) pp; (void) cc; (void) s;
  87. return (TIFFNoDecode(tif, "scanline"));
  88. }
  89. int
  90. _TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  91. {
  92. (void) pp; (void) cc; (void) s;
  93. return (TIFFNoDecode(tif, "strip"));
  94. }
  95. int
  96. _TIFFNoTileDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  97. {
  98. (void) pp; (void) cc; (void) s;
  99. return (TIFFNoDecode(tif, "tile"));
  100. }
  101. int
  102. _TIFFNoSeek(TIFF* tif, uint32 off)
  103. {
  104. (void) off;
  105. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  106. "Compression algorithm does not support random access");
  107. return (0);
  108. }
  109. int
  110. _TIFFNoPreCode(TIFF* tif, uint16 s)
  111. {
  112. (void) tif; (void) s;
  113. return (1);
  114. }
  115. static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
  116. static void _TIFFvoid(TIFF* tif) { (void) tif; }
  117. void
  118. _TIFFSetDefaultCompressionState(TIFF* tif)
  119. {
  120. tif->tif_fixuptags = _TIFFNoFixupTags;
  121. tif->tif_decodestatus = TRUE;
  122. tif->tif_setupdecode = _TIFFtrue;
  123. tif->tif_predecode = _TIFFNoPreCode;
  124. tif->tif_decoderow = _TIFFNoRowDecode;
  125. tif->tif_decodestrip = _TIFFNoStripDecode;
  126. tif->tif_decodetile = _TIFFNoTileDecode;
  127. tif->tif_encodestatus = TRUE;
  128. tif->tif_setupencode = _TIFFtrue;
  129. tif->tif_preencode = _TIFFNoPreCode;
  130. tif->tif_postencode = _TIFFtrue;
  131. tif->tif_encoderow = _TIFFNoRowEncode;
  132. tif->tif_encodestrip = _TIFFNoStripEncode;
  133. tif->tif_encodetile = _TIFFNoTileEncode;
  134. tif->tif_close = _TIFFvoid;
  135. tif->tif_seek = _TIFFNoSeek;
  136. tif->tif_cleanup = _TIFFvoid;
  137. tif->tif_defstripsize = _TIFFDefaultStripSize;
  138. tif->tif_deftilesize = _TIFFDefaultTileSize;
  139. tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
  140. }
  141. int
  142. TIFFSetCompressionScheme(TIFF* tif, int scheme)
  143. {
  144. const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
  145. _TIFFSetDefaultCompressionState(tif);
  146. /*
  147. * Don't treat an unknown compression scheme as an error.
  148. * This permits applications to open files with data that
  149. * the library does not have builtin support for, but which
  150. * may still be meaningful.
  151. */
  152. return (c ? (*c->init)(tif, scheme) : 1);
  153. }
  154. /*
  155. * Other compression schemes may be registered. Registered
  156. * schemes can also override the builtin versions provided
  157. * by this library.
  158. */
  159. typedef struct _codec {
  160. struct _codec* next;
  161. TIFFCodec* info;
  162. } codec_t;
  163. static codec_t* registeredCODECS = NULL;
  164. const TIFFCodec*
  165. TIFFFindCODEC(uint16 scheme)
  166. {
  167. const TIFFCodec* c;
  168. codec_t* cd;
  169. for (cd = registeredCODECS; cd; cd = cd->next)
  170. if (cd->info->scheme == scheme)
  171. return ((const TIFFCodec*) cd->info);
  172. for (c = _TIFFBuiltinCODECS; c->name; c++)
  173. if (c->scheme == scheme)
  174. return (c);
  175. return ((const TIFFCodec*) 0);
  176. }
  177. TIFFCodec*
  178. TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
  179. {
  180. codec_t* cd = (codec_t*)
  181. _TIFFmalloc((tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
  182. if (cd != NULL) {
  183. cd->info = (TIFFCodec*) ((uint8*) cd + sizeof (codec_t));
  184. cd->info->name = (char*)
  185. ((uint8*) cd->info + sizeof (TIFFCodec));
  186. strcpy(cd->info->name, name);
  187. cd->info->scheme = scheme;
  188. cd->info->init = init;
  189. cd->next = registeredCODECS;
  190. registeredCODECS = cd;
  191. } else {
  192. TIFFErrorExt(0, "TIFFRegisterCODEC",
  193. "No space to register compression scheme %s", name);
  194. return NULL;
  195. }
  196. return (cd->info);
  197. }
  198. void
  199. TIFFUnRegisterCODEC(TIFFCodec* c)
  200. {
  201. codec_t* cd;
  202. codec_t** pcd;
  203. for (pcd = &registeredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
  204. if (cd->info == c) {
  205. *pcd = cd->next;
  206. _TIFFfree(cd);
  207. return;
  208. }
  209. TIFFErrorExt(0, "TIFFUnRegisterCODEC",
  210. "Cannot remove compression scheme %s; not registered", c->name);
  211. }
  212. /************************************************************************/
  213. /* TIFFGetConfisuredCODECs() */
  214. /************************************************************************/
  215. /**
  216. * Get list of configured codecs, both built-in and registered by user.
  217. * Caller is responsible to free this structure.
  218. *
  219. * @return returns array of TIFFCodec records (the last record should be NULL)
  220. * or NULL if function failed.
  221. */
  222. TIFFCodec*
  223. TIFFGetConfiguredCODECs()
  224. {
  225. int i = 1;
  226. codec_t *cd;
  227. const TIFFCodec* c;
  228. TIFFCodec* codecs = NULL;
  229. TIFFCodec* new_codecs;
  230. for (cd = registeredCODECS; cd; cd = cd->next) {
  231. new_codecs = (TIFFCodec *)
  232. _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
  233. if (!new_codecs) {
  234. _TIFFfree (codecs);
  235. return NULL;
  236. }
  237. codecs = new_codecs;
  238. _TIFFmemcpy(codecs + i - 1, cd->info, sizeof(TIFFCodec));
  239. i++;
  240. }
  241. for (c = _TIFFBuiltinCODECS; c->name; c++) {
  242. if (TIFFIsCODECConfigured(c->scheme)) {
  243. new_codecs = (TIFFCodec *)
  244. _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
  245. if (!new_codecs) {
  246. _TIFFfree (codecs);
  247. return NULL;
  248. }
  249. codecs = new_codecs;
  250. _TIFFmemcpy(codecs + i - 1, (const void*)c, sizeof(TIFFCodec));
  251. i++;
  252. }
  253. }
  254. new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
  255. if (!new_codecs) {
  256. _TIFFfree (codecs);
  257. return NULL;
  258. }
  259. codecs = new_codecs;
  260. _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
  261. return codecs;
  262. }
  263. /* vim: set ts=8 sts=8 sw=8 noet: */
  264. /*
  265. * Local Variables:
  266. * mode: c
  267. * c-basic-offset: 8
  268. * fill-column: 78
  269. * End:
  270. */