|

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::CPSDMyPSD::CPSD psd; int nErrorCode = psd.Load("c:\\image.psd");
if ( 0 == nErrorCode )
{
int nDPI_x, nDPI_y;
int nWidth, nHeight;
psd.Dimensions(nWidth, nHeight);
psd.DPI(nDPI_x, nDPI_y);
HBITMAP hBitmap = psd.Detach();
}
else if ( -1 == nErrorCode )
;
else if ( -2 == nErrorCode )
;
else if ( -3 == nErrorCode )
;
else if ( -4 == nErrorCode )
;
else if ( -5 == nErrorCode )
;
else if ( -6 == nErrorCode )
;
else if ( -7 == nErrorCode )
;
else if ( -8 == nErrorCode )
;
else if ( -9 == nErrorCode )
;
else if ( -10 == nErrorCode )
;
else if ( -11 == nErrorCode )
;
else if ( -12 == nErrorCode )
;
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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh) | FirstPrevNext |
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I tried to download your code,but cannot do that,the massege is "download is fail",why? Can you help me? thank you very much!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
haha,there is no problem if I used the IE. perhaps TT have some bugs!!! Recently,I am learning the psd file format,and developing the psd viewer. Thanks a lot!!!
-- modified at 9:27 Friday 22nd June, 2007
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Based on this i have compiled a parser to be used in photoshop preview handler in Vista check here[^]
Do let me know what you think about that
Raj
Omit Needless Words - Strunk, William, Jr.
Vista? Photoshop Preview Handler here
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hello, guy,
Can you tall me how can get psd thumbnail from psd files? If anyone know it,please tell me.Thanks.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
This is probably my last response.
Thumbnails are stored is a thumbnail resource format. Depending on the psd format for Photoshop 4.0 the resource ID is 1033 as of version 5.0 and later the ID is 1036.
Before reading the actual thumbnail’s data you need to read a header so that you can find out how to interpret them. Below you’ll find the necessary information in order to correctly interpret read and interpret the data.
4 bytes: Indicating the data’s format - 1 data are in JPEG RGB format – 0 RGB raw 4 bytes: Width of thumbnail in pixels 4 bytes: Height of thumbnail in pixels 4 bytes: Padded row bytes – (width * bitspixels + 31) / 32 * 4 4 bytes: Total size – width bytes * height * planes 4 bytes: Compressed size, just for consistency check 2 bytes: bitspixels - 24 bits per pixel 2 bytes: - 1 number of planes Variable size: Actual data. If resource ID is 1033 ie Photoshop 4.0 then data is in BGR format otherwise the RGB format is used.
You can either translate the JPEG format yourself or use any library that supports JPEG files.
Hope the above information is of help.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
Inside the code there is a loop:
std::string strOSType; for(unsigned int nChar = 0; nChar < 4; ++nChar) strOSType.push_back(image_resource.OSType[nChar]);
This can be translated into:
std::string strOSType = ""; for(unsigned int nChar = 0; nChar < 4; ++nChar) strOSType += image_resource.OSType[nChar];
Same way for any other push_back method used for std::strings
As for your other question regarding pop_back I couldn't find anywhere in my code where I might have used it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|

