home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / libtiff / c / tif_compre < prev    next >
Encoding:
Text File  |  1995-10-12  |  5.4 KB  |  206 lines

  1. /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_compress.c,v 1.49 1995/10/12 16:41:08 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1988-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 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.     TIFFError(tif->tif_name,
  39.         "%s %s encoding is not implemented", c->name, method);
  40.     return (-1);
  41. }
  42.  
  43. int
  44. _TIFFNoRowEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
  45. {
  46.     (void) pp; (void) cc; (void) s;
  47.     return (TIFFNoEncode(tif, "scanline"));
  48. }
  49.  
  50. int
  51. _TIFFNoStripEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
  52. {
  53.     (void) pp; (void) cc; (void) s;
  54.     return (TIFFNoEncode(tif, "strip"));
  55. }
  56.  
  57. int
  58. _TIFFNoTileEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
  59. {
  60.     (void) pp; (void) cc; (void) s;
  61.     return (TIFFNoEncode(tif, "tile"));
  62. }
  63.  
  64. static int
  65. TIFFNoDecode(TIFF* tif, char* method)
  66. {
  67.     const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
  68.     TIFFError(tif->tif_name,
  69.         "%s %s decoding is not implemented", c->name, method);
  70.     return (-1);
  71. }
  72.  
  73. int
  74. _TIFFNoRowDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
  75. {
  76.     (void) pp; (void) cc; (void) s;
  77.     return (TIFFNoDecode(tif, "scanline"));
  78. }
  79.  
  80. int
  81. _TIFFNoStripDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
  82. {
  83.     (void) pp; (void) cc; (void) s;
  84.     return (TIFFNoDecode(tif, "strip"));
  85. }
  86.  
  87. int
  88. _TIFFNoTileDecode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
  89. {
  90.     (void) pp; (void) cc; (void) s;
  91.     return (TIFFNoDecode(tif, "tile"));
  92. }
  93.  
  94. int
  95. _TIFFNoSeek(TIFF* tif, uint32 off)
  96. {
  97.     (void) off;
  98.     TIFFError(tif->tif_name,
  99.         "Compression algorithm does not support random access");
  100.     return (0);
  101. }
  102.  
  103. int
  104. _TIFFNoPreCode(TIFF* tif, tsample_t s)
  105. {
  106.     (void) tif; (void) s;
  107.     return (1);
  108. }
  109.  
  110. static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
  111. static void _TIFFvoid(TIFF* tif) { (void) tif; }
  112.  
  113. int
  114. TIFFSetCompressionScheme(TIFF* tif, int scheme)
  115. {
  116.     const TIFFCodec *c = TIFFFindCODEC(scheme);
  117.  
  118.     if (!c) {
  119.         TIFFError(tif->tif_name,
  120.             "Unknown data compression algorithm %u (0x%x)",
  121.             scheme, scheme);
  122.         return (0);
  123.     }
  124.     tif->tif_setupdecode = _TIFFtrue;
  125.     tif->tif_predecode = _TIFFNoPreCode;
  126.     tif->tif_decoderow = _TIFFNoRowDecode;
  127.     tif->tif_decodestrip = _TIFFNoStripDecode;
  128.     tif->tif_decodetile = _TIFFNoTileDecode;
  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;
  141.     return ((*c->init)(tif, scheme));
  142. }
  143.  
  144. /*
  145.  * Other compression schemes may be registered.  Registered
  146.  * schemes can also override the builtin versions provided
  147.  * by this library.
  148.  */
  149. typedef struct _codec {
  150.     struct _codec*    next;
  151.     TIFFCodec*    info;
  152. } codec_t;
  153. static    codec_t* registeredCODECS = NULL;
  154.  
  155. const TIFFCodec*
  156. TIFFFindCODEC(uint16 scheme)
  157. {
  158.     const TIFFCodec* c;
  159.     codec_t* cd;
  160.  
  161.     for (cd = registeredCODECS; cd; cd = cd->next)
  162.         if (cd->info->scheme == scheme)
  163.             return ((const TIFFCodec*) cd->info);
  164.     for (c = _TIFFBuiltinCODECS; c->name; c++)
  165.         if (c->scheme == scheme)
  166.             return (c);
  167.     return ((const TIFFCodec*) 0);
  168. }
  169.  
  170. TIFFCodec*
  171. TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
  172. {
  173.     codec_t* cd = (codec_t*)
  174.         _TIFFmalloc(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1);
  175.  
  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.         TIFFError("TIFFRegisterCODEC",
  187.             "No space to register compression scheme %s", name);
  188.     return (cd->info);
  189. }
  190.  
  191. void
  192. TIFFUnRegisterCODEC(TIFFCodec* c)
  193. {
  194.     codec_t* cd;
  195.     codec_t** pcd;
  196.  
  197.     for (pcd = ®isteredCODECS; (cd = *pcd); pcd = &cd->next)
  198.         if (cd->info == c) {
  199.             *pcd = cd->next;
  200.             _TIFFfree(cd);
  201.             return;
  202.         }
  203.     TIFFError("TIFFUnRegisterCODEC",
  204.         "Cannot remove compression scheme %s; not registered", c->name);
  205. }
  206.