roentgen b75cab
/* $Id: tif_thunder.c,v 1.12 2011-04-02 20:54:09 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
#include "tiffiop.h"
roentgen b75cab
#include <assert.h></assert.h>
roentgen b75cab
#ifdef THUNDER_SUPPORT
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 *
roentgen b75cab
 * ThunderScan 4-bit Compression Algorithm Support
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * ThunderScan uses an encoding scheme designed for
roentgen b75cab
 * 4-bit pixel values.  Data is encoded in bytes, with
roentgen b75cab
 * each byte split into a 2-bit code word and a 6-bit
roentgen b75cab
 * data value.  The encoding gives raw data, runs of
roentgen b75cab
 * pixels, or pixel values encoded as a delta from the
roentgen b75cab
 * previous pixel value.  For the latter, either 2-bit
roentgen b75cab
 * or 3-bit delta values are used, with the deltas packed
roentgen b75cab
 * into a single byte.
roentgen b75cab
 */
roentgen b75cab
#define	THUNDER_DATA		0x3f	/* mask for 6-bit data */
roentgen b75cab
#define	THUNDER_CODE		0xc0	/* mask for 2-bit code word */
roentgen b75cab
/* code values */
roentgen b75cab
#define	THUNDER_RUN		0x00	/* run of pixels w/ encoded count */
roentgen b75cab
#define	THUNDER_2BITDELTAS	0x40	/* 3 pixels w/ encoded 2-bit deltas */
roentgen b75cab
#define	    DELTA2_SKIP		2	/* skip code for 2-bit deltas */
roentgen b75cab
#define	THUNDER_3BITDELTAS	0x80	/* 2 pixels w/ encoded 3-bit deltas */
roentgen b75cab
#define	    DELTA3_SKIP		4	/* skip code for 3-bit deltas */
roentgen b75cab
#define	THUNDER_RAW		0xc0	/* raw data encoded */
roentgen b75cab
roentgen b75cab
static const int twobitdeltas[4] = { 0, 1, 0, -1 };
roentgen b75cab
static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
roentgen b75cab
roentgen b75cab
#define	SETPIXEL(op, v) {                     \
roentgen b75cab
	lastpixel = (v) & 0xf;                \
roentgen b75cab
        if ( npixels < maxpixels )         \
roentgen b75cab
        {                                     \
roentgen b75cab
	  if (npixels++ & 1)                  \
roentgen b75cab
	    *op++ |= lastpixel;               \
roentgen b75cab
	  else                                \
roentgen b75cab
	    op[0] = (uint8) (lastpixel << 4); \
roentgen b75cab
        }                                     \
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
ThunderSetupDecode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "ThunderSetupDecode";
roentgen b75cab
roentgen b75cab
        if( tif->tif_dir.td_bitspersample != 4 )
roentgen b75cab
        {
roentgen b75cab
                TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                             "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
roentgen b75cab
                             (int) tif->tif_dir.td_bitspersample );
roentgen b75cab
                return 0;
roentgen b75cab
        }
roentgen b75cab
        
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "ThunderDecode";
roentgen b75cab
	register unsigned char *bp;
roentgen b75cab
	register tmsize_t cc;
roentgen b75cab
	unsigned int lastpixel;
roentgen b75cab
	tmsize_t npixels;
roentgen b75cab
roentgen b75cab
	bp = (unsigned char *)tif->tif_rawcp;
roentgen b75cab
	cc = tif->tif_rawcc;
roentgen b75cab
	lastpixel = 0;
roentgen b75cab
	npixels = 0;
roentgen b75cab
	while (cc > 0 && npixels < maxpixels) {
roentgen b75cab
		int n, delta;
roentgen b75cab
roentgen b75cab
		n = *bp++, cc--;
roentgen b75cab
		switch (n & THUNDER_CODE) {
roentgen b75cab
		case THUNDER_RUN:		/* pixel run */
roentgen b75cab
			/*
roentgen b75cab
			 * Replicate the last pixel n times,
roentgen b75cab
			 * where n is the lower-order 6 bits.
roentgen b75cab
			 */
roentgen b75cab
			if (npixels & 1) {
roentgen b75cab
				op[0] |= lastpixel;
roentgen b75cab
				lastpixel = *op++; npixels++; n--;
roentgen b75cab
			} else
roentgen b75cab
				lastpixel |= lastpixel << 4;
roentgen b75cab
			npixels += n;
roentgen b75cab
			if (npixels < maxpixels) {
roentgen b75cab
				for (; n > 0; n -= 2)
roentgen b75cab
					*op++ = (uint8) lastpixel;
roentgen b75cab
			}
roentgen b75cab
			if (n == -1)
roentgen b75cab
				*--op &= 0xf0;
roentgen b75cab
			lastpixel &= 0xf;
roentgen b75cab
			break;
roentgen b75cab
		case THUNDER_2BITDELTAS:	/* 2-bit deltas */
roentgen b75cab
			if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
roentgen b75cab
				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
roentgen b75cab
			if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
roentgen b75cab
				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
roentgen b75cab
			if ((delta = (n & 3)) != DELTA2_SKIP)
roentgen b75cab
				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
roentgen b75cab
			break;
roentgen b75cab
		case THUNDER_3BITDELTAS:	/* 3-bit deltas */
roentgen b75cab
			if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
roentgen b75cab
				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
roentgen b75cab
			if ((delta = (n & 7)) != DELTA3_SKIP)
roentgen b75cab
				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
roentgen b75cab
			break;
roentgen b75cab
		case THUNDER_RAW:		/* raw data */
roentgen b75cab
			SETPIXEL(op, n);
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
	tif->tif_rawcc = cc;
roentgen b75cab
	if (npixels != maxpixels) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "%s data at scanline %lu (%I64u != %I64u)",
roentgen b75cab
			     npixels < maxpixels ? "Not enough" : "Too much",
roentgen b75cab
			     (unsigned long) tif->tif_row,
roentgen b75cab
			     (unsigned __int64) npixels,
roentgen b75cab
			     (unsigned __int64) maxpixels);
roentgen b75cab
#else
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "%s data at scanline %lu (%llu != %llu)",
roentgen b75cab
			     npixels < maxpixels ? "Not enough" : "Too much",
roentgen b75cab
			     (unsigned long) tif->tif_row,
roentgen b75cab
			     (unsigned long long) npixels,
roentgen b75cab
			     (unsigned long long) maxpixels);
roentgen b75cab
#endif
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "ThunderDecodeRow";
roentgen b75cab
	uint8* row = buf;
roentgen b75cab
	
roentgen b75cab
	(void) s;
roentgen b75cab
	if (occ % tif->tif_scanlinesize)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	while (occ > 0) {
roentgen b75cab
		if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
roentgen b75cab
			return (0);
roentgen b75cab
		occ -= tif->tif_scanlinesize;
roentgen b75cab
		row += tif->tif_scanlinesize;
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFInitThunderScan(TIFF* tif, int scheme)
roentgen b75cab
{
roentgen b75cab
	(void) scheme;
roentgen b75cab
roentgen b75cab
        tif->tif_setupdecode = ThunderSetupDecode;
roentgen b75cab
	tif->tif_decoderow = ThunderDecodeRow;
roentgen b75cab
	tif->tif_decodestrip = ThunderDecodeRow; 
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
#endif /* THUNDER_SUPPORT */
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
 */