Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 0a4886e

Browse files
committed
Update with bg color
1 parent f2ec814 commit 0a4886e

File tree

5 files changed

+42
-173
lines changed

5 files changed

+42
-173
lines changed

winforms-image-processor/winforms-image-processor/DrawTools/ColorInterpolator.cs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static Color InterpolateBetween(
2424
{
2525
throw new ArgumentOutOfRangeException("lambda");
2626
}
27+
Console.WriteLine(lambda);
2728
Color color = Color.FromArgb(
2829
InterpolateComponent(endPoint1, endPoint2, lambda, _redSelector),
2930
InterpolateComponent(endPoint1, endPoint2, lambda, _greenSelector),

winforms-image-processor/winforms-image-processor/DrawTools/MidPointLine.cs

+12-171
Original file line numberDiff line numberDiff line change
@@ -104,171 +104,6 @@ List<Point> BresenhamMidPointAlgorithm(Point start, Point end)
104104
return points;
105105
}
106106

107-
public byte[] AALine(byte[] pixels, int stride)
108-
{
109-
110-
var bytes = pixels;
111-
112-
int x0 = startPoint.Value.X, y0 = startPoint.Value.Y;
113-
int x1 = endPoint.Value.X, y1 = endPoint.Value.Y;
114-
115-
int sa = shapeColor.A;
116-
uint sg = shapeColor.G;
117-
uint srb = (uint)int.Parse($"00{Convert.ToString(shapeColor.R, 16)}00{Convert.ToString(shapeColor.B, 16)}", System.Globalization.NumberStyles.HexNumber);
118-
119-
int pixelWidth = stride / 4;
120-
int pixelHeight = bytes.Length / stride;
121-
122-
if ((x0 == x1) && (y0 == y1)) return new byte[0]; // edge case causing invDFloat to overflow, found by Shai Rubinshtein
123-
124-
if (x0 < 1) x0 = 1;
125-
if (x0 > pixelWidth - 2) x0 = pixelWidth - 2;
126-
if (y0 < 1) y0 = 1;
127-
if (y0 > pixelHeight - 2) y0 = pixelHeight - 2;
128-
129-
if (x1 < 1) x1 = 1;
130-
if (x1 > pixelWidth - 2) x1 = pixelWidth - 2;
131-
if (y1 < 1) y1 = 1;
132-
if (y1 > pixelHeight - 2) y1 = pixelHeight - 2;
133-
134-
int addr = y0 * pixelWidth + x0;
135-
int dx = x1 - x0;
136-
int dy = y1 - y0;
137-
138-
int du;
139-
int dv;
140-
int u;
141-
int v;
142-
int uincr;
143-
int vincr;
144-
145-
// By switching to (u,v), we combine all eight octants
146-
int adx = dx, ady = dy;
147-
if (dx < 0) adx = -dx;
148-
if (dy < 0) ady = -dy;
149-
150-
if (adx > ady)
151-
{
152-
du = adx;
153-
dv = ady;
154-
u = x1;
155-
v = y1;
156-
uincr = 1;
157-
vincr = pixelWidth;
158-
if (dx < 0) uincr = -uincr;
159-
if (dy < 0) vincr = -vincr;
160-
161-
}
162-
else
163-
{
164-
du = ady;
165-
dv = adx;
166-
u = y1;
167-
v = x1;
168-
uincr = pixelWidth;
169-
vincr = 1;
170-
if (dy < 0) uincr = -uincr;
171-
if (dx < 0) vincr = -vincr;
172-
}
173-
174-
int uend = u + du;
175-
int d = (dv << 1) - du; // Initial value as in Bresenham's
176-
int incrS = dv << 1; // Δd for straight increments
177-
int incrD = (dv - du) << 1; // Δd for diagonal increments
178-
179-
double invDFloat = 1.0 / (4.0 * Math.Sqrt(du * du + dv * dv)); // Precomputed inverse denominator
180-
double invD2duFloat = 0.75 - 2.0 * (du * invDFloat); // Precomputed constant
181-
182-
const int PRECISION_SHIFT = 10; // result distance should be from 0 to 1 << PRECISION_SHIFT, mapping to a range of 0..1
183-
const int PRECISION_MULTIPLIER = 1 << PRECISION_SHIFT;
184-
int invD = (int)(invDFloat * PRECISION_MULTIPLIER);
185-
int invD2du = (int)(invD2duFloat * PRECISION_MULTIPLIER * sa);
186-
int ZeroDot75 = (int)(0.75 * PRECISION_MULTIPLIER * sa);
187-
188-
int invDMulAlpha = invD * sa;
189-
int duMulInvD = du * invDMulAlpha; // used to help optimize twovdu * invD
190-
int dMulInvD = d * invDMulAlpha; // used to help optimize twovdu * invD
191-
//int twovdu = 0; // Numerator of distance; starts at 0
192-
int twovduMulInvD = 0; // since twovdu == 0
193-
int incrSMulInvD = incrS * invDMulAlpha;
194-
int incrDMulInvD = incrD * invDMulAlpha;
195-
196-
do
197-
{
198-
AlphaBlendNormalOnPremultiplied(pixels, addr, (ZeroDot75 - twovduMulInvD) >> PRECISION_SHIFT, srb, sg);
199-
AlphaBlendNormalOnPremultiplied(pixels, addr + vincr, (invD2du + twovduMulInvD) >> PRECISION_SHIFT, srb, sg);
200-
AlphaBlendNormalOnPremultiplied(pixels, addr - vincr, (invD2du - twovduMulInvD) >> PRECISION_SHIFT, srb, sg);
201-
202-
if (d < 0)
203-
{
204-
// choose straight (u direction)
205-
twovduMulInvD = dMulInvD + duMulInvD;
206-
d += incrS;
207-
dMulInvD += incrSMulInvD;
208-
}
209-
else
210-
{
211-
// choose diagonal (u+v direction)
212-
twovduMulInvD = dMulInvD - duMulInvD;
213-
d += incrD;
214-
dMulInvD += incrDMulInvD;
215-
v++;
216-
addr += vincr;
217-
}
218-
u++;
219-
addr += uincr;
220-
} while (u < uend);
221-
222-
return bytes;
223-
}
224-
225-
/// <summary>
226-
/// Blends a specific source color on top of a destination premultiplied color
227-
/// </summary>
228-
/// <param name="pixels">Array containing destination color</param>
229-
/// <param name="index">Index of destination pixel</param>
230-
/// <param name="sa">Source alpha (0..255)</param>
231-
/// <param name="srb">Source non-premultiplied red and blue component in the format 0x00rr00bb</param>
232-
/// <param name="sg">Source green component (0..255)</param>
233-
private void AlphaBlendNormalOnPremultiplied(byte[] pixels, int index, int sa, uint srb, uint sg)
234-
{
235-
uint destPixel = (uint)pixels[index];
236-
uint da, dg, drb;
237-
238-
da = (destPixel >> 24);
239-
dg = ((destPixel >> 8) & 0xff);
240-
drb = destPixel & 0x00FF00FF;
241-
242-
// blend with high-quality alpha and lower quality but faster 1-off RGBs
243-
//Color col = Color.FromArgb((int)(
244-
// ((sa + ((da * (255 - sa) * 0x8081) >> 23)) << 24) | // aplha
245-
// (((sg - dg) * sa + (dg << 8)) & 0xFFFFFF00) | // green
246-
// (((((srb - drb) * sa) >> 8) + drb) & 0x00FF00FF) // red and blue
247-
//));
248-
249-
uint dr = ((destPixel >> 16) & 0xff);
250-
uint db = ((destPixel) & 0xff);
251-
252-
int sb = shapeColor.B;
253-
int sr = shapeColor.R;
254-
255-
pixels[index * 4] = (byte)((((sb - db) * sa) >> 8) + db);
256-
pixels[index * 4 + 1] = (byte)(((sg - dg) * sa + (dg << 8)) & 0xFFFFFF00);
257-
pixels[index * 4 + 2] = (byte)(((((sr - dr) * sa) >> 8) + dr) << 16);
258-
pixels[index * 4 + 3] = (byte)((sa + ((da * (255 - sa) * 0x8081) >> 23)) << 24);
259-
260-
261-
262-
//uint srb = (uint)((sr << 16) | sb);
263-
264-
265-
//pixels[index] = (int)(
266-
// ((sa + ((da * (255 - sa) * 0x8081) >> 23)) << 24) | // alpha
267-
// (((((sr - dr) * sa) >> 8) + dr) << 16) | // red
268-
// ( ((sg - dg) * sa + (dg << 8)) & 0xFFFFFF00 ) | // green
269-
// ( (((sb - db) * sa) >> 8) + db ) ); // blue
270-
}
271-
272107
Bitmap GuptaSproullAlgorithm(Bitmap bmp)
273108
// http://elynxsdk.free.fr/ext-docs/Rasterization/Antialiasing/Gupta%20sproull%20antialiased%20lines.htm
274109
// https://jamesarich.weebly.com/uploads/1/4/0/3/14035069/480xprojectreport.pdf
@@ -316,8 +151,13 @@ Bitmap GuptaSproullAlgorithm(Bitmap bmp)
316151
do
317152
{
318153
newColorPixel(bmp, x, y, twovdu * invD);
319-
newColorPixel(bmp, x, y + iy, invD2du - twovdu * invD);
320-
newColorPixel(bmp, x, y - iy, invD2du + twovdu * invD);
154+
for (int i = 0; i < thickness; i++)
155+
{
156+
newColorPixel(bmp, x, y + i * iy, invD2du - i * twovdu * invD);
157+
newColorPixel(bmp, x, y - i * iy, invD2du + i * twovdu * invD);
158+
}
159+
//newColorPixel(bmp, x, y + iy, invD2du - twovdu * invD);
160+
//newColorPixel(bmp, x, y - iy, invD2du + twovdu * invD);
321161

322162
//newColorPixel(pw, pr, x, y, twovdu * invD, color);
323163
//newColorPixel(pw, pr, x, y + iy, invD2du - twovdu * invD, color);
@@ -346,8 +186,11 @@ Bitmap GuptaSproullAlgorithm(Bitmap bmp)
346186
do
347187
{
348188
newColorPixel(bmp, x, y, twovdu * invD);
349-
newColorPixel(bmp, x, y + iy, invD2du - twovdu * invD);
350-
newColorPixel(bmp, x, y - iy, invD2du + twovdu * invD);
189+
for (int i = 0; i < thickness; i++)
190+
{
191+
newColorPixel(bmp, x, y + i * iy, invD2du - i * twovdu * invD);
192+
newColorPixel(bmp, x, y - i * iy, invD2du + i * twovdu * invD);
193+
}
351194

352195
//newColorPixel(pw, pr, x, y, twovdu * invD, color);
353196
//newColorPixel(pw, pr, x, y + iy, invD2du - twovdu * invD, color);
@@ -379,12 +222,10 @@ void newColorPixel(Bitmap bmp, int x, int y, double dist)
379222
double value = 1 - Math.Pow((dist * 2 / 3), 2);
380223

381224
Color old = bmp.GetPixelFast(x,y);
382-
383225
Color col = ColorInterpolator.InterpolateBetween(old, shapeColor, value);
384226

385227
bmp.SetPixelFast(x, y, col);
386228
}
387229

388-
389230
}
390231
}

