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