Reading/processing a Photshop document from a C++ program

O
Posted By
Olumide
Oct 11, 2006
Views
2459
Replies
21
Status
Closed
Hello –

The C++ program I’m currently writing needs to be able to read photshop documents (PSD images), and extract some information from each layer. How would one approach writing such a program? (I have not done any photoshop pluging development programming, I have some experience with graphics APIs.)

I have the Photoshop SDK, but it appears to concentrate almost exclusively on plugin development – or so it seems.

Thanks,

– Olumide

How to Improve Photoshop Performance

Learn how to optimize Photoshop for maximum speed, troubleshoot common issues, and keep your projects organized so that you can work faster than ever before!

MR
Mike Russell
Oct 11, 2006
"Olumide" wrote in message
The C++ program I’m currently writing needs to be able to read photshop documents (PSD images), and extract some information from each layer. How would one approach writing such a program? (I have not done any photoshop pluging development programming, I have some experience with graphics APIs.)

I have the Photoshop SDK, but it appears to concentrate almost exclusively on plugin development – or so it seems.

See what Toby and others have to say here, but here’s my take.

If your software will be running on a Windows with Photoshop installed, you can use Photoshop’s COM interface to open a document in Photoshop and access layer information via COM objects. This is by far the easiest way to go, and you may have something up in a matter of days. This information is available in the Scripting SDK, which is a free download from www.adobe.com.

For a stand alone program that runs outside of Photoshop, you’ll need to dig a little deeper. Join the Adobe Solution Network, and get the file format SDK, and parse the file yourself. Another alternative would be to get a library from someone who has done this already. Here we’re talking a matter of a week or two for an experienced person, perhaps longer for someone just starting out with Photoshop. Reading the psd file format can be messy because of compatibility issues. You can simplify the job by requiring that the psd’s be created by a certain version of Photoshop.

Mike Russell
www.curvemeister.com/forum/
O
Olumide
Oct 11, 2006
Mike Russell wrote:
If your software will be running on a Windows with Photoshop installed, you can use Photoshop’s COM interface to open a document in Photoshop and access layer information via COM objects. This is by far the easiest way to go, and you may have something up in a matter of days. This information is available in the Scripting SDK, which is a free download from www.adobe.com.
For a stand alone program that runs outside of Photoshop, you’ll need to dig a little deeper. Join the Adobe Solution Network, and get the file format SDK, and parse the file yourself.

Thanks, Mike. I already have the SDK, and I was puzzled as to why chapter (number 9) on the format module was missing from the API guide PDF document (as is the File Formats documentation that the same guide refers to in chapter 1).

I really don’t want to be tied to the photoshop app, all I need is to be able to parse a PSD file. Maybe its time to consider the gimp — after all, it too supports layers.

– OLumide
T
toby
Oct 11, 2006
Mike Russell wrote:
"Olumide" wrote in message
The C++ program I’m currently writing needs to be able to read photshop documents (PSD images), and extract some information from each layer. How would one approach writing such a program? (I have not done any photoshop pluging development programming, I have some experience with graphics APIs.)

I have the Photoshop SDK, but it appears to concentrate almost exclusively on plugin development – or so it seems.

See what Toby and others have to say here, but here’s my take.
If your software will be running on a Windows with Photoshop installed, you can use Photoshop’s COM interface to open a document in Photoshop and access layer information via COM objects. This is by far the easiest way to go, and you may have something up in a matter of days. This information is available in the Scripting SDK, which is a free download from www.adobe.com.
For a stand alone program that runs outside of Photoshop, you’ll need to dig a little deeper. Join the Adobe Solution Network, and get the file format SDK, and parse the file yourself. Another alternative would be to get a library from someone who has done this already. Here we’re talking a matter of a week or two for an experienced person, perhaps longer for someone just starting out with Photoshop.

Crumbs. Google isn’t doing the OP any favours. This parser extracts raster elements of a PSD (layer image, layer transparency, layer masks, spot channels, merged composite):
http://telegraphics.com.au/svn/psdparse/trunk/
It was written from the PSD v6 spec (last public one, I believe) and doesn’t do anything about vector, text, effects layers, PSB or any other extensions. But it does work on rasters from all PSD versions through CS2.

It shouldn’t be too hard to adapt to a library for your app, or you can ask me to do it.

Reading the psd file format can be messy
because of compatibility issues. You can simplify the job by requiring that the psd’s be created by a certain version of Photoshop.

Rasters seem to be compatible from v3 through CS2. With the CS2 Advanced SDK you might be able to do something with the non-raster layers, but I never needed this.


Mike Russell
www.curvemeister.com/forum/
T
toby
Oct 11, 2006
Olumide wrote:
Mike Russell wrote:

For a stand alone program that runs outside of Photoshop, you’ll need to dig a little deeper. Join the Adobe Solution Network, and get the file format SDK, and parse the file yourself.

Thanks, Mike. I already have the SDK, and I was puzzled as to why chapter (number 9) on the format module was missing from the API guide

They ripped it out when they made the SDK non-public. My code was entirely based on v6 file format documentation. You can apparently get it with the Advanced SDK, but my application form for that has been in their hands for approval now for 4 months without result (lucky the project wasn’t urgent).

PDF document (as is the File Formats documentation that the same guide refers to in chapter 1).

I really don’t want to be tied to the photoshop app, all I need is to be able to parse a PSD file. Maybe its time to consider the gimp — after all, it too supports layers.

– OLumide
O
Olumide
Oct 11, 2006
toby wrote:
They ripped it out when they made the SDK non-public. My code was entirely based on v6 file format documentation. You can apparently get it with the Advanced SDK, but my application form for that has been in their hands for approval now for 4 months without result (lucky the project wasn’t urgent).

Why do they do that? … To prevent GIMP folks from writing a version that reads up to date PSDs?

… This parser extracts raster elements of a PSD (layer image, layer transparency, layer masks, spot channels, merged composite):
http://telegraphics.com.au/svn/psdparse/trunk/

Looks promising. The stuff is it your’s right? Can you make the lot avaliable as a zip file please?

Thanks,

– Olumide
T
toby
Oct 11, 2006
Olumide wrote:
toby wrote:
They ripped it out when they made the SDK non-public. My code was entirely based on v6 file format documentation. You can apparently get it with the Advanced SDK, but my application form for that has been in their hands for approval now for 4 months without result (lucky the project wasn’t urgent).

Why do they do that? … To prevent GIMP folks from writing a version that reads up to date PSDs?

Yeah, it’s all about suppressing (potential) competitors. King Herod put babies to the sword out of fear of being felled by prophecy, MS and Adobe work on the same principle.

… This parser extracts raster elements of a PSD (layer image, layer transparency, layer masks, spot channels, merged composite):
http://telegraphics.com.au/svn/psdparse/trunk/

Looks promising. The stuff is it your’s right? Can you make the lot avaliable as a zip file please?

It’s all mine. I can help you work with it. You can get a tarball via the "Browse" link (which takes you to ViewVC interface).

Thanks,

– Olumide
O
Olumide
Oct 12, 2006
toby wrote:
Looks promising. The stuff is it your’s right? Can you make the lot avaliable as a zip file please?

It’s all mine. I can help you work with it. You can get a tarball via the "Browse" link (which takes you to ViewVC interface).

What browse link. I can’t see any. Can you please post the URL to the tarball?

Thanks,

– Olumide
T
toby
Oct 12, 2006
Olumide wrote:
toby wrote:
Looks promising. The stuff is it your’s right? Can you make the lot avaliable as a zip file please?

It’s all mine. I can help you work with it. You can get a tarball via the "Browse" link (which takes you to ViewVC interface).

What browse link. I can’t see any. Can you please post the URL to the tarball?

I beg your pardon – I forgot you weren’t coming in through this page, http://telegraphics.com.au/sw/#psdparse

The Browse URL is
http://telegraphics.com.au/viewcvs/trunk/?root=psdparse

Manually distributing tarballs sucks. I stopped doing that years ago.

Thanks,

– Olumide
MS
Michael Soibelman
Oct 12, 2006
toby wrote:

Olumide wrote:
toby wrote:
Looks promising. The stuff is it your’s right? Can you make the lot avaliable as a zip file please?

It’s all mine. I can help you work with it. You can get a tarball via the "Browse" link (which takes you to ViewVC interface).

What browse link. I can’t see any. Can you please post the URL to the tarball?

I beg your pardon – I forgot you weren’t coming in through this page, http://telegraphics.com.au/sw/#psdparse

