This repository was archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
444 lines (389 loc) · 15.3 KB
/
Utils.cs
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using PaintDotNet;
using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BrushFilter
{
/// <summary>
/// Provides common functionality shared across multiple classes.
/// </summary>
static class Utils
{
#region Methods
/// <summary>
/// If the given value is out of range, it's clamped to the nearest
/// bound (low or high). Example: 104 in range 0 - 100 becomes 100.
/// </summary>
public static int Clamp(int value, int low, int high)
{
if (value < low)
{
value = low;
}
else if (value > high)
{
value = high;
}
return value;
}
/// <summary>
/// If the given value is out of range, it's clamped to the nearest
/// bound (low or high). Example: -0.1 in range 0 - 1 becomes 0.
/// </summary>
public static float ClampF(float value, float low, float high)
{
if (value < low)
{
value = low;
}
else if (value > high)
{
value = high;
}
return value;
}
/// <summary>
/// Overwrites alpha channel information in the dstImage with the
/// alpha information from the srcImage. Both images must be the
/// same size and Format32bppArgb. Returns success.
/// </summary>
/// <param name="srcImg">
/// The image to copy from.
/// </param>
/// <param name="dstImg">
/// The image to be overwritten.
/// </param>
public static unsafe bool CopyBitmapAlpha(Bitmap srcImg, Bitmap dstImg)
{
//Formats and size must be the same.
if (srcImg.PixelFormat != PixelFormat.Format32bppArgb ||
dstImg.PixelFormat != PixelFormat.Format32bppArgb ||
srcImg.Width != dstImg.Width ||
srcImg.Height != dstImg.Height)
{
return false;
}
BitmapData srcData = srcImg.LockBits(
new Rectangle(0, 0,
srcImg.Width,
srcImg.Height),
ImageLockMode.ReadOnly,
srcImg.PixelFormat);
BitmapData destData = dstImg.LockBits(
new Rectangle(0, 0,
dstImg.Width,
dstImg.Height),
ImageLockMode.WriteOnly,
dstImg.PixelFormat);
//Copies each pixel.
byte* srcRow = (byte*)srcData.Scan0;
byte* dstRow = (byte*)destData.Scan0;
int srcImgHeight = srcImg.Height;
int srcImgWidth = srcImg.Width;
Parallel.For(0, srcImgHeight, (y) =>
{
for (int x = 0; x < srcImgWidth; x++)
{
int ptr = y * srcData.Stride + x * 4;
dstRow[ptr + 3] = srcRow[ptr + 3];
}
});
srcImg.UnlockBits(srcData);
dstImg.UnlockBits(destData);
return true;
}
/// <summary>
/// Strictly copies all data from one bitmap over the other. They
/// must have the same size and pixel format. The image can be made
/// fully transparent without reducing color, which is used for an
/// "uncovering" effect. Returns success.
/// </summary>
/// <param name="srcImg">
/// The image to copy from.
/// </param>
/// <param name="dstImg">
/// The image to be overwritten.
/// </param>
public static unsafe bool CopyBitmapPure(Bitmap srcImg, Bitmap dstImg)
{
//Formats and size must be the same.
if (srcImg.PixelFormat != PixelFormat.Format32bppArgb ||
dstImg.PixelFormat != PixelFormat.Format32bppArgb ||
srcImg.Width != dstImg.Width ||
srcImg.Height != dstImg.Height)
{
return false;
}
BitmapData srcData = srcImg.LockBits(
new Rectangle(0, 0,
srcImg.Width,
srcImg.Height),
ImageLockMode.ReadOnly,
srcImg.PixelFormat);
BitmapData destData = dstImg.LockBits(
new Rectangle(0, 0,
dstImg.Width,
dstImg.Height),
ImageLockMode.WriteOnly,
dstImg.PixelFormat);
//Copies each pixel.
byte* srcRow = (byte*)srcData.Scan0;
byte* dstRow = (byte*)destData.Scan0;
int srcImgHeight = srcImg.Height;
int srcImgWidth = srcImg.Width;
Parallel.For(0, srcImgHeight, (y) =>
{
for (int x = 0; x < srcImgWidth; x++)
{
int ptr = y * srcData.Stride + x * 4;
dstRow[ptr] = srcRow[ptr];
dstRow[ptr + 1] = srcRow[ptr + 1];
dstRow[ptr + 2] = srcRow[ptr + 2];
dstRow[ptr + 3] = srcRow[ptr + 3];
}
});
srcImg.UnlockBits(srcData);
dstImg.UnlockBits(destData);
return true;
}
/// <summary>
/// Returns the original bitmap data in another format by drawing it.
/// All transparency is removed and made white.
/// </summary>
public static Bitmap FormatImage(Bitmap img, PixelFormat format)
{
Bitmap clone = new Bitmap(img.Width, img.Height, format);
using (Graphics gr = Graphics.FromImage(clone))
{
gr.SmoothingMode = SmoothingMode.None;
gr.DrawImage(img, 0, 0, img.Width, img.Height);
}
return clone;
}
/// <summary>
/// Constructs an outline of the given region with the given bounds
/// and scaling factor.
/// </summary>
/// <param name="region">
/// The selection to approximate.
/// </param>
/// <param name="bounds">
/// The boundaries of the image.
/// </param>
/// <param name="scalingMultiplier">
/// The amount to scale the size of the outline by.
/// </param>
public static PdnRegion ConstructOutline(
this PdnRegion region,
RectangleF bounds,
float scalingMultiplier)
{
GraphicsPath path = new GraphicsPath();
PdnRegion newRegion = region.Clone();
//The size to scale the region by.
Matrix scalematrix = new Matrix(
bounds,
new PointF[]{
new PointF(bounds.Left, bounds.Top),
new PointF(bounds.Right * scalingMultiplier, bounds.Top),
new PointF(bounds.Left, bounds.Bottom * scalingMultiplier)
});
newRegion.Transform(scalematrix);
//Makes the new region slightly larger by inflating rectangles.
foreach (RectangleF rect in newRegion.GetRegionScans())
{
path.AddRectangle(RectangleF.Inflate(rect, 1, 1));
}
//Subtracts the old region, leaving an outline from the expansion.
PdnRegion result = new PdnRegion(path);
result.Exclude(newRegion);
return result;
}
/// <summary>
/// Pads the given bitmap to be square.
/// </summary>
/// <param name="img">
/// The image to pad. The original is untouched.
/// </param>
public static Bitmap MakeBitmapSquare(Bitmap img)
{
//Exits if it's already square.
if (img.Width == img.Height)
{
return new Bitmap(img);
}
//Creates a new bitmap with the minimum square size.
int size = Math.Max(img.Height, img.Width);
Bitmap newImg = new Bitmap(size, size);
using (Graphics graphics = Graphics.FromImage(newImg))
{
graphics.FillRectangle(Brushes.White,
new Rectangle(0, 0, newImg.Width, newImg.Height));
graphics.DrawImage(img,
(size - img.Width) / 2,
(size - img.Height) / 2,
img.Width, img.Height);
}
return newImg;
}
/// <summary>
/// Returns a copy of the image, rotated about its center.
/// </summary>
/// <param name="origBmp">
/// The image to clone and change.
/// </param>
/// <param name="angle">
/// The angle in degrees; positive or negative.
/// </param>
public static Bitmap RotateImage(Bitmap origBmp, float angle)
{
//Performs nothing if there is no need.
if (angle == 0)
{
return origBmp;
}
//Places the angle in the range 0 <= x < 360.
while (angle < 0)
{
angle += 360;
}
while (angle >= 360)
{
angle -= 360;
}
//Calculates the new bounds of the image with trigonometry.
double radAngle = angle * Math.PI / 180;
double cos = Math.Abs(Math.Cos(radAngle));
double sin = Math.Abs(Math.Sin(radAngle));
int newWidth = (int)Math.Ceiling(origBmp.Width * cos + origBmp.Height * sin);
int newHeight = (int)Math.Ceiling(origBmp.Width * sin + origBmp.Height * cos);
//Creates the new image and a graphic canvas to draw the rotation.
Bitmap newBmp = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newBmp))
{
//Fills the image with white.
g.FillRectangle(Brushes.White, new Rectangle(0, 0, newBmp.Width, newBmp.Height));
//Uses matrices to centrally-rotate the original image.
g.TranslateTransform(
(float)(newWidth - origBmp.Width) / 2,
(float)(newHeight - origBmp.Height) / 2);
g.TranslateTransform(
(float)origBmp.Width / 2,
(float)origBmp.Height / 2);
g.RotateTransform(angle);
//Undoes the transform.
g.TranslateTransform(-(float)origBmp.Width / 2, -(float)origBmp.Height / 2);
//Draws the image.
g.DrawImage(origBmp, 0, 0, origBmp.Width, origBmp.Height);
}
return newBmp;
}
/// <summary>
/// Creates and returns a Winforms control for the given property
/// based on its type. When the control is modified, the property
/// is automatically updated.
/// </summary>
public static Control CreateControl(this Property prop, int propIndex, ControlInfo dlg)
{
if (prop is BooleanProperty)
{
var control = new CheckBox();
control.Checked = (bool)prop.Value;
control.CheckedChanged += (a, b) => { prop.Value = control.Checked; };
return control;
}
else if (prop is DoubleProperty propKnown)
{
var control = new TrackBar();
control.TickStyle = TickStyle.None;
control.Minimum = (int)(propKnown.MinValue * 100);
control.Maximum = (int)(propKnown.MaxValue * 100);
control.Value = (int)(propKnown.Value * 100);
control.ValueChanged += (a, b) => { prop.Value = control.Value / 100d; };
return control;
}
else if (prop is Int32Property propKnown2)
{
var control = new TrackBar();
control.TickStyle = TickStyle.None;
control.Minimum = propKnown2.MinValue;
control.Maximum = propKnown2.MaxValue;
control.Value = propKnown2.Value;
control.ValueChanged += (a, b) => { prop.Value = control.Value; };
return control;
}
else if (prop is StaticListChoiceProperty propKnown3)
{
var control = new ComboBox();
//Adds each option to the combobox as a tuple.
for (int i = 0; i < propKnown3.ValueChoices.Length; i++)
{
string name = propKnown3.Value.ToString();
var value = propKnown3.ValueChoices[i].ToString();
control.Items.Add(new Tuple<string, object>(name, value));
//Sets the default item in the combobox.
if (name == value)
{
control.SelectedIndex = i;
}
}
control.ValueMember = "Item1";
control.DisplayMember = "Item2";
control.SelectedValueChanged += (a, b) =>
{
prop.Value = propKnown3.ValueChoices[control.SelectedIndex];
};
return control;
}
return null;
}
/// <summary>
/// Creates and returns a Winforms label for the given property
/// based on its type. The label's Tag attribute contains the
/// name of the property.
/// </summary>
public static Label CreateLabel(this Property prop, int propIndex, ControlInfo dlg)
{
var control = new Label();
control.Tag = prop.Name;
//Probes for the display name or description of the property to use.
if (dlg.ChildControls.Count > propIndex)
{
var properties = dlg?.ChildControls[propIndex]?.ControlProperties?.Properties;
if (properties != null)
{
for (int i = 0; i < properties.Count(); i++)
{
var candidate = properties.ElementAt(i);
if (candidate.Name ==
ControlInfoPropertyNames.DisplayName.ToString() &&
!String.IsNullOrEmpty(candidate.Value.ToString()))
{
control.Tag = candidate.Value.ToString();
}
else if (candidate.Name ==
ControlInfoPropertyNames.Description.ToString() &&
!String.IsNullOrEmpty(candidate.Value.ToString()))
{
control.Tag = candidate.Value.ToString();
}
}
}
}
//The label always displays the value of the property.
control.Text = (string)control.Tag + ": " + prop.Value.ToString();
prop.ValueChanged += (a, b) =>
{
control.Text = (string)control.Tag + ": " + prop.Value.ToString();
};
return control;
}
#endregion
}
}