roentgen b75cab
/* $Id: tif_next.c,v 1.13 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
#include "tiffiop.h"
roentgen b75cab
#ifdef NEXT_SUPPORT
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 *
roentgen b75cab
 * NeXT 2-bit Grey Scale Compression Algorithm Support
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#define SETPIXEL(op, v) {			\
roentgen b75cab
	switch (npixels++ & 3) {		\
roentgen b75cab
	case 0:	op[0]  = (unsigned char) ((v) << 6); break;	\
roentgen b75cab
	case 1:	op[0] |= (v) << 4; break;	\
roentgen b75cab
	case 2:	op[0] |= (v) << 2; break;	\
roentgen b75cab
	case 3:	*op++ |= (v);	   break;	\
roentgen b75cab
	}					\
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define LITERALROW	0x00
roentgen b75cab
#define LITERALSPAN	0x40
roentgen b75cab
#define WHITE   	((1<<2)-1)
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "NeXTDecode";
roentgen b75cab
	unsigned char *bp, *op;
roentgen b75cab
	tmsize_t cc;
roentgen b75cab
	uint8* row;
roentgen b75cab
	tmsize_t scanline, n;
roentgen b75cab
roentgen b75cab
	(void) s;
roentgen b75cab
	/*
roentgen b75cab
	 * Each scanline is assumed to start off as all
roentgen b75cab
	 * white (we assume a PhotometricInterpretation
roentgen b75cab
	 * of ``min-is-black'').
roentgen b75cab
	 */
roentgen b75cab
	for (op = (unsigned char*) buf, cc = occ; cc-- > 0;)
roentgen b75cab
		*op++ = 0xff;
roentgen b75cab
roentgen b75cab
	bp = (unsigned char *)tif->tif_rawcp;
roentgen b75cab
	cc = tif->tif_rawcc;
roentgen b75cab
	scanline = tif->tif_scanlinesize;
roentgen b75cab
	if (occ % scanline)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	for (row = buf; occ > 0; occ -= scanline, row += scanline) {
roentgen b75cab
		n = *bp++, cc--;
roentgen b75cab
		switch (n) {
roentgen b75cab
		case LITERALROW:
roentgen b75cab
			/*
roentgen b75cab
			 * The entire scanline is given as literal values.
roentgen b75cab
			 */
roentgen b75cab
			if (cc < scanline)
roentgen b75cab
				goto bad;
roentgen b75cab
			_TIFFmemcpy(row, bp, scanline);
roentgen b75cab
			bp += scanline;
roentgen b75cab
			cc -= scanline;
roentgen b75cab
			break;
roentgen b75cab
		case LITERALSPAN: {
roentgen b75cab
			tmsize_t off;
roentgen b75cab
			/*
roentgen b75cab
			 * The scanline has a literal span that begins at some
roentgen b75cab
			 * offset.
roentgen b75cab
			 */
roentgen b75cab
			off = (bp[0] * 256) + bp[1];
roentgen b75cab
			n = (bp[2] * 256) + bp[3];
roentgen b75cab
			if (cc < 4+n || off+n > scanline)
roentgen b75cab
				goto bad;
roentgen b75cab
			_TIFFmemcpy(row+off, bp+4, n);
roentgen b75cab
			bp += 4+n;
roentgen b75cab
			cc -= 4+n;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		default: {
roentgen b75cab
			uint32 npixels = 0, grey;
roentgen b75cab
			uint32 imagewidth = tif->tif_dir.td_imagewidth;
roentgen b75cab
roentgen b75cab
			/*
roentgen b75cab
			 * The scanline is composed of a sequence of constant
roentgen b75cab
			 * color ``runs''.  We shift into ``run mode'' and
roentgen b75cab
			 * interpret bytes as codes of the form
roentgen b75cab
			 * <color><npixels> until we've filled the scanline.</npixels></color>
roentgen b75cab
			 */
roentgen b75cab
			op = row;
roentgen b75cab
			for (;;) {
roentgen b75cab
				grey = (uint32)((n>>6) & 0x3);
roentgen b75cab
				n &= 0x3f;
roentgen b75cab
				/*
roentgen b75cab
				 * Ensure the run does not exceed the scanline
roentgen b75cab
				 * bounds, potentially resulting in a security
roentgen b75cab
				 * issue.
roentgen b75cab
				 */
roentgen b75cab
				while (n-- > 0 && npixels < imagewidth)
roentgen b75cab
					SETPIXEL(op, grey);
roentgen b75cab
				if (npixels >= imagewidth)
roentgen b75cab
					break;
roentgen b75cab
				if (cc == 0)
roentgen b75cab
					goto bad;
roentgen b75cab
				n = *bp++, cc--;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	tif->tif_rawcp = (uint8*) bp;
roentgen b75cab
	tif->tif_rawcc = cc;
roentgen b75cab
	return (1);
roentgen b75cab
bad:
roentgen b75cab
	TIFFErrorExt(tif->tif_clientdata, module, "Not enough data for scanline %ld",
roentgen b75cab
	    (long) tif->tif_row);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFInitNeXT(TIFF* tif, int scheme)
roentgen b75cab
{
roentgen b75cab
	(void) scheme;
roentgen b75cab
	tif->tif_decoderow = NeXTDecode;  
roentgen b75cab
	tif->tif_decodestrip = NeXTDecode;  
roentgen b75cab
	tif->tif_decodetile = NeXTDecode;
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
#endif /* NEXT_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
 */