aboutsummaryrefslogtreecommitdiff
path: root/aberth/main.cpp
blob: 152640f4c6cb0a1cde44e0c48551c71b3dd883d1 (plain)
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstring>
#include <cassert>
#include "../lodepng.h"
#include "compute_host.h"
#include "defs.h"
#include "kernel.h"

using namespace std;


static void writeCounts(int W, int H, const vector<int> &counts, const char *fname) {
	ofstream f(fname);
	f << W << ' ' << H << '\n';
	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			if (x != 0) f << ' ';
			f << counts[W * y + x];
		}
		f << '\n';
	}
}

static tuple<int, int, vector<int>> readCounts(const char *fname) {
	ifstream f(fname);
	int W, H;
	f >> W >> H;
	vector<int> counts(W * H);
	for (int &v : counts) f >> v;
	return make_tuple(W, H, counts);
}

static int rankCounts(vector<int> &counts) {
	int maxcount = 0;
	for (int i = 0; i < (int)counts.size(); i++) {
		maxcount = max(maxcount, counts[i]);
	}

	vector<int> cumul(maxcount + 1, 0);
	for (int v : counts) cumul[v]++;
	cumul[0] = 0;
	for (int i = 1; i < (int)cumul.size(); i++) cumul[i] += cumul[i-1];
	// assert(cumul[maxcount + 1] == (int)counts.size());

	for (int &v : counts) v = cumul[v];

	return cumul[maxcount];
}

static vector<uint8_t> drawImage(int W, int H, const vector<int> &counts, int maxcount) {
	vector<uint8_t> image(3 * W * H);

	for (int y = 0; y < H; y++) {
		for (int x = 0; x < W; x++) {
			double value = (double)counts[W * y + x] / maxcount * 255;
			image[3 * (W * y + x) + 0] = value;
			image[3 * (W * y + x) + 1] = value;
			image[3 * (W * y + x) + 2] = value;
		}
	}

	return image;
}

int main(int argc, char **argv) {
	int W, H;
	vector<int> counts;

	if (argc <= 1) {
		W = H = 900;
		const Com bottomLeft = Com(-1.5, -1.5);
		const Com topRight = Com(1.5, 1.5);

		// counts = computeHost(W, H, bottomLeft, topRight);
		Kernel().run_chunked(counts, W, H, bottomLeft, topRight, 42, 1 << 14);
		// Kernel().run_all(counts, W, H, bottomLeft, topRight, 42);

		writeCounts(W, H, counts, "out.txt");
	} else if (argc == 2 && strcmp(argv[1], "-h") != 0) {
		tie(W, H, counts) = readCounts(argv[1]);
	} else {
		cerr << "Usage: " << argv[0] << "            -- compute and draw" << endl;
		cerr << "Usage: " << argv[0] << " <out.txt>  -- draw already-computed data" << endl;
		return 1;
	}

	int maxcount = rankCounts(counts);

	vector<uint8_t> image = drawImage(W, H, counts, maxcount);

	assert(lodepng_encode24_file("out.png", image.data(), W, H) == 0);
}