summary refs log tree commit diff
path: root/ascii-town-heatmap.rs
blob: fa818cfa104c1e5cb31016123704315074a9211b (plain) (blame)
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
// cargo-deps: hsl, image, csv = "1.0.0-beta.4", serde, serde_derive

// ~~~ PUBLIC DOMAIN ~~~
// I, the copyright holder of this work, hereby release it
// into the public domain. This applies worldwide.
// In case this is not legally possible, I grant any entity
// the right to use this work for any purpose, without any
// conditions, unless such conditions are required by law.

extern crate csv;
extern crate hsl;
extern crate image;
extern crate serde;
#[macro_use]
extern crate serde_derive;

use std::collections::{BTreeSet, HashMap};
use std::io;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Tile {
    tile_x: usize,
    tile_y: usize,
    create_time: u64,
    modify_count: u64,
    modify_time: u64,
    access_count: u64,
    access_time: u64,
}

// Torus size (in tiles).
const TORUS_SZ: u32 = 512;

// Tile width/height (in pixels).
const TILE_W: u32 = 8;
const TILE_H: u32 = 5;

// ~2017-07-30.
const CREATE_ONE: u64 = 1501439553;

fn main() {
    let mut tiles = [[0; TORUS_SZ as usize]; TORUS_SZ as usize];
    let mut ord_c = BTreeSet::new();
    for result in csv::Reader::from_reader(io::stdin()).deserialize() {
        let Tile { tile_x: x, tile_y: y, mut create_time, .. } = result.unwrap();
        if create_time == 1 { create_time = CREATE_ONE }
        ord_c.insert(create_time);
        tiles[y][x] = create_time;
    }

    // Normalize all values by mapping them to equally spaced values in [0, 1].
    let normal_map = |ord: BTreeSet<u64>| -> HashMap<u64, f64> {
        ord.iter().enumerate().map(|(i, &x)| {
            (x, i as f64 / (ord.len() - 1) as f64)
        }).collect()
    };
    let normal_c = normal_map(ord_c);

    // Compose the heatmap image in-memory from the 2D array.
    let mut heatmap = image::ImageBuffer::new(TORUS_SZ * TILE_W, TORUS_SZ * TILE_H);
    for y in 0..TORUS_SZ {
        for x in 0..TORUS_SZ {
            // Get and normalize the values.
            let c = tiles[y as usize][x as usize];
            let c = normal_c[&c];

            let h = (1.0 - c) * 240.0;
            let s = c * 0.5 + 0.5;
            let l = if c > 0.0 { c * 0.5 } else { 0.0 };

            let (r, g, b) = hsl::HSL { h, s, l }.to_rgb();
            let rgb = image::Rgb([r, g, b]);

            let coord = |x, dx, px| ((x * 2 + TORUS_SZ + 1) * px / 2 + dx) % (TORUS_SZ * px);
            for dy in 0..TILE_H {
                let y = coord(y, dy, TILE_H);
                for dx in 0..TILE_W {
                    let x = coord(x, dx, TILE_W);
                    heatmap.put_pixel(x, y, rgb);
                }
            }
        }
    }

    // Save the heatmap image.
    heatmap.save("heatmap.png").unwrap();
}