-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill.pas
318 lines (257 loc) · 9.31 KB
/
fill.pas
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
unit fill;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, ExtCtrls, Controls, Graphics, hctypes, hcGlobals
, Math
//Graphics32 defined in Project options - Custom Options
{$IFDEF GR32} , GR32_L, GR32_Image {$ENDIF}
//BGRA Bitmap
, LResources, BGRABitmap, BGRABitmapTypes
, crt;
type
TDirection = (North, East, South, West);
TPixel = record
x, y: integer;
end;
var
x1, y1, x2, y2, i: integer;
_radius: single;
cur, mark, mark2: TPixel;
curDir, markDir, mark2Dir: TDirection;
backtrack, findloop: boolean;
procedure hc_Fill(Image1, Image2: TImage; Button: TMouseButton;
Shift: TShiftState; PX, PY: integer; mEvent: integer // 1:Down,2:Move:3:Up);
);
procedure FloodFillHC(x, y: integer; fillColor, borderColor: TColor; img: Timage);
procedure FloodFillHC2(x, y: integer; fillColor, borderColor: TColor; img: TImage);
procedure FloodFillHC3(x, y: integer; fillColor, borderColor: TColor; img: TImage);
{$IFDEF GR32} procedure FloodFillHC4(x, y: integer; fillColor, borderColor: TColor; image: TImage); {$ENDIF}
procedure FloodFillHC5(x, y: integer; fillColor, borderColor: TColor; img: TImage);
implementation
procedure hc_Fill(image1, Image2: TImage; Button: TMouseButton;
Shift: TShiftState; PX, PY: integer; mEvent: integer // 1:Down,2:Move:3:Up);
);
var
pointColor: TColor;
begin
if (Button = mbLeft) and (mEvent = hMouseDown) then //DOWN
begin
Image1.Canvas.Draw(0, 0, Image2.Picture.Graphic);
pointColor := image1.Picture.Bitmap.Canvas.Pixels[PX, PY];
image1.Picture.Bitmap.Canvas.Brush.Color := foreColor;
image1.Picture.Bitmap.Canvas.Brush.Style := bsSolid;
{$IFDEF MSWINDOWS}
//Win32 Api
//Image1.Picture.Bitmap.Canvas.FloodFill(px, py, pointcolor, TFillStyle.fsSurface);
FloodFillHC5(px, py, foreColor, image1.Picture.Bitmap.Canvas.Pixels[px,
py], image1);
{$ENDIF}
{$IFDEF UNIX}
FloodFillHC5( px,py,foreColor,image1.Picture.Bitmap.Canvas.Pixels[px, py],image1);
{$ENDIF}
Image1.Refresh;
end
else if (Button = mbLeft) and (mEvent = hMouseMove) then //MOVE
begin
end
else if (Button = mbLeft) and (mEvent = hMouseUp) then //UP
begin
Image2.Canvas.Draw(0, 0, Image1.Picture.Graphic);
end;
end;
procedure FloodFillHC5(x, y: integer; fillColor, borderColor: TColor; img: TImage);
var
bmp1: TBGRABitmap;
Pixel: TBGRAPixel;
begin
Pixel.FromColor(fillColor, 255);
bmp1 := TBGRABitmap.Create(img.Picture.Bitmap);
// bmp.FillRect(20, 20, 100, 40, BGRA(255, 192, 0), dmSet);
bmp1.FloodFill(X, Y, pixel, TFloodfillMode.fmSet);
bmp1.Draw(img.Canvas, 0, 0, True);
bmp1.Free;
end;
procedure FloodFillHC(x, y: integer; fillColor, borderColor: TColor; img: TImage);
begin
// Boundary check to ensure (x, y) is within the image dimensions
if (x < 0) or (x >= img.Picture.Bitmap.Width) or (y < 0) or
(y >= img.Picture.Bitmap.Height) then
Exit;
// If the pixel is the border color or already the fill color, do nothing
if (img.Picture.Bitmap.Canvas.Pixels[x, y] <> borderColor) or
(img.Picture.Bitmap.Canvas.Pixels[x, y] = fillColor) then
Exit;
// img.refresh;
// Set the current pixel to the fill color
img.Picture.Bitmap.Canvas.Pixels[x, y] := fillColor;
// Recursively fill adjacent pixels
FloodFillHC(x + 1, y, fillColor, borderColor, img);
FloodFillHC(x - 1, y, fillColor, borderColor, img);
FloodFillHC(x, y + 1, fillColor, borderColor, img);
FloodFillHC(x, y - 1, fillColor, borderColor, img);
end;
procedure FloodFillHC2(x, y: integer; fillColor, borderColor: TColor; img: TImage);
var
Stack: array of TPoint;
Current: TPoint;
Width, Height: integer;
Index: integer;
begin
// Get image dimensions
Width := img.Picture.Bitmap.Width;
Height := img.Picture.Bitmap.Height;
// Boundary check to ensure the starting point is within the image dimensions
if (x < 0) or (x >= Width) or (y < 0) or (y >= Height) then
Exit;
// If the starting pixel is the border color or already the fill color, do nothing
if (img.Picture.Bitmap.Canvas.Pixels[x, y] <> borderColor) or
(img.Picture.Bitmap.Canvas.Pixels[x, y] = fillColor) then
Exit;
// Initialize stack with the starting point
SetLength(Stack, 1);
Stack[0] := Point(x, y);
// Loop while there are points to process in the stack
while Length(Stack) > 0 do
begin
// Pop the top point from the stack
Index := High(Stack);
Current := Stack[Index];
SetLength(Stack, Index); // Remove the current point from the stack
// Check if the current point is within bounds and has the correct color
if (Current.X < 0) or (Current.X >= Width) or (Current.Y < 0) or
(Current.Y >= Height) or
(img.Picture.Bitmap.Canvas.Pixels[Current.X, Current.Y] <> borderColor) or
(img.Picture.Bitmap.Canvas.Pixels[Current.X, Current.Y] = fillColor) then
Continue;
// Set the current pixel to the fill color
img.Picture.Bitmap.Canvas.Pixels[Current.X, Current.Y] := fillColor;
// Push neighboring points onto the stack
SetLength(Stack, Length(Stack) + 4);
Stack[High(Stack) - 3] := Point(Current.X + 1, Current.Y);
Stack[High(Stack) - 2] := Point(Current.X - 1, Current.Y);
Stack[High(Stack) - 1] := Point(Current.X, Current.Y + 1);
Stack[High(Stack)] := Point(Current.X, Current.Y - 1);
end;
end;
procedure FloodFillHC3(x, y: integer; fillColor, borderColor: TColor; img: TImage);
var
Queue: array of TPoint;
Current: TPoint;
Width, Height: integer;
Head, Tail, Max, updateCount: integer;
begin
// Get image dimensions
Width := img.Picture.Bitmap.Width;
Height := img.Picture.Bitmap.Height;
// Boundary check to ensure the starting point is within the image dimensions
if (x < 0) or (x >= Width) or (y < 0) or (y >= Height) then
Exit;
// If the starting pixel is the border color or already the fill color, do nothing
if (img.Picture.Bitmap.Canvas.Pixels[x, y] = fillColor) then
Exit;
// Initialize queue with the starting point
Max := Width * Height;
SetLength(Queue, Max * 5);
Head := 0;
Tail := 1;
updateCount := 0;
Queue[Head] := Point(x, y);
// Loop while there are points to process in the queue
while (Head < Tail) do
begin
// Dequeue the front point
Current := Queue[Head];
Inc(Head);
// Check if the current point is within bounds and has the correct color
if (Current.X < 0) or (Current.X >= Width) or (Current.Y < 0) or
(Current.Y >= Height) or
(img.Picture.Bitmap.Canvas.Pixels[Current.X, Current.Y] <> borderColor) or
(img.Picture.Bitmap.Canvas.Pixels[Current.X, Current.Y] = fillColor) then
begin
Continue;
end;
// Set the current pixel to the fill color
img.Picture.Bitmap.Canvas.Pixels[Current.X, Current.Y] := fillColor;
Inc(updateCount);
if (updateCount >= 1000) then
begin
img.Refresh;
updateCount := 0;
end;
// Enqueue neighboring points
Queue[Tail] := Point(Current.X + 1, Current.Y);
Inc(Tail);
Queue[Tail] := Point(Current.X - 1, Current.Y);
Inc(Tail);
Queue[Tail] := Point(Current.X, Current.Y + 1);
Inc(Tail);
Queue[Tail] := Point(Current.X, Current.Y - 1);
Inc(Tail);
end;
end;
{$IFDEF GR32}
procedure FloodFillHC4(x, y: integer; fillColor, borderColor: TColor; image: TImage);
//Buggy don't use this
var
Queue: array of TPoint;
Current: TPoint;
_Width, _Height: integer;
Head, Tail, Max: integer;
img: TImage32;
begin
// Create an instance of TImage32
img := TImage32.Create(nil);
// Ensure the TImage32 has a valid Bitmap
img.Bitmap.SetSize(image.Picture.Bitmap.Width, image.Picture.Bitmap.Height);
// Copy the TImage bitmap to TImage32
// img.Bitmap.Assign(image.Picture.Bitmap);
// Get image dimensions
img.Bitmap.Canvas.Draw(x, y, image.Picture.bitmap);
_Width := image.Picture.Bitmap.Width;
_Height := image.Picture.Bitmap.Height;
// Boundary check to ensure the starting point is within the image dimensions
if (x < 0) or (x >= _Width) or (y < 0) or (y >= _Height) then
Exit;
// If the starting pixel is the border color or already the fill color, do nothing
//if (img.Picture.Bitmap.Canvas.Pixels[x, y] = fillColor) then
// Exit;
if (img.Bitmap.Pixel[x, y] = fillColor) then
Exit;
// Initialize queue with the starting point
Max := _Width * _Height;
SetLength(Queue, Max * 5);
Head := 0;
Tail := 1;
Queue[Head] := Point(x, y);
// Loop while there are points to process in the queue
while (Head < Tail) do
begin
// Dequeue the front point
Current := Queue[Head];
Inc(Head);
// Check if the current point is within bounds and has the correct color
if (Current.X < 0) or (Current.X >= _Width) or (Current.Y < 0) or
(Current.Y >= _Height) or (img.Bitmap.Pixel[Current.X, Current.Y] <>
borderColor) or (img.Bitmap.Pixel[Current.X, Current.Y] = fillColor) then
begin
Continue;
end;
// Set the current pixel to the fill color
img.Bitmap.Pixel[Current.X, Current.Y] := fillColor;
// Enqueue neighboring points
Queue[Tail] := Point(Current.X + 4, Current.Y);
Inc(Tail);
Queue[Tail] := Point(Current.X - 4, Current.Y);
Inc(Tail);
Queue[Tail] := Point(Current.X, Current.Y + 4);
Inc(Tail);
Queue[Tail] := Point(Current.X, Current.Y - 4);
Inc(Tail);
end;
//image.Picture.Bitmap.Canvas.Draw(0,0,);
image.Picture.Bitmap.Assign(img.Bitmap);
img.Free;
end;
{$ENDIF}
end.