ReactOS  0.4.15-dev-1201-gb2cf5a4
tif_thunder.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 //#include <assert.h>
27 
28 #ifdef THUNDER_SUPPORT
29 /*
30  * TIFF Library.
31  *
32  * ThunderScan 4-bit Compression Algorithm Support
33  */
34 
35 /*
36  * ThunderScan uses an encoding scheme designed for
37  * 4-bit pixel values. Data is encoded in bytes, with
38  * each byte split into a 2-bit code word and a 6-bit
39  * data value. The encoding gives raw data, runs of
40  * pixels, or pixel values encoded as a delta from the
41  * previous pixel value. For the latter, either 2-bit
42  * or 3-bit delta values are used, with the deltas packed
43  * into a single byte.
44  */
45 #define THUNDER_DATA 0x3f /* mask for 6-bit data */
46 #define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
47 /* code values */
48 #define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */
49 #define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
50 #define DELTA2_SKIP 2 /* skip code for 2-bit deltas */
51 #define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
52 #define DELTA3_SKIP 4 /* skip code for 3-bit deltas */
53 #define THUNDER_RAW 0xc0 /* raw data encoded */
54 
55 static const int twobitdeltas[4] = { 0, 1, 0, -1 };
56 static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
57 
58 #define SETPIXEL(op, v) { \
59  lastpixel = (v) & 0xf; \
60  if ( npixels < maxpixels ) \
61  { \
62  if (npixels++ & 1) \
63  *op++ |= lastpixel; \
64  else \
65  op[0] = (uint8) (lastpixel << 4); \
66  } \
67 }
68 
69 static int
70 ThunderSetupDecode(TIFF* tif)
71 {
72  static const char module[] = "ThunderSetupDecode";
73 
74  if( tif->tif_dir.td_bitspersample != 4 )
75  {
77  "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
78  (int) tif->tif_dir.td_bitspersample );
79  return 0;
80  }
81 
82 
83  return (1);
84 }
85 
86 static int
87 ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
88 {
89  static const char module[] = "ThunderDecode";
90  register unsigned char *bp;
91  register tmsize_t cc;
92  unsigned int lastpixel;
93  tmsize_t npixels;
94 
95  bp = (unsigned char *)tif->tif_rawcp;
96  cc = tif->tif_rawcc;
97  lastpixel = 0;
98  npixels = 0;
99  while (cc > 0 && npixels < maxpixels) {
100  int n, delta;
101 
102  n = *bp++;
103  cc--;
104  switch (n & THUNDER_CODE) {
105  case THUNDER_RUN: /* pixel run */
106  /*
107  * Replicate the last pixel n times,
108  * where n is the lower-order 6 bits.
109  */
110  if (npixels & 1) {
111  op[0] |= lastpixel;
112  lastpixel = *op++; npixels++; n--;
113  } else
114  lastpixel |= lastpixel << 4;
115  npixels += n;
116  if (npixels < maxpixels) {
117  for (; n > 0; n -= 2)
118  *op++ = (uint8) lastpixel;
119  }
120  if (n == -1)
121  *--op &= 0xf0;
122  lastpixel &= 0xf;
123  break;
124  case THUNDER_2BITDELTAS: /* 2-bit deltas */
125  if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
126  SETPIXEL(op, (unsigned)((int)lastpixel + twobitdeltas[delta]));
127  if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
128  SETPIXEL(op, (unsigned)((int)lastpixel + twobitdeltas[delta]));
129  if ((delta = (n & 3)) != DELTA2_SKIP)
130  SETPIXEL(op, (unsigned)((int)lastpixel + twobitdeltas[delta]));
131  break;
132  case THUNDER_3BITDELTAS: /* 3-bit deltas */
133  if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
134  SETPIXEL(op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
135  if ((delta = (n & 7)) != DELTA3_SKIP)
136  SETPIXEL(op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
137  break;
138  case THUNDER_RAW: /* raw data */
139  SETPIXEL(op, n);
140  break;
141  }
142  }
143  tif->tif_rawcp = (uint8*) bp;
144  tif->tif_rawcc = cc;
145  if (npixels != maxpixels) {
146 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
148  "%s data at scanline %lu (%I64u != %I64u)",
149  npixels < maxpixels ? "Not enough" : "Too much",
150  (unsigned long) tif->tif_row,
151  (unsigned __int64) npixels,
152  (unsigned __int64) maxpixels);
153 #else
155  "%s data at scanline %lu (%llu != %llu)",
156  npixels < maxpixels ? "Not enough" : "Too much",
157  (unsigned long) tif->tif_row,
158  (unsigned long long) npixels,
159  (unsigned long long) maxpixels);
160 #endif
161  return (0);
162  }
163 
164  return (1);
165 }
166 
167 static int
168 ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
169 {
170  static const char module[] = "ThunderDecodeRow";
171  uint8* row = buf;
172 
173  (void) s;
174  if (occ % tif->tif_scanlinesize)
175  {
176  TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
177  return (0);
178  }
179  while (occ > 0) {
180  if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
181  return (0);
182  occ -= tif->tif_scanlinesize;
183  row += tif->tif_scanlinesize;
184  }
185  return (1);
186 }
187 
188 int
190 {
191  (void) scheme;
192 
193  tif->tif_setupdecode = ThunderSetupDecode;
194  tif->tif_decoderow = ThunderDecodeRow;
195  tif->tif_decodestrip = ThunderDecodeRow;
196  return (1);
197 }
198 #endif /* THUNDER_SUPPORT */
199 
200 /* vim: set ts=8 sts=8 sw=8 noet: */
201 /*
202  * Local Variables:
203  * mode: c
204  * c-basic-offset: 8
205  * fill-column: 78
206  * End:
207  */
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:180
uint8 * tif_rawcp
Definition: tiffiop.h:199
uint16 td_bitspersample
Definition: tif_dir.h:75
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLdouble n
Definition: glext.h:7729
DWORD scheme
tmsize_t tif_scanlinesize
Definition: tiffiop.h:193
thandle_t tif_clientdata
Definition: tiffiop.h:207
tmsize_t tif_rawcc
Definition: tiffiop.h:200
Definition: tiffiop.h:115
while(1)
Definition: macro.lex.yy.c:740
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:65
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:182
int TIFFInitThunderScan(TIFF *, int)
UINT op
Definition: effect.c:224
unsigned char uint8
Definition: types.h:28
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Definition: tif_error.c:65
GLdouble s
Definition: gl.h:2039
uint32_t cc
Definition: isohybrid.c:75
uint32 tif_row
Definition: tiffiop.h:159
unsigned short uint16
Definition: types.h:30
TIFFDirectory tif_dir
Definition: tiffiop.h:151
#define SETPIXEL(buf, x, y, p)
Definition: vnc.c:1021
uint32 td_imagewidth
Definition: tif_dir.h:72
#define __int64
Definition: basetyps.h:16
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
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
TIFFBoolMethod tif_setupdecode
Definition: tiffiop.h:174