/src/FreeImage/Source/LibTIFF/tif_compress.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 295 lines · 205 code · 32 blank · 58 comment · 21 complexity · a7b7f952e869a85a8976954d065719df MD5 · raw file

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