]> OCCT Git - occt.git/commitdiff
0033731: Visualization - Transparent background for rendered image files CR0-ww-depot-781
authorhossamali <Hossam.Ali@opencascade.com>
Wed, 5 Jun 2024 11:12:45 +0000 (14:12 +0300)
committerhosali <Hossam.Ali@opencascade.com>
Fri, 7 Jun 2024 17:01:48 +0000 (17:01 +0000)
- "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

src/Image/Image_PixMap.cxx
src/Image/Image_PixMap.hxx
src/V3d/V3d_ImageDumpOptions.hxx
src/V3d/V3d_View.cxx
src/V3d/V3d_View.hxx

index c99971aefa6729d495714d9db6b94f105050c8bb..f967d9cb22bf72f229f749834ce2146108dcd00e 100644 (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  :
index b4bef537b0418398dea1dd4adae7d4b1d2fb0795..e9207591b8e69ed5361bd2afe13c90930ca4f3ae 100644 (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);
 
index 368f7c881d639b2e96bb23f7951c1af83f5b58bf..77cfc969f8caa9b2f0fcdd7c76c96d1ceaa6514a 100644 (file)
 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:
 
index 7658ea79842fcff2bbafee0c277ebbb36d22cf72..ae22147685efb8db867614b6b8894ec76933e8d9 100644 (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);
index 22847902e993e4815eb548aa5bd8ae78b1f58868..1e62f03f06fd8e2b7119aece85341ff2d0df730a 100644 (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);
   }