@@ -104,171 +104,6 @@ List<Point> BresenhamMidPointAlgorithm(Point start, Point end)
104
104
return points ;
105
105
}
106
106
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
-
272
107
Bitmap GuptaSproullAlgorithm ( Bitmap bmp )
273
108
// http://elynxsdk.free.fr/ext-docs/Rasterization/Antialiasing/Gupta%20sproull%20antialiased%20lines.htm
274
109
// https://jamesarich.weebly.com/uploads/1/4/0/3/14035069/480xprojectreport.pdf
@@ -316,8 +151,13 @@ Bitmap GuptaSproullAlgorithm(Bitmap bmp)
316
151
do
317
152
{
318
153
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);
321
161
322
162
//newColorPixel(pw, pr, x, y, twovdu * invD, color);
323
163
//newColorPixel(pw, pr, x, y + iy, invD2du - twovdu * invD, color);
@@ -346,8 +186,11 @@ Bitmap GuptaSproullAlgorithm(Bitmap bmp)
346
186
do
347
187
{
348
188
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
+ }
351
194
352
195
//newColorPixel(pw, pr, x, y, twovdu * invD, color);
353
196
//newColorPixel(pw, pr, x, y + iy, invD2du - twovdu * invD, color);
@@ -379,12 +222,10 @@ void newColorPixel(Bitmap bmp, int x, int y, double dist)
379
222
double value = 1 - Math . Pow ( ( dist * 2 / 3 ) , 2 ) ;
380
223
381
224
Color old = bmp . GetPixelFast ( x , y ) ;
382
-
383
225
Color col = ColorInterpolator . InterpolateBetween ( old , shapeColor , value ) ;
384
226
385
227
bmp . SetPixelFast ( x , y , col ) ;
386
228
}
387
229
388
-
389
230
}
390
231
}
0 commit comments