forked from zepouet/Xee-xCode-4.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZWHandle.m
65 lines (51 loc) · 1.13 KB
/
LZWHandle.m
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
#import "LZWHandle.h"
NSString *LZWInvalidCodeException=@"LZWInvalidCodeException";
@implementation LZWHandle
-(id)initWithHandle:(CSHandle *)handle earlyChange:(BOOL)earlychange
{
if(self=[super initWithHandle:handle])
{
early=earlychange;
lzw=AllocLZW(4096+1,2);
}
return self;
}
-(void)dealloc
{
FreeLZW(lzw);
[super dealloc];
}
-(void)clearTable
{
ClearLZWTable(lzw);
symbolsize=9;
currbyte=0;
}
-(void)resetByteStream
{
[self clearTable];
}
-(uint8_t)produceByteAtOffset:(off_t)pos
{
if(!currbyte)
{
int symbol;
for(;;)
{
symbol=CSInputNextBitString(input,symbolsize);
if(symbol==256) [self clearTable];
else break;
}
if(symbol==257) CSByteStreamEOF(self);
int err=NextLZWSymbol(lzw,symbol);
if(err!=LZWNoError) [NSException raise:LZWInvalidCodeException format:@"Invalid code in LZW stream (error code %d)",err];
currbyte=LZWReverseOutputToBuffer(lzw,buffer);
int offs=early?1:0;
int numsymbols=LZWSymbolCount(lzw);
if(numsymbols==512-offs) symbolsize=10;
else if(numsymbols==1024-offs) symbolsize=11;
else if(numsymbols==2048-offs) symbolsize=12;
}
return buffer[--currbyte];
}
@end