1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-03 17:56:21 +03:00

0033731: Visualization - Transparent background for rendered image files

- "V3d_ImageDumpOptions" extended with flag for removing background
- "image_PixMap" extended with "ColorKeying" to isolate a foreground
    object against a background area of uniform color
This commit is contained in:
hossamali 2024-06-05 14:12:45 +03:00 committed by hosali
parent f55b04c09c
commit 90ae8408e7
5 changed files with 78 additions and 13 deletions

View File

@ -17,6 +17,7 @@
#include <NCollection_AlignedAllocator.hxx>
#include <Standard_ProgramError.hxx>
#include <Message.hxx>
#include <algorithm>
@ -829,6 +830,42 @@ void Image_PixMap::ToBlackWhite (Image_PixMap& theImage)
}
}
// =======================================================================
// 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 :

View File

@ -51,6 +51,9 @@ public:
//! 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);

View File

@ -21,12 +21,13 @@
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:

View File

@ -2941,6 +2941,17 @@ Standard_Boolean V3d_View::ToPixMap (Image_PixMap& theImage,
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);
@ -3012,6 +3023,16 @@ Standard_Boolean V3d_View::ToPixMap (Image_PixMap& theImage,
}
}
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);

View File

@ -845,18 +845,20 @@ public:
//! Dumps the full contents of the view to a pixmap.
//! Internally this method calls Redraw() with an offscreen render buffer of requested target size (theWidth x theHeight),
//! so that there is no need resizing a window control for making a dump of different size.
//! @param theImage target image, will be re-allocated to match theWidth x theHeight
//! @param theWidth target image width
//! @param theHeight target image height
//! @param theBufferType type of the view buffer to dump (color / depth)
//! @param theToAdjustAspect when true, active view aspect ratio will be overridden by (theWidth / theHeight)
//! @param theStereoOptions how to dump stereographic camera
//! @param theImage target image, will be re-allocated to match theWidth x theHeight
//! @param theWidth target image width
//! @param theHeight target image height
//! @param theBufferType type of the view buffer to dump (color / depth)
//! @param theToAdjustAspect when true, active view aspect ratio will be overridden by (theWidth / theHeight)
//! @param theStereoOptions how to dump stereographic camera
//! @param theRemoveBackground flag to remove view background
Standard_Boolean ToPixMap (Image_PixMap& theImage,
const Standard_Integer theWidth,
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;
@ -864,6 +866,7 @@ public:
aParams.BufferType = theBufferType;
aParams.StereoOptions = theStereoOptions;
aParams.ToAdjustAspect = theToAdjustAspect;
aParams.ToRemoveBackground = theRemoveBackground;
return ToPixMap (theImage, aParams);
}