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_PixMap_H__ |
17 | #define _Image_PixMap_H__ |
18 | |
19 | #include <Image_PixMapData.hxx> |
20 | #include <Standard_Transient.hxx> |
21 | #include <Quantity_Color.hxx> |
22 | |
23 | //! Class represents packed image plane. |
24 | class Image_PixMap : public Standard_Transient |
25 | { |
26 | |
27 | public: |
28 | |
29 | //! This enumeration define packed image plane formats |
30 | typedef enum tagFormat { |
31 | ImgUNKNOWN = 0, //!< unsupported or unknown format |
076ca35c |
32 | ImgGray = 1, //!< 1 byte per pixel, intensity of the color |
33 | ImgAlpha, //!< 1 byte per pixel, transparency |
692613e5 |
34 | ImgRGB, //!< 3 bytes packed RGB image plane |
35 | ImgBGR, //!< same as RGB but with different components order |
36 | ImgRGB32, //!< 4 bytes packed RGB image plane (1 extra byte for alignment, may have undefined value) |
37 | ImgBGR32, //!< same as RGB but with different components order |
38 | ImgRGBA, //!< 4 bytes packed RGBA image plane |
39 | ImgBGRA, //!< same as RGBA but with different components order |
076ca35c |
40 | ImgGrayF, //!< 1 float (4-bytes) per pixel (1-component plane), intensity of the color |
41 | ImgAlphaF, //!< 1 float (4-bytes) per pixel (1-component plane), transparency |
692613e5 |
42 | ImgRGBF, //!< 3 floats (12-bytes) RGB image plane |
43 | ImgBGRF, //!< same as RGBF but with different components order |
44 | ImgRGBAF, //!< 4 floats (16-bytes) RGBA image plane |
45 | ImgBGRAF, //!< same as RGBAF but with different components order |
46 | } ImgFormat; |
47 | |
48 | //! Determine Big-Endian at runtime |
49 | static inline bool IsBigEndianHost() |
50 | { |
51 | union { int myInt; char myChar[sizeof(int)]; } aUnion; |
52 | aUnion.myInt = 1; |
53 | return !aUnion.myChar[0]; |
54 | } |
55 | |
56 | public: // high-level API |
57 | |
58 | inline ImgFormat Format() const |
59 | { |
60 | return myImgFormat; |
61 | } |
62 | |
076ca35c |
63 | //! Override pixel format specified by InitXXX() methods. |
64 | //! Will throw exception if pixel size of new format is not equal to currently initialized format. |
65 | //! Intended to switch formats indicating different interpretation of the same data |
66 | //! (e.g. ImgGray and ImgAlpha). |
67 | Standard_EXPORT void SetFormat (const ImgFormat thePixelFormat); |
68 | |
692613e5 |
69 | //! @return image width in pixels |
70 | inline Standard_Size Width() const |
71 | { |
ca0c0b11 |
72 | return myData.SizeX; |
692613e5 |
73 | } |
74 | |
75 | //! @return image height in pixels |
76 | inline Standard_Size Height() const |
77 | { |
ca0c0b11 |
78 | return myData.SizeY; |
692613e5 |
79 | } |
80 | |
81 | //! @return image width in pixels |
82 | inline Standard_Size SizeX() const |
83 | { |
ca0c0b11 |
84 | return myData.SizeX; |
692613e5 |
85 | } |
86 | |
87 | //! @return image height in pixels |
88 | inline Standard_Size SizeY() const |
89 | { |
ca0c0b11 |
90 | return myData.SizeY; |
692613e5 |
91 | } |
92 | |
93 | //! @return width / height. |
94 | inline Standard_Real Ratio() const |
95 | { |
ca0c0b11 |
96 | return (SizeY() > 0) ? (Standard_Real(SizeX()) / Standard_Real(SizeY())) : 1.0; |
692613e5 |
97 | } |
98 | |
99 | //! @return true if data is NULL. |
100 | bool IsEmpty() const |
101 | { |
ca0c0b11 |
102 | return myData.IsEmpty(); |
692613e5 |
103 | } |
104 | |
105 | //! Empty constructor. Initialize the NULL image plane. |
106 | Standard_EXPORT Image_PixMap(); |
107 | |
108 | //! Destructor |
109 | Standard_EXPORT virtual ~Image_PixMap(); |
110 | |
111 | //! Returns the pixel color. This function is relatively slow. |
112 | //! @param theX - column index from left |
113 | //! @param theY - row index from top |
114 | //! @return the pixel color |
115 | inline Quantity_Color PixelColor (const Standard_Integer theX, |
116 | const Standard_Integer theY) const |
117 | { |
118 | Quantity_Parameter aDummy; |
119 | return PixelColor (theX, theY, aDummy); |
120 | } |
121 | |
122 | //! Returns the pixel color. This function is relatively slow. |
123 | //! theAlpha argument is set to color intensity (0 - transparent, 1 - opaque) |
124 | Standard_EXPORT Quantity_Color PixelColor (const Standard_Integer theX, |
125 | const Standard_Integer theY, |
126 | Quantity_Parameter& theAlpha) const; |
127 | |
128 | //! Initialize image plane as wrapper over alien data. |
129 | //! Data will not be copied! Notice that caller should ensure |
130 | //! that data pointer will not be released during this wrapper lifetime. |
131 | //! You may call InitCopy() to perform data copying. |
132 | Standard_EXPORT virtual bool InitWrapper (ImgFormat thePixelFormat, |
133 | Standard_Byte* theDataPtr, |
134 | const Standard_Size theSizeX, |
135 | const Standard_Size theSizeY, |
136 | const Standard_Size theSizeRowBytes = 0); |
137 | |
138 | //! Initialize image plane with required dimensions. |
139 | //! Memory will be left uninitialized (performance trick). |
140 | Standard_EXPORT virtual bool InitTrash (ImgFormat thePixelFormat, |
141 | const Standard_Size theSizeX, |
142 | const Standard_Size theSizeY, |
143 | const Standard_Size theSizeRowBytes = 0); |
144 | |
145 | //! Initialize by copying data. |
146 | //! If you want to copy alien data you should create wrapper using InitWrapper() before. |
147 | Standard_EXPORT virtual bool InitCopy (const Image_PixMap& theCopy); |
148 | |
149 | //! Initialize image plane with required dimensions. |
150 | //! Buffer will be zeroed (black color for most formats). |
151 | Standard_EXPORT bool InitZero (ImgFormat thePixelFormat, |
152 | const Standard_Size theSizeX, |
153 | const Standard_Size theSizeY, |
154 | const Standard_Size theSizeRowBytes = 0, |
155 | const Standard_Byte theValue = 0); |
156 | |
157 | //! Method correctly deallocate internal buffer. |
ca0c0b11 |
158 | Standard_EXPORT virtual void Clear(); |
692613e5 |
159 | |
ca0c0b11 |
160 | public: //! @name low-level API for batch-processing (pixels reading / comparison / modification) |
692613e5 |
161 | |
f823def0 |
162 | //! Returns TRUE if image data is stored from Top to the Down. |
163 | //! By default Bottom Up order is used instead |
692613e5 |
164 | //! (topmost scanlines starts from the bottom in memory). |
f823def0 |
165 | //! which is most image frameworks naturally support. |
166 | //! |
692613e5 |
167 | //! Notice that access methods within this class automatically |
168 | //! convert input row-index to apply this flag! |
169 | //! You should use this flag only if interconnect with alien APIs and buffers. |
f823def0 |
170 | //! @return true if image data is top-down |
692613e5 |
171 | inline bool IsTopDown() const |
172 | { |
ca0c0b11 |
173 | return myData.TopToDown == 1; |
692613e5 |
174 | } |
175 | |
176 | //! Setup scanlines order in memory - top-down or bottom-up. |
177 | //! Drawers should explicitly specify this value if current state IsTopDown() was ignored! |
f823def0 |
178 | //! @param theIsTopDown top-down flag |
179 | inline void SetTopDown (const bool theIsTopDown) |
692613e5 |
180 | { |
ca0c0b11 |
181 | myData.SetTopDown (theIsTopDown); |
692613e5 |
182 | } |
183 | |
184 | //! Returns +1 if scanlines ordered in Top->Down order in memory and -1 otherwise. |
185 | //! @return scanline increment for Top->Down iteration |
186 | inline Standard_Size TopDownInc() const |
187 | { |
ca0c0b11 |
188 | return myData.TopToDown; |
692613e5 |
189 | } |
190 | |
191 | //! @return data pointer for low-level operations (copying entire buffer, parsing with extra tools etc.). |
192 | inline const Standard_Byte* Data() const |
193 | { |
ca0c0b11 |
194 | return myData.Data(); |
692613e5 |
195 | } |
196 | |
197 | //! @return data pointer for low-level operations (copying entire buffer, parsing with extra tools etc.). |
198 | inline Standard_Byte* ChangeData() |
199 | { |
ca0c0b11 |
200 | return myData.ChangeData(); |
692613e5 |
201 | } |
202 | |
203 | //! @return data pointer to requested row (first column). |
204 | inline const Standard_Byte* Row (const Standard_Size theRow) const |
205 | { |
206 | return myData.Row (theRow); |
207 | } |
208 | |
209 | //! @return data pointer to requested row (first column). |
210 | inline Standard_Byte* ChangeRow (const Standard_Size theRow) |
211 | { |
212 | return myData.ChangeRow (theRow); |
213 | } |
214 | |
215 | //! @return bytes reserved for one pixel (may include extra bytes for alignment). |
216 | inline Standard_Size SizePixelBytes() const |
217 | { |
ca0c0b11 |
218 | return myData.SizeBPP; |
692613e5 |
219 | } |
220 | |
221 | //! @return bytes reserved for one pixel (may include extra bytes for alignment). |
a940de41 |
222 | Standard_EXPORT static Standard_Size SizePixelBytes (const Image_PixMap::ImgFormat thePixelFormat); |
692613e5 |
223 | |
224 | //! @return bytes reserved per row. |
225 | //! Could be larger than needed to store packed row (extra bytes for alignment etc.). |
226 | inline Standard_Size SizeRowBytes() const |
227 | { |
ca0c0b11 |
228 | return myData.SizeRowBytes; |
692613e5 |
229 | } |
230 | |
231 | //! @return the extra bytes in the row. |
232 | inline Standard_Size RowExtraBytes() const |
233 | { |
ca0c0b11 |
234 | return SizeRowBytes() - SizeX() * SizePixelBytes(); |
692613e5 |
235 | } |
236 | |
237 | //! Compute the maximal row alignment for current row size. |
238 | //! @return maximal row alignment in bytes (up to 16 bytes). |
239 | inline Standard_Size MaxRowAligmentBytes() const |
240 | { |
241 | return myData.MaxRowAligmentBytes(); |
242 | } |
243 | |
ca0c0b11 |
244 | //! @return buffer size |
692613e5 |
245 | inline Standard_Size SizeBytes() const |
246 | { |
ca0c0b11 |
247 | return myData.Size(); |
692613e5 |
248 | } |
249 | |
ca0c0b11 |
250 | //! Access image pixel with specified color type. |
251 | //! This method does not perform any type checks - use on own risk (check Format() before)! |
692613e5 |
252 | template <typename ColorType_t> |
ca0c0b11 |
253 | inline const ColorType_t& Value (const Standard_Size theRow, |
254 | const Standard_Size theCol) const |
692613e5 |
255 | { |
ca0c0b11 |
256 | return *reinterpret_cast<const ColorType_t*>(myData.Value (theRow, theCol)); |
692613e5 |
257 | } |
258 | |
259 | //! Access image pixel with specified color type. |
ca0c0b11 |
260 | //! This method does not perform any type checks - use on own risk (check Format() before)! |
692613e5 |
261 | template <typename ColorType_t> |
ca0c0b11 |
262 | inline ColorType_t& ChangeValue (const Standard_Size theRow, |
263 | const Standard_Size theCol) |
692613e5 |
264 | { |
ca0c0b11 |
265 | return *reinterpret_cast<ColorType_t* >(myData.ChangeValue (theRow, theCol)); |
692613e5 |
266 | } |
267 | |
692613e5 |
268 | protected: |
269 | |
ca0c0b11 |
270 | Image_PixMapData myData; //!< data buffer |
271 | ImgFormat myImgFormat; //!< pixel format |
692613e5 |
272 | |
273 | private: |
274 | |
275 | //! Copying allowed only within Handles |
276 | Image_PixMap (const Image_PixMap& ); |
277 | Image_PixMap& operator= (const Image_PixMap& ); |
278 | |
279 | public: |
280 | |
92efcf78 |
281 | DEFINE_STANDARD_RTTIEXT(Image_PixMap,Standard_Transient) // Type definition |
692613e5 |
282 | |
283 | }; |
284 | |
a13f2dc4 |
285 | DEFINE_STANDARD_HANDLE(Image_PixMap, Standard_Transient) |
286 | |
692613e5 |
287 | #endif // _Image_PixMap_H__ |