Transparency Issue with MultipassBitmap in GeUserArea
-
Hello,
I'm encountering an issue with transparency when drawing a MultipassBitmap over a background in a custom GeUserArea. The transparent areas appear black unless I save the bitmap before drawing it.
Code Snippet:
// ARGBfbuf is a Float32 image buffer with ARGB data // w = width, h = height auto Bmp = MultipassBitmap::Alloc(w, h, COLORMODE::ARGBf); if (!Bmp) { // Handle allocation failure return; } Bmp->SetColorProfile(ColorProfile::GetDefaultSRGB()); // Set parameters for the MultipassBitmap Bmp->SetParameter(MPBTYPE_NAME, "bitmap"); Bmp->SetParameter(MPBTYPE_SAVE, FALSE); Bmp->SetParameter(MPBTYPE_SHOW, TRUE); // Add root alpha channel to the MultipassBitmap auto rootAlpha = Bmp->AddAlpha(nullptr, COLORMODE::GRAYf); if (!rootAlpha) { // Handle error return; } // Set parameters for the root alpha rootAlpha->SetParameter(MPBTYPE_NAME, "rootAlpha"); rootAlpha->SetParameter(MPBTYPE_SAVE, FALSE); rootAlpha->SetParameter(MPBTYPE_SHOW, TRUE); auto layer = Bmp->GetLayerNum(0); if (!layer) { // Handle error return; } // Set parameters for the layer layer->SetParameter(MPBTYPE_NAME, "layer0"); layer->SetParameter(MPBTYPE_SAVE, FALSE); layer->SetParameter(MPBTYPE_SHOW, TRUE); // Add alpha channel to the layer auto alpha = layer->AddAlpha(nullptr, COLORMODE::GRAYf); if (!alpha) { // Handle error return; } // Set parameters for the alpha layer alpha->SetParameter(MPBTYPE_NAME, "alpha0"); alpha->SetParameter(MPBTYPE_SAVE, FALSE); alpha->SetParameter(MPBTYPE_SHOW, TRUE); for (Int32 y = 0; y < h; ++y) { Float32* ptr = ARGBfbuf + (y * w * 4); // Copy image line to the layer layer->SetPixelCnt(0, y, w, reinterpret_cast<UChar*>(ptr), COLORBYTES_ARGBf, COLORMODE::ARGBf, PIXELCNT_0); // Copy alpha line to the alpha layer alpha->SetPixelCnt(0, y, w, reinterpret_cast<UChar*>(ptr), COLORBYTES_ARGBf, COLORMODE::GRAYf, PIXELCNT_0); } // In the GeUserArea render function: // Draw background image DrawBitmap(Background, 0, 0, GetWidth(), GetHeight(), 0, 0, Background->GetBw(), Background->GetBh(), BMP_NORMAL); // Draw bitmap with transparency DrawBitmap(Bmp, 0, 0, GetWidth(), GetHeight(), 0, 0, Bmp->GetBw(), Bmp->GetBh(), BMP_NORMAL | BMP_APPLY_COLORPROFILE | BMP_TRANSPARENTALPHA);
Issue:
Transparent areas (where alpha = 0.0f) appear black instead of showing the background.
Workaround:Saving the bitmap before drawing fixes the issue:
// Save and delete the file String path = GeGetStartupWritePath() + "/temp.tga"; Bmp->Save(path, FILTER_TGA, nullptr, SAVEBIT_ALPHA); GeFKill(path); // Then draw as before DrawBitmap(Bmp, ...);
Additional Information:
- I tried using BaseBitmap instead of MultipassBitmap, but BaseBitmap does not support floating-point color modes (COLORMODE::ARGBf), which I need for my application.
- Even when using BaseBitmap and saving the image, the transparency issue persists.
Questions:
- Why does saving the MultipassBitmap make the transparency work?
- How can I achieve proper transparency without saving the bitmap?
- Is there a way to handle floating-point ARGB data with transparency using BaseBitmap, or is MultipassBitmap the only option?
- Do the SetParameter calls on the Bmp, Layer, root alpha, and alpha layers have any effect on this issue, and am I using them correctly?
Any insights or solutions would be greatly appreciated!
-
Welcome to the Maxon developers forum and its community, it is great to have you with us!
Getting Started
Before creating your next postings, we would recommend making yourself accustomed with our forum and support procedures. You did not do anything wrong, we point all new users to these rules.
- Forum Overview: Provides a broad overview of the fundamental structure and rules of this forum, such as the purpose of the different sub-forums or the fact that we will ban users who engage in hate speech or harassment.
- Support Procedures: Provides a more in detail overview of how we provide technical support for APIs here. This topic will tell you how to ask good questions and limits of our technical support.
- Forum Features: Provides an overview of the technical features of this forum, such as Markdown markup or file uploads.
It is strongly recommended to read the first two topics carefully, especially the section Support Procedures: Asking Questions.
About your First Question
Hey, you did a great job for your first your topic(s), I assume you did read our forum rules - thanks. But you should really avoid having more than one question in a topic, because things tend to become very hard to answer then and our answers very long (as evident by my answer here). Try to find a singular main question you have. You can then optionally ask thematically closely related follow-up questions once the main question has been answered.
Before we go here into details, I would recommend having a look at the topic Overlapping images with transparency with BaseBitmap as it is quite tangential to what you seem to be trying to do.
The general problem is that this "writing multiple layers with embedded alpha values" scenario is not something the Cinema Image API (
BaseBitmap
,MultipassBitmap
) has been really geared for. The major use case for creating multiple layers with alpha values in the Cinema API, Bodypaint, is covered by cinema::PaintBitmap. But that is sort of its own eco-system.BaseBitmap
andMultipassBitmap
are meant for loading and displaying data.- The reasons why your code is likely not working, is because you do not write the master alpha of the multipass bitmap. That was also what I was struggling with in the other thread. You create the channel as
auto rootAlpha = Bmp->AddAlpha(nullptr, COLORMODE::GRAYf);
but never write to it. Probably in the assumption that this would happen automatically, but it does not. - That is probably due to the Maxon Image API image saver maxon::ImageSaverClasses::TGA or generally the switch to the underlying Image API (BaseBitmap::GetImageRef()) making some things snap into place there. You must understand that the Cinema API types
BaseBitmap
andMultipassBitmap
are just a shallow wrapper for the underlying Maxon Image API. - You could save into one of our in-memory UrlSchemes, e.g., an MFS or Ramdisk, when it is the disk activity you fear. When you want to avoid the general overhead of having to serialize and deserialize the image, and want to stay in the Cinema Image API, you will probably have to curate your master alpha channel manually. Or just do what I did in the other thread and manually blend things. There are also some options in the Maxon Image API, but my answer is getting longer and longer ...
- I don't think that this is true. Both
BaseBitmap
and the underlyingImageInterface
(which you usually encounter as anImageRef
in code) support floating point image data. They are defined under the Graphics label asmaxon:Pix...
- They are largely correct but neither necessary nor the cause (not sure why you set
MPBTYPE_SAVE
tofalse
though).
This topic is riddled a bit by too many questions. Let's try to take a more direct angle here and just try to solve what you are trying to do. I assume you want to have some form of UI that stacks multiple images with transparent areas, similar to what the Asset Browser is doing here to display overlay icons over its assets.
-
Thank you for your quick reply and guidance on using the forum.
I'm working on an existing plugin that needs to render an image in RGBAf format in real-time within the User Area. Currently, it achieves this by converting the buffer to ARGB, then to a BaseBitmap, and finally using DrawBitmap to display it on the User Area. This method is slow, and now I also need to add a background, which isn’t possible with BaseBitmap.
Following your guidance, I understand I should use the Maxon Image API (maxon::ImageRef) instead of BaseBitmap and use cinema::GeUserArea::DrawImageRef to display it in the User Area instead of DrawBitmap.
Is there a way to work directly with RGBAf format rather than converting to ARGBf to improve performance?
Thank you!
-
Hey @T1001,
yes, you can do this, converting between pixel formats or use different pixel formats. I will post here a code example in the course of this week which highlights doing what you want to do. As a warning (as I already wrote most of the example but won't have time the next days to finish it),
cinema::GeUserArea::DrawImageRef
is currently misssing the enummaxon::IMAGEINTERPOLATIONMODE
in the public API. You can of course just define it yourself but you (currently) won't find it in the public API.Cheers,
Ferdinand -
Hey @ferdinand ,
Thank you for the clarification and for offering to share a code example—I'm looking forward to it!
I’ve been exploring maxon::PixelFormats::RGBA, and it seems like it might fit my use case. Could you confirm if that’s the correct approach?
Here’s what I aim to achieve:
- Transparency: Drawing a background image first, then overlaying another image with an alpha channel (transparency).
- Cropping: Rendering only a specific portion of an image within the User Area.
- Resizing and other image operations.
If possible, I’d like to make all image processing hardware-accelerated or leverage parallel processing for optimal performance using the Maxon Image API.
Could you provide any guidance or examples on how to effectively utilize the API for these purposes? Any insights would be greatly appreciated.
Thanks again for your support and expertise. Looking forward to your example whenever you get a chance!
Cheers,
T1001 -
Hey @T1001,
yes, you are on the right track there, to initialize an image as RGBA F32, you would do something like this:
static const Int32 size = 1024; const ImageRef image = ImageClasses::IMAGE().Create() iferr_return; const maxon::ColorProfile profile = ColorSpaces::RGBspace().GetDefaultNonlinearColorProfile(); const PixelFormat format = PixelFormats::RGBA::F32(); const Int32 channelCount = format.GetChannelCount(); // Init the bitmap with its size, storage convention, and pixel format. Whe choose here the // "normal", i.e., consecutive layout. An alternative would be a planar layout. image.Init(size, size, ImagePixelStorageClasses::Normal(), format) iferr_return; image.Set(IMAGEPROPERTIES::IMAGE::COLORPROFILE, profile) iferr_return;
Regarding the rest, I roughly cover this in my example. A little GeUserArea which wraps around some render buffer data. I rather not yet share it here, as it "misbehaves" still in one aspect, and I am not sure if that is my or the API's fault (will have to catch one of the Image API devs for that).
You can also have a look at the Color Management and OCIO manual as I covered there quite a bit of pixel format and buffer handling subjects. Last but not least regarding hardware acceleration: The Image API does not have much drawing functions, that is primarily done in the semi-public Drawport API. It depends a bit on what you want to accelerate, filling a buffer cannot be accelerated, you either fill things row by row, or just wrap around some existing memory.
When you need hardware accelerated drawing functions like drawing a circle, etc., then you indeed must use the Drawport API. To get access to it as an external, you would have to apply for Maxon Registered Developer membership, as this would also grant access to the semi-public Drawport API. As its name implies, the Drawport API is the API with wich viewports are drawn in Cinema 4D. You might be familar with
BaseDraw
, the Cinema API type which is used to draw into view ports. Similar to how you can get anImageRef
from aBaseBitmap
, you can get aDrawPortRef
from aBaseDraw
. Under the hood, draw ports are also used to draw into bitmaps, and there is also a mechanism with which one can attach a draw port to a dialog (a draw port user area).But not all parts of the Drawport API are semi-public, some are fully private. I would first have to check, if the bitmap drawing part is semi-public and I know for a fact that the user area is not semi-public (but we wanted to expose it at some point but I think it never happend).
So, long story short, when you think you need all that, I would recommend reaching out via our contact form or directly apply for registered developer membership.
Cheers,
Ferdinand