LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_next.c
Go to the documentation of this file.
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
30#include "tiffiop.h"
31#ifdef NEXT_SUPPORT
32/*
33 * TIFF Library.
34 *
35 * NeXT 2-bit Grey Scale Compression Algorithm Support
36 */
37
54#define SETPIXEL(op, v) \
55 { \
56 switch (npixels++ & 3) \
57 { \
58 case 0: \
59 op[0] = (unsigned char)((v) << 6); \
60 break; \
61 case 1: \
62 op[0] |= (unsigned char)((v) << 4); \
63 break; \
64 case 2: \
65 op[0] |= (unsigned char)((v) << 2); \
66 break; \
67 case 3: \
68 *op++ |= (unsigned char)(v); \
69 op_offset++; \
70 break; \
71 default: \
72 break; \
73 } \
74 }
75
81#define LITERALROW 0x00
89#define LITERALSPAN 0x40
90
113static int NeXTDecode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
114{
115 static const char module[] = "NeXTDecode";
116 unsigned char *bp, *op;
117 tmsize_t cc;
118 uint8_t *row;
119 tmsize_t scanline, n;
120
121 (void)s;
122 /*
123 * Each scanline is assumed to start off as all
124 * white (we assume a PhotometricInterpretation
125 * of ``min-is-black'').
126 */
127 for (op = (unsigned char *)buf, cc = occ; cc-- > 0;)
128 *op++ = 0xff;
129
130 bp = (unsigned char *)tif->tif_rawcp;
131 cc = tif->tif_rawcc;
132 scanline = tif->tif_dir.td_scanlinesize;
133 if (occ % scanline)
134 {
135 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
136 return (0);
137 }
138 for (row = buf; cc > 0 && occ > 0; occ -= scanline, row += scanline)
139 {
140 n = *bp++;
141 cc--;
142 switch (n)
143 {
144 case LITERALROW:
145 /*
146 * The entire scanline is given as literal values.
147 */
148 if (cc < scanline)
149 goto bad;
150 _TIFFmemcpy(row, bp, scanline);
151 bp += scanline;
152 cc -= scanline;
153 break;
154 case LITERALSPAN:
155 {
156 tmsize_t off;
157 /*
158 * The scanline has a literal span that begins at some
159 * offset.
160 */
161 if (cc < 4)
162 goto bad;
163 off = (bp[0] * 256) + bp[1];
164 n = (bp[2] * 256) + bp[3];
165 if (cc < 4 + n || off + n > scanline)
166 goto bad;
167 _TIFFmemcpy(row + off, bp + 4, n);
168 bp += 4 + n;
169 cc -= 4 + n;
170 break;
171 }
172 default:
173 {
174 uint32_t npixels = 0, grey;
175 tmsize_t op_offset = 0;
176 uint32_t imagewidth = tif->tif_dir.td_imagewidth;
177 if (isTiled(tif))
178 imagewidth = tif->tif_dir.td_tilewidth;
179
180 /*
181 * The scanline is composed of a sequence of constant
182 * color ``runs''. We shift into ``run mode'' and
183 * interpret bytes as codes of the form
184 * <color><npixels> until we've filled the scanline.
185 */
186 op = row;
187 for (;;)
188 {
189 grey = (uint32_t)((n >> 6) & 0x3);
190 n &= 0x3f;
191 /*
192 * Ensure the run does not exceed the scanline
193 * bounds, potentially resulting in a security
194 * issue.
195 */
196 while (n-- > 0 && npixels < imagewidth &&
197 op_offset < scanline)
198 SETPIXEL(op, grey);
199 if (npixels >= imagewidth)
200 break;
201 if (op_offset >= scanline)
202 {
203 TIFFErrorExtR(tif, module,
204 "Invalid data for scanline %" PRIu32,
205 tif->tif_dir.td_row);
206 return (0);
207 }
208 if (cc == 0)
209 goto bad;
210 n = *bp++;
211 cc--;
212 }
213 break;
214 }
215 }
216 }
217 tif->tif_rawcp = (uint8_t *)bp;
218 tif->tif_rawcc = cc;
219 return (1);
220bad:
221 TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
222 tif->tif_dir.td_row);
223 return (0);
224}
225
239static int NeXTPreDecode(TIFF *tif, uint16_t s)
240{
241 static const char module[] = "NeXTPreDecode";
242 TIFFDirectory *td = &tif->tif_dir;
243 (void)s;
244
245 if (td->td_bitspersample != 2)
246 {
247 TIFFErrorExtR(tif, module, "Unsupported BitsPerSample = %" PRIu16,
248 td->td_bitspersample);
249 return (0);
250 }
251 if (td->td_samplesperpixel != 1 &&
252 td->td_planarconfig != PLANARCONFIG_SEPARATE)
253 {
254 TIFFErrorExtR(tif, module,
255 "Samples/pixel shall be 1 for NeXT decoding, "
256 "or PlanarConfiguration must be set to Separate.");
257 return (0);
258 }
259 return (1);
260}
261
262int TIFFInitNeXT(TIFF *tif, int scheme)
263{
264 (void)scheme;
269 return (1);
270}
271#endif /* NEXT_SUPPORT */
static int NeXTDecode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
Expand NeXT literal rows spans and grayscale runs.
Definition tif_next.c:113
#define LITERALROW
NeXT row opcode: the scanline follows as td_scanlinesize literal bytes.
Definition tif_next.c:81
int TIFFInitNeXT(TIFF *tif, int scheme)
Install NeXT transfer callbacks.
Definition tif_next.c:262
#define LITERALSPAN
NeXT row opcode: a 16-bit big-endian offset and length precede a literal byte span within the white s...
Definition tif_next.c:89
#define SETPIXEL(op, v)
Store a 2-bit pixel into the packed output byte at op.
Definition tif_next.c:54
static int NeXTPreDecode(TIFF *tif, uint16_t s)
Validate two-bit single-plane NeXT decoding.
Definition tif_next.c:239
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_imagewidth
Image width in pixels.
Definition tif_dir.h:154
uint32_t td_tilewidth
Tile width in pixels.
Definition tif_dir.h:160
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
Definition tiffiop.h:630
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Definition tiffiop.h:651
Declare private library state, callbacks, and implementation helpers.