1 // Created on: 2013-06-25
2 // Created by: Dmitry BOBYLEV
3 // Copyright (c) 2013-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 <Graphic3d_MarkerImage.hxx>
18 #include <Image_PixMap.hxx>
19 #include <Standard_Atomic.hxx>
20 #include <TColStd_HArray1OfByte.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_MarkerImage,Standard_Transient)
26 static volatile Standard_Integer THE_MARKER_IMAGE_COUNTER = 0;
30 // =======================================================================
31 // function : Graphic3d_MarkerImage
33 // =======================================================================
34 Graphic3d_MarkerImage::Graphic3d_MarkerImage (const Handle(Image_PixMap)& theImage)
37 myWidth ((Standard_Integer )theImage->Width()),
38 myHeight ((Standard_Integer )theImage->Height())
40 myImageId = TCollection_AsciiString ("Graphic3d_MarkerImage_")
41 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_MARKER_IMAGE_COUNTER));
43 myImageAlphaId = TCollection_AsciiString ("Graphic3d_MarkerImageAlpha_")
44 + TCollection_AsciiString (THE_MARKER_IMAGE_COUNTER);
47 // =======================================================================
48 // function : Graphic3d_MarkerImage
50 // =======================================================================
51 Graphic3d_MarkerImage::Graphic3d_MarkerImage (const Handle(TColStd_HArray1OfByte)& theBitMap,
52 const Standard_Integer& theWidth,
53 const Standard_Integer& theHeight)
54 : myBitMap (theBitMap),
59 myImageId = TCollection_AsciiString ("Graphic3d_MarkerImage_")
60 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_MARKER_IMAGE_COUNTER));
62 myImageAlphaId = TCollection_AsciiString ("Graphic3d_MarkerImageAlpha_")
63 + TCollection_AsciiString (THE_MARKER_IMAGE_COUNTER);
66 // =======================================================================
67 // function : GetBitMapArray
69 // =======================================================================
70 Handle(TColStd_HArray1OfByte) Graphic3d_MarkerImage::GetBitMapArray (const Standard_Real& theAlphaValue) const
72 if (!myBitMap.IsNull())
77 Handle(TColStd_HArray1OfByte) aBitMap;
83 const Standard_Integer aNumOfBytesInRow = (Standard_Integer )(myImage->Width() / 8) + (myImage->Width() % 8 ? 1 : 0);
84 const Standard_Integer aNumOfBytes = (Standard_Integer )(aNumOfBytesInRow * myImage->Height());
85 const Standard_Integer aHeight = (Standard_Integer )myImage->Height();
86 const Standard_Integer aWidth = (Standard_Integer )myImage->Width();
87 aBitMap = new TColStd_HArray1OfByte (0, aNumOfBytes - 1);
89 for (Standard_Integer aRow = 0; aRow < aHeight; aRow++)
91 for (Standard_Integer aColumn = 0; aColumn < aWidth; aColumn++)
93 const Quantity_ColorRGBA aColor = myImage->PixelColor (aColumn, aRow);
94 Standard_Boolean aBitOn = Standard_False;
95 if (myImage->Format() == Image_Format_Gray)
97 aBitOn = aColor.GetRGB().Red() > theAlphaValue;
99 else //if (myImage->Format() == Image_Format_Alpha)
101 aBitOn = aColor.Alpha() > theAlphaValue;
104 Standard_Integer anIndex = aNumOfBytesInRow * aRow + aColumn / 8;
105 aBitMap->SetValue (anIndex, (Standard_Byte)(aBitMap->Value (anIndex) +
106 (aBitOn ? (0x80 >> (aColumn % 8)) : 0)));
113 // =======================================================================
114 // function : GetImage
116 // =======================================================================
117 const Handle(Image_PixMap)& Graphic3d_MarkerImage::GetImage()
119 if (!myImage.IsNull())
124 if (myBitMap.IsNull())
129 // Converting a byte array to bitmap image. Row and column offsets are used
130 // to store bitmap in a square image, so the image will not be stretched
131 // when rendering with point sprites.
132 const Standard_Integer aNumOfBytesInRow = myWidth / 8 + (myWidth % 8 ? 1 : 0);
133 const Standard_Integer aSize = Max (myWidth, myHeight);
134 const Standard_Integer aRowOffset = (aSize - myHeight) / 2 + myMargin;
135 const Standard_Integer aColumnOffset = (aSize - myWidth ) / 2 + myMargin;
136 const Standard_Integer aLowerIndex = myBitMap->Lower();
138 myImage = new Image_PixMap();
139 myImage->InitZero (Image_Format_Alpha, aSize + myMargin * 2, aSize + myMargin * 2);
140 for (Standard_Integer aRowIter = 0; aRowIter < myHeight; aRowIter++)
142 Standard_Byte* anImageRow = myImage->ChangeRow (aRowIter + aRowOffset);
143 for (Standard_Integer aColumnIter = 0; aColumnIter < myWidth; aColumnIter++)
145 Standard_Boolean aBitOn = (myBitMap->Value (aLowerIndex + aNumOfBytesInRow * aRowIter + aColumnIter / 8) & (0x80 >> (aColumnIter % 8))) != 0;
146 anImageRow[aColumnIter + aColumnOffset] = aBitOn ? 255 : 0;
153 // =======================================================================
154 // function : GetImageAlpha
156 // =======================================================================
157 const Handle(Image_PixMap)& Graphic3d_MarkerImage::GetImageAlpha()
159 if (!myImageAlpha.IsNull())
164 if (!myImage.IsNull())
166 if (myImage->Format() == Image_Format_Gray
167 || myImage->Format() == Image_Format_Alpha)
169 myImageAlpha = myImage;
173 myImageAlpha = new Image_PixMap();
174 myImageAlpha->InitZero (Image_Format_Alpha, myImage->Width(), myImage->Height());
175 myImageAlpha->SetTopDown (Standard_False);
176 for (Standard_Size aRowIter = 0; aRowIter < myImage->Height(); aRowIter++)
178 Standard_Byte* anImageRow = myImageAlpha->ChangeRow (aRowIter);
179 for (Standard_Size aColumnIter = 0; aColumnIter < myImage->Width(); aColumnIter++)
181 const Quantity_ColorRGBA aColor = myImage->PixelColor ((Standard_Integer)aColumnIter, (Standard_Integer)aRowIter);
182 anImageRow[aColumnIter] = Standard_Byte (255.0 * aColor.Alpha());
191 // =======================================================================
192 // function : GetImageId
194 // =======================================================================
195 const TCollection_AsciiString& Graphic3d_MarkerImage::GetImageId() const
200 // =======================================================================
201 // function : GetImageAlphaId
203 // =======================================================================
204 const TCollection_AsciiString& Graphic3d_MarkerImage::GetImageAlphaId() const
206 return myImageAlphaId;
209 // =======================================================================
210 // function : GetTextureSize
212 // =======================================================================
213 void Graphic3d_MarkerImage::GetTextureSize (Standard_Integer& theWidth,
214 Standard_Integer& theHeight) const
217 theHeight = myHeight;