Coverage Report

Created: 2026-02-14 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/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
145k
#define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
46
/* code values */
47
67.6k
#define THUNDER_RUN 0x00        /* run of pixels w/ encoded count */
48
38.7k
#define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
49
116k
#define DELTA2_SKIP 2           /* skip code for 2-bit deltas */
50
25.0k
#define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
51
50.1k
#define DELTA3_SKIP 4           /* skip code for 3-bit deltas */
52
13.9k
#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
162k
    {                                                                          \
59
162k
        lastpixel = (v)&0xf;                                                   \
60
162k
        if (npixels < maxpixels)                                               \
61
162k
        {                                                                      \
62
161k
            if (npixels++ & 1)                                                 \
63
161k
                *op++ |= lastpixel;                                            \
64
161k
            else                                                               \
65
161k
                op[0] = (uint8_t)(lastpixel << 4);                             \
66
161k
        }                                                                      \
67
162k
    }
68
69
static int ThunderSetupDecode(TIFF *tif)
70
639
{
71
639
    static const char module[] = "ThunderSetupDecode";
72
73
639
    if (tif->tif_dir.td_bitspersample != 4)
74
7
    {
75
7
        TIFFErrorExtR(tif, module,
76
7
                      "Wrong bitspersample value (%d), Thunder decoder only "
77
7
                      "supports 4bits per sample.",
78
7
                      (int)tif->tif_dir.td_bitspersample);
79
7
        return 0;
80
7
    }
81
82
632
    return (1);
83
639
}
84
85
static int ThunderDecode(TIFF *tif, uint8_t *op0, tmsize_t maxpixels)
86
1.76k
{
87
1.76k
    static const char module[] = "ThunderDecode";
88
1.76k
    unsigned char *bp;
89
1.76k
    tmsize_t cc;
90
1.76k
    unsigned int lastpixel;
91
1.76k
    tmsize_t npixels;
92
1.76k
    uint8_t *op = op0;
93
94
1.76k
    bp = (unsigned char *)tif->tif_rawcp;
95
1.76k
    cc = tif->tif_rawcc;
96
1.76k
    lastpixel = 0;
97
1.76k
    npixels = 0;
98
147k
    while (cc > 0 && npixels < maxpixels)
99
145k
    {
100
145k
        int n, delta;
101
102
145k
        n = *bp++;
103
145k
        cc--;
104
145k
        switch (n & THUNDER_CODE)
105
145k
        {
106
67.6k
            case THUNDER_RUN: /* pixel run */
107
                /*
108
                 * Replicate the last pixel n times,
109
                 * where n is the lower-order 6 bits.
110
                 */
111
67.6k
                if (n == 0)
112
38.3k
                    break;
113
29.3k
                if (npixels & 1)
114
13.6k
                {
115
13.6k
                    op[0] |= lastpixel;
116
13.6k
                    lastpixel = *op++;
117
13.6k
                    npixels++;
118
13.6k
                    n--;
119
13.6k
                }
120
15.6k
                else
121
15.6k
                    lastpixel |= lastpixel << 4;
122
29.3k
                npixels += n;
123
29.3k
                if (npixels > maxpixels)
124
110
                    break;
125
165k
                for (; n > 0; n -= 2)
126
136k
                    *op++ = (uint8_t)lastpixel;
127
29.1k
                if (n == -1)
128
13.6k
                    *--op &= 0xf0;
129
29.1k
                lastpixel &= 0xf;
130
29.1k
                break;
131
38.7k
            case THUNDER_2BITDELTAS: /* 2-bit deltas */
132
38.7k
                if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
133
34.0k
                    SETPIXEL(op,
134
38.7k
                             (unsigned)((int)lastpixel + twobitdeltas[delta]));
135
38.7k
                if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
136
33.2k
                    SETPIXEL(op,
137
38.7k
                             (unsigned)((int)lastpixel + twobitdeltas[delta]));
138
38.7k
                if ((delta = (n & 3)) != DELTA2_SKIP)
139
35.1k
                    SETPIXEL(op,
140
38.7k
                             (unsigned)((int)lastpixel + twobitdeltas[delta]));
141
38.7k
                break;
142
25.0k
            case THUNDER_3BITDELTAS: /* 3-bit deltas */
143
25.0k
                if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
144
23.1k
                    SETPIXEL(
145
25.0k
                        op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
146
25.0k
                if ((delta = (n & 7)) != DELTA3_SKIP)
147
22.6k
                    SETPIXEL(
148
25.0k
                        op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
149
25.0k
                break;
150
13.9k
            case THUNDER_RAW: /* raw data */
151
13.9k
                SETPIXEL(op, n);
152
13.9k
                break;
153
0
            default:
154
0
                break;
155
145k
        }
156
145k
    }
157
1.76k
    tif->tif_rawcp = (uint8_t *)bp;
158
1.76k
    tif->tif_rawcc = cc;
159
1.76k
    if (npixels != maxpixels)
160
615
    {
161
615
        uint8_t *op_end = op0 + (maxpixels + 1) / 2;
162
615
        memset(op, 0, (size_t)(op_end - op));
163
615
        TIFFErrorExtR(tif, module,
164
615
                      "%s data at scanline %lu (%" PRIu64 " != %" PRIu64 ")",
165
615
                      npixels < maxpixels ? "Not enough" : "Too much",
166
615
                      (unsigned long)tif->tif_row, (uint64_t)npixels,
167
615
                      (uint64_t)maxpixels);
168
615
        return (0);
169
615
    }
170
171
1.15k
    return (1);
172
1.76k
}
173
174
static int ThunderDecodeRow(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
175
628
{
176
628
    static const char module[] = "ThunderDecodeRow";
177
628
    uint8_t *row = buf;
178
179
628
    (void)s;
180
628
    if (occ % tif->tif_scanlinesize)
181
0
    {
182
0
        TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
183
0
        return (0);
184
0
    }
185
1.78k
    while (occ > 0)
186
1.76k
    {
187
1.76k
        if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
188
615
            return (0);
189
1.15k
        occ -= tif->tif_scanlinesize;
190
1.15k
        row += tif->tif_scanlinesize;
191
1.15k
    }
192
13
    return (1);
193
628
}
194
195
int TIFFInitThunderScan(TIFF *tif, int scheme)
196
768
{
197
768
    (void)scheme;
198
199
768
    tif->tif_setupdecode = ThunderSetupDecode;
200
768
    tif->tif_decoderow = ThunderDecodeRow;
201
768
    tif->tif_decodestrip = ThunderDecodeRow;
202
768
    return (1);
203
768
}
204
#endif /* THUNDER_SUPPORT */