My favorites | Sign in
Logo
xee
                
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#import "XeePhotoshopLoader.h"
#import "Xee8BIMParser.h"
#import "XeePhotoshopLayerParser.h"

#import "XeeInterleavingHandle.h"
#import "XeeRawImage.h"
#import "XeeBitmapRawImage.h"
#import "XeeIndexedRawImage.h"
#import "CSRegex.h"



@implementation XeePhotoshopImage

+(NSArray *)fileTypes
{
return [NSArray arrayWithObjects:@"psd",@"'8BPS'",nil];
}

+(BOOL)canOpenFile:(NSString *)name firstBlock:(NSData *)block attributes:(NSDictionary *)attributes
{
uint8 *header=(uint8 *)[block bytes];
if([block length]>6&&XeeBEUInt32(header)=='8BPS'&&XeeBEUInt16(header+4)==1) return YES;
return NO;
}

-(id)init
{
if(self=[super init])
{
}
return self;
}

-(void)dealloc
{
[super dealloc];
}

-(SEL)initLoader
{
CSHandle *fh=[self handle];

[fh skipBytes:12];

channels=[fh readUInt16BE];
height=[fh readUInt32BE];
width=[fh readUInt32BE];
bitdepth=[fh readUInt16BE];
mode=[fh readUInt16BE];

// Colour data section
uint32 colourlen=[fh readUInt32BE];
off_t resourceoffs=[fh offsetInFile]+colourlen;

XeePalette *pal=nil;
if(mode==XeePhotoshopIndexedMode&&colourlen>=768)
{
pal=[[[XeePalette alloc] init] autorelease];
uint8 palbuf[768];
[fh readBytes:768 toBuffer:palbuf];
for(int i=0;i<256;i++)
[pal setColourAtIndex:i red:palbuf[i] green:palbuf[i+256] blue:palbuf[i+512]];
}


// Resources section
[fh seekToFileOffset:resourceoffs];
uint32 resourcelen=[fh readUInt32BE];
off_t layermaskoffs=[fh offsetInFile]+resourcelen;

Xee8BIMParser *parser=[[Xee8BIMParser alloc] initWithHandle:fh];

NSArray *metaprops=[parser propertyArrayWithPhotoshopFirst:YES];
BOOL hasmerged=[parser hasMergedImage];
int numcols=[parser numberOfIndexedColours];
int trans=[parser indexOfTransparentColour];
if(trans>=0) [pal setTransparent:trans];

[parser release];



// Layers section
[fh seekToFileOffset:layermaskoffs];
uint32 layermasklen=[fh readUInt32BE];
off_t imageoffs=[fh offsetInFile]+layermasklen;

NSArray *layers=nil;
BOOL hasalpha=NO;

uint32 layerlen=[fh readUInt32BE];
off_t maskoffs=[fh offsetInFile]+layerlen;

if(layerlen>0) layers=[XeePhotoshopLayerParser parseLayersFromHandle:fh parentImage:self alphaFlag:&hasalpha];

[fh seekToFileOffset:maskoffs];
uint32 masklen=[fh readUInt32BE];
[fh skipBytes:masklen];

while([fh offsetInFile]+12<=imageoffs)
{
uint32 sign=[fh readUInt32BE];
uint32 marker=[fh readUInt32BE];
uint32 chunklen=[fh readUInt32BE];
off_t nextchunk=[fh offsetInFile]+((chunklen+3)&~3);
// At this point, I'd like to take a moment to speak to you about the Adobe PSD format.
// PSD is not a good format. PSD is not even a bad format. Calling it such would be an
// insult to other bad formats, such as PCX or JPEG. No, PSD is an abysmal format. Having
// worked on this code for several weeks now, my hate for PSD has grown to a raging fire
// that burns with the fierce passion of a million suns.
// If there are two different ways of doing something, PSD will do both, in different
// places. It will then make up three more ways no sane human would think of, and do those
// too. PSD makes inconsistency an art form. Why, for instance, did it suddenly decide
// that *these* particular chunks should be aligned to four bytes, and that this alignement
// should *not* be included in the size? Other chunks in other places are either unaligned,
// or aligned with the alignment included in the size. Here, though, it is not included.
// Either one of these three behaviours would be fine. A sane format would pick one. PSD,
// of course, uses all three, and more.
// Trying to get data out of a PSD file is like trying to find something in the attic of
// your eccentric old uncle who died in a freak freshwater shark attack on his 58th
// birthday. That last detail may not be important for the purposes of the simile, but
// at this point I am spending a lot of time imagining amusing fates for the people
// responsible for this Rube Goldberg of a file format.
// Earlier, I tried to get a hold of the latest specs for the PSD file format. To do this,
// I had to apply to them for permission to apply to them to have them consider sending
// me this sacred tome. This would have involved faxing them a copy of some document or
// other, probably signed in blood. I can only imagine that they make this process so
// difficult because they are intensely ashamed of having created this abomination. I
// was naturally not gullible enough to go through with this procedure, but if I had done
// so, I would have printed out every single page of the spec, and set them all on fire.
// Were it within my power, I would gather every single copy of those specs, and launch
// them on a spaceship directly into the sun.
//
// PSD is not my favourite file format.

if(sign!='8BIM') break; // sanity check

switch(marker)
{
case 'Lr16':
layers=[XeePhotoshopLayerParser parseLayersFromHandle:fh parentImage:self alphaFlag:NULL];
break;

case 'Mt16':
hasalpha=YES;
break;

case 'Anno':
{
if([fh readUInt16BE]!=2) break;
if([fh readUInt16BE]!=1) break;

int numanno=[fh readUInt32BE];
NSMutableArray *annotations=[NSMutableArray array];

for(int i=0;i<numanno;i++)
{
uint32 annolen=[fh readUInt32BE];
off_t nextanno=[fh offsetInFile]+annolen-4;
if([fh readUInt32BE]=='txtA')
{
[fh skipBytes:46];
int len=[fh readUInt8];
[fh skipBytes:len+((len&1)^1)];
len=[fh readUInt8];
[fh skipBytes:len+((len&1)^1)];
len=[fh readUInt8];
NSString *datestr=[[[NSString alloc] initWithData:[fh readDataOfLength:len] encoding:NSISOLatin1StringEncoding] autorelease];
[fh skipBytes:((len&1)^1)+4];

NSCalendarDate *date=nil;
NSArray *matches=[datestr substringsCapturedByPattern:@"^D:([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([+-])([0-9]{2})'([0-9]{2})'$"];
if(matches)
{
int year=[[matches objectAtIndex:1] intValue];
int month=[[matches objectAtIndex:2] intValue];
int day=[[matches objectAtIndex:3] intValue];
int hour=[[matches objectAtIndex:4] intValue];
int minute=[[matches objectAtIndex:5] intValue];
int second=[[matches objectAtIndex:6] intValue];
int tzmult=[[matches objectAtIndex:7] isEqual:@"-"]?-1:1;
int tzhour=[[matches objectAtIndex:8] intValue];
int tzmin=[[matches objectAtIndex:9] intValue];

NSTimeZone *tz=[NSTimeZone timeZoneForSecondsFromGMT:tzmult*(tzhour*60+tzmin)*60];
date=[NSCalendarDate dateWithYear:year month:month day:day hour:hour minute:minute second:second timeZone:tz];
}

if([fh readUInt32BE]=='txtC')
{
len=[fh readUInt32BE];
NSData *annodata=[fh readDataOfLength:len];
const uint8 *annobytes=[annodata bytes];

NSString *str;
if(len>2&&annobytes[0]==0xfe&&annobytes[1]==0xff)
str=[[[NSString alloc] initWithBytes:annobytes+2 length:len-2
encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF16BE)] autorelease];
else str=[[[NSString alloc] initWithData:annodata encoding:NSISOLatin1StringEncoding] autorelease];

[annotations addObjectsFromArray:[XeePropertyItem itemsWithLabel:
NSLocalizedString(@"Annotation",@"Photoshop annotation property title")
textValue:str]];
[annotations addObject:[XeePropertyItem itemWithLabel:
NSLocalizedString(@"Added at",@"Photoshop annotation date property title")
value:date]];
}
}
[fh seekToFileOffset:nextanno];
}
if(numanno) [properties addObject:[XeePropertyItem itemWithLabel:
NSLocalizedString(@"Photoshop annotations",@"Photoshop annotations section title")
value:annotations identifier:@"psanno"]];
}
break;
}

[fh seekToFileOffset:nextchunk];
}


