|
Packit |
994f1a |
/* $Id: tif_thunder.c,v 1.5.2.1 2010-06-08 18:50:43 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 THUNDER_SUPPORT
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* TIFF Library.
|
|
Packit |
994f1a |
*
|
|
Packit |
994f1a |
* ThunderScan 4-bit Compression Algorithm Support
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* ThunderScan uses an encoding scheme designed for
|
|
Packit |
994f1a |
* 4-bit pixel values. Data is encoded in bytes, with
|
|
Packit |
994f1a |
* each byte split into a 2-bit code word and a 6-bit
|
|
Packit |
994f1a |
* data value. The encoding gives raw data, runs of
|
|
Packit |
994f1a |
* pixels, or pixel values encoded as a delta from the
|
|
Packit |
994f1a |
* previous pixel value. For the latter, either 2-bit
|
|
Packit |
994f1a |
* or 3-bit delta values are used, with the deltas packed
|
|
Packit |
994f1a |
* into a single byte.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
#define THUNDER_DATA 0x3f /* mask for 6-bit data */
|
|
Packit |
994f1a |
#define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
|
|
Packit |
994f1a |
/* code values */
|
|
Packit |
994f1a |
#define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */
|
|
Packit |
994f1a |
#define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
|
|
Packit |
994f1a |
#define DELTA2_SKIP 2 /* skip code for 2-bit deltas */
|
|
Packit |
994f1a |
#define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
|
|
Packit |
994f1a |
#define DELTA3_SKIP 4 /* skip code for 3-bit deltas */
|
|
Packit |
994f1a |
#define THUNDER_RAW 0xc0 /* raw data encoded */
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
static const int twobitdeltas[4] = { 0, 1, 0, -1 };
|
|
Packit |
994f1a |
static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
|
|
Packit |
994f1a |
|
|
Packit Service |
55a0b9 |
#define SETPIXEL(op, v) { \
|
|
Packit Service |
55a0b9 |
lastpixel = (v) & 0xf; \
|
|
Packit Service |
55a0b9 |
if (npixels++ & 1) \
|
|
Packit Service |
55a0b9 |
*op++ |= lastpixel; \
|
|
Packit Service |
55a0b9 |
else \
|
|
Packit |
994f1a |
op[0] = (tidataval_t) (lastpixel << 4); \
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
static int
|
|
Packit |
994f1a |
ThunderDecode(TIFF* tif, tidata_t op, tsize_t maxpixels)
|
|
Packit |
994f1a |
{
|
|
Packit |
994f1a |
register unsigned char *bp;
|
|
Packit |
994f1a |
register tsize_t cc;
|
|
Packit |
994f1a |
unsigned int lastpixel;
|
|
Packit |
994f1a |
tsize_t npixels;
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
bp = (unsigned char *)tif->tif_rawcp;
|
|
Packit |
994f1a |
cc = tif->tif_rawcc;
|
|
Packit |
994f1a |
lastpixel = 0;
|
|
Packit |
994f1a |
npixels = 0;
|
|
Packit |
994f1a |
while (cc > 0 && npixels < maxpixels) {
|
|
Packit |
994f1a |
int n, delta;
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
n = *bp++, cc--;
|
|
Packit |
994f1a |
switch (n & THUNDER_CODE) {
|
|
Packit |
994f1a |
case THUNDER_RUN: /* pixel run */
|
|
Packit |
994f1a |
/*
|
|
Packit |
994f1a |
* Replicate the last pixel n times,
|
|
Packit |
994f1a |
* where n is the lower-order 6 bits.
|
|
Packit |
994f1a |
*/
|
|
Packit |
994f1a |
if (npixels & 1) {
|
|
Packit |
994f1a |
op[0] |= lastpixel;
|
|
Packit |
994f1a |
lastpixel = *op++; npixels++; n--;
|
|
Packit |
994f1a |
} else
|
|
Packit |
994f1a |
lastpixel |= lastpixel << 4;
|
|
Packit |
994f1a |
npixels += n;
|
|
Packit |
994f1a |
if (npixels < maxpixels) {
|
|
Packit |
994f1a |
for (; n > 0; n -= 2)
|
|
Packit |
994f1a |
*op++ = (tidataval_t) lastpixel;
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
if (n == -1)
|
|
Packit |
994f1a |
*--op &= 0xf0;
|
|
Packit |
994f1a |
lastpixel &= 0xf;
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
case THUNDER_2BITDELTAS: /* 2-bit deltas */
|
|
Packit |
994f1a |
if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
|
|
Packit |
994f1a |
SETPIXEL(op, lastpixel + twobitdeltas[delta]);
|
|
Packit |
994f1a |
if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
|
|
Packit |
994f1a |
SETPIXEL(op, lastpixel + twobitdeltas[delta]);
|
|
Packit |
994f1a |
if ((delta = (n & 3)) != DELTA2_SKIP)
|
|
Packit |
994f1a |
SETPIXEL(op, lastpixel + twobitdeltas[delta]);
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
case THUNDER_3BITDELTAS: /* 3-bit deltas */
|
|
Packit |
994f1a |
if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
|
|
Packit |
994f1a |
SETPIXEL(op, lastpixel + threebitdeltas[delta]);
|
|
Packit |
994f1a |
if ((delta = (n & 7)) != DELTA3_SKIP)
|
|
Packit |
994f1a |
SETPIXEL(op, lastpixel + threebitdeltas[delta]);
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
case THUNDER_RAW: /* raw data */
|
|
Packit |
994f1a |
SETPIXEL(op, n);
|
|
Packit |
994f1a |
break;
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
tif->tif_rawcp = (tidata_t) bp;
|
|
Packit |
994f1a |
tif->tif_rawcc = cc;
|
|
Packit |
994f1a |
if (npixels != maxpixels) {
|
|
Packit |
994f1a |
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
|
|
Packit |
994f1a |
"ThunderDecode: %s data at scanline %ld (%lu != %lu)",
|
|
Packit |
994f1a |
npixels < maxpixels ? "Not enough" : "Too much",
|
|
Packit |
994f1a |
(long) tif->tif_row, (long) npixels, (long) maxpixels);
|
|
Packit |
994f1a |
return (0);
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
return (1);
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
static int
|
|
Packit |
994f1a |
ThunderDecodeRow(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
|
|
Packit |
994f1a |
{
|
|
Packit |
994f1a |
tidata_t row = buf;
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
(void) s;
|
|
Packit |
994f1a |
while ((long)occ > 0) {
|
|
Packit |
994f1a |
if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
|
|
Packit |
994f1a |
return (0);
|
|
Packit |
994f1a |
occ -= tif->tif_scanlinesize;
|
|
Packit |
994f1a |
row += tif->tif_scanlinesize;
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
return (1);
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
|
|
Packit |
994f1a |
int
|
|
Packit |
994f1a |
TIFFInitThunderScan(TIFF* tif, int scheme)
|
|
Packit |
994f1a |
{
|
|
Packit |
994f1a |
(void) scheme;
|
|
Packit |
994f1a |
tif->tif_decoderow = ThunderDecodeRow;
|
|
Packit |
994f1a |
tif->tif_decodestrip = ThunderDecodeRow;
|
|
Packit |
994f1a |
return (1);
|
|
Packit |
994f1a |
}
|
|
Packit |
994f1a |
#endif /* THUNDER_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 |
*/
|