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::{Pixbuf, PixbufAnimation, PixbufFormat, ffi};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
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            unsafe {
405                let f: &F = &*(f as *const F);
406                f(PixbufLoader::from_glib_borrow(this).unsafe_cast_ref())
407            }
408        }
409        unsafe {
410            let f: Box_<F> = Box_::new(f);
411            connect_raw(
412                self.as_ptr() as *mut _,
413                c"area-prepared".as_ptr() as *const _,
414                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
415                    area_prepared_trampoline::<Self, F> as *const (),
416                )),
417                Box_::into_raw(f),
418            )
419        }
420    }
421
422    /// This signal is emitted when a significant area of the image being
423    /// loaded has been updated.
424    ///
425    /// Normally it means that a complete scanline has been read in, but
426    /// it could be a different area as well.
427    ///
428    /// Applications can use this signal to know when to repaint
429    /// areas of an image that is being loaded.
430    /// ## `x`
431    /// X offset of upper-left corner of the updated area.
432    /// ## `y`
433    /// Y offset of upper-left corner of the updated area.
434    /// ## `width`
435    /// Width of updated area.
436    /// ## `height`
437    /// Height of updated area.
438    #[doc(alias = "area-updated")]
439    fn connect_area_updated<F: Fn(&Self, i32, i32, i32, i32) + 'static>(
440        &self,
441        f: F,
442    ) -> SignalHandlerId {
443        unsafe extern "C" fn area_updated_trampoline<
444            P: IsA<PixbufLoader>,
445            F: Fn(&P, i32, i32, i32, i32) + 'static,
446        >(
447            this: *mut ffi::GdkPixbufLoader,
448            x: std::ffi::c_int,
449            y: std::ffi::c_int,
450            width: std::ffi::c_int,
451            height: std::ffi::c_int,
452            f: glib::ffi::gpointer,
453        ) {
454            unsafe {
455                let f: &F = &*(f as *const F);
456                f(
457                    PixbufLoader::from_glib_borrow(this).unsafe_cast_ref(),
458                    x,
459                    y,
460                    width,
461                    height,
462                )
463            }
464        }
465        unsafe {
466            let f: Box_<F> = Box_::new(f);
467            connect_raw(
468                self.as_ptr() as *mut _,
469                c"area-updated".as_ptr() as *const _,
470                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
471                    area_updated_trampoline::<Self, F> as *const (),
472                )),
473                Box_::into_raw(f),
474            )
475        }
476    }
477
478    /// This signal is emitted when gdk_pixbuf_loader_close() is called.
479    ///
480    /// It can be used by different parts of an application to receive
481    /// notification when an image loader is closed by the code that
482    /// drives it.
483    #[doc(alias = "closed")]
484    fn connect_closed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
485        unsafe extern "C" fn closed_trampoline<P: IsA<PixbufLoader>, F: Fn(&P) + 'static>(
486            this: *mut ffi::GdkPixbufLoader,
487            f: glib::ffi::gpointer,
488        ) {
489            unsafe {
490                let f: &F = &*(f as *const F);
491                f(PixbufLoader::from_glib_borrow(this).unsafe_cast_ref())
492            }
493        }
494        unsafe {
495            let f: Box_<F> = Box_::new(f);
496            connect_raw(
497                self.as_ptr() as *mut _,
498                c"closed".as_ptr() as *const _,
499                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
500                    closed_trampoline::<Self, F> as *const (),
501                )),
502                Box_::into_raw(f),
503            )
504        }
505    }
506
507    /// This signal is emitted when the pixbuf loader has been fed the
508    /// initial amount of data that is required to figure out the size
509    /// of the image that it will create.
510    ///
511    /// Applications can call gdk_pixbuf_loader_set_size() in response
512    /// to this signal to set the desired size to which the image
513    /// should be scaled.
514    /// ## `width`
515    /// the original width of the image
516    /// ## `height`
517    /// the original height of the image
518    #[doc(alias = "size-prepared")]
519    fn connect_size_prepared<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
520        unsafe extern "C" fn size_prepared_trampoline<
521            P: IsA<PixbufLoader>,
522            F: Fn(&P, i32, i32) + 'static,
523        >(
524            this: *mut ffi::GdkPixbufLoader,
525            width: std::ffi::c_int,
526            height: std::ffi::c_int,
527            f: glib::ffi::gpointer,
528        ) {
529            unsafe {
530                let f: &F = &*(f as *const F);
531                f(
532                    PixbufLoader::from_glib_borrow(this).unsafe_cast_ref(),
533                    width,
534                    height,
535                )
536            }
537        }
538        unsafe {
539            let f: Box_<F> = Box_::new(f);
540            connect_raw(
541                self.as_ptr() as *mut _,
542                c"size-prepared".as_ptr() as *const _,
543                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
544                    size_prepared_trampoline::<Self, F> as *const (),
545                )),
546                Box_::into_raw(f),
547            )
548        }
549    }
550}
551
552impl<O: IsA<PixbufLoader>> PixbufLoaderExt for O {}