lapix 0.1.1

Image editor backend library for pixel art
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::color::TRANSPARENT;
use crate::{graphics, Bitmap, Color, FreeImage, Point, Rect, Size};
use serde::{Deserialize, Serialize};

/// Effects that certain actions can have on the canvas
#[derive(Debug, Clone, Copy)]
pub enum CanvasEffect {
    /// Action does not affect the canvas at all
    None,
    /// Action forces the canvas to be updated
    Update,
    /// Action forces the canvas to be recreated
    New,
    /// Action forces the layers to be updated
    Layer,
}

/// The canvas is the area where drawing can take place. Each layer has a
/// canvas, and the canvas in turn holds an image internally to represent the
/// drawing on it.
#[derive(Debug, Serialize, Deserialize)]
pub struct Canvas<IMG> {
    inner: IMG,
}

impl<IMG: Bitmap> Canvas<IMG> {
    /// Create a new blank canvas with a specified size
    pub fn new(size: Size<i32>) -> Self {
        Self {
            inner: IMG::new(size, TRANSPARENT),
        }
    }

    /// Get the inner image held by this canvas
    pub fn inner(&self) -> &IMG {
        &self.inner
    }

    /// Get the size of the canvas in pixels. This is the amount of pixels of
    /// the underlying image, not the real screen-size that the canvas takes up
    /// when displayed.
    pub fn size(&self) -> Size<i32> {
        self.inner.size()
    }

    /// Get the width of the canvas in pixels (width of the underlying image,
    /// not real screen-size)
    pub fn width(&self) -> i32 {
        self.inner.width()
    }

    /// Get the height of the canvas in pixels (height of the underlying image)
    pub fn height(&self) -> i32 {
        self.inner.height()
    }

    /// Get the rectangle representing the canvas dimensions, starting from the
    /// origin at (0, 0).
    pub fn rect(&self) -> Rect<i32> {
        Rect::new(0, 0, self.width(), self.height())
    }

    /// Get a representation of the inner image as a slice of bytes
    pub fn bytes(&self) -> &[u8] {
        self.inner.bytes()
    }

    /// Check whether a point is inside the canvas
    pub fn is_in_bounds(&self, p: Point<i32>) -> bool {
        p.x >= 0 && p.y >= 0 && p.x < self.width() && p.y < self.height()
    }

    /// Set the image of the canvas with a predefined one
    pub fn set_img(&mut self, img: IMG) {
        self.inner = img;
    }

    /// Take the inner image replacing it with a dummy empty one.
    pub fn take_inner(&mut self) -> IMG {
        std::mem::replace(&mut self.inner, IMG::new(Size::new(0, 0), TRANSPARENT))
    }

    /// Set all pixels of the canvas to transparent
    pub fn clear(&mut self) -> IMG {
        let size = self.size();
        std::mem::replace(&mut self.inner, IMG::new(size, TRANSPARENT))
    }

    /// Resize the canvas, keeping the content that is in bounds and in case of
    /// an increase in one of the dimensions, set new pixels to transparent.
    pub fn resize(&mut self, size: Size<i32>) -> IMG {
        let new_img = IMG::new(size, TRANSPARENT);
        let old_img = std::mem::replace(&mut self.inner, new_img);
        self.inner.set_from(&old_img);

        old_img
    }

    /// Get the color of a pixel in a certain position in the canvas
    pub fn pixel(&self, p: Point<i32>) -> Color {
        self.inner.pixel(p)
    }

    /// Set the color of a pixel in a certain position in the canvas. If there
    /// was an actual change, return the data needed for a reversal, that is,
    /// which point needs to be set to which color to reverse the action.
    pub fn set_pixel(&mut self, p: Point<i32>, color: Color) -> Option<(Point<i32>, Color)> {
        if self.is_in_bounds(p) {
            let old = self.inner.pixel(p);

            if color == old {
                return None;
            }

            self.inner.set_pixel(p, color);
            return Some((p, old));
        }

        None
    }

    /// Draw a line between two points in the canvas with a certain color.
    /// Returns a set of reversals (points and the colors they need to be set to
    /// in order to reverse the action).
    pub fn line(
        &mut self,
        p1: Point<i32>,
        p2: Point<i32>,
        color: Color,
    ) -> Vec<(Point<i32>, Color)> {
        let line = graphics::line(p1, p2);
        let mut reversals = Vec::new();

        for p in line {
            if let Some(action) = self.set_pixel(p, color) {
                reversals.push(action);
            }
        }
        reversals
    }

