1 /*
2 Copyright (c) 2015-2021 Oleg Baharev, Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 /**
30  * Render simple geometric shapes
31  *
32  * Copyright: Oleg Baharev, Timur Gafarov 2015-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Oleg Baharev, Timur Gafarov
35  */
36 module dlib.image.render.shapes;
37 
38 import std.math;
39 import dlib.image.image;
40 import dlib.image.color;
41 
42 enum Black = Color4f(0, 0, 0, 1);
43 enum White = Color4f(1, 1, 1, 1);
44 
45 /// Fill image with solid color
46 void fillColor(SuperImage simg, Color4f col)
47 {
48     foreach(y; simg.col)
49     foreach(x; simg.row)
50         simg[x, y] = col;
51 }
52 
53 /// Draw a line segment
54 void drawLine(SuperImage img, Color4f color, int x1, int y1, int x2, int y2)
55 {
56     int dx = x2 - x1;
57     int ix = (dx > 0) - (dx < 0);
58     int dx2 = abs(dx) * 2;
59     int dy = y2 - y1;
60     int iy = (dy > 0) - (dy < 0);
61     int dy2 = abs(dy) * 2;
62     img[x1, y1] = color;
63 
64     if (dx2 >= dy2)
65     {
66         int error = dy2 - (dx2 / 2);
67         while (x1 != x2)
68         {
69             if (error >= 0 && (error || (ix > 0)))
70             {
71                 error -= dx2;
72                 y1 += iy;
73             }
74 
75             error += dy2;
76             x1 += ix;
77             img[x1, y1] = color;
78         }
79     }
80     else
81     {
82         int error = dx2 - (dy2 / 2);
83         while (y1 != y2)
84         {
85             if (error >= 0 && (error || (iy > 0)))
86             {
87                 error -= dy2;
88                 x1 += ix;
89             }
90 
91             error += dx2;
92             y1 += iy;
93             img[x1, y1] = color;
94         }
95     }
96 }
97 
98 /// Draw a filled circle
99 void drawCircle(SuperImage img, Color4f col, int x0, int y0, uint r)
100 {
101     int f = 1 - r;
102     int ddF_x = 0;
103     int ddF_y = -2 * r;
104     int x = 0;
105     int y = r;
106 
107     img[x0, y0 + r] = col;
108     img[x0, y0 - r] = col;
109     img[x0 + r, y0] = col;
110     img[x0 - r, y0] = col;
111 
112     while(x < y)
113     {
114         if(f >= 0)
115         {
116             y--;
117             ddF_y += 2;
118             f += ddF_y;
119         }
120         x++;
121         ddF_x += 2;
122         f += ddF_x + 1;
123         img[x0 + x, y0 + y] = col;
124         img[x0 - x, y0 + y] = col;
125         img[x0 + x, y0 - y] = col;
126         img[x0 - x, y0 - y] = col;
127         img[x0 + y, y0 + x] = col;
128         img[x0 - y, y0 + x] = col;
129         img[x0 + y, y0 - x] = col;
130         img[x0 - y, y0 - x] = col;
131     }
132 }