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