0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Image / Image_PixMapData.hxx
1 // Created on: 2012-07-18
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef Image_PixMapData_HeaderFile
17 #define Image_PixMapData_HeaderFile
18
19 #include <Image_Color.hxx>
20 #include <NCollection_Buffer.hxx>
21 #include <NCollection_Vec3.hxx>
22
23 //! Structure to manage image buffer.
24 class Image_PixMapData : public NCollection_Buffer
25 {
26 public:
27
28   //! Empty constructor.
29   Image_PixMapData()
30   : NCollection_Buffer (Handle(NCollection_BaseAllocator)()),
31     myTopRowPtr  (NULL),
32     SizeBPP      (0),
33     SizeX        (0),
34     SizeY        (0),
35     SizeZ        (0),
36     SizeRowBytes (0),
37     SizeSliceBytes (0),
38     TopToDown    (Standard_Size(-1))
39   {
40     //
41   }
42
43   //! Initializer.
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)
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)
60   {
61     SetAllocator (theAlloc); // will free old data as well
62
63     myData       = theDataPtr;
64     myTopRowPtr  = NULL;
65     SizeBPP      = theSizeBPP;
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;
72     if (myData == NULL)
73     {
74       Allocate (mySize);
75     }
76     SetTopDown (TopToDown == 1);
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     }
87   }
88
89   //! Return data pointer to requested row (first column).
90   const Standard_Byte* Row (const Standard_Size theRow) const
91   {
92     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
93   }
94
95   //! Return data pointer to requested row (first column).
96   Standard_Byte* ChangeRow (const Standard_Size theRow)
97   {
98     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown);
99   }
100
101   //! Return data pointer to requested position.
102   const Standard_Byte* Value (const Standard_Size theRow,
103                               const Standard_Size theCol) const
104   {
105     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
106   }
107
108   //! Return data pointer to requested position.
109   Standard_Byte* ChangeValue (Standard_Size theRow,
110                               Standard_Size theCol)
111   {
112     return myTopRowPtr + ptrdiff_t(SizeRowBytes * theRow * TopToDown) + SizeBPP * theCol;
113   }
114
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
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
176   {
177     Standard_Size anAlignment = 2;
178     for (; anAlignment <= 16; anAlignment <<= 1)
179     {
180       if ((SizeRowBytes % anAlignment) != 0 || (Standard_Size(myData) % anAlignment) != 0)
181       {
182         return (anAlignment >> 1);
183       }
184     }
185     return anAlignment;
186   }
187
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)
192   {
193     TopToDown   = (theIsTopDown ? 1 : Standard_Size(-1));
194     myTopRowPtr = ((TopToDown == 1 || myData == NULL)
195                 ? myData : (myData + SizeRowBytes * (SizeY - 1)));
196   }
197
198 protected:
199
200   Standard_Byte* myTopRowPtr;  //!< pointer to the topmost row (depending on scanlines order in memory)
201
202 public:
203
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
211
212 public:
213
214   DEFINE_STANDARD_RTTIEXT(Image_PixMapData, NCollection_Buffer)
215
216 };
217
218 DEFINE_STANDARD_HANDLE(Image_PixMapData, NCollection_Buffer)
219
220 #endif // _Image_PixMapData_H__