Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:47:25

0001 // Created on: 2012-07-18
0002 // Created by: Kirill GAVRILOV
0003 // Copyright (c) 2012-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef Image_PixMapData_HeaderFile
0017 #define Image_PixMapData_HeaderFile
0018 
0019 #include <Image_Color.hxx>
0020 #include <NCollection_Buffer.hxx>
0021 #include <NCollection_Vec3.hxx>
0022 
0023 //! Structure to manage image buffer.
0024 class Image_PixMapData : public NCollection_Buffer
0025 {
0026 public:
0027 
0028   //! Empty constructor.
0029   Image_PixMapData()
0030   : NCollection_Buffer (Handle(NCollection_BaseAllocator)()),
0031     myTopRowPtr  (NULL),
0032     SizeBPP      (0),
0033     SizeX        (0),
0034     SizeY        (0),
0035     SizeZ        (0),
0036     SizeRowBytes (0),
0037     SizeSliceBytes (0),
0038     TopToDown    (Standard_Size(-1))
0039   {
0040     //
0041   }
0042 
0043   //! Initializer.
0044   bool Init (const Handle(NCollection_BaseAllocator)& theAlloc,
0045              const Standard_Size                      theSizeBPP,
0046              const Standard_Size                      theSizeX,
0047              const Standard_Size                      theSizeY,
0048              const Standard_Size                      theSizeRowBytes,
0049              Standard_Byte*                           theDataPtr)
0050   {
0051     return Init (theAlloc, theSizeBPP, NCollection_Vec3<Standard_Size> (theSizeX, theSizeY, 1), theSizeRowBytes, theDataPtr);
0052   }
0053 
0054   //! Initializer.
0055   bool Init (const Handle(NCollection_BaseAllocator)& theAlloc,
0056              const Standard_Size                      theSizeBPP,
0057              const NCollection_Vec3<Standard_Size>&   theSizeXYZ,
0058              const Standard_Size                      theSizeRowBytes,
0059              Standard_Byte*                           theDataPtr)
0060   {
0061     SetAllocator (theAlloc); // will free old data as well
0062 
0063     myData       = theDataPtr;
0064     myTopRowPtr  = NULL;
0065     SizeBPP      = theSizeBPP;
0066     SizeX        = theSizeXYZ.x();
0067     SizeY        = theSizeXYZ.y();
0068     SizeZ        = theSizeXYZ.z();
0069     SizeRowBytes = theSizeRowBytes != 0 ? theSizeRowBytes : (SizeX * theSizeBPP);
0070     SizeSliceBytes = SizeRowBytes * SizeY;
0071     mySize       = SizeSliceBytes * SizeZ;
0072     if (myData == NULL)
0073     {
0074       Allocate (mySize);
0075     }
0076     SetTopDown (TopToDown == 1);
0077     return !IsEmpty();
0078   }
0079 
0080   //! Reset all values to zeros.
0081   void ZeroData()
0082   {
0083     if (myData != NULL)
0084     {
0085       memset (myData, 0, mySize);
0086     }
0087   }
0088 
0089   //! Return data pointer to requested row (first column).
0090   const Standard_Byte* Row (const Standard_Size theRow) const
0091   {
0092     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
0093   }
0094 
0095   //! Return data pointer to requested row (first column).
0096   Standard_Byte* ChangeRow (const Standard_Size theRow)
0097   {
0098     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
0099   }
0100 
0101   //! Return data pointer to requested position.
0102   const Standard_Byte* Value (const Standard_Size theRow,
0103                               const Standard_Size theCol) const
0104   {
0105     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
0106   }
0107 
0108   //! Return data pointer to requested position.
0109   Standard_Byte* ChangeValue (Standard_Size theRow,
0110                               Standard_Size theCol)
0111   {
0112     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
0113   }
0114 
0115   //! Return data pointer to requested position.
0116   const Standard_Byte* ValueXY (Standard_Size theX,
0117                                 Standard_Size theY) const
0118   {
0119     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX;
0120   }
0121 
0122   //! Return data pointer to requested position.
0123   Standard_Byte* ChangeValueXY (Standard_Size theX,
0124                                 Standard_Size theY)
0125   {
0126     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX;
0127   }
0128 
0129 public:
0130 
0131   //! Return data pointer to requested 2D slice.
0132   const Standard_Byte* Slice (Standard_Size theSlice) const
0133   {
0134     return myData + ptrdiff_t(SizeSliceBytes * theSlice);
0135   }
0136 
0137   //! Return data pointer to requested 2D slice.
0138   Standard_Byte* ChangeSlice (Standard_Size theSlice)
0139   {
0140     return myData + ptrdiff_t(SizeSliceBytes * theSlice);
0141   }
0142 
0143   //! Return data pointer to requested row (first column).
0144   const Standard_Byte* SliceRow (Standard_Size theSlice,
0145                                  Standard_Size theRow) const
0146   {
0147     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + ptrdiff_t(SizeSliceBytes * theSlice);
0148   }
0149 
0150   //! Return data pointer to requested row (first column).
0151   Standard_Byte* ChangeSliceRow (Standard_Size theSlice,
0152                                  Standard_Size theRow)
0153   {
0154     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + ptrdiff_t(SizeSliceBytes * theSlice);
0155   }
0156 
0157   //! Return data pointer to requested position.
0158   const Standard_Byte* ValueXYZ (Standard_Size theX,
0159                                  Standard_Size theY,
0160                                  Standard_Size theZ) const
0161   {
0162     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX + ptrdiff_t(SizeSliceBytes * theZ);
0163   }
0164 
0165   //! Return data pointer to requested position.
0166   Standard_Byte* ChangeValueXYZ (Standard_Size theX,
0167                                  Standard_Size theY,
0168                                  Standard_Size theZ)
0169   {
0170     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX + ptrdiff_t(SizeSliceBytes * theZ);
0171   }
0172 
0173   //! Compute the maximal row alignment for current row size.
0174   //! @return maximal row alignment in bytes (up to 16 bytes).
0175   Standard_Size MaxRowAligmentBytes() const
0176   {
0177     Standard_Size anAlignment = 2;
0178     for (; anAlignment <= 16; anAlignment <<= 1)
0179     {
0180       if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0)
0181       {
0182         return (anAlignment >> 1);
0183       }
0184     }
0185     return anAlignment;
0186   }
0187 
0188   //! Setup scanlines order in memory - top-down or bottom-up.
0189   //! Drawers should explicitly specify this value if current state IsTopDown() was ignored!
0190   //! @param theIsTopDown top-down flag
0191   void SetTopDown (const bool theIsTopDown)
0192   {
0193     TopToDown   = (theIsTopDown ? 1 : Standard_Size(-1));
0194     myTopRowPtr = ((TopToDown == 1 || myData == NULL)
0195                 ? myData : (myData + SizeRowBytes * (SizeY - 1)));
0196   }
0197 
0198 protected:
0199 
0200   Standard_Byte* myTopRowPtr;  //!< pointer to the topmost row (depending on scanlines order in memory)
0201 
0202 public:
0203 
0204   Standard_Size  SizeBPP;        //!< bytes per pixel
0205   Standard_Size  SizeX;          //!< width  in pixels
0206   Standard_Size  SizeY;          //!< height in pixels
0207   Standard_Size  SizeZ;          //!< depth  in pixels
0208   Standard_Size  SizeRowBytes;   //!< number of bytes per line (in most cases equal to 3 * sizeX)
0209   Standard_Size  SizeSliceBytes; //!< number of bytes per 2D slice
0210   Standard_Size  TopToDown;      //!< image scanlines direction in memory from Top to the Down
0211 
0212 public:
0213 
0214   DEFINE_STANDARD_RTTIEXT(Image_PixMapData, NCollection_Buffer)
0215 
0216 };
0217 
0218 DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer)
0219 
0220 #endif // _Image_PixMapData_H__