-
Notifications
You must be signed in to change notification settings - Fork 74
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
QByteArrayCursor #741
Draft
LeonMatthesKDAB
wants to merge
2
commits into
KDAB:main
Choose a base branch
from
LeonMatthesKDAB:qbytearraycursor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
QByteArrayCursor #741
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Leon Matthes <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#ifdef CXX_QT_GUI_FEATURE | ||
|
||
#include <QtGui/QImage> | ||
|
||
#include "rust/cxx.h" | ||
|
||
#include <cstdint> | ||
|
||
namespace rust { | ||
|
||
// QImage has a move constructor, however it is basically trivial. | ||
template<> | ||
struct IsRelocatable<QImage> : ::std::true_type | ||
{ | ||
}; | ||
|
||
namespace cxxqtlib1 { | ||
using QImageFormat = QImage::Format; | ||
|
||
QImage | ||
qimageInitFromData(const rust::Slice<std::uint8_t const> data, | ||
rust::Str format); | ||
|
||
} // namespace cxxqtlib1 | ||
} // namespace rust | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ pub use qvector3d::QVector3D; | |
|
||
mod qvector4d; | ||
pub use qvector4d::QVector4D; | ||
|
||
mod qimage; | ||
pub use qimage::QImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Leon Matthes <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
#ifdef CXX_QT_GUI_FEATURE | ||
|
||
#include "cxx-qt-lib/qimage.h" | ||
#include "../assertion_utils.h" | ||
#include <string> | ||
|
||
// A QImage inherits from QPaintDevice. | ||
|
||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) | ||
// QPaintDevice in Qt5 contains two things: | ||
// 1. ushort painters; (due to the following field this has padding to make it | ||
// 64-bit long) | ||
// 2. QPaintDevicePrivate *reserved; | ||
// Then QImage adds an additional field: | ||
// 3. QImageData *d; | ||
// For a total of 3 pointers in length. | ||
// Because of the added v-table, it's a total of 4 pointers in size. | ||
assert_alignment_and_size(QImage, | ||
alignof(::std::size_t), | ||
sizeof(::std::size_t) * 4); | ||
#else | ||
// In Qt6 the QPaintDevice doesn't contain the `reserved` pointer, making it 1 | ||
// pointer smaller | ||
assert_alignment_and_size(QImage, | ||
alignof(::std::size_t), | ||
sizeof(::std::size_t) * 3); | ||
#endif | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
QImage | ||
qimageInitFromData(const rust::Slice<std::uint8_t const> data, rust::Str format) | ||
{ | ||
std::string formatString(format); | ||
return QImage::fromData(static_cast<const unsigned char*>(data.data()), | ||
static_cast<int>(data.size()), | ||
formatString.empty() ? nullptr : formatString.data()); | ||
} | ||
|
||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Leon Matthes <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use cxx::{type_id, ExternType}; | ||
use std::mem::MaybeUninit; | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qimage.h"); | ||
type QImage = super::QImage; | ||
|
||
/// 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; | ||
} | ||
|
||
#[namespace = "rust::cxxqtlib1"] | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/common.h"); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qimage_drop"] | ||
fn drop(image: &mut QImage); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qimage_init_from_data"] | ||
fn qimageInitFromData(data: &[u8], format: &str) -> QImage; | ||
} | ||
} | ||
|
||
/// > ⚠ **Warning**: The QImage API in CXX-Qt-lib is not yet complete and subject to change. | ||
/// | ||
/// This struct is the Rust representation of the [`QImage`](https://doc.qt.io/qt-6/qimage.html) | ||
/// class. | ||
/// | ||
/// It provides a way to store and manipulate images in a hardware-independent manner. | ||
#[repr(C)] | ||
pub struct QImage { | ||
// Static checks on the C++ side ensure this is true. | ||
// See qcolor.cpp | ||
#[cfg(qt_version_major = "5")] | ||
_data: MaybeUninit<[usize; 4]>, | ||
#[cfg(qt_version_major = "6")] | ||
_data: MaybeUninit<[usize; 3]>, | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ side to ensure the size & alignment is the same. | ||
unsafe impl ExternType for QImage { | ||
type Id = type_id!("QImage"); | ||
type Kind = cxx::kind::Trivial; | ||
} | ||
|
||
impl Drop for QImage { | ||
fn drop(&mut self) { | ||
ffi::qimage_drop(self); | ||
} | ||
} | ||
|
||
impl QImage { | ||
/// Convert raw image data to a [`QImage`]. | ||
/// | ||
/// The data must be in the given `format`. | ||
/// See [`QImageReader::supportedImageFormats()`](https://doc.qt.io/qt-6/qimagereader.html#supportedImageFormats) for the list of supported formats. | ||
/// | ||
/// If no `format` is provided, the format will be quessed from the image header. | ||
pub fn from_data(data: &[u8], format: Option<&str>) -> Option<Self> { | ||
let image = ffi::qimage_init_from_data(data, format.unwrap_or("")); | ||
|
||
if !image.is_null() { | ||
Some(image) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this doc go on the impl or on the fn ? does it work in a cargo doc ?