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_H__
17 #define _Image_PixMapData_H__
19 #include <Image_Color.hxx>
20 #include <NCollection_Buffer.hxx>
22 //! Structure to manage image buffer.
23 class Image_PixMapData : public NCollection_Buffer
27 //! Empty constructor.
29 : NCollection_Buffer (Handle(NCollection_BaseAllocator)()),
35 TopToDown (Standard_Size(-1))
41 bool Init (const Handle(NCollection_BaseAllocator)& theAlloc,
42 const Standard_Size theSizeBPP,
43 const Standard_Size theSizeX,
44 const Standard_Size theSizeY,
45 const Standard_Size theSizeRowBytes,
46 Standard_Byte* theDataPtr)
48 SetAllocator (theAlloc); // will free old data as well
55 SizeRowBytes = theSizeRowBytes != 0 ? theSizeRowBytes : (theSizeX * theSizeBPP);
56 mySize = SizeRowBytes * SizeY;
61 SetTopDown (TopToDown == 1);
65 //! Reset all values to zeros.
70 memset (myData, 0, mySize);
74 //! @return data pointer to requested row (first column).
75 inline const Standard_Byte* Row (const Standard_Size theRow) const
77 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
80 //! @return data pointer to requested row (first column).
81 inline Standard_Byte* ChangeRow (const Standard_Size theRow)
83 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
86 //! @return data pointer to requested position.
87 inline const Standard_Byte* Value (const Standard_Size theRow,
88 const Standard_Size theCol) const
90 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
93 //! @return data pointer to requested position.
94 inline Standard_Byte* ChangeValue (const Standard_Size theRow,
95 const Standard_Size theCol)
97 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
100 //! Compute the maximal row alignment for current row size.
101 //! @return maximal row alignment in bytes (up to 16 bytes).
102 inline Standard_Size MaxRowAligmentBytes() const
104 Standard_Size anAlignment = 2;
105 for (; anAlignment <= 16; anAlignment <<= 1)
107 if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0)
109 return (anAlignment >> 1);
115 //! Setup scanlines order in memory - top-down or bottom-up.
116 //! Drawers should explicitly specify this value if current state IsTopDown() was ignored!
117 //! @param theIsTopDown top-down flag
118 inline void SetTopDown (const bool theIsTopDown)
120 TopToDown = (theIsTopDown ? 1 : Standard_Size(-1));
121 myTopRowPtr = ((TopToDown == 1 || myData == NULL)
122 ? myData : (myData + SizeRowBytes * (SizeY - 1)));
127 Standard_Byte* myTopRowPtr; //!< pointer to the topmost row (depending on scanlines order in memory)
131 Standard_Size SizeBPP; //!< bytes per pixel
132 Standard_Size SizeX; //!< width in pixels
133 Standard_Size SizeY; //!< height in pixels
134 Standard_Size SizeRowBytes; //!< number of bytes per line (in most cases equal to 3 * sizeX)
135 Standard_Size TopToDown; //!< image scanlines direction in memory from Top to the Down
140 DEFINE_STANDARD_RTTI_INLINE(Image_PixMapData,NCollection_Buffer) // Type definition
144 DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer)
146 #endif // _Image_PixMapData_H__