https://github.com/zepouet/Xee-xCode-4.5/blob/master/XeePhotoshopLoader.m#L108 Skip to content Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories - + Security - * Team * Enterprise * Explore + Explore GitHub - Learn & contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Nonprofit - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} zepouet / Xee-xCode-4.5 * Watch 2 * Star 34 * Fork 5 * Code * Issues 1 * Pull requests 0 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights Permalink master Xee-xCode-4.5/XeePhotoshopLoader.m Go to file * Go to file T * Go to line L * * Copy path @zepouet zepouet initial commit Latest commit 102e567 Jan 27, 2013 History 1 contributor Users who have contributed to this file 496 lines (404 sloc) 14.3 KB Raw Blame #import "XeePhotoshopLoader.h" #import "Xee8BIMParser.h" #import "XeePhotoshopLayerParser.h" #import "XeeInterleavingHandle.h" #import "XeeRawImage.h" #import "XeeBitmapRawImage.h" #import "XeeIndexedRawImage.h" #import @implementation XeePhotoshopImage +(NSArray *)fileTypes { return [NSArray arrayWithObjects:@"psd",@"'8BPS'",nil]; } +(BOOL)canOpenFile:(NSString *)name firstBlock:(NSData *)block attributes:(NSDictionary *)attributes { uint8_t *header=(uint8_t *)[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_t colourlen=[fh readUInt32BE]; off_t resourceoffs=[fh offsetInFile]+colourlen; XeePalette *pal=nil; if(mode==XeePhotoshopIndexedMode&&colourlen>=768) { pal=[XeePalette palette]; uint8_t 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_t 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_t layermasklen=[fh readUInt32BE]; off_t imageoffs=[fh offsetInFile]+layermasklen; NSArray *layers=nil; BOOL hasalpha=NO; uint32_t layerlen=[fh readUInt32BE]; off_t maskoffs=[fh offsetInFile]+layerlen; if(layerlen>0) layers=[XeePhotoshopLayerParser parseLayersFromHandle:fh parentImage:self alphaFlag:&hasalpha]; [fh seekToFileOffset:maskoffs]; uint32_t masklen=[fh readUInt32BE]; [fh skipBytes:masklen]; while([fh offsetInFile]+12<=imageoffs) { uint32_t sign=[fh readUInt32BE]; uint32_t marker=[fh readUInt32BE]; uint32_t 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;i2&&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<=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(loadImage);*/ } -(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>8; } else { return curr&0xff; } } return 0; } @end * Copy lines * Copy permalink * View git blame * Reference in new issue [ ] Go * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.