CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
image_conversion.cpp
Go to the documentation of this file.
1
9#include <OpenImageIO/imagebuf.h>
10#include <OpenImageIO/imagebufalgo.h>
11#include <spdlog/spdlog.h>
12
14
15std::unique_ptr<Common::ImageRegion> convert_RGBA_F32_to_RGBA_U8(const Common::ImageRegion& input)
16{
17 if (!input.isValid() || input.m_channels != 4 || input.m_format != Common::PixelFormat::RGBA_F32) {
18 spdlog::error("convert_RGBA_F32_to_RGBA_U8: Input is invalid or not RGBA_F32");
19 return nullptr;
20 }
21
22 // 1. Setup OIIO Source Buffer (Zero-Copy)
23 OIIO::ImageSpec src_spec(input.m_width, input.m_height, input.m_channels, OIIO::TypeDesc::FLOAT);
24 OIIO::ImageBuf src_buf(src_spec, const_cast<float*>(input.m_data.data()));
25
26 // 2. Setup OIIO Destination Spec
27 OIIO::ImageSpec dst_spec(input.m_width, input.m_height, input.m_channels, OIIO::TypeDesc::UINT8);
28 OIIO::ImageBuf dst_buf(dst_spec);
29
30 // 3. Perform Conversion (F32 -> U8) using OIIO
31 if (!OIIO::ImageBufAlgo::copy(dst_buf, src_buf)) {
32 spdlog::error("convert_RGBA_F32_to_RGBA_U8: OIIO conversion failed");
33 return nullptr;
34 }
35
36 // 4. Extract U8 data to ImageRegion
37 auto result { std::make_unique<Common::ImageRegion>() };
38 result->m_x = input.x();
39 result->m_y = input.y();
40 result->m_width = input.width();
41 result->m_height = input.height();
42 result->m_channels = input.channels();
43
44 const size_t total_elements { static_cast<size_t>(input.width()) * input.height() * input.channels() };
45 result->m_data.resize(total_elements);
46
47 if (!dst_buf.get_pixels(OIIO::ROI::All(), OIIO::TypeDesc::UINT8, result->m_data.data())) {
48 spdlog::error("convert_RGBA_F32_to_RGBA_U8: Failed to extract U8 pixels from OIIO buffer");
49 return nullptr;
50 }
51
52 return result;
53}
54
55} // namespace CaptureMoment::Core::Utils
Declarations of image conversion utilities.
@ RGBA_F32
4-channel format: Red, Green, Blue, Alpha in 32-bit float.
std::unique_ptr< Common::ImageRegion > convert_RGBA_F32_to_RGBA_U8(const Common::ImageRegion &input)
Converts an RGBA_F32 ImageRegion to a new RGBA_U8 ImageRegion.
Represents a rectangular region of an image and its pixel data.
constexpr ImageCoord x() const noexcept
constexpr ImageDim height() const noexcept
constexpr ImageChan channels() const noexcept
constexpr ImageCoord y() const noexcept
ImageDim m_width
Width of this image region in pixels.
ImageDim m_height
Height of this image region in pixels.
constexpr ImageDim width() const noexcept
std::vector< float > m_data
Pixel data (row-major layout).
PixelFormat m_format
Format specifying how pixels are stored.
constexpr bool isValid() const noexcept
Validates the integrity of the ImageRegion (Overflow-Safe Version).
ImageChan m_channels
Number of color channels per pixel.
CaptureMoment — footer