/src/FreeImage/Source/FreeImage/PSDParser.h
C++ Header | 271 lines | 146 code | 25 blank | 100 comment | 0 complexity | 9d05a0f61bb11f1f49e7fcfd5605b455 MD5 | raw file
1// ========================================================== 2// Photoshop Loader 3// 4// Design and implementation by 5// - Hervé Drolon (drolon@infonie.fr) 6// - Mihail Naydenov (mnaydenov@users.sourceforge.net) 7// 8// Based on LGPL code created and published by http://sourceforge.net/projects/elynx/ 9// 10// This file is part of FreeImage 3 11// 12// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY 13// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES 14// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE 15// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED 16// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT 17// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY 18// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL 19// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER 20// THIS DISCLAIMER. 21// 22// Use at your own risk! 23// ========================================================== 24 25#ifndef PSDPARSER_H 26#define PSDPARSER_H 27 28/** 29Table 2-12: File header section. 30The file header contains the basic properties of the image. 31*/ 32typedef struct psdHeader { 33 BYTE Signature[4]; //! Always equal 8BPS, do not try to read the file if the signature does not match this value. 34 BYTE Version[2]; //! Always equal 1, do not read file if the version does not match this value. 35 char Reserved[6]; //! Must be zero. 36 BYTE Channels[2]; //! Number of channels including any alpha channels, supported range is 1 to 24. 37 BYTE Rows[4]; //! The height of the image in pixels. Supported range is 1 to 30,000. 38 BYTE Columns[4]; //! The width of the image in pixels. Supported range is 1 to 30,000. 39 BYTE Depth[2]; //! The number of bits per channel. Supported values are 1, 8, and 16. 40 BYTE Mode[2]; //! Colour mode of the file, Bitmap=0, Grayscale=1, Indexed=2, RGB=3, CMYK=4, Multichannel=7, Duotone=8, Lab=9. 41} psdHeader; 42 43/** 44Table 2-12: HeaderInfo Color spaces 45@see psdHeader 46*/ 47class psdHeaderInfo { 48public: 49 short _Channels; //! Numer of channels including any alpha channels, supported range is 1 to 24. 50 int _Height; //! The height of the image in pixels. Supported range is 1 to 30,000. 51 int _Width; //! The width of the image in pixels. Supported range is 1 to 30,000. 52 short _BitsPerChannel;//! The number of bits per channel. Supported values are 1, 8, and 16. 53 short _ColourMode; //! Colour mode of the file, Bitmap=0, Grayscale=1, Indexed=2, RGB=3, CMYK=4, Multichannel=7, Duotone=8, Lab=9. 54 55public: 56 psdHeaderInfo(); 57 ~psdHeaderInfo(); 58 /** 59 @return Returns the number of bytes read 60 */ 61 bool Read(FreeImageIO *io, fi_handle handle); 62}; 63 64/** 65Table 2-13 Color mode data section 66 67Only indexed color and duotone have color mode data. For all other modes, 68this section is just 4 bytes: the length field, which is set to zero. 69For indexed color images, the length will be equal to 768, and the color data 70will contain the color table for the image, in non-interleaved order. 71For duotone images, the color data will contain the duotone specification, 72the format of which is not documented. Other applications that read 73Photoshop files can treat a duotone image as a grayscale image, and just 74preserve the contents of the duotone information when reading and writing 75the file. 76*/ 77class psdColourModeData { 78public: 79 int _Length; //! The length of the following color data 80 BYTE * _plColourData; //! The color data 81 82public: 83 psdColourModeData(); 84 ~psdColourModeData(); 85 /** 86 @return Returns the number of bytes read 87 */ 88 bool Read(FreeImageIO *io, fi_handle handle); 89 bool FillPalette(FIBITMAP *dib); 90}; 91 92/** 93Table 2-1: Image resource block 94NB: Resource data is padded to make size even 95*/ 96class psdImageResource { 97public: 98 int _Length; 99 char _OSType[4]; //! Photoshop always uses its signature, 8BIM 100 short _ID; //! Unique identifier. Image resource IDs on page 8 101 BYTE * _plName; //! A pascal string, padded to make size even (a null name consists of two bytes of 0) 102 int _Size; //! Actual size of resource data. This does not include the Type, ID, Name or Size fields. 103 104public: 105 psdImageResource(); 106 ~psdImageResource(); 107 void Reset(); 108}; 109 110/** 111Table A-6: ResolutionInfo structure 112This structure contains information about the resolution of an image. It is 113written as an image resource. See the Document file formats chapter for more 114details. 115*/ 116class psdResolutionInfo { 117public: 118 short _widthUnit; //! Display width as 1=inches; 2=cm; 3=points; 4=picas; 5=columns. 119 short _heightUnit; //! Display height as 1=inches; 2=cm; 3=points; 4=picas; 5=columns. 120 short _hRes; //! Horizontal resolution in pixels per inch. 121 short _vRes; //! Vertical resolution in pixels per inch. 122 int _hResUnit; //! 1=display horizontal resolution in pixels per inch; 2=display horizontal resolution in pixels per cm. 123 int _vResUnit; //! 1=display vertical resolution in pixels per inch; 2=display vertical resolution in pixels per cm. 124 125public: 126 psdResolutionInfo(); 127 ~psdResolutionInfo(); 128 /** 129 @return Returns the number of bytes read 130 */ 131 int Read(FreeImageIO *io, fi_handle handle); 132 /** 133 @param res_x [out] X resolution in pixels/meter 134 @param res_y [out] Y resolution in pixels/meter 135 */ 136 void GetResolutionInfo(unsigned &res_x, unsigned &res_y); 137}; 138 139// Obsolete - Photoshop 2.0 140class psdResolutionInfo_v2 { 141public: 142 short _Channels; 143 short _Rows; 144 short _Columns; 145 short _Depth; 146 short _Mode; 147 148public: 149 psdResolutionInfo_v2(); 150 ~psdResolutionInfo_v2(); 151 /** 152 @return Returns the number of bytes read 153 */ 154 int Read(FreeImageIO *io, fi_handle handle); 155}; 156 157/** 158Table A-7: DisplayInfo Color spaces 159This structure contains display information about each channel. It is written as an image resource. 160*/ 161class psdDisplayInfo { 162public: 163 short _ColourSpace; 164 short _Colour[4]; 165 short _Opacity; //! 0..100 166 BYTE _Kind; //! selected = 0, protected = 1 167 BYTE _padding; //! should be zero 168 169public: 170 psdDisplayInfo(); 171 ~psdDisplayInfo(); 172 /** 173 @return Returns the number of bytes read 174 */ 175 int Read(FreeImageIO *io, fi_handle handle); 176}; 177 178/** 179Table 2-5: Thumbnail resource header 180Adobe Photoshop 5.0 and later stores thumbnail information for preview 181display in an image resource block. These resource blocks consist of an initial 18228 byte header, followed by a JFIF thumbnail in RGB (red, green, blue) order 183for both Macintosh and Windows. Adobe Photoshop 4.0 stored the 184thumbnail information in the same format except the data section is BGR 185(blue, green, red). The Adobe Photoshop 4.0 format is at resource ID 1033 186and the Adobe Photoshop 5.0 format is at resource ID 1036. 187*/ 188class psdThumbnail { 189public: 190 int _Format; //! = 1 (kJpegRGB). Also supports kRawRGB (0). 191 int _Width; //! Width of thumbnail in pixels. 192 int _Height; //! Height of thumbnail in pixels. 193 int _WidthBytes; //! Padded row bytes as (width * bitspixel + 31) / 32 * 4. 194 int _Size; //! Total size as widthbytes * height * planes 195 int _CompressedSize; //! Size after compression. Used for consistentcy check. 196 short _BitPerPixel; //! = 24. Bits per pixel. 197 short _Planes; //! = 1. Number of planes. 198 FIBITMAP * _dib; //! JFIF data as uncompressed dib. Note: For resource ID 1033 the data is in BGR format. 199 200public: 201 psdThumbnail(); 202 ~psdThumbnail(); 203 FIBITMAP* getDib() { return _dib; } 204 /** 205 @return Returns the number of bytes read 206 */ 207 int Read(FreeImageIO *io, fi_handle handle, int iResourceSize, bool isBGR); 208 209private: 210 psdThumbnail(const psdThumbnail&); 211 psdThumbnail& operator=(const psdThumbnail&); 212}; 213 214class psdICCProfile { 215public: 216 int _ProfileSize; 217 BYTE * _ProfileData; 218public: 219 psdICCProfile(); 220 ~psdICCProfile(); 221 void clear(); 222 /** 223 @return Returns the number of bytes read 224 */ 225 int Read(FreeImageIO *io, fi_handle handle, int size); 226}; 227 228/** 229PSD loader 230*/ 231class psdParser { 232private: 233 psdHeaderInfo _headerInfo; 234 psdColourModeData _colourModeData; 235 psdResolutionInfo _resolutionInfo; 236 psdResolutionInfo_v2 _resolutionInfo_v2; 237 psdDisplayInfo _displayInfo; 238 psdThumbnail _thumbnail; 239 psdICCProfile _iccProfile; 240 241 short _ColourCount; 242 short _TransparentIndex; 243 int _GlobalAngle; 244 bool _bResolutionInfoFilled; 245 bool _bResolutionInfoFilled_v2; 246 bool _bDisplayInfoFilled; 247 bool _bThumbnailFilled; 248 bool _bCopyright; 249 250 int _fi_flags; 251 int _fi_format_id; 252 253private: 254 /** Actually ignore it */ 255 bool ReadLayerAndMaskInfoSection(FreeImageIO *io, fi_handle handle); 256 FIBITMAP* ReadImageData(FreeImageIO *io, fi_handle handle); 257 258public: 259 psdParser(); 260 ~psdParser(); 261 FIBITMAP* Load(FreeImageIO *io, fi_handle handle, int s_format_id, int flags=0); 262 /** Also used by the TIFF plugin */ 263 bool ReadImageResources(FreeImageIO *io, fi_handle handle, LONG length=0); 264 /** Used by the TIFF plugin */ 265 FIBITMAP* GetThumbnail() { 266 return _thumbnail.getDib(); 267 } 268}; 269 270#endif // PSDPARSER_H 271