6aca4d39 |
1 | // Created on: 2010-07-18 |
692613e5 |
2 | // Created by: Kirill GAVRILOV |
6aca4d39 |
3 | // Copyright (c) 2010-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
b311480e |
15 | |
692613e5 |
16 | #include <Image_PixMap.hxx> |
acc62560 |
17 | #include <Standard.hxx> |
7fd59977 |
18 | |
692613e5 |
19 | IMPLEMENT_STANDARD_HANDLE (Image_PixMap, Standard_Transient) |
20 | IMPLEMENT_STANDARD_RTTIEXT(Image_PixMap, Standard_Transient) |
7fd59977 |
21 | |
692613e5 |
22 | // ======================================================================= |
23 | // function : Image_PixMap |
24 | // purpose : |
25 | // ======================================================================= |
26 | Image_PixMap::Image_PixMap() |
27 | : myImgFormat (Image_PixMap::ImgGray), |
28 | myIsOwnPointer (true) |
29 | { |
30 | memset (&myData, 0, sizeof(myData)); |
31 | myData.mySizeBPP = 1; |
f823def0 |
32 | myData.myTopToDown = Standard_Size(-1); |
692613e5 |
33 | setFormat (Image_PixMap::ImgGray); |
34 | } |
7fd59977 |
35 | |
692613e5 |
36 | // ======================================================================= |
37 | // function : ~Image_PixMap |
38 | // purpose : |
39 | // ======================================================================= |
40 | Image_PixMap::~Image_PixMap() |
41 | { |
42 | Clear(); |
43 | } |
7fd59977 |
44 | |
692613e5 |
45 | Standard_Size Image_PixMap::SizePixelBytes (const Image_PixMap::ImgFormat thePixelFormat) |
7fd59977 |
46 | { |
692613e5 |
47 | switch (thePixelFormat) |
7fd59977 |
48 | { |
692613e5 |
49 | case ImgGrayF: |
50 | return sizeof(float); |
51 | case ImgRGBAF: |
52 | case ImgBGRAF: |
53 | return sizeof(float) * 4; |
54 | case ImgRGBF: |
55 | case ImgBGRF: |
56 | return sizeof(float) * 3; |
57 | case ImgRGBA: |
58 | case ImgBGRA: |
59 | return 4; |
60 | case ImgRGB32: |
61 | case ImgBGR32: |
62 | return 4; |
63 | case ImgRGB: |
64 | case ImgBGR: |
65 | return 3; |
66 | case ImgGray: |
7fd59977 |
67 | default: |
692613e5 |
68 | return 1; |
7fd59977 |
69 | } |
7fd59977 |
70 | } |
71 | |
692613e5 |
72 | // ======================================================================= |
73 | // function : setFormat |
74 | // purpose : |
75 | // ======================================================================= |
76 | void Image_PixMap::setFormat (Image_PixMap::ImgFormat thePixelFormat) |
7fd59977 |
77 | { |
692613e5 |
78 | myImgFormat = thePixelFormat; |
79 | myData.mySizeBPP = SizePixelBytes (myImgFormat); |
7fd59977 |
80 | } |
81 | |
692613e5 |
82 | // ======================================================================= |
83 | // function : setTopDown |
84 | // purpose : |
85 | // ======================================================================= |
86 | void Image_PixMap::setTopDown() |
7fd59977 |
87 | { |
692613e5 |
88 | myData.myTopRowPtr = ((myData.myTopToDown == 1 || myData.myDataPtr == NULL) |
89 | ? myData.myDataPtr : (myData.myDataPtr + myData.mySizeRowBytes * (myData.mySizeY - 1))); |
7fd59977 |
90 | } |
91 | |
692613e5 |
92 | // ======================================================================= |
93 | // function : InitWrapper |
94 | // purpose : |
95 | // ======================================================================= |
96 | bool Image_PixMap::InitWrapper (Image_PixMap::ImgFormat thePixelFormat, |
97 | Standard_Byte* theDataPtr, |
98 | const Standard_Size theSizeX, |
99 | const Standard_Size theSizeY, |
100 | const Standard_Size theSizeRowBytes) |
7fd59977 |
101 | { |
692613e5 |
102 | Clear (thePixelFormat); |
103 | if ((theSizeX == 0) || (theSizeY == 0) || (theDataPtr == NULL)) |
7fd59977 |
104 | { |
692613e5 |
105 | return false; |
7fd59977 |
106 | } |
692613e5 |
107 | myData.mySizeX = theSizeX; |
108 | myData.mySizeY = theSizeY; |
109 | myData.mySizeRowBytes = (theSizeRowBytes != 0) ? theSizeRowBytes : (theSizeX * myData.mySizeBPP); |
110 | myData.myDataPtr = theDataPtr; |
111 | myIsOwnPointer = false; |
112 | setTopDown(); |
113 | return true; |
114 | } |
7fd59977 |
115 | |
692613e5 |
116 | // ======================================================================= |
117 | // function : InitTrash |
118 | // purpose : |
119 | // ======================================================================= |
120 | bool Image_PixMap::InitTrash (Image_PixMap::ImgFormat thePixelFormat, |
121 | const Standard_Size theSizeX, |
122 | const Standard_Size theSizeY, |
123 | const Standard_Size theSizeRowBytes) |
124 | { |
125 | Clear (thePixelFormat); |
126 | if ((theSizeX == 0) || (theSizeY == 0)) |
7fd59977 |
127 | { |
692613e5 |
128 | return false; |
7fd59977 |
129 | } |
692613e5 |
130 | myData.mySizeX = theSizeX; |
131 | myData.mySizeY = theSizeY; |
132 | myData.mySizeRowBytes = myData.mySizeX * myData.mySizeBPP; |
133 | if (theSizeRowBytes > myData.mySizeRowBytes) |
7fd59977 |
134 | { |
692613e5 |
135 | // use argument only if it greater |
136 | myData.mySizeRowBytes = theSizeRowBytes; |
7fd59977 |
137 | } |
acc62560 |
138 | myData.myDataPtr = (Standard_Byte* )Standard::AllocateAligned (SizeBytes(), 16); |
692613e5 |
139 | myIsOwnPointer = true; |
140 | setTopDown(); |
141 | return myData.myDataPtr != NULL; |
7fd59977 |
142 | } |
143 | |
692613e5 |
144 | // ======================================================================= |
145 | // function : InitZero |
146 | // purpose : |
147 | // ======================================================================= |
148 | bool Image_PixMap::InitZero (Image_PixMap::ImgFormat thePixelFormat, |
149 | const Standard_Size theSizeX, |
150 | const Standard_Size theSizeY, |
151 | const Standard_Size theSizeRowBytes, |
152 | const Standard_Byte theValue) |
7fd59977 |
153 | { |
692613e5 |
154 | if (!InitTrash (thePixelFormat, theSizeX, theSizeY, theSizeRowBytes)) |
155 | { |
156 | return false; |
157 | } |
158 | memset (myData.myDataPtr, (int )theValue, SizeBytes()); |
159 | return true; |
7fd59977 |
160 | } |
161 | |
692613e5 |
162 | // ======================================================================= |
163 | // function : InitCopy |
164 | // purpose : |
165 | // ======================================================================= |
166 | bool Image_PixMap::InitCopy (const Image_PixMap& theCopy) |
7fd59977 |
167 | { |
692613e5 |
168 | if (&theCopy == this) |
7fd59977 |
169 | { |
692613e5 |
170 | // self-copying disallowed |
171 | return false; |
7fd59977 |
172 | } |
692613e5 |
173 | if (InitTrash (theCopy.myImgFormat, theCopy.myData.mySizeX, theCopy.myData.mySizeY, theCopy.myData.mySizeRowBytes)) |
174 | { |
175 | memcpy (myData.myDataPtr, theCopy.myData.myDataPtr, theCopy.SizeBytes()); |
176 | return true; |
177 | } |
178 | return false; |
7fd59977 |
179 | } |
180 | |
85e096c3 |
181 | // ======================================================================= |
692613e5 |
182 | // function : Clear |
85e096c3 |
183 | // purpose : |
184 | // ======================================================================= |
692613e5 |
185 | void Image_PixMap::Clear (Image_PixMap::ImgFormat thePixelFormat) |
85e096c3 |
186 | { |
692613e5 |
187 | if (myIsOwnPointer && (myData.myDataPtr != NULL)) |
188 | { |
acc62560 |
189 | Standard::FreeAligned (myData.myDataPtr); |
692613e5 |
190 | } |
191 | myData.myDataPtr = myData.myTopRowPtr = NULL; |
192 | myIsOwnPointer = true; |
193 | myData.mySizeX = myData.mySizeY = myData.mySizeRowBytes = 0; |
194 | setFormat (thePixelFormat); |
85e096c3 |
195 | } |
196 | |
197 | // ======================================================================= |
198 | // function : PixelColor |
199 | // purpose : |
200 | // ======================================================================= |
201 | Quantity_Color Image_PixMap::PixelColor (const Standard_Integer theX, |
202 | const Standard_Integer theY, |
203 | Quantity_Parameter& theAlpha) const |
7fd59977 |
204 | { |
692613e5 |
205 | if (IsEmpty() || |
206 | theX < 0 || (Standard_Size )theX >= myData.mySizeX || |
207 | theY < 0 || (Standard_Size )theY >= myData.mySizeY) |
7fd59977 |
208 | { |
85e096c3 |
209 | theAlpha = 0.0; // transparent |
7fd59977 |
210 | return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB); |
211 | } |
692613e5 |
212 | |
213 | switch (myImgFormat) |
7fd59977 |
214 | { |
692613e5 |
215 | case ImgGrayF: |
216 | { |
217 | const Standard_ShortReal& aPixel = Value<Standard_ShortReal> (theY, theX); |
218 | theAlpha = 1.0; // opaque |
219 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel)), |
220 | Quantity_Parameter (Standard_Real (aPixel)), |
221 | Quantity_Parameter (Standard_Real (aPixel)), |
222 | Quantity_TOC_RGB); |
692613e5 |
223 | } |
224 | case ImgRGBAF: |
225 | { |
226 | const Image_ColorRGBAF& aPixel = Value<Image_ColorRGBAF> (theY, theX); |
227 | theAlpha = aPixel.a(); |
228 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
229 | Quantity_Parameter (aPixel.g()), |
230 | Quantity_Parameter (aPixel.b()), |
231 | Quantity_TOC_RGB); |
232 | } |
233 | case ImgBGRAF: |
234 | { |
235 | const Image_ColorBGRAF& aPixel = Value<Image_ColorBGRAF> (theY, theX); |
236 | theAlpha = aPixel.a(); |
237 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
238 | Quantity_Parameter (aPixel.g()), |
239 | Quantity_Parameter (aPixel.b()), |
240 | Quantity_TOC_RGB); |
241 | } |
242 | case ImgRGBF: |
243 | { |
244 | const Image_ColorRGBF& aPixel = Value<Image_ColorRGBF> (theY, theX); |
245 | theAlpha = 1.0; // opaque |
246 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
247 | Quantity_Parameter (aPixel.g()), |
248 | Quantity_Parameter (aPixel.b()), |
249 | Quantity_TOC_RGB); |
250 | } |
251 | case ImgBGRF: |
7fd59977 |
252 | { |
692613e5 |
253 | const Image_ColorBGRF& aPixel = Value<Image_ColorBGRF> (theY, theX); |
254 | theAlpha = 1.0; // opaque |
255 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
256 | Quantity_Parameter (aPixel.g()), |
257 | Quantity_Parameter (aPixel.b()), |
258 | Quantity_TOC_RGB); |
259 | } |
260 | case ImgRGBA: |
261 | { |
262 | const Image_ColorRGBA& aPixel = Value<Image_ColorRGBA> (theY, theX); |
263 | theAlpha = Standard_Real (aPixel.a()) / 255.0; |
264 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
265 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
266 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
267 | Quantity_TOC_RGB); |
268 | } |
269 | case ImgBGRA: |
270 | { |
271 | const Image_ColorBGRA& aPixel = Value<Image_ColorBGRA> (theY, theX); |
272 | theAlpha = Standard_Real (aPixel.a()) / 255.0; |
273 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
274 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
275 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
276 | Quantity_TOC_RGB); |
277 | } |
278 | case ImgRGB32: |
279 | { |
280 | const Image_ColorRGB32& aPixel = Value<Image_ColorRGB32> (theY, theX); |
281 | theAlpha = 1.0; // opaque |
282 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
283 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
284 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
285 | Quantity_TOC_RGB); |
286 | } |
287 | case ImgBGR32: |
288 | { |
289 | const Image_ColorBGR32& aPixel = Value<Image_ColorBGR32> (theY, theX); |
290 | theAlpha = 1.0; // opaque |
291 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
292 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
293 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
294 | Quantity_TOC_RGB); |
295 | } |
296 | case ImgRGB: |
297 | { |
298 | const Image_ColorRGB& aPixel = Value<Image_ColorRGB> (theY, theX); |
299 | theAlpha = 1.0; // opaque |
300 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
301 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
302 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
303 | Quantity_TOC_RGB); |
304 | } |
305 | case ImgBGR: |
306 | { |
307 | const Image_ColorBGR& aPixel = Value<Image_ColorBGR> (theY, theX); |
308 | theAlpha = 1.0; // opaque |
309 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
310 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
311 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
312 | Quantity_TOC_RGB); |
313 | } |
314 | case ImgGray: |
315 | { |
316 | const Standard_Byte& aPixel = Value<Standard_Byte> (theY, theX); |
317 | theAlpha = 1.0; // opaque |
318 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel) / 255.0), |
319 | Quantity_Parameter (Standard_Real (aPixel) / 255.0), |
320 | Quantity_Parameter (Standard_Real (aPixel) / 255.0), |
321 | Quantity_TOC_RGB); |
322 | } |
323 | default: |
324 | { |
325 | // not supported image type |
326 | theAlpha = 0.0; // transparent |
327 | return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB); |
7fd59977 |
328 | } |
329 | } |
7fd59977 |
330 | } |