1use crate::{ffi, Colorspace, InterpType, PixbufFormat, PixbufRotation};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9 #[doc(alias = "GdkPixbuf")]
10 pub struct Pixbuf(Object<ffi::GdkPixbuf>) @implements gio::Icon, gio::LoadableIcon;
11
12 match fn {
13 type_ => || ffi::gdk_pixbuf_get_type(),
14 }
15}
16
17impl Pixbuf {
18 #[doc(alias = "gdk_pixbuf_new")]
19 pub fn new(
20 colorspace: Colorspace,
21 has_alpha: bool,
22 bits_per_sample: i32,
23 width: i32,
24 height: i32,
25 ) -> Option<Pixbuf> {
26 unsafe {
27 from_glib_full(ffi::gdk_pixbuf_new(
28 colorspace.into_glib(),
29 has_alpha.into_glib(),
30 bits_per_sample,
31 width,
32 height,
33 ))
34 }
35 }
36
37 #[doc(alias = "gdk_pixbuf_new_from_bytes")]
38 #[doc(alias = "new_from_bytes")]
39 pub fn from_bytes(
40 data: &glib::Bytes,
41 colorspace: Colorspace,
42 has_alpha: bool,
43 bits_per_sample: i32,
44 width: i32,
45 height: i32,
46 rowstride: i32,
47 ) -> Pixbuf {
48 unsafe {
49 from_glib_full(ffi::gdk_pixbuf_new_from_bytes(
50 data.to_glib_none().0,
51 colorspace.into_glib(),
52 has_alpha.into_glib(),
53 bits_per_sample,
54 width,
55 height,
56 rowstride,
57 ))
58 }
59 }
60
61 #[doc(alias = "gdk_pixbuf_new_from_file")]
68 #[doc(alias = "new_from_file")]
69 pub fn from_file(filename: impl AsRef<std::path::Path>) -> Result<Pixbuf, glib::Error> {
70 unsafe {
71 let mut error = std::ptr::null_mut();
72 let ret = ffi::gdk_pixbuf_new_from_file(filename.as_ref().to_glib_none().0, &mut error);
73 if error.is_null() {
74 Ok(from_glib_full(ret))
75 } else {
76 Err(from_glib_full(error))
77 }
78 }
79 }
80
81 #[doc(alias = "gdk_pixbuf_new_from_file_at_scale")]
82 #[doc(alias = "new_from_file_at_scale")]
83 pub fn from_file_at_scale(
84 filename: impl AsRef<std::path::Path>,
85 width: i32,
86 height: i32,
87 preserve_aspect_ratio: bool,
88 ) -> Result<Pixbuf, glib::Error> {
89 unsafe {
90 let mut error = std::ptr::null_mut();
91 let ret = ffi::gdk_pixbuf_new_from_file_at_scale(
92 filename.as_ref().to_glib_none().0,
93 width,
94 height,
95 preserve_aspect_ratio.into_glib(),
96 &mut error,
97 );
98 if error.is_null() {
99 Ok(from_glib_full(ret))
100 } else {
101 Err(from_glib_full(error))
102 }
103 }
104 }
105
106 #[doc(alias = "gdk_pixbuf_new_from_file_at_size")]
107 #[doc(alias = "new_from_file_at_size")]
108 pub fn from_file_at_size(
109 filename: impl AsRef<std::path::Path>,
110 width: i32,
111 height: i32,
112 ) -> Result<Pixbuf, glib::Error> {
113 unsafe {
114 let mut error = std::ptr::null_mut();
115 let ret = ffi::gdk_pixbuf_new_from_file_at_size(
116 filename.as_ref().to_glib_none().0,
117 width,
118 height,
119 &mut error,
120 );
121 if error.is_null() {
122 Ok(from_glib_full(ret))
123 } else {
124 Err(from_glib_full(error))
125 }
126 }
127 }
128
129 #[doc(alias = "gdk_pixbuf_new_from_resource")]
130 #[doc(alias = "new_from_resource")]
131 pub fn from_resource(resource_path: &str) -> Result<Pixbuf, glib::Error> {
132 unsafe {
133 let mut error = std::ptr::null_mut();
134 let ret = ffi::gdk_pixbuf_new_from_resource(resource_path.to_glib_none().0, &mut error);
135 if error.is_null() {
136 Ok(from_glib_full(ret))
137 } else {
138 Err(from_glib_full(error))
139 }
140 }
141 }
142
143 #[doc(alias = "gdk_pixbuf_new_from_resource_at_scale")]
144 #[doc(alias = "new_from_resource_at_scale")]
145 pub fn from_resource_at_scale(
146 resource_path: &str,
147 width: i32,
148 height: i32,
149 preserve_aspect_ratio: bool,
150 ) -> Result<Pixbuf, glib::Error> {
151 unsafe {
152 let mut error = std::ptr::null_mut();
153 let ret = ffi::gdk_pixbuf_new_from_resource_at_scale(
154 resource_path.to_glib_none().0,
155 width,
156 height,
157 preserve_aspect_ratio.into_glib(),
158 &mut error,
159 );
160 if error.is_null() {
161 Ok(from_glib_full(ret))
162 } else {
163 Err(from_glib_full(error))
164 }
165 }
166 }
167
168 #[doc(alias = "gdk_pixbuf_new_from_stream")]
169 #[doc(alias = "new_from_stream")]
170 pub fn from_stream(
171 stream: &impl IsA<gio::InputStream>,
172 cancellable: Option<&impl IsA<gio::Cancellable>>,
173 ) -> Result<Pixbuf, glib::Error> {
174 unsafe {
175 let mut error = std::ptr::null_mut();
176 let ret = ffi::gdk_pixbuf_new_from_stream(
177 stream.as_ref().to_glib_none().0,
178 cancellable.map(|p| p.as_ref()).to_glib_none().0,
179 &mut error,
180 );
181 if error.is_null() {
182 Ok(from_glib_full(ret))
183 } else {
184 Err(from_glib_full(error))
185 }
186 }
187 }
188
189 #[doc(alias = "gdk_pixbuf_new_from_stream_at_scale")]
190 #[doc(alias = "new_from_stream_at_scale")]
191 pub fn from_stream_at_scale(
192 stream: &impl IsA<gio::InputStream>,
193 width: i32,
194 height: i32,
195 preserve_aspect_ratio: bool,
196 cancellable: Option<&impl IsA<gio::Cancellable>>,
197 ) -> Result<Pixbuf, glib::Error> {
198 unsafe {
199 let mut error = std::ptr::null_mut();
200 let ret = ffi::gdk_pixbuf_new_from_stream_at_scale(
201 stream.as_ref().to_glib_none().0,
202 width,
203 height,
204 preserve_aspect_ratio.into_glib(),
205 cancellable.map(|p| p.as_ref()).to_glib_none().0,
206 &mut error,
207 );
208 if error.is_null() {
209 Ok(from_glib_full(ret))
210 } else {
211 Err(from_glib_full(error))
212 }
213 }
214 }
215
216 #[doc(alias = "gdk_pixbuf_new_from_xpm_data")]
217 #[doc(alias = "new_from_xpm_data")]
218 pub fn from_xpm_data(data: &[&str]) -> Result<Pixbuf, glib::BoolError> {
219 unsafe {
220 Option::<_>::from_glib_full(ffi::gdk_pixbuf_new_from_xpm_data(data.to_glib_none().0))
221 .ok_or_else(|| glib::bool_error!("Invalid XPM data"))
222 }
223 }
224
225 #[doc(alias = "gdk_pixbuf_add_alpha")]
226 pub fn add_alpha(
227 &self,
228 substitute_color: bool,
229 r: u8,
230 g: u8,
231 b: u8,
232 ) -> Result<Pixbuf, glib::BoolError> {
233 unsafe {
234 Option::<_>::from_glib_full(ffi::gdk_pixbuf_add_alpha(
235 self.to_glib_none().0,
236 substitute_color.into_glib(),
237 r,
238 g,
239 b,
240 ))
241 .ok_or_else(|| glib::bool_error!("Failed to add alpha channel"))
242 }
243 }
244
245 #[doc(alias = "gdk_pixbuf_apply_embedded_orientation")]
246 #[must_use]
247 pub fn apply_embedded_orientation(&self) -> Option<Pixbuf> {
248 unsafe {
249 from_glib_full(ffi::gdk_pixbuf_apply_embedded_orientation(
250 self.to_glib_none().0,
251 ))
252 }
253 }
254
255 #[doc(alias = "gdk_pixbuf_composite")]
256 pub fn composite(
257 &self,
258 dest: &Pixbuf,
259 dest_x: i32,
260 dest_y: i32,
261 dest_width: i32,
262 dest_height: i32,
263 offset_x: f64,
264 offset_y: f64,
265 scale_x: f64,
266 scale_y: f64,
267 interp_type: InterpType,
268 overall_alpha: i32,
269 ) {
270 unsafe {
271 ffi::gdk_pixbuf_composite(
272 self.to_glib_none().0,
273 dest.to_glib_none().0,
274 dest_x,
275 dest_y,
276 dest_width,
277 dest_height,
278 offset_x,
279 offset_y,
280 scale_x,
281 scale_y,
282 interp_type.into_glib(),
283 overall_alpha,
284 );
285 }
286 }
287
288 #[doc(alias = "gdk_pixbuf_composite_color")]
289 pub fn composite_color(
290 &self,
291 dest: &Pixbuf,
292 dest_x: i32,
293 dest_y: i32,
294 dest_width: i32,
295 dest_height: i32,
296 offset_x: f64,
297 offset_y: f64,
298 scale_x: f64,
299 scale_y: f64,
300 interp_type: InterpType,
301 overall_alpha: i32,
302 check_x: i32,
303 check_y: i32,
304 check_size: i32,
305 color1: u32,
306 color2: u32,
307 ) {
308 unsafe {
309 ffi::gdk_pixbuf_composite_color(
310 self.to_glib_none().0,
311 dest.to_glib_none().0,
312 dest_x,
313 dest_y,
314 dest_width,
315 dest_height,
316 offset_x,
317 offset_y,
318 scale_x,
319 scale_y,
320 interp_type.into_glib(),
321 overall_alpha,
322 check_x,
323 check_y,
324 check_size,
325 color1,
326 color2,
327 );
328 }
329 }
330
331 #[doc(alias = "gdk_pixbuf_composite_color_simple")]
332 #[must_use]
333 pub fn composite_color_simple(
334 &self,
335 dest_width: i32,
336 dest_height: i32,
337 interp_type: InterpType,
338 overall_alpha: i32,
339 check_size: i32,
340 color1: u32,
341 color2: u32,
342 ) -> Option<Pixbuf> {
343 unsafe {
344 from_glib_full(ffi::gdk_pixbuf_composite_color_simple(
345 self.to_glib_none().0,
346 dest_width,
347 dest_height,
348 interp_type.into_glib(),
349 overall_alpha,
350 check_size,
351 color1,
352 color2,
353 ))
354 }
355 }
356
357 #[doc(alias = "gdk_pixbuf_copy")]
358 #[must_use]
359 pub fn copy(&self) -> Option<Pixbuf> {
360 unsafe { from_glib_full(ffi::gdk_pixbuf_copy(self.to_glib_none().0)) }
361 }
362
363 #[doc(alias = "gdk_pixbuf_copy_area")]
364 pub fn copy_area(
365 &self,
366 src_x: i32,
367 src_y: i32,
368 width: i32,
369 height: i32,
370 dest_pixbuf: &Pixbuf,
371 dest_x: i32,
372 dest_y: i32,
373 ) {
374 unsafe {
375 ffi::gdk_pixbuf_copy_area(
376 self.to_glib_none().0,
377 src_x,
378 src_y,
379 width,
380 height,
381 dest_pixbuf.to_glib_none().0,
382 dest_x,
383 dest_y,
384 );
385 }
386 }
387
388 #[doc(alias = "gdk_pixbuf_copy_options")]
389 pub fn copy_options(&self, dest_pixbuf: &Pixbuf) -> bool {
390 unsafe {
391 from_glib(ffi::gdk_pixbuf_copy_options(
392 self.to_glib_none().0,
393 dest_pixbuf.to_glib_none().0,
394 ))
395 }
396 }
397
398 #[doc(alias = "gdk_pixbuf_fill")]
399 pub fn fill(&self, pixel: u32) {
400 unsafe {
401 ffi::gdk_pixbuf_fill(self.to_glib_none().0, pixel);
402 }
403 }
404
405 #[doc(alias = "gdk_pixbuf_flip")]
406 #[must_use]
407 pub fn flip(&self, horizontal: bool) -> Option<Pixbuf> {
408 unsafe {
409 from_glib_full(ffi::gdk_pixbuf_flip(
410 self.to_glib_none().0,
411 horizontal.into_glib(),
412 ))
413 }
414 }
415
416 #[doc(alias = "gdk_pixbuf_get_bits_per_sample")]
417 #[doc(alias = "get_bits_per_sample")]
418 #[doc(alias = "bits-per-sample")]
419 pub fn bits_per_sample(&self) -> i32 {
420 unsafe { ffi::gdk_pixbuf_get_bits_per_sample(self.to_glib_none().0) }
421 }
422
423 #[doc(alias = "gdk_pixbuf_get_byte_length")]
424 #[doc(alias = "get_byte_length")]
425 pub fn byte_length(&self) -> usize {
426 unsafe { ffi::gdk_pixbuf_get_byte_length(self.to_glib_none().0) }
427 }
428
429 #[doc(alias = "gdk_pixbuf_get_colorspace")]
430 #[doc(alias = "get_colorspace")]
431 pub fn colorspace(&self) -> Colorspace {
432 unsafe { from_glib(ffi::gdk_pixbuf_get_colorspace(self.to_glib_none().0)) }
433 }
434
435 #[doc(alias = "gdk_pixbuf_get_has_alpha")]
436 #[doc(alias = "get_has_alpha")]
437 #[doc(alias = "has-alpha")]
438 pub fn has_alpha(&self) -> bool {
439 unsafe { from_glib(ffi::gdk_pixbuf_get_has_alpha(self.to_glib_none().0)) }
440 }
441
442 #[doc(alias = "gdk_pixbuf_get_height")]
443 #[doc(alias = "get_height")]
444 pub fn height(&self) -> i32 {
445 unsafe { ffi::gdk_pixbuf_get_height(self.to_glib_none().0) }
446 }
447
448 #[doc(alias = "gdk_pixbuf_get_n_channels")]
449 #[doc(alias = "get_n_channels")]
450 #[doc(alias = "n-channels")]
451 pub fn n_channels(&self) -> i32 {
452 unsafe { ffi::gdk_pixbuf_get_n_channels(self.to_glib_none().0) }
453 }
454
455 #[doc(alias = "gdk_pixbuf_get_option")]
456 #[doc(alias = "get_option")]
457 pub fn option(&self, key: &str) -> Option<glib::GString> {
458 unsafe {
459 from_glib_none(ffi::gdk_pixbuf_get_option(
460 self.to_glib_none().0,
461 key.to_glib_none().0,
462 ))
463 }
464 }
465
466 #[doc(alias = "gdk_pixbuf_get_rowstride")]
473 #[doc(alias = "get_rowstride")]
474 pub fn rowstride(&self) -> i32 {
475 unsafe { ffi::gdk_pixbuf_get_rowstride(self.to_glib_none().0) }
476 }
477
478 #[doc(alias = "gdk_pixbuf_get_width")]
479 #[doc(alias = "get_width")]
480 pub fn width(&self) -> i32 {
481 unsafe { ffi::gdk_pixbuf_get_width(self.to_glib_none().0) }
482 }
483
484 #[doc(alias = "gdk_pixbuf_new_subpixbuf")]
485 #[must_use]
486 pub fn new_subpixbuf(&self, src_x: i32, src_y: i32, width: i32, height: i32) -> Pixbuf {
487 unsafe {
488 from_glib_full(ffi::gdk_pixbuf_new_subpixbuf(
489 self.to_glib_none().0,
490 src_x,
491 src_y,
492 width,
493 height,
494 ))
495 }
496 }
497
498 #[doc(alias = "gdk_pixbuf_read_pixel_bytes")]
499 pub fn read_pixel_bytes(&self) -> glib::Bytes {
500 unsafe { from_glib_full(ffi::gdk_pixbuf_read_pixel_bytes(self.to_glib_none().0)) }
501 }
502
503 #[doc(alias = "gdk_pixbuf_remove_option")]
504 pub fn remove_option(&self, key: &str) -> bool {
505 unsafe {
506 from_glib(ffi::gdk_pixbuf_remove_option(
507 self.to_glib_none().0,
508 key.to_glib_none().0,
509 ))
510 }
511 }
512
513 #[doc(alias = "gdk_pixbuf_rotate_simple")]
514 #[must_use]
515 pub fn rotate_simple(&self, angle: PixbufRotation) -> Option<Pixbuf> {
516 unsafe {
517 from_glib_full(ffi::gdk_pixbuf_rotate_simple(
518 self.to_glib_none().0,
519 angle.into_glib(),
520 ))
521 }
522 }
523
524 #[doc(alias = "gdk_pixbuf_saturate_and_pixelate")]
525 pub fn saturate_and_pixelate(&self, dest: &Pixbuf, saturation: f32, pixelate: bool) {
526 unsafe {
527 ffi::gdk_pixbuf_saturate_and_pixelate(
528 self.to_glib_none().0,
529 dest.to_glib_none().0,
530 saturation,
531 pixelate.into_glib(),
532 );
533 }
534 }
535
536 #[doc(alias = "gdk_pixbuf_scale")]
585 pub fn scale(
586 &self,
587 dest: &Pixbuf,
588 dest_x: i32,
589 dest_y: i32,
590 dest_width: i32,
591 dest_height: i32,
592 offset_x: f64,
593 offset_y: f64,
594 scale_x: f64,
595 scale_y: f64,
596 interp_type: InterpType,
597 ) {
598 unsafe {
599 ffi::gdk_pixbuf_scale(
600 self.to_glib_none().0,
601 dest.to_glib_none().0,
602 dest_x,
603 dest_y,
604 dest_width,
605 dest_height,
606 offset_x,
607 offset_y,
608 scale_x,
609 scale_y,
610 interp_type.into_glib(),
611 );
612 }
613 }
614
615 #[doc(alias = "gdk_pixbuf_scale_simple")]
616 #[must_use]
617 pub fn scale_simple(
618 &self,
619 dest_width: i32,
620 dest_height: i32,
621 interp_type: InterpType,
622 ) -> Option<Pixbuf> {
623 unsafe {
624 from_glib_full(ffi::gdk_pixbuf_scale_simple(
625 self.to_glib_none().0,
626 dest_width,
627 dest_height,
628 interp_type.into_glib(),
629 ))
630 }
631 }
632
633 #[doc(alias = "gdk_pixbuf_set_option")]
634 pub fn set_option(&self, key: &str, value: &str) -> bool {
635 unsafe {
636 from_glib(ffi::gdk_pixbuf_set_option(
637 self.to_glib_none().0,
638 key.to_glib_none().0,
639 value.to_glib_none().0,
640 ))
641 }
642 }
643
644 #[doc(alias = "pixel-bytes")]
645 pub fn pixel_bytes(&self) -> Option<glib::Bytes> {
646 ObjectExt::property(self, "pixel-bytes")
647 }
648
649 #[doc(alias = "gdk_pixbuf_calculate_rowstride")]
650 pub fn calculate_rowstride(
651 colorspace: Colorspace,
652 has_alpha: bool,
653 bits_per_sample: i32,
654 width: i32,
655 height: i32,
656 ) -> i32 {
657 unsafe {
658 ffi::gdk_pixbuf_calculate_rowstride(
659 colorspace.into_glib(),
660 has_alpha.into_glib(),
661 bits_per_sample,
662 width,
663 height,
664 )
665 }
666 }
667
668 #[doc(alias = "gdk_pixbuf_get_formats")]
669 #[doc(alias = "get_formats")]
670 pub fn formats() -> Vec<PixbufFormat> {
671 unsafe { FromGlibPtrContainer::from_glib_container(ffi::gdk_pixbuf_get_formats()) }
672 }
673
674 #[cfg(feature = "v2_40")]
675 #[cfg_attr(docsrs, doc(cfg(feature = "v2_40")))]
676 #[doc(alias = "gdk_pixbuf_init_modules")]
677 pub fn init_modules(path: &str) -> Result<(), glib::Error> {
678 unsafe {
679 let mut error = std::ptr::null_mut();
680 let is_ok = ffi::gdk_pixbuf_init_modules(path.to_glib_none().0, &mut error);
681 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
682 if error.is_null() {
683 Ok(())
684 } else {
685 Err(from_glib_full(error))
686 }
687 }
688 }
689}