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