]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/libtiff/tif_next.c
Fix configure on GNU Hurd.
[wxWidgets.git] / src / tiff / libtiff / tif_next.c
1
2 /*
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26 #include "tiffiop.h"
27 #ifdef NEXT_SUPPORT
28 /*
29 * TIFF Library.
30 *
31 * NeXT 2-bit Grey Scale Compression Algorithm Support
32 */
33
34 #define SETPIXEL(op, v) { \
35 switch (npixels++ & 3) { \
36 case 0: op[0] = (unsigned char) ((v) << 6); break; \
37 case 1: op[0] |= (v) << 4; break; \
38 case 2: op[0] |= (v) << 2; break; \
39 case 3: *op++ |= (v); break; \
40 } \
41 }
42
43 #define LITERALROW 0x00
44 #define LITERALSPAN 0x40
45 #define WHITE ((1<<2)-1)
46
47 static int
48 NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
49 {
50 static const char module[] = "NeXTDecode";
51 unsigned char *bp, *op;
52 tmsize_t cc;
53 uint8* row;
54 tmsize_t scanline, n;
55
56 (void) s;
57 /*
58 * Each scanline is assumed to start off as all
59 * white (we assume a PhotometricInterpretation
60 * of ``min-is-black'').
61 */
62 for (op = (unsigned char*) buf, cc = occ; cc-- > 0;)
63 *op++ = 0xff;
64
65 bp = (unsigned char *)tif->tif_rawcp;
66 cc = tif->tif_rawcc;
67 scanline = tif->tif_scanlinesize;
68 if (occ % scanline)
69 {
70 TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
71 return (0);
72 }
73 for (row = buf; occ > 0; occ -= scanline, row += scanline) {
74 n = *bp++, cc--;
75 switch (n) {
76 case LITERALROW:
77 /*
78 * The entire scanline is given as literal values.
79 */
80 if (cc < scanline)
81 goto bad;
82 _TIFFmemcpy(row, bp, scanline);
83 bp += scanline;
84 cc -= scanline;
85 break;
86 case LITERALSPAN: {
87 tmsize_t off;
88 /*
89 * The scanline has a literal span that begins at some
90 * offset.
91 */
92 off = (bp[0] * 256) + bp[1];
93 n = (bp[2] * 256) + bp[3];
94 if (cc < 4+n || off+n > scanline)
95 goto bad;
96 _TIFFmemcpy(row+off, bp+4, n);
97 bp += 4+n;
98 cc -= 4+n;
99 break;
100 }
101 default: {
102 uint32 npixels = 0, grey;
103 uint32 imagewidth = tif->tif_dir.td_imagewidth;
104
105 /*
106 * The scanline is composed of a sequence of constant
107 * color ``runs''. We shift into ``run mode'' and
108 * interpret bytes as codes of the form
109 * <color><npixels> until we've filled the scanline.
110 */
111 op = row;
112 for (;;) {
113 grey = (uint32)((n>>6) & 0x3);
114 n &= 0x3f;
115 /*
116 * Ensure the run does not exceed the scanline
117 * bounds, potentially resulting in a security
118 * issue.
119 */
120 while (n-- > 0 && npixels < imagewidth)
121 SETPIXEL(op, grey);
122 if (npixels >= imagewidth)
123 break;
124 if (cc == 0)
125 goto bad;
126 n = *bp++, cc--;
127 }
128 break;
129 }
130 }
131 }
132 tif->tif_rawcp = (uint8*) bp;
133 tif->tif_rawcc = cc;
134 return (1);
135 bad:
136 TIFFErrorExt(tif->tif_clientdata, module, "Not enough data for scanline %ld",
137 (long) tif->tif_row);
138 return (0);
139 }
140
141 int
142 TIFFInitNeXT(TIFF* tif, int scheme)
143 {
144 (void) scheme;
145 tif->tif_decoderow = NeXTDecode;
146 tif->tif_decodestrip = NeXTDecode;
147 tif->tif_decodetile = NeXTDecode;
148 return (1);
149 }
150 #endif /* NEXT_SUPPORT */
151
152 /* vim: set ts=8 sts=8 sw=8 noet: */
153 /*
154 * Local Variables:
155 * mode: c
156 * c-basic-offset: 8
157 * fill-column: 78
158 * End:
159 */