    /// Draw a rectangle (outline) between two points in the canvas with a
    /// certain color. Returns a set of reversals (points and the colors they
    /// need to be set to in order to reverse the action).
    pub fn rectangle(
        &mut self,
        p1: Point<i32>,
        p2: Point<i32>,
        color: Color,
    ) -> Vec<(Point<i32>, Color)> {
        let rect = graphics::rectangle(p1, p2);
        let mut reversals = Vec::new();

        for p in rect {
            if let Some(action) = self.set_pixel(p, color) {
                reversals.push(action);
            }
        }
        reversals
    }

    /// Set an area of the canvas (determined by a rectangle) to a certain
    /// color. Returns a set of reversals (points and colors they need to be set
    /// to in order to reverse the action).
    pub fn set_area(&mut self, area: Rect<i32>, color: Color) -> Vec<(Point<i32>, Color)> {
        let mut reversals = Vec::new();

        for i in 0..area.w {
            for j in 0..area.h {
                if let Some(action) = self.set_pixel((i + area.x, j + area.y).into(), color) {
                    reversals.push(action);
                }
            }
        }

        reversals
    }

    /// Paste a free image into the canvas, overriding the contents that existed
    /// below that area. Returns a set of reversals (points and colors they need
    /// to be set to in order to reverse the action).
    pub fn paste_obj(&mut self, obj: &FreeImage<IMG>) -> Vec<(Point<i32>, Color)> {
        let mut reversals = Vec::new();
        for i in 0..obj.rect.w {
            for j in 0..obj.rect.h {
                let ij = Point::new(i, j);
                let color = obj.texture.pixel(ij);
                let p = ij + obj.rect.pos();

                if self.is_in_bounds(p) {
                    let blended = color.blend_over(self.pixel(p));
                    if let Some(action) = self.set_pixel(p, blended) {
                        reversals.push(action);
                    }
                }
            }
        }

        reversals
    }

    /// Paint an enclosed area with a certain color. Returns a set of reversals
    /// (points and colors they need to be set to in order to reverse the
    /// action).
    pub fn bucket(&mut self, p: Point<i32>, color: Color) -> Vec<(Point<i32>, Color)> {
        let old_color = self.inner.pixel(p);

        if color == old_color {
            return Vec::new();
        }

        let w = self.inner.width() as usize;
        let h = self.inner.height() as usize;

        let mut marked = vec![false; w * h];
        let mut visit = vec![(p.x, p.y)];

        let mut reversals = Vec::new();

        loop {
            if visit.is_empty() {
                break;
            }

            let mut new_visit = Vec::new();
            while let Some((vx, vy)) = visit.pop() {
                marked[(vy as usize) * w + vx as usize] = true;

                if let Some(action) = self.set_pixel((vx, vy).into(), color) {
                    reversals.push(action);
                }

                for (nx, ny) in self.neighbors(vx, vy).into_iter().flatten() {
                    let ind = (ny as usize) * w + nx as usize;
                    if self.inner.pixel((nx, ny).into()) == old_color && !marked[ind] {
                        new_visit.push((nx, ny));
                        marked[ind] = true;
                    }
                }
            }

            visit.append(&mut new_visit);
        }

        reversals
    }

    fn neighbors(&self, x: i32, y: i32) -> [Option<(i32, i32)>; 4] {
        let mut neighbors = [None; 4];
        let w = self.inner.width();
        let h = self.inner.height();

        if x + 1 < w {
            neighbors[0] = Some((x + 1, y));
        }
        if x > 0 {
            neighbors[1] = Some((x - 1, y));
        }
        if y + 1 < h {
            neighbors[2] = Some((x, y + 1));
        }
        if y > 0 {
            neighbors[3] = Some((x, y - 1));
        }

        neighbors
    }

