package main // Pictures for the zoo (the robot's face, exhibit 13's postcards): a palette image, a few shapes, a // 5x7 font for capitals and digits, and a PNG writer. No compression (zlib's stored blocks): a // postcard is 1 bit per pixel, about 2 KB, so there is nothing to gain from a deflate here. import "core:hash" Rgb :: [3]u8 Image :: struct { w, h: int, palette: []Rgb, px: []u8, // palette indices, row by row } image_new :: proc(w, h: int, palette: []Rgb) -> Image { p := make([]Rgb, len(palette)) copy(p, palette) return {w, h, p, make([]u8, w * h)} } set_px :: proc(im: ^Image, x, y: int, c: u8) { if x >= 0 && y >= 0 && x < im.w && y < im.h do im.px[y * im.w + x] = c } // Filled, corners included (GD's imagefilledrectangle). fill_rect :: proc(im: ^Image, x0, y0, x1, y1: int, c: u8) { for y in y0 ..= y1 do for x in x0 ..= x1 do set_px(im, x, y, c) } // A one-pixel frame. frame_rect :: proc(im: ^Image, x0, y0, x1, y1: int, c: u8) { for x in x0 ..= x1 {set_px(im, x, y0, c); set_px(im, x, y1, c)} for y in y0 ..= y1 {set_px(im, x0, y, c); set_px(im, x1, y, c)} } fill_ellipse :: proc(im: ^Image, cx, cy, r: int, c: u8) { for y in -r ..= r do for x in -r ..= r do if x * x + y * y <= r * r do set_px(im, cx + x, cy + y, c) } // Capitals, digits and ". : -", 5 wide and 7 high, one byte per row (bit 4 is the left column). // Lower case is drawn as upper case. FONT_CHARS :: " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:-" FONT := [?][7]u8 { {0, 0, 0, 0, 0, 0, 0}, // space {0x0e, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11}, // A {0x1e, 0x11, 0x11, 0x1e, 0x11, 0x11, 0x1e}, // B {0x0e, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0e}, // C {0x1e, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1e}, // D {0x1f, 0x10, 0x10, 0x1e, 0x10, 0x10, 0x1f}, // E {0x1f, 0x10, 0x10, 0x1e, 0x10, 0x10, 0x10}, // F {0x0e, 0x11, 0x10, 0x17, 0x11, 0x11, 0x0f}, // G {0x11, 0x11, 0x11, 0x1f, 0x11, 0x11, 0x11}, // H {0x0e, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0e}, // I {0x07, 0x02, 0x02, 0x02, 0x02, 0x12, 0x0c}, // J {0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11}, // K {0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1f}, // L {0x11, 0x1b, 0x15, 0x15, 0x11, 0x11, 0x11}, // M {0x11, 0x11, 0x19, 0x15, 0x13, 0x11, 0x11}, // N {0x0e, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e}, // O {0x1e, 0x11, 0x11, 0x1e, 0x10, 0x10, 0x10}, // P {0x0e, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0d}, // Q {0x1e, 0x11, 0x11, 0x1e, 0x14, 0x12, 0x11}, // R {0x0f, 0x10, 0x10, 0x0e, 0x01, 0x01, 0x1e}, // S {0x1f, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04}, // T {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0e}, // U {0x11, 0x11, 0x11, 0x11, 0x11, 0x0a, 0x04}, // V {0x11, 0x11, 0x11, 0x15, 0x15, 0x15, 0x0a}, // W {0x11, 0x11, 0x0a, 0x04, 0x0a, 0x11, 0x11}, // X {0x11, 0x11, 0x11, 0x0a, 0x04, 0x04, 0x04}, // Y {0x1f, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1f}, // Z {0x0e, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0e}, // 0 {0x04, 0x0c, 0x04, 0x04, 0x04, 0x04, 0x0e}, // 1 {0x0e, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1f}, // 2 {0x1f, 0x02, 0x04, 0x02, 0x01, 0x11, 0x0e}, // 3 {0x02, 0x06, 0x0a, 0x12, 0x1f, 0x02, 0x02}, // 4 {0x1f, 0x10, 0x1e, 0x01, 0x01, 0x11, 0x0e}, // 5 {0x06, 0x08, 0x10, 0x1e, 0x11, 0x11, 0x0e}, // 6 {0x1f, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08}, // 7 {0x0e, 0x11, 0x11, 0x0e, 0x11, 0x11, 0x0e}, // 8 {0x0e, 0x11, 0x11, 0x0f, 0x01, 0x02, 0x0c}, // 9 {0, 0, 0, 0, 0, 0x0c, 0x0c}, // . {0, 0x0c, 0x0c, 0, 0x0c, 0x0c, 0}, // : {0, 0, 0, 0x1f, 0, 0, 0}, // - } // Text at (x, y), each font pixel `scale` pixels square, 6 * scale per character. text :: proc(im: ^Image, x, y: int, s: string, c: u8, scale := 1) { cx := x for ch in transmute([]u8)s { up := ch - 32 if ch >= 'a' && ch <= 'z' else ch gi := 0 for f, i in transmute([]u8)string(FONT_CHARS) do if f == up do gi = i for row in 0 ..< 7 { bits := FONT[gi][row] for col in 0 ..< 5 do if bits & (0x10 >> u8(col)) != 0 do fill_rect(im, cx + col * scale, y + row * scale, cx + col * scale + scale - 1, y + row * scale + scale - 1, c) } cx += 6 * scale } } // ---- PNG ---- @(private = "file") chunk :: proc(out: ^[dynamic]u8, kind: string, data: []u8) { append(out, ..be32(u32(len(data)))) start := len(out) append(out, ..transmute([]u8)kind) append(out, ..data) append(out, ..be32(hash.crc32(out[start:]))) } png_encode :: proc(im: ^Image) -> []u8 { depth := 1 if len(im.palette) <= 2 else 2 if len(im.palette) <= 4 else 4 if len(im.palette) <= 16 else 8 per := 8 / depth row := (im.w + per - 1) / per raw := make([dynamic]u8, 0, (row + 1) * im.h) for y in 0 ..< im.h { append(&raw, 0) // filter: none line := make([]u8, row) for x in 0 ..< im.w { v := im.px[y * im.w + x] line[x / per] |= v << u8(8 - depth * (x % per + 1)) } append(&raw, ..line) } // zlib: stored blocks of at most 65535 bytes, then adler32 z := make([dynamic]u8) append(&z, 0x78, 0x01) for off := 0; true; /**/ { n := min(65535, len(raw) - off) last := off + n == len(raw) append(&z, 1 if last else 0, u8(n), u8(n >> 8), u8(~u16(n)), u8(~u16(n) >> 8)) append(&z, ..raw[off:off + n]) off += n if last do break } append(&z, ..be32(hash.adler32(raw[:]))) out := make([dynamic]u8) append(&out, 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n') ihdr := join(be32(u32(im.w)), be32(u32(im.h)), {u8(depth), 3, 0, 0, 0}) chunk(&out, "IHDR", ihdr) pl := make([]u8, 3 * len(im.palette)) for c, i in im.palette {pl[3 * i] = c[0]; pl[3 * i + 1] = c[1]; pl[3 * i + 2] = c[2]} chunk(&out, "PLTE", pl) chunk(&out, "IDAT", z[:]) chunk(&out, "IEND", nil) return out[:] }