Click here to Skip to main content
11,493,937 members (70,946 online)
Click here to Skip to main content

Import Adobe Photoshop (.psd) images

, 24 Jul 2006 CPOL 197.1K 5.6K 84
Rate this:
Please Sign up or sign in to vote.
A generic class to import Adobe's Photoshop (.psd) images.

Sample Image - MyPSD.jpg

Introduction

MyPSD::CPSD class is a C++ class that can load images saved in Adobe's Photoshop native format. Due to my new job I had to upgrade to Visual Studio 2005, but still the class is as portable as possible, hence it uses standard C++ and STL. With minor changes, it can be ported to Visual Studio 6 (uses an older version of STL) or even to other Operating Systems.

License

The class MyPSD::CPSD is free; but if you are to use it in any kind of software, especially commercial ones, I would appreciate if you could just email me, it's nice to learn that something you've written is used by others.

Using MyPSD::CPSD in your projects

To use the class, just include in your project the two files MyPSD.h/.cpp. Since the class is placed inside its own namespace, either use the full name MyPSD::CPSD psd; or first the directive using namespace MyPSD; and then just declare a local variable CPSD psd;.

How to use MyPSD::CPSD

MyPSD::CPSD psd; int nErrorCode = psd.Load("c:\\image.psd");
if ( 0 == nErrorCode )   // No errors were found
{
     int nDPI_x, nDPI_y; // for Horizontal & Vertical dpi
     int nWidth, nHeight; // for Width & Height in pixels
     psd.Dimensions(nWidth, nHeight);
     psd.DPI(nDPI_x, nDPI_y);
     HBITMAP hBitmap = psd.Detach();
}
else if ( -1 == nErrorCode ) // Cannot open file
     ; // Do something
else if ( -2 == nErrorCode ) // Error in the header of the file
     ; // Do something 
else if ( -3 == nErrorCode ) // Error in ColourMode Data, i.e. Palette
     ; // Do something       // File must be Index to have ColourMode Data 
else if ( -4 == nErrorCode ) // Error in ImageResource section of the file
     ; // Do something
else if ( -5 == nErrorCode ) // Error in Mask Info section of the file
     ; // Do something 
else if ( -6 == nErrorCode ) // Error in Image Data section of the file
     ; // Do something       // Actual data for pixels 
else if ( -7 == nErrorCode ) // Image is Black & White
     ; // Do something       // not yet implemented 
else if ( -8 == nErrorCode ) // An unsupported file format encountered
     ; // Do something 
else if ( -9 == nErrorCode ) // Cannot create HBITMAP handle
     ; // Do something 
else if ( -10 == nErrorCode ) // Data are compressed
     ; // Do something        // Zip format without prediction. Not yet
                              // implemented 
else if ( -11 == nErrorCode ) // Data are compressed
     ; // Do something        // Zip format with prediction. Not yet 
                              // implemented 
else if ( -12 == nErrorCode ) // Data are in an unknown format
     ; // Do something

How to make it compatible to Visual Studio 6

The STL that comes with Visual Studio 6 does not support the method push_back for std::string. One way is to use CStrings, but then you will have to use MFC, and the other way is to make some minor changes where strings are used. I.e.:

std::string strOSType;
for(unsigned int nChar = 0; nChar < 4; ++nChar)
    strOSType.push_back(image_resource.OSType[nChar]);

std::string strOSType = "";
for(unsigned int nChar = 0; nChar < 4; ++nChar)
    strOSType += image_resource.OSType[nChar];

More info than necessary... future release... maybe

In the MyPsd.h file, I have put more info than is actually used in the demo, as my intention is someday to implement layers as they are supported in Photoshop. The way MyPSD::CPSD class is implemented in the current version, the HBITMAP that someone gets is the merged outcome of all layers. In the future, I will try to have each layer separately detached from the original image, but don't hold your breath as I don't know if I have enough free time to do so.

Supported Formats

RGB, Lab, CMY, CMYK, Indexed, Grayscale, Duotone. These formats could be uncompressed or compressed with RLE.

Unsupported Formats/Not yet implemented

Bitmap (Black & White), and any format that is compressed with ZIP with prediction and without prediction, and that is basically because I don't know the ZIP compression algorithm (with and without prediction).

