LibPSD.h

Go to the documentation of this file.
00001 #ifndef __LIBPSD_H_       
00002 #define __LIBPSD_H_
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <vector>
00006 
00007 
00008 static const char *PSDErrorString[] = {
00009    "PSD_SUCCESS",
00010    "PSD_FAIL",
00011    "PSD_MEMORY_FAILURE",
00012    "PSD_ERROR_OPENING_FILE",
00013    "PSD_FOPEN_FAIL",
00014    "PSD_FILE_NOT_PSD",
00015    "PSD_READ_ERROR",
00016    "PSD_WRITE_ERROR",
00017    "PSD_WRONG_VERSION",
00018    "PSD_NO_INSTANCE",
00019    "PSD_INDEX_OUT_OF_RANGE",
00020    "PSD_FILE_HEADER_NOT_VALID",
00021    "PSD_PSDFILE_ERROR",
00022    "PSD_INVALID_LAYER",
00023    "PSD_CHANNEL_INDEX_OUT_OF_RANGE", 
00024    "PSD_LAYER_INDEX_OUT_OF_RANGE", 
00025    "PSD_DECOMPRESSION_ERROR",
00026    "PSD_BITDEPTH_ERROR"
00027 };
00028 
00029 
00030 namespace adsk { namespace libPSD { 
00031                                           
00032 class PSDFile;
00033 
00034 enum PSDBlendMode
00035 {
00036     blendNORMAL = 0,
00037     blendMULTIPLY,
00038     blendDARKEN,
00039     blendDIFFERENCE,
00040     blendEXCLUSION,
00041     blendHARDLIGHT,
00042     blendCOLORDODGE,
00043     blendLIGHTEN,
00044     blendSCREEN,
00045     blendCOLORBURN,
00046     blendOVERLAY,
00047     blendHUE,
00048     blendSATURATION,
00049     blendLUMINOSITY,
00050     blendCOLOR,
00051     blendDISSOLVE,
00052     blendSOFTLIGHT,
00053     blendLINEARDODGE,
00054     blendVIVIDLIGHT,
00055     blendLINEARBURN,
00056     blendLINEARLIGHT,
00057     blendPINLIGHT,
00058     blendHARDMIX, 
00059     blendDARKERCOLOR,
00060     blendLIGHTERCOLOR,
00061     blendSUBTRACT,
00062     blendDIVIDE,          
00063     blendUNKNOW
00064 };
00065 
00066 enum PSDError {
00067     PSD_SUCCESS = 0,
00068     PSD_FAIL,
00069     PSD_MEMORY_FAILURE,
00070     PSD_ERROR_OPENING_FILE,
00071     PSD_FOPEN_FAIL,
00072     PSD_FILE_NOT_PSD,
00073     PSD_READ_ERROR,
00074     PSD_WRITE_ERROR,
00075     PSD_WRONG_VERSION,
00076     PSD_NO_INSTANCE,
00077     PSD_INDEX_OUT_OF_RANGE,
00078     PSD_FILE_HEADER_NOT_VALID,
00079     PSD_PSDFILE_ERROR,
00080     PSD_INVALID_LAYER,
00081     PSD_CHANNEL_INDEX_OUT_OF_RANGE, 
00082     PSD_LAYER_INDEX_OUT_OF_RANGE, 
00083     PSD_DECOMPRESSION_ERROR,
00084     PSD_BITDEPTH_ERROR
00085 };
00086 
00087 
00088 
00089 enum CompressionMethod 
00090 {
00091     PSD_COMPRESSION_NONE = -1,
00092     PSD_COMPRESSION_RAW, 
00093     PSD_COMPRESSION_RLE,        
00094     PSD_COMPRESSION_ZIP, 
00095     PSD_COMPRESSION_ZIP_PREDICTION 
00096 };
00097 
00098 struct BlendChannelInfo
00099 {
00100     unsigned int  source;
00101     unsigned int  destination;
00102     BlendChannelInfo() : source(0), destination(0) {}
00103 };
00104 
00105 enum layerType { LAYER_NORMAL = 0, 
00106                  LAYER_SET_OPEN, 
00107                  LAYER_SET_CLOSED, 
00108                  LAYER_DIVIDER 
00109 };
00110 
00111 struct SectionDividerSetting
00112 {
00113     unsigned int    layerType;
00114     unsigned int    signature;
00115     unsigned int    blendKey;
00116     SectionDividerSetting() : layerType(0), signature(0), blendKey(0) {}
00117 };
00118 
00119 struct AdditionalLayerInfo
00120 {
00121     unsigned int    signature;
00122     unsigned int    key;
00123     unsigned int    dataLength;
00124     unsigned char*  variableData;
00125     AdditionalLayerInfo() : signature(0), key(0), dataLength(0), variableData(0) {}
00126 };
00127 
00128 struct RESOLUTIONINFO {
00129    unsigned int     hRes;         // Fixed-point number: pixels per inch
00130    unsigned short   hResUnit;     // 1=pixels per inch, 2=pixels per centimeter
00131    unsigned short   WidthUnit;    // 1=in, 2=cm, 3=pt, 4=picas, 5=columns
00132    unsigned int     vRes;         // Fixed-point number: pixels per inch
00133    unsigned short   vResUnit;     // 1=pixels per inch, 2=pixels per centimeter
00134    unsigned short   HeightUnit;   // 1=in, 2=cm, 3=pt, 4=picas, 5=columns 
00135    RESOLUTIONINFO() : hRes(0), hResUnit(0), WidthUnit(0), vRes(0), vResUnit(0), HeightUnit(0) {}
00136 };
00137 
00138 struct LAYERSTATE {        
00139     unsigned short  state;        // 2 byte index of target layer (current layer) 0 = bottom.
00140     LAYERSTATE() : state(0) {}
00141 };
00142 
00143 struct IMAGERESOURCES {
00144     unsigned int    signature;
00145     unsigned short  key;
00146     char*           resName;
00147     unsigned int    resLength;
00148     char*           variableResData;
00149     IMAGERESOURCES() : signature('8BIM'), key(0), resName(0), resLength(0), variableResData(0) {}
00150 };
00151 
00152 struct PSD_FILE_HEADER {
00153     int             signature;  //-- "8BPS"
00154     unsigned short  version;    //-- Always 1
00155     char            fileRes[6];
00156     unsigned short  channels;   //-- 1 to 24
00157     unsigned int    height;     //-- Height 
00158     unsigned int    width;      //-- Width
00159     unsigned short  depthValue; //-- 1, 8, or 16, or 32.
00160     unsigned short  colorMode;  //-- PH_xxx Above
00161     PSD_FILE_HEADER() : signature('8BPS'), version(1), channels(4), height(0), width(0), depthValue(8), colorMode(3) {}
00162 };
00163 
00164 class MBDLL_DECL LRect
00165 {
00166 public  :
00167     LRect(int left=0, int top=0, int right=0, int bottom=0);
00168     LRect& operator= (const LRect& LR);
00169 
00170     int  top;
00171     int  left;
00172     int  bottom;
00173     int  right;
00174 } ;
00175 
00176 inline LRect::LRect(int inLeft, int inTop, int inRight, int inBottom)
00177 {
00178     left    = inLeft ;
00179     right   = inRight ;
00180     top     = inTop ;
00181     bottom  = inBottom ;
00182 }
00183 
00184 inline  LRect& LRect::operator= (const LRect& LR)
00185 {
00186     if (this != &LR) {
00187         this->bottom = LR.bottom;
00188         this->left   = LR.left;
00189         this->right  = LR.right;
00190         this->top    = LR.top;
00191     }
00192     return *this;
00193 }
00194 
00195 // Layer information structures
00196 class  MBDLL_DECL ChannelInfo
00197 {
00198 public :
00199         ChannelInfo();
00200 
00201         short               channelID;
00202         int                 channelDataLen;
00203         CompressionMethod   channelCompression;
00204         unsigned int        channelPos;
00205 } ;
00206 
00207 inline ChannelInfo::ChannelInfo()
00208     : channelID(0)
00209     ,channelDataLen(0)
00210     ,channelCompression(PSD_COMPRESSION_RAW)
00211     ,channelPos(0)
00212 { }
00213 
00214 //------------------------------------------------------------------------------
00220 class  MBDLL_DECL PSDLayerMeta
00221 {
00222 friend class PSDFile;
00223 
00224 public :
00225     PSDLayerMeta();
00226     ~PSDLayerMeta();
00227 
00228     LRect                     m_layerRect;
00229     unsigned short            m_numChannels;
00230     std::vector<ChannelInfo>  m_channelInfo;
00231 
00232     unsigned int              m_blendModeSig;
00233     unsigned int              m_blendModeKey;
00234     unsigned char             m_baseClipping;
00235     unsigned char             m_filler;
00236     unsigned int              m_extraDataField;
00237 
00238     unsigned int              m_layerMaskSize;
00239     LRect                     m_vectorMaskRect;
00240     char                      m_vectorMaskColor;
00241     char                      m_vectorMaskFlags;
00242 
00243     LRect                     m_layerMaskRect;
00244     char                      m_layerMaskFlags;
00245     char                      m_userMaskBgColor;
00246     unsigned int              m_blendRangeSize;
00247     unsigned int              m_grayBlendSource;
00248     unsigned int              m_grayBlendDest;
00249     BlendChannelInfo*         m_BlendChannelRange; 
00250 
00251     unsigned int              m_width;
00252     unsigned int              m_height;
00253     unsigned int              m_nbPixels;
00254 
00255     unsigned int              m_numAdditionalLayers;
00256     AdditionalLayerInfo*      m_listAddLayerInfo;
00257 
00258     // for layerType, see "lsct" Section Divider Setting 
00259     //  on page 39 of File Format pdf 
00260     SectionDividerSetting     m_sectionDivider; // lsct data
00261     unsigned int              m_layerId;        // Layer ID which needs to be read and updated
00262                                                 // to the resources section.
00263 
00264 public:
00265     const QString&      getName() const { return m_layerName; }
00266     void                setName(const QString& layerName);
00267 
00268     // 0.0 is completely transparent, 1.0 is completely opaque
00269     float               getOpacity() const { return m_opacity/255.0f; }
00270     void                setOpacity(float opacity);
00271 
00272     // locked
00273     bool                getTransparencyProtected() const{return (bool)(m_optionFlags & 0x01);}
00274     void                setTransparencyProtected(bool trsProtected);
00275 
00276     bool                getLocked() const             { return m_bLocked;   }
00277     void                setLocked(bool locked = true) { m_bLocked = locked; }
00278     
00279     // visibility flag
00280     bool                getVisibility() const {return (bool)!(m_optionFlags & 0x02);}
00281     void                setVisibility(bool visFlag);
00282 
00283 
00284     PSDBlendMode        getBlendingType() const;
00285     void                setBlendingType(PSDBlendMode blendMode);
00286 
00287     void                setNbChannel(unsigned short nbChannel) {m_numChannels=nbChannel;}
00288     void                setLayerRect(int top, int left, int bottom, int right);
00289     void                setChannelInfo(CompressionMethod method, 
00290                                        unsigned short nbChannel = 4);
00291 
00292 private :
00293     void                readEachLayerRecord(PSDFile*       psdFile, 
00294                                             unsigned int & actualSize);
00295 
00296     PSDLayerMeta&       operator=   (const PSDLayerMeta *LM);
00297 
00298     QString             m_layerName;
00299     unsigned char       m_opacity;
00300     unsigned char       m_optionFlags;
00301     bool                m_bWroteToFile;
00302     bool                m_bLocked;
00303 };
00304 
00305 //------------------------------------------------------------------------------
00312 class  MBDLL_DECL PSDFileMeta {
00313 
00314     friend class PSDFile;
00315     // PSD File header block gets filled here.
00316     struct VersionInfo {    
00317         unsigned int    version;
00318         bool            hasRealMergedData;
00319         int             writerNameL;
00320         char*           writerName;
00321         int             readerNameL;
00322         char*           readerName;
00323         unsigned int    fileVersion;
00324         VersionInfo() : version(0), hasRealMergedData(0), writerNameL(0), 
00325                         writerName(0), readerNameL(0), readerName(0), fileVersion(0) {}
00326     };
00327 
00328 
00329     class ThumbnailResource
00330     {
00331         unsigned int    format;                 // 1 = kJpegRGB . Also supports kRawRGB (0).
00332         unsigned int    width;                  // Width of thumbnail in pixels.
00333         unsigned int    height;                 // Height of thumbnail in pixels.
00334         unsigned int    width_bytes;            // Padded row bytes = (width * bits per pixel + 31) / 32 * 4.
00335         unsigned int    total_size;             // Total size = widthbytes * height * planes
00336         unsigned int    size_after_compression; // Used for consistency check.
00337         unsigned int    bits_per_pixel;         // 24
00338         unsigned int    number_of_planes;       // 1
00339         unsigned char*  jfif_data;             
00340         unsigned int*   thumbnail_data;  
00341         ThumbnailResource() : format(0), width(0), height(0),                
00342                               width_bytes(0), total_size(0), size_after_compression(0), 
00343                               bits_per_pixel(0), number_of_planes(0), jfif_data(0), thumbnail_data(0) {}       
00344     };
00345 
00346 private:
00347     void DestroyVI();
00348     void DestroyThumbnail();
00349 
00350     RESOLUTIONINFO*     m_pResolutionInfo;
00351     unsigned int        m_imageResourcesSize;
00352     unsigned char*      m_colorData;
00353     unsigned int        m_colorDataSize;
00354     LAYERSTATE*         m_pLayerState;
00355     PSDFileMeta&        operator= (const PSDFileMeta &FM); 
00356 
00357 public: 
00358     PSDFileMeta();
00359     ~PSDFileMeta();
00360     /*
00361     * Get/Set the X Resolution in Pixel per Inch.
00362     */
00363     float               GetXResolution() const;
00364     void                SetXResolution(float xres);
00365     /*
00366     * Get/Set the Y Resolution in Pixel per Inch.
00367     */
00368     float               GetYResolution() const;
00369     void                SetYResolution(float yres);
00370     /*
00371     * Get the resolution unit 
00372     * @Return value short 0=none, 1=pixels per inch, 2=pixels per centimeter
00373     */
00374     unsigned short      GetResUnit() const;
00375     void                SetResUnit(unsigned short resUnit);
00376     bool                SetFileHeaderStruct(unsigned int inWidth, unsigned int inHeight, 
00377                                             unsigned short inColorDepth, 
00378                                             unsigned short inChannels);
00379     /*
00380     * Get/Set the number of layer.
00381     *
00382     */
00383     unsigned int        GetNbLayers() const                { return m_numLayers; }
00384     void                SetNbLayers(unsigned int nbLayers) { m_numLayers = nbLayers; }
00385 
00386     // ColorModeData
00387     bool                SetColorModeData(unsigned char* colorModeData, 
00388                                          unsigned int   colorModeDataSize);
00389 
00390     // Get/Set the layer state.
00391     bool                LayerStateValid() const { return m_pLayerState != 0;   }
00392     unsigned short      GetLayerState()   const { return m_pLayerState->state; }
00393     void                SetLayerState(unsigned short layerState);
00394     
00395     // Synch File Resources
00396     void                Synch();
00397 
00398     PSD_FILE_HEADER     m_hdr;
00399     VersionInfo         m_vi;
00400     ThumbnailResource*  m_pThumbnail;
00401     unsigned int        m_numLayers; // to obtain the layer count, we need to 
00402                                      // read the layer&mask section.
00403     std::vector<IMAGERESOURCES*> m_vImageRes;
00404 };
00405 
00406 
00407 //------------------------------------------------------------------------------
00422 PSDFile   MBDLL_DECL    *PSDOpen(const QString& fileName, bool write = false);
00423 
00424 //------------------------------------------------------------------------------
00433 PSDError   MBDLL_DECL   PSDGetLastError(PSDFile *psdFile);
00434 
00435 //------------------------------------------------------------------------------
00445 const MBDLL_DECL  PSDFileMeta   *PSDGetFileMeta(const PSDFile *psdFile);
00446 
00447 //------------------------------------------------------------------------------
00460 bool  MBDLL_DECL        PSDSetFileMeta(PSDFile *psdFile, 
00461                                        const PSDFileMeta *psdFileMeta);
00462 
00463 //------------------------------------------------------------------------------
00475 const MBDLL_DECL  PSDLayerMeta     *PSDGetLayerMeta(const PSDFile *psdFile, 
00476                                         unsigned int   layerIndex);
00477 
00478 
00479 //------------------------------------------------------------------------------
00491 void  MBDLL_DECL        PSDSetLayerMeta(PSDFile *psdFile, unsigned int layerIndex, 
00492                                         PSDLayerMeta *psdLayerMeta);
00493 
00494 
00495 //------------------------------------------------------------------------------
00515 bool MBDLL_DECL PSDReadChannelImageData(PSDFile         *psdFile, 
00516                                         unsigned int     layerIndex, 
00517                                         unsigned int     channelIndex, 
00518                                         void            *buffer, 
00519                                         size_t           bufferSize);
00520                             
00521 bool MBDLL_DECL PSDWriteChannelImageData(PSDFile        *psdFile, 
00522                                          unsigned int    layerIndex, 
00523                                          unsigned int    channelIndex, 
00524                                          void           *buffer, 
00525                                          size_t          bufferSize);
00526                              
00527 bool MBDLL_DECL PSDReadChannelImageData(PSDFile         *psdFile, 
00528                                         void            *buffer, 
00529                                         size_t           bufferSize);
00530 
00531 bool MBDLL_DECL PSDReadCompositeImageData(PSDFile       *psdFile, 
00532                                           unsigned int   imageWidth, 
00533                                           unsigned int   imageHeight, 
00534                                           void          *buffer, 
00535                                           size_t         bufferSize);     
00536                               
00537 bool MBDLL_DECL PSDWriteCompositeImageData(PSDFile      *psdFile, 
00538                                            unsigned int  imageWidth, 
00539                                            unsigned int  imageHeight, 
00540                                            CompressionMethod compressMethod, 
00541                                            void         *buffer, 
00542                                            size_t        bufferSize);
00543      
00544 bool MBDLL_DECL PSDWrite4ChanCompositeImageData(PSDFile     *psdFile, 
00545                                                 unsigned int imageWidth, 
00546                                                 unsigned int imageHeight, 
00547                                                 CompressionMethod compressMethod, 
00548                                                 void **ARGBPixels);
00549  
00550 //------------------------------------------------------------------------------
00559 void  MBDLL_DECL        PSDClose(PSDFile *psdFile);
00560 
00561 
00562 
00563 // Helper/utility functions
00564 
00565 //-----------------------------------------------------------------------------
00588 bool  MBDLL_DECL PSDBlendLayers(unsigned char* rSrc, unsigned char* gSrc,
00589                     unsigned char* bSrc, unsigned char* aSrc, 
00590                     float glbOpacity,    LRect          inRectLayer1, 
00591                     int blendMode, unsigned int inWidth, unsigned int inHeight,
00592                     unsigned char* &rDst, unsigned char* &gDst,
00593                     unsigned char* &bDst, unsigned char* &aDst);
00594 
00595 //-----------------------------------------------------------------------------
00615 void  MBDLL_DECL PSDInterleaveImageData(int nPixels, const unsigned char *r, const unsigned char *g, 
00616                                          const unsigned char *b, const unsigned char *a, 
00617                                          unsigned int *target, bool multAlpha=true);
00618 
00619 //-----------------------------------------------------------------------------
00640 void  MBDLL_DECL PSDInterleaveImageData16_16(int nPixels, 
00641                                  const unsigned short *r, const unsigned short *g, 
00642                                  const unsigned short *b, const unsigned short *a, 
00643                                  unsigned short *target, bool multAlpha=true);
00644 
00645 
00646 //-----------------------------------------------------------------------------
00667 void  MBDLL_DECL PSDInterleaveImageData32_32(int nPixels, const float *r, const float *g, 
00668                                               const float *b, const float *a, 
00669                                               float *target,  bool multAlpha=true);
00670 
00671 
00672 //-----------------------------------------------------------------------------
00693 void  MBDLL_DECL PSDInterleaveImageData16(int nPixels, const unsigned short *r, const unsigned short *g, 
00694                                            const unsigned short *b, const unsigned short *a, 
00695                                            unsigned int *target, bool multAlpha=true);
00696 
00697 //-----------------------------------------------------------------------------
00718 void  MBDLL_DECL PSDInterleaveImageData32(int nPixels, const float *r, const float *g, 
00719                                            const float *b, const float *a, 
00720                                            unsigned int *target, bool multAlpha=true);
00721 
00722 
00723 };}; //namespace Adsk;libPSD 
00724 #endif
00725 
00726