|
Packit |
994f1a |
/* $Id: tif_next.c,v 1.8.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* Copyright (c) 1988-1997 Sam Leffler
|
|
Packit |
994f1a |
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
Packit |
994f1a |
*
|
|
Packit |
994f1a |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
Packit |
994f1a |
* its documentation for any purpose is hereby granted without fee, provided
|
|
Packit |
994f1a |
* that (i) the above copyright notices and this permission notice appear in
|
|
Packit |
994f1a |
* all copies of the software and related documentation, and (ii) the names of
|
|
Packit |
994f1a |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
Packit |
994f1a |
* publicity relating to the software without the specific, prior written
|
|
Packit |
994f1a |
* permission of Sam Leffler and Silicon Graphics.
|
|
Packit |
994f1a |
*
|
|
Packit |
994f1a |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
994f1a |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
Packit |
994f1a |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
Packit |
994f1a |
*
|
|
Packit |
994f1a |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
Packit |
994f1a |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
Packit |
994f1a |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
Packit |
994f1a |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
Packit |
994f1a |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
Packit |
994f1a |
* OF THIS SOFTWARE.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
#include "tiffiop.h"
|
|
Packit |
994f1a |
#ifdef NEXT_SUPPORT
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* TIFF Library.
|
|
Packit |
994f1a |
*
|
|
Packit |
994f1a |
* NeXT 2-bit Grey Scale Compression Algorithm Support
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
#define SETPIXEL(op, v) { \
|
|
Packit |
994f1a |
switch (npixels++ & 3) { \
|
|
Packit |
994f1a |
case 0: op[0] = (unsigned char) ((v) << 6); break; \
|
|
Packit |
994f1a |
case 1: op[0] |= (v) << 4; break; \
|
|
Packit |
994f1a |
case 2: op[0] |= (v) << 2; break; \
|
|
Packit |
994f1a |
case 3: *op++ |= (v); break; \
|
|
Packit |
994f1a |
} \
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
#define LITERALROW 0x00
|
|
Packit |
994f1a |
#define LITERALSPAN 0x40
|
|
Packit |
994f1a |
#define WHITE ((1<<2)-1)
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
static int
|
|
Packit |
994f1a |
NeXTDecode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
|
|
Packit |
994f1a |
{
|
|
Packit |
994f1a |
unsigned char *bp, *op;
|
|
Packit |
994f1a |
tsize_t cc;
|
|
Packit |
994f1a |
tidata_t row;
|
|
Packit |
994f1a |
tsize_t scanline, n;
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
(void) s;
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* Each scanline is assumed to start off as all
|
|
Packit |
994f1a |
* white (we assume a PhotometricInterpretation
|
|
Packit |
994f1a |
* of ``min-is-black'').
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
for (op = buf, cc = occ; cc-- > 0;)
|
|
Packit |
994f1a |
*op++ = 0xff;
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
bp = (unsigned char *)tif->tif_rawcp;
|
|
Packit |
994f1a |
cc = tif->tif_rawcc;
|
|
Packit |
994f1a |
scanline = tif->tif_scanlinesize;
|
|
Packit |
994f1a |
for (row = buf; occ > 0; occ -= scanline, row += scanline) {
|
|
Packit |
994f1a |
n = *bp++, cc--;
|
|
Packit |
994f1a |
switch (n) {
|
|
Packit |
994f1a |
case LITERALROW:
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* The entire scanline is given as literal values.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
if (cc < scanline)
|
|
Packit |
994f1a |
goto bad;
|
|
Packit |
994f1a |
_TIFFmemcpy(row, bp, scanline);
|
|
Packit |
994f1a |
bp += scanline;
|
|
Packit |
994f1a |
cc -= scanline;
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
case LITERALSPAN: {
|
|
Packit |
994f1a |
tsize_t off;
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* The scanline has a literal span that begins at some
|
|
Packit |
994f1a |
* offset.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
off = (bp[0] * 256) + bp[1];
|
|
Packit |
994f1a |
n = (bp[2] * 256) + bp[3];
|
|
Packit |
994f1a |
if (cc < 4+n || off+n > scanline)
|
|
Packit |
994f1a |
goto bad;
|
|
Packit |
994f1a |
_TIFFmemcpy(row+off, bp+4, n);
|
|
Packit |
994f1a |
bp += 4+n;
|
|
Packit |
994f1a |
cc -= 4+n;
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
default: {
|
|
Packit |
994f1a |
uint32 npixels = 0, grey;
|
|
Packit |
994f1a |
uint32 imagewidth = tif->tif_dir.td_imagewidth;
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* The scanline is composed of a sequence of constant
|
|
Packit |
994f1a |
* color ``runs''. We shift into ``run mode'' and
|
|
Packit |
994f1a |
* interpret bytes as codes of the form
|
|
Packit |
994f1a |
* <color><npixels> until we've filled the scanline.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
op = row;
|
|
Packit |
994f1a |
for (;;) {
|
|
Packit |
994f1a |
grey = (n>>6) & 0x3;
|
|
Packit |
994f1a |
n &= 0x3f;
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* Ensure the run does not exceed the scanline
|
|
Packit |
994f1a |
* bounds, potentially resulting in a security
|
|
Packit |
994f1a |
* issue.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
while (n-- > 0 && npixels < imagewidth)
|
|
Packit |
994f1a |
SETPIXEL(op, grey);
|
|
Packit |
994f1a |
if (npixels >= imagewidth)
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
if (cc == 0)
|
|
Packit |
994f1a |
goto bad;
|
|
Packit |
994f1a |
n = *bp++, cc--;
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
tif->tif_rawcp = (tidata_t) bp;
|
|
Packit |
994f1a |
tif->tif_rawcc = cc;
|
|
Packit |
994f1a |
return (1);
|
|
Packit |
994f1a |
bad:
|
|
Packit |
994f1a |
TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "NeXTDecode: Not enough data for scanline %ld",
|
|
Packit |
994f1a |
(long) tif->tif_row);
|
|
Packit |
994f1a |
return (0);
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
int
|
|
Packit |
994f1a |
TIFFInitNeXT(TIFF* tif, int scheme)
|
|
Packit |
994f1a |
{
|
|
Packit |
994f1a |
(void) scheme;
|
|
Packit |
994f1a |
tif->tif_decoderow = NeXTDecode;
|
|
Packit |
994f1a |
tif->tif_decodestrip = NeXTDecode;
|
|
Packit |
994f1a |
tif->tif_decodetile = NeXTDecode;
|
|
Packit |
994f1a |
return (1);
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
#endif /* NEXT_SUPPORT */
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
/* vim: set ts=8 sts=8 sw=8 noet: */
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* Local Variables:
|
|
Packit |
994f1a |
* mode: c
|
|
Packit |
994f1a |
* c-basic-offset: 8
|
|
Packit |
994f1a |
* fill-column: 78
|
|
Packit |
994f1a |
* End:
|
|
Packit |
994f1a |
*/
|