1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::io::{Error, ErrorKind};
use std::path::Path;

use bindings::ffi;
use bindings::{AsNative, FromNative, CString};
use colors::Color;
use console::{BackgroundFlag, Console};

pub struct Image {
    tcod_image: ffi::TCOD_image_t,
    width: i32,
    height: i32,
}

impl AsNative<ffi::TCOD_image_t> for Image {
    unsafe fn as_native(&self) -> &ffi::TCOD_image_t {
        &self.tcod_image
    }
}

impl FromNative<ffi::TCOD_image_t> for Image {
    unsafe fn from_native(image: ffi::TCOD_image_t) -> Image {
        let (width, height) = get_image_size(image);
        assert!(width != 0);
        Image { tcod_image: image, width: width, height: height }
    }
}

#[inline]
unsafe fn get_image_size(tcod_image: ffi::TCOD_image_t) -> (i32, i32) {
    let (mut width, mut height) = (0, 0);
    ffi::TCOD_image_get_size(tcod_image, &mut width, &mut height);
    (width, height)
}

impl Image {
    pub fn new(width: i32, height: i32) -> Image {
        unsafe {
            Image {
                tcod_image: ffi::TCOD_image_new(width, height),
                width: width,
                height: height,
            }
        }
    }

    pub fn from_file<T>(path: T) -> Result<Image, Error> where T: AsRef<Path> {
        let path_string = CString::new(path.as_ref().to_str().unwrap()).unwrap();
        unsafe {
            let tcod_image = ffi::TCOD_image_load(path_string.as_ptr());
            let (width, height) = get_image_size(tcod_image);

            if width == 0 {
                Err(Error::new(ErrorKind::InvalidInput, "The provided image format is not supported by libtcod"))
            } else {
                Ok(Image { tcod_image: tcod_image, width: width, height: height })
            }
        }
    }

    pub fn from_console<T>(console: &T) -> Image where T: Console {
        unsafe {
            let tcod_image = ffi::TCOD_image_from_console(*console.as_native());
            let (width, height) = get_image_size(tcod_image);
            Image {
                tcod_image: tcod_image,
                width: width,
                height: height
            }
        }
    }

    pub fn refresh_console<T>(&mut self, console: &T) where T: Console {
        assert!(
            {
                let img = Image::from_console(console);
                self.width == img.width && self.height == img.height
            },

            "libtcod only supports console refreshing with consoles of equivalent sizes"
        );

        unsafe {
            ffi::TCOD_image_refresh_console(self.tcod_image, *console.as_native());
        }
    }

    pub fn save<T>(&self, path: T) where T: AsRef<Path> {
        let path_string = CString::new(path.as_ref().to_str().unwrap()).unwrap();
        unsafe {
            ffi::TCOD_image_save(self.tcod_image, path_string.as_ptr());
        }
    }

    pub fn width(&self) -> i32 {
        self.width
    }

    pub fn height(&self) -> i32 {
        self.height
    }

    pub fn get_size(&self) -> (i32, i32) {
        (self.width, self.height)
    }

    pub fn get_pixel(&self, x: i32, y: i32) -> Color {
        assert!(x >= 0 && y >= 0 && x < self.width && y < self.height);
        unsafe {
            FromNative::from_native(ffi::TCOD_image_get_pixel(self.tcod_image, x, y))
        }
    }

    pub fn get_alpha(&self, x: i32, y: i32) -> i32 {
        assert!(x >= 0 && y >= 0 && x < self.width && y < self.height);
        unsafe {
            ffi::TCOD_image_get_alpha(self.tcod_image, x, y)
        }
    }

    pub fn is_pixel_transparent(&self, x: i32, y: i32) -> bool {
        assert!(x >= 0 && y >= 0 && x < self.width && y < self.height);
        unsafe {
            ffi::TCOD_image_is_pixel_transparent(self.tcod_image, x, y) != 0
        }
    }

    pub fn get_mipmap_pixel(&self, (x0, y0): (f32, f32), (x1, y1): (f32, f32)) -> Color {
        assert!(x0 >= 0.0 && y0 >= 0.0 &&
                x0 < x1 && y0 < y1 &&
                x1 < self.width as f32 && y1 < self.height as f32);
        unsafe {
            FromNative::from_native(ffi::TCOD_image_get_mipmap_pixel(self.tcod_image, x0, y0, x1, y1))
        }
    }

    pub fn set_key_color(&mut self, color: Color) {
        unsafe {
            ffi::TCOD_image_set_key_color(self.tcod_image, *color.as_native());
        }
    }

    pub fn clear(&mut self, color: Color) {
        unsafe {
            ffi::TCOD_image_clear(self.tcod_image, *color.as_native());
        }
    }

    pub fn put_pixel(&mut self, x: i32, y: i32, color: Color) {
        assert!(x >= 0 && y >= 0 && x < self.width && y < self.height);
        unsafe {
            ffi::TCOD_image_put_pixel(self.tcod_image, x, y, *color.as_native());
        }
    }

    pub fn scale(&mut self, width: i32, height: i32) {
        unsafe {
            ffi::TCOD_image_scale(self.tcod_image, width, height);
        }
        self.width = width;
        self.height = height;
    }

    pub fn hflip(&mut self) {
        unsafe {
            ffi::TCOD_image_hflip(self.tcod_image);
        }
    }

    pub fn vflip(&mut self) {
        unsafe {
            ffi::TCOD_image_vflip(self.tcod_image);
        }
    }

    pub fn rotate90(&mut self, num_rotations: i32) {
        let (width, height) = unsafe {
            ffi::TCOD_image_rotate90(self.tcod_image, num_rotations);
            get_image_size(self.tcod_image)
        };
        self.width = width;
        self.height = height;
    }

    pub fn invert(&mut self) {
        unsafe {
            ffi::TCOD_image_invert(self.tcod_image);
        }
    }
}

pub fn blit_rect<T>(src: &Image, (width, height): (i32, i32),
                    dst: &mut T, (x, y): (i32, i32), flag: BackgroundFlag) where T: Console {
    assert!(width >= -1 && height >= -1 && width <= src.width && height <= src.height);
    assert!(x >= 0 && y >= 0 && x < dst.width() && y < dst.height());
    unsafe {
        ffi::TCOD_image_blit_rect(src.tcod_image, *dst.as_native(), x, y, width, height, flag as u32);
    }
}

pub fn blit<T>(src: &Image, (scale_x, scale_y): (f32, f32), angle: f32,
               dst: &mut T, (x, y): (f32, f32),  flag: BackgroundFlag) where T: Console {
    assert!(scale_x > 0.0 && scale_y > 0.0);
    assert!(x >= 0.0 && y >= 0.0 && x < dst.width() as f32 && y < dst.height() as f32);
    unsafe {
        ffi::TCOD_image_blit(src.tcod_image, *dst.as_native(), x, y, flag as u32, scale_x, scale_y, angle);
    }
}

pub fn blit_2x<T>(src: &Image, (src_x, src_y): (i32, i32), (width, height): (i32, i32),
                  dst: &mut T,  (dst_x, dst_y): (i32, i32)) where T: Console {
    assert!(width >= -1 && height >= -1 && width <= src.width && height <= src.height);
    assert!(src_x >= 0 && src_y >= 0 && src_x < src.width && src_y < src.height);
    assert!(dst_x >= 0 && dst_y >= 0 && dst_x < dst.width() && dst_y < dst.height());
    unsafe {
        ffi::TCOD_image_blit_2x(src.tcod_image, *dst.as_native(), dst_x, dst_y, src_x, src_y, width, height);
    }
}