Thanks

I would like to thank my mathematician friend Kostas Papamarinos who helped me translate the data from planar to RGB order, and all of you who are going to use my MyPSD::CPSD class and either send me an email or not.

What's new in version 1.0.0.4

  • Bug fixes - For correctly rendering Grayscale and Duotone PSD files. Samples included in the images.zip file that previous version could not read. As Visual Studio 2005 is more strict to the standard C++ and I hated all the warnings I was getting I have used the proposed functions which supposedly are more secure. I would like to apologise for any inconvenience caused by this change. If there is any problem just replace them with their equivalent older versions. Finally, for the above mentioned reasons, I am using a function to convert doubles to integers, as I would like to avoid type casting either done by me or the system .

What's new in version 1.0.0.3

  • Bug fixes - For correctly rendering PSD files. Each Image Resource Block includes a Pascal string, and the way I translated into C/C++ was not the proper way, as a result some files could not be read.

What's new in version 1.0.0.2

  • Bug fixes - I've found out that my previous version had some minor memory leaks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ihaml
Software Developer
Greece Greece
No Biography provided

Comments and Discussions

 
GeneralAbout illustrator Pin
Mr.Heng13-Sep-10 13:04
memberMr.Heng13-Sep-10 13:04 
GeneralFix for crash with some PSD files Pin
Having To Register Isn't Cool17-Jun-09 6:11
memberHaving To Register Isn't Cool17-Jun-09 6:11 
GeneralRe: Fix for crash with some PSD files Pin
ihaml17-Jun-09 7:50
memberihaml17-Jun-09 7:50 
Questionwhat is each section in PSD file? Pin
dtdh18-Nov-07 15:33
memberdtdh18-Nov-07 15:33 
Hello everyone!
Now I want to import PSD file in layer, that is each layer displays as one picture, but I can't understand the PSD format completely, such as what is the Channel Image Data in setcion four, what is different between Channel Imaga Data and Image Data in section five ..etc? can anyone give me an answer in detail? I want to communicate the PSD format with you, thanks for your help.

