1 // Created on: 2012-07-18
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef Image_PixMapData_HeaderFile
17 #define Image_PixMapData_HeaderFile
19 #include <Image_Color.hxx>
20 #include <NCollection_Buffer.hxx>
21 #include <NCollection_Vec3.hxx>
23 //! Structure to manage image buffer.
24 class Image_PixMapData : public NCollection_Buffer
28 //! Empty constructor.
30 : NCollection_Buffer (Handle(NCollection_BaseAllocator)()),
38 TopToDown (Standard_Size(-1))
44 bool Init (const Handle(NCollection_BaseAllocator)& theAlloc,
45 const Standard_Size theSizeBPP,
46 const Standard_Size theSizeX,
47 const Standard_Size theSizeY,
48 const Standard_Size theSizeRowBytes,
49 Standard_Byte* theDataPtr)
51 return Init (theAlloc, theSizeBPP, NCollection_Vec3<Standard_Size> (theSizeX, theSizeY, 1), theSizeRowBytes, theDataPtr);
55 bool Init (const Handle(NCollection_BaseAllocator)& theAlloc,
56 const Standard_Size theSizeBPP,
57 const NCollection_Vec3<Standard_Size>& theSizeXYZ,
58 const Standard_Size theSizeRowBytes,
59 Standard_Byte* theDataPtr)
61 SetAllocator (theAlloc); // will free old data as well
66 SizeX = theSizeXYZ.x();
67 SizeY = theSizeXYZ.y();
68 SizeZ = theSizeXYZ.z();
69 SizeRowBytes = theSizeRowBytes != 0 ? theSizeRowBytes : (SizeX * theSizeBPP);
70 SizeSliceBytes = SizeRowBytes * SizeY;
71 mySize = SizeSliceBytes * SizeZ;
76 SetTopDown (TopToDown == 1);
80 //! Reset all values to zeros.
85 memset (myData, 0, mySize);
89 //! Return data pointer to requested row (first column).
90 const Standard_Byte* Row (const Standard_Size theRow) const
92 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
95 //! Return data pointer to requested row (first column).
96 Standard_Byte* ChangeRow (const Standard_Size theRow)
98 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
101 //! Return data pointer to requested position.
102 const Standard_Byte* Value (const Standard_Size theRow,
103 const Standard_Size theCol) const
105 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
108 //! Return data pointer to requested position.
109 Standard_Byte* ChangeValue (Standard_Size theRow,
110 Standard_Size theCol)
112 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
115 //! Return data pointer to requested position.
116 const Standard_Byte* ValueXY (Standard_Size theX,
117 Standard_Size theY) const
119 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX;
122 //! Return data pointer to requested position.
123 Standard_Byte* ChangeValueXY (Standard_Size theX,
126 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX;
131 //! Return data pointer to requested 2D slice.
132 const Standard_Byte* Slice (Standard_Size theSlice) const
134 return myData + ptrdiff_t(SizeSliceBytes * theSlice);
137 //! Return data pointer to requested 2D slice.
138 Standard_Byte* ChangeSlice (Standard_Size theSlice)
140 return myData + ptrdiff_t(SizeSliceBytes * theSlice);
143 //! Return data pointer to requested row (first column).
144 const Standard_Byte* SliceRow (Standard_Size theSlice,
145 Standard_Size theRow) const
147 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + ptrdiff_t(SizeSliceBytes * theSlice);
150 //! Return data pointer to requested row (first column).
151 Standard_Byte* ChangeSliceRow (Standard_Size theSlice,
152 Standard_Size theRow)
154 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + ptrdiff_t(SizeSliceBytes * theSlice);
157 //! Return data pointer to requested position.
158 const Standard_Byte* ValueXYZ (Standard_Size theX,
160 Standard_Size theZ) const
162 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX + ptrdiff_t(SizeSliceBytes * theZ);
165 //! Return data pointer to requested position.
166 Standard_Byte* ChangeValueXYZ (Standard_Size theX,
170 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX + ptrdiff_t(SizeSliceBytes * theZ);
173 //! Compute the maximal row alignment for current row size.
174 //! @return maximal row alignment in bytes (up to 16 bytes).
175 Standard_Size MaxRowAligmentBytes() const
177 Standard_Size anAlignment = 2;
178 for (; anAlignment <= 16; anAlignment <<= 1)
180 if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0)
182 return (anAlignment >> 1);
188 //! Setup scanlines order in memory - top-down or bottom-up.
189 //! Drawers should explicitly specify this value if current state IsTopDown() was ignored!
190 //! @param theIsTopDown top-down flag
191 void SetTopDown (const bool theIsTopDown)
193 TopToDown = (theIsTopDown ? 1 : Standard_Size(-1));
194 myTopRowPtr = ((TopToDown == 1 || myData == NULL)
195 ? myData : (myData + SizeRowBytes * (SizeY - 1)));
200 Standard_Byte* myTopRowPtr; //!< pointer to the topmost row (depending on scanlines order in memory)
204 Standard_Size SizeBPP; //!< bytes per pixel
205 Standard_Size SizeX; //!< width in pixels
206 Standard_Size SizeY; //!< height in pixels
207 Standard_Size SizeZ; //!< depth in pixels
208 Standard_Size SizeRowBytes; //!< number of bytes per line (in most cases equal to 3 * sizeX)
209 Standard_Size SizeSliceBytes; //!< number of bytes per 2D slice
210 Standard_Size TopToDown; //!< image scanlines direction in memory from Top to the Down
214 DEFINE_STANDARD_RTTIEXT(Image_PixMapData, NCollection_Buffer)
218 DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer)
220 #endif // _Image_PixMapData_H__