The Browse URL is
http://telegraphics.com.au/viewcvs/trunk/?root=psdparse

Manually distributing tarballs sucks. I stopped doing that years ago.
Thanks,

– Olumide

Thanks for this program. Maybe someday I’ll need it for something. It does work fine. Had to run ‘sh ./configure’ to get it to configure on OpenSuSE-10.1, but this was easy enough. Then I noticed that, at least forthe development branch of Gimp (2.3.11) opening up a PSD file was no problem… So you could use Gimp to work with this type of file as well.

Thanks again.
T
toby
Oct 12, 2006
Michael Soibelman wrote:
toby wrote:

I beg your pardon – I forgot you weren’t coming in through this page, http://telegraphics.com.au/sw/#psdparse

The Browse URL is
http://telegraphics.com.au/viewcvs/trunk/?root=psdparse

Thanks for this program. Maybe someday I’ll need it for something. It does work fine. Had to run ‘sh ./configure’ to get it to configure on OpenSuSE-10.1,

The tarball that ViewCVS creates leaves all files with default permissions (i.e. not executable), although I didn’t realise this until you pointed it out. Another reason why checking out using Subversion is preferable: You get correct permissions.

but this was easy enough. Then I noticed that, at least forthe development branch of Gimp (2.3.11) opening up a PSD file was no problem… So you could use Gimp to work with this type of file as well.
Thanks again.
O
Olumide
Oct 12, 2006
toby wrote:
What browse link. I can’t see any. Can you please post the URL to the tarball?

I beg your pardon – I forgot you weren’t coming in through this page, http://telegraphics.com.au/sw/#psdparse

Thanks, I see pdfparse is standalone executable. However, what I need is a library of function with which I can use to read a PSD file from my C++ app. I guess I can start out by using bits and pieces of the main.c – or is there a better way?

– Olumide
T
toby
Oct 13, 2006
Olumide wrote:
toby wrote:
What browse link. I can’t see any. Can you please post the URL to the tarball?

I beg your pardon – I forgot you weren’t coming in through this page, http://telegraphics.com.au/sw/#psdparse

Thanks, I see pdfparse is standalone executable. However, what I need is a library of function with which I can use to read a PSD file from my C++ app. I guess I can start out by using bits and pieces of the main.c – or is there a better way?

main() includes the main parsing sequence, yes. I’ve added a few more comments.

I’d suggest you convert main to an ordinary function, and add hooks for the data you want to extract. For instance, you can supply your own versions of pngsetupwrite() and pngwriteimage(), or change their call sites to meet your own needs. You will want to use the code in pngwriteimage() which reads, uncompresses, and possibly interleaves the channel image data (by row).

Let me know if you have trouble doing this. You’ll probably want to suppress most informational output.

– Olumide
T
toby
Oct 13, 2006
Olumide wrote:
toby wrote:
What browse link. I can’t see any. Can you please post the URL to the tarball?

I beg your pardon – I forgot you weren’t coming in through this page, http://telegraphics.com.au/sw/#psdparse

Thanks, I see pdfparse is standalone executable. However, what I need is a library of function with which I can use to read a PSD file from my C++ app.

If you give me an idea of what functionality you need, I can think about the adaptation required. This should have been made into a library a while ago… 🙂

I guess I can start out by using bits and pieces of the
main.c – or is there a better way?

– Olumide
O
Olumide
Oct 13, 2006
toby wrote:
Thanks, I see pdfparse is standalone executable. However, what I need is a library of function with which I can use to read a PSD file from my C++ app.

If you give me an idea of what functionality you need, I can think about the adaptation required. This should have been made into a library a while ago… 🙂

Actually, I’d like to get the number of layers, and the pixel data for *each* layer in some on an array basis. (Each layer of my test image is an island of colored pixels surrounded by alpha/white pixels. I need to get the border pixels in order to move to the next phase of my work.)

I’ve been trying to parse the file for myself, and although it started well I kinda got stuck at the layer and mask information section (header is legth info 4 bytes). For some weird reason, I keep getting length of the miscelleneous information section to be 4294967176 instead of the more reasonable value of 43912 (I got the latter value from a matlab PSD reader).

If you can come up with some sort of library, I’d rather use that 😉 . Thanks,

