2bae28bce5369c077bfb284d30184c83d01c8af6
[reactos.git] / reactos / base / applications / paint / drawing.c
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: drawing.c
5 * PURPOSE: The drawing functions used by the tools
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include <windows.h>
12
13 /* FUNCTIONS ********************************************************/
14
15 void Line(HDC hdc, short x1, short y1, short x2, short y2, int color, int thickness)
16 {
17 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, color));
18 MoveToEx(hdc, x1, y1, NULL);
19 LineTo(hdc, x2, y2);
20 DeleteObject(SelectObject(hdc, oldPen));
21 }
22
23 void Rect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, int style)
24 {
25 HBRUSH oldBrush;
26 LOGBRUSH logbrush;
27 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
28 if (style==0) logbrush.lbStyle = BS_HOLLOW; else logbrush.lbStyle = BS_SOLID;
29 if (style==2) logbrush.lbColor = fg; else logbrush.lbColor = bg;
30 logbrush.lbHatch = 0;
31 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
32 Rectangle(hdc, x1, y1, x2, y2);
33 DeleteObject(SelectObject(hdc, oldBrush));
34 DeleteObject(SelectObject(hdc, oldPen));
35 }
36
37 void Ellp(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, int style)
38 {
39 HBRUSH oldBrush;
40 LOGBRUSH logbrush;
41 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
42 if (style==0) logbrush.lbStyle = BS_HOLLOW; else logbrush.lbStyle = BS_SOLID;
43 if (style==2) logbrush.lbColor = fg; else logbrush.lbColor = bg;
44 logbrush.lbHatch = 0;
45 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
46 Ellipse(hdc, x1, y1, x2, y2);
47 DeleteObject(SelectObject(hdc, oldBrush));
48 DeleteObject(SelectObject(hdc, oldPen));
49 }
50
51 void RRect(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int thickness, int style)
52 {
53 LOGBRUSH logbrush;
54 HBRUSH oldBrush;
55 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
56 if (style==0) logbrush.lbStyle = BS_HOLLOW; else logbrush.lbStyle = BS_SOLID;
57 if (style==2) logbrush.lbColor = fg; else logbrush.lbColor = bg;
58 logbrush.lbHatch = 0;
59 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
60 RoundRect(hdc, x1, y1, x2, y2, 16, 16);
61 DeleteObject(SelectObject(hdc, oldBrush));
62 DeleteObject(SelectObject(hdc, oldPen));
63 }
64
65 void Poly(HDC hdc, POINT *lpPoints, int nCount, int fg, int bg, int thickness, int style, BOOL closed)
66 {
67 LOGBRUSH logbrush;
68 HBRUSH oldBrush;
69 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, fg));
70 if (style==0) logbrush.lbStyle = BS_HOLLOW; else logbrush.lbStyle = BS_SOLID;
71 if (style==2) logbrush.lbColor = fg; else logbrush.lbColor = bg;
72 logbrush.lbHatch = 0;
73 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
74 if (closed)
75 Polygon(hdc, lpPoints, nCount);
76 else
77 Polyline(hdc, lpPoints, nCount);
78 DeleteObject(SelectObject(hdc, oldBrush));
79 DeleteObject(SelectObject(hdc, oldPen));
80 }
81
82 void Bezier(HDC hdc, POINT p1, POINT p2, POINT p3, POINT p4, int color, int thickness)
83 {
84 POINT fourPoints[4];
85 fourPoints[0] = p1;
86 fourPoints[1] = p2;
87 fourPoints[2] = p3;
88 fourPoints[3] = p4;
89 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, color));
90 PolyBezier(hdc, fourPoints, 4);
91 DeleteObject(SelectObject(hdc, oldPen));
92 }
93
94 void Fill(HDC hdc, int x, int y, int color)
95 {
96 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
97 ExtFloodFill(hdc, x, y, GetPixel(hdc, x, y), FLOODFILLSURFACE);
98 DeleteObject(SelectObject(hdc, oldBrush));
99 }
100
101 void Erase(HDC hdc, short x1, short y1, short x2, short y2, int color, int radius)
102 {
103 short a;
104 HPEN oldPen;
105 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
106 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
107 for (a=0; a<=100; a++)
108 Rectangle(hdc, (x1*(100-a)+x2*a)/100-radius+1, (y1*(100-a)+y2*a)/100-radius+1, (x1*(100-a)+x2*a)/100+radius+1, (y1*(100-a)+y2*a)/100+radius+1);
109 DeleteObject(SelectObject(hdc, oldBrush));
110 DeleteObject(SelectObject(hdc, oldPen));
111 }
112
113 void Replace(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int radius)
114 {
115 short a, x, y;
116 for (a=0; a<=100; a++)
117 for (y=(y1*(100-a)+y2*a)/100-radius+1; y<(y1*(100-a)+y2*a)/100+radius+1; y++)
118 for (x=(x1*(100-a)+x2*a)/100-radius+1; x<(x1*(100-a)+x2*a)/100+radius+1; x++)
119 if (GetPixel(hdc, x, y)==fg) SetPixel(hdc, x, y, bg);
120 }
121
122 void Airbrush(HDC hdc, short x, short y, int color, int r)
123 {
124 short a;
125 short b;
126 for (b=-r; b<=r; b++) for (a=-r; a<=r; a++) if ((a*a+b*b<=r*r)&&(rand()%4==0)) SetPixel(hdc, x+a, y+b, color);
127 }
128
129 void Brush(HDC hdc, short x1, short y1, short x2, short y2, int color, int style)
130 {
131 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
132 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
133 short a;
134 switch (style)
135 {
136 case 0:
137 for (a=0; a<=100; a++)
138 Ellipse(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, (x1*(100-a)+x2*a)/100+4, (y1*(100-a)+y2*a)/100+4);
139 break;
140 case 1:
141 for (a=0; a<=100; a++)
142 Ellipse(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
143 break;
144 case 2:
145 MoveToEx(hdc, x1, y1, NULL);
146 LineTo(hdc, x2, y2);
147 SetPixel(hdc, x2, y2, color);
148 break;
149 case 3:
150 for (a=0; a<=100; a++)
151 Rectangle(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);
152 break;
153 case 4:
154 for (a=0; a<=100; a++)
155 Rectangle(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
156 break;
157 case 5:
158 for (a=0; a<=100; a++)
159 Rectangle(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);
160 break;
161 case 6:
162 for (a=0; a<=100; a++)
163 {
164 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100+5, NULL);
165 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100-3);
166 }
167 break;
168 case 7:
169 for (a=0; a<=100; a++)
170 {
171 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100+3, NULL);
172 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100-2);
173 }
174 break;
175 case 8:
176 for (a=0; a<=100; a++)
177 {
178 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100+1, NULL);
179 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100-1);
180 }
181 break;
182 case 9:
183 for (a=0; a<=100; a++)
184 {
185 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, NULL);
186 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);
187 }
188 break;
189 case 10:
190 for (a=0; a<=100; a++)
191 {
192 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, NULL);
193 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
194 }
195 break;
196 case 11:
197 for (a=0; a<=100; a++)
198 {
199 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, NULL);
200 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);
201 }
202 break;
203 }
204 DeleteObject(SelectObject(hdc, oldBrush));
205 DeleteObject(SelectObject(hdc, oldPen));
206 }
207
208 void RectSel(HDC hdc, short x1, short y1, short x2, short y2)
209 {
210 HBRUSH oldBrush;
211 LOGBRUSH logbrush;
212 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
213 logbrush.lbStyle = BS_HOLLOW;
214 logbrush.lbColor = 0;
215 logbrush.lbHatch = 0;
216 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
217 Rectangle(hdc, x1, y1, x2, y2);
218 DeleteObject(SelectObject(hdc, oldBrush));
219 DeleteObject(SelectObject(hdc, oldPen));
220 }
221
222 void SelectionFrame(HDC hdc, int x1, int y1, int x2, int y2)
223 {
224 HBRUSH oldBrush;
225 LOGBRUSH logbrush;
226 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
227 logbrush.lbStyle = BS_HOLLOW;
228 logbrush.lbColor = 0;
229 logbrush.lbHatch = 0;
230 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
231 Rectangle(hdc, x1, y1, x2, y2);
232 DeleteObject(SelectObject(hdc, oldBrush));
233 DeleteObject(SelectObject(hdc, oldPen));
234 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00000000));
235 oldBrush = SelectObject(hdc, CreateSolidBrush(0x00000000));
236 Rectangle(hdc, x1-1, y1-1, x1+2, y1+2);
237 Rectangle(hdc, x2-2, y1-1, x2+2, y1+2);
238 Rectangle(hdc, x1-1, y2-2, x1+2, y2+1);
239 Rectangle(hdc, x2-2, y2-2, x2+2, y2+1);
240 Rectangle(hdc, (x1+x2)/2-1, y1-1, (x1+x2)/2+2, y1+2);
241 Rectangle(hdc, (x1+x2)/2-1, y2-2, (x1+x2)/2+2, y2+1);
242 Rectangle(hdc, x1-1, (y1+y2)/2-1, x1+2, (y1+y2)/2+2);
243 Rectangle(hdc, x2-2, (y1+y2)/2-1, x2+1, (y1+y2)/2+2);
244 DeleteObject(SelectObject(hdc, oldBrush));
245 DeleteObject(SelectObject(hdc, oldPen));
246 }