5,427,303 members and growing! (16,003 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate

Import Adobe Photoshop (.psd) images

By ihaml

A generic class to import Adobe's Photoshop (.psd) images.
VC6, VC7.1, C++Windows, NT4, Win2K, WinXP, Win2003, MFC, STL, GDI, VS.NET2003, VS6, Visual Studio, Dev

Posted: 15 May 2005
Updated: 24 Jul 2006
Views: 84,192
Bookmarked: 56 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
55 votes for this Article.
Popularity: 7.88 Rating: 4.53 out of 5
1 vote, 1.8%
1
0 votes, 0.0%
2
1 vote, 1.8%
3
7 votes, 12.7%
4
46 votes, 83.6%
5

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 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

About the Author

ihaml



Occupation: Web Developer
Location: Greece Greece

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh)FirstPrevNext
Subject  Author Date 
Questionwhat is each section in PSD file?memberdtdh15:33 18 Nov '07  
Generalwhy cannot download sourcecode?memberpsdviewer5:53 19 Jun '07  
GeneralRe: why cannot download sourcecode?memberihaml20:53 19 Jun '07  
GeneralRe: why cannot download sourcecode? [modified]memberpsdviewer4:15 20 Jun '07  
GeneralIts awesomememberQuartz.14:32 3 Apr '07  
Questionpsd thumbnailmemberwohahaha4:44 10 Feb '07  
AnswerRe: psd thumbnailmemberihaml8:46 10 Feb '07  
GeneralMay you help me?memberan_handsome16:43 14 Jan '07  
AnswerRe: May you help me?memberihaml8:02 15 Jan '07  
AnswerRe: May you help me?memberihaml8:12 15 Jan '07  
GeneralI have a asking about article Import Adobe Photoshopmemberan_handsome16:37 14 Jan '07  
GeneralRe: I have a asking about article Import Adobe Photoshopmemberihaml8:08 15 Jan '07  
GeneralStrange Problem at loadingmemberimage_processor4:41 6 Aug '06  
GeneralRe: Strange Problem at loadingmemberihaml5:21 6 Aug '06  
GeneralRe: Strange Problem at loadingmemberimage_processor22:36 6 Aug '06  
GeneralRe: Strange Problem at loadingmemberihaml23:22 16 Aug '06  
GeneralRe: Strange Problem at loadingmemberimage_processor19:10 23 Aug '06  
GeneralGood Job, and a questionmemberDSclub17:08 1 Aug '06  
QuestionTransparencies and Alpha.memberJim Koornneef4:09 25 Jul '06  
GeneralTransparent background?membercrjaes12:25 7 Dec '05  
GeneralRe: Transparent background?memberihaml23:52 7 Dec '05  
GeneralRe: Transparent background?memberc2j23:54 21 Mar '06  
GeneralPSD selection retrievalmemberZero DeHero23:41 1 Oct '05  
General[XPost] Reading Layer imagesmemberJonas Beckeman10:41 28 Jul '05  
Generallicense...memberMario M.13:50 15 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jul 2006
Editor: Chris Maunder
Copyright 2005 by ihaml
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project