pub struct Image {
pub raw: *mut NvdImage,
/* private fields */
}Expand description
A simple image loaded from NvDialog. This is the equivalent to NvdImage, although the implementation
makes a few extra safety checks from the Rust side.
Fields§
§raw: *mut NvdImageImplementations§
Source§impl Image
impl Image
Sourcepub fn from_filename(filename: PathBuf) -> Result<Self, ImageError>
pub fn from_filename(filename: PathBuf) -> Result<Self, ImageError>
Creates an Image from a specified filename.
This function attempts to load an image from the given file path using NvDialog’s image loading facilities. It performs a few checks to ensure robustness and safety from the Rust side.
§Arguments
filename- APathBufspecifying the file path of the image to be loaded.
§Returns
Result<Self, ImageError>- Returns anOkvariant containing theImageif successful, or anImageErrorif an error occurs during the process.
§Errors
ImageError::PathNotFound- Returned if the file path does not exist.ImageError::UnknownError- Returned if the image cannot be created from the data.
§Safety
This function makes use of unsafe blocks to interface with the C functions
from NvDialog. It is assumed that nvd_image_from_filename and
nvd_create_image are safe to call with the provided arguments.
Examples found in repository?
4fn main() {
5 nvdialog_rs::init().expect("failed to initialize libnvdialog");
6 let extensions = ["png", "jpeg", "jpg", "ico", "webp"];
7 let filedlg = FileDialog::new(
8 "Choose the icon for the dialog",
9 FileDialogType::OpenFile,
10 Some(extensions),
11 );
12 let filename = filedlg.retrieve_filename().unwrap_or_else(|| {
13 eprintln!("No filename given!");
14 abort()
15 });
16 let img = Image::from_filename(filename).unwrap_or_else(|e| {
17 eprintln!("Failed to read image! {}", e.to_string());
18 abort();
19 });
20
21 let dialog = AboutDialog::new()
22 .name("NvDialog Example")
23 .description(
24 "An example of using nvdialog-rs and NvdAboutDialog, with a custom user-picked icon",
25 )
26 .icon(img)
27 .build();
28
29 dialog.show();
30}Sourcepub unsafe fn from_data(
data: &[u8],
width: u32,
height: u32,
) -> Result<Self, ImageError>
pub unsafe fn from_data( data: &[u8], width: u32, height: u32, ) -> Result<Self, ImageError>
Creates an Image from the raw data of an image file.
This function creates an Image from raw data that is assumed to be in a format
that NvDialog can read. The data is sent to the NvDialog library without any
checking on the Rust side, so it is left up to the user to ensure that the data
is valid.
§Safety
This function is marked unsafe because the data passed to it is not checked
in any way. If the data is not valid, it may cause undefined behavior when
passed to the C library.
§Arguments
data- The raw image data. This is assumed to be the contents of an image file and should be in a format that NvDialog can read.width- The width of the image in pixels.height- The height of the image in pixels.
§Returns
Result<Self, ImageError>- Returns anOkvariant containing theImageif successful, or anImageErrorif an error occurs.
§Errors
ImageError::InvalidFormat- Returned if the data is not in a valid format for NvDialog.ImageError::UnknownError- Returned if the image cannot be created from the data for some other reason.