And something else, it seems you haven't read the article as I explain how to translate the push_back method for std::string in the article's section:
How to make it compatible to Visual Studio 6
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I'm sorry but I don't have that much free time in my disposal to answer all messages or even support and make modifications/additions to my code/article as much as I would love to have.
I wrote this article while working in my previous job where I had a lot more free time but less interesting job so "I was seeking for an outlet"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have used your files MyPSD.h and MyPSD.cpp in a simple ATL project to test them ...
there is no doubt that it is a great wrok ...
but I have a problem ... when I load supprted images ... it works fine ...
if I load at last one time an unsuporrted format ... it never works again ...
forexample,
Image x.psd is loaded successfully with error code 0 ... Image y.psd is not loaded successfully with error code -6 ... Image x.psd is not loaded successfully with error code -4 ... although it was loaded successfully before loading y.psd ...
could someone tell where is the problem please ....
I'm so confused ...
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
The only reason I can think of is perhaps I don't release memory and/or gdi objects correctly when an unsupported file is read.
Or I might have a bug ( bug in my code.....).
Just email me the problematic (y.psd) file I could try to see what I am doing wrong.
I should inform you though that for the next 2 weeks I'll be on vacations. Finally summer has arrived for my as well. 2 weeks with no computers, I need this time off to recharge my batteries.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
thanks a lot for your reply ...
I had traced your code step by step ... ...
At CPSD::ReadImageData(FILE* pFile) function ... I found the following pecie of code:
... unsigned char* p = pData; ... if ( 128 > len ) { len++; Count += len;
while (len) { (int)fread(&ByteValue, sizeof(ByteValue), 1, pFile); nValue = Calculate( ByteValue, sizeof(ByteValue) );
*p = nValue; p += sizeof(ByteValue); len--; } } else if ( 128 < len ) { // Next -len+1 bytes in the dest are replicated from next source byte. // (Interpret len as a negative 8-bit int.) len ^= 0x0FF; len += 2; (int)fread(&ByteValue, sizeof(ByteValue), 1, pFile);
nValue = Calculate( ByteValue, sizeof(ByteValue) ); Count += len;
while (len) { *p = nValue; p += sizeof(ByteValue); len--; } }
I think in someway that the pointer *p is become invalid ... I don't know how actually, cause I didn't made a deep look ... so I replaced the upper code by the following one ...
... unsigned char* p = pData; unsigned char* pEnd = &pData[nTotalBytes-1]; ... if ( 128 > len ) { len++; Count += len;
while (len) { (int)fread(&ByteValue, sizeof(ByteValue), 1, pFile); nValue = Calculate( ByteValue, sizeof(ByteValue) );
if(p >= pData && p <= pEnd) { *p = nValue; p += sizeof(ByteValue); } len--; } } else if ( 128 < len ) { // Next -len+1 bytes in the dest are replicated from next source byte. // (Interpret len as a negative 8-bit int.) len ^= 0x0FF; len += 2; (int)fread(&ByteValue, sizeof(ByteValue), 1, pFile);
nValue = Calculate( ByteValue, sizeof(ByteValue) ); Count += len;
while (len) { if(p >= pData && p <= pEnd) { *p = nValue; p += sizeof(ByteValue); } len--; } }
it works fine with me without problem ... ...
But I have another problem now ... when I load a 24 bit psd image ... and ask about the bit depth by calling BitsPerPixel() ... it returns 8 to me ... this happens with all 24 bit PSD images... ... and it recognized by the photo shop and many applications as 24 bits images ...
do you have any suggestions to me ... waiting yout reply...
wish you a nice day ...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Next version of the code will also have your changes.
As far as the 8bit I don't remember from where (image header etc) I read the value, out of my head for the moment I can imagine is Bits per pixel per channel, hence 8bits per pixel per 3 channels (RGB) equals 24. I think, I'll get back to you with the answer.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Can someone show me a way to save a picture in CMYK color mode?
With GDI+ 2.0, I can only save a bitmap(JPEG) in RGB color mode!
plz help me, urgently!!!
dsclub@hotmail.com
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
MyPSD is great, but I have one question...is there any way to keep the transparent background of a PSD file when it is placed into the HBITMAP object?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
As far as I know HBITMAP doesn't support transparency, but I might be wrong. HBITMAP is a handle (something like a pointer) to memory, you don't have access directly to the actual bits there. To get access to the data you use
::GetObject(..., ..., ....)
Check MSDN for more help.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Yes, bitmaps support even per-pixel RGBA values (per-pixel Alpha blending). This is not the problem.
I'd love to see code that does support this. The PSD code given here does give no hint of how to read/handle transparency channels.
Ch.
|
| Sign In·View Thread·PermaLink | 1.80/5 (2 votes) |
|
|
|
 |
|
|
PSD allows for saving a selection ('walking ants') in the file. if the selection is saved within photoshop then it is also saved in the PSD file. Anyone knows how to retrieve the selection?
And going a step further, same question on Photoshop Paths.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm writing a C# psd importer for use within my graphics/game toolkit Endogine, and I've succeeded in parsing most of the file (layer info, effect settings etc) but I have problems with the actual images. I've looked at the codeproject articles but they only read the merged bitmap, not the individual layers (as noted). I think I'm almost there, but the RLE encoding in layers seems to differ from the merged one (e.g. the RLE "header" is per channel, not per bitmap).
I don't have much more energy to spend on PSDs right now (and I have a "cheat", using psd2png.exe) but if anyone wants to continue trying, download the Endogine source at http://endogine.com/CS/Endogine.zip (I haven't udated the codeproject article yet)
The Photoshop code is in Endogine\Serialization\Photoshop (Photoshop.cs as the starting point). Start it up, right-click DefaultLayer in the SceneGraphViewer, select Import/Replace to import a psd.
|
| Sign In·View Thread·PermaLink | 1.25/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|