1 //! Surface Rotozoomer
2 
3 use libc::c_int;
4 use ::surface::Surface;
5 use ::get_error;
6 pub use std::f64::consts::PI;
7 use sys::gfx::rotozoom;
8 
9 
10 /// `RotozoomSurface` for work with rust-sdl2 Surface type
11 pub trait RotozoomSurface {
12     /// Rotates and zooms a surface and optional anti-aliasing.
rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result<Surface, String>13     fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result<Surface, String>;
14     /// Rotates and zooms a surface with different horizontal and vertical scaling factors and optional anti-aliasing.
rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>15     fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>;
16     /// Zoom a surface by independent horizontal and vertical factors with optional smoothing.
zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>17     fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>;
18     /// Shrink a surface by an integer ratio using averaging.
shrink(&self, factorx: i32, factory: i32) -> Result<Surface, String>19     fn shrink(&self, factorx: i32, factory: i32) -> Result<Surface, String>;
20     /// Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
rotate_90deg(&self, turns: i32) -> Result<Surface, String>21     fn rotate_90deg(&self, turns: i32) -> Result<Surface, String>;
22 }
23 
24 impl<'a> RotozoomSurface for Surface<'a> {
rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result<Surface, String>25     fn rotozoom(&self, angle: f64, zoom: f64, smooth: bool) -> Result<Surface, String> {
26         let raw = unsafe {
27             rotozoom::rotozoomSurface(self.raw(), angle, zoom, smooth as c_int)
28         };
29         if (raw as *mut ()).is_null() {
30             Err(get_error())
31         } else {
32             unsafe { Ok(Surface::from_ll(raw)) }
33         }
34     }
rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>35     fn rotozoom_xy(&self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String> {
36         let raw = unsafe {
37             rotozoom::rotozoomSurfaceXY(self.raw(), angle, zoomx, zoomy, smooth as c_int)
38         };
39         if (raw as *mut ()).is_null() {
40             Err(get_error())
41         } else {
42             unsafe { Ok(Surface::from_ll(raw)) }
43         }
44     }
zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String>45     fn zoom(&self, zoomx: f64, zoomy: f64, smooth: bool) -> Result<Surface, String> {
46         let raw = unsafe {
47             rotozoom::zoomSurface(self.raw(), zoomx, zoomy, smooth as c_int)
48         };
49         if (raw as *mut ()).is_null() {
50             Err(get_error())
51         } else {
52             unsafe { Ok(Surface::from_ll(raw)) }
53         }
54     }
shrink(&self, factorx: i32, factory: i32) -> Result<Surface, String>55     fn shrink(&self, factorx: i32, factory: i32) -> Result<Surface, String> {
56         let raw = unsafe {
57             rotozoom::shrinkSurface(self.raw(), factorx as c_int, factory as c_int)
58         };
59         if (raw as *mut ()).is_null() {
60             Err(get_error())
61         } else {
62             unsafe { Ok(Surface::from_ll(raw)) }
63         }
64     }
rotate_90deg(&self, turns: i32) -> Result<Surface, String>65     fn rotate_90deg(&self, turns: i32) -> Result<Surface, String> {
66         let raw = unsafe {
67             rotozoom::rotateSurface90Degrees(self.raw(), turns as c_int)
68         };
69         if (raw as *mut ()).is_null() {
70             Err(get_error())
71         } else {
72             unsafe { Ok(Surface::from_ll(raw)) }
73         }
74     }
75 }
76 
get_zoom_size(width: i32, height: i32, zoomx: f64, zoomy: f64) -> (i32, i32)77 pub fn get_zoom_size(width: i32, height: i32, zoomx: f64, zoomy: f64) -> (i32, i32) {
78     let mut w: c_int = 0;
79     let mut h: c_int = 0;
80     unsafe { rotozoom::zoomSurfaceSize(width as c_int, height as c_int, zoomx, zoomy, &mut w, &mut h) }
81     (w as i32, h as i32)
82 }
83 
get_rotozoom_size(width: i32, height: i32, angle: f64, zoom: f64) -> (i32, i32)84 pub fn get_rotozoom_size(width: i32, height: i32, angle: f64, zoom: f64) -> (i32, i32) {
85     let mut w: c_int = 0;
86     let mut h: c_int = 0;
87     unsafe { rotozoom::rotozoomSurfaceSize(width as c_int, height as c_int, angle, zoom, &mut w, &mut h) }
88     (w as i32, h as i32)
89 }
90 
get_rotozoom_xy_size(width: i32, height: i32, angle: f64, zoomx: f64, zoomy: f64) -> (i32, i32)91 pub fn get_rotozoom_xy_size(width: i32, height: i32, angle: f64, zoomx: f64, zoomy: f64) -> (i32, i32) {
92     let mut w: c_int = 0;
93     let mut h: c_int = 0;
94     unsafe { rotozoom::rotozoomSurfaceSizeXY(width as c_int, height as c_int, angle, zoomx, zoomy, &mut w, &mut h) }
95     (w as i32, h as i32)
96 }
97