Coverage Report

Created: 2026-07-24 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/tiff/libtiff/tif_thunder.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
#include "tiffiop.h"
26
#include <assert.h>
27
#ifdef THUNDER_SUPPORT
28
/*
29
 * TIFF Library.
30
 *
31
 * ThunderScan 4-bit Compression Algorithm Support
32
 */
33
34
/*
35
 * ThunderScan uses an encoding scheme designed for
36
 * 4-bit pixel values.  Data is encoded in bytes, with
37
 * each byte split into a 2-bit code word and a 6-bit
38
 * data value.  The encoding gives raw data, runs of
39
 * pixels, or pixel values encoded as a delta from the
40
 * previous pixel value.  For the latter, either 2-bit
41
 * or 3-bit delta values are used, with the deltas packed
42
 * into a single byte.
43
 */
44
#define THUNDER_DATA 0x3f /* mask for 6-bit data */
45
140
#define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
46
/* code values */
47
82
#define THUNDER_RUN 0x00        /* run of pixels w/ encoded count */
48
4
#define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
49
12
#define DELTA2_SKIP 2           /* skip code for 2-bit deltas */
50
0
#define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
51
0
#define DELTA3_SKIP 4           /* skip code for 3-bit deltas */
52
54
#define THUNDER_RAW 0xc0        /* raw data encoded */
53
54
static const int twobitdeltas[4] = {0, 1, 0, -1};
55
static const int threebitdeltas[8] = {0, 1, 2, 3, 0, -3, -2, -1};
56
57
#define SETPIXEL(op, v)                                                        \
58
66
    {                                                                          \
59
66
        lastpixel = (v)&0xf;                                                   \
60
66
        if (npixels < maxpixels)                                               \
61
66
        {                                                                      \
62
65
            if (npixels++ & 1)                                                 \
63
65
                *op++ |= lastpixel;                                            \
64
65
            else                                                               \
65
65
                op[0] = (uint8_t)(lastpixel << 4);                             \
66
65
        }                                                                      \
67
66
    }
68
69
static int ThunderSetupDecode(TIFF *tif)
70
2
{
71
2
    static const char module[] = "ThunderSetupDecode";
72
73
2
    if (tif->tif_dir.td_bitspersample != 4)
74
0
    {
75
0
        TIFFErrorExtR(tif, module,
76
0
                      "Wrong bitspersample value (%d), Thunder decoder only "
77
0
                      "supports 4bits per sample.",
78
0
                      (int)tif->tif_dir.td_bitspersample);
79
0
        return 0;
80
0
    }
81
82
2
    return (1);
83
2
}
84
85
static int ThunderDecode(TIFF *tif, uint8_t *op0, tmsize_t maxpixels)
86
534
{
87
534
    static const char module[] = "ThunderDecode";
88
534
    register unsigned char *bp;
89
534
    register tmsize_t cc;
90
534
    unsigned int lastpixel;
91
534
    tmsize_t npixels;
92
534
    uint8_t *op = op0;
93
94
534
    bp = (unsigned char *)tif->tif_rawcp;
95
534
    cc = tif->tif_rawcc;
96
534
    lastpixel = 0;
97
534
    npixels = 0;
98
674
    while (cc > 0 && npixels < maxpixels)
99
140
    {
100
140
        int n, delta;
101
102
140
        n = *bp++;
103
140
        cc--;
104
140
        switch (n & THUNDER_CODE)
105
140
        {
106
82
            case THUNDER_RUN: /* pixel run */
107
                /*
108
                 * Replicate the last pixel n times,
109
                 * where n is the lower-order 6 bits.
110
                 */
111
82
                if (n == 0)
112
38
                    break;
113
44
                if (npixels & 1)
114
15
                {
115
15
                    op[0] |= lastpixel;
116
15
                    lastpixel = *op++;
117
15
                    npixels++;
118
15
                    n--;
119
15
                }
120
29
                else
121
29
                    lastpixel |= lastpixel << 4;
122
44
                npixels += n;
123
44
                if (npixels > maxpixels)
124
17
                    break;
125
110
                for (; n > 0; n -= 2)
126
83
                    *op++ = (uint8_t)lastpixel;
127
27
                if (n == -1)
128
12
                    *--op &= 0xf0;
129
27
                lastpixel &= 0xf;
130
27
                break;
131
4
            case THUNDER_2BITDELTAS: /* 2-bit deltas */
132
4
                if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
133
4
                    SETPIXEL(op,
134
4
                             (unsigned)((int)lastpixel + twobitdeltas[delta]));
135
4
                if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
136
4
                    SETPIXEL(op,
137
4
                             (unsigned)((int)lastpixel + twobitdeltas[delta]));
138
4
                if ((delta = (n & 3)) != DELTA2_SKIP)
139
4
                    SETPIXEL(op,
140
4
                             (unsigned)((int)lastpixel + twobitdeltas[delta]));
141
4
                break;
142
0
            case THUNDER_3BITDELTAS: /* 3-bit deltas */
143
0
                if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
144
0
                    SETPIXEL(
145
0
                        op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
146
0
                if ((delta = (n & 7)) != DELTA3_SKIP)
147
0
                    SETPIXEL(
148
0
                        op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
149
0
                break;
150
54
            case THUNDER_RAW: /* raw data */
151
54
                SETPIXEL(op, n);
152
54
                break;
153
140
        }
154
140
    }
155
534
    tif->tif_rawcp = (uint8_t *)bp;
156
534
    tif->tif_rawcc = cc;
157
534
    if (npixels != maxpixels)
158
530
    {
159
530
        uint8_t *op_end = op0 + (maxpixels + 1) / 2;
160
530
        memset(op, 0, (size_t)(op_end - op));
161
530
        TIFFErrorExtR(tif, module,
162
530
                      "%s data at scanline %lu (%" PRIu64 " != %" PRIu64 ")",
163
530
                      npixels < maxpixels ? "Not enough" : "Too much",
164
530
                      (unsigned long)tif->tif_row, (uint64_t)npixels,
165
530
                      (uint64_t)maxpixels);
166
530
        return (0);
167
530
    }
168
169
4
    return (1);
170
534
}
171
172
static int ThunderDecodeRow(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
173
534
{
174
534
    static const char module[] = "ThunderDecodeRow";
175
534
    uint8_t *row = buf;
176
177
534
    (void)s;
178
534
    if (occ % tif->tif_scanlinesize)
179
0
    {
180
0
        TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
181
0
        return (0);
182
0
    }
183
538
    while (occ > 0)
184
534
    {
185
534
        if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
186
530
            return (0);
187
4
        occ -= tif->tif_scanlinesize;
188
4
        row += tif->tif_scanlinesize;
189
4
    }
190
4
    return (1);
191
534
}
192
193
int TIFFInitThunderScan(TIFF *tif, int scheme)
194
8
{
195
8
    (void)scheme;
196
197
8
    tif->tif_setupdecode = ThunderSetupDecode;
198
8
    tif->tif_decoderow = ThunderDecodeRow;
199
8
    tif->tif_decodestrip = ThunderDecodeRow;
200
8
    return (1);
201
8
}
202
#endif /* THUNDER_SUPPORT */