nx/
bitmap.rs

1// Copyright © 2015-2018, Peter Atashian
2//! Bitmaps in NX files
3use lz4::{decompress};
4
5/// Represents a bitmap
6#[derive(Clone, Copy)]
7pub struct Bitmap<'a> {
8    width: u16,
9    height: u16,
10    data: &'a [u8],
11}
12impl<'a> Bitmap<'a> {
13    /// The width in pixels
14    pub fn width(&self) -> u16 {
15        self.width
16    }
17    /// The height in pixels
18    pub fn height(&self) -> u16 {
19        self.height
20    }
21    /// The length of the data in bytes
22    pub fn len(&self) -> u32 {
23        self.width as u32 * self.height as u32 * 4
24    }
25    /// Creates a `Bitmap` from the supplied data
26    pub unsafe fn construct(data: &'a [u8], width: u16, height: u16) -> Bitmap<'a> {
27        Bitmap { width: width, height: height, data: data }
28    }
29    /// Decompresses the bitmap data into the provided buffer
30    pub fn data(&self, out: &mut [u8]) {
31        assert_eq!(out.len(), self.len() as usize);
32        let len = decompress(self.data, out);
33        assert_eq!(len, Ok(self.len() as usize));
34    }
35}