Converting image bit depth
-
Hi Folks,
I need to transfer some images into different bit depths (mainly 8 and 32-bits). I see inside Cinema4D there's a few commands for these, such as ID 170657 and ID 170659.
Is it possible to use these in code, for example, to convert an 8-bit to 32-bit? If so, how?
WP.
-
Hello @WickedP,
Thank you for reaching out to us. First, the classic API image saver frontend, the
BitmapSaverplugins, provide little granular control these days, and unlike for the scene loaders and savers, you often cannot control things like bit depths and format options by setting values in the settings container of the saver.For anything but the simplest bitmap IO operations, you must use the Image API. Your question is also slightly ambiguous, as you do not specify if you want to do this conversion in memory only or if the output should be written to disk. Let me unpack things in bullet points. Color management and pixel formats are also unfortunately not a trivial subject, so there is quite a bit of ground to cover.
- BaseBitmap::Save can save image data to disk but does not offer you any more granularity than the bitmap savers themselves. You can of course reconstruct an image pixel by pixel with a new bit depth, but I would really recommend using the Image API instead.
- BaseBitmap::GetImageRef: Returns the underlying Image API
ImageInterfacerepresentation of aBaseBitmap. TheImageInterfaceis the actual entity which represents bitmap data these days in Cinema 4D andBaseBitmapis just a hollow wrapper around it. - ImageInterface Manual: Showcases the basics of the Image API, including saving an image to disk. Note that you must insert your
ImageRefunder aImageTextureRefas shown in the example, as onlyImageTextureInterfacehas theSavemethod. - Color Management Manual: Demonstrates pixel format and color space conversions of image data using the Image API. The Color Convert Bitmap Data example should be quite close to what you want to do. Your "bit depth" task is covered by the pixel format one is choosing in this example. So, when you pick
RGB::U8as an input andRGBA::F32as an output format, you would convert data in the8bit uint RGBformat to a32bit float RGBAformat. - When you just want to convert image buffers without writing them to disk, things are mostly the same, but you should then look at the Color Convert Singular Pixels and Color Convert Chunks of Pixel Data examples instead.
Cheers,
Ferdinand -
Thanks @ferdinand,
Conversion is in memory only. I may output at a later stage, though I do save using BaseBitmap->Save() for those times.
This episode turned out to be quite more than I bargained for in the end. Had to make some serious project changes. But I do believe I've now solved my image problems (albeit, for now!).
WP.
-
Hey @WickedP,
Good to hear that you solved your problem. And I know that the color management and pixel format stuff can be a royal pain in the ***, you have my sympathies. Things tend to also get REALLY lovely when you also must deal with OCIO.
Please do not hesitate to reach out again when you run in further troubles.
Cheers,
ferdinand