gdk_pixbuf/auto/
pixbuf_loader.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
5use crate::{ffi, Pixbuf, PixbufAnimation, PixbufFormat};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// Incremental image loader.
16    ///
17    /// [`PixbufLoader`][crate::PixbufLoader] provides a way for applications to drive the
18    /// process of loading an image, by letting them send the image data
19    /// directly to the loader instead of having the loader read the data
20    /// from a file. Applications can use this functionality instead of
21    /// `gdk_pixbuf_new_from_file()` or `gdk_pixbuf_animation_new_from_file()`
22    /// when they need to parse image data in small chunks. For example,
23    /// it should be used when reading an image from a (potentially) slow
24    /// network connection, or when loading an extremely large file.
25    ///
26    /// To use [`PixbufLoader`][crate::PixbufLoader] to load an image, create a new instance,
27    /// and call [`PixbufLoaderExt::write()`][crate::prelude::PixbufLoaderExt::write()] to send the data
28    /// to it. When done, [`PixbufLoaderExt::close()`][crate::prelude::PixbufLoaderExt::close()] should be
29    /// called to end the stream and finalize everything.
30    ///
31    /// The loader will emit three important signals throughout the process:
32    ///
33    ///  - [`size-prepared`][struct@crate::PixbufLoader#size-prepared] will be emitted as
34    ///    soon as the image has enough information to determine the size of
35    ///    the image to be used. If you want to scale the image while loading
36    ///    it, you can call [`PixbufLoaderExt::set_size()`][crate::prelude::PixbufLoaderExt::set_size()] in
37    ///    response to this signal.
38    ///  - [`area-prepared`][struct@crate::PixbufLoader#area-prepared] will be emitted as
39    ///    soon as the pixbuf of the desired has been allocated. You can obtain
40    ///    the [`Pixbuf`][crate::Pixbuf] instance by calling [`PixbufLoaderExt::pixbuf()`][crate::prelude::PixbufLoaderExt::pixbuf()].
41    ///    If you want to use it, simply acquire a reference to it. You can
42    ///    also call `gdk_pixbuf_loader_get_pixbuf()` later to get the same
43    ///    pixbuf.
44    ///  - [`area-updated`][struct@crate::PixbufLoader#area-updated] will be emitted every
45    ///    time a region is updated. This way you can update a partially
46    ///    completed image. Note that you do not know anything about the
47    ///    completeness of an image from the updated area. For example, in an
48    ///    interlaced image you will need to make several passes before the
49    ///    image is done loading.
50    ///
51    /// ## Loading an animation
52    ///
53    /// Loading an animation is almost as easy as loading an image. Once the
54    /// first [`area-prepared`][struct@crate::PixbufLoader#area-prepared] signal has been
55    /// emitted, you can call [`PixbufLoaderExt::animation()`][crate::prelude::PixbufLoaderExt::animation()] to
56    /// get the [`PixbufAnimation`][crate::PixbufAnimation] instance, and then call
57    /// and [`PixbufAnimationExtManual::iter()`][crate::prelude::PixbufAnimationExtManual::iter()] to get a
58    /// [`PixbufAnimationIter`][crate::PixbufAnimationIter] to retrieve the pixbuf for the
59    /// desired time stamp.
60    ///
61    /// ## Signals
62    ///
63    ///
64    /// #### `area-prepared`
65    ///  This signal is emitted when the pixbuf loader has allocated the
66    /// pixbuf in the desired size.
67    ///
68    /// After this signal is emitted, applications can call
69    /// gdk_pixbuf_loader_get_pixbuf() to fetch the partially-loaded
70    /// pixbuf.
71    ///
72    ///
73    ///
74    ///
75    /// #### `area-updated`
76    ///  This signal is emitted when a significant area of the image being
77    /// loaded has been updated.
78    ///
79    /// Normally it means that a complete scanline has been read in, but
80    /// it could be a different area as well.
81    ///
82    /// Applications can use this signal to know when to repaint
83    /// areas of an image that is being loaded.
84    ///
85    ///
86    ///
87    ///
88    /// #### `closed`
89    ///  This signal is emitted when gdk_pixbuf_loader_close() is called.
90    ///
91    /// It can be used by different parts of an application to receive
92    /// notification when an image loader is closed by the code that
93    /// drives it.
94    ///
95    ///
96    ///
97    ///
98    /// #### `size-prepared`
99    ///  This signal is emitted when the pixbuf loader has been fed the
100    /// initial amount of data that is required to figure out the size
101    /// of the image that it will create.
102    ///
103    /// Applications can call gdk_pixbuf_loader_set_size() in response
104    /// to this signal to set the desired size to which the image
105    /// should be scaled.
106    ///
107    ///
108    ///
109    /// # Implements
110    ///
111    /// [`PixbufLoaderExt`][trait@crate::prelude::PixbufLoaderExt]
112    #[doc(alias = "GdkPixbufLoader")]
113    pub struct PixbufLoader(Object<ffi::GdkPixbufLoader, ffi::GdkPixbufLoaderClass>);
114
115    match fn {
116        type_ => || ffi::gdk_pixbuf_loader_get_type(),
117    }
118}
119
120impl PixbufLoader {
121    pub const NONE: Option<&'static PixbufLoader> = None;
122
123    /// Creates a new pixbuf loader object.
124    ///
125    /// # Returns
126    ///
127    /// A newly-created pixbuf loader.
128    #[doc(alias = "gdk_pixbuf_loader_new")]
129    pub fn new() -> PixbufLoader {
130        unsafe { from_glib_full(ffi::gdk_pixbuf_loader_new()) }
131    }
132
133    /// Creates a new pixbuf loader object that always attempts to parse
134    /// image data as if it were an image of MIME type @mime_type, instead of
135    /// identifying the type automatically.
136    ///
137    /// This function is useful if you want an error if the image isn't the
138    /// expected MIME type; for loading image formats that can't be reliably
139    /// identified by looking at the data; or if the user manually forces a
140    /// specific MIME type.
141    ///
142    /// The list of supported mime types depends on what image loaders
143    /// are installed, but typically "image/png", "image/jpeg", "image/gif",
144    /// "image/tiff" and "image/x-xpixmap" are among the supported mime types.
145    /// To obtain the full list of supported mime types, call
146    /// gdk_pixbuf_format_get_mime_types() on each of the #GdkPixbufFormat
147    /// structs returned by gdk_pixbuf_get_formats().
148    /// ## `mime_type`
149    /// the mime type to be loaded
150    ///
151    /// # Returns
152    ///
153    /// A newly-created pixbuf loader.
154    #[doc(alias = "gdk_pixbuf_loader_new_with_mime_type")]
155    #[doc(alias = "new_with_mime_type")]
156    pub fn with_mime_type(mime_type: &str) -> Result<PixbufLoader, glib::Error> {
157        unsafe {
158            let mut error = std::ptr::null_mut();
159            let ret =
160                ffi::gdk_pixbuf_loader_new_with_mime_type(mime_type.to_glib_none().0, &mut error);
161            if error.is_null() {
162                Ok(from_glib_full(ret))
163            } else {
164                Err(from_glib_full(error))
165            }
166        }
167    }
168
169    /// Creates a new pixbuf loader object that always attempts to parse
170    /// image data as if it were an image of type @image_type, instead of
171    /// identifying the type automatically.
172    ///
173    /// This function is useful if you want an error if the image isn't the
174    /// expected type; for loading image formats that can't be reliably
175    /// identified by looking at the data; or if the user manually forces
176    /// a specific type.
177    ///
178    /// The list of supported image formats depends on what image loaders
179    /// are installed, but typically "png", "jpeg", "gif", "tiff" and
180    /// "xpm" are among the supported formats. To obtain the full list of
181    /// supported image formats, call gdk_pixbuf_format_get_name() on each
182    /// of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
183    /// ## `image_type`
184    /// name of the image format to be loaded with the image
185    ///
186    /// # Returns
187    ///
188    /// A newly-created pixbuf loader.
189    #[doc(alias = "gdk_pixbuf_loader_new_with_type")]
190    #[doc(alias = "new_with_type")]
191    pub fn with_type(image_type: &str) -> Result<PixbufLoader, glib::Error> {
192        unsafe {
193            let mut error = std::ptr::null_mut();
194            let ret = ffi::gdk_pixbuf_loader_new_with_type(image_type.to_glib_none().0, &mut error);
195            if error.is_null() {
196                Ok(from_glib_full(ret))
197            } else {
198                Err(from_glib_full(error))
199            }
200        }
201    }
202}
203
204impl Default for PixbufLoader {
205    fn default() -> Self {
206        Self::new()
207    }
208}
209
210/// Trait containing all [`struct@PixbufLoader`] methods.
211///
212/// # Implementors
213///
214/// [`PixbufLoader`][struct@crate::PixbufLoader]
215pub trait PixbufLoaderExt: IsA<PixbufLoader> + 'static {
216    /// Informs a pixbuf loader that no further writes with
217    /// gdk_pixbuf_loader_write() will occur, so that it can free its
218    /// internal loading structures.
219    ///
220    /// This function also tries to parse any data that hasn't yet been parsed;
221    /// if the remaining data is partial or corrupt, an error will be returned.
222    ///
223    /// If `FALSE` is returned, `error` will be set to an error from the
224    /// `GDK_PIXBUF_ERROR` or `G_FILE_ERROR` domains.
225    ///
226    /// If you're just cancelling a load rather than expecting it to be finished,
227    /// passing `NULL` for `error` to ignore it is reasonable.
228    ///
229    /// Remember that this function does not release a reference on the loader, so
230    /// you will need to explicitly release any reference you hold.
231    ///
232    /// # Returns
233    ///
234    /// `TRUE` if all image data written so far was successfully
235    ///   passed out via the update_area signal
236    #[doc(alias = "gdk_pixbuf_loader_close")]
237    fn close(&self) -> Result<(), glib::Error> {
238        unsafe {
239            let mut error = std::ptr::null_mut();
240            let is_ok = ffi::gdk_pixbuf_loader_close(self.as_ref().to_glib_none().0, &mut error);
241            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
242            if error.is_null() {
243                Ok(())
244            } else {
245                Err(from_glib_full(error))
246            }
247        }
248    }
249
250    /// Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
251    ///
252    /// In general it only makes sense to call this function after the
253    /// [`area-prepared`][struct@crate::PixbufLoader#area-prepared] signal has been emitted by
254    /// the loader.
255    ///
256    /// If the loader doesn't have enough bytes yet, and hasn't emitted the `area-prepared`
257    /// signal, this function will return `NULL`.
258    ///
259    /// # Returns
260    ///
261    /// The animation that the loader is
262    ///   currently loading
263    #[doc(alias = "gdk_pixbuf_loader_get_animation")]
264    #[doc(alias = "get_animation")]
265    fn animation(&self) -> Option<PixbufAnimation> {
266        unsafe {
267            from_glib_none(ffi::gdk_pixbuf_loader_get_animation(
268                self.as_ref().to_glib_none().0,
269            ))
270        }
271    }
272
273    /// Obtains the available information about the format of the
274    /// currently loading image file.
275    ///
276    /// # Returns
277    ///
278    /// A #GdkPixbufFormat
279    #[doc(alias = "gdk_pixbuf_loader_get_format")]
280    #[doc(alias = "get_format")]
281    fn format(&self) -> Option<PixbufFormat> {
282        unsafe {
283            from_glib_none(ffi::gdk_pixbuf_loader_get_format(
284                self.as_ref().to_glib_none().0,
285            ))
286        }
287    }
288
289    /// Queries the #GdkPixbuf that a pixbuf loader is currently creating.
290    ///
291    /// In general it only makes sense to call this function after the
292    /// [`area-prepared`][struct@crate::PixbufLoader#area-prepared] signal has been
293    /// emitted by the loader; this means that enough data has been read
294    /// to know the size of the image that will be allocated.
295    ///
296    /// If the loader has not received enough data via gdk_pixbuf_loader_write(),
297    /// then this function returns `NULL`.
298    ///
299    /// The returned pixbuf will be the same in all future calls to the loader,
300    /// so if you want to keep using it, you should acquire a reference to it.
301    ///
302    /// Additionally, if the loader is an animation, it will return the "static
303    /// image" of the animation (see gdk_pixbuf_animation_get_static_image()).
304    ///
305    /// # Returns
306    ///
307    /// The pixbuf that the loader is
308    ///   creating
309    #[doc(alias = "gdk_pixbuf_loader_get_pixbuf")]
310    #[doc(alias = "get_pixbuf")]
311    fn pixbuf(&self) -> Option<Pixbuf> {
312        unsafe {
313            from_glib_none(ffi::gdk_pixbuf_loader_get_pixbuf(
314                self.as_ref().to_glib_none().0,
315            ))
316        }
317    }
318
319    /// Causes the image to be scaled while it is loaded.
320    ///
321    /// The desired image size can be determined relative to the original
322    /// size of the image by calling gdk_pixbuf_loader_set_size() from a
323    /// signal handler for the ::size-prepared signal.
324    ///
325    /// Attempts to set the desired image size  are ignored after the
326    /// emission of the ::size-prepared signal.
327    /// ## `width`
328    /// The desired width of the image being loaded.
329    /// ## `height`
330    /// The desired height of the image being loaded.
331    #[doc(alias = "gdk_pixbuf_loader_set_size")]
332    fn set_size(&self, width: i32, height: i32) {
333        unsafe {
334            ffi::gdk_pixbuf_loader_set_size(self.as_ref().to_glib_none().0, width, height);
335        }
336    }
337
338    /// Parses the next `count` bytes in the given image buffer.
339    /// ## `buf`
340    /// Pointer to image data.
341    ///
342    /// # Returns
343    ///
344    /// `TRUE` if the write was successful, or
345    ///   `FALSE` if the loader cannot parse the buffer
346    #[doc(alias = "gdk_pixbuf_loader_write")]
347    fn write(&self, buf: &[u8]) -> Result<(), glib::Error> {
348        let count = buf.len() as _;
349        unsafe {
350            let mut error = std::ptr::null_mut();
351            let is_ok = ffi::gdk_pixbuf_loader_write(
352                self.as_ref().to_glib_none().0,
353                buf.to_glib_none().0,
354                count,
355                &mut error,
356            );
357            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
358            if error.is_null() {
359                Ok(())
360            } else {
361                Err(from_glib_full(error))
362            }
363        }
364    }
365
366    /// Parses the next contents of the given image buffer.
367    /// ## `buffer`
368    /// The image data as a `GBytes` buffer.
369    ///
370    /// # Returns
371    ///
372    /// `TRUE` if the write was successful, or `FALSE` if
373    ///   the loader cannot parse the buffer
374    #[doc(alias = "gdk_pixbuf_loader_write_bytes")]
375    fn write_bytes(&self, buffer: &glib::Bytes) -> Result<(), glib::Error> {
376        unsafe {
377            let mut error = std::ptr::null_mut();
378            let is_ok = ffi::gdk_pixbuf_loader_write_bytes(
379                self.as_ref().to_glib_none().0,
380                buffer.to_glib_none().0,
381                &mut error,
382            );
383            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
384            if error.is_null() {
385                Ok(())
386            } else {
387                Err(from_glib_full(error))
388            }
389        }
390    }
391
392    /// This signal is emitted when the pixbuf loader has allocated the
393    /// pixbuf in the desired size.
394    ///
395    /// After this signal is emitted, applications can call
396    /// gdk_pixbuf_loader_get_pixbuf() to fetch the partially-loaded
397    /// pixbuf.
398    #[doc(alias = "area-prepared")]
399    fn connect_area_prepared<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
400        unsafe extern "C" fn area_prepared_trampoline<P: IsA<PixbufLoader>, F: Fn(&P) + 'static>(
401            this: *mut ffi::GdkPixbufLoader,
402            f: glib::ffi::gpointer,
403        ) {
404            let f: &F = &*(f as *const F);
405            f(PixbufLoader::from_glib_borrow(this).unsafe_cast_ref())
406        }
407        unsafe {
408            let f: Box_<F> = Box_::new(f);
409            connect_raw(
410                self.as_ptr() as *mut _,
411                c"area-prepared".as_ptr() as *const _,
412                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
413                    area_prepared_trampoline::<Self, F> as *const (),
414                )),
415                Box_::into_raw(f),
416            )
417        }
418    }
419
420    /// This signal is emitted when a significant area of the image being
421    /// loaded has been updated.
422    ///
423    /// Normally it means that a complete scanline has been read in, but
424    /// it could be a different area as well.
425    ///
426    /// Applications can use this signal to know when to repaint
427    /// areas of an image that is being loaded.
428    /// ## `x`
429    /// X offset of upper-left corner of the updated area.
430    /// ## `y`
431    /// Y offset of upper-left corner of the updated area.
432    /// ## `width`
433    /// Width of updated area.
434    /// ## `height`
435    /// Height of updated area.
436    #[doc(alias = "area-updated")]
437    fn connect_area_updated<F: Fn(&Self, i32, i32, i32, i32) + 'static>(
438        &self,
439        f: F,
440    ) -> SignalHandlerId {
441        unsafe extern "C" fn area_updated_trampoline<
442            P: IsA<PixbufLoader>,
443            F: Fn(&P, i32, i32, i32, i32) + 'static,
444        >(
445            this: *mut ffi::GdkPixbufLoader,
446            x: std::ffi::c_int,
447            y: std::ffi::c_int,
448            width: std::ffi::c_int,
449            height: std::ffi::c_int,
450            f: glib::ffi::gpointer,
451        ) {
452            let f: &F = &*(f as *const F);
453            f(
454                PixbufLoader::from_glib_borrow(this).unsafe_cast_ref(),
455                x,
456                y,
457                width,
458                height,
459            )
460        }
461        unsafe {
462            let f: Box_<F> = Box_::new(f);
463            connect_raw(
464                self.as_ptr() as *mut _,
465                c"area-updated".as_ptr() as *const _,
466                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
467                    area_updated_trampoline::<Self, F> as *const (),
468                )),
469                Box_::into_raw(f),
470            )
471        }
472    }
473
474    /// This signal is emitted when gdk_pixbuf_loader_close() is called.
475    ///
476    /// It can be used by different parts of an application to receive
477    /// notification when an image loader is closed by the code that
478    /// drives it.
479    #[doc(alias = "closed")]
480    fn connect_closed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
481        unsafe extern "C" fn closed_trampoline<P: IsA<PixbufLoader>, F: Fn(&P) + 'static>(
482            this: *mut ffi::GdkPixbufLoader,
483            f: glib::ffi::gpointer,
484        ) {
485            let f: &F = &*(f as *const F);
486            f(PixbufLoader::from_glib_borrow(this).unsafe_cast_ref())
487        }
488        unsafe {
489            let f: Box_<F> = Box_::new(f);
490            connect_raw(
491                self.as_ptr() as *mut _,
492                c"closed".as_ptr() as *const _,
493                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
494                    closed_trampoline::<Self, F> as *const (),
495                )),
496                Box_::into_raw(f),
497            )
498        }
499    }
500
501    /// This signal is emitted when the pixbuf loader has been fed the
502    /// initial amount of data that is required to figure out the size
503    /// of the image that it will create.
504    ///
505    /// Applications can call gdk_pixbuf_loader_set_size() in response
506    /// to this signal to set the desired size to which the image
507    /// should be scaled.
508    /// ## `width`
509    /// the original width of the image
510    /// ## `height`
511    /// the original height of the image
512    #[doc(alias = "size-prepared")]
513    fn connect_size_prepared<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
514        unsafe extern "C" fn size_prepared_trampoline<
515            P: IsA<PixbufLoader>,
516            F: Fn(&P, i32, i32) + 'static,
517        >(
518            this: *mut ffi::GdkPixbufLoader,
519            width: std::ffi::c_int,
520            height: std::ffi::c_int,
521            f: glib::ffi::gpointer,
522        ) {
523            let f: &F = &*(f as *const F);
524            f(
525                PixbufLoader::from_glib_borrow(this).unsafe_cast_ref(),
526                width,
527                height,
528            )
529        }
530        unsafe {
531            let f: Box_<F> = Box_::new(f);
532            connect_raw(
533                self.as_ptr() as *mut _,
534                c"size-prepared".as_ptr() as *const _,
535                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
536                    size_prepared_trampoline::<Self, F> as *const (),
537                )),
538                Box_::into_raw(f),
539            )
540        }
541    }
542}
543
544impl<O: IsA<PixbufLoader>> PixbufLoaderExt for O {}