Blame libtiff/tif_thunder.c

Packit 7838c8
/* $Id: tif_thunder.c,v 1.13 2016-09-04 21:32:56 erouault Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 1988-1997 Sam Leffler
Packit 7838c8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 7838c8
 *
Packit 7838c8
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit 7838c8
 * its documentation for any purpose is hereby granted without fee, provided
Packit 7838c8
 * that (i) the above copyright notices and this permission notice appear in
Packit 7838c8
 * all copies of the software and related documentation, and (ii) the names of
Packit 7838c8
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 7838c8
 * publicity relating to the software without the specific, prior written
Packit 7838c8
 * permission of Sam Leffler and Silicon Graphics.
Packit 7838c8
 * 
Packit 7838c8
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Packit 7838c8
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Packit 7838c8
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Packit 7838c8
 * 
Packit 7838c8
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 7838c8
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 7838c8
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 7838c8
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Packit 7838c8
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Packit 7838c8
 * OF THIS SOFTWARE.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tiffiop.h"
Packit 7838c8
#include <assert.h>
Packit 7838c8
#ifdef THUNDER_SUPPORT
Packit 7838c8
/*
Packit 7838c8
 * TIFF Library.
Packit 7838c8
 *
Packit 7838c8
 * ThunderScan 4-bit Compression Algorithm Support
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * ThunderScan uses an encoding scheme designed for
Packit 7838c8
 * 4-bit pixel values.  Data is encoded in bytes, with
Packit 7838c8
 * each byte split into a 2-bit code word and a 6-bit
Packit 7838c8
 * data value.  The encoding gives raw data, runs of
Packit 7838c8
 * pixels, or pixel values encoded as a delta from the
Packit 7838c8
 * previous pixel value.  For the latter, either 2-bit
Packit 7838c8
 * or 3-bit delta values are used, with the deltas packed
Packit 7838c8
 * into a single byte.
Packit 7838c8
 */
Packit 7838c8
#define	THUNDER_DATA		0x3f	/* mask for 6-bit data */
Packit 7838c8
#define	THUNDER_CODE		0xc0	/* mask for 2-bit code word */
Packit 7838c8
/* code values */
Packit 7838c8
#define	THUNDER_RUN		0x00	/* run of pixels w/ encoded count */
Packit 7838c8
#define	THUNDER_2BITDELTAS	0x40	/* 3 pixels w/ encoded 2-bit deltas */
Packit 7838c8
#define	    DELTA2_SKIP		2	/* skip code for 2-bit deltas */
Packit 7838c8
#define	THUNDER_3BITDELTAS	0x80	/* 2 pixels w/ encoded 3-bit deltas */
Packit 7838c8
#define	    DELTA3_SKIP		4	/* skip code for 3-bit deltas */
Packit 7838c8
#define	THUNDER_RAW		0xc0	/* raw data encoded */
Packit 7838c8
Packit 7838c8
static const int twobitdeltas[4] = { 0, 1, 0, -1 };
Packit 7838c8
static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
Packit 7838c8
Packit 7838c8
#define	SETPIXEL(op, v) {                     \
Packit 7838c8
	lastpixel = (v) & 0xf;                \
Packit 7838c8
        if ( npixels < maxpixels )         \
Packit 7838c8
        {                                     \
Packit 7838c8
	  if (npixels++ & 1)                  \
Packit 7838c8
	    *op++ |= lastpixel;               \
Packit 7838c8
	  else                                \
Packit 7838c8
	    op[0] = (uint8) (lastpixel << 4); \
Packit 7838c8
        }                                     \
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
ThunderSetupDecode(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "ThunderSetupDecode";
Packit 7838c8
Packit 7838c8
        if( tif->tif_dir.td_bitspersample != 4 )
Packit 7838c8
        {
Packit 7838c8
                TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
                             "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
Packit 7838c8
                             (int) tif->tif_dir.td_bitspersample );
Packit 7838c8
                return 0;
Packit 7838c8
        }
Packit 7838c8
        
Packit 7838c8
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "ThunderDecode";
Packit 7838c8
	register unsigned char *bp;
Packit 7838c8
	register tmsize_t cc;
Packit 7838c8
	unsigned int lastpixel;
Packit 7838c8
	tmsize_t npixels;
Packit 7838c8
Packit 7838c8
	bp = (unsigned char *)tif->tif_rawcp;
Packit 7838c8
	cc = tif->tif_rawcc;
Packit 7838c8
	lastpixel = 0;