    /// Get an image from a certain area of the canvas (determined by a
    /// rectangle).
    pub fn img_from_area(&self, area: Rect<i32>) -> IMG {
        let mut img = IMG::new((area.w, area.h).into(), TRANSPARENT);

        for i in 0..area.w {
            for j in 0..area.h {
                let ij = Point::new(i, j);
                let p = area.pos() + ij;

                if p.x < self.width() && p.y < self.height() {
                    let color = self.pixel(p);
                    img.set_pixel(ij, color);
                }
            }
        }

        img
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bitmap::TestImage;
    use crate::color::TRANSPARENT;
    use test_case::test_case;

    #[test]
    fn basic_properties() {
        let canvas = Canvas::<TestImage>::new(Size::new(5, 10));

        assert_eq!(canvas.width(), 5);
        assert_eq!(canvas.height(), 10);
        assert_eq!(canvas.size(), Size::new(5, 10));
        assert_eq!(canvas.rect(), Rect::new(0, 0, 5, 10));
    }

    #[test_case((1, 1), (0, 0), true)]
    #[test_case((1, 1), (1, 0), false)]
    #[test_case((1, 1), (0, 1), false)]
    #[test_case((2, 2), (0, 0), true)]
    #[test_case((2, 2), (0, 1), true)]
    #[test_case((2, 2), (1, 0), true)]
    #[test_case((2, 2), (1, 1), true)]
    #[test_case((2, 2), (-1, 0), false)]
    #[test_case((2, 2), (1, 2), false)]
    fn is_in_bounds(size: impl Into<Size<i32>>, p: impl Into<Point<i32>>, res: bool) {
        let canvas = Canvas::<TestImage>::new(size.into());
        assert_eq!(canvas.is_in_bounds(p.into()), res);
    }

    #[test]
    fn resize_same() {
        let black = Color::new(0, 0, 0, 255);
        let mut canvas = Canvas::<TestImage>::new(Size::new(1, 2));
        canvas.set_pixel(Point::new(0, 0), black);
        canvas.set_pixel(Point::new(0, 1), black);
        canvas.resize(Size::new(1, 2));

        assert_eq!(canvas.size(), Size::new(1, 2));
        assert_eq!(canvas.pixel(Point::new(0, 0)), black);
        assert_eq!(canvas.pixel(Point::new(0, 1)), black);
    }

    #[test]
    fn resize_smaller() {
        let black = Color::new(0, 0, 0, 255);
        let mut canvas = Canvas::<TestImage>::new(Size::new(2, 2));
        canvas.set_pixel(Point::new(0, 0), black);
        canvas.set_pixel(Point::new(0, 1), black);
        canvas.set_pixel(Point::new(1, 0), black);
        canvas.set_pixel(Point::new(1, 1), black);
        canvas.resize(Size::new(1, 1));

        assert_eq!(canvas.size(), Size::new(1, 1));
        assert_eq!(canvas.pixel(Point::new(0, 0)), black);
    }

    #[test]
    fn resize_bigger() {
        let black = Color::new(0, 0, 0, 255);
        let mut canvas = Canvas::<TestImage>::new(Size::new(1, 1));
        canvas.set_pixel(Point::new(0, 0), black);
        canvas.resize(Size::new(2, 1));

        assert_eq!(canvas.size(), Size::new(2, 1));
        assert_eq!(canvas.pixel(Point::new(0, 0)), black);
        assert_eq!(canvas.pixel(Point::new(1, 0)), TRANSPARENT);
    }

    fn assert_points(canvas: &Canvas<TestImage>, points: &[(i32, i32)]) {
        let black = Color::new(0, 0, 0, 255);
        for i in 0..canvas.width() {
            for j in 0..canvas.height() {
                let color = if points.contains(&(i, j)) {
                    black
                } else {
                    TRANSPARENT
                };
                assert_eq!(canvas.pixel(Point::new(i, j)), color);
            }
        }
    }

    #[test_case((0, 0), (0, 0), vec![(0, 0)])]
    #[test_case((0, 0), (0, 1), vec![(0, 0), (0, 1)])]
    #[test_case((0, 1), (0, 0), vec![(0, 0), (0, 1)])]
    #[test_case((0, 0), (2, 2), vec![(0, 0), (1, 1), (2, 2)])]
    fn line<P: Into<Point<i32>>>(p: P, q: P, line: Vec<(i32, i32)>) {
        let mut canvas = Canvas::<TestImage>::new(Size::new(5, 5));
        let black = Color::new(0, 0, 0, 255);
        canvas.line(p.into(), q.into(), black);
        assert_points(&canvas, &line);
    }

    #[test]
    fn rect() {
        let mut canvas = Canvas::<TestImage>::new(Size::new(5, 5));
        let black = Color::new(0, 0, 0, 255);
        canvas.rectangle((0, 0).into(), (2, 2).into(), black);

        assert_points(
            &canvas,
            &[
                (0, 0),
                (0, 1),
                (0, 2),
                (1, 0),
                (1, 2),
                (2, 0),
                (2, 1),
                (2, 2),
            ],
        );
    }

    #[test]
    fn bucket() {
        let mut canvas = Canvas::<TestImage>::new(Size::new(5, 5));
        let black = Color::new(0, 0, 0, 255);
        canvas.set_pixel(Point::new(0, 1), black);
        canvas.set_pixel(Point::new(1, 0), black);
        canvas.set_pixel(Point::new(2, 0), black);
        canvas.set_pixel(Point::new(1, 2), black);
        canvas.set_pixel(Point::new(2, 2), black);
        canvas.set_pixel(Point::new(3, 1), black);
        canvas.bucket(Point::new(1, 1), black);
        assert_points(
            &canvas,
            &[
                (0, 1),
                (1, 0),
                (2, 0),
                (1, 2),
                (2, 2),
                (3, 1),
                (1, 1),
                (2, 1),
            ],
        );
    }
}