Click here to Skip to main content
11,479,486 members (46,463 online)
Click here to Skip to main content

Yet Another PSD Parser

, 10 Oct 2006 199.9K 5.7K 65
Rate this:
Please Sign up or sign in to vote.
Read/write Photoshop PSD files, serialize to XML, extract layer bitmaps, and more.

Main window

A simple host application for testing the API. Main window: Drag'n'drop a PSD file, select which layer to inspect (or Global for the merged image and global data).

Xml window

XML inspector: For viewing all data in the document. Shows info about the selected layer or global info.

Introduction

This is a work in progress that aims to give .NET developers full control over Photoshop PSD files. All features aren't covered by the library, and there are, without doubt, some documents it will choke on, but with good feedback from users, this will hopefully be fixed over time.

The main usage of the API is for reading and writing PSD files from your own applications. It can also be used as a reference for writing parsers for non-.NET languages. Thanks to the XML serializing feature, it can easily be used for investigating unknown tags (or for implementing a zip-style file format; XML settings plus PNG files for channel data).

Examples of PSD sections it can handle:

  • Individual layers, including extracting their bitmaps, transparencies and masks
  • Global channels
  • Slices, Grid/Guides, Thumbnails
  • Vector Paths
  • Layer effects (shadows, glows etc.)
  • Adjustment layers (Brightness/Contrast, Curves, Levels etc.)
  • Text layers (well, not really; there's an ocean of data for a simple text layer, and I don't know what half of it is for, but strictly speaking they're being parsed)

Note that it is not a PSD renderer; this library is for manipulating the files themselves. I will implement the most important rendering features in PaintLab, and possibly make it available as a DLL for immediate rendering (i.e. without opening the paint app itself).

Background

A few years back, I wanted to decode parts of the PSD format in order to speed up the production pipeline for a project. I looked at the available parsers, both commercial and open source, but none of them had the features I needed, nor a design I felt comfortable with, so I decided to write my own by using the SDK. But after finding out how closely Adobe guarded the secrets of their format, and that I wouldn't be able to sign their harsh SDK license agreement, I knew I would have to do it the hard way.

Reverse engineering is a tedious process (thus slow), and with the project where I needed the parser gone by, I didn't put much time on it.

Then I started working on the paint application PaintLab, and I realized that PSD support was probably one of the most important features - without it, there is no chance of Photoshop users even considering using the program. So, instead of working with the interesting features of PaintLab, I've had to put a fair amount of time into examining the PSD format, while cursing the fact that such a widely used format has no decent open source codec. I hope my work here can save other people's time, and help them being creative instead of dealing with the boring details of a file format.

With this project, I think I've managed to create a decent architecture, but there's still work to be done before it has a 100% compatibility with PSD files.

Using the code

As the code is part of the Endogine project, and uses several of the its classes, Endogine.dll has to be available for referencing. The host app also uses Endogine.Editors.dll for the XML editor.

The API is currently a pretty straightforward reflection of the PSD file structure:

using Endogine.Codecs.Photoshop;
using Endogine.Codecs.Photoshop.ImageResources;
using Endogine.Codecs.Photoshop.LayerResources;

Document psd = new Document("somefile.psd");

int documentHeight = psd.Header.Rows;
//get the height of the document

//get GridGuidesInfo ImageResource
GridGuidesInfo guidesInfo = 
  (GridGuidesInfo)psd.GetResource(typeof(GridGuidesInfo));
Console.WriteLine("Guide location: " + 
        guidesInfo.Guides[0].LocationInPixels);

Bitmap bmp = psd.Layers[0].Bitmap;
//get layer 0 bitmap

//get transparency bitmap of layer 1
Bitmap bmpChannel = psd.Layers[1].Channels[-1].Bitmap;
BrightnessContrast bc = (BrightnessContrast)
   psd.Layers[2].GetResource(typeof(BrightnessContrast));
Console.WriteLine("Layer 2 (Adjustment layer) Brighness: " + 
                  bc.Brightness);

psd.SaveXml("somefile.xml", true);
//save as XML file, and export layer bitmaps as PNGs

Points of Interest

The newer Photoshop SDKs, which include the file format specifications, are protected by a license agreement that compromises the usability of code produced by anyone who has signed it. That's why I've had to use reverse engineering for creating many parts of the code, and why it's taking so long to complete it.

If you want to contribute to the project, please understand that I won't accept code from anyone who has signed an Adobe SDK agreement. That could potentially mean trouble ahead, the day evil Adobe wants to shut down this subversive operation.

If the program fails to load a document, please add a note about it in the comments section below and keep the file around - if I have time, I'll ask you to send it to me for closer inspection. Note that I currently only have interest in documents created with Photoshop version 6 or later.

TODO

  • Understand the properties of text layer tags (TySh and Txt2). They seem extremely bloated; help with these would be very welcome.
  • Better access of data in Descriptor tags
  • Implement write functionality in all classes
  • Make sure it always writes correct PSD files
  • Deserialize from XML / PNGs
  • Implement ICC parser
  • Investigate what the tags marked as Unknown are for
  • Error handling

So far, I've only cared about RGB, 8-bit per channel images. Other formats are implicitly supported (channel data is treated the same way), but there's no specific functionality for them, like converting to RGB color space. Should you need color space conversion, there are classes for that in the Endogine.ColorEx namespace.

License

This project uses the BSD license. You are free to use the code or the binaries however you want, including commercially, as long as the copyright notice and license text are included as specified in LICENSE.txt (comes with the zip files). 

History

  • 2006-10-08 - First draft of the library.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jonas Beckeman
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

 
BugNull reference exception when opening [2014] Pin
tlhintoq17-Dec-14 6:25
membertlhintoq17-Dec-14 6:25 
Questionits not working Pin
Member 1084693014-Jul-14 20:31
memberMember 1084693014-Jul-14 20:31 
Questionquestion about the photoshop version Pin
Member 1076806512-May-14 19:29
memberMember 1076806512-May-14 19:29 
QuestionThanks Pin
Milad.D28-Aug-12 20:15
memberMilad.D28-Aug-12 20:15 
QuestionMaybe Broken Pin
Benjamin Gatti3-May-12 12:12
memberBenjamin Gatti3-May-12 12:12 
Questionwhy the downloaded psdparser Project File becomes a LogOn.aspx file Pin
ouyang_3721-Feb-12 23:32
memberouyang_3721-Feb-12 23:32 
QuestionThere is an alternative [modified] Pin
papirosnik12-Feb-12 4:27
memberpapirosnik12-Feb-12 4:27 
AnswerRe: There is an alternative Pin
tamilpuyal_2817-Oct-12 1:12
membertamilpuyal_2817-Oct-12 1:12 
QuestionSupport for MonoDevelop / Linux? Pin
Andreas Hennings13-Sep-11 5:21
memberAndreas Hennings13-Sep-11 5:21 
AnswerRe: Support for MonoDevelop / Linux? Pin
Member 161958917-Jan-12 20:57
memberMember 161958917-Jan-12 20:57 
Question"Call target happened abnormally " Pin
yan_jason22-Feb-11 17:41
memberyan_jason22-Feb-11 17:41 
Questionwhy don't working this program? [modified] Pin
ggam12346-Jul-10 21:45
memberggam12346-Jul-10 21:45 
GeneralHelp... DLL issue Pin
Juan5-Feb-10 10:25
memberJuan5-Feb-10 10:25 
GeneralRe: Help... DLL issue Pin
Juan5-Feb-10 10:54
memberJuan5-Feb-10 10:54 
Generalmy parser handles descriptor based data v6+ type tool, effects layers, etc) Pin
qu1j0t330-Nov-09 12:56
memberqu1j0t330-Nov-09 12:56 
QuestionPSD color mode Pin
sonce15-Sep-09 7:13
membersonce15-Sep-09 7:13 
GeneralRasterize Pin
rammjuve26-Aug-09 5:31
memberrammjuve26-Aug-09 5:31 
GeneralDemo Not working for 3 PSD files. Pin
JEGsBOND17-Sep-08 6:21
memberJEGsBOND17-Sep-08 6:21 
GeneralRe: Demo Not working for 3 PSD files. Pin
walky26-Dec-08 6:20
memberwalky26-Dec-08 6:20 
GeneralRe: Demo Not working for 3 PSD files. Pin
JEGsBOND26-Dec-08 10:19
memberJEGsBOND26-Dec-08 10:19 
QuestionHow can i get the version of PSD Pin
Feng.h12-Aug-08 21:01
memberFeng.h12-Aug-08 21:01 
GeneralAdd one more layer to the existing PSD File Pin
kr_suresh12-Aug-08 19:49
memberkr_suresh12-Aug-08 19:49 
GeneralError : Unable to read beyond the end of the stream. Pin
my4color17-May-08 1:02
membermy4color17-May-08 1:02 
GeneralLayer Object offsets Pin
anthonyfg17-Apr-08 12:42
memberanthonyfg17-Apr-08 12:42 
GeneralAbout PSD format Pin
Member 19817024-Feb-08 21:42
memberMember 19817024-Feb-08 21:42 
GeneralCan't open Photoshop CS3 file Pin
Member 233150730-Dec-07 10:00
memberMember 233150730-Dec-07 10:00 
GeneralRe: Can't open Photoshop CS3 file Pin
Jonas Beckeman16-Jan-08 8:59
memberJonas Beckeman16-Jan-08 8:59 
GeneralRe: Can't open Photoshop CS3 file Pin
Member 233150725-Jan-08 3:15
memberMember 233150725-Jan-08 3:15 
GeneralRe: Can't open Photoshop CS3 file Pin
Member 1076806512-May-14 23:56
memberMember 1076806512-May-14 23:56 
Generalplease any one help me Pin
MostafaSayed12-Dec-07 2:47
memberMostafaSayed12-Dec-07 2:47 
GeneralA qustion please Pin
MostafaSayed9-Dec-07 2:46
memberMostafaSayed9-Dec-07 2:46 
QuestionHow can I get textlayer angle value with your dll Pin
guanxiaohui4-Nov-07 22:37
memberguanxiaohui4-Nov-07 22:37 
QuestionNeed Help File for PSD Parser DLL's Pin
JP John23-Oct-07 2:58
memberJP John23-Oct-07 2:58 
AnswerRe: Need Help File for PSD Parser DLL's Pin
Jonas Beckeman29-Oct-07 11:21
memberJonas Beckeman29-Oct-07 11:21 
GeneralNeed ASP.NET PSD Parser DLL's Pin
JP John23-Oct-07 2:51
memberJP John23-Oct-07 2:51 
QuestionGreat Work Pin
afaz20-Sep-07 1:39
memberafaz20-Sep-07 1:39 
AnswerRe: Great Work Pin
Jonas Beckeman29-Oct-07 11:28
memberJonas Beckeman29-Oct-07 11:28 
GeneralSaving files Pin
jdjensenAZ29-Aug-07 8:57
memberjdjensenAZ29-Aug-07 8:57 
GeneralRe: Saving files Pin
JP John23-Oct-07 3:18
memberJP John23-Oct-07 3:18 
GeneralRe: Saving files Pin
Matteo Pietri13-Feb-08 0:04
memberMatteo Pietri13-Feb-08 0:04 
QuestionCan't find layersets Pin
Marc Delahousse25-May-07 12:07
memberMarc Delahousse25-May-07 12:07 
AnswerRe: Can't find layersets Pin
Jonas Beckeman29-Oct-07 11:32
memberJonas Beckeman29-Oct-07 11:32 
GeneralVery Great! and another question Pin
domap17-May-07 18:11
memberdomap17-May-07 18:11 
GeneralRe: Very Great! and another question Pin
Jonas Beckeman18-May-07 6:50
memberJonas Beckeman18-May-07 6:50 
QuestionGREAT WORK, have 1 Question.. Pin
NetDevelopment3-Apr-07 7:24
memberNetDevelopment3-Apr-07 7:24 
AnswerRe: GREAT WORK, have 1 Question.. Pin
NetDevelopment3-Apr-07 7:33
memberNetDevelopment3-Apr-07 7:33 
GeneralRe: GREAT WORK, have 1 Question.. Pin
Jonas Beckeman3-Apr-07 7:49
memberJonas Beckeman3-Apr-07 7:49 
AnswerRe: GREAT WORK, have 1 Question.. Pin
Jonas Beckeman3-Apr-07 7:42
memberJonas Beckeman3-Apr-07 7:42 
GeneralDamn cool Pin
Quartz.2-Apr-07 14:35
memberQuartz.2-Apr-07 14:35 
GeneralRe: Damn cool Pin
Jonas Beckeman3-Apr-07 7:32
memberJonas Beckeman3-Apr-07 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 10 Oct 2006
Article Copyright 2006 by Jonas Beckeman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid