gdk4/auto/texture.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5#[cfg(feature = "v4_16")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
7use crate::ColorState;
8#[cfg(feature = "v4_10")]
9#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
10use crate::MemoryFormat;
11use crate::{ffi, Paintable};
12use glib::{prelude::*, translate::*};
13
14glib::wrapper! {
15 /// Refers to pixel data in various forms.
16 ///
17 /// It is primarily meant for pixel data that will not change over
18 /// multiple frames, and will be used for a long time.
19 ///
20 /// There are various ways to create [`Texture`][crate::Texture] objects from a
21 /// [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf], or from bytes stored in memory, a file, or a
22 /// `Gio::Resource`.
23 ///
24 /// The ownership of the pixel data is transferred to the [`Texture`][crate::Texture]
25 /// instance; you can only make a copy of it, via [`TextureExtManual::download()`][crate::prelude::TextureExtManual::download()].
26 ///
27 /// [`Texture`][crate::Texture] is an immutable object: That means you cannot change
28 /// anything about it other than increasing the reference count via
29 /// `GObject::Object::ref()`, and consequently, it is a threadsafe object.
30 ///
31 /// GDK provides a number of threadsafe texture loading functions:
32 /// [`from_resource()`][Self::from_resource()],
33 /// [`from_bytes()`][Self::from_bytes()],
34 /// [`from_file()`][Self::from_file()],
35 /// [`from_filename()`][Self::from_filename()],
36 /// [`for_pixbuf()`][Self::for_pixbuf()]. Note that these are meant for loading
37 /// icons and resources that are shipped with the toolkit or application. It
38 /// is recommended that you use a dedicated image loading framework such as
39 /// [glycin](https://lib.rs/crates/glycin), if you need to load untrusted image
40 /// data.
41 ///
42 /// This is an Abstract Base Class, you cannot instantiate it.
43 ///
44 /// ## Properties
45 ///
46 ///
47 /// #### `color-state`
48 /// The color state of the texture.
49 ///
50 /// Readable | Writeable | Construct Only
51 ///
52 ///
53 /// #### `height`
54 /// The height of the texture, in pixels.
55 ///
56 /// Readable | Writeable | Construct Only
57 ///
58 ///
59 /// #### `width`
60 /// The width of the texture, in pixels.
61 ///
62 /// Readable | Writeable | Construct Only
63 ///
64 /// # Implements
65 ///
66 /// [`TextureExt`][trait@crate::prelude::TextureExt], [`PaintableExt`][trait@crate::prelude::PaintableExt], [`trait@gio::prelude::IconExt`], [`trait@gio::prelude::LoadableIconExt`], [`TextureExtManual`][trait@crate::prelude::TextureExtManual]
67 #[doc(alias = "GdkTexture")]
68 pub struct Texture(Object<ffi::GdkTexture, ffi::GdkTextureClass>) @implements Paintable, gio::Icon, gio::LoadableIcon;
69
70 match fn {
71 type_ => || ffi::gdk_texture_get_type(),
72 }
73}
74
75impl Texture {
76 pub const NONE: Option<&'static Texture> = None;
77
78 /// Creates a new texture object representing the [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf].
79 ///
80 /// This function is threadsafe, so that you can e.g. use GTask
81 /// and `Gio::Task::run_in_thread()` to avoid blocking the main thread
82 /// while loading a big image.
83 /// ## `pixbuf`
84 /// a [`gdk_pixbuf::Pixbuf`][crate::gdk_pixbuf::Pixbuf]
85 ///
86 /// # Returns
87 ///
88 /// a new [`Texture`][crate::Texture]
89 #[doc(alias = "gdk_texture_new_for_pixbuf")]
90 #[doc(alias = "new_for_pixbuf")]
91 pub fn for_pixbuf(pixbuf: &gdk_pixbuf::Pixbuf) -> Texture {
92 assert_initialized_main_thread!();
93 unsafe { from_glib_full(ffi::gdk_texture_new_for_pixbuf(pixbuf.to_glib_none().0)) }
94 }
95
96 /// Creates a new texture by loading an image from memory,
97 ///
98 /// The file format is detected automatically. The supported formats
99 /// are PNG, JPEG and TIFF, though more formats might be available.
100 ///
101 /// If [`None`] is returned, then @error will be set.
102 ///
103 /// This function is threadsafe, so that you can e.g. use GTask
104 /// and `Gio::Task::run_in_thread()` to avoid blocking the main thread
105 /// while loading a big image.
106 /// ## `bytes`
107 /// a `GBytes` containing the data to load
108 ///
109 /// # Returns
110 ///
111 /// A newly-created [`Texture`][crate::Texture]
112 #[cfg(feature = "v4_6")]
113 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
114 #[doc(alias = "gdk_texture_new_from_bytes")]
115 #[doc(alias = "new_from_bytes")]
116 pub fn from_bytes(bytes: &glib::Bytes) -> Result<Texture, glib::Error> {
117 assert_initialized_main_thread!();
118 unsafe {
119 let mut error = std::ptr::null_mut();
120 let ret = ffi::gdk_texture_new_from_bytes(bytes.to_glib_none().0, &mut error);
121 if error.is_null() {
122 Ok(from_glib_full(ret))
123 } else {
124 Err(from_glib_full(error))
125 }
126 }
127 }
128
129 /// Creates a new texture by loading an image from a file.
130 ///
131 /// The file format is detected automatically. The supported formats
132 /// are PNG, JPEG and TIFF, though more formats might be available.
133 ///
134 /// If [`None`] is returned, then @error will be set.
135 ///
136 /// This function is threadsafe, so that you can e.g. use GTask
137 /// and `Gio::Task::run_in_thread()` to avoid blocking the main thread
138 /// while loading a big image.
139 /// ## `file`
140 /// `GFile` to load
141 ///
142 /// # Returns
143 ///
144 /// A newly-created [`Texture`][crate::Texture]
145 #[doc(alias = "gdk_texture_new_from_file")]
146 #[doc(alias = "new_from_file")]
147 pub fn from_file(file: &impl IsA<gio::File>) -> Result<Texture, glib::Error> {
148 assert_initialized_main_thread!();
149 unsafe {
150 let mut error = std::ptr::null_mut();
151 let ret = ffi::gdk_texture_new_from_file(file.as_ref().to_glib_none().0, &mut error);
152 if error.is_null() {
153 Ok(from_glib_full(ret))
154 } else {
155 Err(from_glib_full(error))
156 }
157 }
158 }
159
160 /// Creates a new texture by loading an image from a file.
161 ///
162 /// The file format is detected automatically. The supported formats
163 /// are PNG, JPEG and TIFF, though more formats might be available.
164 ///
165 /// If [`None`] is returned, then @error will be set.
166 ///
167 /// This function is threadsafe, so that you can e.g. use GTask
168 /// and `Gio::Task::run_in_thread()` to avoid blocking the main thread
169 /// while loading a big image.
170 /// ## `path`
171 /// the filename to load
172 ///
173 /// # Returns
174 ///
175 /// A newly-created [`Texture`][crate::Texture]
176 #[cfg(feature = "v4_6")]
177 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
178 #[doc(alias = "gdk_texture_new_from_filename")]
179 #[doc(alias = "new_from_filename")]
180 pub fn from_filename(path: impl AsRef<std::path::Path>) -> Result<Texture, glib::Error> {
181 assert_initialized_main_thread!();
182 unsafe {
183 let mut error = std::ptr::null_mut();
184 let ret =
185 ffi::gdk_texture_new_from_filename(path.as_ref().to_glib_none().0, &mut error);
186 if error.is_null() {
187 Ok(from_glib_full(ret))
188 } else {
189 Err(from_glib_full(error))
190 }
191 }
192 }
193
194 /// Creates a new texture by loading an image from a resource.
195 ///
196 /// The file format is detected automatically. The supported formats
197 /// are PNG and JPEG, though more formats might be available.
198 ///
199 /// It is a fatal error if @resource_path does not specify a valid
200 /// image resource and the program will abort if that happens.
201 /// If you are unsure about the validity of a resource, use
202 /// [`from_file()`][Self::from_file()] to load it.
203 ///
204 /// This function is threadsafe, so that you can e.g. use GTask
205 /// and `Gio::Task::run_in_thread()` to avoid blocking the main thread
206 /// while loading a big image.
207 /// ## `resource_path`
208 /// the path of the resource file
209 ///
210 /// # Returns
211 ///
212 /// A newly-created [`Texture`][crate::Texture]
213 #[doc(alias = "gdk_texture_new_from_resource")]
214 #[doc(alias = "new_from_resource")]
215 pub fn from_resource(resource_path: &str) -> Texture {
216 assert_initialized_main_thread!();
217 unsafe {
218 from_glib_full(ffi::gdk_texture_new_from_resource(
219 resource_path.to_glib_none().0,
220 ))
221 }
222 }
223}
224
225unsafe impl Send for Texture {}
226unsafe impl Sync for Texture {}
227
228/// Trait containing all [`struct@Texture`] methods.
229///
230/// # Implementors
231///
232/// [`DmabufTexture`][struct@crate::DmabufTexture], [`GLTexture`][struct@crate::GLTexture], [`MemoryTexture`][struct@crate::MemoryTexture], [`Texture`][struct@crate::Texture]
233pub trait TextureExt: IsA<Texture> + 'static {
234 /// Returns the color state associated with the texture.
235 ///
236 /// # Returns
237 ///
238 /// the color state of the [`Texture`][crate::Texture]
239 #[cfg(feature = "v4_16")]
240 #[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
241 #[doc(alias = "gdk_texture_get_color_state")]
242 #[doc(alias = "get_color_state")]
243 #[doc(alias = "color-state")]
244 fn color_state(&self) -> ColorState {
245 unsafe {
246 from_glib_none(ffi::gdk_texture_get_color_state(
247 self.as_ref().to_glib_none().0,
248 ))
249 }
250 }
251
252 /// Gets the memory format most closely associated with the data of
253 /// the texture.
254 ///
255 /// Note that it may not be an exact match for texture data
256 /// stored on the GPU or with compression.
257 ///
258 /// The format can give an indication about the bit depth and opacity
259 /// of the texture and is useful to determine the best format for
260 /// downloading the texture.
261 ///
262 /// # Returns
263 ///
264 /// the preferred format for the texture's data
265 #[cfg(feature = "v4_10")]
266 #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
267 #[doc(alias = "gdk_texture_get_format")]
268 #[doc(alias = "get_format")]
269 fn format(&self) -> MemoryFormat {
270 unsafe { from_glib(ffi::gdk_texture_get_format(self.as_ref().to_glib_none().0)) }
271 }
272
273 /// Returns the height of the @self, in pixels.
274 ///
275 /// # Returns
276 ///
277 /// the height of the [`Texture`][crate::Texture]
278 #[doc(alias = "gdk_texture_get_height")]
279 #[doc(alias = "get_height")]
280 fn height(&self) -> i32 {
281 unsafe { ffi::gdk_texture_get_height(self.as_ref().to_glib_none().0) }
282 }
283
284 /// Returns the width of @self, in pixels.
285 ///
286 /// # Returns
287 ///
288 /// the width of the [`Texture`][crate::Texture]
289 #[doc(alias = "gdk_texture_get_width")]
290 #[doc(alias = "get_width")]
291 fn width(&self) -> i32 {
292 unsafe { ffi::gdk_texture_get_width(self.as_ref().to_glib_none().0) }
293 }
294
295 /// Store the given @self to the @filename as a PNG file.
296 ///
297 /// This is a utility function intended for debugging and testing.
298 /// If you want more control over formats, proper error handling or
299 /// want to store to a [`gio::File`][crate::gio::File] or other location, you might want to
300 /// use [`save_to_png_bytes()`][Self::save_to_png_bytes()] or look into the
301 /// gdk-pixbuf library.
302 /// ## `filename`
303 /// the filename to store to
304 ///
305 /// # Returns
306 ///
307 /// [`true`] if saving succeeded, [`false`] on failure.
308 #[doc(alias = "gdk_texture_save_to_png")]
309 fn save_to_png(
310 &self,
311 filename: impl AsRef<std::path::Path>,
312 ) -> Result<(), glib::error::BoolError> {
313 unsafe {
314 glib::result_from_gboolean!(
315 ffi::gdk_texture_save_to_png(
316 self.as_ref().to_glib_none().0,
317 filename.as_ref().to_glib_none().0
318 ),
319 "Failed to save the texture as png"
320 )
321 }
322 }
323
324 /// Store the given @self in memory as a PNG file.
325 ///
326 /// Use [`Texture::from_bytes()`][crate::Texture::from_bytes()] to read it back.
327 ///
328 /// If you want to serialize a texture, this is a convenient and
329 /// portable way to do that.
330 ///
331 /// If you need more control over the generated image, such as
332 /// attaching metadata, you should look into an image handling
333 /// library such as the gdk-pixbuf library.
334 ///
335 /// If you are dealing with high dynamic range float data, you
336 /// might also want to consider [`save_to_tiff_bytes()`][Self::save_to_tiff_bytes()]
337 /// instead.
338 ///
339 /// # Returns
340 ///
341 /// a newly allocated `GBytes` containing PNG data
342 #[cfg(feature = "v4_6")]
343 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
344 #[doc(alias = "gdk_texture_save_to_png_bytes")]
345 fn save_to_png_bytes(&self) -> glib::Bytes {
346 unsafe {
347 from_glib_full(ffi::gdk_texture_save_to_png_bytes(
348 self.as_ref().to_glib_none().0,
349 ))
350 }
351 }
352
353 /// Store the given @self to the @filename as a TIFF file.
354 ///
355 /// GTK will attempt to store data without loss.
356 /// ## `filename`
357 /// the filename to store to
358 ///
359 /// # Returns
360 ///
361 /// [`true`] if saving succeeded, [`false`] on failure.
362 #[cfg(feature = "v4_6")]
363 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
364 #[doc(alias = "gdk_texture_save_to_tiff")]
365 fn save_to_tiff(
366 &self,
367 filename: impl AsRef<std::path::Path>,
368 ) -> Result<(), glib::error::BoolError> {
369 unsafe {
370 glib::result_from_gboolean!(
371 ffi::gdk_texture_save_to_tiff(
372 self.as_ref().to_glib_none().0,
373 filename.as_ref().to_glib_none().0
374 ),
375 "Failed to save the texture as tiff"
376 )
377 }
378 }
379
380 /// Store the given @self in memory as a TIFF file.
381 ///
382 /// Use [`Texture::from_bytes()`][crate::Texture::from_bytes()] to read it back.
383 ///
384 /// This function is intended to store a representation of the
385 /// texture's data that is as accurate as possible. This is
386 /// particularly relevant when working with high dynamic range
387 /// images and floating-point texture data.
388 ///
389 /// If that is not your concern and you are interested in a
390 /// smaller size and a more portable format, you might want to
391 /// use [`save_to_png_bytes()`][Self::save_to_png_bytes()].
392 ///
393 /// # Returns
394 ///
395 /// a newly allocated `GBytes` containing TIFF data
396 #[cfg(feature = "v4_6")]
397 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
398 #[doc(alias = "gdk_texture_save_to_tiff_bytes")]
399 fn save_to_tiff_bytes(&self) -> glib::Bytes {
400 unsafe {
401 from_glib_full(ffi::gdk_texture_save_to_tiff_bytes(
402 self.as_ref().to_glib_none().0,
403 ))
404 }
405 }
406}
407
408impl<O: IsA<Texture>> TextureExt for O {}