cogl/auto/bitmap.rs
1use crate::{Context, Object, PixelBuffer, PixelFormat};
2
3use glib::translate::*;
4use std::{fmt, mem, ptr};
5
6glib_wrapper! {
7 pub struct Bitmap(Object<ffi::CoglBitmap, BitmapClass>) @extends Object;
8
9 match fn {
10 get_type => || ffi::cogl_bitmap_get_gtype(),
11 }
12}
13
14impl Bitmap {
15 /// Creates a bitmap using some existing data. The data is not copied
16 /// so the application must keep the buffer alive for the lifetime of
17 /// the `Bitmap`. This can be used for example with
18 /// `Framebuffer::read_pixels_into_bitmap` to read data directly
19 /// into an application buffer with the specified rowstride.
20 /// ## `context`
21 /// A `Context`
22 /// ## `width`
23 /// The width of the bitmap.
24 /// ## `height`
25 /// The height of the bitmap.
26 /// ## `format`
27 /// The format of the pixel data.
28 /// ## `rowstride`
29 /// The rowstride of the bitmap (the number of bytes from
30 /// the start of one row of the bitmap to the next).
31 /// ## `data`
32 /// A pointer to the data. The bitmap will take ownership of this data.
33 ///
34 /// # Returns
35 ///
36 /// A new `Bitmap`.
37 pub fn new_for_data(
38 context: &Context,
39 width: i32,
40 height: i32,
41 format: PixelFormat,
42 rowstride: i32,
43 data: &[u8],
44 ) -> Bitmap {
45 unsafe {
46 from_glib_full(ffi::cogl_bitmap_new_for_data(
47 context.to_glib_none().0,
48 width,
49 height,
50 format.to_glib(),
51 rowstride,
52 data.to_glib_none().0,
53 ))
54 }
55 }
56
57 //pub fn from_buffer(buffer: /*Unknown conversion*//*Unimplemented*/Buffer, format: PixelFormat, width: i32, height: i32, rowstride: i32, offset: i32) -> Bitmap {
58 // unsafe { TODO: call cogl_sys:cogl_bitmap_new_from_buffer() }
59 //}
60
61 pub fn from_file(filename: &str) -> Result<Bitmap, glib::Error> {
62 unsafe {
63 let mut error = ptr::null_mut();
64 let ret = ffi::cogl_bitmap_new_from_file(filename.to_glib_none().0, &mut error);
65 if error.is_null() {
66 Ok(from_glib_full(ret))
67 } else {
68 Err(from_glib_full(error))
69 }
70 }
71 }
72
73 pub fn with_size(context: &Context, width: u32, height: u32, format: PixelFormat) -> Bitmap {
74 unsafe {
75 from_glib_full(ffi::cogl_bitmap_new_with_size(
76 context.to_glib_none().0,
77 width,
78 height,
79 format.to_glib(),
80 ))
81 }
82 }
83
84 ///
85 /// # Returns
86 ///
87 /// the `PixelBuffer` that this
88 /// buffer uses for storage. Note that if the bitmap was created with
89 /// `Bitmap::new_from_file` then it will not actually be using a
90 /// pixel buffer and this function will return `None`.
91 pub fn get_buffer(&self) -> Option<PixelBuffer> {
92 unsafe { from_glib_none(ffi::cogl_bitmap_get_buffer(self.to_glib_none().0)) }
93 }
94
95 ///
96 /// # Returns
97 ///
98 /// the `PixelFormat` that the data for the bitmap is in.
99 pub fn get_format(&self) -> PixelFormat {
100 unsafe { from_glib(ffi::cogl_bitmap_get_format(self.to_glib_none().0)) }
101 }
102
103 ///
104 /// # Returns
105 ///
106 /// the height of the bitmap
107 pub fn get_height(&self) -> i32 {
108 unsafe { ffi::cogl_bitmap_get_height(self.to_glib_none().0) }
109 }
110
111 ///
112 /// # Returns
113 ///
114 /// the rowstride of the bitmap. This is the number of
115 /// bytes between the address of start of one row to the address of the
116 /// next row in the image.
117 pub fn get_rowstride(&self) -> i32 {
118 unsafe { ffi::cogl_bitmap_get_rowstride(self.to_glib_none().0) }
119 }
120
121 ///
122 /// # Returns
123 ///
124 /// the width of the bitmap
125 pub fn get_width(&self) -> i32 {
126 unsafe { ffi::cogl_bitmap_get_width(self.to_glib_none().0) }
127 }
128
129 /// Parses an image file enough to extract the width and height
130 /// of the bitmap.
131 /// ## `filename`
132 /// the file to check
133 /// ## `width`
134 /// return location for the bitmap width, or `None`
135 /// ## `height`
136 /// return location for the bitmap height, or `None`
137 ///
138 /// # Returns
139 ///
140 /// `true` if the image was successfully parsed
141 pub fn get_size_from_file(filename: &str) -> (bool, i32, i32) {
142 unsafe {
143 let mut width = mem::MaybeUninit::uninit();
144 let mut height = mem::MaybeUninit::uninit();
145 let ret = ffi::cogl_bitmap_get_size_from_file(
146 filename.to_glib_none().0,
147 width.as_mut_ptr(),
148 height.as_mut_ptr(),
149 );
150 let width = width.assume_init();
151 let height = height.assume_init();
152 (ret == crate::TRUE, width, height)
153 }
154 }
155}
156
157impl fmt::Display for Bitmap {
158 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159 write!(f, "Bitmap")
160 }
161}