ReactOS 0.4.16-dev-338-g34e76ad
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
25#include <precomp.h>
26
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); op_offset++; break; \
40 } \
41}
42
43#define LITERALROW 0x00
44#define LITERALSPAN 0x40
45#define WHITE ((1<<2)-1)
46
47static int
48NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
49{
50 static const char module[] = "NeXTDecode";
51 unsigned char *bp, *op;
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; cc > 0 && occ > 0; occ -= scanline, row += scanline) {
74 n = *bp++;
75 cc--;
76 switch (n) {
77 case LITERALROW:
78 /*
79 * The entire scanline is given as literal values.
80 */
81 if (cc < scanline)
82 goto bad;
83 _TIFFmemcpy(row, bp, scanline);
84 bp += scanline;
85 cc -= scanline;
86 break;
87 case LITERALSPAN: {
88 tmsize_t off;
89 /*
90 * The scanline has a literal span that begins at some
91 * offset.
92 */
93 if( cc < 4 )
94 goto bad;
95 off = (bp[0] * 256) + bp[1];
96 n = (bp[2] * 256) + bp[3];
97 if (cc < 4+n || off+n > scanline)
98 goto bad;
99 _TIFFmemcpy(row+off, bp+4, n);
100 bp += 4+n;
101 cc -= 4+n;
102 break;
103 }
104 default: {
105 uint32 npixels = 0, grey;
106 tmsize_t op_offset = 0;
107 uint32 imagewidth = tif->tif_dir.td_imagewidth;
108 if( isTiled(tif) )
109 imagewidth = tif->tif_dir.td_tilewidth;
110
111 /*
112 * The scanline is composed of a sequence of constant
113 * color ``runs''. We shift into ``run mode'' and
114 * interpret bytes as codes of the form
115 * <color><npixels> until we've filled the scanline.
116 */
117 op = row;
118 for (;;) {
119 grey = (uint32)((n>>6) & 0x3);
120 n &= 0x3f;
121 /*
122 * Ensure the run does not exceed the scanline
123 * bounds, potentially resulting in a security
124 * issue.
125 */
126 while (n-- > 0 && npixels < imagewidth && op_offset < scanline)
127 SETPIXEL(op, grey);
128 if (npixels >= imagewidth)
129 break;
130 if (op_offset >= scanline ) {
131 TIFFErrorExt(tif->tif_clientdata, module, "Invalid data for scanline %ld",
132 (long) tif->tif_row);
133 return (0);
134 }
135 if (cc == 0)
136 goto bad;
137 n = *bp++;
138 cc--;
139 }
140 break;
141 }
142 }
143 }
144 tif->tif_rawcp = (uint8*) bp;
145 tif->tif_rawcc = cc;
146 return (1);
147bad:
148 TIFFErrorExt(tif->tif_clientdata, module, "Not enough data for scanline %ld",
149 (long) tif->tif_row);
150 return (0);
151}
152
153static int
154NeXTPreDecode(TIFF* tif, uint16 s)
155{
156 static const char module[] = "NeXTPreDecode";
157 TIFFDirectory *td = &tif->tif_dir;
158 (void)s;
159
160 if( td->td_bitspersample != 2 )
161 {
162 TIFFErrorExt(tif->tif_clientdata, module, "Unsupported BitsPerSample = %d",
163 td->td_bitspersample);
164 return (0);
165 }
166 return (1);
167}
168
169int
170TIFFInitNeXT(TIFF* tif, int scheme)
171{
172 (void) scheme;
173 tif->tif_predecode = NeXTPreDecode;
174 tif->tif_decoderow = NeXTDecode;
175 tif->tif_decodestrip = NeXTDecode;
176 tif->tif_decodetile = NeXTDecode;
177 return (1);
178}
179#endif /* NEXT_SUPPORT */
180
181/* vim: set ts=8 sts=8 sw=8 noet: */
182/*
183 * Local Variables:
184 * mode: c
185 * c-basic-offset: 8
186 * fill-column: 78
187 * End:
188 */
unsigned short uint16
Definition: types.h:30
unsigned int uint32
Definition: types.h:32
unsigned char uint8
Definition: types.h:28
UINT op
Definition: effect.c:236
struct png_info_def *typedef unsigned char **typedef struct png_info_def *typedef struct png_info_def *typedef struct png_info_def *typedef unsigned char ** row
Definition: typeof.h:78
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
uint32_t cc
Definition: isohybrid.c:75
if(dx< 0)
Definition: linetemp.h:194
DWORD scheme
uint16 td_bitspersample
Definition: tif_dir.h:75
uint32 td_imagewidth
Definition: tif_dir.h:72
uint32 td_tilewidth
Definition: tif_dir.h:73
Definition: tiffiop.h:115
tmsize_t tif_scanlinesize
Definition: tiffiop.h:193
tmsize_t tif_rawcc
Definition: tiffiop.h:200
TIFFPreMethod tif_predecode
Definition: tiffiop.h:175
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:182
thandle_t tif_clientdata
Definition: tiffiop.h:207
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:180
TIFFDirectory tif_dir
Definition: tiffiop.h:151
uint32 tif_row
Definition: tiffiop.h:159
uint8 * tif_rawcp
Definition: tiffiop.h:199
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:184
#define TIFFInitNeXT
Definition: tif_codec.c:45
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Definition: tif_error.c:65
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:344
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:65
#define isTiled(tif)
Definition: tiffiop.h:229
#define SETPIXEL(buf, x, y, p)
Definition: vnc.c:1021