导入 Adobe Photoshop (.psd) 图像
一个通用的类来导入 Adobe Photoshop (.psd) 图像。
引言
MyPSD::CPSD
类是一个 C++ 类,可以加载以 Adobe Photoshop 本机格式保存的图像。 由于我的新工作,我不得不升级到 Visual Studio 2005,但该类仍然尽可能地具有可移植性,因此它使用标准 C++ 和 STL。 稍作更改,就可以将其移植到 Visual Studio 6(使用旧版本的 STL)甚至其他操作系统。
许可证
MyPSD::CPSD
类是免费的; 但是,如果您要在任何类型的软件中使用它,尤其是商业软件,如果您能给我发电子邮件,我将不胜感激,很高兴得知您编写的东西被其他人使用。
在您的项目中使用 MyPSD::CPSD
要使用该类,只需在您的项目中包含两个文件 *MyPSD.h/.cpp*。 由于该类位于其自己的命名空间中,因此请使用完整名称 MyPSD::CPSD psd;
或首先使用指令 using namespace MyPSD;
,然后只声明一个局部变量 CPSD psd;
。
如何使用 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
如何使其与 Visual Studio 6 兼容
Visual Studio 6 附带的 STL 不支持 std::string
的 push_back
方法。 一种方法是使用 CString
,但是您将不得不使用 MFC,另一种方法是在使用字符串的地方进行一些小的更改。 例如
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];
比需要的更多信息...未来的版本...也许
在 *MyPsd.h* 文件中,我放入了比演示中实际使用的更多信息,因为我的意图是有一天实现 Photoshop 中支持的图层。 MyPSD::CPSD
类在当前版本中的实现方式是,某人获得的 HBITMAP
是所有图层的合并结果。 将来,我将尝试将每个图层与原始图像分开,但不要屏住呼吸,因为我不知道是否有足够的空闲时间来这样做。
支持的格式
RGB、Lab、CMY、CMYK、索引、灰度、双色调。 这些格式可以未压缩或使用 RLE 压缩。
不支持的格式/尚未实现
位图(黑白)和任何使用 ZIP 压缩(带预测和不带预测)的格式,这主要是因为我不知道 ZIP 压缩算法(带和不带预测)。
谢谢
我要感谢我的数学家朋友 Kostas Papamarinos,他帮助我将数据从平面顺序转换为 RGB 顺序,并感谢所有将要使用我的 MyPSD::CPSD
类并给我发送电子邮件的人。
1.0.0.4 版中的新功能
- 错误修复 - 用于正确渲染灰度和双色调 PSD 文件。 images.zip 文件中包含的样本是以前版本无法读取的。 由于 Visual Studio 2005 对标准 C++ 的要求更高,并且我讨厌我收到的所有警告,因此我使用了建议的函数,这些函数据说是更安全的。 我为这种更改带来的任何不便表示歉意。 如果有任何问题,只需将它们替换为等效的旧版本即可。 最后,由于上述原因,我使用一个函数将 double 转换为整数,因为我想避免由我自己或系统完成的类型转换。
1.0.0.3 版中的新功能
- 错误修复 - 用于正确渲染 PSD 文件。 每个图像资源块都包含一个 Pascal 字符串,而我翻译成 C/C++ 的方式不正确,因此某些文件无法读取。
1.0.0.2 版中的新功能
- 错误修复 - 我发现我以前的版本有一些*小的*内存泄漏。