Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-15 09:16:08

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   //! Empty constructor.
0028   Image_PixMapData()
0029       : NCollection_Buffer(occ::handle<NCollection_BaseAllocator>()),
0030         myTopRowPtr(nullptr),
0031         SizeBPP(0),
0032         SizeX(0),
0033         SizeY(0),
0034         SizeZ(0),
0035         SizeRowBytes(0),
0036         SizeSliceBytes(0),
0037         TopToDown(size_t(-1))
0038   {
0039     //
0040   }
0041 
0042   //! Initializer.
0043   bool Init(const occ::handle<NCollection_BaseAllocator>& theAlloc,
0044             const size_t                                  theSizeBPP,
0045             const size_t                                  theSizeX,
0046             const size_t                                  theSizeY,
0047             const size_t                                  theSizeRowBytes,
0048             uint8_t*                                      theDataPtr)
0049   {
0050     return Init(theAlloc,
0051                 theSizeBPP,
0052                 NCollection_Vec3<size_t>(theSizeX, theSizeY, 1),
0053                 theSizeRowBytes,
0054                 theDataPtr);
0055   }
0056 
0057   //! Initializer.
0058   bool Init(const occ::handle<NCollection_BaseAllocator>& theAlloc,
0059             const size_t                                  theSizeBPP,
0060             const NCollection_Vec3<size_t>&               theSizeXYZ,
0061             const size_t                                  theSizeRowBytes,
0062             uint8_t*                                      theDataPtr)
0063   {
0064     SetAllocator(theAlloc); // will free old data as well
0065 
0066     myData         = theDataPtr;
0067     myTopRowPtr    = nullptr;
0068     SizeBPP        = theSizeBPP;
0069     SizeX          = theSizeXYZ.x();
0070     SizeY          = theSizeXYZ.y();
0071     SizeZ          = theSizeXYZ.z();
0072     SizeRowBytes   = theSizeRowBytes != 0 ? theSizeRowBytes : (SizeX * theSizeBPP);
0073     SizeSliceBytes = SizeRowBytes * SizeY;
0074     mySize         = SizeSliceBytes * SizeZ;
0075     if (myData == nullptr)
0076     {
0077       Allocate(mySize);
0078     }
0079     SetTopDown(TopToDown == 1);
0080     return !IsEmpty();
0081   }
0082 
0083   //! Reset all values to zeros.
0084   void ZeroData()
0085   {
0086     if (myData != nullptr)
0087     {
0088       memset(myData, 0, mySize);
0089     }
0090   }
0091 
0092   //! Return data pointer to requested row (first column).
0093   const uint8_t* Row(const size_t theRow) const
0094   {
0095     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
0096   }
0097 
0098   //! Return data pointer to requested row (first column).
0099   uint8_t* ChangeRow(const size_t theRow)
0100   {
0101     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
0102   }
0103 
0104   //! Return data pointer to requested position.
0105   const uint8_t* Value(const size_t theRow, const size_t theCol) const
0106   {
0107     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
0108   }
0109 
0110   //! Return data pointer to requested position.
0111   uint8_t* ChangeValue(size_t theRow, size_t theCol)
0112   {
0113     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
0114   }
0115 
0116   //! Return data pointer to requested position.
0117   const uint8_t* ValueXY(size_t theX, size_t theY) const
0118   {
0119     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX;
0120   }
0121 
0122   //! Return data pointer to requested position.
0123   uint8_t* ChangeValueXY(size_t theX, size_t theY)
0124   {
0125     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX;
0126   }
0127 
0128 public:
0129   //! Return data pointer to requested 2D slice.
0130   const uint8_t* Slice(size_t theSlice) const
0131   {
0132     return myData + ptrdiff_t(SizeSliceBytes * theSlice);
0133   }
0134 
0135   //! Return data pointer to requested 2D slice.
0136   uint8_t* ChangeSlice(size_t theSlice) { return myData + ptrdiff_t(SizeSliceBytes * theSlice); }
0137 
0138   //! Return data pointer to requested row (first column).
0139   const uint8_t* SliceRow(size_t theSlice, size_t theRow) const
0140   {
0141     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown)
0142            + ptrdiff_t(SizeSliceBytes * theSlice);
0143   }
0144 
0145   //! Return data pointer to requested row (first column).
0146   uint8_t* ChangeSliceRow(size_t theSlice, size_t theRow)
0147   {
0148     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown)
0149            + ptrdiff_t(SizeSliceBytes * theSlice);
0150   }
0151 
0152   //! Return data pointer to requested position.
0153   const uint8_t* ValueXYZ(size_t theX, size_t theY, size_t theZ) const
0154   {
0155     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX
0156            + ptrdiff_t(SizeSliceBytes * theZ);
0157   }
0158 
0159   //! Return data pointer to requested position.
0160   uint8_t* ChangeValueXYZ(size_t theX, size_t theY, size_t theZ)
0161   {
0162     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX
0163            + ptrdiff_t(SizeSliceBytes * theZ);
0164   }
0165 
0166   //! Compute the maximal row alignment for current row size.
0167   //! @return maximal row alignment in bytes (up to 16 bytes).
0168   size_t MaxRowAligmentBytes() const
0169   {
0170     size_t anAlignment = 2;
0171     for (; anAlignment <= 16; anAlignment <<= 1)
0172     {
0173       if ((SizeRowBytes % anAlignment) != 0 || (size_t(myData) % anAlignment) != 0)
0174       {
0175         return (anAlignment >> 1);
0176       }
0177     }
0178     return anAlignment;
0179   }
0180 
0181   //! Setup scanlines order in memory - top-down or bottom-up.
0182   //! Drawers should explicitly specify this value if current state IsTopDown() was ignored!
0183   //! @param theIsTopDown top-down flag
0184   void SetTopDown(const bool theIsTopDown)
0185   {
0186     TopToDown = (theIsTopDown ? 1 : size_t(-1));
0187     myTopRowPtr =
0188       ((TopToDown == 1 || myData == nullptr) ? myData : (myData + SizeRowBytes * (SizeY - 1)));
0189   }
0190 
0191 protected:
0192   // clang-format off
0193   uint8_t* myTopRowPtr;  //!< pointer to the topmost row (depending on scanlines order in memory)
0194   // clang-format on
0195 
0196 public:
0197   size_t SizeBPP;        //!< bytes per pixel
0198   size_t SizeX;          //!< width  in pixels
0199   size_t SizeY;          //!< height in pixels
0200   size_t SizeZ;          //!< depth  in pixels
0201   size_t SizeRowBytes;   //!< number of bytes per line (in most cases equal to 3 * sizeX)
0202   size_t SizeSliceBytes; //!< number of bytes per 2D slice
0203   size_t TopToDown;      //!< image scanlines direction in memory from Top to the Down
0204 
0205 public:
0206   DEFINE_STANDARD_RTTIEXT(Image_PixMapData, NCollection_Buffer)
0207 };
0208 
0209 #endif // _Image_PixMapData_H__