winforms-image-processor/winforms-image-processor/Forms/DrawForm.Designer.cs

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

winforms-image-processor/winforms-image-processor/Forms/DrawForm.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ private void newToolStripMenuItem_Click(object sender, EventArgs e)
3434
toolsToolStripMenuItem.Enabled = true;
3535
}
3636

37+
Color backColor = Color.Green;
3738
Bitmap NewBitmap()
3839
{
3940
var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
4041

4142
Graphics g = Graphics.FromImage(bmp);
42-
g.Clear(Color.Green);
43+
g.Clear(backColor);
4344

4445
return bmp;
4546
}
@@ -213,7 +214,18 @@ private void openToolStripMenuItem_Click(object sender, EventArgs e)
213214

214215
private void antialiasingToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
215216
{
217+
if (pictureBox1.Image == null)
218+
return;
216219
RefreshShapes();
217220
}
221+
222+
private void backgroundColorToolStripMenuItem_Click(object sender, EventArgs e)
223+
{
224+
if (colorDialog2.ShowDialog() == DialogResult.OK)
225+
{
226+
backColor = colorDialog2.Color;
227+
RefreshShapes();
228+
}
229+
}
218230
}
219231
}

winforms-image-processor/winforms-image-processor/Forms/DrawForm.resx

+3
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,7 @@
123123
<metadata name="colorDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
124124
<value>132, 17</value>
125125
</metadata>
126+
<metadata name="colorDialog2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
127+
<value>253, 17</value>
128+
</metadata>
126129
</root>

0 commit comments

Comments
 (0)