Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve QImage support #746

Merged
merged 22 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib-headers/include/gui/qimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ QImage
qimageInitFromData(const rust::Slice<std::uint8_t const> data,
rust::Str format);

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

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

::std::int64_t
qimageCacheKey(const QImage& image)
{
return static_cast<::std::int64_t>(image.cacheKey());
}
}
}

Expand Down
96 changes: 96 additions & 0 deletions crates/cxx-qt-lib/src/gui/qimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,100 @@ mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qimage.h");
type QImage = super::QImage;
include!("cxx-qt-lib/qsize.h");
type QSize = crate::QSize;
include!("cxx-qt-lib/qrect.h");
type QRect = crate::QRect;
include!("cxx-qt-lib/qcolor.h");
type QColor = crate::QColor;
include!("cxx-qt-lib/qpoint.h");
type QPoint = crate::QPoint;

/// Whether the QImage is null.
///
/// This means that the QImage has all parameters set to zero and no allocated data.
#[rust_name = "is_null"]
fn isNull(self: &QImage) -> bool;

/// Returns true if all the colors in the image are shades of gray
#[rust_name = "all_gray"]
fn allGray(self: &QImage) -> bool;
Montel marked this conversation as resolved.
Show resolved Hide resolved

/// For 32-bit images, this function is equivalent to allGray().
/// For color indexed images, this function returns true if color(i) is QRgb(i, i, i)
/// for all indexes of the color table; otherwise returns false.
#[rust_name = "is_gray_scale"]
fn isGrayscale(self: &QImage) -> bool;

/// Returns true if the image has a format that respects the alpha channel, otherwise returns false.
#[rust_name = "has_alpha_channel"]
fn hasAlphaChannel(self: &QImage) -> bool;

/// Returns a sub-area of the image as a new image.
fn copy(self: &QImage, rect: &QRect) -> QImage;

/// Returns the size of the color table for the image.
#[rust_name = "color_count"]
fn colorCount(self: &QImage) -> i32;

/// Resizes the color table to contain colorCount entries.
#[rust_name = "set_color_count"]
fn setColorCount(self: &mut QImage, colorCount: i32);

/// Returns the depth of the image.
fn depth(self: &QImage) -> i32;

/// Fills the entire image with the given color.
fn fill(self: &mut QImage, color: &QColor);

/// Returns the height of the image.
fn height(self: &QImage) -> i32;

/// Returns the width of the image.
fn width(self: &QImage) -> i32;

/// Returns the size of the image.
fn size(self: &QImage) -> QSize;

/// Returns the enclosing rectangle (0, 0, width(), height()) of the image.
fn rect(self: &QImage) -> QRect;

/// Returns the color of the pixel at coordinates (x, y) as a QColor.
#[rust_name = "pixel_color"]
fn pixelColor(self: &QImage, x: i32, y: i32) -> QColor;

/// Sets the pixel color at (x, y) to color.
#[rust_name = "set_pixel_color"]
fn setPixelColor(self: &mut QImage, x: i32, y: i32, color: &QColor);

/// Returns the number of pixels that fit horizontally in a physical meter.
#[rust_name = "dots_per_meterx"]
fn dotsPerMeterX(self: &QImage) -> i32;

/// Returns the number of pixels that fit vertically in a physical meter.
#[rust_name = "dots_per_metery"]
fn dotsPerMeterY(self: &QImage) -> i32;

/// Returns true if pos is a valid coordinate pair within the image.
fn valid(self: &QImage, x: i32, y: i32) -> bool;
Montel marked this conversation as resolved.
Show resolved Hide resolved

/// Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.
fn offset(self: &QImage) -> QPoint;

/// Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset.
#[rust_name = "set_offset"]
fn setOffset(self: &mut QImage, point: &QPoint);

/// Returns the pixel index at (x, y).
#[rust_name = "pixel_index"]
fn pixelIndex(self: &QImage, x: i32, y: i32) -> i32;

/// Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false.
fn mirror(self: &mut QImage, horizontal: bool, vertical: bool);
Montel marked this conversation as resolved.
Show resolved Hide resolved

/// Sets the alpha channel of this image to the given alphaChannel.
#[rust_name = "set_alpha_channel"]
fn setAlphaChannel(self: &mut QImage, alphaChannel: &QImage);
}

#[namespace = "rust::cxxqtlib1"]
Expand All @@ -30,6 +118,10 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qimage_init_from_data"]
fn qimageInitFromData(data: &[u8], format: &str) -> QImage;

#[doc(hidden)]
#[rust_name = "qimage_cache_key"]
fn qimageCacheKey(image: &QImage) -> i64;
}
}

Expand Down Expand Up @@ -79,4 +171,8 @@ impl QImage {
None
}
}
/// Returns a number that identifies the contents of this QImage object.
pub fn cache_key(&self) -> i64 {
ffi::qimage_cache_key(self)
}
}
Loading