roentgen b75cab
/* $Id: tif_compress.c,v 1.22 2010-03-10 18:56:48 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library
roentgen b75cab
 *
roentgen b75cab
 * Compression Scheme Configuration Support.
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFNoEncode(TIFF* tif, const char* method)
roentgen b75cab
{
roentgen b75cab
	const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
roentgen b75cab
roentgen b75cab
	if (c) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			     "%s %s encoding is not implemented",
roentgen b75cab
			     c->name, method);
roentgen b75cab
	} else {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			"Compression scheme %u %s encoding is not implemented",
roentgen b75cab
			     tif->tif_dir.td_compression, method);
roentgen b75cab
	}
roentgen b75cab
	return (-1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) pp; (void) cc; (void) s;
roentgen b75cab
	return (TIFFNoEncode(tif, "scanline"));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) pp; (void) cc; (void) s;
roentgen b75cab
	return (TIFFNoEncode(tif, "strip"));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoTileEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) pp; (void) cc; (void) s;
roentgen b75cab
	return (TIFFNoEncode(tif, "tile"));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFNoDecode(TIFF* tif, const char* method)
roentgen b75cab
{
roentgen b75cab
	const TIFFCodec* c = TIFFFindCODEC(tif->tif_dir.td_compression);
roentgen b75cab
roentgen b75cab
	if (c)
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			     "%s %s decoding is not implemented",
roentgen b75cab
			     c->name, method);
roentgen b75cab
	else
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			     "Compression scheme %u %s decoding is not implemented",
roentgen b75cab
			     tif->tif_dir.td_compression, method);
roentgen b75cab
	return (-1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoFixupTags(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	(void) tif;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) pp; (void) cc; (void) s;
roentgen b75cab
	return (TIFFNoDecode(tif, "scanline"));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) pp; (void) cc; (void) s;
roentgen b75cab
	return (TIFFNoDecode(tif, "strip"));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoTileDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) pp; (void) cc; (void) s;
roentgen b75cab
	return (TIFFNoDecode(tif, "tile"));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoSeek(TIFF* tif, uint32 off)
roentgen b75cab
{
roentgen b75cab
	(void) off;
roentgen b75cab
	TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
		     "Compression algorithm does not support random access");
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFNoPreCode(TIFF* tif, uint16 s)
roentgen b75cab
{
roentgen b75cab
	(void) tif; (void) s;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int _TIFFtrue(TIFF* tif) { (void) tif; return (1); }
roentgen b75cab
static void _TIFFvoid(TIFF* tif) { (void) tif; }
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFSetDefaultCompressionState(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	tif->tif_fixuptags = _TIFFNoFixupTags; 
roentgen b75cab
	tif->tif_decodestatus = TRUE;
roentgen b75cab
	tif->tif_setupdecode = _TIFFtrue;
roentgen b75cab
	tif->tif_predecode = _TIFFNoPreCode;
roentgen b75cab
	tif->tif_decoderow = _TIFFNoRowDecode;  
roentgen b75cab
	tif->tif_decodestrip = _TIFFNoStripDecode;
roentgen b75cab
	tif->tif_decodetile = _TIFFNoTileDecode;  
roentgen b75cab
	tif->tif_encodestatus = TRUE;
roentgen b75cab
	tif->tif_setupencode = _TIFFtrue;
roentgen b75cab
	tif->tif_preencode = _TIFFNoPreCode;
roentgen b75cab
	tif->tif_postencode = _TIFFtrue;
roentgen b75cab
	tif->tif_encoderow = _TIFFNoRowEncode;
roentgen b75cab
	tif->tif_encodestrip = _TIFFNoStripEncode;  
roentgen b75cab
	tif->tif_encodetile = _TIFFNoTileEncode;  
roentgen b75cab
	tif->tif_close = _TIFFvoid;
roentgen b75cab
	tif->tif_seek = _TIFFNoSeek;
roentgen b75cab
	tif->tif_cleanup = _TIFFvoid;
roentgen b75cab
	tif->tif_defstripsize = _TIFFDefaultStripSize;
roentgen b75cab
	tif->tif_deftilesize = _TIFFDefaultTileSize;
roentgen b75cab
	tif->tif_flags &= ~(TIFF_NOBITREV|TIFF_NOREADRAW);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFSetCompressionScheme(TIFF* tif, int scheme)
roentgen b75cab
{
roentgen b75cab
	const TIFFCodec *c = TIFFFindCODEC((uint16) scheme);
roentgen b75cab
roentgen b75cab
	_TIFFSetDefaultCompressionState(tif);
roentgen b75cab
	/*
roentgen b75cab
	 * Don't treat an unknown compression scheme as an error.
roentgen b75cab
	 * This permits applications to open files with data that
roentgen b75cab
	 * the library does not have builtin support for, but which
roentgen b75cab
	 * may still be meaningful.
roentgen b75cab
	 */
