forked from JoshDullen/libtiffN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tif_packbits.cs
executable file
·306 lines (286 loc) · 8.54 KB
/
tif_packbits.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#if PACKBITS_SUPPORT
// tif_packbits.cs
//
// Based on LIBTIFF, Version 3.9.4 - 15-Jun-2010
// Copyright (c) 2006-2010 by the Authors
// Copyright (c) 1988-1997 Sam Leffler
// Copyright (c) 1991-1997 Silicon Graphics, Inc.
//
// Permission to use, copy, modify, distribute, and sell this software and
// its documentation for any purpose is hereby granted without fee, provided
// that (i) the above copyright notices and this permission notice appear in
// all copies of the software and related documentation, and (ii) the names of
// Sam Leffler and Silicon Graphics may not be used in any advertising or
// publicity relating to the software without the specific, prior written
// permission of Sam Leffler and Silicon Graphics.
//
// THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
// ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
// TIFF Library.
//
// PackBits Compression Algorithm Support
using System;
using System.Collections.Generic;
using System.Text;
namespace Free.Ports.LibTiff
{
public static partial class libtiff
{
class PackBitsState : ICodecState
{
internal int data;
}
static bool PackBitsPreEncode(TIFF tif, ushort sampleNumber)
{
PackBitsState state=null;
try
{
tif.tif_data=state=new PackBitsState();
}
catch
{
return false;
}
// Calculate the scanline/tile-width size in bytes.
if(isTiled(tif)) state.data=TIFFTileRowSize(tif);
else state.data=TIFFScanlineSize(tif);
return true;
}
static bool PackBitsPostEncode(TIFF tif)
{
tif.tif_data=null;
return true;
}
enum PackBitsEncodeState
{
BASE,
LITERAL,
RUN,
LITERAL_RUN
}
const byte Minus1=255;
const byte Minus127=129;
// Encode a run of pixels.
static bool PackBitsEncode(TIFF tif, byte[] buf, int cc, ushort s)
{
return PackBitsEncode1(tif, buf, 0, cc, s);
}
// Encode a run of pixels to *(buf+buf_offset)..
static bool PackBitsEncode1(TIFF tif, byte[] buf, int buf_offset, int cc, ushort s)
{
unsafe
{
fixed(byte* bp_=buf)
{
byte* bp=bp_+buf_offset;
uint op=tif.tif_rawcp;
uint ep=tif.tif_rawdatasize;
uint lastliteral=0;
PackBitsEncodeState state=PackBitsEncodeState.BASE;
while(cc>0)
{
// Find the longest string of identical bytes.
byte b=*bp++;
cc--;
int n=1;
for(; cc>0&&b==*bp; cc--, bp++) n++;
again:
if(op+2>=ep)
{ // insure space for new data
// Be careful about writing the last
// literal. Must write up to that point
// and then copy the remainder to the
// front of the buffer.
if(state==PackBitsEncodeState.LITERAL||state==PackBitsEncodeState.LITERAL_RUN)
{
uint slop=op-lastliteral;
tif.tif_rawcc+=lastliteral-tif.tif_rawcp;
if(!TIFFFlushData1(tif)) return false;
op=tif.tif_rawcp;
while((slop--)>0) tif.tif_rawdata[op++]=tif.tif_rawdata[lastliteral++];
lastliteral=tif.tif_rawcp;
}
else
{
tif.tif_rawcc+=op-tif.tif_rawcp;
if(!TIFFFlushData1(tif)) return false;
op=tif.tif_rawcp;
}
}
switch(state)
{
case PackBitsEncodeState.BASE: // initial state, set run/literal
if(n>1)
{
state=PackBitsEncodeState.RUN;
if(n>128)
{
tif.tif_rawdata[op++]=Minus127;
tif.tif_rawdata[op++]=b;
n-=128;
goto again;
}
tif.tif_rawdata[op++]=(byte)(-(n-1));
tif.tif_rawdata[op++]=b;
}
else
{
lastliteral=op;
tif.tif_rawdata[op++]=0;
tif.tif_rawdata[op++]=b;
state=PackBitsEncodeState.LITERAL;
}
break;
case PackBitsEncodeState.LITERAL: // last object was literal string
if(n>1)
{
state=PackBitsEncodeState.LITERAL_RUN;
if(n>128)
{
tif.tif_rawdata[op++]=Minus127;
tif.tif_rawdata[op++]=b;
n-=128;
goto again;
}
tif.tif_rawdata[op++]=(byte)(-(n-1)); // encode run
tif.tif_rawdata[op++]=b;
}
else
{ // extend literal
tif.tif_rawdata[lastliteral]++;
if(tif.tif_rawdata[lastliteral]==127) state=PackBitsEncodeState.BASE;
tif.tif_rawdata[op++]=b;
}
break;
case PackBitsEncodeState.RUN: // last object was run
if(n>1)
{
if(n>128)
{
tif.tif_rawdata[op++]=Minus127;
tif.tif_rawdata[op++]=b;
n-=128;
goto again;
}
tif.tif_rawdata[op++]=(byte)(-(n-1));
tif.tif_rawdata[op++]=b;
}
else
{
lastliteral=op;
tif.tif_rawdata[op++]=0;
tif.tif_rawdata[op++]=b;
state=PackBitsEncodeState.LITERAL;
}
break;
case PackBitsEncodeState.LITERAL_RUN: // literal followed by a run
// Check to see if previous run should
// be converted to a literal, in which
// case we convert literal-run-literal
// to a single literal.
if(n==1&&tif.tif_rawdata[op-2]==Minus1&&tif.tif_rawdata[lastliteral]<126)
{
state=((tif.tif_rawdata[lastliteral]+=2)==127?PackBitsEncodeState.BASE:PackBitsEncodeState.LITERAL);
tif.tif_rawdata[op-2]=tif.tif_rawdata[op-1]; // replicate
}
else state=PackBitsEncodeState.RUN;
goto again;
}
}
tif.tif_rawcc+=op-tif.tif_rawcp;
tif.tif_rawcp=op;
}
}
return true;
}
// Encode a rectangular chunk of pixels. We break it up
// into row-sized pieces to insure that encoded runs do
// not span rows. Otherwise, there can be problems with
// the decoder if data is read, for example, by scanlines
// when it was encoded by strips.
static bool PackBitsEncodeChunk(TIFF tif, byte[] buf, int cc, ushort s)
{
int rowsize=((PackBitsState)tif.tif_data).data;
int bp=0;
while(cc>0)
{
int chunk=rowsize;
if(cc<chunk) chunk=cc;
if(!PackBitsEncode1(tif, buf, bp, chunk, s)) return false;
bp+=chunk;
cc-=chunk;
}
return true;
}
static bool PackBitsDecode(TIFF tif, byte[] op, int occ, ushort s)
{
uint bp=tif.tif_rawcp;
int cc=(int)tif.tif_rawcc;
uint op_ind=0;
while(cc>0&&(long)occ>0)
{
int n=tif.tif_rawdata[bp++];
cc--;
// Watch out for compilers that
// don't sign extend chars...
if(n>=128) n-=256;
if(n<0)
{ // replicate next byte -n+1 times
if(n==-128) continue; // nop
n=-n+1;
if(occ<n)
{
TIFFWarningExt(tif.tif_clientdata, tif.tif_name, "PackBitsDecode: discarding {0} bytes to avoid buffer overrun", n-occ);
n=occ;
}
occ-=n;
byte b=tif.tif_rawdata[bp++];
cc--;
while((n--)>0) op[op_ind++]=b;
}
else
{ // copy next n+1 bytes literally
if(occ<n+1)
{
TIFFWarningExt(tif.tif_clientdata, tif.tif_name, "PackBitsDecode: discarding {0} bytes to avoid buffer overrun", n-occ+1);
n=occ-1;
}
n++;
Array.Copy(tif.tif_rawdata, bp, op, op_ind, n);
op_ind+=(uint)n; occ-=n;
bp+=(uint)n; cc-=n;
}
}
tif.tif_rawcp=bp;
tif.tif_rawcc=(uint)cc;
if(occ>0)
{
TIFFErrorExt(tif.tif_clientdata, tif.tif_name, "PackBitsDecode: Not enough data for scanline {0}", tif.tif_row);
return false;
}
return true;
}
static bool TIFFInitPackBits(TIFF tif, COMPRESSION scheme)
{
//tif.tif_predecode=PackBitsPreDecode;
tif.tif_decoderow=PackBitsDecode;
tif.tif_decodestrip=PackBitsDecode;
tif.tif_decodetile=PackBitsDecode;
tif.tif_preencode=PackBitsPreEncode;
tif.tif_postencode=PackBitsPostEncode;
tif.tif_encoderow=PackBitsEncode;
tif.tif_encodestrip=PackBitsEncodeChunk;
tif.tif_encodetile=PackBitsEncodeChunk;
return true;
}
}
}
#endif // PACKBITS_SUPPORT