switch(mode)
{
case XeePhotoshopBitmapMode: [self setDepthBitmap]; break;
case XeePhotoshopGreyscaleMode: [self setDepthGrey:bitdepth alpha:hasalpha floating:bitdepth==32?YES:NO]; break;
case XeePhotoshopIndexedMode: [self setDepthIndexed:numcols?numcols:1<<bitdepth]; break;
case XeePhotoshopRGBMode: [self setDepthRGB:bitdepth alpha:hasalpha floating:bitdepth==32?YES:NO]; break;
case XeePhotoshopCMYKMode: [self setDepthCMYK:bitdepth alpha:hasalpha]; break;
case XeePhotoshopLabMode: [self setDepthLab:bitdepth alpha:hasalpha]; break;

case XeePhotoshopMultichannelMode:
[self setDepth:
[NSString stringWithFormat:NSLocalizedString(@"%d bits multichannel",@"Description for multichannel (Photoshop) images"),bitdepth]
iconName:@"depth_grey"];
break;

case XeePhotoshopDuotoneMode:
[self setDepth:
[NSString stringWithFormat:NSLocalizedString(@"%d bits duotone",@"Description for duotone (Photoshop) images"),bitdepth]
iconName:@"depth_rgb"];
break;
}

[properties addObjectsFromArray:metaprops];

