summaryrefslogtreecommitdiff
path: root/src/estd/graphics/root.zig
blob: da09450a13e32d4ef2d284cddc088b39564c606a (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const std = @import("std");

pub const Pixel = packed struct(u32) {
	blue: u8,
	green: u8,
	red: u8,
	_padding: u8 = 0x0,
};

pub const Canvas = struct {
	const Self = @This();

	width: u32,
	height: u32,
	buffer: []volatile Pixel,

	pub fn fill(self: *const Canvas, pixel: Pixel) void {
		@memset(self.buffer, pixel);
	}

	fn gaussian_mean(values: [9]u32) u32 {
		return (values[0] +     values[1] * 2 + values[2] +
		        values[3] * 2 + values[4] * 8 + values[5] * 2 +
		        values[6] +     values[7] * 2 + values[8]) / 20;
	}

	pub fn set_with_anti_aliasing(self: *const Canvas, x: u32, y: u32) void {
		var neighbors_red: [9]u32 = undefined;
		var neighbors_green: [9]u32 = undefined;
		var neighbors_blue: [9]u32 = undefined;

		for (0..3) |h| {
			for (0..3) |w| {
				const pixel = self.buffer[(y + 1 - h) * self.width + x + 1 - w];
				neighbors_red[w + 3 * h] = pixel.red;
				neighbors_green[w + 3 * h] = pixel.green;
				neighbors_blue[w + 3 * h] = pixel.blue;
			}
		}

		self.buffer[x + (y * self.width)] = .{
			.red = @intCast(Self.gaussian_mean(neighbors_red)),
			.green = @intCast(Self.gaussian_mean(neighbors_green)),
			.blue = @intCast(Self.gaussian_mean(neighbors_blue)),
		};
	}
};

pub const Box = struct {
	const Self = @This();

	x: u32,
	y: u32,
	width: u32,
	height: u32,
	color: Pixel,
	radius: u32 = 0,

	fn render_rect(
		self: *const Self,
		x: u32,
		y: u32,
		width: u32,
		height: u32,
		canvas: *const Canvas
	) void {
		if (x >= canvas.width or y >= canvas.height) {
			return;
		}

		const x_end = @min(x + width, canvas.width);
		const y_end = @min(y + height, canvas.height);

		for (y..y_end) |yp| {
			const offset = yp * canvas.width;
			@memset(
				canvas.buffer[(offset + x)..(offset + x_end)],
				self.color
			);
		}
	}

	pub fn render(self: *const Self, canvas: *const Canvas) void {
		if (self.radius == 0) {
			self.render_rect(self.x, self.y, self.width, self.height, canvas);
			return;
		}

		const radius = @min(self.radius, self.width / 2, self.height / 2);
		const core_width = self.width - 2 * radius;
		const core_height = self.height - 2 * radius;

		self.render_rect(
			self.x + radius,
			self.y,
			core_width,
			radius,
			canvas
		);

		self.render_rect(
			self.x + radius,
			self.y + radius + core_height,
			core_width,
			radius,
			canvas
		);

		self.render_rect(
			self.x,
			self.y + radius,
			self.width,
			core_height,
			canvas
		);

		for (0..radius) |yp| {
			const width: u32 = @intFromFloat(std.math.round(
				@as(f64, @floatFromInt(radius)) *
				@cos(std.math.asin(
						@as(f64, @floatFromInt(yp)) / @as(f64, @floatFromInt(radius))
				))
			));
			const spacing = radius - width;

			var offset = ((radius - yp) + self.y) * canvas.width + self.x + spacing;
			@memset(
				canvas.buffer[offset..offset + width],
				self.color
			);

			offset = ((radius - yp) + self.y) * canvas.width + self.x + core_width + radius;
			@memset(
				canvas.buffer[offset..offset + width],
				self.color
			);

			offset = (yp + self.y + radius + core_height) * canvas.width + self.x + spacing;
			@memset(
				canvas.buffer[offset..offset + width],
				self.color
			);

			offset = (yp + self.y + radius + core_height) * canvas.width + self.x + core_width + radius;
			@memset(
				canvas.buffer[offset..offset + width],
				self.color
			);
		}

		for (0..radius) |yp| {
			const width: u32 = @intFromFloat(std.math.round(
				@as(f64, @floatFromInt(radius)) *
				@cos(std.math.asin(
						@as(f64, @floatFromInt(yp)) / @as(f64, @floatFromInt(radius))
				))
			));

			const spacing = radius - width;

			canvas.set_with_anti_aliasing(
				self.x + spacing,
				@intCast(self.y + radius - yp),
			);

			canvas.set_with_anti_aliasing(
				self.x + spacing - 1,
				@intCast(self.y + radius - yp),
			);

			canvas.set_with_anti_aliasing(
				self.x + core_width + radius + width,
				@intCast(self.y + radius - yp),
			);

			canvas.set_with_anti_aliasing(
				self.x + spacing,
				@intCast(self.y + yp + radius + core_height),
			);

			canvas.set_with_anti_aliasing(
				self.x + core_width + radius + width,
				@intCast(self.y +  yp + radius + core_height),
			);
		}

		for (0..core_width) |w| {
			canvas.set_with_anti_aliasing(@intCast(self.x + radius + w), self.y);
			canvas.set_with_anti_aliasing(@intCast(self.x + radius + w), self.y + self.height);
		}

		for (0..core_height) |h| {
			canvas.set_with_anti_aliasing(self.x, @intCast(self.y + radius + h));
			canvas.set_with_anti_aliasing(self.x + self.width, @intCast(self.y + radius + h));
		}
	}
};