- add flag to 3D view dump for removing background based color keying.
- add color keying function to Image_PixMap
#include <NCollection_AlignedAllocator.hxx>
#include <Standard_ProgramError.hxx>
+#include <Message.hxx>
#include <algorithm>
}
}
+// =======================================================================
+// function : ColorKeying
+// purpose :
+// =======================================================================
+void Image_PixMap::ColorKeying(const Quantity_Color& theKey, Image_PixMap& theImage)
+{
+ if (theImage.myImgFormat != Image_Format_RGBA && theImage.myImgFormat != Image_Format_BGRA)
+ {
+ Message::SendWarning("Warning: Image format should support alpha channel");
+ }
+
+ const Standard_Byte aRed = (Standard_Byte) (theKey.Red() * 255);
+ const Standard_Byte aGreen = (Standard_Byte) (theKey.Green() * 255);
+ const Standard_Byte aBlue = (Standard_Byte) (theKey.Blue() * 255);
+
+ if (theImage.myImgFormat == Image_Format_RGBA)
+ {
+ for (Standard_Size i = 0; i < theImage.myData.Size(); i += 4)
+ {
+ if (theImage.myData.ChangeData()[i] == aRed && theImage.myData.ChangeData()[i + 1] == aGreen && theImage.myData.ChangeData()[i + 2] == aBlue)
+ {
+ theImage.myData.ChangeData()[i + 3] = 0;
+ }
+ }
+ }
+ if (theImage.myImgFormat == Image_Format_BGRA)
+ {
+ for (Standard_Size i = 0; i < theImage.myData.Size(); i += 4)
+ {
+ if (theImage.myData.ChangeData()[i] == aBlue && theImage.myData.ChangeData()[i + 1] == aGreen && theImage.myData.ChangeData()[i + 2] == aRed)
+ {
+ theImage.myData.ChangeData()[i + 3] = 0;
+ }
+ }
+ }
+}
// =======================================================================
// function : FlipY
// purpose :
//! Convert image to Black/White.
Standard_EXPORT static void ToBlackWhite (Image_PixMap& theImage);
+ //! Isolating a foreground object against a background area of uniform color
+ Standard_EXPORT static void ColorKeying(const Quantity_Color& theKey, Image_PixMap& theImage);
+
//! Reverse line order as it draws it from bottom to top.
Standard_EXPORT static bool FlipY (Image_PixMap& theImage);
struct V3d_ImageDumpOptions
{
- Standard_Integer Width; //!< width of image dump to allocate an image, 0 by default (meaning that image should be already allocated)
- Standard_Integer Height; //!< height of image dump to allocate an image, 0 by default (meaning that image should be already allocated)
- Graphic3d_BufferType BufferType; //!< which buffer to dump (color / depth), Graphic3d_BT_RGB by default
- V3d_StereoDumpOptions StereoOptions; //!< dumping stereoscopic camera, V3d_SDO_MONO by default (middle-point monographic projection)
- Standard_Integer TileSize; //!< the view dimension limited for tiled dump, 0 by default (automatic tiling depending on hardware capabilities)
- Standard_Boolean ToAdjustAspect; //!< flag to override active view aspect ratio by (Width / Height) defined for image dump (TRUE by default)
+ Standard_Integer Width; //!< width of image dump to allocate an image, 0 by default (meaning that image should be already allocated)
+ Standard_Integer Height; //!< height of image dump to allocate an image, 0 by default (meaning that image should be already allocated)
+ Graphic3d_BufferType BufferType; //!< which buffer to dump (color / depth), Graphic3d_BT_RGB by default
+ V3d_StereoDumpOptions StereoOptions; //!< dumping stereoscopic camera, V3d_SDO_MONO by default (middle-point monographic projection)
+ Standard_Integer TileSize; //!< the view dimension limited for tiled dump, 0 by default (automatic tiling depending on hardware capabilities)
+ Standard_Boolean ToAdjustAspect; //!< flag to override active view aspect ratio by (Width / Height) defined for image dump (TRUE by default)
+ Standard_Boolean ToRemoveBackground; //!< flag to override background with chroma key background
public:
aCamera->SetAspect (Standard_Real(aTargetSize.x()) / Standard_Real(aTargetSize.y()));
}
+ // backup background
+ Aspect_Background theBackground = myView->Background();
+ Quantity_Color aChroma = Quantity_NOC_MAGENTA;
+ // backup antialiasing settings
+ Standard_Boolean aIsAntialiasingEnabled = myView->RenderingParams().IsAntialiasingEnabled;
+ if (theParams.ToRemoveBackground)
+ {
+ myView->SetBackground(Aspect_Background(aChroma));
+ myView->ChangeRenderingParams().IsAntialiasingEnabled = Standard_False;
+ }
+
// render immediate structures into back buffer rather than front
const Standard_Boolean aPrevImmediateMode = myView->SetImmediateModeDrawToFront (Standard_False);
}
}
+ if(theParams.ToRemoveBackground)
+ {
+ // color keying
+ Image_PixMap::ColorKeying(aChroma, theImage);
+ // restore original background
+ myView->SetBackground(theBackground);
+ // restore Antialiasing settings
+ myView->ChangeRenderingParams().IsAntialiasingEnabled = aIsAntialiasingEnabled;
+ }
+
// restore state
myView->SetImmediateModeDrawToFront (aPrevImmediateMode);
aCamera->Copy (aStoreMapping);
const Standard_Integer theHeight,
const Graphic3d_BufferType& theBufferType = Graphic3d_BT_RGB,
const Standard_Boolean theToAdjustAspect = Standard_True,
- const V3d_StereoDumpOptions theStereoOptions = V3d_SDO_MONO)
+ const V3d_StereoDumpOptions theStereoOptions = V3d_SDO_MONO,
+ const Standard_Boolean theRemoveBackground = Standard_False)
{
V3d_ImageDumpOptions aParams;
aParams.Width = theWidth;
aParams.BufferType = theBufferType;
aParams.StereoOptions = theStereoOptions;
aParams.ToAdjustAspect = theToAdjustAspect;
+ aParams.ToRemoveBackground = theRemoveBackground;
return ToPixMap (theImage, aParams);
}