tif_packbits.c 8.83 KB
Newer Older
1
/* $Id: tif_packbits.c,v 1.20 2010-03-10 18:56:49 bfriesen Exp $ */
2 3 4 5 6

/*
 * Copyright (c) 1988-1997 Sam Leffler
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
 *
7
 * Permission to use, copy, modify, distribute, and sell this software and
8 9 10 11 12 13
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
14 15 16 17 18
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
19 20 21
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 23
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 25 26 27 28 29 30 31 32 33 34 35 36
 * OF THIS SOFTWARE.
 */

#include "tiffiop.h"
#ifdef PACKBITS_SUPPORT
/*
 * TIFF Library.
 *
 * PackBits Compression Algorithm Support
 */
#include <stdio.h>

static int
37
PackBitsPreEncode(TIFF* tif, uint16 s)
38
{
39
    (void) s;
40

41 42 43 44 45 46 47 48 49 50
    if (!(tif->tif_data = (uint8*)_TIFFmalloc(sizeof(tmsize_t))))
        return (0);
    /*
     * Calculate the scanline/tile-width size in bytes.
     */
    if (isTiled(tif))
        *(tmsize_t*)tif->tif_data = TIFFTileRowSize(tif);
    else
        *(tmsize_t*)tif->tif_data = TIFFScanlineSize(tif);
    return (1);
51 52 53 54 55 56 57
}

static int
PackBitsPostEncode(TIFF* tif)
{
        if (tif->tif_data)
            _TIFFfree(tif->tif_data);
58
    return (1);
59 60 61 62 63 64
}

/*
 * Encode a run of pixels.
 */
static int
65
PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
66
{
67 68 69 70 71 72 73
    unsigned char* bp = (unsigned char*) buf;
    uint8* op;
    uint8* ep;
    uint8* lastliteral;
    long n, slop;
    int b;
    enum { BASE, LITERAL, RUN, LITERAL_RUN } state;
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    (void) s;
    op = tif->tif_rawcp;
    ep = tif->tif_rawdata + tif->tif_rawdatasize;
    state = BASE;
    lastliteral = 0;
    while (cc > 0) {
        /*
         * Find the longest string of identical bytes.
         */
        b = *bp++, cc--, n = 1;
        for (; cc > 0 && b == *bp; cc--, bp++)
            n++;
    again:
        if (op + 2 >= ep) {		/* insure space for new data */
            /*
             * Be careful about writing the last
             * literal.  Must write up to that point
             * and then copy the remainder to the
             * front of the buffer.
             */
            if (state == LITERAL || state == LITERAL_RUN) {
                slop = (long)(op - lastliteral);
                tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
                if (!TIFFFlushData1(tif))
                    return (-1);
                op = tif->tif_rawcp;
                while (slop-- > 0)
                    *op++ = *lastliteral++;
                lastliteral = tif->tif_rawcp;
            } else {
                tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
                if (!TIFFFlushData1(tif))
                    return (-1);
                op = tif->tif_rawcp;
            }
        }
        switch (state) {
        case BASE:		/* initial state, set run/literal */
            if (n > 1) {
                state = RUN;
                if (n > 128) {
                    *op++ = (uint8) -127;
                    *op++ = (uint8) b;
                    n -= 128;
                    goto again;
                }
                *op++ = (uint8)(-(n-1));
                *op++ = (uint8) b;
            } else {
                lastliteral = op;
                *op++ = 0;
                *op++ = (uint8) b;
                state = LITERAL;
            }
            break;
        case LITERAL:		/* last object was literal string */
            if (n > 1) {
                state = LITERAL_RUN;
                if (n > 128) {
                    *op++ = (uint8) -127;
                    *op++ = (uint8) b;
                    n -= 128;
                    goto again;
                }
                *op++ = (uint8)(-(n-1));	/* encode run */
                *op++ = (uint8) b;
            } else {			/* extend literal */
                if (++(*lastliteral) == 127)
                    state = BASE;
                *op++ = (uint8) b;
            }
            break;
        case RUN:		/* last object was run */
            if (n > 1) {
                if (n > 128) {
                    *op++ = (uint8) -127;
                    *op++ = (uint8) b;
                    n -= 128;
                    goto again;
                }
                *op++ = (uint8)(-(n-1));
                *op++ = (uint8) b;
            } else {
                lastliteral = op;
                *op++ = 0;
                *op++ = (uint8) b;
                state = LITERAL;
            }
            break;
        case LITERAL_RUN:	/* literal followed by a run */
            /*
             * Check to see if previous run should
             * be converted to a literal, in which
             * case we convert literal-run-literal
             * to a single literal.
             */
            if (n == 1 && op[-2] == (uint8) -1 &&
                *lastliteral < 126) {
                state = (((*lastliteral) += 2) == 127 ?
                    BASE : LITERAL);
                op[-2] = op[-1];	/* replicate */
            } else
                state = RUN;
            goto again;
        }
    }
    tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
    tif->tif_rawcp = op;
    return (1);
184 185 186 187 188 189 190 191 192 193
}