Questionwhy cannot download sourcecode? Pin
psdviewer19-Jun-07 5:53
memberpsdviewer19-Jun-07 5:53 
AnswerRe: why cannot download sourcecode? Pin
ihaml19-Jun-07 20:53
memberihaml19-Jun-07 20:53 
GeneralRe: why cannot download sourcecode? [modified] Pin
psdviewer20-Jun-07 4:15
memberpsdviewer20-Jun-07 4:15 
GeneralIts awesome Pin
Quartz.3-Apr-07 14:32
memberQuartz.3-Apr-07 14:32 
Questionpsd thumbnail Pin
wohahaha10-Feb-07 4:44
memberwohahaha10-Feb-07 4:44 
AnswerRe: psd thumbnail Pin
ihaml10-Feb-07 8:46
memberihaml10-Feb-07 8:46 
QuestionMay you help me? Pin
an_handsome14-Jan-07 16:43
memberan_handsome14-Jan-07 16:43 
AnswerRe: May you help me? Pin
ihaml15-Jan-07 8:02
memberihaml15-Jan-07 8:02 
AnswerRe: May you help me? Pin
ihaml15-Jan-07 8:12
memberihaml15-Jan-07 8:12 
GeneralI have a asking about article Import Adobe Photoshop Pin
an_handsome14-Jan-07 16:37
memberan_handsome14-Jan-07 16:37 
GeneralRe: I have a asking about article Import Adobe Photoshop Pin
ihaml15-Jan-07 8:08
memberihaml15-Jan-07 8:08 
GeneralStrange Problem at loading Pin
image_processor6-Aug-06 4:41
memberimage_processor6-Aug-06 4:41 
GeneralRe: Strange Problem at loading Pin
ihaml6-Aug-06 5:21
memberihaml6-Aug-06 5:21 
GeneralRe: Strange Problem at loading Pin
image_processor6-Aug-06 22:36
memberimage_processor6-Aug-06 22:36 
GeneralRe: Strange Problem at loading Pin
ihaml16-Aug-06 23:22
memberihaml16-Aug-06 23:22 
GeneralRe: Strange Problem at loading Pin
image_processor23-Aug-06 19:10
memberimage_processor23-Aug-06 19:10 
GeneralGood Job, and a question Pin
DSclub1-Aug-06 17:08
memberDSclub1-Aug-06 17:08 
QuestionTransparencies and Alpha. Pin
Jim Koornneef25-Jul-06 4:09
memberJim Koornneef25-Jul-06 4:09 
QuestionTransparent background? Pin
crjaes7-Dec-05 12:25
membercrjaes7-Dec-05 12:25 
AnswerRe: Transparent background? Pin
ihaml7-Dec-05 23:52
memberihaml7-Dec-05 23:52 
GeneralRe: Transparent background? Pin
c2j221-Mar-06 3:54
memberc2j221-Mar-06 3:54 
GeneralPSD selection retrieval Pin
Zero DeHero1-Oct-05 23:41
memberZero DeHero1-Oct-05 23:41 
GeneralRe: PSD selection retrieval Pin
lorddarq13-Jan-10 22:50
memberlorddarq13-Jan-10 22:50 
General[XPost] Reading Layer images Pin
Jonas Beckeman28-Jul-05 10:41
memberJonas Beckeman28-Jul-05 10:41 
Generallicense... Pin
Mario M.15-Jul-05 13:50
memberMario M.15-Jul-05 13:50 
Generalspeed Pin
Unruled Boy13-Jul-05 7:24
memberUnruled Boy13-Jul-05 7:24 
GeneralRe: speed Pin
ihaml14-Jul-05 0:24
memberihaml14-Jul-05 0:24 
GeneralRe: speed Pin
Unruled Boy14-Jul-05 0:30
memberUnruled Boy14-Jul-05 0:30 
GeneralRe: speed Pin
Unruled Boy14-Jul-05 3:34
memberUnruled Boy14-Jul-05 3:34 
QuestionZip format with prediction ?? Pin
liop11-Jul-05 7:53
memberliop11-Jul-05 7:53 
AnswerRe: Zip format with prediction ?? Pin
genny773-Dec-09 9:13
membergenny773-Dec-09 9:13 
GeneralRe: Zip format with prediction ?? Pin
ihaml3-Dec-09 10:30
memberihaml3-Dec-09 10:30 
GeneralShortly corrections Pin
ihaml6-Jul-05 2:10
memberihaml6-Jul-05 2:10 
GeneralI do have C# version Pin
Hottabych20-Jun-05 10:54
memberHottabych20-Jun-05 10:54 
GeneralRe: I do have C# version Pin
ihaml21-Jun-05 3:02
memberihaml21-Jun-05 3:02 
GeneralRe: I do have C# version Pin
Unruled Boy1-Jul-05 20:11
memberUnruled Boy1-Jul-05 20:11 
GeneralRe: I do have C# version Pin
Igor Tolmachev1-Jul-05 21:45
memberIgor Tolmachev1-Jul-05 21:45 
GeneralRe: I do have C# version Pin
Igor Tolmachev3-Jul-05 1:42
memberIgor Tolmachev3-Jul-05 1:42 
GeneralRe: I do have C# version Pin
Unruled Boy12-Jul-05 18:38
memberUnruled Boy12-Jul-05 18:38 
QuestionCan I help you? Pin
AlexSagal19-Jun-05 16:54
memberAlexSagal19-Jun-05 16:54 
Generalimplement into CxImage Pin
JimmyO16-Jun-05 0:28
memberJimmyO16-Jun-05 0:28 
GeneralRe: implement into CxImage Pin
ihaml16-Jun-05 22:36
memberihaml16-Jun-05 22:36 
Generalgood job Pin
tonnyyyao7-Jun-05 18:14
membertonnyyyao7-Jun-05 18:14 
GeneralRe: good job Pin
ihaml8-Jun-05 2:52
memberihaml8-Jun-05 2:52 
GeneralAny chance of porting to C# Pin
jackmos27-May-05 6:01
memberjackmos27-May-05 6:01 
GeneralRe: Any chance of porting to C# Pin
ihaml2-Jun-05 3:01
memberihaml2-Jun-05 3:01 

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 24 Jul 2006
Article Copyright 2005 by ihaml
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid