/* $Id: tif_next.c,v 1.19 2016-09-04 21:32:56 erouault Exp $ */12/*3* Copyright (c) 1988-1997 Sam Leffler4* Copyright (c) 1991-1997 Silicon Graphics, Inc.5*6* Permission to use, copy, modify, distribute, and sell this software and7* its documentation for any purpose is hereby granted without fee, provided8* that (i) the above copyright notices and this permission notice appear in9* all copies of the software and related documentation, and (ii) the names of10* Sam Leffler and Silicon Graphics may not be used in any advertising or11* publicity relating to the software without the specific, prior written12* permission of Sam Leffler and Silicon Graphics.13*14* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,15* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY16* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.17*18* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR19* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,20* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,21* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF22* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE23* OF THIS SOFTWARE.24*/2526#include "tiffiop.h"27#ifdef NEXT_SUPPORT28/*29* TIFF Library.30*31* NeXT 2-bit Grey Scale Compression Algorithm Support32*/3334#define SETPIXEL(op, v) { \35switch (npixels++ & 3) { \36case 0: op[0] = (unsigned char) ((v) << 6); break; \37case 1: op[0] |= (v) << 4; break; \38case 2: op[0] |= (v) << 2; break; \39case 3: *op++ |= (v); op_offset++; break; \40} \41}4243#define LITERALROW 0x0044#define LITERALSPAN 0x4045#define WHITE ((1<<2)-1)4647static int48NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)49{50static const char module[] = "NeXTDecode";51unsigned char *bp, *op;52tmsize_t cc;53uint8* row;54tmsize_t scanline, n;5556(void) s;57/*58* Each scanline is assumed to start off as all59* white (we assume a PhotometricInterpretation60* of ``min-is-black'').61*/62for (op = (unsigned char*) buf, cc = occ; cc-- > 0;)63*op++ = 0xff;6465bp = (unsigned char *)tif->tif_rawcp;66cc = tif->tif_rawcc;67scanline = tif->tif_scanlinesize;68if (occ % scanline)69{70TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");71return (0);72}73for (row = buf; cc > 0 && occ > 0; occ -= scanline, row += scanline) {74n = *bp++;75cc--;76switch (n) {77case LITERALROW:78/*79* The entire scanline is given as literal values.80*/81if (cc < scanline)82goto bad;83_TIFFmemcpy(row, bp, scanline);84bp += scanline;85cc -= scanline;86break;87case LITERALSPAN: {88tmsize_t off;89/*90* The scanline has a literal span that begins at some91* offset.92*/93if( cc < 4 )94goto bad;95off = (bp[0] * 256) + bp[1];96n = (bp[2] * 256) + bp[3];97if (cc < 4+n || off+n > scanline)98goto bad;99_TIFFmemcpy(row+off, bp+4, n);100bp += 4+n;101cc -= 4+n;102break;103}104default: {105uint32 npixels = 0, grey;106tmsize_t op_offset = 0;107uint32 imagewidth = tif->tif_dir.td_imagewidth;108if( isTiled(tif) )109imagewidth = tif->tif_dir.td_tilewidth;110111/*112* The scanline is composed of a sequence of constant113* color ``runs''. We shift into ``run mode'' and114* interpret bytes as codes of the form115* <color><npixels> until we've filled the scanline.116*/117op = row;118for (;;) {119grey = (uint32)((n>>6) & 0x3);120n &= 0x3f;121/*122* Ensure the run does not exceed the scanline123* bounds, potentially resulting in a security124* issue.125*/126while (n-- > 0 && npixels < imagewidth && op_offset < scanline)127SETPIXEL(op, grey);128if (npixels >= imagewidth)129break;130if (op_offset >= scanline ) {131TIFFErrorExt(tif->tif_clientdata, module, "Invalid data for scanline %ld",132(long) tif->tif_row);133return (0);134}135if (cc == 0)136goto bad;137n = *bp++;138cc--;139}140break;141}142}143}144tif->tif_rawcp = (uint8*) bp;145tif->tif_rawcc = cc;146return (1);147bad:148TIFFErrorExt(tif->tif_clientdata, module, "Not enough data for scanline %ld",149(long) tif->tif_row);150return (0);151}152153static int154NeXTPreDecode(TIFF* tif, uint16 s)155{156static const char module[] = "NeXTPreDecode";157TIFFDirectory *td = &tif->tif_dir;158(void)s;159160if( td->td_bitspersample != 2 )161{162TIFFErrorExt(tif->tif_clientdata, module, "Unsupported BitsPerSample = %d",163td->td_bitspersample);164return (0);165}166return (1);167}168169int170TIFFInitNeXT(TIFF* tif, int scheme)171{172(void) scheme;173tif->tif_predecode = NeXTPreDecode;174tif->tif_decoderow = NeXTDecode;175tif->tif_decodestrip = NeXTDecode;176tif->tif_decodetile = NeXTDecode;177return (1);178}179#endif /* NEXT_SUPPORT */180181/* vim: set ts=8 sts=8 sw=8 noet: */182/*183* Local Variables:184* mode: c185* c-basic-offset: 8186* fill-column: 78187* End:188*/189190191