roentgen b75cab
	return (c ? (*c->init)(tif, scheme) : 1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Other compression schemes may be registered.  Registered
roentgen b75cab
 * schemes can also override the builtin versions provided
roentgen b75cab
 * by this library.
roentgen b75cab
 */
roentgen b75cab
typedef struct _codec {
roentgen b75cab
	struct _codec* next;
roentgen b75cab
	TIFFCodec* info;
roentgen b75cab
} codec_t;
roentgen b75cab
static codec_t* registeredCODECS = NULL;
roentgen b75cab
roentgen b75cab
const TIFFCodec*
roentgen b75cab
TIFFFindCODEC(uint16 scheme)
roentgen b75cab
{
roentgen b75cab
	const TIFFCodec* c;
roentgen b75cab
	codec_t* cd;
roentgen b75cab
roentgen b75cab
	for (cd = registeredCODECS; cd; cd = cd->next)
roentgen b75cab
		if (cd->info->scheme == scheme)
roentgen b75cab
			return ((const TIFFCodec*) cd->info);
roentgen b75cab
	for (c = _TIFFBuiltinCODECS; c->name; c++)
roentgen b75cab
		if (c->scheme == scheme)
roentgen b75cab
			return (c);
roentgen b75cab
	return ((const TIFFCodec*) 0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
TIFFCodec*
roentgen b75cab
TIFFRegisterCODEC(uint16 scheme, const char* name, TIFFInitMethod init)
roentgen b75cab
{
roentgen b75cab
	codec_t* cd = (codec_t*)
roentgen b75cab
	    _TIFFmalloc((tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
roentgen b75cab
roentgen b75cab
	if (cd != NULL) {
roentgen b75cab
		cd->info = (TIFFCodec*) ((uint8*) cd + sizeof (codec_t));
roentgen b75cab
		cd->info->name = (char*)
roentgen b75cab
		    ((uint8*) cd->info + sizeof (TIFFCodec));
roentgen b75cab
		strcpy(cd->info->name, name);
roentgen b75cab
		cd->info->scheme = scheme;
roentgen b75cab
		cd->info->init = init;
roentgen b75cab
		cd->next = registeredCODECS;
roentgen b75cab
		registeredCODECS = cd;
roentgen b75cab
	} else {
roentgen b75cab
		TIFFErrorExt(0, "TIFFRegisterCODEC",
roentgen b75cab
		    "No space to register compression scheme %s", name);
roentgen b75cab
		return NULL;
roentgen b75cab
	}
roentgen b75cab
	return (cd->info);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
TIFFUnRegisterCODEC(TIFFCodec* c)
roentgen b75cab
{
roentgen b75cab
	codec_t* cd;
roentgen b75cab
	codec_t** pcd;
roentgen b75cab
roentgen b75cab
	for (pcd = ®isteredCODECS; (cd = *pcd); pcd = &cd->next)
roentgen b75cab
		if (cd->info == c) {
roentgen b75cab
			*pcd = cd->next;
roentgen b75cab
			_TIFFfree(cd);
roentgen b75cab
			return;
roentgen b75cab
		}
roentgen b75cab
	TIFFErrorExt(0, "TIFFUnRegisterCODEC",
roentgen b75cab
	    "Cannot remove compression scheme %s; not registered", c->name);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                       TIFFGetConfisuredCODECs()                      */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
/**
roentgen b75cab
 * Get list of configured codecs, both built-in and registered by user.
roentgen b75cab
 * Caller is responsible to free this structure.
roentgen b75cab
 * 
roentgen b75cab
 * @return returns array of TIFFCodec records (the last record should be NULL)
roentgen b75cab
 * or NULL if function failed.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
TIFFCodec*
roentgen b75cab
TIFFGetConfiguredCODECs()
roentgen b75cab
{
roentgen b75cab
	int i = 1;
roentgen b75cab
	codec_t *cd;
roentgen b75cab
	const TIFFCodec* c;
roentgen b75cab
	TIFFCodec* codecs = NULL;
roentgen b75cab
	TIFFCodec* new_codecs;
roentgen b75cab
roentgen b75cab
	for (cd = registeredCODECS; cd; cd = cd->next) {
roentgen b75cab
		new_codecs = (TIFFCodec *)
roentgen b75cab
			_TIFFrealloc(codecs, i * sizeof(TIFFCodec));
roentgen b75cab
		if (!new_codecs) {
roentgen b75cab
			_TIFFfree (codecs);
roentgen b75cab
			return NULL;
roentgen b75cab
		}
roentgen b75cab
		codecs = new_codecs;
roentgen b75cab
		_TIFFmemcpy(codecs + i - 1, cd, sizeof(TIFFCodec));
roentgen b75cab
		i++;
roentgen b75cab
	}
roentgen b75cab
	for (c = _TIFFBuiltinCODECS; c->name; c++) {
roentgen b75cab
		if (TIFFIsCODECConfigured(c->scheme)) {
roentgen b75cab
			new_codecs = (TIFFCodec *)
roentgen b75cab
				_TIFFrealloc(codecs, i * sizeof(TIFFCodec));
roentgen b75cab
			if (!new_codecs) {
roentgen b75cab
				_TIFFfree (codecs);
roentgen b75cab
				return NULL;
roentgen b75cab
			}
roentgen b75cab
			codecs = new_codecs;
roentgen b75cab
			_TIFFmemcpy(codecs + i - 1, (const void*)c, sizeof(TIFFCodec));
roentgen b75cab
			i++;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
roentgen b75cab
	if (!new_codecs) {
roentgen b75cab
		_TIFFfree (codecs);
roentgen b75cab
		return NULL;
roentgen b75cab
	}
roentgen b75cab
	codecs = new_codecs;
roentgen b75cab
	_TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
roentgen b75cab
roentgen b75cab
	return codecs;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */