vzense-rust 0.4.2

High-level library for Vzense cameras
Documentation
//! Draw ASCII text to a `u8` image buffer.

pub fn no_ir_info(buffer: &mut [u8]) {
    draw_string(buffer, 640, 60, 200, 3, 159, "IR NOT ACTIVATED FOR DCAM560");
}

pub fn no_depth_info(buffer: &mut [u8]) {
    draw_string(
        buffer,
        640,
        40,
        200,
        3,
        255,
        "DEPTH NOT ACTIVATED FOR DCAM560",
    );
}

/// Draw ASCII text to a `u8` image buffer. `width` is the image width, (`x,y`) the start position.
pub fn draw_string(
    buffer: &mut [u8],
    width: usize,
    x: usize,
    y: usize,
    scale: usize,
    brightness: u8,
    text: &str,
) {
    let mut current_x = x;
    for c in text.chars() {
        draw_char(buffer, width, current_x, y, scale, brightness, c);
        // Move to the next character position, considering the scale
        current_x += (5 * scale) + scale; // (width of char + 1-pixel space) * scale
    }
}

fn draw_char(
    buffer: &mut [u8],
    width: usize,
    x: usize,
    y: usize,
    scale: usize,
    brightness: u8,
    character: char,
) {
    if !character.is_ascii() {
        return;
    }

    let font_index = character as usize - 32;
    if font_index >= FONT.len() {
        return;
    }

    let font_char = FONT[font_index];

    for row in 0..7 {
        for (col, char) in font_char.iter().enumerate() {
            // Check if the pixel is "on" in the font definition
            if (char >> row) & 0x01 == 1 {
                // Now, draw a scale x scale block of pixels
                for sub_row in 0..scale {
                    for sub_col in 0..scale {
                        let pixel_x = x + (col * scale) + sub_col;
                        let pixel_y = y + (row * scale) + sub_row;

                        // Calculate the index in the 1D buffer
                        let pixel_index = pixel_y * width + pixel_x;

                        if pixel_index < buffer.len() {
                            buffer[pixel_index] = brightness;
                        }
                    }
                }
            }
        }
    }
}

