Mudbox/LibPSD.h Source File

LibPSD.h
Go to the documentation of this file.
1 #ifndef __LIBPSD_H_
2 #define __LIBPSD_H_
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <vector>
6 
7 
8 static const char *PSDErrorString[] = {
9  "PSD_SUCCESS",
10  "PSD_FAIL",
11  "PSD_MEMORY_FAILURE",
12  "PSD_ERROR_OPENING_FILE",
13  "PSD_FOPEN_FAIL",
14  "PSD_FILE_NOT_PSD",
15  "PSD_READ_ERROR",
16  "PSD_WRITE_ERROR",
17  "PSD_WRONG_VERSION",
18  "PSD_NO_INSTANCE",
19  "PSD_INDEX_OUT_OF_RANGE",
20  "PSD_FILE_HEADER_NOT_VALID",
21  "PSD_PSDFILE_ERROR",
22  "PSD_INVALID_LAYER",
23  "PSD_CHANNEL_INDEX_OUT_OF_RANGE",
24  "PSD_LAYER_INDEX_OUT_OF_RANGE",
25  "PSD_DECOMPRESSION_ERROR",
26  "PSD_BITDEPTH_ERROR"
27 };
28 
29 
30 namespace adsk { namespace libPSD {
31 
32 class PSDFile;
33 
35 {
64 };
65 
66 enum PSDError {
85 };
86 
87 
88 
90 {
96 };
97 
99 {
100  unsigned int source;
101  unsigned int destination;
102  BlendChannelInfo() : source(0), destination(0) {}
103 };
104 
109 };
110 
112 {
113  unsigned int layerType;
114  unsigned int signature;
115  unsigned int blendKey;
116  SectionDividerSetting() : layerType(0), signature(0), blendKey(0) {}
117 };
118 
120 {
121  unsigned int signature;
122  unsigned int key;
123  unsigned int dataLength;
124  unsigned char* variableData;
125  AdditionalLayerInfo() : signature(0), key(0), dataLength(0), variableData(0) {}
126 };
127 
129  unsigned int hRes; // Fixed-point number: pixels per inch
130  unsigned short hResUnit; // 1=pixels per inch, 2=pixels per centimeter
131  unsigned short WidthUnit; // 1=in, 2=cm, 3=pt, 4=picas, 5=columns
132  unsigned int vRes; // Fixed-point number: pixels per inch
133  unsigned short vResUnit; // 1=pixels per inch, 2=pixels per centimeter
134  unsigned short HeightUnit; // 1=in, 2=cm, 3=pt, 4=picas, 5=columns
135  RESOLUTIONINFO() : hRes(0), hResUnit(0), WidthUnit(0), vRes(0), vResUnit(0), HeightUnit(0) {}
136 };
137 
138 struct LAYERSTATE {
139  unsigned short state; // 2 byte index of target layer (current layer) 0 = bottom.
140  LAYERSTATE() : state(0) {}
141 };
142 
144  unsigned int signature;
145  unsigned short key;
146  char* resName;
147  unsigned int resLength;
149  IMAGERESOURCES() : signature('8BIM'), key(0), resName(0), resLength(0), variableResData(0) {}
150 };
151 
153  int signature; //-- "8BPS"
154  unsigned short version; //-- Always 1
155  char fileRes[6];
156  unsigned short channels; //-- 1 to 24
157  unsigned int height; //-- Height
158  unsigned int width; //-- Width
159  unsigned short depthValue; //-- 1, 8, or 16, or 32.
160  unsigned short colorMode; //-- PH_xxx Above
161  PSD_FILE_HEADER() : signature('8BPS'), version(1), channels(4), height(0), width(0), depthValue(8), colorMode(3) {}
162 };
163 
165 {
166 public :
167  LRect(int left=0, int top=0, int right=0, int bottom=0);
168  LRect& operator= (const LRect& LR);
169 
170  int top;
171  int left;
172  int bottom;
173  int right;
174 } ;
175 
176 inline LRect::LRect(int inLeft, int inTop, int inRight, int inBottom)
177 {
178  left = inLeft ;
179  right = inRight ;
180  top = inTop ;
181  bottom = inBottom ;
182 }
183 
184 inline LRect& LRect::operator= (const LRect& LR)
185 {
186  if (this != &LR) {
187  this->bottom = LR.bottom;
188  this->left = LR.left;
189  this->right = LR.right;
190  this->top = LR.top;
191  }
192  return *this;
193 }
194 
195 // Layer information structures
197 {
198 public :
199  ChannelInfo();
200 
201  short channelID;
204  unsigned int channelPos;
205 } ;
206 
208  : channelID(0)
209  ,channelDataLen(0)
210  ,channelCompression(PSD_COMPRESSION_RAW)
211  ,channelPos(0)
212 { }
213 
214 //------------------------------------------------------------------------------
221 {
222 friend class PSDFile;
223 
224 public :
225  PSDLayerMeta();
226  ~PSDLayerMeta();
227 
229  unsigned short m_numChannels;
230  std::vector<ChannelInfo> m_channelInfo;
231 
232  unsigned int m_blendModeSig;
233  unsigned int m_blendModeKey;
234  unsigned char m_baseClipping;
235  unsigned char m_filler;
236  unsigned int m_extraDataField;
237 
238  unsigned int m_layerMaskSize;
242 
246  unsigned int m_blendRangeSize;
247  unsigned int m_grayBlendSource;
248  unsigned int m_grayBlendDest;
250 
251  unsigned int m_width;
252  unsigned int m_height;
253  unsigned int m_nbPixels;
254 
255  unsigned int m_numAdditionalLayers;
257 
258  // for layerType, see "lsct" Section Divider Setting
259  // on page 39 of File Format pdf
261  unsigned int m_layerId; // Layer ID which needs to be read and updated
262  // to the resources section.
263 
264 public:
265  const QString& getName() const { return m_layerName; }
266  void setName(const QString& layerName);
267 
268  // 0.0 is completely transparent, 1.0 is completely opaque
269  float getOpacity() const { return m_opacity/255.0f; }
270  void setOpacity(float opacity);
271 
272  // locked
273  bool getTransparencyProtected() const{return (bool)(m_optionFlags & 0x01);}
274  void setTransparencyProtected(bool trsProtected);
275 
276  bool getLocked() const { return m_bLocked; }
277  void setLocked(bool locked = true) { m_bLocked = locked; }
278 
279  // visibility flag
280  bool getVisibility() const {return (bool)!(m_optionFlags & 0x02);}
281  void setVisibility(bool visFlag);
282 
283 
284  PSDBlendMode getBlendingType() const;
285  void setBlendingType(PSDBlendMode blendMode);
286 
287  void setNbChannel(unsigned short nbChannel) {m_numChannels=nbChannel;}
288  void setLayerRect(int top, int left, int bottom, int right);
289  void setChannelInfo(CompressionMethod method,
290  unsigned short nbChannel = 4);
291 
292 private :
293  void readEachLayerRecord(PSDFile* psdFile,
294  unsigned int & actualSize);
295 
296  PSDLayerMeta& operator= (const PSDLayerMeta *LM);
297 
298  QString m_layerName;
299  unsigned char m_opacity;
300  unsigned char m_optionFlags;
301  bool m_bWroteToFile;
302  bool m_bLocked;
303 };
304 
305 //------------------------------------------------------------------------------
313 
314  friend class PSDFile;
315  // PSD File header block gets filled here.
316  struct VersionInfo {
317  unsigned int version;
318  bool hasRealMergedData;
319  int writerNameL;
320  char* writerName;
321  int readerNameL;
322  char* readerName;
323  unsigned int fileVersion;
324  VersionInfo() : version(0), hasRealMergedData(0), writerNameL(0),
325  writerName(0), readerNameL(0), readerName(0), fileVersion(0) {}
326  };
327 
328 
329  class ThumbnailResource
330  {
331  unsigned int format; // 1 = kJpegRGB . Also supports kRawRGB (0).
332  unsigned int width; // Width of thumbnail in pixels.
333  unsigned int height; // Height of thumbnail in pixels.
334  unsigned int width_bytes; // Padded row bytes = (width * bits per pixel + 31) / 32 * 4.
335  unsigned int total_size; // Total size = widthbytes * height * planes
336  unsigned int size_after_compression; // Used for consistency check.
337  unsigned int bits_per_pixel; // 24
338  unsigned int number_of_planes; // 1
339  unsigned char* jfif_data;
340  unsigned int* thumbnail_data;
341  ThumbnailResource() : format(0), width(0), height(0),
342  width_bytes(0), total_size(0), size_after_compression(0),
343  bits_per_pixel(0), number_of_planes(0), jfif_data(0), thumbnail_data(0) {}
344  };
345 
346 private:
347  void DestroyVI();
348  void DestroyThumbnail();
349 
350  RESOLUTIONINFO* m_pResolutionInfo;
351  unsigned int m_imageResourcesSize;
352  unsigned char* m_colorData;
353  unsigned int m_colorDataSize;
354  LAYERSTATE* m_pLayerState;
355  PSDFileMeta& operator= (const PSDFileMeta &FM);
356 
357 public:
358  PSDFileMeta();
359  ~PSDFileMeta();
360  /*
361  * Get/Set the X Resolution in Pixel per Inch.
362  */
363  float GetXResolution() const;
364  void SetXResolution(float xres);
365  /*
366  * Get/Set the Y Resolution in Pixel per Inch.
367  */
368  float GetYResolution() const;
369  void SetYResolution(float yres);
370  /*
371  * Get the resolution unit
372  * @Return value short 0=none, 1=pixels per inch, 2=pixels per centimeter
373  */
374  unsigned short GetResUnit() const;
375  void SetResUnit(unsigned short resUnit);
376  bool SetFileHeaderStruct(unsigned int inWidth, unsigned int inHeight,
377  unsigned short inColorDepth,
378  unsigned short inChannels);
379  /*
380  * Get/Set the number of layer.
381  *
382  */
383  unsigned int GetNbLayers() const { return m_numLayers; }
384  void SetNbLayers(unsigned int nbLayers) { m_numLayers = nbLayers; }
385 
386  // ColorModeData
387  bool SetColorModeData(unsigned char* colorModeData,
388  unsigned int colorModeDataSize);
389 
390  // Get/Set the layer state.
391  bool LayerStateValid() const { return m_pLayerState != 0; }
392  unsigned short GetLayerState() const { return m_pLayerState->state; }
393  void SetLayerState(unsigned short layerState);
394 
395  // Synch File Resources
396  void Synch();
397 
399  VersionInfo m_vi;
400  ThumbnailResource* m_pThumbnail;
401  unsigned int m_numLayers; // to obtain the layer count, we need to
402  // read the layer&mask section.
403  std::vector<IMAGERESOURCES*> m_vImageRes;
404 };
405 
406 
407 //------------------------------------------------------------------------------
422 PSDFile MBDLL_DECL *PSDOpen(const QString& fileName, bool write = false);
423 
424 //------------------------------------------------------------------------------
433 PSDError MBDLL_DECL PSDGetLastError(PSDFile *psdFile);
434 
435 //------------------------------------------------------------------------------
445 const MBDLL_DECL PSDFileMeta *PSDGetFileMeta(const PSDFile *psdFile);
446 
447 //------------------------------------------------------------------------------
460 bool MBDLL_DECL PSDSetFileMeta(PSDFile *psdFile,
461  const PSDFileMeta *psdFileMeta);
462 
463 //------------------------------------------------------------------------------
475 const MBDLL_DECL PSDLayerMeta *PSDGetLayerMeta(const PSDFile *psdFile,
476  unsigned int layerIndex);
477 
478 
479 //------------------------------------------------------------------------------
491 void MBDLL_DECL PSDSetLayerMeta(PSDFile *psdFile, unsigned int layerIndex,
492  PSDLayerMeta *psdLayerMeta);
493 
494 
495 //------------------------------------------------------------------------------
515 bool MBDLL_DECL PSDReadChannelImageData(PSDFile *psdFile,
516  unsigned int layerIndex,
517  unsigned int channelIndex,
518  void *buffer,
519  size_t bufferSize);
520 
521 bool MBDLL_DECL PSDWriteChannelImageData(PSDFile *psdFile,
522  unsigned int layerIndex,
523  unsigned int channelIndex,
524  void *buffer,
525  size_t bufferSize);
526 
527 bool MBDLL_DECL PSDReadChannelImageData(PSDFile *psdFile,
528  void *buffer,
529  size_t bufferSize);
530 
531 bool MBDLL_DECL PSDReadCompositeImageData(PSDFile *psdFile,
532  unsigned int imageWidth,
533  unsigned int imageHeight,
534  void *buffer,
535  size_t bufferSize);
536 
537 bool MBDLL_DECL PSDWriteCompositeImageData(PSDFile *psdFile,
538  unsigned int imageWidth,
539  unsigned int imageHeight,
540  CompressionMethod compressMethod,
541  void *buffer,
542  size_t bufferSize);
543 
544 bool MBDLL_DECL PSDWrite4ChanCompositeImageData(PSDFile *psdFile,
545  unsigned int imageWidth,
546  unsigned int imageHeight,
547  CompressionMethod compressMethod,
548  void **ARGBPixels);
549 
550 //------------------------------------------------------------------------------
559 void MBDLL_DECL PSDClose(PSDFile *psdFile);
560 
561 
562 
563 // Helper/utility functions
564 
565 //-----------------------------------------------------------------------------
588 bool MBDLL_DECL PSDBlendLayers(unsigned char* rSrc, unsigned char* gSrc,
589  unsigned char* bSrc, unsigned char* aSrc,
590  float glbOpacity, LRect inRectLayer1,
591  int blendMode, unsigned int inWidth, unsigned int inHeight,
592  unsigned char* &rDst, unsigned char* &gDst,
593  unsigned char* &bDst, unsigned char* &aDst);
594 
595 //-----------------------------------------------------------------------------
615 void MBDLL_DECL PSDInterleaveImageData(int nPixels, const unsigned char *r, const unsigned char *g,
616  const unsigned char *b, const unsigned char *a,
617  unsigned int *target, bool multAlpha=true);
618 
619 //-----------------------------------------------------------------------------
640 void MBDLL_DECL PSDInterleaveImageData16_16(int nPixels,
641  const unsigned short *r, const unsigned short *g,
642  const unsigned short *b, const unsigned short *a,
643  unsigned short *target, bool multAlpha=true);
644 
645 
646 //-----------------------------------------------------------------------------
667 void MBDLL_DECL PSDInterleaveImageData32_32(int nPixels, const float *r, const float *g,
668  const float *b, const float *a,
669  float *target, bool multAlpha=true);
670 
671 
672 //-----------------------------------------------------------------------------
693 void MBDLL_DECL PSDInterleaveImageData16(int nPixels, const unsigned short *r, const unsigned short *g,
694  const unsigned short *b, const unsigned short *a,
695  unsigned int *target, bool multAlpha=true);
696 
697 //-----------------------------------------------------------------------------
718 void MBDLL_DECL PSDInterleaveImageData32(int nPixels, const float *r, const float *g,
719  const float *b, const float *a,
720  unsigned int *target, bool multAlpha=true);
721 
722 
723 };}; //namespace Adsk;libPSD
724 #endif
725 
726 
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
std::vector< ChannelInfo > m_channelInfo
Definition: LibPSD.h:230
AdditionalLayerInfo * m_listAddLayerInfo
Definition: LibPSD.h:256
unsigned char m_filler
Definition: LibPSD.h:235
unsigned int m_blendModeKey
Definition: LibPSD.h:233
float getOpacity() const
Definition: LibPSD.h:269
unsigned int m_numAdditionalLayers
Definition: LibPSD.h:255
unsigned int m_nbPixels
Definition: LibPSD.h:253
bool MBDLL_DECL PSDWrite4ChanCompositeImageData(PSDFile *psdFile, unsigned int imageWidth, unsigned int imageHeight, CompressionMethod compressMethod, void **ARGBPixels)
The PSDLayerMeta object contains informations about a layer.
Definition: LibPSD.h:220
unsigned short key
Definition: LibPSD.h:145
bool MBDLL_DECL PSDWriteCompositeImageData(PSDFile *psdFile, unsigned int imageWidth, unsigned int imageHeight, CompressionMethod compressMethod, void *buffer, size_t bufferSize)
Definition: LibPSD.h:30
GLenum GLsizei width
Definition: GLee.h:873
unsigned short state
Definition: LibPSD.h:139
unsigned short hResUnit
Definition: LibPSD.h:130
unsigned int m_blendModeSig
Definition: LibPSD.h:232
unsigned int signature
Definition: LibPSD.h:144
bool MBDLL_DECL PSDWriteChannelImageData(PSDFile *psdFile, unsigned int layerIndex, unsigned int channelIndex, void *buffer, size_t bufferSize)
unsigned int resLength
Definition: LibPSD.h:147
PSDError MBDLL_DECL PSDGetLastError(PSDFile *psdFile)
Obtain the last status of libPSD.
bool getTransparencyProtected() const
Definition: LibPSD.h:273
unsigned short m_numChannels
Definition: LibPSD.h:229
unsigned short vResUnit
Definition: LibPSD.h:133
unsigned short WidthUnit
Definition: LibPSD.h:131
unsigned int m_layerId
Definition: LibPSD.h:261
bool MBDLL_DECL PSDReadChannelImageData(PSDFile *psdFile, unsigned int layerIndex, unsigned int channelIndex, void *buffer, size_t bufferSize)
Read the channel image data for this layer and this channel of psdFile object.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
void MBDLL_DECL PSDInterleaveImageData32(int nPixels, const float *r, const float *g, const float *b, const float *a, unsigned int *target, bool multAlpha=true)
InterleaveImageData32 takes the 4 separate 32 bit float RGBA planes and interleaves them into a singl...
unsigned short HeightUnit
Definition: LibPSD.h:134
LRect(int left=0, int top=0, int right=0, int bottom=0)
Definition: LibPSD.h:176
unsigned int m_height
Definition: LibPSD.h:252
const MBDLL_DECL PSDFileMeta * PSDGetFileMeta(const PSDFile *psdFile)
Get the file metadata of a PSD file.
void MBDLL_DECL PSDInterleaveImageData32_32(int nPixels, const float *r, const float *g, const float *b, const float *a, float *target, bool multAlpha=true)
InterleaveImageData32 takes the 4 separate 32 bit float RGBA planes and interleaves them into a singl...
bool MBDLL_DECL PSDReadCompositeImageData(PSDFile *psdFile, unsigned int imageWidth, unsigned int imageHeight, void *buffer, size_t bufferSize)
unsigned short channels
Definition: LibPSD.h:156
LRect & operator=(const LRect &LR)
Definition: LibPSD.h:184
unsigned short colorMode
Definition: LibPSD.h:160
GLenum GLsizei GLenum format
Definition: GLee.h:873
unsigned int GetNbLayers() const
Definition: LibPSD.h:383
The PSDFileMeta object contains meta data about the psd file, such header information, the number of layers, etc...
Definition: LibPSD.h:312
void MBDLL_DECL PSDInterleaveImageData16_16(int nPixels, const unsigned short *r, const unsigned short *g, const unsigned short *b, const unsigned short *a, unsigned short *target, bool multAlpha=true)
InterleaveImageData16 takes the 4 separate 16 bit RGBA planes and interleaves them into a single 16 b...
GLubyte g
Definition: GLee.h:5404
ThumbnailResource * m_pThumbnail
Definition: LibPSD.h:400
GLenum GLsizei GLsizei height
Definition: GLee.h:883
unsigned short version
Definition: LibPSD.h:154
PSDFile MBDLL_DECL * PSDOpen(const QString &fileName, bool write=false)
Open a Photoshop(PSD) file and fill internally meta data (file meta and layers meta data)...
CompressionMethod channelCompression
Definition: LibPSD.h:203
bool getLocked() const
Definition: LibPSD.h:276
unsigned int m_numLayers
Definition: LibPSD.h:401
bool MBDLL_DECL PSDBlendLayers(unsigned char *rSrc, unsigned char *gSrc, unsigned char *bSrc, unsigned char *aSrc, float glbOpacity, LRect inRectLayer1, int blendMode, unsigned int inWidth, unsigned int inHeight, unsigned char *&rDst, unsigned char *&gDst, unsigned char *&bDst, unsigned char *&aDst)
BlendLayers is a helper function that blend 2 layers based on the src's blend mode.
void MBDLL_DECL PSDInterleaveImageData(int nPixels, const unsigned char *r, const unsigned char *g, const unsigned char *b, const unsigned char *a, unsigned int *target, bool multAlpha=true)
InterleaveImageData takes the 4 seperate RGBA planes and interleaves them into a single RGBA image –...
void MBDLL_DECL PSDInterleaveImageData16(int nPixels, const unsigned short *r, const unsigned short *g, const unsigned short *b, const unsigned short *a, unsigned int *target, bool multAlpha=true)
InterleaveImageData16 takes the 4 separate 16 bit RGBA planes and interleaves them into a single 8 bi...
GLubyte GLubyte b
Definition: GLee.h:5404
unsigned int channelPos
Definition: LibPSD.h:204
SectionDividerSetting m_sectionDivider
Definition: LibPSD.h:260
unsigned char m_baseClipping
Definition: LibPSD.h:234
unsigned int m_layerMaskSize
Definition: LibPSD.h:238
GLuint buffer
Definition: GLee.h:1557
std::vector< IMAGERESOURCES * > m_vImageRes
Definition: LibPSD.h:403
static const char * PSDErrorString[]
Definition: LibPSD.h:8
void MBDLL_DECL PSDClose(PSDFile *psdFile)
Close the psdFile object and free all private data.
const QString & getName() const
Definition: LibPSD.h:265
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
bool MBDLL_DECL PSDSetFileMeta(PSDFile *psdFile, const PSDFileMeta *psdFileMeta)
Set the file metadata of psdFile.
unsigned short GetLayerState() const
Definition: LibPSD.h:392
void MBDLL_DECL PSDSetLayerMeta(PSDFile *psdFile, unsigned int layerIndex, PSDLayerMeta *psdLayerMeta)
Set the layer meta data for layerIndex of psdFile object.
const MBDLL_DECL PSDLayerMeta * PSDGetLayerMeta(const PSDFile *psdFile, unsigned int layerIndex)
Get the layer meta data for layerIndex of psdFile object.
unsigned int m_extraDataField
Definition: LibPSD.h:236
unsigned int m_blendRangeSize
Definition: LibPSD.h:246
bool getVisibility() const
Definition: LibPSD.h:280
void setLocked(bool locked=true)
Definition: LibPSD.h:277
void SetNbLayers(unsigned int nbLayers)
Definition: LibPSD.h:384
bool LayerStateValid() const
Definition: LibPSD.h:391
BlendChannelInfo * m_BlendChannelRange
Definition: LibPSD.h:249
PSD_FILE_HEADER m_hdr
Definition: LibPSD.h:398
unsigned short depthValue
Definition: LibPSD.h:159
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
CompressionMethod
Definition: LibPSD.h:89
unsigned char * variableData
Definition: LibPSD.h:124
#define MBDLL_DECL
Definition: dllinterface.h:35
unsigned int m_width
Definition: LibPSD.h:251
unsigned int m_grayBlendSource
Definition: LibPSD.h:247
unsigned int m_grayBlendDest
Definition: LibPSD.h:248
void setNbChannel(unsigned short nbChannel)
Definition: LibPSD.h:287