1 /**
2  * Drawing functions.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.utils.graphics.draw;
15 
16 import std.algorithm : sort;
17 import std.traits;
18 
19 import ae.utils.geometry : TAU;
20 import ae.utils.graphics.view;
21 import ae.utils.graphics.color : solidStorageUnit;
22 import ae.utils.math;
23 import ae.utils.meta : structFields, SignedBitsType, UnsignedBitsType;
24 
25 version(unittest) import ae.utils.graphics.image;
26 
27 // Constraints could be simpler if this was fixed:
28 // https://issues.dlang.org/show_bug.cgi?id=12386
29 
30 /// Get the pixel color at the specified coordinates,
31 /// or fall back to the specified default value if
32 /// the coordinates are out of bounds.
33 COLOR safeGet(V, COLOR)(auto ref V v, xy_t x, xy_t y, COLOR def)
34 	if (isView!V && is(COLOR : ViewColor!V))
35 {
36 	if (x>=0 && y>=0 && x<v.w && y<v.h)
37 		return v[x, y];
38 	else
39 		return def;
40 }
41 
42 unittest
43 {
44 	auto v = onePixel(7);
45 	assert(v.safeGet(0, 0, 0) == 7);
46 	assert(v.safeGet(0, 1, 0) == 0);
47 }
48 
49 /// Set the pixel color at the specified coordinates
50 /// if the coordinates are not out of bounds.
51 void safePut(V, COLOR)(auto ref V v, xy_t x, xy_t y, COLOR value)
52 	if (isWritableView!V && is(COLOR : ViewColor!V))
53 {
54 	if (x>=0 && y>=0 && x<v.w && y<v.h)
55 		v[x, y] = value;
56 }
57 
58 unittest
59 {
60 	auto v = Image!int(1, 1);
61 	v.safePut(0, 0, 7);
62 	v.safePut(0, 1, 9);
63 	assert(v[0, 0] == 7);
64 }
65 
66 /// Forwards to safePut or opIndex, depending on the
67 /// CHECKED parameter. Allows propagation of a
68 /// CHECKED parameter from other callers.
69 void putPixel(bool CHECKED, V, COLOR)(auto ref V v, xy_t x, xy_t y, COLOR value)
70 	if (isWritableView!V && is(COLOR : ViewColor!V))
71 {
72 	static if (CHECKED)
73 		v.safePut(x, y, value);
74 	else
75 		v[x, y] = value;
76 }
77 
78 unittest
79 {
80 	auto v = Image!int(1, 1);
81 	v.putPixel!false(0, 0, 7);
82 	v.putPixel!true(0, 1, 9);
83 	assert(v[0, 0] == 7);
84 }
85 
86 /// Gets a pixel's address from a direct view.
87 ViewColor!V* pixelPtr(V)(auto ref V v, xy_t x, xy_t y)
88 if (isDirectView!V && is(typeof(&v.scanline(0)[0][0])))
89 {
90 	return &v.scanline(y)[x][0];
91 }
92 
93 unittest
94 {
95 	auto v = Image!xy_t(1, 1);
96 	v[0, 0] = 7;
97 	auto p = v.pixelPtr(0, 0);
98 	assert(*p == 7);
99 }
100 
101 /// Fills a writable view with a solid color.
102 void fill(V, COLOR)(auto ref V v, COLOR c)
103 	if (isWritableView!V
104 	 && is(COLOR : ViewColor!V))
105 {
106 	static if (isDirectView!V)
107 	{
108 		auto s = solidStorageUnit!(ViewStorageType!V)(c);
109 		foreach (y; 0..v.h)
110 			v.scanline(y)[] = s;
111 	}
112 	else
113 	{
114 		foreach (y; 0..v.h)
115 			foreach (x; 0..v.w)
116 				v[x, y] = c;
117 	}
118 }
119 deprecated alias clear = fill;
120 
121 unittest
122 {
123 	auto i = onePixel(0).copy();
124 	i.fill(1);
125 	assert(i[0, 0] == 1);
126 	auto t = i.tile(10, 10);
127 	t.fill(2);
128 	assert(i[0, 0] == 2);
129 }
130 
131 // ***************************************************************************
132 
133 private enum CheckHLine =
134 q{
135 	static if (CHECKED)
136 	{
137 		if (x1 >= v.w || x2 <= 0 || y < 0 || y >= v.h || x1 >= x2) return;
138 		if (x1 <    0) x1 =   0;
139 		if (x2 >= v.w) x2 = v.w;
140 	}
141 	assert(x1 <= x2);
142 };
143 
144 private enum CheckVLine =
145 q{
146 	static if (CHECKED)
147 	{
148 		if (x < 0 || x >= v.w || y1 >= v.h || y2 <= 0 || y1 >= y2) return;
149 		if (y1 <    0) y1 =   0;
150 		if (y2 >= v.h) y2 = v.h;
151 	}
152 	assert(y1 <= y2);
153 };
154 
155 /// Draw a horizontal line.
156 void hline(bool CHECKED=true, V, COLOR)(auto ref V v, xy_t x1, xy_t x2, xy_t y, COLOR c)
157 	if (isWritableView!V && is(COLOR : ViewColor!V))
158 {
159 	mixin(CheckHLine);
160 	static if (isDirectView!V && ViewStorageType!V.length == 1)
161 	{
162 		auto s = solidStorageUnit!(ViewStorageType!V)(c);
163 		v.scanline(y)[x1..x2] = s;
164 	}
165 	else
166 		foreach (x; x1..x2)
167 			v[x, y] = c;
168 }
169 
170 /// Draw a vertical line.
171 void vline(bool CHECKED=true, V, COLOR)(auto ref V v, xy_t x, xy_t y1, xy_t y2, COLOR c)
172 	if (isWritableView!V && is(COLOR : ViewColor!V))
173 {
174 	mixin(CheckVLine);
175 	foreach (y; y1..y2) // TODO: optimize
176 		v[x, y] = c;
177 }
178 
179 /// Draw a line.
180 void line(bool CHECKED=true, V, COLOR)(auto ref V v, xy_t x1, xy_t y1, xy_t x2, xy_t y2, COLOR c)
181 	if (isWritableView!V && is(COLOR : ViewColor!V))
182 {
183 	mixin FixMath;
184 	import std.algorithm.mutation : swap;
185 
186 	enum DrawLine = q{
187 		// Axis-independent part. Mixin context:
188 		// a0 .. a1  - longer side
189 		// b0 .. b1  - shorter side
190 		// DrawPixel - mixin to draw a pixel at coordinates (a, b)
191 
192 		if (a0 == a1)
193 			return;
194 
195 		if (a0 > a1)
196 		{
197 			 swap(a0, a1);
198 			 swap(b0, b1);
199 		}
200 
201 		// Use fixed-point for b position and offset per 1 pixel along "a" axis
202 		assert(b0 < (1L<<coordinateBits) && b1 < (1L<<coordinateBits));
203 		auto bPos = cast(SignedBitsType!(coordinateBits*2))(b0 << coordinateBits);
204 		auto bOff = cast(SignedBitsType!(coordinateBits*2))(((b1-b0) << coordinateBits) / (a1-a0));
205 
206 		foreach (a; a0..a1+1)
207 		{
208 			xy_t b = (bPos += bOff) >> coordinateBits;
209 			mixin(DrawPixel);
210 		}
211 	};
212 
213 	import std.math : abs;
214 
215 	if (abs(x2-x1) > abs(y2-y1))
216 	{
217 		alias x1 a0;
218 		alias x2 a1;
219 		alias y1 b0;
220 		alias y2 b1;
221 		enum DrawPixel = q{ v.putPixel!CHECKED(a, b, c); };
222 		mixin(DrawLine);
223 	}
224 	else
225 	{
226 		alias y1 a0;
227 		alias y2 a1;
228 		alias x1 b0;
229 		alias x2 b1;
230 		enum DrawPixel = q{ v.putPixel!CHECKED(b, a, c); };
231 		mixin(DrawLine);
232 	}
233 }
234 
235 /// Draws a rectangle with a solid line.
236 /// The coordinates represent bounds (open on the right) for the outside of the rectangle.
237 void rect(bool CHECKED=true, V, COLOR)(auto ref V v, xy_t x1, xy_t y1, xy_t x2, xy_t y2, COLOR c)
238 	if (isWritableView!V && is(COLOR : ViewColor!V))
239 {
240 	sort2(x1, x2);
241 	sort2(y1, y2);
242 	v.hline!CHECKED(x1, x2, y1  , c);
243 	v.hline!CHECKED(x1, x2, y2-1, c);
244 	v.vline!CHECKED(x1  , y1, y2, c);
245 	v.vline!CHECKED(x2-1, y1, y2, c);
246 }
247 
248 /// Draw a filled rectangle.
249 void fillRect(bool CHECKED=true, V, COLOR)(auto ref V v, xy_t x1, xy_t y1, xy_t x2, xy_t y2, COLOR b) // [)
250 	if (isWritableView!V && is(COLOR : ViewColor!V))
251 {
252 	sort2(x1, x2);
253 	sort2(y1, y2);
254 	static if (CHECKED)
255 	{
256 		if (x1 >= v.w || y1 >= v.h || x2 <= 0 || y2 <= 0 || x1==x2 || y1==y2) return;
257 		if (x1 <    0) x1 =   0;
258 		if (y1 <    0) y1 =   0;
259 		if (x2 >= v.w) x2 = v.w;
260 		if (y2 >= v.h) y2 = v.h;
261 	}
262 	foreach (y; y1..y2)
263 		v.hline!false(x1, x2, y, b);
264 }
265 
266 /// Draw a filled rectangle with an outline.
267 void fillRect(bool CHECKED=true, V, COLOR)(auto ref V v, xy_t x1, xy_t y1, xy_t x2, xy_t y2, COLOR c, COLOR b) // [)
268 	if (isWritableView!V && is(COLOR : ViewColor!V))
269 {
270 	v.rect!CHECKED(x1, y1, x2, y2, c);
271 	if (x2-x1>2 && y2-y1>2)
272 		v.fillRect!CHECKED(x1+1, y1+1, x2-1, y2-1, b);
273 }
274 
275 /// Recursively replace the color of all adjacent pixels of the same color,
276 /// starting with the given coordinates.
277 /// Unchecked! Make sure area is bounded.
278 void uncheckedFloodFill(V, COLOR)(auto ref V v, xy_t x, xy_t y, COLOR c)
279 	if (isDirectView!V && is(COLOR : ViewColor!V))
280 {
281 	v.floodFillPtr(&v[x, y], c, v[x, y]);
282 }
283 
284 private void floodFillPtr(V, COLOR)(auto ref V v, COLOR* pp, COLOR c, COLOR f)
285 	if (isDirectView!V && is(COLOR : ViewColor!V))
286 {
287 	COLOR* p0 = pp; while (*p0==f) p0--; p0++;
288 	COLOR* p1 = pp; while (*p1==f) p1++; p1--;
289 	auto stride = v.scanline(1).ptr-v.scanline(0).ptr;
290 	for (auto p=p0; p<=p1; p++)
291 		*p = c;
292 	p0 -= stride; p1 -= stride;
293 	for (auto p=p0; p<=p1; p++)
294 		if (*p == f)
295 			v.floodFillPtr(p, c, f);
296 	p0 += stride*2; p1 += stride*2;
297 	for (auto p=p0; p<=p1; p++)
298 		if (*p == f)
299 			v.floodFillPtr(p, c, f);
300 }
301 
302 /// Draw a filled circle.
303 void fillCircle(V, COLOR)(auto ref V v, xy_t x, xy_t y, xy_t r, COLOR c)
304 	if (isWritableView!V && is(COLOR : ViewColor!V))
305 {
306 	import std.algorithm.comparison : min;
307 
308 	xy_t x0 = x>r?x-r:0;
309 	xy_t y0 = y>r?y-r:0;
310 	xy_t x1 = min(x+r, v.w-1);
311 	xy_t y1 = min(y+r, v.h-1);
312 	xy_t rs = sqr(r);
313 	// TODO: optimize
314 	foreach (py; y0..y1+1)
315 		foreach (px; x0..x1+1)
316 			if (sqr(x-px) + sqr(y-py) < rs)
317 				v[px, py] = c;
318 }
319 
320 /// Draw a filled sector (circle slice).
321 void fillSector(V, COLOR)(auto ref V v, xy_t x, xy_t y, xy_t r0, xy_t r1, real a0, real a1, COLOR c)
322 	if (isWritableView!V && is(COLOR : ViewColor!V))
323 {
324 	import std.algorithm.comparison : min;
325 	import std.math : atan2;
326 
327 	xy_t x0 = x>r1?x-r1:0;
328 	xy_t y0 = y>r1?y-r1:0;
329 	xy_t x1 = min(x+r1, v.w-1);
330 	xy_t y1 = min(y+r1, v.h-1);
331 	xy_t r0s = sqr(r0);
332 	xy_t r1s = sqr(r1);
333 	if (a0 > a1)
334 		a1 += TAU;
335 	foreach (py; y0..y1+1)
336 		foreach (px; x0..x1+1)
337 		{
338 			xy_t dx = px-x;
339 			xy_t dy = py-y;
340 			xy_t rs = sqr(dx) + sqr(dy);
341 			if (r0s <= rs && rs < r1s)
342 			{
343 				real a = atan2(cast(real)dy, cast(real)dx);
344 				if ((a0 <= a     && a     <= a1) ||
345 				    (a0 <= a+TAU && a+TAU <= a1))
346 					v[px, py] = c;
347 			}
348 		}
349 }
350 
351 /// Polygon point definition.
352 struct Coord
353 {
354 	///
355 	xy_t x, y;
356 	string toString() { import std.string; return format("%s", [this.tupleof]); } ///
357 }
358 
359 /// Draw a filled polygon with a variable number of points.
360 void fillPoly(V, COLOR)(auto ref V v, Coord[] coords, COLOR f)
361 	if (isWritableView!V && is(COLOR : ViewColor!V))
362 {
363 	import std.algorithm.comparison : min, max;
364 
365 	xy_t minY, maxY;
366 	minY = maxY = coords[0].y;
367 	foreach (c; coords[1..$])
368 		minY = min(minY, c.y),
369 		maxY = max(maxY, c.y);
370 
371 	foreach (y; minY..maxY+1)
372 	{
373 		xy_t[] intersections;
374 		for (uint i=0; i<coords.length; i++)
375 		{
376 			auto c0=coords[i], c1=coords[i==$-1?0:i+1];
377 			if (y==c0.y)
378 			{
379 				assert(y == coords[i%$].y);
380 				int pi = i-1; xy_t py;
381 				while ((py=coords[(pi+$)%$].y)==y)
382 					pi--;
383 				int ni = i+1; xy_t ny;
384 				while ((ny=coords[ni%$].y)==y)
385 					ni++;
386 				if (ni > coords.length)
387 					continue;
388 				if ((py>y) == (y>ny))
389 					intersections ~= coords[i%$].x;
390 				i = ni-1;
391 			}
392 			else
393 			if (c0.y<y && y<c1.y)
394 				intersections ~= itpl(c0.x, c1.x, y, c0.y, c1.y);
395 			else
396 			if (c1.y<y && y<c0.y)
397 				intersections ~= itpl(c1.x, c0.x, y, c1.y, c0.y);
398 		}
399 
400 		assert(intersections.length % 2==0);
401 		intersections.sort();
402 		for (uint i=0; i<intersections.length; i+=2)
403 			v.hline!true(intersections[i], intersections[i+1], y, f);
404 	}
405 }
406 
407 /// Draw a line of a given thickness.
408 /// Does not draw caps.
409 void thickLine(V, COLOR)(auto ref V v, xy_t x1, xy_t y1, xy_t x2, xy_t y2, xy_t r, COLOR c)
410 	if (isWritableView!V && is(COLOR : ViewColor!V))
411 {
412 	xy_t dx = x2-x1;
413 	xy_t dy = y2-y1;
414 	xy_t d  = cast(xy_t)sqrt(cast(float)(sqr(dx)+sqr(dy)));
415 	if (d==0) return;
416 
417 	xy_t nx = dx*r/d;
418 	xy_t ny = dy*r/d;
419 
420 	fillPoly([
421 		Coord(x1-ny, y1+nx),
422 		Coord(x1+ny, y1-nx),
423 		Coord(x2+ny, y2-nx),
424 		Coord(x2-ny, y2+nx),
425 	], c);
426 }
427 
428 /// Draw a polygon of a given thickness.
429 /// Does not draw caps.
430 void thickLinePoly(V, COLOR)(auto ref V v, Coord[] coords, xy_t r, COLOR c)
431 	if (isWritableView!V && is(COLOR : ViewColor!V))
432 {
433 	foreach (i; 0..coords.length)
434 		thickLine(coords[i].tupleof, coords[(i+1)%$].tupleof, r, c);
435 }
436 
437 // ************************************************************************************************************************************
438 
439 private mixin template FixMath(ubyte coordinateBitsParam = 16)
440 {
441 	import ae.utils.meta : SignedBitsType, UnsignedBitsType;
442 
443 	enum coordinateBits = coordinateBitsParam;
444 
445 	static assert(COLOR.homogeneous, "Asymmetric color types not supported, fix me!");
446 	/// Fixed-point type, big enough to hold a coordinate, with fractionary precision corresponding to channel precision.
447 	alias fix  = SignedBitsType!(COLOR.channelBits   + coordinateBits);
448 	/// Type to hold temporary values for multiplication and division
449 	alias fix2 = SignedBitsType!(COLOR.channelBits*2 + coordinateBits);
450 
451 	static assert(COLOR.channelBits < 32, "Shift operators are broken for shifts over 32 bits, fix me!");
452 	fix tofix(T:int  )(T x) { return cast(fix) (x<<COLOR.channelBits); }
453 	fix tofix(T:float)(T x) { return cast(fix) (x*(1<<COLOR.channelBits)); }
454 	T fixto(T:int)(fix x) { return cast(T)(x>>COLOR.channelBits); }
455 
456 	fix fixsqr(fix x)        { return cast(fix)((cast(fix2)x*x) >> COLOR.channelBits); }
457 	fix fixmul(fix x, fix y) { return cast(fix)((cast(fix2)x*y) >> COLOR.channelBits); }
458 	fix fixdiv(fix x, fix y) { return cast(fix)((cast(fix2)x << COLOR.channelBits)/y); }
459 
460 	static assert(COLOR.ChannelType.sizeof*8 == COLOR.channelBits, "COLORs with ChannelType not corresponding to native type not currently supported, fix me!");
461 	/// Type only large enough to hold a fractionary part of a "fix" (i.e. color channel precision). Used for alpha values, etc.
462 	alias COLOR.ChannelType frac;
463 	/// Type to hold temporary values for multiplication and division
464 	alias UnsignedBitsType!(COLOR.channelBits*2) frac2;
465 
466 	frac tofrac(T:float)(T x) { return cast(frac) (x*(1<<COLOR.channelBits)); }
467 	frac fixfpart(fix x) { return cast(frac)x; }
468 	frac fracsqr(frac x        ) { return cast(frac)((cast(frac2)x*x) >> COLOR.channelBits); }
469 	frac fracmul(frac x, frac y) { return cast(frac)((cast(frac2)x*y) >> COLOR.channelBits); }
470 
471 	frac tofracBounded(T:float)(T x) { return cast(frac) bound(tofix(x), 0, frac.max); }
472 }
473 
474 // ************************************************************************************************************************************
475 
476 /// Fill `v` with white noise.
477 void whiteNoise(V)(V v)
478 	if (isWritableView!V)
479 {
480 	import std.random;
481 	alias COLOR = ViewColor!V;
482 
483 	for (xy_t y=0;y<v.h/2;y++)
484 		for (xy_t x=0;x<v.w/2;x++)
485 			v[x*2, y*2] = COLOR.monochrome(uniform!(COLOR.ChannelType)());
486 
487 	// interpolate
488 	enum AVERAGE = q{(a+b)/2};
489 
490 	for (xy_t y=0;y<v.h/2;y++)
491 		for (xy_t x=0;x<v.w/2-1;x++)
492 			v[x*2+1, y*2  ] = COLOR.op!AVERAGE(v[x*2  , y*2], v[x*2+2, y*2  ]);
493 	for (xy_t y=0;y<v.h/2-1;y++)
494 		for (xy_t x=0;x<v.w/2;x++)
495 			v[x*2  , y*2+1] = COLOR.op!AVERAGE(v[x*2  , y*2], v[x*2  , y*2+2]);
496 	for (xy_t y=0;y<v.h/2-1;y++)
497 		for (xy_t x=0;x<v.w/2-1;x++)
498 			v[x*2+1, y*2+1] = COLOR.op!AVERAGE(v[x*2+1, y*2], v[x*2+2, y*2+2]);
499 }
500 
501 private template softRoundShape(bool RING)
502 {
503 	void softRoundShape(T, V, COLOR)(auto ref V v, T x, T y, T r0, T r1, T r2, COLOR color)
504 		if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V))
505 	{
506 		mixin FixMath;
507 
508 		assert(r0 <= r1);
509 		assert(r1 <= r2);
510 		assert(r2 < 256); // precision constraint - see SqrType
511 		//xy_t ix = cast(xy_t)x;
512 		//xy_t iy = cast(xy_t)y;
513 		//xy_t ir1 = cast(xy_t)sqr(r1-1);
514 		//xy_t ir2 = cast(xy_t)sqr(r2+1);
515 		xy_t x1 = cast(xy_t)(x-r2-1); if (x1<0) x1=0;
516 		xy_t y1 = cast(xy_t)(y-r2-1); if (y1<0) y1=0;
517 		xy_t x2 = cast(xy_t)(x+r2+1); if (x2>v.w) x2 = v.w;
518 		xy_t y2 = cast(xy_t)(y+r2+1); if (y2>v.h) y2 = v.h;
519 
520 		static if (RING)
521 		auto r0s = r0*r0;
522 		auto r1s = r1*r1;
523 		auto r2s = r2*r2;
524 		//float rds = r2s - r1s;
525 
526 		fix fx = tofix(x);
527 		fix fy = tofix(y);
528 
529 		static if (RING)
530 		fix fr0s = tofix(r0s);
531 		fix fr1s = tofix(r1s);
532 		fix fr2s = tofix(r2s);
533 
534 		static if (RING)
535 		fix fr10 = fr1s - fr0s;
536 		fix fr21 = fr2s - fr1s;
537 
538 		for (xy_t cy=y1;cy<y2;cy++)
539 		{
540 			auto row = v[cy];
541 			for (xy_t cx=x1;cx<x2;cx++)
542 			{
543 				alias SignedBitsType!(2*(8 + COLOR.channelBits)) SqrType; // fit the square of radius expressed as fixed-point
544 				fix frs = cast(fix)((sqr(cast(SqrType)fx-tofix(cx)) + sqr(cast(SqrType)fy-tofix(cy))) >> COLOR.channelBits); // shift-right only once instead of once-per-sqr
545 
546 				//static frac alphafunc(frac x) { return fracsqr(x); }
547 				static frac alphafunc(frac x) { return x; }
548 
549 				static if (RING)
550 				{
551 					if (frs<fr0s)
552 						{}
553 					else
554 					if (frs<fr2s)
555 					{
556 						frac alpha;
557 						if (frs<fr1s)
558 							alpha = alphafunc(cast(frac)fixdiv(frs-fr0s, fr10));
559 						else
560 							alpha = alphafunc(cast(frac)fixdiv(frs-fr1s, fr21)).flipBits;
561 						row[cx] = COLOR.op!q{.blend(a, b, c)}(color, row[cx], alpha);
562 					}
563 				}
564 				else
565 				{
566 					if (frs<fr1s)
567 						row[cx] = color;
568 					else
569 					if (frs<fr2s)
570 					{
571 						frac alpha = alphafunc(cast(frac)fixdiv(frs-fr1s, fr21)).flipBits;
572 						row[cx] = COLOR.op!q{.blend(a, b, c)}(color, row[cx], alpha);
573 					}
574 				}
575 			}
576 		}
577 	}
578 }
579 
580 /// Draw a circle using a smooth interpolated line.
581 void softRing(T, V, COLOR)(auto ref V v, T x, T y, T r0, T r1, T r2, COLOR color)
582 	if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V))
583 {
584 	v.softRoundShape!true(x, y, r0, r1, r2, color);
585 }
586 
587 /// ditto
588 void softCircle(T, V, COLOR)(auto ref V v, T x, T y, T r1, T r2, COLOR color)
589 	if (isWritableView!V && isNumeric!T && is(COLOR : ViewColor!V))
590 {
591 	v.softRoundShape!false(x, y, cast(T)0, r1, r2, color);
592 }
593 
594 /// Draw a 1x1 rectangle at fractional coordinates.
595 /// Affects up to 4 pixels in the image.
596 template aaPutPixel(bool CHECKED=true, bool USE_ALPHA=true)
597 {
598 	void aaPutPixel(F:float, V, COLOR, frac)(auto ref V v, F x, F y, COLOR color, frac alpha)
599 		if (isWritableView!V && is(COLOR : ViewColor!V))
600 	{
601 		mixin FixMath;
602 
603 		void plot(bool CHECKED2)(xy_t x, xy_t y, frac f)
604 		{
605 			static if (CHECKED2)
606 				if (x<0 || x>=v.w || y<0 || y>=v.h)
607 					return;
608 
609 			COLOR* p = v.pixelPtr(x, y);
610 			static if (USE_ALPHA) f = fracmul(f, cast(frac)alpha);
611 			*p = COLOR.op!q{.blend(a, b, c)}(color, *p, f);
612 		}
613 
614 		fix fx = tofix(x);
615 		fix fy = tofix(y);
616 		int ix = fixto!int(fx);
617 		int iy = fixto!int(fy);
618 		static if (CHECKED)
619 			if (ix>=0 && iy>=0 && ix+1<v.w && iy+1<v.h)
620 			{
621 				plot!false(ix  , iy  , fracmul(fixfpart(fx).flipBits, fixfpart(fy).flipBits));
622 				plot!false(ix  , iy+1, fracmul(fixfpart(fx).flipBits, fixfpart(fy)         ));
623 				plot!false(ix+1, iy  , fracmul(fixfpart(fx)         , fixfpart(fy).flipBits));
624 				plot!false(ix+1, iy+1, fracmul(fixfpart(fx)         , fixfpart(fy)         ));
625 				return;
626 			}
627 		plot!CHECKED(ix  , iy  , fracmul(fixfpart(fx).flipBits, fixfpart(fy).flipBits));
628 		plot!CHECKED(ix  , iy+1, fracmul(fixfpart(fx).flipBits, fixfpart(fy)         ));
629 		plot!CHECKED(ix+1, iy  , fracmul(fixfpart(fx)         , fixfpart(fy).flipBits));
630 		plot!CHECKED(ix+1, iy+1, fracmul(fixfpart(fx)         , fixfpart(fy)         ));
631 	}
632 }
633 
634 /// ditto
635 void aaPutPixel(bool CHECKED=true, F:float, V, COLOR)(auto ref V v, F x, F y, COLOR color)
636 	if (isWritableView!V && is(COLOR : ViewColor!V))
637 {
638 	//aaPutPixel!(false, F)(x, y, color, 0); // doesn't work, wtf
639 	alias aaPutPixel!(CHECKED, false) f;
640 	f(v, x, y, color, 0);
641 }
642 
643 /// Draw a horizontal line
644 void hline(bool CHECKED=true, V, COLOR, frac)(auto ref V v, xy_t x1, xy_t x2, xy_t y, COLOR color, frac alpha)
645 	if (isWritableView!V && is(COLOR : ViewColor!V))
646 {
647 	mixin(CheckHLine);
648 
649 	if (alpha==0)
650 		return;
651 	else
652 	if (alpha==frac.max)
653 		.hline!CHECKED(v, x1, x2, y, color);
654 	else
655 		foreach (ref s; v.scanline(y)[x1..x2])
656 			foreach (ref p; s)
657 				p = COLOR.op!q{.blend(a, b, c)}(color, p, alpha);
658 }
659 
660 /// Draw a vertical line
661 void vline(bool CHECKED=true, V, COLOR, frac)(auto ref V v, xy_t x, xy_t y1, xy_t y2, COLOR color, frac alpha)
662 	if (isWritableView!V && is(COLOR : ViewColor!V))
663 {
664 	mixin(CheckVLine);
665 
666 	if (alpha==0)
667 		return;
668 	else
669 	if (alpha==frac.max)
670 		foreach (y; y1..y2)
671 			v[x, y] = color;
672 	else
673 		foreach (y; y1..y2)
674 		{
675 			auto p = v.pixelPtr(x, y);
676 			*p = COLOR.op!q{.blend(a, b, c)}(color, *p, alpha);
677 		}
678 }
679 
680 /// Draw a filled rectangle at fractional coordinates.
681 /// Edges are anti-aliased.
682 void aaFillRect(bool CHECKED=true, F:float, V, COLOR)(auto ref V v, F x1, F y1, F x2, F y2, COLOR color)
683 	if (isWritableView!V && is(COLOR : ViewColor!V))
684 {
685 	mixin FixMath;
686 
687 	sort2(x1, x2);
688 	sort2(y1, y2);
689 	fix x1f = tofix(x1); int x1i = fixto!int(x1f);
690 	fix y1f = tofix(y1); int y1i = fixto!int(y1f);
691 	fix x2f = tofix(x2); int x2i = fixto!int(x2f);
692 	fix y2f = tofix(y2); int y2i = fixto!int(y2f);
693 
694 	v.vline!CHECKED(x1i, y1i+1, y2i, color, fixfpart(x1f).flipBits);
695 	v.vline!CHECKED(x2i, y1i+1, y2i, color, fixfpart(x2f)         );
696 	v.hline!CHECKED(x1i+1, x2i, y1i, color, fixfpart(y1f).flipBits);
697 	v.hline!CHECKED(x1i+1, x2i, y2i, color, fixfpart(y2f)         );
698 	v.aaPutPixel!CHECKED(x1i, y1i, color, fracmul(fixfpart(x1f).flipBits, fixfpart(y1f).flipBits));
699 	v.aaPutPixel!CHECKED(x1i, y2i, color, fracmul(fixfpart(x1f).flipBits, fixfpart(y2f)         ));
700 	v.aaPutPixel!CHECKED(x2i, y1i, color, fracmul(fixfpart(x2f)         , fixfpart(y1f).flipBits));
701 	v.aaPutPixel!CHECKED(x2i, y2i, color, fracmul(fixfpart(x2f)         , fixfpart(y2f)         ));
702 
703 	v.fillRect!CHECKED(x1i+1, y1i+1, x2i, y2i, color);
704 }
705 
706 /// Draw an anti-aliased line.
707 void aaLine(bool CHECKED=true, V, COLOR)(auto ref V v, float x1, float y1, float x2, float y2, COLOR color)
708 	if (isWritableView!V && is(COLOR : ViewColor!V))
709 {
710 	import std.math : abs;
711 
712 	// Simplistic straight-forward implementation. TODO: optimize
713 	if (abs(x1-x2) > abs(y1-y2))
714 		for (auto x=x1; sign(x1-x2)!=sign(x2-x); x += sign(x2-x1))
715 			v.aaPutPixel!CHECKED(x, itpl(y1, y2, x, x1, x2), color);
716 	else
717 		for (auto y=y1; sign(y1-y2)!=sign(y2-y); y += sign(y2-y1))
718 			v.aaPutPixel!CHECKED(itpl(x1, x2, y, y1, y2), y, color);
719 }
720 
721 /// ditto
722 void aaLine(bool CHECKED=true, V, COLOR, frac)(auto ref V v, float x1, float y1, float x2, float y2, COLOR color, frac alpha)
723 	if (isWritableView!V && is(COLOR : ViewColor!V))
724 {
725 	import std.math : abs;
726 
727 	// ditto
728 	if (abs(x1-x2) > abs(y1-y2))
729 		for (auto x=x1; sign(x1-x2)!=sign(x2-x); x += sign(x2-x1))
730 			v.aaPutPixel!CHECKED(x, itpl(y1, y2, x, x1, x2), color, alpha);
731 	else
732 		for (auto y=y1; sign(y1-y2)!=sign(y2-y); y += sign(y2-y1))
733 			v.aaPutPixel!CHECKED(itpl(x1, x2, y, y1, y2), y, color, alpha);
734 }
735 
736 unittest
737 {
738 	// Test instantiation
739 	import ae.utils.graphics.color;
740 	auto i = Image!RGB(100, 100);
741 	auto c = RGB(1, 2, 3);
742 	i.whiteNoise();
743 	i.aaLine(10, 10, 20, 20, c);
744 	i.aaLine(10f, 10f, 20f, 20f, c, 100);
745 	i.rect(10, 10, 20, 20, c);
746 	i.fillRect(10, 10, 20, 20, c);
747 	i.aaFillRect(10, 10, 20, 20, c);
748 	i.vline(10, 10, 20, c);
749 	i.vline(10, 10, 20, c);
750 	i.line(10, 10, 20, 20, c);
751 	i.fillCircle(10, 10, 10, c);
752 	i.fillSector(10, 10, 10, 10, 0.0, TAU, c);
753 	i.softRing(50, 50, 10, 15, 20, c);
754 	i.softCircle(50, 50, 10, 15, c);
755 	i.fillPoly([Coord(10, 10), Coord(10, 20), Coord(20, 20)], c);
756 	i.uncheckedFloodFill(15, 15, RGB(4, 5, 6));
757 }
758 
759 // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
760 
761 // Outdated code from before the overhaul.
762 // TODO: fix and update
763 
764 version(none):
765 
766 /// Draws an image. Returns this.
767 typeof(this) draw(bool CHECKED=true, SRCCANVAS)(xy_t x, xy_t y, SRCCANVAS v)
768 	if (IsCanvas!SRCCANVAS && is(COLOR == SRCCANVAS.COLOR))
769 {
770 	static if (CHECKED)
771 	{
772 		if (v.w == 0 || v.h == 0 ||
773 			x+v.w <= 0 || y+v.h <= 0 || x >= w || y >= h)
774 			return this;
775 
776 		auto r = v.window(0, 0, v.w, v.h);
777 		if (x < 0)
778 			r = r.window(-x, 0, r.w, r.h),
779 			x = 0;
780 		if (y < 0)
781 			r = r.window(0, -y, r.w, r.h),
782 			y = 0;
783 		if (x+r.w > w)
784 			r = r.window(0, 0, w-x, r.h);
785 		if (y+r.h > h)
786 			r = r.window(0, 0, r.w, h-y);
787 
788 		draw!false(x, y, r);
789 	}
790 	else
791 	{
792 		assert(v.w > 0 && v.h > 0);
793 		assert(x >= 0 && x+v.w <= w && y >= 0 && y+v.h <= h);
794 
795 		// TODO: alpha blending
796 		size_t dstStart = y*stride+x, srcStart = 0;
797 		foreach (j; 0..v.h)
798 			pixels[dstStart..dstStart+v.w] = v.pixels[srcStart..srcStart+v.w],
799 			dstStart += stride,
800 			srcStart += v.stride;
801 	}
802 
803 	return this;
804 }
805 
806 /// Downscale an image minding subpixel positioning on LCD screens.
807 void subpixelDownscale()()
808 	if (structFields!COLOR == ["r","g","b"] || structFields!COLOR == ["b","g","r"])
809 {
810 	Image!COLOR i;
811 	i.size(HRX + hr.w*3 + HRX, hr.h);
812 	i.draw(0, 0, hr.window(0, 0, HRX, hr.h));
813 	i.window(HRX, 0, HRX+hr.w*3, hr.h).upscaleDraw!(3, 1)(hr);
814 	i.draw(HRX + hr.w*3, 0, hr.window(hr.w-HRX, 0, hr.w, hr.h));
815 	alias Color!(COLOR.BaseType, "g") BASE;
816 	Image!BASE[3] channels;
817 	Image!BASE scratch;
818 	scratch.size(hr.w*3, hr.h);
819 
820 	foreach (xy_t cx, char c; ValueTuple!('r', 'g', 'b'))
821 	{
822 		auto w = i.window(cx*HRX, 0, cx*HRX+hr.w*3, hr.h);
823 		scratch.transformDraw!(`COLOR(c.`~c~`)`)(0, 0, w);
824 		channels[cx].size(lr.w, lr.h);
825 		channels[cx].downscaleDraw!(3*HRX, HRY)(scratch);
826 	}
827 
828 	foreach (y; 0..lr.h)
829 		foreach (x; 0..lr.w)
830 		{
831 			COLOR c;
832 			c.r = channels[0][x, y].g;
833 			c.g = channels[1][x, y].g;
834 			c.b = channels[2][x, y].g;
835 			lr[x, y] = c;
836 		}
837 }