Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/cxx-qt-lib/include/gui/qimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ QImage
qimageInitFromData(const rust::Slice<std::uint8_t const> data,
rust::Str format);

QImage
qimageInitFromRawParts(const std::uint8_t* data,
int width,
int height,
QImageFormat format,
QImageCleanupFunction cleanupFunction,
void* cleanupInfo);

QImage
qimageInitFromRawParts(std::uint8_t* data,
int width,
int height,
QImageFormat format,
QImageCleanupFunction cleanupFunction,
void* cleanupInfo);

::std::int64_t
qimageCacheKey(const QImage& image);

Expand Down
32 changes: 32 additions & 0 deletions crates/cxx-qt-lib/src/gui/qimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ qimageInitFromData(const rust::Slice<std::uint8_t const> data, rust::Str format)
formatString.empty() ? nullptr : formatString.data());
}

QImage
qimageInitFromRawParts(const std::uint8_t* data,
int width,
int height,
QImageFormat format,
QImageCleanupFunction cleanupFunction,
void* cleanupInfo)
{
return QImage(static_cast<const unsigned char*>(data),
width,
height,
format,
cleanupFunction,
cleanupInfo);
}

QImage
qimageInitFromRawParts(std::uint8_t* data,
int width,
int height,
QImageFormat format,
QImageCleanupFunction cleanupFunction,
void* cleanupInfo)
{
return QImage(static_cast<unsigned char*>(data),
width,
height,
format,
cleanupFunction,
cleanupInfo);
}

::std::int64_t
qimageCacheKey(const QImage& image)
{
Expand Down
23 changes: 11 additions & 12 deletions crates/cxx-qt-lib/src/gui/qimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ mod ffi {
#[allow(dead_code)]
type QSizeF = crate::QSizeF;
type QImageCleanupFunction = super::QImageCleanupFunction;
type uchar;

/// Returns `true` if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise `false`.
///
Expand Down Expand Up @@ -388,11 +387,11 @@ mod ffi {
}

#[namespace = "rust::cxxqtlib1"]
extern "C++" {
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qimage_init_from_raw_parts_mut"]
unsafe fn construct(
data: *mut uchar,
unsafe fn qimageInitFromRawParts(
data: *mut u8,
width: i32,
height: i32,
format: QImageFormat,
Expand All @@ -402,8 +401,8 @@ mod ffi {

#[doc(hidden)]
#[rust_name = "qimage_init_from_raw_parts"]
unsafe fn construct(
data: *const uchar,
unsafe fn qimageInitFromRawParts(
data: *const u8,
width: i32,
height: i32,
format: QImageFormat,
Expand Down Expand Up @@ -503,7 +502,7 @@ impl QImage {
/// # Safety
/// For details on safety see the [Qt documentation](https://doc.qt.io/qt/qimage.html#QImage-7)
pub unsafe fn from_raw_parts(
data: *const ffi::uchar,
data: *const u8,
width: i32,
height: i32,
format: QImageFormat,
Expand All @@ -525,7 +524,7 @@ impl QImage {
/// # Safety
/// For details on safety see the [Qt documentation](https://doc.qt.io/qt/qimage.html#QImage-8)
pub unsafe fn from_raw_parts_mut(
data: *mut ffi::uchar,
data: *mut u8,
width: i32,
height: i32,
format: QImageFormat,
Expand Down Expand Up @@ -561,12 +560,12 @@ impl QImage {
// In this case the *mut ffi::c_void is actually a `*mut Vec<u8>` that was created by
// Box::into_raw(), so can be re-created by Box::from_raw().
// QImage also guarantees that this is only called once when the last copy is destroyed.
let the_box: Box<Vec<u8>> = unsafe { Box::from_raw(boxed_vec as *mut Vec<u8>) };
let the_box: Box<Vec<u8>> = unsafe { Box::from_raw(boxed_vec.cast()) };
drop(the_box);
}
let data = Box::new(data);
let bytes = data.as_ptr() as *mut ffi::uchar;
let raw_box = Box::into_raw(data) as *mut ffi::c_void;
let mut data = Box::new(data);
let bytes = data.as_mut_ptr();
let raw_box = Box::into_raw(data).cast();
QImage::from_raw_parts_mut(bytes, width, height, format, delete_boxed_vec, raw_box)
}

Expand Down
Loading