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> |
076ca35c |
18 | #include <Standard_ProgramError.hxx> |
7fd59977 |
19 | |
a096a7a5 |
20 | #include <algorithm> |
21 | |
92efcf78 |
22 | IMPLEMENT_STANDARD_RTTIEXT(Image_PixMap,Standard_Transient) |
23 | |
692613e5 |
24 | // ======================================================================= |
25 | // function : Image_PixMap |
26 | // purpose : |
27 | // ======================================================================= |
28 | Image_PixMap::Image_PixMap() |
dc858f4c |
29 | : myImgFormat (Image_Format_Gray) |
692613e5 |
30 | { |
ca0c0b11 |
31 | // |
692613e5 |
32 | } |
7fd59977 |
33 | |
692613e5 |
34 | // ======================================================================= |
35 | // function : ~Image_PixMap |
36 | // purpose : |
37 | // ======================================================================= |
38 | Image_PixMap::~Image_PixMap() |
39 | { |
40 | Clear(); |
41 | } |
7fd59977 |
42 | |
dc858f4c |
43 | Standard_Size Image_PixMap::SizePixelBytes (const Image_Format thePixelFormat) |
7fd59977 |
44 | { |
692613e5 |
45 | switch (thePixelFormat) |
7fd59977 |
46 | { |
dc858f4c |
47 | case Image_Format_GrayF: |
48 | case Image_Format_AlphaF: |
692613e5 |
49 | return sizeof(float); |
67312b79 |
50 | case Image_Format_RGF: |
51 | return sizeof(float) * 2; |
dc858f4c |
52 | case Image_Format_RGBAF: |
53 | case Image_Format_BGRAF: |
692613e5 |
54 | return sizeof(float) * 4; |
dc858f4c |
55 | case Image_Format_RGBF: |
56 | case Image_Format_BGRF: |
692613e5 |
57 | return sizeof(float) * 3; |
dc858f4c |
58 | case Image_Format_RGBA: |
59 | case Image_Format_BGRA: |
692613e5 |
60 | return 4; |
dc858f4c |
61 | case Image_Format_RGB32: |
62 | case Image_Format_BGR32: |
692613e5 |
63 | return 4; |
dc858f4c |
64 | case Image_Format_RGB: |
65 | case Image_Format_BGR: |
692613e5 |
66 | return 3; |
dc858f4c |
67 | case Image_Format_Gray: |
68 | case Image_Format_Alpha: |
076ca35c |
69 | return 1; |
dc858f4c |
70 | case Image_Format_UNKNOWN: |
692613e5 |
71 | return 1; |
7fd59977 |
72 | } |
076ca35c |
73 | return 1; |
7fd59977 |
74 | } |
75 | |
692613e5 |
76 | // ======================================================================= |
076ca35c |
77 | // function : SetFormat |
692613e5 |
78 | // purpose : |
79 | // ======================================================================= |
dc858f4c |
80 | void Image_PixMap::SetFormat (Image_Format thePixelFormat) |
7fd59977 |
81 | { |
076ca35c |
82 | if (myImgFormat == thePixelFormat) |
83 | { |
84 | return; |
85 | } |
86 | |
87 | if (!IsEmpty() |
88 | && SizePixelBytes (myImgFormat) != SizePixelBytes (thePixelFormat)) |
89 | { |
9775fa61 |
90 | throw Standard_ProgramError("Image_PixMap::SetFormat() - incompatible pixel format"); |
076ca35c |
91 | return; |
92 | } |
93 | |
ca0c0b11 |
94 | myImgFormat = thePixelFormat; |
7fd59977 |
95 | } |
96 | |
692613e5 |
97 | // ======================================================================= |
98 | // function : InitWrapper |
99 | // purpose : |
100 | // ======================================================================= |
dc858f4c |
101 | bool Image_PixMap::InitWrapper (Image_Format thePixelFormat, |
102 | Standard_Byte* theDataPtr, |
103 | const Standard_Size theSizeX, |
104 | const Standard_Size theSizeY, |
105 | const Standard_Size theSizeRowBytes) |
7fd59977 |
106 | { |
ca0c0b11 |
107 | Clear(); |
108 | myImgFormat = thePixelFormat; |
692613e5 |
109 | if ((theSizeX == 0) || (theSizeY == 0) || (theDataPtr == NULL)) |
7fd59977 |
110 | { |
692613e5 |
111 | return false; |
7fd59977 |
112 | } |
ca0c0b11 |
113 | |
114 | Handle(NCollection_BaseAllocator) anEmptyAlloc; |
115 | myData.Init (anEmptyAlloc, Image_PixMap::SizePixelBytes (thePixelFormat), |
116 | theSizeX, theSizeY, theSizeRowBytes, theDataPtr); |
692613e5 |
117 | return true; |
118 | } |
7fd59977 |
119 | |
692613e5 |
120 | // ======================================================================= |
121 | // function : InitTrash |
122 | // purpose : |
123 | // ======================================================================= |
dc858f4c |
124 | bool Image_PixMap::InitTrash (Image_Format thePixelFormat, |
125 | const Standard_Size theSizeX, |
126 | const Standard_Size theSizeY, |
127 | const Standard_Size theSizeRowBytes) |
692613e5 |
128 | { |
ca0c0b11 |
129 | Clear(); |
130 | myImgFormat = thePixelFormat; |
692613e5 |
131 | if ((theSizeX == 0) || (theSizeY == 0)) |
7fd59977 |
132 | { |
692613e5 |
133 | return false; |
7fd59977 |
134 | } |
ca0c0b11 |
135 | |
136 | // use argument only if it greater |
137 | const Standard_Size aSizeRowBytes = std::max (theSizeRowBytes, theSizeX * SizePixelBytes (thePixelFormat)); |
138 | Handle(NCollection_BaseAllocator) anAlloc = new NCollection_AlignedAllocator (16); |
139 | myData.Init (anAlloc, Image_PixMap::SizePixelBytes (thePixelFormat), |
140 | theSizeX, theSizeY, aSizeRowBytes, NULL); |
141 | return !myData.IsEmpty(); |
7fd59977 |
142 | } |
143 | |
692613e5 |
144 | // ======================================================================= |
145 | // function : InitZero |
146 | // purpose : |
147 | // ======================================================================= |
dc858f4c |
148 | bool Image_PixMap::InitZero (Image_Format 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 | } |
ca0c0b11 |
158 | memset (myData.ChangeData(), (int )theValue, SizeBytes()); |
692613e5 |
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 | } |
ca0c0b11 |
173 | if (InitTrash (theCopy.myImgFormat, theCopy.SizeX(), theCopy.SizeY(), theCopy.SizeRowBytes())) |
692613e5 |
174 | { |
ca0c0b11 |
175 | memcpy (myData.ChangeData(), theCopy.myData.Data(), theCopy.SizeBytes()); |
692613e5 |
176 | return true; |
177 | } |
178 | return false; |
7fd59977 |
179 | } |
180 | |
85e096c3 |
181 | // ======================================================================= |
692613e5 |
182 | // function : Clear |
85e096c3 |
183 | // purpose : |
184 | // ======================================================================= |
ca0c0b11 |
185 | void Image_PixMap::Clear() |
85e096c3 |
186 | { |
ca0c0b11 |
187 | Handle(NCollection_BaseAllocator) anEmptyAlloc; |
188 | myData.Init (anEmptyAlloc, Image_PixMap::SizePixelBytes (myImgFormat), |
189 | 0, 0, 0, NULL); |
85e096c3 |
190 | } |
191 | |
192 | // ======================================================================= |
193 | // function : PixelColor |
194 | // purpose : |
195 | // ======================================================================= |
e958a649 |
196 | Quantity_ColorRGBA Image_PixMap::PixelColor (const Standard_Integer theX, |
aaf8d6a9 |
197 | const Standard_Integer theY, |
198 | const Standard_Boolean theToLinearize) const |
7fd59977 |
199 | { |
ca0c0b11 |
200 | if (IsEmpty() |
201 | || theX < 0 || (Standard_Size )theX >= SizeX() |
202 | || theY < 0 || (Standard_Size )theY >= SizeY()) |
7fd59977 |
203 | { |
e958a649 |
204 | return Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 0.0f); // transparent |
7fd59977 |
205 | } |
692613e5 |
206 | |
207 | switch (myImgFormat) |
7fd59977 |
208 | { |
dc858f4c |
209 | case Image_Format_GrayF: |
692613e5 |
210 | { |
211 | const Standard_ShortReal& aPixel = Value<Standard_ShortReal> (theY, theX); |
e958a649 |
212 | return Quantity_ColorRGBA (NCollection_Vec4<float> (aPixel, aPixel, aPixel, 1.0f)); // opaque |
692613e5 |
213 | } |
dc858f4c |
214 | case Image_Format_AlphaF: |
076ca35c |
215 | { |
216 | const Standard_ShortReal& aPixel = Value<Standard_ShortReal> (theY, theX); |
e958a649 |
217 | return Quantity_ColorRGBA (NCollection_Vec4<float> (1.0f, 1.0f, 1.0f, aPixel)); |
076ca35c |
218 | } |
67312b79 |
219 | case Image_Format_RGF: |
220 | { |
221 | const Image_ColorRGF& aPixel = Value<Image_ColorRGF> (theY, theX); |
222 | return Quantity_ColorRGBA (NCollection_Vec4<float> (aPixel.r(), aPixel.g(), 0.0f, 1.0f)); |
223 | } |
dc858f4c |
224 | case Image_Format_RGBAF: |
692613e5 |
225 | { |
226 | const Image_ColorRGBAF& aPixel = Value<Image_ColorRGBAF> (theY, theX); |
e958a649 |
227 | return Quantity_ColorRGBA (NCollection_Vec4<float> (aPixel.r(), aPixel.g(), aPixel.b(), aPixel.a())); |
692613e5 |
228 | } |
dc858f4c |
229 | case Image_Format_BGRAF: |
692613e5 |
230 | { |
231 | const Image_ColorBGRAF& aPixel = Value<Image_ColorBGRAF> (theY, theX); |
e958a649 |
232 | return Quantity_ColorRGBA (NCollection_Vec4<float> (aPixel.r(), aPixel.g(), aPixel.b(), aPixel.a())); |
692613e5 |
233 | } |
dc858f4c |
234 | case Image_Format_RGBF: |
692613e5 |
235 | { |
236 | const Image_ColorRGBF& aPixel = Value<Image_ColorRGBF> (theY, theX); |
e958a649 |
237 | return Quantity_ColorRGBA (NCollection_Vec4<float> (aPixel.r(), aPixel.g(), aPixel.b(), 1.0f)); // opaque |
692613e5 |
238 | } |
dc858f4c |
239 | case Image_Format_BGRF: |
7fd59977 |
240 | { |
692613e5 |
241 | const Image_ColorBGRF& aPixel = Value<Image_ColorBGRF> (theY, theX); |
e958a649 |
242 | return Quantity_ColorRGBA (NCollection_Vec4<float> (aPixel.r(), aPixel.g(), aPixel.b(), 1.0f)); // opaque |
692613e5 |
243 | } |
dc858f4c |
244 | case Image_Format_RGBA: |
692613e5 |
245 | { |
246 | const Image_ColorRGBA& aPixel = Value<Image_ColorRGBA> (theY, theX); |
aaf8d6a9 |
247 | return theToLinearize |
248 | ? Quantity_ColorRGBA (Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.r()) / 255.0f), |
249 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.g()) / 255.0f), |
250 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.b()) / 255.0f), |
251 | float(aPixel.a()) / 255.0f) |
252 | : Quantity_ColorRGBA (float(aPixel.r()) / 255.0f, float(aPixel.g()) / 255.0f, float(aPixel.b()) / 255.0f, float(aPixel.a()) / 255.0f); |
692613e5 |
253 | } |
dc858f4c |
254 | case Image_Format_BGRA: |
692613e5 |
255 | { |
256 | const Image_ColorBGRA& aPixel = Value<Image_ColorBGRA> (theY, theX); |
aaf8d6a9 |
257 | return theToLinearize |
258 | ? Quantity_ColorRGBA (Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.r()) / 255.0f), |
259 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.g()) / 255.0f), |
260 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.b()) / 255.0f), |
261 | float(aPixel.a()) / 255.0f) |
262 | : Quantity_ColorRGBA (float(aPixel.r()) / 255.0f, float(aPixel.g()) / 255.0f, float(aPixel.b()) / 255.0f, float(aPixel.a()) / 255.0f); |
692613e5 |
263 | } |
dc858f4c |
264 | case Image_Format_RGB32: |
692613e5 |
265 | { |
266 | const Image_ColorRGB32& aPixel = Value<Image_ColorRGB32> (theY, theX); |
aaf8d6a9 |
267 | return theToLinearize |
268 | ? Quantity_ColorRGBA (Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.r()) / 255.0f), |
269 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.g()) / 255.0f), |
270 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.b()) / 255.0f), 1.0f) |
271 | : Quantity_ColorRGBA (float(aPixel.r()) / 255.0f, float(aPixel.g()) / 255.0f, float(aPixel.b()) / 255.0f, 1.0f); |
692613e5 |
272 | } |
dc858f4c |
273 | case Image_Format_BGR32: |
692613e5 |
274 | { |
275 | const Image_ColorBGR32& aPixel = Value<Image_ColorBGR32> (theY, theX); |
aaf8d6a9 |
276 | return theToLinearize |
277 | ? Quantity_ColorRGBA (Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.r()) / 255.0f), |
278 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.g()) / 255.0f), |
279 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.b()) / 255.0f), 1.0f) |
280 | : Quantity_ColorRGBA (float(aPixel.r()) / 255.0f, float(aPixel.g()) / 255.0f, float(aPixel.b()) / 255.0f, 1.0f); |
692613e5 |
281 | } |
dc858f4c |
282 | case Image_Format_RGB: |
692613e5 |
283 | { |
284 | const Image_ColorRGB& aPixel = Value<Image_ColorRGB> (theY, theX); |
aaf8d6a9 |
285 | return theToLinearize |
286 | ? Quantity_ColorRGBA (Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.r()) / 255.0f), |
287 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.g()) / 255.0f), |
288 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.b()) / 255.0f), 1.0f) |
289 | : Quantity_ColorRGBA (float(aPixel.r()) / 255.0f, float(aPixel.g()) / 255.0f, float(aPixel.b()) / 255.0f, 1.0f); |
692613e5 |
290 | } |
dc858f4c |
291 | case Image_Format_BGR: |
692613e5 |
292 | { |
293 | const Image_ColorBGR& aPixel = Value<Image_ColorBGR> (theY, theX); |
aaf8d6a9 |
294 | return theToLinearize |
295 | ? Quantity_ColorRGBA (Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.r()) / 255.0f), |
296 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.g()) / 255.0f), |
297 | Quantity_Color::Convert_sRGB_To_LinearRGB (float(aPixel.b()) / 255.0f), 1.0f) |
298 | : Quantity_ColorRGBA (float(aPixel.r()) / 255.0f, float(aPixel.g()) / 255.0f, float(aPixel.b()) / 255.0f, 1.0f); |
692613e5 |
299 | } |
dc858f4c |
300 | case Image_Format_Gray: |
692613e5 |
301 | { |
302 | const Standard_Byte& aPixel = Value<Standard_Byte> (theY, theX); |
e958a649 |
303 | return Quantity_ColorRGBA (float(aPixel) / 255.0f, float(aPixel) / 255.0f, float(aPixel) / 255.0f, 1.0f); // opaque |
692613e5 |
304 | } |
dc858f4c |
305 | case Image_Format_Alpha: |
692613e5 |
306 | { |
076ca35c |
307 | const Standard_Byte& aPixel = Value<Standard_Byte> (theY, theX); |
e958a649 |
308 | return Quantity_ColorRGBA (1.0f, 1.0f, 1.0f, float(aPixel) / 255.0f); |
076ca35c |
309 | } |
dc858f4c |
310 | case Image_Format_UNKNOWN: |
076ca35c |
311 | { |
312 | break; |
7fd59977 |
313 | } |
314 | } |
076ca35c |
315 | |
316 | // unsupported image type |
e958a649 |
317 | return Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 0.0f); // transparent |
7fd59977 |
318 | } |
f9f740d6 |
319 | |
decdee7d |
320 | // ======================================================================= |
321 | // function : SetPixelColor |
322 | // purpose : |
323 | // ======================================================================= |
324 | void Image_PixMap::SetPixelColor (const Standard_Integer theX, |
325 | const Standard_Integer theY, |
aaf8d6a9 |
326 | const Quantity_ColorRGBA& theColor, |
327 | const Standard_Boolean theToDeLinearize) |
decdee7d |
328 | { |
329 | if (IsEmpty() |
330 | || theX < 0 || Standard_Size(theX) >= SizeX() |
331 | || theY < 0 || Standard_Size(theY) >= SizeY()) |
332 | { |
333 | return; |
334 | } |
335 | |
e958a649 |
336 | const NCollection_Vec4<float>& aColor = theColor; |
decdee7d |
337 | switch (myImgFormat) |
338 | { |
dc858f4c |
339 | case Image_Format_GrayF: |
decdee7d |
340 | { |
e958a649 |
341 | ChangeValue<Standard_ShortReal> (theY, theX) = aColor.r(); |
decdee7d |
342 | return; |
343 | } |
dc858f4c |
344 | case Image_Format_AlphaF: |
decdee7d |
345 | { |
e958a649 |
346 | ChangeValue<Standard_ShortReal> (theY, theX) = aColor.a(); |
decdee7d |
347 | return; |
348 | } |
67312b79 |
349 | case Image_Format_RGF: |
350 | { |
351 | Image_ColorRGF& aPixel = ChangeValue<Image_ColorRGF> (theY, theX); |
352 | aPixel.r() = aColor.r(); |
353 | aPixel.g() = aColor.g(); |
354 | return; |
355 | } |
dc858f4c |
356 | case Image_Format_RGBAF: |
decdee7d |
357 | { |
358 | Image_ColorRGBAF& aPixel = ChangeValue<Image_ColorRGBAF> (theY, theX); |
e958a649 |
359 | aPixel.r() = aColor.r(); |
360 | aPixel.g() = aColor.g(); |
361 | aPixel.b() = aColor.b(); |
362 | aPixel.a() = aColor.a(); |
decdee7d |
363 | return; |
364 | } |
dc858f4c |
365 | case Image_Format_BGRAF: |
decdee7d |
366 | { |
367 | Image_ColorBGRAF& aPixel = ChangeValue<Image_ColorBGRAF> (theY, theX); |
e958a649 |
368 | aPixel.r() = aColor.r(); |
369 | aPixel.g() = aColor.g(); |
370 | aPixel.b() = aColor.b(); |
371 | aPixel.a() = aColor.a(); |
decdee7d |
372 | return; |
373 | } |
dc858f4c |
374 | case Image_Format_RGBF: |
decdee7d |
375 | { |
376 | Image_ColorRGBF& aPixel = ChangeValue<Image_ColorRGBF> (theY, theX); |
e958a649 |
377 | aPixel.r() = aColor.r(); |
378 | aPixel.g() = aColor.g(); |
379 | aPixel.b() = aColor.b(); |
decdee7d |
380 | return; |
381 | } |
dc858f4c |
382 | case Image_Format_BGRF: |
decdee7d |
383 | { |
384 | Image_ColorBGRF& aPixel = ChangeValue<Image_ColorBGRF> (theY, theX); |
e958a649 |
385 | aPixel.r() = aColor.r(); |
386 | aPixel.g() = aColor.g(); |
387 | aPixel.b() = aColor.b(); |
decdee7d |
388 | return; |
389 | } |
dc858f4c |
390 | case Image_Format_RGBA: |
decdee7d |
391 | { |
392 | Image_ColorRGBA& aPixel = ChangeValue<Image_ColorRGBA> (theY, theX); |
aaf8d6a9 |
393 | if (theToDeLinearize) |
394 | { |
395 | aPixel.r() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.r()) * 255.0f); |
396 | aPixel.g() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.g()) * 255.0f); |
397 | aPixel.b() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.b()) * 255.0f); |
398 | } |
399 | else |
400 | { |
401 | aPixel.r() = Standard_Byte(aColor.r() * 255.0f); |
402 | aPixel.g() = Standard_Byte(aColor.g() * 255.0f); |
403 | aPixel.b() = Standard_Byte(aColor.b() * 255.0f); |
404 | } |
e958a649 |
405 | aPixel.a() = Standard_Byte(aColor.a() * 255.0f); |
decdee7d |
406 | return; |
407 | } |
dc858f4c |
408 | case Image_Format_BGRA: |
decdee7d |
409 | { |
410 | Image_ColorBGRA& aPixel = ChangeValue<Image_ColorBGRA> (theY, theX); |
aaf8d6a9 |
411 | if (theToDeLinearize) |
412 | { |
413 | aPixel.r() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.r()) * 255.0f); |
414 | aPixel.g() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.g()) * 255.0f); |
415 | aPixel.b() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.b()) * 255.0f); |
416 | } |
417 | else |
418 | { |
419 | aPixel.r() = Standard_Byte(aColor.r() * 255.0f); |
420 | aPixel.g() = Standard_Byte(aColor.g() * 255.0f); |
421 | aPixel.b() = Standard_Byte(aColor.b() * 255.0f); |
422 | } |
e958a649 |
423 | aPixel.a() = Standard_Byte(aColor.a() * 255.0f); |
decdee7d |
424 | return; |
425 | } |
dc858f4c |
426 | case Image_Format_RGB32: |
decdee7d |
427 | { |
428 | Image_ColorRGB32& aPixel = ChangeValue<Image_ColorRGB32> (theY, theX); |
aaf8d6a9 |
429 | if (theToDeLinearize) |
430 | { |
431 | aPixel.r() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.r()) * 255.0f); |
432 | aPixel.g() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.g()) * 255.0f); |
433 | aPixel.b() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.b()) * 255.0f); |
434 | } |
435 | else |
436 | { |
437 | aPixel.r() = Standard_Byte(aColor.r() * 255.0f); |
438 | aPixel.g() = Standard_Byte(aColor.g() * 255.0f); |
439 | aPixel.b() = Standard_Byte(aColor.b() * 255.0f); |
440 | } |
decdee7d |
441 | aPixel.a_() = 255; |
442 | return; |
443 | } |
dc858f4c |
444 | case Image_Format_BGR32: |
decdee7d |
445 | { |
446 | Image_ColorBGR32& aPixel = ChangeValue<Image_ColorBGR32> (theY, theX); |
aaf8d6a9 |
447 | if (theToDeLinearize) |
448 | { |
449 | aPixel.r() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.r()) * 255.0f); |
450 | aPixel.g() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.g()) * 255.0f); |
451 | aPixel.b() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.b()) * 255.0f); |
452 | } |
453 | else |
454 | { |
455 | aPixel.r() = Standard_Byte(aColor.r() * 255.0f); |
456 | aPixel.g() = Standard_Byte(aColor.g() * 255.0f); |
457 | aPixel.b() = Standard_Byte(aColor.b() * 255.0f); |
458 | } |
decdee7d |
459 | aPixel.a_() = 255; |
460 | return; |
461 | } |
dc858f4c |
462 | case Image_Format_RGB: |
decdee7d |
463 | { |
464 | Image_ColorRGB& aPixel = ChangeValue<Image_ColorRGB> (theY, theX); |
aaf8d6a9 |
465 | if (theToDeLinearize) |
466 | { |
467 | aPixel.r() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.r()) * 255.0f); |
468 | aPixel.g() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.g()) * 255.0f); |
469 | aPixel.b() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.b()) * 255.0f); |
470 | } |
471 | else |
472 | { |
473 | aPixel.r() = Standard_Byte(aColor.r() * 255.0f); |
474 | aPixel.g() = Standard_Byte(aColor.g() * 255.0f); |
475 | aPixel.b() = Standard_Byte(aColor.b() * 255.0f); |
476 | } |
decdee7d |
477 | return; |
478 | } |
dc858f4c |
479 | case Image_Format_BGR: |
decdee7d |
480 | { |
481 | Image_ColorBGR& aPixel = ChangeValue<Image_ColorBGR> (theY, theX); |
aaf8d6a9 |
482 | if (theToDeLinearize) |
483 | { |
484 | aPixel.r() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.r()) * 255.0f); |
485 | aPixel.g() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.g()) * 255.0f); |
486 | aPixel.b() = Standard_Byte(Quantity_Color::Convert_LinearRGB_To_sRGB (aColor.b()) * 255.0f); |
487 | } |
488 | else |
489 | { |
490 | aPixel.r() = Standard_Byte(aColor.r() * 255.0f); |
491 | aPixel.g() = Standard_Byte(aColor.g() * 255.0f); |
492 | aPixel.b() = Standard_Byte(aColor.b() * 255.0f); |
493 | } |
decdee7d |
494 | return; |
495 | } |
dc858f4c |
496 | case Image_Format_Gray: |
decdee7d |
497 | { |
e958a649 |
498 | ChangeValue<Standard_Byte> (theY, theX) = Standard_Byte(aColor.r() * 255.0f); |
decdee7d |
499 | return; |
500 | } |
dc858f4c |
501 | case Image_Format_Alpha: |
decdee7d |
502 | { |
e958a649 |
503 | ChangeValue<Standard_Byte> (theY, theX) = Standard_Byte(aColor.a() * 255.0f); |
decdee7d |
504 | return; |
505 | } |
dc858f4c |
506 | case Image_Format_UNKNOWN: |
decdee7d |
507 | { |
508 | return; |
509 | } |
510 | } |
511 | } |
512 | |
f9f740d6 |
513 | // ======================================================================= |
514 | // function : SwapRgbaBgra |
515 | // purpose : |
516 | // ======================================================================= |
517 | bool Image_PixMap::SwapRgbaBgra (Image_PixMap& theImage) |
518 | { |
519 | switch (theImage.Format()) |
520 | { |
dc858f4c |
521 | case Image_Format_BGR32: |
522 | case Image_Format_RGB32: |
523 | case Image_Format_BGRA: |
524 | case Image_Format_RGBA: |
f9f740d6 |
525 | { |
dc858f4c |
526 | const bool toResetAlpha = theImage.Format() == Image_Format_BGR32 |
527 | || theImage.Format() == Image_Format_RGB32; |
f9f740d6 |
528 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) |
529 | { |
530 | for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) |
531 | { |
532 | Image_ColorRGBA& aPixel = theImage.ChangeValue<Image_ColorRGBA> (aRow, aCol); |
533 | Image_ColorBGRA aPixelCopy = theImage.Value <Image_ColorBGRA> (aRow, aCol); |
534 | aPixel.r() = aPixelCopy.r(); |
535 | aPixel.g() = aPixelCopy.g(); |
536 | aPixel.b() = aPixelCopy.b(); |
537 | if (toResetAlpha) |
538 | { |
539 | aPixel.a() = 255; |
540 | } |
541 | } |
542 | } |
543 | return true; |
544 | } |
dc858f4c |
545 | case Image_Format_BGR: |
546 | case Image_Format_RGB: |
f9f740d6 |
547 | { |
548 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) |
549 | { |
550 | for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) |
551 | { |
552 | Image_ColorRGB& aPixel = theImage.ChangeValue<Image_ColorRGB> (aRow, aCol); |
553 | Image_ColorBGR aPixelCopy = theImage.Value <Image_ColorBGR> (aRow, aCol); |
554 | aPixel.r() = aPixelCopy.r(); |
555 | aPixel.g() = aPixelCopy.g(); |
556 | aPixel.b() = aPixelCopy.b(); |
557 | } |
558 | } |
559 | return true; |
560 | } |
dc858f4c |
561 | case Image_Format_BGRF: |
562 | case Image_Format_RGBF: |
563 | case Image_Format_BGRAF: |
564 | case Image_Format_RGBAF: |
f9f740d6 |
565 | { |
566 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) |
567 | { |
568 | for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) |
569 | { |
570 | Image_ColorRGBF& aPixel = theImage.ChangeValue<Image_ColorRGBF> (aRow, aCol); |
571 | Image_ColorBGRF aPixelCopy = theImage.Value <Image_ColorBGRF> (aRow, aCol); |
572 | aPixel.r() = aPixelCopy.r(); |
573 | aPixel.g() = aPixelCopy.g(); |
574 | aPixel.b() = aPixelCopy.b(); |
575 | } |
576 | } |
577 | return true; |
578 | } |
579 | default: return false; |
580 | } |
581 | } |
e958a649 |
582 | |
583 | // ======================================================================= |
584 | // function : ToBlackWhite |
585 | // purpose : |
586 | // ======================================================================= |
587 | void Image_PixMap::ToBlackWhite (Image_PixMap& theImage) |
588 | { |
589 | switch (theImage.Format()) |
590 | { |
591 | case Image_Format_Gray: |
592 | case Image_Format_Alpha: |
593 | { |
594 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) |
595 | { |
596 | for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) |
597 | { |
598 | unsigned char& aPixel = theImage.ChangeValue<unsigned char> (aRow, aCol); |
599 | if (aPixel != 0) |
600 | { |
601 | aPixel = 255; |
602 | } |
603 | } |
604 | } |
605 | break; |
606 | } |
607 | case Image_Format_RGB: |
608 | case Image_Format_BGR: |
609 | case Image_Format_RGB32: |
610 | case Image_Format_BGR32: |
611 | case Image_Format_RGBA: |
612 | case Image_Format_BGRA: |
613 | { |
614 | const NCollection_Vec3<unsigned char> aWhite24 (255, 255, 255); |
615 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) |
616 | { |
617 | for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) |
618 | { |
619 | NCollection_Vec3<unsigned char>& aPixel = theImage.ChangeValue< NCollection_Vec3<unsigned char> > (aRow, aCol); |
620 | if (aPixel[0] != 0 |
621 | || aPixel[1] != 0 |
622 | || aPixel[2] != 0) |
623 | { |
624 | aPixel = aWhite24; |
625 | } |
626 | } |
627 | } |
628 | break; |
629 | } |
630 | default: |
631 | { |
632 | const Quantity_ColorRGBA aWhiteRgba (1.0f, 1.0f, 1.0f, 1.0f); |
633 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) |
634 | { |
635 | for (Standard_Size aCol = 0; aCol < theImage.SizeX(); ++aCol) |
636 | { |
637 | const Quantity_ColorRGBA aPixelRgba = theImage.PixelColor (Standard_Integer(aCol), Standard_Integer(aRow)); |
638 | const NCollection_Vec4<float>& aPixel = aPixelRgba; |
639 | if (aPixel[0] != 0.0f |
640 | || aPixel[1] != 0.0f |
641 | || aPixel[2] != 0.0f) |
642 | { |
643 | theImage.SetPixelColor (int(aCol), int(aRow), aWhiteRgba); |
644 | } |
645 | } |
646 | } |
647 | break; |
648 | } |
649 | } |
650 | } |