tif_compress.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* $Id: tif_compress.c,v 1.22 2010-03-10 18:56:48 bfriesen Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library
  27. *
  28. * Compression Scheme Configuration Support.
  29. */
  30. #include "tiffiop.h"
  31. static int
  32. TIFFNoEncode(TIFF* tif, const char* method)
  33. {
  34. const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
  35. if (c) {
  36. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  37. "%s %s encoding is not implemented",
  38. c->name, method);
  39. } else {
  40. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  41. "Compression scheme %u %s encoding is not implemented",
  42. tif->tif_dir.td_compression, method);
  43. }
  44. return (-1);
  45. }
  46. int
  47. _TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  48. {
  49. (void) pp; (void) cc; (void) s;
  50. return (TIFFNoEncode(tif, "scanline"));
  51. }
  52. int
  53. _TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  54. {
  55. (void) pp; (void) cc; (void) s;
  56. return (TIFFNoEncode(tif, "strip"));
  57. }
  58. int
  59. _TIFFNoTileEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  60. {
  61. (void) pp; (void) cc; (void) s;
  62. return (TIFFNoEncode(tif, "tile"));
  63. }
  64. static int
  65. TIFFNoDecode(TIFF* tif, const char* method)
  66. {
  67. const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
  68. if (c)
  69. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  70. "%s %s decoding is not implemented",
  71. c->name, method);
  72. else
  73. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  74. "Compression scheme %u %s decoding is not implemented",
  75. tif->tif_dir.td_compression, method);
  76. return (-1);
  77. }
  78. int
  79. _TIFFNoFixupTags(TIFF* tif)
  80. {
  81. (void) tif;
  82. return (1);
  83. }
  84. int
  85. _TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  86. {
  87. (void) pp; (void) cc; (void) s;
  88. return (TIFFNoDecode(tif, "scanline"));
  89. }
  90. int
  91. _TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  92. {
  93. (void) pp; (void) cc; (void) s;
  94. return (TIFFNoDecode(tif, "strip"));
  95. }
  96. int
  97. _TIFFNoTileDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
  98. {
  99. (void) pp; (void) cc; (void) s;
  100. return (TIFFNoDecode(tif, "tile"));
  101. }
  102. int
  103. _TIFFNoSeek(TIFF* tif, uint32 off)
  104. {
  105. (void) off;
  106. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  107. "Compression algorithm does not support random access");
  108. return (0);
  109. }
  110. int
  111. _TIFFNoPreCode(TIFF* tif, uint16 s)
  112. {
  113. (void) tif; (void) s;
  114. return (1);
  115. }
  116. static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
  117. static void _TIFFvoid(TIFF* tif) { (void) tif; }
  118. void
  119. _TIFFSetDefaultCompressionState(TIFF* tif)
  120. {
  121. tif->tif_fixuptags = _TIFFNoFixupTags;
  122. tif->tif_decodestatus = TRUE;
  123. tif->tif_setupdecode = _TIFFtrue;
  124. tif->tif_predecode = _TIFFNoPreCode;
  125. tif->tif_decoderow = _TIFFNoRowDecode;
  126. tif->tif_decodestrip = _TIFFNoStripDecode;
  127. tif->tif_decodetile = _TIFFNoTileDecode;
  128. tif->tif_encodestatus = TRUE;
  129. tif->tif_setupencode = _TIFFtrue;
  130. tif->tif_preencode = _TIFFNoPreCode;
  131. tif->tif_postencode = _TIFFtrue;
  132. tif->tif_encoderow = _TIFFNoRowEncode;
  133. tif->tif_encodestrip = _TIFFNoStripEncode;
  134. tif->tif_encodetile = _TIFFNoTileEncode;
  135. tif->tif_close = _TIFFvoid;
  136. tif->tif_seek = _TIFFNoSeek;
  137. tif->tif_cleanup = _TIFFvoid;
  138. tif->tif_defstripsize = _TIFFDefaultStripSize;
  139. tif->tif_deftilesize = _TIFFDefaultTileSize;
  140. tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
  141. }
  142. int
  143. TIFFSetCompressionScheme(TIFF* tif, int scheme)
  144. {
  145. const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
  146. _TIFFSetDefaultCompressionState(tif);
  147. /*
  148. * Don't treat an unknown compression scheme as an error.
  149. * This permits applications to open files with data that
  150. * the library does not have builtin support for, but which
  151. * may still be meaningful.
  152. */
  153. return (c ? (*c->init)(tif, scheme) : 1);
  154. }
  155. /*
  156. * Other compression schemes may be registered. Registered
  157. * schemes can also override the builtin versions provided
  158. * by this library.
  159. */
  160. typedef struct _codec {
  161. struct _codec* next;
  162. TIFFCodec* info;
  163. } codec_t;
  164. static codec_t* registeredCODECS = NULL;
  165. const TIFFCodec*
  166. TIFFFindCODEC(uint16 scheme)
  167. {
  168. const TIFFCodec* c;
  169. codec_t* cd;
  170. for (cd = registeredCODECS; cd; cd = cd->next)
  171. if (cd->info->scheme == scheme)
  172. return ((const TIFFCodec*) cd->info);
  173. for (c = _TIFFBuiltinCODECS; c->name; c++)
  174. if (c->scheme == scheme)
  175. return (c);
  176. return ((const TIFFCodec*) 0);
  177. }
  178. TIFFCodec*
  179. TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
  180. {
  181. codec_t* cd = (codec_t*)
  182. _TIFFmalloc((tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
  183. if (cd != NULL) {
  184. cd->info = (TIFFCodec*) ((uint8*) cd + sizeof (codec_t));
  185. cd->info->name = (char*)
  186. ((uint8*) cd->info + sizeof (TIFFCodec));
  187. strcpy(cd->info->name, name);
  188. cd->info->scheme = scheme;
  189. cd->info->init = init;
  190. cd->next = registeredCODECS;
  191. registeredCODECS = cd;
  192. } else {
  193. TIFFErrorExt(0, "TIFFRegisterCODEC",
  194. "No space to register compression scheme %s", name);
  195. return NULL;
  196. }
  197. return (cd->info);
  198. }
  199. void
  200. TIFFUnRegisterCODEC(TIFFCodec* c)
  201. {
  202. codec_t* cd;
  203. codec_t** pcd;
  204. for (pcd = &registeredCODECS; (cd = *pcd); pcd = &cd->next)
  205. if (cd->info == c) {
  206. *pcd = cd->next;
  207. _TIFFfree(cd);
  208. return;
  209. }
  210. TIFFErrorExt(0, "TIFFUnRegisterCODEC",
  211. "Cannot remove compression scheme %s; not registered", c->name);
  212. }
  213. /************************************************************************/
  214. /* TIFFGetConfisuredCODECs() */
  215. /************************************************************************/
  216. /**
  217. * Get list of configured codecs, both built-in and registered by user.
  218. * Caller is responsible to free this structure.
  219. *
  220. * @return returns array of TIFFCodec records (the last record should be NULL)
  221. * or NULL if function failed.
  222. */
  223. TIFFCodec*
  224. TIFFGetConfiguredCODECs()
  225. {
  226. int i = 1;
  227. codec_t *cd;
  228. const TIFFCodec* c;
  229. TIFFCodec* codecs = NULL;
  230. TIFFCodec* new_codecs;
  231. for (cd = registeredCODECS; cd; cd = cd->next) {
  232. new_codecs = (TIFFCodec *)
  233. _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
  234. if (!new_codecs) {
  235. _TIFFfree (codecs);
  236. return NULL;
  237. }
  238. codecs = new_codecs;
  239. _TIFFmemcpy(codecs + i - 1, cd, sizeof(TIFFCodec));
  240. i++;
  241. }
  242. for (c = _TIFFBuiltinCODECS; c->name; c++) {
  243. if (TIFFIsCODECConfigured(c->scheme)) {
  244. new_codecs = (TIFFCodec *)
  245. _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
  246. if (!new_codecs) {
  247. _TIFFfree (codecs);
  248. return NULL;
  249. }
  250. codecs = new_codecs;
  251. _TIFFmemcpy(codecs + i - 1, (const void*)c, sizeof(TIFFCodec));
  252. i++;
  253. }
  254. }
  255. new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
  256. if (!new_codecs) {
  257. _TIFFfree (codecs);
  258. return NULL;
  259. }
  260. codecs = new_codecs;
  261. _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
  262. return codecs;
  263. }
  264. /* vim: set ts=8 sts=8 sw=8 noet: */
  265. /*
  266. * Local Variables:
  267. * mode: c
  268. * c-basic-offset: 8
  269. * fill-column: 78
  270. * End:
  271. */