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 | |
5bd54bef |
16 | #ifndef Image_PixMapData_HeaderFile |
17 | #define Image_PixMapData_HeaderFile |
692613e5 |
18 | |
19 | #include <Image_Color.hxx> |
ca0c0b11 |
20 | #include <NCollection_Buffer.hxx> |
633084b8 |
21 | #include <NCollection_Vec3.hxx> |
692613e5 |
22 | |
ca0c0b11 |
23 | //! Structure to manage image buffer. |
24 | class Image_PixMapData : public NCollection_Buffer |
692613e5 |
25 | { |
ca0c0b11 |
26 | public: |
692613e5 |
27 | |
ca0c0b11 |
28 | //! Empty constructor. |
29 | Image_PixMapData() |
30 | : NCollection_Buffer (Handle(NCollection_BaseAllocator)()), |
31 | myTopRowPtr (NULL), |
32 | SizeBPP (0), |
33 | SizeX (0), |
34 | SizeY (0), |
633084b8 |
35 | SizeZ (0), |
ca0c0b11 |
36 | SizeRowBytes (0), |
633084b8 |
37 | SizeSliceBytes (0), |
ca0c0b11 |
38 | TopToDown (Standard_Size(-1)) |
692613e5 |
39 | { |
ca0c0b11 |
40 | // |
692613e5 |
41 | } |
42 | |
ca0c0b11 |
43 | //! Initializer. |
66d1cdc6 |
44 | bool Init (const Handle(NCollection_BaseAllocator)& theAlloc, |
ca0c0b11 |
45 | const Standard_Size theSizeBPP, |
46 | const Standard_Size theSizeX, |
47 | const Standard_Size theSizeY, |
48 | const Standard_Size theSizeRowBytes, |
49 | Standard_Byte* theDataPtr) |
633084b8 |
50 | { |
51 | return Init (theAlloc, theSizeBPP, NCollection_Vec3<Standard_Size> (theSizeX, theSizeY, 1), theSizeRowBytes, theDataPtr); |
52 | } |
53 | |
54 | //! Initializer. |
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) |
692613e5 |
60 | { |
ca0c0b11 |
61 | SetAllocator (theAlloc); // will free old data as well |
62 | |
63 | myData = theDataPtr; |
64 | myTopRowPtr = NULL; |
65 | SizeBPP = theSizeBPP; |
633084b8 |
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; |
ca0c0b11 |
72 | if (myData == NULL) |
73 | { |
74 | Allocate (mySize); |
75 | } |
76 | SetTopDown (TopToDown == 1); |
66d1cdc6 |
77 | return !IsEmpty(); |
78 | } |
79 | |
80 | //! Reset all values to zeros. |
81 | void ZeroData() |
82 | { |
83 | if (myData != NULL) |
84 | { |
85 | memset (myData, 0, mySize); |
86 | } |
692613e5 |
87 | } |
88 | |
633084b8 |
89 | //! Return data pointer to requested row (first column). |
90 | const Standard_Byte* Row (const Standard_Size theRow) const |
692613e5 |
91 | { |
8ba3d978 |
92 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown); |
692613e5 |
93 | } |
94 | |
633084b8 |
95 | //! Return data pointer to requested row (first column). |
96 | Standard_Byte* ChangeRow (const Standard_Size theRow) |
692613e5 |
97 | { |
8ba3d978 |
98 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown); |
692613e5 |
99 | } |
100 | |
633084b8 |
101 | //! Return data pointer to requested position. |
102 | const Standard_Byte* Value (const Standard_Size theRow, |
103 | const Standard_Size theCol) const |
692613e5 |
104 | { |
8ba3d978 |
105 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol; |
692613e5 |
106 | } |
107 | |
633084b8 |
108 | //! Return data pointer to requested position. |
109 | Standard_Byte* ChangeValue (Standard_Size theRow, |
110 | Standard_Size theCol) |
692613e5 |
111 | { |
8ba3d978 |
112 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol; |
692613e5 |
113 | } |
114 | |
633084b8 |
115 | //! Return data pointer to requested position. |
116 | const Standard_Byte* ValueXY (Standard_Size theX, |
117 | Standard_Size theY) const |
118 | { |
119 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX; |
120 | } |
121 | |
122 | //! Return data pointer to requested position. |
123 | Standard_Byte* ChangeValueXY (Standard_Size theX, |
124 | Standard_Size theY) |
125 | { |
126 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX; |
127 | } |
128 | |
129 | public: |
130 | |
131 | //! Return data pointer to requested 2D slice. |
132 | const Standard_Byte* Slice (Standard_Size theSlice) const |
133 | { |
134 | return myData + ptrdiff_t(SizeSliceBytes * theSlice); |
135 | } |
136 | |
137 | //! Return data pointer to requested 2D slice. |
138 | Standard_Byte* ChangeSlice (Standard_Size theSlice) |
139 | { |
140 | return myData + ptrdiff_t(SizeSliceBytes * theSlice); |
141 | } |
142 | |
143 | //! Return data pointer to requested row (first column). |
144 | const Standard_Byte* SliceRow (Standard_Size theSlice, |
145 | Standard_Size theRow) const |
146 | { |
147 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + ptrdiff_t(SizeSliceBytes * theSlice); |
148 | } |
149 | |
150 | //! Return data pointer to requested row (first column). |
151 | Standard_Byte* ChangeSliceRow (Standard_Size theSlice, |
152 | Standard_Size theRow) |
153 | { |
154 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + ptrdiff_t(SizeSliceBytes * theSlice); |
155 | } |
156 | |
157 | //! Return data pointer to requested position. |
158 | const Standard_Byte* ValueXYZ (Standard_Size theX, |
159 | Standard_Size theY, |
160 | Standard_Size theZ) const |
161 | { |
162 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX + ptrdiff_t(SizeSliceBytes * theZ); |
163 | } |
164 | |
165 | //! Return data pointer to requested position. |
166 | Standard_Byte* ChangeValueXYZ (Standard_Size theX, |
167 | Standard_Size theY, |
168 | Standard_Size theZ) |
169 | { |
170 | return myTopRowPtr + ptrdiff_t(SizeRowBytes * theY * TopToDown) + SizeBPP * theX + ptrdiff_t(SizeSliceBytes * theZ); |
171 | } |
172 | |
692613e5 |
173 | //! Compute the maximal row alignment for current row size. |
174 | //! @return maximal row alignment in bytes (up to 16 bytes). |
633084b8 |
175 | Standard_Size MaxRowAligmentBytes() const |
692613e5 |
176 | { |
177 | Standard_Size anAlignment = 2; |
178 | for (; anAlignment <= 16; anAlignment <<= 1) |
179 | { |
ca0c0b11 |
180 | if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0) |
692613e5 |
181 | { |
182 | return (anAlignment >> 1); |
183 | } |
184 | } |
185 | return anAlignment; |
186 | } |
187 | |
ca0c0b11 |
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 |
633084b8 |
191 | void SetTopDown (const bool theIsTopDown) |
692613e5 |
192 | { |
ca0c0b11 |
193 | TopToDown = (theIsTopDown ? 1 : Standard_Size(-1)); |
194 | myTopRowPtr = ((TopToDown == 1 || myData == NULL) |
195 | ? myData : (myData + SizeRowBytes * (SizeY - 1))); |
692613e5 |
196 | } |
197 | |
ca0c0b11 |
198 | protected: |
692613e5 |
199 | |
ca0c0b11 |
200 | Standard_Byte* myTopRowPtr; //!< pointer to the topmost row (depending on scanlines order in memory) |
692613e5 |
201 | |
202 | public: |
203 | |
633084b8 |
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 |
7d3e64ef |
211 | |
212 | public: |
213 | |
633084b8 |
214 | DEFINE_STANDARD_RTTIEXT(Image_PixMapData, NCollection_Buffer) |
7d3e64ef |
215 | |
692613e5 |
216 | }; |
217 | |
7d3e64ef |
218 | DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer) |
ca0c0b11 |
219 | |
692613e5 |
220 | #endif // _Image_PixMapData_H__ |