aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-mosaic-edges.c
blob: b59dbda3ae72f3e8f39b6a623dc72871db8d13df (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* See LICENSE file for copyright and license details. */
#include "common.h"

USAGE("[-xy]")

int
main(int argc, char *argv[])
{
	int tiled_x = 0;
	int tiled_y = 0;
	struct stream stream;
	void *colours[2];
	char *buf, *edges, *here;
	size_t i, n, x, y;
	int v;

	ARGBEGIN {
	case 'x':
		tiled_x = 1;
		break;
	case 'y':
		tiled_y = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (argc)
		usage();

	eopen_stream(&stream, NULL);
	echeck_dimensions(&stream, WIDTH | HEIGHT, NULL);

	n = stream.width * stream.height;
	buf = emalloc(stream.frame_size);
	edges = emalloc((n + 7) / 8);

	colours[0] = alloca(stream.pixel_size);
	colours[1] = alloca(stream.pixel_size);
	memset(colours[0], 0, stream.pixel_size);

	if (!strcmp(stream.pixfmt, "xyza")) {
		((double *)(colours[1]))[0] = (double)1;
		((double *)(colours[1]))[1] = (double)1;
		((double *)(colours[1]))[2] = (double)1;
		((double *)(colours[1]))[3] = (double)1;
	} else if (!strcmp(stream.pixfmt, "xyza f")) {
		((float *)(colours[1]))[0] = (float)1;
		((float *)(colours[1]))[1] = (float)1;
		((float *)(colours[1]))[2] = (float)1;
		((float *)(colours[1]))[3] = (float)1;
	} else {
		eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt);
	}

	fprint_stream_head(stdout, &stream);
	efflush(stdout, "<stdout>");

	while (eread_frame(&stream, buf)) {
		memset(edges, 0, (n + 7) / 8);
		for (i = 0; i < n; i++) {
			here = buf + i * stream.pixel_size;
			x = i % stream.width;
			y = i / stream.width;

			if (x != stream.width - 1) {
				if (memcmp(here + stream.pixel_size, here, stream.pixel_size))
					goto at_edge;
			} else if (tiled_x) {
				if (memcmp(here + stream.pixel_size - stream.row_size, here, stream.pixel_size))
					goto at_edge;
			}

			if (x) {
				if (memcmp(here - stream.pixel_size, here, stream.pixel_size))
					goto at_edge;
			} else if (tiled_x) {
				if (memcmp(here + stream.row_size - stream.pixel_size, here, stream.pixel_size))
					goto at_edge;
			}

			if (y != stream.height - 1) {
				if (memcmp(here + stream.row_size, here, stream.pixel_size))
					goto at_edge;
			} else if (tiled_y) {
				if (memcmp(here + stream.row_size - stream.frame_size, here, stream.pixel_size))
					goto at_edge;
			}

			if (y) {
				if (memcmp(here - stream.row_size, here, stream.pixel_size))
					goto at_edge;
			} else if (tiled_y) {
				if (memcmp(here + stream.frame_size - stream.row_size, here, stream.pixel_size))
					goto at_edge;
			}

			continue;
		at_edge:
			edges[i >> 3] |= (char)(1 << (i & 7));
		}
		for (i = 0; i < n; i++) {
			v = (edges[i >> 3] >> (i & 7)) & 1;
			memcpy(buf + i * stream.pixel_size, colours[v], stream.pixel_size);
		}
		ewriteall(STDOUT_FILENO, buf, stream.frame_size, "<stdout>");
	}

	free(buf);
	free(edges);
	return 0;
}