C++: implement creation of image from raw data

Issue #616
This commit is contained in:
Olivier Goffart 2023-03-17 17:20:04 +01:00 committed by Olivier Goffart
parent c1dbe974ef
commit be47c8464c
3 changed files with 48 additions and 6 deletions

View file

@ -11,6 +11,9 @@
namespace slint {
using cbindgen_private::types::Rgb8Pixel;
using cbindgen_private::types::Rgba8Pixel;
/// An image type that can be displayed by the Image element
struct Image
{
@ -25,13 +28,33 @@ public:
return img;
}
/*
static Image load_from_argb(int width, int height, const SharedVector<uint32_t> &data) {
/// Construct an image from a vector of RGB pixel.
/// The size of the vector \a data should be \a width * \a height
static Image from_raw_data(unsigned int width, unsigned int height,
const SharedVector<Rgb8Pixel> &data)
{
Image img;
img.data = Data::EmbeddedRgbaImage(width, height, data);
img.data = Data::ImageInner_EmbeddedImage(
cbindgen_private::types::ImageCacheKey::Invalid(),
cbindgen_private::types::SharedImageBuffer::RGB8(
cbindgen_private::types::SharedPixelBuffer<Rgb8Pixel> {
.width = width, .height = height, .data = data }));
return img;
}
/// Construct an image from a vector of RGBA pixels.
/// The size of the vector \a data should be \a width * \a height
static Image from_raw_data(unsigned int width, unsigned int height,
const SharedVector<Rgba8Pixel> &data)
{
Image img;
img.data = Data::ImageInner_EmbeddedImage(
cbindgen_private::types::ImageCacheKey::Invalid(),
cbindgen_private::types::SharedImageBuffer::RGBA8(
cbindgen_private::types::SharedPixelBuffer<Rgba8Pixel> {
.width = width, .height = height, .data = data }));
return img;
}
*/
/// Returns the size of the Image in pixels.
Size<unsigned int> size() const { return cbindgen_private::types::slint_image_size(&data); }

View file

@ -172,6 +172,23 @@ TEST_CASE("Image")
REQUIRE(actual_path.has_value());
REQUIRE(*actual_path == SOURCE_DIR "/../../../logo/slint-logo-square-light-128x128.png");
}
img = Image::from_raw_data(0, 0, SharedVector<Rgba8Pixel> {});
{
auto size = img.size();
REQUIRE(size.width == 0);
REQUIRE(size.height == 0);
REQUIRE(!img.path().has_value());
}
auto red = Rgb8Pixel { 0xff, 0, 0 };
auto blu = Rgb8Pixel { 0, 0, 0xff };
img = Image::from_raw_data(3, 2, SharedVector<Rgb8Pixel> { red, red, blu, red, blu, blu });
{
auto size = img.size();
REQUIRE(size.width == 3);
REQUIRE(size.height == 2);
REQUIRE(!img.path().has_value());
}
}
TEST_CASE("SharedVector")

View file

@ -670,7 +670,8 @@ pub(crate) mod ffi {
use super::super::IntSize;
use super::*;
/// Expand Rgb8Pixel so that cbindgen can see it. (is in fact rgb::RGB<u8>)
// Expand Rgb8Pixel so that cbindgen can see it. (is in fact rgb::RGB<u8>)
/// Represents a RGB pixel
#[cfg(cbindgen)]
#[repr(C)]
struct Rgb8Pixel {
@ -679,7 +680,8 @@ pub(crate) mod ffi {
b: u8,
}
/// Expand Rgba8Pixel so that cbindgen can see it. (is in fact rgb::RGBA<u8>)
// Expand Rgba8Pixel so that cbindgen can see it. (is in fact rgb::RGBA<u8>)
/// Represents a RGBA pixel
#[cfg(cbindgen)]
#[repr(C)]
struct Rgba8Pixel {