1 // Created on: 2010-07-18
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2010-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Image_PixMap.hxx>
17 #include <NCollection_AlignedAllocator.hxx>
19 IMPLEMENT_STANDARD_HANDLE (Image_PixMap, Standard_Transient)
20 IMPLEMENT_STANDARD_RTTIEXT(Image_PixMap, Standard_Transient)
22 // =======================================================================
23 // function : Image_PixMap
25 // =======================================================================
26 Image_PixMap::Image_PixMap()
27 : myImgFormat (Image_PixMap::ImgGray)
32 // =======================================================================
33 // function : ~Image_PixMap
35 // =======================================================================
36 Image_PixMap::~Image_PixMap()
41 Standard_Size Image_PixMap::SizePixelBytes (const Image_PixMap::ImgFormat thePixelFormat)
43 switch (thePixelFormat)
49 return sizeof(float) * 4;
52 return sizeof(float) * 3;
68 // =======================================================================
69 // function : setFormat
71 // =======================================================================
72 void Image_PixMap::setFormat (Image_PixMap::ImgFormat thePixelFormat)
74 myImgFormat = thePixelFormat;
77 // =======================================================================
78 // function : InitWrapper
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)
88 myImgFormat = thePixelFormat;
89 if ((theSizeX == 0) || (theSizeY == 0) || (theDataPtr == NULL))
94 Handle(NCollection_BaseAllocator) anEmptyAlloc;
95 myData.Init (anEmptyAlloc, Image_PixMap::SizePixelBytes (thePixelFormat),
96 theSizeX, theSizeY, theSizeRowBytes, theDataPtr);
100 // =======================================================================
101 // function : InitTrash
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)
110 myImgFormat = thePixelFormat;
111 if ((theSizeX == 0) || (theSizeY == 0))
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();
124 // =======================================================================
125 // function : InitZero
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)
134 if (!InitTrash (thePixelFormat, theSizeX, theSizeY, theSizeRowBytes))
138 memset (myData.ChangeData(), (int )theValue, SizeBytes());
142 // =======================================================================
143 // function : InitCopy
145 // =======================================================================
146 bool Image_PixMap::InitCopy (const Image_PixMap& theCopy)
148 if (&theCopy == this)
150 // self-copying disallowed
153 if (InitTrash (theCopy.myImgFormat, theCopy.SizeX(), theCopy.SizeY(), theCopy.SizeRowBytes()))
155 memcpy (myData.ChangeData(), theCopy.myData.Data(), theCopy.SizeBytes());
161 // =======================================================================
164 // =======================================================================
165 void Image_PixMap::Clear()
167 Handle(NCollection_BaseAllocator) anEmptyAlloc;
168 myData.Init (anEmptyAlloc, Image_PixMap::SizePixelBytes (myImgFormat),
172 // =======================================================================
173 // function : PixelColor
175 // =======================================================================
176 Quantity_Color Image_PixMap::PixelColor (const Standard_Integer theX,
177 const Standard_Integer theY,
178 Quantity_Parameter& theAlpha) const
181 || theX < 0 || (Standard_Size )theX >= SizeX()
182 || theY < 0 || (Standard_Size )theY >= SizeY())
184 theAlpha = 0.0; // transparent
185 return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB);
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)),
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()),
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()),
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()),
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()),
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),
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),
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),
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),
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),
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),
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),
300 // not supported image type
301 theAlpha = 0.0; // transparent
302 return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB);