forked from zepouet/Xee-xCode-4.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDFStream.m
504 lines (396 loc) · 11.4 KB
/
PDFStream.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#import "PDFStream.h"
#import "PDFParser.h"
#import "PDFEncryptionHandler.h"
#import "CCITTHandle.h"
#import "LZWHandle.h"
#import <XADMaster/CSZlibHandle.h>
#import <XADMaster/CSMemoryHandle.h>
#import <XADMaster/CSMultiHandle.h>
@implementation PDFStream
-(id)initWithDictionary:(NSDictionary *)dictionary fileHandle:(CSHandle *)filehandle
reference:(PDFObjectReference *)reference parser:(PDFParser *)owner
{
if(self=[super init])
{
dict=[dictionary retain];
fh=[filehandle retain];
offs=[fh offsetInFile];
ref=[reference retain];
parser=owner;
}
return self;
}
-(void)dealloc
{
[dict release];
[fh release];
[ref release];
[super dealloc];
}
-(NSDictionary *)dictionary { return dict; }
-(PDFObjectReference *)reference { return ref; }
-(BOOL)isImage
{
NSString *type=[dict objectForKey:@"Type"];
NSString *subtype=[dict objectForKey:@"Subtype"];
return (!type||[type isEqual:@"XObject"])&&subtype&&[subtype isEqual:@"Image"]; // kludge for broken Ghostscript PDFs
}
-(BOOL)isJPEG
{
return [[self finalFilter] isEqual:@"DCTDecode"]&&[self bitsPerComponent]==8;
}
-(BOOL)isJPEG2000
{
return [[self finalFilter] isEqual:@"JPXDecode"];
}
-(BOOL)isMask
{
return [dict boolValueForKey:@"ImageMask" default:NO]&&[self bitsPerComponent]==1;
}
-(BOOL)isBitmap
{
return [self isGrey]&&[self bitsPerComponent]==1;
}
-(BOOL)isIndexed
{
NSString *colourspace=[self colourSpaceOrAlternate];
return [colourspace isEqual:@"Indexed"];
}
-(BOOL)isGrey
{
NSString *colourspace=[self colourSpaceOrAlternate];
return [colourspace isEqual:@"DeviceGray"]||[colourspace isEqual:@"CalGray"];
}
-(BOOL)isRGB
{
NSString *colourspace=[self colourSpaceOrAlternate];
return [colourspace isEqual:@"DeviceRGB"]||[colourspace isEqual:@"CalRGB"];
}
-(BOOL)isCMYK
{
NSString *colourspace=[self colourSpaceOrAlternate];
return [colourspace isEqual:@"DeviceCMYK"]||[colourspace isEqual:@"CalCMYK"];
}
-(BOOL)isLab
{
NSString *colourspace=[self colourSpaceOrAlternate];
return [colourspace isEqual:@"DeviceLab"]||[colourspace isEqual:@"CalLab"];
}
-(NSString *)finalFilter
{
id filter=[dict objectForKey:@"Filter"];
if(!filter) return NO;
else if([filter isKindOfClass:[NSArray class]]) return [filter lastObject];
else return filter;
}
-(int)bitsPerComponent
{
NSNumber *val=[dict objectForKey:@"BitsPerComponent"];
if(val&&[val isKindOfClass:[NSNumber class]]) return [val intValue];
return 0;
}
-(NSString *)colourSpaceOrAlternate
{
id colourspace=[dict objectForKey:@"ColorSpace"];
if(!colourspace) return nil;
return [self _parseColourSpace:colourspace];
}
-(NSString *)subColourSpaceOrAlternate
{
id colourspace=[dict objectForKey:@"ColorSpace"];
if(!colourspace) return nil;
if(![colourspace isKindOfClass:[NSArray class]]) return nil;
if([colourspace count]!=4) return nil;
if(![[colourspace objectAtIndex:0] isEqual:@"Indexed"]) return nil;
return [self _parseColourSpace:[colourspace objectAtIndex:1]];
}
-(NSString *)_parseColourSpace:(id)colourspace
{
if([colourspace isKindOfClass:[NSString class]]) return colourspace;
else if([colourspace isKindOfClass:[NSArray class]])
{
int count=[colourspace count];
if(count<1) return nil;
NSString *name=[colourspace objectAtIndex:0];
if([name isEqual:@"ICCBased"])
{
PDFStream *def=[colourspace objectAtIndex:1];
if(![def isKindOfClass:[PDFStream class]]) return nil;
NSString *alternate=[[def dictionary] objectForKey:@"Alternate"];
if(alternate) return alternate;
int n=[[def dictionary] intValueForKey:@"N" default:0];
switch(n)
{
case 1: return @"DeviceGray";
case 3: return @"DeviceRGB";
case 4: return @"DeviceCMYK";
default: return nil;
}
}
else return name;
}
else return nil;
}
-(int)numberOfColours
{
id colourspace=[dict objectForKey:@"ColorSpace"];
if(!colourspace) return 0;
if(![colourspace isKindOfClass:[NSArray class]]) return 0;
if([colourspace count]!=4) return 0;
if(![[colourspace objectAtIndex:0] isEqual:@"Indexed"]) return 0;
return [[colourspace objectAtIndex:2] intValue]+1;
}
-(NSData *)paletteData
{
id colourspace=[dict objectForKey:@"ColorSpace"];
if(!colourspace) return nil;
if(![colourspace isKindOfClass:[NSArray class]]) return nil;
if([colourspace count]!=4) return nil;
if(![[colourspace objectAtIndex:0] isEqual:@"Indexed"]) return nil;
id palette=[colourspace objectAtIndex:3];
if([palette isKindOfClass:[PDFStream class]]) return [[palette handle] remainingFileContents];
else if([palette isKindOfClass:[PDFString class]]) return [palette data];
else return nil;
}
-(NSArray *)decodeArray
{
id decode=[dict objectForKey:@"Decode"];
if(!decode) return nil;
if(![decode isKindOfClass:[NSArray class]]) return nil;
int n;
if([self isGrey]||[self isMask]) n=1;
else if([self isRGB]||[self isLab]) n=3;
else if([self isCMYK]) n=4;
else return nil;
if([decode count]!=n*2) return nil;
return decode;
}
-(CSHandle *)rawHandle
{
return [fh subHandleFrom:offs length:[dict intValueForKey:@"Length" default:0]];
}
-(CSHandle *)handle
{
return [self handleExcludingLast:NO];
}
-(CSHandle *)JPEGHandle
{
//NSLog(@"%@",dict);
return [self handleExcludingLast:YES];
}
-(CSHandle *)handleExcludingLast:(BOOL)excludelast
{
CSHandle *handle;
PDFEncryptionHandler *encryption=[parser encryptionHandler];
if(encryption) handle=[encryption decryptStream:self];
else handle=[self rawHandle];
NSArray *filter=[dict arrayForKey:@"Filter"];
NSArray *decodeparms=[dict arrayForKey:@"DecodeParms"];
if(filter)
{
int count=[filter count];
if(excludelast) count--;
for(int i=0;i<count;i++)
{
handle=[self handleForFilterName:[filter objectAtIndex:i]
decodeParms:[decodeparms objectAtIndex:i] parentHandle:handle];
if(!handle) return nil;
}
}
return handle;
}
-(CSHandle *)handleForFilterName:(NSString *)filtername decodeParms:(NSDictionary *)decodeparms parentHandle:(CSHandle *)parent
{
if(!decodeparms) decodeparms=[NSDictionary dictionary];
if([filtername isEqual:@"FlateDecode"])
{
return [self predictorHandleForDecodeParms:decodeparms
parentHandle:[CSZlibHandle zlibHandleWithHandle:parent]];
}
else if([filtername isEqual:@"CCITTFaxDecode"])
{
int k=[decodeparms intValueForKey:@"K" default:0];
int cols=[decodeparms intValueForKey:@"Columns" default:1728];
int white=[decodeparms intValueForKey:@"BlackIs1" default:NO]?0:1;
if(k==0) return nil;
else if(k>0) return nil;
// if(k==0) return [[[CCITTFaxT41DHandle alloc] initWithHandle:parent columns:cols white:white] autorelease];
// else if(k>0) return [[[CCITTFaxT42DHandle alloc] initWithHandle:parent columns:cols white:white] autorelease];
else return [[[CCITTFaxT6Handle alloc] initWithHandle:parent columns:cols white:white] autorelease];
}
else if([filtername isEqual:@"LZWDecode"])
{
int early=[decodeparms intValueForKey:@"EarlyChange" default:1];
return [self predictorHandleForDecodeParms:decodeparms
parentHandle:[[[LZWHandle alloc] initWithHandle:parent earlyChange:early] autorelease]];
}
else if([filtername isEqual:@"ASCII85Decode"])
{
return [[[PDFASCII85Handle alloc] initWithHandle:parent] autorelease];
}
else if([filtername isEqual:@"Crypt"]) return parent; // handled elsewhere
return nil;
}
-(CSHandle *)predictorHandleForDecodeParms:(NSDictionary *)decodeparms parentHandle:(CSHandle *)parent
{
NSNumber *predictor=[decodeparms objectForKey:@"Predictor"];
if(!predictor) return parent;
int pred=[predictor intValue];
if(pred==1) return parent;
NSNumber *columns=[decodeparms objectForKey:@"Columns"];
NSNumber *colors=[decodeparms objectForKey:@"Colors"];
NSNumber *bitspercomponent=[decodeparms objectForKey:@"BitsPerComponent"];
int cols=columns?[columns intValue]:1;
int comps=colors?[colors intValue]:1;
int bpc=bitspercomponent?[bitspercomponent intValue]:8;
if(pred==2) return [[[PDFTIFFPredictorHandle alloc] initWithHandle:parent columns:cols components:comps bitsPerComponent:bpc] autorelease];
else if(pred>=10&&pred<=15) return [[[PDFPNGPredictorHandle alloc] initWithHandle:parent columns:cols components:comps bitsPerComponent:bpc] autorelease];
else [NSException raise:@"PDFStreamPredictorException" format:@"PDF Predictor %d not supported",pred];
return nil;
}
-(NSString *)description { return [NSString stringWithFormat:@"<Stream with dictionary: %@>",dict]; }
@end
@implementation PDFASCII85Handle
-(void)resetByteStream
{
finalbytes=0;
}
static uint8_t ASCII85NextByte(CSInputBuffer *input)
{
uint8_t b;
do { b=CSInputNextByte(input); }
while(!((b>=33&&b<=117)||b=='z'||b=='~'));
return b;
}
-(uint8_t)produceByteAtOffset:(off_t)pos
{
int byte=pos&3;
if(byte==0)
{
uint8_t c1=ASCII85NextByte(input);
if(c1=='z') val=0;
else if(c1=='~') CSByteStreamEOF(self);
else
{
uint8_t c2,c3,c4,c5;
c2=ASCII85NextByte(input);
if(c2!='~')
{
c3=ASCII85NextByte(input);
if(c3!='~')
{
c4=ASCII85NextByte(input);
if(c4!='~')
{
c5=ASCII85NextByte(input);
if(c5=='~') { c5=33; finalbytes=3; }
}
else { c4=c5=33; finalbytes=2; }
}
else { c3=c4=c5=33; finalbytes=1; }
}
else CSByteStreamEOF(self);
val=((((c1-33)*85+c2-33)*85+c3-33)*85+c4-33)*85+c5-33;
}
return val>>24;
}
else
{
if(finalbytes&&byte>=finalbytes) CSByteStreamEOF(self);
return val>>24-byte*8;
}
}
@end
@implementation PDFTIFFPredictorHandle
-(id)initWithHandle:(CSHandle *)handle columns:(int)columns
components:(int)components bitsPerComponent:(int)bitspercomp
{
if(self=[super initWithHandle:handle])
{
cols=columns;
comps=components;
bpc=bitspercomp;
if(bpc!=8) [NSException raise:@"PDFTIFFPredictorException" format:@"Bit depth %d not supported for TIFF predictor",bpc];
if(comps>4||comps<1) [NSException raise:@"PDFTIFFPredictorException" format:@"Color count %d not supported for TIFF predictor",bpc];
}
return self;
}
-(uint8_t)produceByteAtOffset:(off_t)pos
{
if(bpc==8)
{
int comp=pos%comps;
if((pos/comps)%cols==0) prev[comp]=CSInputNextByte(input);
else prev[comp]+=CSInputNextByte(input);
return prev[comp];
}
return 0;
}
@end
static inline int iabs(int a) { return a>=0?a:-a; }
@implementation PDFPNGPredictorHandle
-(id)initWithHandle:(CSHandle *)handle columns:(int)columns
components:(int)components bitsPerComponent:(int)bitspercomp
{
if(self=[super initWithHandle:handle])
{
cols=columns;
comps=components;
bpc=bitspercomp;
if(bpc<8) comps=1;
if(bpc>8) [NSException raise:@"PDFPNGPredictorException" format:@"Bit depth %d not supported for PNG predictor",bpc];
prevbuf=malloc(cols*comps+2*comps);
}
return self;
}
-(void)dealloc
{
free(prevbuf);
[super dealloc];
}
-(void)resetByteStream
{
memset(prevbuf,0,cols*comps+2*comps);
}
-(uint8_t)produceByteAtOffset:(off_t)pos
{
if(bpc<=8)
{
int row=pos/(cols*comps);
int col=pos%(cols*comps);
int buflen=cols*comps+2*comps;
int bufoffs=((col-comps*row)%buflen+buflen)%buflen;
if(col==0)
{
type=CSInputNextByte(input);
for(int i=0;i<comps;i++) prevbuf[(i+cols*comps+comps+bufoffs)%buflen]=0;
}
int x=CSInputNextByte(input);
int a=prevbuf[(cols*comps+comps+bufoffs)%buflen];
int b=prevbuf[(comps+bufoffs)%buflen];
int c=prevbuf[bufoffs];
int val;
switch(type)
{
case 0: val=x; break;
case 1: val=x+a; break;
case 2: val=x+b; break;
case 3: val=x+(a+b)/2; break;
case 4:
{
int p=a+b-c;
int pa=iabs(p-a);
int pb=iabs(p-b);
int pc=iabs(p-c);
if(pa<=b&&pa<=pc) val=pa;
else if(pb<=pc) val=pb;
else val=pc;
}
break;
}
prevbuf[bufoffs]=val;
return val;
}
return 0;
}
@end