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
use sc2_proto::common;

use {FromProto, Result};

/// Data used to sample the current state of the map at certain points.
#[derive(Debug, Clone)]
pub struct ImageData {
    bits_per_pixel: u32,
    data: Vec<u8>,
    dimensions: (u32, u32),
}

impl ImageData {
    /// Number of bits to interpret as a pixel in the data.
    pub fn get_bpp(&self) -> u32 {
        self.bits_per_pixel
    }

    /// Raw image data.
    pub fn get_raw_data(&self) -> &[u8] {
        &self.data
    }

    /// Dimensions of the image.
    pub fn get_dimensions(&self) -> (u32, u32) {
        self.dimensions
    }
}

impl FromProto<common::ImageData> for ImageData {
    fn from_proto(mut data: common::ImageData) -> Result<Self> {
        Ok(Self {
            bits_per_pixel: data.get_bits_per_pixel() as u32,
            data: data.take_data(),
            dimensions: (
                data.get_size().get_x() as u32,
                data.get_size().get_y() as u32,
            ),
        })
    }
}