use libc::{c_uint, size_t};
use std::ffi::CString;
use traits::Wrappable;
use sfml_types::Vector2u;
use graphics::{Color, IntRect};
use sfml_types::sfBool;
use csfml_graphics_sys as ffi;
use std::io::{Read, Seek};
use system::inputstream::InputStream;
pub struct Image {
image: *mut ffi::sfImage
}
impl Image {
pub fn new(width: u32, height: u32) -> Option<Image> {
let image = unsafe { ffi::sfImage_create(width as c_uint,
height as c_uint) };
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn new_from_stream<T: Read + Seek>(stream: &mut T) -> Option<Image> {
let mut input_stream = InputStream::new(stream);
let image = unsafe { ffi::sfImage_createFromStream(&mut input_stream.0) };
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn new_from_memory(mem: &[u8]) -> Option<Image> {
let image = unsafe { ffi::sfImage_createFromMemory(&mem[0], mem.len() as size_t) };
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn new_from_color(width: u32,
height: u32,
color: &Color) -> Option<Image> {
let image =
unsafe { ffi::sfImage_createFromColor(width as c_uint,
height as c_uint, color.0) };
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn new_from_file(filename: &str) -> Option<Image> {
let c_filename = CString::new(filename.as_bytes()).unwrap().as_ptr();
let image = unsafe {
ffi::sfImage_createFromFile(c_filename)
};
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn clone_opt(&self) -> Option<Image> {
let image = unsafe { ffi::sfImage_copy(self.image) };
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn create_from_pixels(width: u32,
height: u32,
pixels: &[u8]) -> Option<Image> {
let image =
unsafe { ffi::sfImage_createFromPixels(width as c_uint,
height as c_uint,
pixels.as_ptr()) };
if image.is_null() {
None
} else {
Some(Image {
image: image
})
}
}
pub fn save_to_file(&self, filename: &str) -> bool {
let c_str = CString::new(filename.as_bytes()).unwrap().as_ptr();
unsafe { ffi::sfImage_saveToFile(self.image, c_str) }.to_bool()
}
pub fn get_size(&self) -> Vector2u {
unsafe {
ffi::sfImage_getSize(self.image)
}
}
pub fn create_mask_from_color(&self, color: &Color, alpha: u8) {
unsafe {
ffi::sfImage_createMaskFromColor(self.image, color.0, alpha)
}
}
pub fn set_pixel(&mut self, x: u32, y: u32, color: &Color) {
unsafe {
ffi::sfImage_setPixel(self.image, x as c_uint, y as c_uint, color.0)
}
}
pub fn get_pixel(&self, x: u32, y: u32) -> Color {
unsafe {
Color(ffi::sfImage_getPixel(self.image, x as c_uint, y as c_uint))
}
}
pub fn flip_horizontally(&mut self) {
unsafe {
ffi::sfImage_flipHorizontally(self.image)
}
}
pub fn flip_vertically(&mut self) {
unsafe {
ffi::sfImage_flipVertically(self.image)
}
}
pub fn copy_image(&mut self,
source: &Image,
dest_x: u32,
dest_y: u32,
source_rect: &IntRect,
apply_alpha: bool) {
unsafe {
ffi::sfImage_copyImage(self.image,
source.unwrap(),
dest_x as c_uint,
dest_y as c_uint,
*source_rect,
sfBool::from_bool(apply_alpha))
}
}
}
impl Clone for Image {
fn clone(&self) -> Image {
let image = unsafe { ffi::sfImage_copy(self.image) };
if image.is_null() {
panic!("Not enough memory to clone Image")
} else {
Image {
image: image
}
}
}
}
impl Wrappable<*mut ffi::sfImage> for Image {
fn wrap(image: *mut ffi::sfImage) -> Image {
Image {
image: image
}
}
fn unwrap(&self) -> *mut ffi::sfImage {
self.image
}
}
impl Drop for Image {
fn drop(&mut self) {
unsafe {
ffi::sfImage_destroy(self.image)
}
}
}