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> |
ca0c0b11 |
17 | #include <NCollection_AlignedAllocator.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() |
ca0c0b11 |
27 | : myImgFormat (Image_PixMap::ImgGray) |
692613e5 |
28 | { |
ca0c0b11 |
29 | // |
692613e5 |
30 | } |
7fd59977 |
31 | |
692613e5 |
32 | // ======================================================================= |
33 | // function : ~Image_PixMap |
34 | // purpose : |
35 | // ======================================================================= |
36 | Image_PixMap::~Image_PixMap() |
37 | { |
38 | Clear(); |
39 | } |
7fd59977 |
40 | |
692613e5 |
41 | Standard_Size Image_PixMap::SizePixelBytes (const Image_PixMap::ImgFormat thePixelFormat) |
7fd59977 |
42 | { |
692613e5 |
43 | switch (thePixelFormat) |
7fd59977 |
44 | { |
692613e5 |
45 | case ImgGrayF: |
46 | return sizeof(float); |
47 | case ImgRGBAF: |
48 | case ImgBGRAF: |
49 | return sizeof(float) * 4; |
50 | case ImgRGBF: |
51 | case ImgBGRF: |
52 | return sizeof(float) * 3; |
53 | case ImgRGBA: |
54 | case ImgBGRA: |
55 | return 4; |
56 | case ImgRGB32: |
57 | case ImgBGR32: |
58 | return 4; |
59 | case ImgRGB: |
60 | case ImgBGR: |
61 | return 3; |
62 | case ImgGray: |
7fd59977 |
63 | default: |
692613e5 |
64 | return 1; |
7fd59977 |
65 | } |
7fd59977 |
66 | } |
67 | |
692613e5 |
68 | // ======================================================================= |
69 | // function : setFormat |
70 | // purpose : |
71 | // ======================================================================= |
72 | void Image_PixMap::setFormat (Image_PixMap::ImgFormat thePixelFormat) |
7fd59977 |
73 | { |
ca0c0b11 |
74 | myImgFormat = thePixelFormat; |
7fd59977 |
75 | } |
76 | |
692613e5 |
77 | // ======================================================================= |
78 | // function : InitWrapper |
79 | // purpose : |
80 | // ======================================================================= |
81 | bool Image_PixMap::InitWrapper (Image_PixMap::ImgFormat thePixelFormat, |
82 | Standard_Byte* theDataPtr, |
83 | const Standard_Size theSizeX, |
84 | const Standard_Size theSizeY, |
85 | const Standard_Size theSizeRowBytes) |
7fd59977 |
86 | { |
ca0c0b11 |
87 | Clear(); |
88 | myImgFormat = thePixelFormat; |
692613e5 |
89 | if ((theSizeX == 0) || (theSizeY == 0) || (theDataPtr == NULL)) |
7fd59977 |
90 | { |
692613e5 |
91 | return false; |
7fd59977 |
92 | } |
ca0c0b11 |
93 | |
94 | Handle(NCollection_BaseAllocator) anEmptyAlloc; |
95 | myData.Init (anEmptyAlloc, Image_PixMap::SizePixelBytes (thePixelFormat), |
96 | theSizeX, theSizeY, theSizeRowBytes, theDataPtr); |
692613e5 |
97 | return true; |
98 | } |
7fd59977 |
99 | |
692613e5 |
100 | // ======================================================================= |
101 | // function : InitTrash |
102 | // purpose : |
103 | // ======================================================================= |
104 | bool Image_PixMap::InitTrash (Image_PixMap::ImgFormat thePixelFormat, |
105 | const Standard_Size theSizeX, |
106 | const Standard_Size theSizeY, |
107 | const Standard_Size theSizeRowBytes) |
108 | { |
ca0c0b11 |
109 | Clear(); |
110 | myImgFormat = thePixelFormat; |
692613e5 |
111 | if ((theSizeX == 0) || (theSizeY == 0)) |
7fd59977 |
112 | { |
692613e5 |
113 | return false; |
7fd59977 |
114 | } |
ca0c0b11 |
115 | |
116 | // use argument only if it greater |
117 | const Standard_Size aSizeRowBytes = std::max (theSizeRowBytes, theSizeX * SizePixelBytes (thePixelFormat)); |
118 | Handle(NCollection_BaseAllocator) anAlloc = new NCollection_AlignedAllocator (16); |
119 | myData.Init (anAlloc, Image_PixMap::SizePixelBytes (thePixelFormat), |
120 | theSizeX, theSizeY, aSizeRowBytes, NULL); |
121 | return !myData.IsEmpty(); |
7fd59977 |
122 | } |
123 | |
692613e5 |
124 | // ======================================================================= |
125 | // function : InitZero |
126 | // purpose : |
127 | // ======================================================================= |
128 | bool Image_PixMap::InitZero (Image_PixMap::ImgFormat thePixelFormat, |
129 | const Standard_Size theSizeX, |
130 | const Standard_Size theSizeY, |
131 | const Standard_Size theSizeRowBytes, |
132 | const Standard_Byte theValue) |
7fd59977 |
133 | { |
692613e5 |
134 | if (!InitTrash (thePixelFormat, theSizeX, theSizeY, theSizeRowBytes)) |
135 | { |
136 | return false; |
137 | } |
ca0c0b11 |
138 | memset (myData.ChangeData(), (int )theValue, SizeBytes()); |
692613e5 |
139 | return true; |
7fd59977 |
140 | } |
141 | |
692613e5 |
142 | // ======================================================================= |
143 | // function : InitCopy |
144 | // purpose : |
145 | // ======================================================================= |
146 | bool Image_PixMap::InitCopy (const Image_PixMap& theCopy) |
7fd59977 |
147 | { |
692613e5 |
148 | if (&theCopy == this) |
7fd59977 |
149 | { |
692613e5 |
150 | // self-copying disallowed |
151 | return false; |
7fd59977 |
152 | } |
ca0c0b11 |
153 | if (InitTrash (theCopy.myImgFormat, theCopy.SizeX(), theCopy.SizeY(), theCopy.SizeRowBytes())) |
692613e5 |
154 | { |
ca0c0b11 |
155 | memcpy (myData.ChangeData(), theCopy.myData.Data(), theCopy.SizeBytes()); |
692613e5 |
156 | return true; |
157 | } |
158 | return false; |
7fd59977 |
159 | } |
160 | |
85e096c3 |
161 | // ======================================================================= |
692613e5 |
162 | // function : Clear |
85e096c3 |
163 | // purpose : |
164 | // ======================================================================= |
ca0c0b11 |
165 | void Image_PixMap::Clear() |
85e096c3 |
166 | { |
ca0c0b11 |
167 | Handle(NCollection_BaseAllocator) anEmptyAlloc; |
168 | myData.Init (anEmptyAlloc, Image_PixMap::SizePixelBytes (myImgFormat), |
169 | 0, 0, 0, NULL); |
85e096c3 |
170 | } |
171 | |
172 | // ======================================================================= |
173 | // function : PixelColor |
174 | // purpose : |
175 | // ======================================================================= |
176 | Quantity_Color Image_PixMap::PixelColor (const Standard_Integer theX, |
177 | const Standard_Integer theY, |
178 | Quantity_Parameter& theAlpha) const |
7fd59977 |
179 | { |
ca0c0b11 |
180 | if (IsEmpty() |
181 | || theX < 0 || (Standard_Size )theX >= SizeX() |
182 | || theY < 0 || (Standard_Size )theY >= SizeY()) |
7fd59977 |
183 | { |
85e096c3 |
184 | theAlpha = 0.0; // transparent |
7fd59977 |
185 | return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB); |
186 | } |
692613e5 |
187 | |
188 | switch (myImgFormat) |
7fd59977 |
189 | { |
692613e5 |
190 | case ImgGrayF: |
191 | { |
192 | const Standard_ShortReal& aPixel = Value<Standard_ShortReal> (theY, theX); |
193 | theAlpha = 1.0; // opaque |
194 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel)), |
195 | Quantity_Parameter (Standard_Real (aPixel)), |
196 | Quantity_Parameter (Standard_Real (aPixel)), |
197 | Quantity_TOC_RGB); |
692613e5 |
198 | } |
199 | case ImgRGBAF: |
200 | { |
201 | const Image_ColorRGBAF& aPixel = Value<Image_ColorRGBAF> (theY, theX); |
202 | theAlpha = aPixel.a(); |
203 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
204 | Quantity_Parameter (aPixel.g()), |
205 | Quantity_Parameter (aPixel.b()), |
206 | Quantity_TOC_RGB); |
207 | } |
208 | case ImgBGRAF: |
209 | { |
210 | const Image_ColorBGRAF& aPixel = Value<Image_ColorBGRAF> (theY, theX); |
211 | theAlpha = aPixel.a(); |
212 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
213 | Quantity_Parameter (aPixel.g()), |
214 | Quantity_Parameter (aPixel.b()), |
215 | Quantity_TOC_RGB); |
216 | } |
217 | case ImgRGBF: |
218 | { |
219 | const Image_ColorRGBF& aPixel = Value<Image_ColorRGBF> (theY, theX); |
220 | theAlpha = 1.0; // opaque |
221 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
222 | Quantity_Parameter (aPixel.g()), |
223 | Quantity_Parameter (aPixel.b()), |
224 | Quantity_TOC_RGB); |
225 | } |
226 | case ImgBGRF: |
7fd59977 |
227 | { |
692613e5 |
228 | const Image_ColorBGRF& aPixel = Value<Image_ColorBGRF> (theY, theX); |
229 | theAlpha = 1.0; // opaque |
230 | return Quantity_Color (Quantity_Parameter (aPixel.r()), |
231 | Quantity_Parameter (aPixel.g()), |
232 | Quantity_Parameter (aPixel.b()), |
233 | Quantity_TOC_RGB); |
234 | } |
235 | case ImgRGBA: |
236 | { |
237 | const Image_ColorRGBA& aPixel = Value<Image_ColorRGBA> (theY, theX); |
238 | theAlpha = Standard_Real (aPixel.a()) / 255.0; |
239 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
240 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
241 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
242 | Quantity_TOC_RGB); |
243 | } |
244 | case ImgBGRA: |
245 | { |
246 | const Image_ColorBGRA& aPixel = Value<Image_ColorBGRA> (theY, theX); |
247 | theAlpha = Standard_Real (aPixel.a()) / 255.0; |
248 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
249 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
250 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
251 | Quantity_TOC_RGB); |
252 | } |
253 | case ImgRGB32: |
254 | { |
255 | const Image_ColorRGB32& aPixel = Value<Image_ColorRGB32> (theY, theX); |
256 | theAlpha = 1.0; // opaque |
257 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
258 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
259 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
260 | Quantity_TOC_RGB); |
261 | } |
262 | case ImgBGR32: |
263 | { |
264 | const Image_ColorBGR32& aPixel = Value<Image_ColorBGR32> (theY, theX); |
265 | theAlpha = 1.0; // opaque |
266 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
267 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
268 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
269 | Quantity_TOC_RGB); |
270 | } |
271 | case ImgRGB: |
272 | { |
273 | const Image_ColorRGB& aPixel = Value<Image_ColorRGB> (theY, theX); |
274 | theAlpha = 1.0; // opaque |
275 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
276 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
277 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
278 | Quantity_TOC_RGB); |
279 | } |
280 | case ImgBGR: |
281 | { |
282 | const Image_ColorBGR& aPixel = Value<Image_ColorBGR> (theY, theX); |
283 | theAlpha = 1.0; // opaque |
284 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel.r()) / 255.0), |
285 | Quantity_Parameter (Standard_Real (aPixel.g()) / 255.0), |
286 | Quantity_Parameter (Standard_Real (aPixel.b()) / 255.0), |
287 | Quantity_TOC_RGB); |
288 | } |
289 | case ImgGray: |
290 | { |
291 | const Standard_Byte& aPixel = Value<Standard_Byte> (theY, theX); |
292 | theAlpha = 1.0; // opaque |
293 | return Quantity_Color (Quantity_Parameter (Standard_Real (aPixel) / 255.0), |
294 | Quantity_Parameter (Standard_Real (aPixel) / 255.0), |
295 | Quantity_Parameter (Standard_Real (aPixel) / 255.0), |
296 | Quantity_TOC_RGB); |
297 | } |
298 | default: |
299 | { |
300 | // not supported image type |
301 | theAlpha = 0.0; // transparent |
302 | return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB); |
7fd59977 |
303 | } |
304 | } |
7fd59977 |
305 | } |