0023544: Texture management in TKOpenGl should be redesigned
[occt.git] / src / Image / Image_PixMap.hxx
CommitLineData
692613e5 1// Created on: 2012-07-18
2// Created by: Kirill GAVRILOV
3// Copyright (c) 2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
20#ifndef _Image_PixMap_H__
21#define _Image_PixMap_H__
22
23#include <Image_PixMapData.hxx>
bf75be98 24#include <Image_PixMap_Handle.hxx>
692613e5 25#include <Standard_Transient.hxx>
26#include <Quantity_Color.hxx>
27
28//! Class represents packed image plane.
29class Image_PixMap : public Standard_Transient
30{
31
32public:
33
34 //! This enumeration define packed image plane formats
35 typedef enum tagFormat {
36 ImgUNKNOWN = 0, //!< unsupported or unknown format
37 ImgGray = 1, //!< 1 byte per pixel
38 ImgRGB, //!< 3 bytes packed RGB image plane
39 ImgBGR, //!< same as RGB but with different components order
40 ImgRGB32, //!< 4 bytes packed RGB image plane (1 extra byte for alignment, may have undefined value)
41 ImgBGR32, //!< same as RGB but with different components order
42 ImgRGBA, //!< 4 bytes packed RGBA image plane
43 ImgBGRA, //!< same as RGBA but with different components order
44 ImgGrayF, //!< 1 float (4-bytes) per pixel (1-component plane)
45 ImgRGBF, //!< 3 floats (12-bytes) RGB image plane
46 ImgBGRF, //!< same as RGBF but with different components order
47 ImgRGBAF, //!< 4 floats (16-bytes) RGBA image plane
48 ImgBGRAF, //!< same as RGBAF but with different components order
49 } ImgFormat;
50
51 //! Determine Big-Endian at runtime
52 static inline bool IsBigEndianHost()
53 {
54 union { int myInt; char myChar[sizeof(int)]; } aUnion;
55 aUnion.myInt = 1;
56 return !aUnion.myChar[0];
57 }
58
59public: // high-level API
60
61 inline ImgFormat Format() const
62 {
63 return myImgFormat;
64 }
65
66 //! @return image width in pixels
67 inline Standard_Size Width() const
68 {
69 return myData.mySizeX;
70 }
71
72 //! @return image height in pixels
73 inline Standard_Size Height() const
74 {
75 return myData.mySizeY;
76 }
77
78 //! @return image width in pixels
79 inline Standard_Size SizeX() const
80 {
81 return myData.mySizeX;
82 }
83
84 //! @return image height in pixels
85 inline Standard_Size SizeY() const
86 {
87 return myData.mySizeY;
88 }
89
90 //! @return width / height.
91 inline Standard_Real Ratio() const
92 {
93 return (myData.mySizeY > 0) ? (Standard_Real(myData.mySizeX) / Standard_Real(myData.mySizeY)) : 1.0;
94 }
95
96 //! @return true if data is NULL.
97 bool IsEmpty() const
98 {
99 return myData.myDataPtr == NULL;
100 }
101
102 //! Empty constructor. Initialize the NULL image plane.
103 Standard_EXPORT Image_PixMap();
104
105 //! Destructor
106 Standard_EXPORT virtual ~Image_PixMap();
107
108 //! Returns the pixel color. This function is relatively slow.
109 //! @param theX - column index from left
110 //! @param theY - row index from top
111 //! @return the pixel color
112 inline Quantity_Color PixelColor (const Standard_Integer theX,
113 const Standard_Integer theY) const
114 {
115 Quantity_Parameter aDummy;
116 return PixelColor (theX, theY, aDummy);
117 }
118
119 //! Returns the pixel color. This function is relatively slow.
120 //! theAlpha argument is set to color intensity (0 - transparent, 1 - opaque)
121 Standard_EXPORT Quantity_Color PixelColor (const Standard_Integer theX,
122 const Standard_Integer theY,
123 Quantity_Parameter& theAlpha) const;
124
125 //! Initialize image plane as wrapper over alien data.
126 //! Data will not be copied! Notice that caller should ensure
127 //! that data pointer will not be released during this wrapper lifetime.
128 //! You may call InitCopy() to perform data copying.
129 Standard_EXPORT virtual bool InitWrapper (ImgFormat thePixelFormat,
130 Standard_Byte* theDataPtr,
131 const Standard_Size theSizeX,
132 const Standard_Size theSizeY,
133 const Standard_Size theSizeRowBytes = 0);
134
135 //! Initialize image plane with required dimensions.
136 //! Memory will be left uninitialized (performance trick).
137 Standard_EXPORT virtual bool InitTrash (ImgFormat thePixelFormat,
138 const Standard_Size theSizeX,
139 const Standard_Size theSizeY,
140 const Standard_Size theSizeRowBytes = 0);
141
142 //! Initialize by copying data.
143 //! If you want to copy alien data you should create wrapper using InitWrapper() before.
144 Standard_EXPORT virtual bool InitCopy (const Image_PixMap& theCopy);
145
146 //! Initialize image plane with required dimensions.
147 //! Buffer will be zeroed (black color for most formats).
148 Standard_EXPORT bool InitZero (ImgFormat thePixelFormat,
149 const Standard_Size theSizeX,
150 const Standard_Size theSizeY,
151 const Standard_Size theSizeRowBytes = 0,
152 const Standard_Byte theValue = 0);
153
154 //! Method correctly deallocate internal buffer.
155 Standard_EXPORT virtual void Clear (ImgFormat thePixelFormat = ImgGray);
156
157public: // low-level API for batch-processing (pixels reading / comparison / modification)
158
159 //! Returns true if image data stored from Top to the Down (default).
160 //! Some external APIs can return bottom-up data instead
161 //! (topmost scanlines starts from the bottom in memory).
162 //! Notice that access methods within this class automatically
163 //! convert input row-index to apply this flag!
164 //! You should use this flag only if interconnect with alien APIs and buffers.
165 //! @return true if image data is top-down.
166 inline bool IsTopDown() const
167 {
168 return myData.myTopToDown == 1;
169 }
170
171 //! Setup scanlines order in memory - top-down or bottom-up.
172 //! Drawers should explicitly specify this value if current state IsTopDown() was ignored!
173 //! @param theIsTopDown - top-down flag.
174 inline void SetTopDown (bool theIsTopDown)
175 {
176 myData.myTopToDown = (theIsTopDown ? 1 : Standard_Size(-1));
177 setTopDown();
178 }
179
180 //! Returns +1 if scanlines ordered in Top->Down order in memory and -1 otherwise.
181 //! @return scanline increment for Top->Down iteration
182 inline Standard_Size TopDownInc() const
183 {
184 return myData.myTopToDown;
185 }
186
187 //! @return data pointer for low-level operations (copying entire buffer, parsing with extra tools etc.).
188 inline const Standard_Byte* Data() const
189 {
190 return myData.myDataPtr;
191 }
192
193 //! @return data pointer for low-level operations (copying entire buffer, parsing with extra tools etc.).
194 inline Standard_Byte* ChangeData()
195 {
196 return myData.myDataPtr;
197 }
198
199 //! @return data pointer to requested row (first column).
200 inline const Standard_Byte* Row (const Standard_Size theRow) const
201 {
202 return myData.Row (theRow);
203 }
204
205 //! @return data pointer to requested row (first column).
206 inline Standard_Byte* ChangeRow (const Standard_Size theRow)
207 {
208 return myData.ChangeRow (theRow);
209 }
210
211 //! @return bytes reserved for one pixel (may include extra bytes for alignment).
212 inline Standard_Size SizePixelBytes() const
213 {
214 return myData.mySizeBPP;
215 }
216
217 //! @return bytes reserved for one pixel (may include extra bytes for alignment).
218 static Standard_Size SizePixelBytes (const Image_PixMap::ImgFormat thePixelFormat);
219
220 //! @return bytes reserved per row.
221 //! Could be larger than needed to store packed row (extra bytes for alignment etc.).
222 inline Standard_Size SizeRowBytes() const
223 {
224 return myData.mySizeRowBytes;
225 }
226
227 //! @return the extra bytes in the row.
228 inline Standard_Size RowExtraBytes() const
229 {
230 return myData.mySizeRowBytes - myData.mySizeX * myData.mySizeBPP;
231 }
232
233 //! Compute the maximal row alignment for current row size.
234 //! @return maximal row alignment in bytes (up to 16 bytes).
235 inline Standard_Size MaxRowAligmentBytes() const
236 {
237 return myData.MaxRowAligmentBytes();
238 }
239
240 inline Standard_Size SizeBytes() const
241 {
242 return myData.SizeBytes();
243 }
244
245 //! Access image buffer for write/read operations with specified color type.
246 template <typename ColorType_t>
247 inline Image_PixMapData<ColorType_t>& EditData()
248 {
249 return *(Image_PixMapData<ColorType_t>* )&myData;
250 }
251
252 //! Access image buffer for read operations with specified color type.
253 template <typename ColorType_t>
254 inline const Image_PixMapData<ColorType_t>& ReadData() const
255 {
256 return *(Image_PixMapData<ColorType_t>* )&myData;
257 }
258
259 //! Access image pixel with specified color type.
260 template <typename ColorType_t>
261 inline const ColorType_t& Value (const Standard_Size theRow,
262 const Standard_Size theCol) const
263 {
264 return ((Image_PixMapData<ColorType_t>* )&myData)->Value (theRow, theCol);
265 }
266
267protected:
268
269 //! Setup pixel format
270 Standard_EXPORT void setFormat (ImgFormat thePixelFormat);
271
272 //! Auxiliary method to setup myTopRowPtr
273 Standard_EXPORT void setTopDown();
274
275protected:
276
277 Image_PixMapData<Standard_Byte> myData;
278 ImgFormat myImgFormat; //!< pixel format
279 bool myIsOwnPointer; //!< if data was allocated by this class - flag is true
280
281private:
282
283 //! Copying allowed only within Handles
284 Image_PixMap (const Image_PixMap& );
285 Image_PixMap& operator= (const Image_PixMap& );
286
287public:
288
289 DEFINE_STANDARD_RTTI(Image_PixMap) // Type definition
290
291};
292
692613e5 293#endif // _Image_PixMap_H__