// Image section

if(hasmerged)
{
[fh seekToFileOffset:imageoffs];

XeeImage *mainimage=nil;
switch(mode)
{
case XeePhotoshopBitmapMode:
mainimage=[[[XeeBitmapRawImage alloc] initWithHandle:[self handleForNumberOfChannels:1 alpha:NO]
width:width height:height parentImage:self] autorelease];
break;

case XeePhotoshopIndexedMode:
mainimage=[[[XeeIndexedRawImage alloc] initWithHandle:[self handleForNumberOfChannels:1 alpha:NO]
width:width height:height palette:pal parentImage:self] autorelease];
break;

case XeePhotoshopGreyscaleMode:
case XeePhotoshopDuotoneMode:
mainimage=[[[XeeRawImage alloc] initWithHandle:[self handleForNumberOfChannels:1 alpha:hasalpha]
width:width height:height depth:bitdepth colourSpace:XeeGreyRawColourSpace
flags:XeeBigEndianRawFlag|XeeAlphaPrecomposedRawFlag|(hasalpha?XeeAlphaLastRawFlag:0)|(bitdepth==32?XeeFloatingPointRawFlag:0)
parentImage:self] autorelease];
break;

case XeePhotoshopRGBMode:
mainimage=[[[XeeRawImage alloc] initWithHandle:[self handleForNumberOfChannels:3 alpha:hasalpha]
width:width height:height depth:bitdepth colourSpace:XeeRGBRawColourSpace
flags:XeeBigEndianRawFlag|XeeAlphaPrecomposedRawFlag|(hasalpha?XeeAlphaLastRawFlag:0)|(bitdepth==32?XeeFloatingPointRawFlag:0)
parentImage:self] autorelease];
break;

case XeePhotoshopCMYKMode:
mainimage=[[[XeeRawImage alloc] initWithHandle:[self handleForNumberOfChannels:4 alpha:hasalpha]
width:width height:height depth:bitdepth colourSpace:XeeCMYKRawColourSpace
flags:XeeBigEndianRawFlag|XeeAlphaPrecomposedRawFlag|(hasalpha?XeeAlphaLastRawFlag:0)
parentImage:self] autorelease];
break;

case XeePhotoshopLabMode:
mainimage=[[[XeeRawImage alloc] initWithHandle:[self handleForNumberOfChannels:3 alpha:hasalpha]
width:width height:height depth:bitdepth colourSpace:XeeLabRawColourSpace
flags:XeeBigEndianRawFlag|XeeAlphaPrecomposedRawFlag|(hasalpha?XeeAlphaLastRawFlag:0)
parentImage:self] autorelease];
break;
}
}

NSEnumerator *enumerator=[layers objectEnumerator];
XeePhotoshopLayerParser *layer;
while(layer=[enumerator nextObject])
{
/*XeeImage *image=*/[layer image];
}

[self setFormat:@"PSD"];

loadersel=NULL;
loaderframe=-1;

return @selector(load);
}

-(void)deallocLoader
{
}

-(SEL)load
{
if(!loadersel)
{
if(loaderframe>=0) [[subimages objectAtIndex:loaderframe] deallocLoader];
loaderframe++;
if(loaderframe>=[self frames])
{
loaded=YES;
return NULL;
}
loadersel=@selector(initLoader);
}
loadersel=(SEL)[[subimages objectAtIndex:loaderframe] performSelector:loadersel];
return @selector(load);
}

