busybar-render 0.0.9

Re-encodes BUSY Bar screen frames into more common image formats
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
use std::fmt;
use std::io::Cursor;
use std::path::Path;

use base64::Engine as _;
use base64::engine::general_purpose::STANDARD;
use image::{DynamicImage, GrayImage, RgbImage};

#[derive(Debug, thiserror::Error)]
pub enum RenderError {
    #[error("{width}x{height} in {layout} needs {expected} bytes, but {actual} were given")]
    UnexpectedSize {
        width: u32,
        height: u32,
        layout: PixelLayout,
        expected: usize,
        actual: usize,
    },

    #[error("a {width}x{height} image does not fit a raster of {pixel} with a {gap} gap")]
    RasterTooLarge {
        width: u32,
        height: u32,
        pixel: u32,
        gap: u32,
    },

    #[error("could not encode a {width}x{height} image as {format}")]
    Encode {
        width: u32,
        height: u32,
        format: ImageFormat,
        #[source]
        source: image::ImageError,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageFormat {
    Bmp,
    Jpeg,
    Png,
}

impl ImageFormat {
    pub fn from_path(path: &Path) -> Option<Self> {
        let extension = path.extension()?.to_str()?.to_ascii_lowercase();

        match extension.as_str() {
            "bmp" => Some(Self::Bmp),
            "jpg" | "jpeg" => Some(Self::Jpeg),
            "png" => Some(Self::Png),
            _ => None,
        }
    }

    fn encoding(self) -> image::ImageFormat {
        match self {
            Self::Bmp => image::ImageFormat::Bmp,
            Self::Jpeg => image::ImageFormat::Jpeg,
            Self::Png => image::ImageFormat::Png,
        }
    }
}

impl fmt::Display for ImageFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Bmp => f.write_str("bmp"),
            Self::Jpeg => f.write_str("jpeg"),
            Self::Png => f.write_str("png"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Raster {
    pixel: u32,
    gap: u32,
}

impl Raster {
    pub fn new(pixel: u32, gap: u32) -> Option<Self> {
        (pixel > 0).then_some(Self { pixel, gap })
    }

    pub fn pixel(self) -> u32 {
        self.pixel
    }

    pub fn gap(self) -> u32 {
        self.gap
    }

    fn extent(self, count: u32) -> Option<u32> {
        let cells = count.checked_mul(self.pixel)?;
        let gaps = count.saturating_sub(1).checked_mul(self.gap)?;

        cells.checked_add(gaps)
    }
}

impl Default for Raster {
    fn default() -> Self {
        Self { pixel: 3, gap: 1 }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PixelLayout {
    Rgb888,
    Bgr888,
    Gray8,
    Gray4,
}

impl PixelLayout {
    fn byte_len(self, width: u32, height: u32) -> usize {
        let pixels = (width as usize) * (height as usize);

        match self {
            Self::Rgb888 | Self::Bgr888 => pixels * 3,
            Self::Gray8 => pixels,
            Self::Gray4 => pixels.div_ceil(2),
        }
    }
}

impl fmt::Display for PixelLayout {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Rgb888 => f.write_str("rgb888"),
            Self::Bgr888 => f.write_str("bgr888"),
            Self::Gray8 => f.write_str("l8"),
            Self::Gray4 => f.write_str("l4"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct RawImage {
    width: u32,
    height: u32,
    image: DynamicImage,
}

impl RawImage {
    pub fn new(
        width: u32,
        height: u32,
        layout: PixelLayout,
        pixels: &[u8],
    ) -> Result<Self, RenderError> {
        let expected = layout.byte_len(width, height);

        if pixels.len() != expected {
            return Err(RenderError::UnexpectedSize {
                width,
                height,
                layout,
                expected,
                actual: pixels.len(),
            });
        }

        let image = match layout {
            PixelLayout::Rgb888 => DynamicImage::ImageRgb8(
                RgbImage::from_raw(width, height, pixels.to_vec()).expect("checked above"),
            ),
            PixelLayout::Bgr888 => {
                let mut pixels = pixels.to_vec();

                // The device says it outputs RGB but it really doesn't, so we swap
                for pixel in pixels.chunks_exact_mut(3) {
                    pixel.swap(0, 2);
                }

                DynamicImage::ImageRgb8(
                    RgbImage::from_raw(width, height, pixels).expect("checked above"),
                )
            }
            PixelLayout::Gray8 => DynamicImage::ImageLuma8(
                GrayImage::from_raw(width, height, pixels.to_vec()).expect("checked above"),
            ),
            PixelLayout::Gray4 => {
                let mut expanded = Vec::with_capacity(pixels.len() * 2);

                for byte in pixels {
                    expanded.push((byte >> 4) * 17);
                    expanded.push((byte & 0x0F) * 17);
                }

                expanded.truncate((width as usize) * (height as usize));

                DynamicImage::ImageLuma8(
                    GrayImage::from_raw(width, height, expanded).expect("checked above"),
                )
            }
        };

        Ok(Self {
            width,
            height,
            image,
        })
    }

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

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

    pub fn with_raster(&self, raster: Raster) -> Result<Self, RenderError> {
        let too_large = || RenderError::RasterTooLarge {
            width: self.width,
            height: self.height,
            pixel: raster.pixel,
            gap: raster.gap,
        };

        let width = raster.extent(self.width).ok_or_else(too_large)?;
        let height = raster.extent(self.height).ok_or_else(too_large)?;

        let step = raster.pixel + raster.gap;

        let image = match &self.image {
            DynamicImage::ImageLuma8(source) => {
                let mut target = GrayImage::new(width, height);
                self::paint(source, &mut target, step, raster.pixel);
                DynamicImage::ImageLuma8(target)
            }
            source => {
                let source = source.to_rgb8();
                let mut target = RgbImage::new(width, height);
                self::paint(&source, &mut target, step, raster.pixel);
                DynamicImage::ImageRgb8(target)
            }
        };

        Ok(Self {
            width,
            height,
            image,
        })
    }

    pub fn encode(&self, format: ImageFormat) -> Result<Vec<u8>, RenderError> {
        let mut buffer = Cursor::new(Vec::new());

        self.image
            .write_to(&mut buffer, format.encoding())
            .map_err(|source| RenderError::Encode {
                width: self.width,
                height: self.height,
                format,
                source,
            })?;

        Ok(buffer.into_inner())
    }

    pub fn encode_base64(&self, format: ImageFormat) -> Result<String, RenderError> {
        self.encode(format).map(|bytes| STANDARD.encode(bytes))
    }
}

fn paint<P: image::Pixel<Subpixel=u8>>(
    source: &image::ImageBuffer<P, Vec<u8>>,
    target: &mut image::ImageBuffer<P, Vec<u8>>,
    step: u32,
    size: u32,
) {
    for (x, y, pixel) in source.enumerate_pixels() {
        for offset_y in 0..size {
            for offset_x in 0..size {
                target.put_pixel(x * step + offset_x, y * step + offset_y, *pixel);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn picks_a_format_from_the_file_extension() {
        assert_eq!(
            ImageFormat::from_path(Path::new("frame.bmp")),
            Some(ImageFormat::Bmp)
        );
        assert_eq!(
            ImageFormat::from_path(Path::new("frame.JPEG")),
            Some(ImageFormat::Jpeg)
        );
        assert_eq!(
            ImageFormat::from_path(Path::new("./out/frame.png")),
            Some(ImageFormat::Png)
        );
        assert_eq!(ImageFormat::from_path(Path::new("frame.raw")), None);
        assert_eq!(ImageFormat::from_path(Path::new("frame")), None);
    }

    #[test]
    fn reads_rgb_in_the_order_it_is_given() {
        let image = RawImage::new(1, 1, PixelLayout::Rgb888, &[0x11, 0x22, 0x33]).unwrap();

        assert_eq!(
            image.image.as_rgb8().unwrap().get_pixel(0, 0).0,
            [0x11, 0x22, 0x33]
        );
    }

    #[test]
    fn swaps_the_channels_of_a_bgr_buffer() {
        let image = RawImage::new(1, 1, PixelLayout::Bgr888, &[0x11, 0x22, 0x33]).unwrap();

        assert_eq!(
            image.image.as_rgb8().unwrap().get_pixel(0, 0).0,
            [0x33, 0x22, 0x11]
        );
    }

    #[test]
    fn expands_four_bit_grayscale_to_eight() {
        let image = RawImage::new(2, 1, PixelLayout::Gray4, &[0xf0]).unwrap();
        let luma = image.image.as_luma8().unwrap();

        assert_eq!(luma.get_pixel(0, 0).0, [0xff]);
        assert_eq!(luma.get_pixel(1, 0).0, [0x00]);
    }

    #[test]
    fn rejects_a_buffer_which_does_not_match_the_geometry() {
        let error = RawImage::new(72, 16, PixelLayout::Rgb888, &[0; 16]).unwrap_err();

        assert_eq!(
            error.to_string(),
            "72x16 in rgb888 needs 3456 bytes, but 16 were given"
        );
    }

    #[test]
    fn encodes_every_format_it_supports() {
        let image = RawImage::new(2, 2, PixelLayout::Gray8, &[0, 64, 128, 255]).unwrap();

        assert!(image.encode(ImageFormat::Bmp).unwrap().starts_with(b"BM"));
        assert!(
            image
                .encode(ImageFormat::Jpeg)
                .unwrap()
                .starts_with(&[0xff, 0xd8])
        );
        assert!(
            image
                .encode(ImageFormat::Png)
                .unwrap()
                .starts_with(b"\x89PNG")
        );
    }

    #[test]
    fn a_raster_spaces_the_pixels_out_and_leaves_black_between_them() {
        let image = RawImage::new(
            2,
            2,
            PixelLayout::Rgb888,
            &[
                0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
            ],
        )
            .unwrap();

        let raster = Raster::new(3, 1).unwrap();
        let rastered = image.with_raster(raster).unwrap();

        assert_eq!((rastered.width(), rastered.height()), (7, 7));

        let pixels = rastered.image.to_rgb8();

        assert_eq!(pixels.get_pixel(0, 0).0, [0xff, 0x00, 0x00]);
        assert_eq!(pixels.get_pixel(2, 2).0, [0xff, 0x00, 0x00]);
        assert_eq!(pixels.get_pixel(3, 0).0, [0x00, 0x00, 0x00]);
        assert_eq!(pixels.get_pixel(0, 3).0, [0x00, 0x00, 0x00]);
        assert_eq!(pixels.get_pixel(4, 0).0, [0x00, 0xff, 0x00]);
        assert_eq!(pixels.get_pixel(4, 4).0, [0xff, 0xff, 0xff]);
    }

    #[test]
    fn a_raster_keeps_a_grayscale_image_grayscale() {
        let image = RawImage::new(2, 1, PixelLayout::Gray8, &[0xff, 0x40]).unwrap();
        let rastered = image.with_raster(Raster::default()).unwrap();

        assert_eq!((rastered.width(), rastered.height()), (7, 3));

        let pixels = rastered.image.as_luma8().expect("stays grayscale");

        assert_eq!(pixels.get_pixel(0, 0).0, [0xff]);
        assert_eq!(pixels.get_pixel(3, 0).0, [0x00]);
        assert_eq!(pixels.get_pixel(4, 0).0, [0x40]);
    }

    #[test]
    fn a_raster_needs_a_pixel_size() {
        assert!(Raster::new(0, 1).is_none());
        assert!(Raster::new(1, 0).is_some());
    }

    #[test]
    fn encodes_to_base64() {
        let image = RawImage::new(1, 1, PixelLayout::Gray8, &[0x7f]).unwrap();
        let encoded = image.encode_base64(ImageFormat::Png).unwrap();

        assert_eq!(
            STANDARD.decode(&encoded).unwrap(),
            image.encode(ImageFormat::Png).unwrap()
        );
    }
}