- strmini.h: Include windef.h so CDECL gets defined for msvc version of DEFINE_GUIDEX...
[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 HPEN oldPen;
85 POINT fourPoints[4];
86 fourPoints[0] = p1;
87 fourPoints[1] = p2;
88 fourPoints[2] = p3;
89 fourPoints[3] = p4;
90 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, thickness, color));
91 PolyBezier(hdc, fourPoints, 4);
92 DeleteObject(SelectObject(hdc, oldPen));
93 }
94
95 void Fill(HDC hdc, int x, int y, int color)
96 {
97 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
98 ExtFloodFill(hdc, x, y, GetPixel(hdc, x, y), FLOODFILLSURFACE);
99 DeleteObject(SelectObject(hdc, oldBrush));
100 }
101
102 void Erase(HDC hdc, short x1, short y1, short x2, short y2, int color, int radius)
103 {
104 short a;
105 HPEN oldPen;
106 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
107 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
108 for (a=0; a<=100; a++)
109 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);
110 DeleteObject(SelectObject(hdc, oldBrush));
111 DeleteObject(SelectObject(hdc, oldPen));
112 }
113
114 void Replace(HDC hdc, short x1, short y1, short x2, short y2, int fg, int bg, int radius)
115 {
116 short a, x, y;
117 for (a=0; a<=100; a++)
118 for (y=(y1*(100-a)+y2*a)/100-radius+1; y<(y1*(100-a)+y2*a)/100+radius+1; y++)
119 for (x=(x1*(100-a)+x2*a)/100-radius+1; x<(x1*(100-a)+x2*a)/100+radius+1; x++)
120 if (GetPixel(hdc, x, y)==fg) SetPixel(hdc, x, y, bg);
121 }
122
123 void Airbrush(HDC hdc, short x, short y, int color, int r)
124 {
125 short a;
126 short b;
127 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);
128 }
129
130 void Brush(HDC hdc, short x1, short y1, short x2, short y2, int color, int style)
131 {
132 HPEN oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
133 HBRUSH oldBrush = SelectObject(hdc, CreateSolidBrush(color));
134 short a;
135 switch (style)
136 {
137 case 0:
138 for (a=0; a<=100; a++)
139 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);
140 break;
141 case 1:
142 for (a=0; a<=100; a++)
143 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);
144 break;
145 case 2:
146 MoveToEx(hdc, x1, y1, NULL);
147 LineTo(hdc, x2, y2);
148 SetPixel(hdc, x2, y2, color);
149 break;
150 case 3:
151 for (a=0; a<=100; a++)
152 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);
153 break;
154 case 4:
155 for (a=0; a<=100; a++)
156 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);
157 break;
158 case 5:
159 for (a=0; a<=100; a++)
160 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);
161 break;
162 case 6:
163 for (a=0; a<=100; a++)
164 {
165 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100+5, NULL);
166 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100-3);
167 }
168 break;
169 case 7:
170 for (a=0; a<=100; a++)
171 {
172 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100+3, NULL);
173 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100-2);
174 }
175 break;
176 case 8:
177 for (a=0; a<=100; a++)
178 {
179 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100+1, NULL);
180 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100-1);
181 }
182 break;
183 case 9:
184 for (a=0; a<=100; a++)
185 {
186 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-3, (y1*(100-a)+y2*a)/100-3, NULL);
187 LineTo(hdc, (x1*(100-a)+x2*a)/100+5, (y1*(100-a)+y2*a)/100+5);
188 }
189 break;
190 case 10:
191 for (a=0; a<=100; a++)
192 {
193 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-2, (y1*(100-a)+y2*a)/100-2, NULL);
194 LineTo(hdc, (x1*(100-a)+x2*a)/100+3, (y1*(100-a)+y2*a)/100+3);
195 }
196 break;
197 case 11:
198 for (a=0; a<=100; a++)
199 {
200 MoveToEx(hdc, (x1*(100-a)+x2*a)/100-1, (y1*(100-a)+y2*a)/100-1, NULL);
201 LineTo(hdc, (x1*(100-a)+x2*a)/100+1, (y1*(100-a)+y2*a)/100+1);
202 }
203 break;
204 }
205 DeleteObject(SelectObject(hdc, oldBrush));
206 DeleteObject(SelectObject(hdc, oldPen));
207 }
208
209 void RectSel(HDC hdc, short x1, short y1, short x2, short y2)
210 {
211 HBRUSH oldBrush;
212 LOGBRUSH logbrush;
213 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
214 logbrush.lbStyle = BS_HOLLOW;
215 logbrush.lbColor = 0;
216 logbrush.lbHatch = 0;
217 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
218 Rectangle(hdc, x1, y1, x2, y2);
219 DeleteObject(SelectObject(hdc, oldBrush));
220 DeleteObject(SelectObject(hdc, oldPen));
221 }
222
223 void SelectionFrame(HDC hdc, int x1, int y1, int x2, int y2)
224 {
225 HBRUSH oldBrush;
226 LOGBRUSH logbrush;
227 HPEN oldPen = SelectObject(hdc, CreatePen(PS_DOT, 1, 0x00000000));
228 logbrush.lbStyle = BS_HOLLOW;
229 logbrush.lbColor = 0;
230 logbrush.lbHatch = 0;
231 oldBrush = SelectObject(hdc, CreateBrushIndirect(&logbrush));
232 Rectangle(hdc, x1, y1, x2, y2);
233 DeleteObject(SelectObject(hdc, oldBrush));
234 DeleteObject(SelectObject(hdc, oldPen));
235 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, 0x00000000));
236 oldBrush = SelectObject(hdc, CreateSolidBrush(0x00000000));
237 Rectangle(hdc, x1-1, y1-1, x1+2, y1+2);
238 Rectangle(hdc, x2-2, y1-1, x2+2, y1+2);
239 Rectangle(hdc, x1-1, y2-2, x1+2, y2+1);
240 Rectangle(hdc, x2-2, y2-2, x2+2, y2+1);
241 Rectangle(hdc, (x1+x2)/2-1, y1-1, (x1+x2)/2+2, y1+2);
242 Rectangle(hdc, (x1+x2)/2-1, y2-2, (x1+x2)/2+2, y2+1);
243 Rectangle(hdc, x1-1, (y1+y2)/2-1, x1+2, (y1+y2)/2+2);
244 Rectangle(hdc, x2-2, (y1+y2)/2-1, x2+1, (y1+y2)/2+2);
245 DeleteObject(SelectObject(hdc, oldBrush));
246 DeleteObject(SelectObject(hdc, oldPen));
247 }