summaryrefslogtreecommitdiff
path: root/tools/autodice.c
blob: e053056fc0d81d599125a035d5b395c42076deff (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
#include <stdio.h>
#include <string.h>
#include <ppm.h>

pixel **image;
int w, h;
pixval maxval;
int rtl = 0;

void save(int x1, int x2, int y1, int y2)
{
	int y;
	ppm_writeppminit(stdout, x2 - x1, y2 - y1, maxval, 0);
	for (y = y1; y < y2; ++y)
		ppm_writeppmrow(stdout, &image[y][x1], x2-x1, maxval, 0);
}

void dice_ltr(int y1, int y2)
{
	int x, y, x0 = 0;
	for (x = 0; x < w; ++x) {
		for (y = y1; y < y2; ++y) {
			pixel p = image[y][x];
			if (p.r < maxval || p.g < maxval || p.b < maxval)
				break;
		}
		if (y == y2) {
			if (x > x0)
				save(x0, x, y1, y2);
			x0 = x + 1;
		}
	}
	if (x > x0)
		save(x0, x, y1, y2);
}

void dice_rtl(int y1, int y2)
{
	int x, y, x0 = w-1;
	for (x = w-1; x >= 0; --x) {
		for (y = y1; y < y2; ++y) {
			pixel p = image[y][x];
			if (p.r < maxval || p.g < maxval || p.b < maxval)
				break;
		}
		if (y == y2) {
			if (x < x0)
				save(x+1, x0+1, y1, y2);
			x0 = x - 1;
		}
	}
	if (x < x0)
		save(x+1, x0+1, y1, y2);
}

void dice(int y1, int y2)
{
	if (rtl)
		dice_rtl(y1, y2);
	else
		dice_ltr(y1, y2);
}

void slice(void)
{
	int x, y, y0 = 0;
	for (y = 0; y < h; ++y) {
		for (x = 0; x < w; ++x) {
			pixel p = image[y][x];
			if (p.r < maxval || p.g < maxval || p.b < maxval)
				break;
		}
		if (x == w) {
			if (y > y0)
				dice(y0, y);
			y0 = y + 1;
		}
	}

	if (y > y0)
		dice(y0, y);
}

int main(int argc, char **argv)
{
	if (argc > 1 && !strcmp(argv[1], "-rtl"))
		rtl = 1;

	pm_init(argv[0], 0);

	image = ppm_readppm(stdin, &w, &h, &maxval);
	slice();
	ppm_freearray(image, h);

	return 0;
}