– Olumide
O
Olumide
Oct 13, 2006
toby wrote:
If you give me an idea of what functionality you need, I can think about the adaptation required. This should have been made into a library a while ago… 🙂

Here’s the URL of the page containg the matlab script I was refering to,
http://www.mathworks.com/matlabcentral/fileexchange/loadFile .do?objectId=4730&objectType=file ..

Basically, the function readpsd() returns raw pixel data data to a multideimensional array. For example’
array = readpsd(‘C:\Work\a_PSD_Image_File.psd’);

Ideally, I’d like to pass a string filename to a PSDdocument constructor, and query it for stuff like number of layers, name of a layer with a given index, raw pixel data for a layer with a given index etc. Example:

PSDdocument psdDoc("C:\Work\a_PSD_Image_File.psd"); // Constructor …
psdDoc.layerCount() // returns unsigned layer count
psdDoc.getLayerName(unsigned index) // returns string layer name unsigned **layerData = psdDoc.getLayerPixels(usigned index) // returns raw pixel data for a given array

😀
O
Olumide
Oct 15, 2006
I’m hacking this, at the moment 😀 :
http://sourceforge.net/projects/openpsd/
T
toby
Oct 15, 2006
Olumide wrote:
I’m hacking this, at the moment 😀 :
http://sourceforge.net/projects/openpsd/

Good luck – "pre-alpha" doesn’t sound too good. Mine is tested with 0 known bugs for the raster functionality listed above. How urgent is your project?
O
Olumide
Oct 15, 2006
toby wrote:
Olumide wrote:
I’m hacking this, at the moment 😀 :
http://sourceforge.net/projects/openpsd/

Good luck – "pre-alpha" doesn’t sound too good. Mine is tested with 0 known bugs for the raster functionality listed above. How urgent is your project?

You could say my project is a bit urgent.

I know the openpsd project is pre-alpha and not nearly complete (still missing raster(?) data reading functionality), I find its basic design easier to understand and extend even though it doesn’t come close to matching what you have. (A library really is the way to go.)

– Olumide

PS: At least openpsd is better than JavaPSD
(http://sourceforge.net/projects/javapsd), they haven’t released any files yet 😀
MR
Mike Russell
Oct 16, 2006
"Olumide" wrote in message
toby wrote:
Olumide wrote:
I’m hacking this, at the moment 😀 :
http://sourceforge.net/projects/openpsd/

Good luck – "pre-alpha" doesn’t sound too good. Mine is tested with 0 known bugs for the raster functionality listed above. How urgent is your project?

You could say my project is a bit urgent.

Another: save your images as raw files and read the layers directly into arrays in Matlab. This is not as flexible as accessing the layers by name, otoh you could probably get something running very quickly. —

Mike Russell
www.curvemeister.com/forum/
O
Olumide
Oct 17, 2006
Mike Russell wrote:
Another: save your images as raw files and read the layers directly into arrays in Matlab. This is not as flexible as accessing the layers by name, otoh you could probably get something running very quickly.

Even better, I may eventually go the TIFF route (I just realized TIFF supports layers). There are a few TIFF libraries out there e.g. LibTIFF http://www.remotesensing.org/libtiff/ , openTIFF
http://sourceforge.net/project/showfiles.php?group_id=18508 .

‘Lets hope these libraries support layers ;-D
T
toby
Oct 18, 2006
Olumide wrote:
toby wrote:
Olumide wrote:
I’m hacking this, at the moment 😀 :
http://sourceforge.net/projects/openpsd/

Good luck – "pre-alpha" doesn’t sound too good. Mine is tested with 0 known bugs for the raster functionality listed above. How urgent is your project?

You could say my project is a bit urgent.

A miracle of bad timing. I’m so busy right now I can’t see myself quickly doing the small changes required to library-ise my code.

I know the openpsd project is pre-alpha and not nearly complete (still missing raster(?) data reading functionality), I find its basic design easier to understand and extend even though it doesn’t come close to matching what you have. (A library really is the way to go.)
– Olumide

PS: At least openpsd is better than JavaPSD
(http://sourceforge.net/projects/javapsd), they haven’t released any files yet 😀

Must-have mockup pack for every graphic designer 🔥🔥🔥

Easy-to-use drag-n-drop Photoshop scene creator with more than 2800 items.

Related Discussion Topics

Nice and short text about related topics in discussion sections