-(CSHandle *)handleForNumberOfChannels:(int)requiredchannels alpha:(BOOL)alpha;
{
int numchannels=requiredchannels+(alpha?1:0);
if(numchannels>channels) return nil;

NSMutableArray *array=[NSMutableArray array];
off_t totalsize=0;
int bpr=(bitdepth*width+7)/8;

int compression=[handle readUInt16BE];
switch(compression)
{
case 0:
for(int i=0;i<numchannels;i++)
{
CSFileHandle *fh=[[handle copy] autorelease];
[fh skipBytes:i*bpr*height];
[array addObject:fh];
}
break;

case 1:
for(int i=0;i<numchannels;i++)
{
XeePackbitsHandle *ph=[[[XeePackbitsHandle alloc] initWithHandle:[[handle copy] autorelease]
rows:height bytesPerRow:bpr channel:i of:channels previousSize:totalsize] autorelease];
totalsize+=[ph totalSize];
[array addObject:ph];
}
break;
}

if(numchannels==1) return [array objectAtIndex:0];
else return [[[XeeInterleavingHandle alloc] initWithHandles:array elementSize:bitdepth] autorelease];
}

-(int)bitDepth { return bitdepth; }

-(int)mode { return mode; }

@end




@implementation XeePackbitsHandle

-(id)initWithHandle:(CSHandle *)handle rows:(int)numrows bytesPerRow:(int)bpr channel:(int)channel of:(int)numchannels previousSize:(off_t)prevsize
{
if(self=[super initWithName:[handle name]])
{
parent=[handle retain];
readatmost_ptr=(int (*)(id,SEL,int,void *))[parent methodForSelector:@selector(readAtMost:toBuffer:)];

rows=numrows;
bytesperrow=bpr;

pos=0;
totalsize=0;
offsets=malloc(sizeof(off_t)*rows);
off_t firstrow=[handle offsetInFile]+numchannels*rows*2+prevsize;

[parent skipBytes:2*rows*channel];
for(int i=0;i<rows;i++)
{
offsets[i]=firstrow+totalsize;
totalsize+=[parent readUInt16BE];
}
}
return self;
}

-(void)dealloc
{
free(offsets);
[parent release];
[super dealloc];
}


-(off_t)offsetInFile { return pos; }

-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
uint8 *ptr=buffer;
int left=num;

while(left)
{
if(pos%bytesperrow==0)
{
[parent seekToFileOffset:offsets[pos/bytesperrow]];
spanleft=0;
}

if(!spanleft)
{
uint8 b;
//do {
if(readatmost_ptr(parent,@selector(readAtMost:toBuffer:),1,&b)!=1) goto end;
//} while(b==0xff);

if(b&0x80)
{
spanleft=(b^0xff)+2;
if(readatmost_ptr(parent,@selector(readAtMost:toBuffer:),1,&spanbyte)!=1) goto end;
literal=NO;
}
else
{
spanleft=b+1;
literal=YES;
}
}

if(literal)
{
if(readatmost_ptr(parent,@selector(readAtMost:toBuffer:),1,ptr++)!=1) goto end;
}
else
{
*ptr++=spanbyte;
}

spanleft--;
left--;
pos++;
}

end:
return num-left;
}

-(off_t)totalSize { return totalsize; }

@end



@implementation XeeDeltaHandle

-(id)initWithHandle:(CSHandle *)handle depth:(int)bitdepth columns:(int)columns
{
if(self=[super initWithName:[handle name]])
{
parent=[handle retain];
// readatmost_ptr=(int (*)(id,SEL,int,void *))[parent methodForSelector:@selector(readAtMost:toBuffer:)];

depth=bitdepth;
cols=columns;
curr=0;
}
return self;
}

-(void)dealloc
{
[parent release];
[super dealloc];
}

-(off_t)offsetInFile { return [parent offsetInFile]; }

-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
off_t start=[parent offsetInFile];
int actual=[parent readAtMost:num toBuffer:buffer];

if(depth==16)
{
uint8 *ptr=(uint8 *)buffer;
int first=(start/2)%cols;

for(int i=0;i<actual/2;i++)
{
if((first+i)%cols==0) curr=XeeBEUInt16(ptr);
else XeeSetBEUInt16(ptr,curr+=XeeBEUInt16(ptr));
ptr+=2;
}
}

return actual;
}

@end
Show details Hide details

Change log

r11 by paracelsus on Sep 11, 2007   Diff
Photoshop loader is DONE for now, fuck you
Adobe
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by paracelsus on Sep 04, 2007   Diff
Photoshop loader almost finished,
CSCoroutine created.
r3 by paracelsus on Jan 30, 2007   Diff
Initial check-in
All revisions of this file

File info

Size: 15142 bytes, 517 lines
Powered by Google Project Hosting