// ascii characters for 5x7 font
const FONT: [[u8; 5]; 95] = [
    // 32:   (Space)
    [0x00, 0x00, 0x00, 0x00, 0x00],
    // 33: !
    [0x00, 0x00, 0x5f, 0x00, 0x00],
    // 34: "
    [0x00, 0x03, 0x00, 0x03, 0x00],
    // 35: #
    [0x14, 0x7e, 0x14, 0x7e, 0x14],
    // 36: $
    [0x24, 0x2a, 0x7f, 0x2a, 0x12],
    // 37: %
    [0x23, 0x13, 0x08, 0x64, 0x64],
    // 38: &
    [0x36, 0x49, 0x56, 0x20, 0x50],
    // 39: '
    [0x00, 0x03, 0x03, 0x00, 0x00],
    // 40: (
    [0x00, 0x1c, 0x22, 0x41, 0x00],
    // 41: )
    [0x00, 0x41, 0x22, 0x1c, 0x00],
    // 42: *
    [0x08, 0x2a, 0x7f, 0x2a, 0x08],
    // 43: +
    [0x08, 0x08, 0x3e, 0x08, 0x08],
    // 44: ,
    [0x00, 0x50, 0x30, 0x00, 0x00],
    // 45: -
    [0x08, 0x08, 0x08, 0x08, 0x08],
    // 46: .
    [0x00, 0x60, 0x60, 0x00, 0x00],
    // 47: /
    [0x20, 0x10, 0x08, 0x04, 0x02],
    // 48: 0
    [0x3e, 0x51, 0x49, 0x45, 0x3e],
    // 49: 1
    [0x00, 0x42, 0x7f, 0x40, 0x00],
    // 50: 2
    [0x62, 0x51, 0x49, 0x49, 0x46],
    // 51: 3
    [0x22, 0x41, 0x49, 0x49, 0x36],
    // 52: 4
    [0x18, 0x14, 0x12, 0x7f, 0x10],
    // 53: 5
    [0x27, 0x45, 0x45, 0x45, 0x39],
    // 54: 6
    [0x3c, 0x4a, 0x49, 0x49, 0x30],
    // 55: 7
    [0x01, 0x71, 0x09, 0x05, 0x03],
    // 56: 8
    [0x36, 0x49, 0x49, 0x49, 0x36],
    // 57: 9
    [0x06, 0x49, 0x49, 0x29, 0x1e],
    // 58: :
    [0x00, 0x36, 0x36, 0x00, 0x00],
    // 59: ;
    [0x00, 0x60, 0x36, 0x00, 0x00],
    // 60: <
    [0x08, 0x14, 0x22, 0x41, 0x00],
    // 61: =
    [0x14, 0x14, 0x14, 0x14, 0x14],
    // 62: >
    [0x00, 0x41, 0x22, 0x14, 0x08],
    // 63: ?
    [0x02, 0x01, 0x51, 0x09, 0x06],
    // 64: @
    [0x3e, 0x41, 0x5d, 0x49, 0x4e],
    // 65: A
    [0x7e, 0x11, 0x11, 0x11, 0x7e],
    // 66: B
    [0x7f, 0x49, 0x49, 0x49, 0x36],
    // 67: C
    [0x3e, 0x41, 0x41, 0x41, 0x22],
    // 68: D
    [0x7f, 0x41, 0x41, 0x41, 0x3e],
    // 69: E
    [0x7f, 0x49, 0x49, 0x49, 0x41],
    // 70: F
    [0x7f, 0x09, 0x09, 0x01, 0x01],
    // 71: G
    [0x3e, 0x41, 0x49, 0x49, 0x7a],
    // 72: H
    [0x7f, 0x08, 0x08, 0x08, 0x7f],
    // 73: I
    [0x00, 0x41, 0x7f, 0x41, 0x00],
    // 74: J
    [0x20, 0x40, 0x41, 0x3f, 0x01],
    // 75: K
    [0x7f, 0x08, 0x14, 0x22, 0x41],
    // 76: L
    [0x7f, 0x40, 0x40, 0x40, 0x40],
    // 77: M
    [0x7f, 0x02, 0x04, 0x02, 0x7f],
    // 78: N
    [0x7f, 0x02, 0x04, 0x08, 0x7f],
    // 79: O
    [0x3e, 0x41, 0x41, 0x41, 0x3e],
    // 80: P
    [0x7f, 0x09, 0x09, 0x09, 0x06],
    // 81: Q
    [0x3e, 0x41, 0x51, 0x21, 0x5e],
    // 82: R
    [0x7f, 0x09, 0x19, 0x29, 0x46],
    // 83: S
    [0x46, 0x49, 0x49, 0x49, 0x31],
    // 84: T
    [0x01, 0x01, 0x7f, 0x01, 0x01],
    // 85: U
    [0x3f, 0x40, 0x40, 0x40, 0x3f],
    // 86: V
    [0x1f, 0x20, 0x40, 0x20, 0x1f],
    // 87: W
    [0x3f, 0x40, 0x38, 0x40, 0x3f],
    // 88: X
    [0x61, 0x14, 0x08, 0x14, 0x61],
    // 89: Y
    [0x01, 0x02, 0x7c, 0x02, 0x01],
    // 90: Z
    [0x41, 0x61, 0x51, 0x49, 0x45],
    // 91: [
    [0x00, 0x7f, 0x41, 0x41, 0x00],
    // 92: \
    [0x02, 0x04, 0x08, 0x10, 0x20],
    // 93: ]
    [0x00, 0x41, 0x41, 0x7f, 0x00],
    // 94: ^
    [0x04, 0x02, 0x01, 0x02, 0x04],
    // 95: _
    [0x40, 0x40, 0x40, 0x40, 0x40],
    // 96: `
    [0x00, 0x01, 0x02, 0x04, 0x00],
    // 97: a
    [0x20, 0x54, 0x54, 0x54, 0x78],
    // 98: b
    [0x7f, 0x44, 0x48, 0x48, 0x30],
    // 99: c
    [0x38, 0x44, 0x44, 0x44, 0x20],
    // 100: d
    [0x30, 0x48, 0x48, 0x44, 0x7f],
    // 101: e
    [0x38, 0x54, 0x54, 0x54, 0x18],
    // 102: f
    [0x04, 0x04, 0x7e, 0x05, 0x05],
    // 103: g
    [0x48, 0x54, 0x54, 0x54, 0x38],
    // 104: h
    [0x7f, 0x08, 0x08, 0x08, 0x70],
    // 105: i
    [0x00, 0x44, 0x7d, 0x40, 0x00],
    // 106: j
    [0x20, 0x40, 0x44, 0x3d, 0x00],
    // 107: k
    [0x7f, 0x08, 0x14, 0x22, 0x41],
    // 108: l
    [0x00, 0x7f, 0x40, 0x40, 0x00],
    // 109: m
    [0x7c, 0x02, 0x04, 0x02, 0x7c],
    // 110: n
    [0x7c, 0x08, 0x08, 0x08, 0x70],
    // 111: o
    [0x38, 0x44, 0x44, 0x44, 0x38],
    // 112: p
    [0x7c, 0x14, 0x14, 0x14, 0x08],
    // 113: q
    [0x08, 0x14, 0x14, 0x14, 0x7c],
    // 114: r
    [0x7c, 0x08, 0x04, 0x04, 0x04],
    // 115: s
    [0x48, 0x54, 0x54, 0x54, 0x20],
    // 116: t
    [0x04, 0x04, 0x3e, 0x04, 0x04],
    // 117: u
    [0x3c, 0x40, 0x40, 0x20, 0x7c],
    // 118: v
    [0x1c, 0x20, 0x40, 0x20, 0x1c],
    // 119: w
    [0x3c, 0x40, 0x38, 0x40, 0x3c],
    // 120: x
    [0x44, 0x28, 0x10, 0x28, 0x44],
    // 121: y
    [0x44, 0x44, 0x44, 0x44, 0x28],
    // 122: z
    [0x44, 0x64, 0x54, 0x4c, 0x44],
    // 123: {
    [0x00, 0x08, 0x3e, 0x41, 0x00],
    // 124: |
    [0x00, 0x00, 0x7f, 0x00, 0x00],
    // 125: }
    [0x00, 0x41, 0x3e, 0x08, 0x00],
    // 126: ~
    [0x08, 0x10, 0x10, 0x08, 0x00],
];