Packit 7838c8
	npixels = 0;
Packit 7838c8
	while (cc > 0 && npixels < maxpixels) {
Packit 7838c8
		int n, delta;
Packit 7838c8
Packit 7838c8
		n = *bp++;
Packit 7838c8
		cc--;
Packit 7838c8
		switch (n & THUNDER_CODE) {
Packit 7838c8
		case THUNDER_RUN:		/* pixel run */
Packit 7838c8
			/*
Packit 7838c8
			 * Replicate the last pixel n times,
Packit 7838c8
			 * where n is the lower-order 6 bits.
Packit 7838c8
			 */
Packit 7838c8
			if (npixels & 1) {
Packit 7838c8
				op[0] |= lastpixel;
Packit 7838c8
				lastpixel = *op++; npixels++; n--;
Packit 7838c8
			} else
Packit 7838c8
				lastpixel |= lastpixel << 4;
Packit 7838c8
			npixels += n;
Packit 7838c8
			if (npixels < maxpixels) {
Packit 7838c8
				for (; n > 0; n -= 2)
Packit 7838c8
					*op++ = (uint8) lastpixel;
Packit 7838c8
			}
Packit 7838c8
			if (n == -1)
Packit 7838c8
				*--op &= 0xf0;
Packit 7838c8
			lastpixel &= 0xf;
Packit 7838c8
			break;
Packit 7838c8
		case THUNDER_2BITDELTAS:	/* 2-bit deltas */
Packit 7838c8
			if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
Packit 7838c8
				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
Packit 7838c8
			if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
Packit 7838c8
				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
Packit 7838c8
			if ((delta = (n & 3)) != DELTA2_SKIP)
Packit 7838c8
				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
Packit 7838c8
			break;
Packit 7838c8
		case THUNDER_3BITDELTAS:	/* 3-bit deltas */
Packit 7838c8
			if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
Packit 7838c8
				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
Packit 7838c8
			if ((delta = (n & 7)) != DELTA3_SKIP)
Packit 7838c8
				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
Packit 7838c8
			break;
Packit 7838c8
		case THUNDER_RAW:		/* raw data */
Packit 7838c8
			SETPIXEL(op, n);
Packit 7838c8
			break;
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	tif->tif_rawcp = (uint8*) bp;
Packit 7838c8
	tif->tif_rawcc = cc;
Packit 7838c8
	if (npixels != maxpixels) {
Packit 7838c8
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
			     "%s data at scanline %lu (%I64u != %I64u)",
Packit 7838c8
			     npixels < maxpixels ? "Not enough" : "Too much",
Packit 7838c8
			     (unsigned long) tif->tif_row,
Packit 7838c8
			     (unsigned __int64) npixels,
Packit 7838c8
			     (unsigned __int64) maxpixels);
Packit 7838c8
#else
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
			     "%s data at scanline %lu (%llu != %llu)",
Packit 7838c8
			     npixels < maxpixels ? "Not enough" : "Too much",
Packit 7838c8
			     (unsigned long) tif->tif_row,
Packit 7838c8
			     (unsigned long long) npixels,
Packit 7838c8
			     (unsigned long long) maxpixels);
Packit 7838c8
#endif
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
        return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "ThunderDecodeRow";
Packit 7838c8
	uint8* row = buf;
Packit 7838c8
	
Packit 7838c8
	(void) s;
Packit 7838c8
	if (occ % tif->tif_scanlinesize)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	while (occ > 0) {
Packit 7838c8
		if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
Packit 7838c8
			return (0);
Packit 7838c8
		occ -= tif->tif_scanlinesize;
Packit 7838c8
		row += tif->tif_scanlinesize;
Packit 7838c8
	}
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
TIFFInitThunderScan(TIFF* tif, int scheme)
Packit 7838c8
{
Packit 7838c8
	(void) scheme;
Packit 7838c8
Packit 7838c8
        tif->tif_setupdecode = ThunderSetupDecode;
Packit 7838c8
	tif->tif_decoderow = ThunderDecodeRow;
Packit 7838c8
	tif->tif_decodestrip = ThunderDecodeRow; 
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
#endif /* THUNDER_SUPPORT */
Packit 7838c8
Packit 7838c8
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 7838c8
/*
Packit 7838c8
 * Local Variables:
Packit 7838c8
 * mode: c
Packit 7838c8
 * c-basic-offset: 8
Packit 7838c8
 * fill-column: 78
Packit 7838c8
 * End:
Packit 7838c8
 */