C++ Rgb565Pixel

This commit is contained in:
Olivier Goffart 2023-07-25 14:59:31 +02:00 committed by Olivier Goffart
parent 7893ec3fd4
commit 3137bfe775

View file

@ -278,6 +278,49 @@ private:
PhysicalRegion(cbindgen_private::types::IntRect inner) : inner(inner) { }
};
/// A 16bit pixel that has 5 red bits, 6 green bits and 5 blue bits
struct Rgb565Pixel
{
/// The red component, encoded in 5 bits.
uint16_t r : 5;
/// The green component, encoded in 6 bits.
uint16_t g : 6;
/// The blue component, encoded in 5 bits.
uint16_t b : 5;
/// Default constructor.
constexpr Rgb565Pixel() : r(0), g(0), b(0) { }
/// \brief Constructor that constructs from an Rgb8Pixel.
explicit constexpr Rgb565Pixel(const Rgb8Pixel &pixel)
: r(pixel.r >> 3), g(pixel.g >> 2), b(pixel.b >> 3)
{
}
/// \brief Get the red component as an 8-bit value.
///
/// The bits are shifted so that the result is between 0 and 255.
/// \return The red component as an 8-bit value.
constexpr uint8_t red() const { return (r << 3) | (r >> 2); }
/// \brief Get the green component as an 8-bit value.
///
/// The bits are shifted so that the result is between 0 and 255.
/// \return The green component as an 8-bit value.
constexpr uint8_t green() const { return (g << 2) | (g >> 4); }
/// \brief Get the blue component as an 8-bit value.
///
/// The bits are shifted so that the result is between 0 and 255.
/// \return The blue component as an 8-bit value.
constexpr uint8_t blue() const { return (b << 3) | (b >> 2); }
/// \brief Convert to Rgb8Pixel.
constexpr operator Rgb8Pixel() const { return { red(), green(), blue() }; }
friend bool operator==(const Rgb565Pixel &, const Rgb565Pixel &) = default;
};
/// Slint's software renderer.
///
/// To be used as a template parameter of the WindowAdapter.
@ -322,11 +365,12 @@ public:
///
/// The stride is the amount of pixels between two lines in the buffer.
/// It is must be at least as large as the width of the window.
PhysicalRegion render(const Window &window, std::span<uint16_t> buffer,
PhysicalRegion render(const Window &window, std::span<Rgb565Pixel> buffer,
std::size_t pixel_stride) const
{
auto r = cbindgen_private::slint_software_renderer_render_rgb565(
inner, &window.window_handle().inner, buffer.data(), buffer.size(), pixel_stride);
inner, &window.window_handle().inner, reinterpret_cast<uint16_t *>(buffer.data()),
buffer.size(), pixel_stride);
return PhysicalRegion { r };
}
};