/*
 * Encode a rectangular chunk of pixels.  We break it up
 * into row-sized pieces to insure that encoded runs do
 * not span rows.  Otherwise, there can be problems with
 * the decoder if data is read, for example, by scanlines
 * when it was encoded by strips.
 */
static int
194
PackBitsEncodeChunk(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
195
{
196 197 198 199
    tmsize_t rowsize = *(tmsize_t*)tif->tif_data;

    while (cc > 0) {
        tmsize_t chunk = rowsize;
200

201 202
        if( cc < chunk )
            chunk = cc;
203

204 205 206 207 208 209
        if (PackBitsEncode(tif, bp, chunk, s) < 0)
            return (-1);
        bp += chunk;
        cc -= chunk;
    }
    return (1);
210 211 212
}

static int
213
PackBitsDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
214
{
215 216 217 218 219
    static const char module[] = "PackBitsDecode";
    char *bp;
    tmsize_t cc;
    long n;
    int b;
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    (void) s;
    bp = (char*) tif->tif_rawcp;
    cc = tif->tif_rawcc;
    while (cc > 0 && occ > 0) {
        n = (long) *bp++, cc--;
        /*
         * Watch out for compilers that
         * don't sign extend chars...
         */
        if (n >= 128)
            n -= 256;
        if (n < 0) {		/* replicate next byte -n+1 times */
            if (n == -128)	/* nop */
                continue;
            n = -n + 1;
            if( occ < (tmsize_t)n )
            {
                TIFFWarningExt(tif->tif_clientdata, module,
                    "Discarding %lu bytes to avoid buffer overrun",
                    (unsigned long) ((tmsize_t)n - occ));
                n = (long)occ;
            }
            occ -= n;
            b = *bp++, cc--;      /* TODO: may be reading past input buffer here when input data is corrupt or ends prematurely */
            while (n-- > 0)
                *op++ = (uint8) b;
        } else {		/* copy next n+1 bytes literally */
            if (occ < (tmsize_t)(n + 1))
            {
                TIFFWarningExt(tif->tif_clientdata, module,
                    "Discarding %lu bytes to avoid buffer overrun",
                    (unsigned long) ((tmsize_t)n - occ + 1));
                n = (long)occ - 1;
            }
            _TIFFmemcpy(op, bp, ++n);  /* TODO: may be reading past input buffer here when input data is corrupt or ends prematurely */
            op += n; occ -= n;
            bp += n; cc -= n;
        }
    }
    tif->tif_rawcp = (uint8*) bp;
    tif->tif_rawcc = cc;
    if (occ > 0) {
        TIFFErrorExt(tif->tif_clientdata, module,
            "Not enough data for scanline %lu",
            (unsigned long) tif->tif_row);
        return (0);
    }
    return (1);
269 270 271 272 273
}

int
TIFFInitPackBits(TIFF* tif, int scheme)
{
274 275 276 277 278 279 280 281 282 283
    (void) scheme;
    tif->tif_decoderow = PackBitsDecode;
    tif->tif_decodestrip = PackBitsDecode;
    tif->tif_decodetile = PackBitsDecode;
    tif->tif_preencode = PackBitsPreEncode;
    tif->tif_postencode = PackBitsPostEncode;
    tif->tif_encoderow = PackBitsEncode;
    tif->tif_encodestrip = PackBitsEncodeChunk;
    tif->tif_encodetile = PackBitsEncodeChunk;
    return (1);
284 285 286 287
}
#endif /* PACKBITS_SUPPORT */

/* vim: set ts=8 sts=8 sw=8 noet: */
288 289 290 291 292 293 294
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 8
 * fill-column: 78
 * End:
 */