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
//! Constructs for generating images using the TIS-100.

/// The colors that the TIS-100 can generate.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Color {
    Black,
    DarkGrey,
    BrightGrey,
    White,
    Red,
}

impl Color {
    /// Get the color for the given integer representation.
    fn from_isize(value: isize) -> Color {
        match value {
            1 => DarkGrey,
            2 => BrightGrey,
            3 => White,
            4 => Red,
            _ => Black,
        }
    }
}

use self::Color::*;

/// The operational modes of the image.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum ImageMode {
    Move,
    Paint,
}

use self::ImageMode::*;

/// An image that can receive values from the TIS-100. When in the `Move` mode, the image receives
/// coordinates that tell it where to draw. When in the `Paint` mode, the image will draw values.
/// Sending a negative value at any time will reset the image to `Move` mode.
#[derive(Debug)]
pub struct Image {
    width: usize,
    height: usize,
    data: Vec<Color>,
    mode: ImageMode,
    position: Vec<isize>,
    offset: usize,
}

impl Image {
    /// Construct a new, empty `Image` with the given width and height.
    pub fn new(width: usize, height: usize) -> Image {
        let mut data = Vec::with_capacity(width * height);
        for _ in 0..width * height {
            data.push(Black);
        }

        Image {
            width: width,
            height: height,
            data: data,
            mode: Move,
            position: Vec::new(),
            offset: 0,
        }
    }

    /// Construct a new `Image` with the given width, height, and initial values.
    pub fn with_data(data: &Vec<isize>, width: usize, height: usize) -> Image {
        assert_eq!(data.len(), width * height);

        let data = data.iter().map(|&i| Color::from_isize(i)).collect::<Vec<_>>();

        Image {
            width: width,
            height: height,
            data: data,
            mode: Move,
            position: Vec::new(),
            offset: 0,
        }
    }

    /// Retrieve the image's data.
    pub fn data(&self) -> &Vec<Color> {
        &self.data
    }

    /// Write a value to the image. If the image is in `Move` mode, then the value will be
    /// interpreted as a coordinate. If the image is in `Paint` mode, then the value will be
    /// interpreted as a color unless the value is negative.
    pub fn write(&mut self, value: isize) {
        if value < 0 {
            self.position.clear();
            self.mode = Move;
            self.offset = 0;
            return;
        }

        if self.mode == Move {
            self.position.push(value);

            if self.position.len() == 2 {
                self.mode = Paint;
            }

            return;
        }

        let row_off = self.position[0] as usize * self.width;

        if row_off < self.width * self.height {
            let col = self.position[1] as usize + self.offset;

            if col < self.width {
                self.data[row_off + col] = Color::from_isize(value);
            }
        }

        self.offset += 1;
    }
}

impl PartialEq for Image {
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data
    }
}

impl Eq for Image {}

#[test]
fn test_color_from_isize() {
    assert_eq!(Color::from_isize(0), Black);
    assert_eq!(Color::from_isize(1), DarkGrey);
    assert_eq!(Color::from_isize(2), BrightGrey);
    assert_eq!(Color::from_isize(3), White);
    assert_eq!(Color::from_isize(4), Red);
    assert_eq!(Color::from_isize(5), Black);
}

#[test]
fn test_image_with_data() {
    let expected = vec![DarkGrey, BrightGrey, White, Red];
    let data = vec![1, 2, 3, 4];
    let image = Image::with_data(&data, 2, 2);

    assert_eq!(expected, image.data().clone());
}

#[test]
fn test_image_write() {
    let expected = vec![DarkGrey, BrightGrey, White, Red];
    let mut image = Image::new(2, 2);

    image.write(0);
    image.write(0);
    image.write(1);
    image.write(2);
    image.write(-1);
    image.write(1);
    image.write(0);
    image.write(3);
    image.write(4);
    image.write(-1);

    assert_eq!(expected, image.data().clone());
}