-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinviz.c
More file actions
72 lines (57 loc) · 2.02 KB
/
binviz.c
File metadata and controls
72 lines (57 loc) · 2.02 KB
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
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define NOB_IMPLEMENTATION
#include "nob.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define MAP_SIZE 256
static size_t map[MAP_SIZE][MAP_SIZE] = {0};
static uint32_t pixels[MAP_SIZE][MAP_SIZE] = {0};
int main(int argc, char **argv)
{
const char *program = nob_shift_args(&argc, &argv);
if (argc <= 0) {
nob_log(NOB_ERROR, "No input is provided");
nob_log(NOB_INFO, "Usage: %s <input>", program);
return 1;
}
Nob_String_Builder content = {0};
for (int i = 0; i < argc; ++i) {
const char *input_file_path = argv[i];
content.count = 0;
nob_log(NOB_INFO, "Reading %s", input_file_path);
if (!nob_read_entire_file(input_file_path, &content)) return 1;
memset(map, 0, sizeof(map));
if (content.count > 0) {
for (size_t i = 0; i < content.count - 1; ++i) {
uint8_t x = content.items[i];
uint8_t y = content.items[i+1];
map[y][x] += 1;
}
}
float max = 0;
for (size_t y = 0; y < MAP_SIZE; ++y) {
for (size_t x = 0; x < MAP_SIZE; ++x) {
float f = 0.0f;
if (map[y][x] > 0) f = logf(map[y][x]);
if (f > max) max = f;
}
}
for (size_t y = 0; y < MAP_SIZE; ++y) {
for (size_t x = 0; x < MAP_SIZE; ++x) {
float t = logf(map[y][x])/max;
uint32_t b = t*255;
pixels[y][x] = 0xFF000000 | b | (b<<8) | (b<<16);
}
}
const char *output_file_path = nob_temp_sprintf("%s.binviz.png", input_file_path);
if (!stbi_write_png(output_file_path, MAP_SIZE, MAP_SIZE, 4, pixels, MAP_SIZE*sizeof(uint32_t))) {
nob_log(NOB_ERROR, "Could not save image %s", output_file_path);
return 1;
}
nob_log(NOB_INFO, "Generated %s", output_file_path);
nob_temp_reset();
}
return 0;
}