0031048: Visualization - runtime error reported by Clang undefined behavior sanitizer...
[occt.git] / src / Image / Image_PixMapData.hxx
CommitLineData
6aca4d39 1// Created on: 2012-07-18
692613e5 2// Created by: Kirill GAVRILOV
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
692613e5 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
692613e5 6//
d5f74e42 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
973c2be1 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.
692613e5 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
692613e5 15
16#ifndef _Image_PixMapData_H__
17#define _Image_PixMapData_H__
18
19#include <Image_Color.hxx>
ca0c0b11 20#include <NCollection_Buffer.hxx>
692613e5 21
ca0c0b11 22//! Structure to manage image buffer.
23class Image_PixMapData : public NCollection_Buffer
692613e5 24{
ca0c0b11 25public:
692613e5 26
ca0c0b11 27 //! Empty constructor.
28 Image_PixMapData()
29 : NCollection_Buffer (Handle(NCollection_BaseAllocator)()),
30 myTopRowPtr (NULL),
31 SizeBPP (0),
32 SizeX (0),
33 SizeY (0),
34 SizeRowBytes (0),
35 TopToDown (Standard_Size(-1))
692613e5 36 {
ca0c0b11 37 //
692613e5 38 }
39
ca0c0b11 40 //! Initializer.
66d1cdc6 41 bool Init (const Handle(NCollection_BaseAllocator)& theAlloc,
ca0c0b11 42 const Standard_Size theSizeBPP,
43 const Standard_Size theSizeX,
44 const Standard_Size theSizeY,
45 const Standard_Size theSizeRowBytes,
46 Standard_Byte* theDataPtr)
692613e5 47 {
ca0c0b11 48 SetAllocator (theAlloc); // will free old data as well
49
50 myData = theDataPtr;
51 myTopRowPtr = NULL;
52 SizeBPP = theSizeBPP;
53 SizeX = theSizeX;
54 SizeY = theSizeY;
55 SizeRowBytes = theSizeRowBytes != 0 ? theSizeRowBytes : (theSizeX * theSizeBPP);
56 mySize = SizeRowBytes * SizeY;
57 if (myData == NULL)
58 {
59 Allocate (mySize);
60 }
61 SetTopDown (TopToDown == 1);
66d1cdc6 62 return !IsEmpty();
63 }
64
65 //! Reset all values to zeros.
66 void ZeroData()
67 {
68 if (myData != NULL)
69 {
70 memset (myData, 0, mySize);
71 }
692613e5 72 }
73
74 //! @return data pointer to requested row (first column).
ca0c0b11 75 inline const Standard_Byte* Row (const Standard_Size theRow) const
692613e5 76 {
8ba3d978 77 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
692613e5 78 }
79
80 //! @return data pointer to requested row (first column).
ca0c0b11 81 inline Standard_Byte* ChangeRow (const Standard_Size theRow)
692613e5 82 {
8ba3d978 83 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
692613e5 84 }
85
86 //! @return data pointer to requested position.
ca0c0b11 87 inline const Standard_Byte* Value (const Standard_Size theRow,
88 const Standard_Size theCol) const
692613e5 89 {
8ba3d978 90 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
692613e5 91 }
92
93 //! @return data pointer to requested position.
ca0c0b11 94 inline Standard_Byte* ChangeValue (const Standard_Size theRow,
95 const Standard_Size theCol)
692613e5 96 {
8ba3d978 97 return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
692613e5 98 }
99
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
103 {
104 Standard_Size anAlignment = 2;
105 for (; anAlignment <= 16; anAlignment <<= 1)
106 {
ca0c0b11 107 if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0)
692613e5 108 {
109 return (anAlignment >> 1);
110 }
111 }
112 return anAlignment;
113 }
114
ca0c0b11 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)
692613e5 119 {
ca0c0b11 120 TopToDown = (theIsTopDown ? 1 : Standard_Size(-1));
121 myTopRowPtr = ((TopToDown == 1 || myData == NULL)
122 ? myData : (myData + SizeRowBytes * (SizeY - 1)));
692613e5 123 }
124
ca0c0b11 125protected:
692613e5 126
ca0c0b11 127 Standard_Byte* myTopRowPtr; //!< pointer to the topmost row (depending on scanlines order in memory)
692613e5 128
129public:
130
ca0c0b11 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
692613e5 136
7d3e64ef 137
138public:
139
92efcf78 140 DEFINE_STANDARD_RTTI_INLINE(Image_PixMapData,NCollection_Buffer) // Type definition
7d3e64ef 141
692613e5 142};
143
7d3e64ef 144DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer)
ca0c0b11 145
692613e5 146#endif // _Image_PixMapData_H__