PSD parser or how to parse a Photoshop file in Java
Somehow they suggested making a parser for the psd file. It seems to be very simple. It is necessary to find what layers the document consists of, to list the layers. If the layer is text, display the text and formatting options. Those. font names, sizes, indents, etc.
Where to begin? Google help us. A lot of information, a bunch of details on how to make plugins for Photoshop. But this is not it. We need a console program that will receive psd files as input and output a textual description of its insides. Installing Photoshop was not originally planned.
This document turned out to be the most useful: PhotoshopFileFormat - the actual specification of the photoshop file structure. A kind of byte-skeleton of the .psd file is exactly what the doctor ordered.
Then I googled on the subject of whether the colleagues in java did something like that? Yes, it turns out there is. Parser-psd - a brief description + sources there. Their program parses the file into layers, and saves them in separate files (!), Plus information on slicing is extracted. For my task it’s a bit different, but as an initial example it really helped.
The file is driven into a regular stream
The file itself consists of five main sections:
1. File header
2. Color mode data
3. Image resources
4. Layer and mask information
5. Image data
header has a fixed size, but other blocks can have different sizes. With this more closely. One byte past and the whole thing will be covered. The size is indicated in the first four bytes of the block, so blocks can be skipped if everything is not parsed
Here the main attention and strict adherence to the specification. Attention! From version to version, the format changes, in the specification these differences are indicated. However, the current specification, which I managed to find from 2010, and I had to use an even older description for the 2007th year. Therefore, when parsing the latest versions of files, you can miss.
Where to begin? Google help us. A lot of information, a bunch of details on how to make plugins for Photoshop. But this is not it. We need a console program that will receive psd files as input and output a textual description of its insides. Installing Photoshop was not originally planned.
This document turned out to be the most useful: PhotoshopFileFormat - the actual specification of the photoshop file structure. A kind of byte-skeleton of the .psd file is exactly what the doctor ordered.
Then I googled on the subject of whether the colleagues in java did something like that? Yes, it turns out there is. Parser-psd - a brief description + sources there. Their program parses the file into layers, and saves them in separate files (!), Plus information on slicing is extracted. For my task it’s a bit different, but as an initial example it really helped.
The file is driven into a regular stream
DataInputStream in = new DataInputStream(input)
and then we work with this stream by byte. The file itself consists of five main sections:
1. File header
2. Color mode data
3. Image resources
4. Layer and mask information
5. Image data
header has a fixed size, but other blocks can have different sizes. With this more closely. One byte past and the whole thing will be covered. The size is indicated in the first four bytes of the block, so blocks can be skipped if everything is not parsed
in.skipBytes(size)
. Here the main attention and strict adherence to the specification. Attention! From version to version, the format changes, in the specification these differences are indicated. However, the current specification, which I managed to find from 2010, and I had to use an even older description for the 2007th year. Therefore, when parsing the latest versions of files, you can miss.