For a recent Flex CMS project I needed PSD importer so I went googling in a hope for a decent PSD parser. I found only one somewhat usable project which was essenialy a Java port of this parser that imported psd files into flash..
The problem with this parser was in supported only very, basic, plain psd files, and adding anything (like a folder group for example ) would break it. So I decided to write my own one instead.
As you might have heard psd isn’t exactly the easiest format to parse, so rather then starting from the scratch I based a portion of the code on the Munegon’s parser, also referenced Yet another PSD parser by Jonas Beckeman written in java. Quite helfull was this Python PSD parser and of course an official PSd file format specifications document from Adobe.It was still lot of trial and error, as even official CS format specification docs aren’t complete and contains errors. But at the end I got this :
and this is the same file in Photoshop :
What is currently supported :
PSD FILE :
- Parsing canvas width x height
- parsing file color information (number of color channels, color depth, color mode)
- parsing file’s composite bitmap snapshot
- parsing all layers and layer folders
- parsing layer bitmap data
- parsing layers bounds and position
- parsing layer name
- parsing layer ID
- parsing layer blend mode
- parsing layer colour channels
- parsing layer alpha
- parsing layer filters
- parsing layer extra properties such as : isLocked, isVisible, clipping applied
- parsing layer type (normal, folder)
What is not supported but planned for the future :
- layer masks
- layer paths
- layer vector shapes
- parsing text layers as formatted string
- parsing layers with zip (with or without prediction) compression
As photshop != flash, there are , and will be some necessary incompatibilities. Although I tried to support all the crossover features, not everything will look the same after import
here are some gotchas :
- Layer alpha in flash is layer opacity in Photoshop..Layer Fill values are ignored.
- Incompatible photoshop layer blend modes are interpreted as Normal blend mode
- Only 4 filters / layer effects are currently supported (drop shadow , inner drop shadow , glow, inner glow) but even these need to have to be applied with normal blend mode, as flash doesn’t support a filters with a different blend mode as the display object they are applied to. For example it’s perfectly possible to have an photoshop layer in screen mode with drop shadow applied in multiply mode, but it flash you don’t have a blend mode settings for a filter..
- only layers with RAW or RLE compression are being parsed at the moment.. So if you don’t see the layer bitmap data it’s probably compressed with zip compression.
Note on the layer folders / layer groups :
Layer groups are being parsed and they are also PSDLayer class type.
To identify them you need to check for the layer type:
There are 4 layer types :LayerType_FOLDER_OPEN, LayerType_FOLDER_CLOSED , LayerType_HIDDEN and LayerType_NORMAL.
Layer folder hidden is marker for the end of the layer group. So if you want to parse the folder structure, check where the layer type folder starts and then every layer that follows is inside of that folder, until you reach layer type hidden.
How to use this parser
Very simple. You just create instance of PSDParser (it is Singleton) and then call “parse” method , passing the content of your psd file in byte array format.
The parsing is synchronous so after that line, you will already have all the file/layers info available..
I’ve made 2 apps that should help you get started.
PSD Viewer is a simple flex app that allows you to load and view psd files and reads the supported layers, while showing their blend modes, visibility , lock, alpha , layer effect etc, in “Photoshop-esque” style. View source is enabled so you can get the source code from here. (If you don’t have the psd file to test use the “testPSD1.psd” file from flex project’s “assets” subfolder).
Simple example is a basic single class as3 app that just loads the psd file and then on click cycles through the layers, bringing the one in the back to the top.
PS: As this is still early beta, some PSD files may/will break the parser (especially those with unsupported features (see above) or those not saved in compatibility mode…