-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
311 lines (279 loc) · 7.27 KB
/
Program.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
using FastBitmap;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AreaSmoother
{
class Program
{
//Test Sqrt
//static void Main(string[] args)
//{
// for(double a=1.0; a<100.0; a+=0.1)
// {
// Console.WriteLine(Math.Sqrt(a)+" "+Sqrt(a));
// }
// Console.WriteLine(new String('=',100));
// for(float f=1.0f; f<100.0f; f+=0.1f)
// {
// Console.WriteLine(Math.Sqrt(f)+" "+Sqrt(f));
// }
//}
static void Main(string[] args)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;
Trace.Listeners.Add(new ConsoleTraceListener());
if (args.Length < 1)
{
Console.WriteLine("specify image file");
return;
}
string filein = args[0];
string fileout = args.Length > 1
? args[1]
: filein+"s.png"
;
var fsin = File.Open(filein,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
Bitmap bmpin = new Bitmap(fsin);
Bitmap bmpot = new Bitmap(bmpin.Width,bmpin.Height,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using(LockBitmap lbin = new LockBitmap(bmpin))
using(LockBitmap lbot = new LockBitmap(bmpot))
{
lbot.LockBits();
lbin.LockBits();
DoSmooting(lbin,lbot);
lbot.UnlockBits();
lbin.UnlockBits();
}
bmpot.Save(fileout);
}
static void DoSmooting(LockBitmap lbin,LockBitmap lbot)
{
int ht = lbin.Height;
int wt = lbin.Width;
int total = ht*wt-1;
int count = 0;
var pp = Parallel.For(0,total,(i) => {
int y = i / wt;
int x = i % wt;
Color nc = SmoothPixel(lbin,x,y);
lbot.SetPixel(x,y,nc);
System.Threading.Interlocked.Increment(ref count);
if (count % 1000 == 0) {
Debug.WriteLine(count+" / "+total);
}
});
//for(int y=ht-1; y>=0; y--)
//{
// for(int x=wt-1; x>=0; x--)
// {
// Color nc = SmoothPixel(lbin,x,y);
// lbot.SetPixel(x,y,nc);
// }
// Debug.WriteLine("y = "+y);
//}
}
static Color SmoothPixel(LockBitmap lb,int px, int py)
{
Color start = lb.GetPixel(px,py);
double bestlen = double.MaxValue;
double bestang = double.NaN;
double bestratio = 1;
Color bestfc = start;
Color bestbc = start;
Point bestfpx = new Point(px,py);
Point bestbpx = new Point(px,py);
double ahigh = Math.PI;
double alow = 0;
for(int tries=1; tries <= 7; tries++)
{
double dang = (ahigh - alow)/3;
for(double a = alow; a<ahigh; a+=dang)
{
Color fc;
Color bc;
Point fp = FindColorAlongRay(lb,a,px,py,false,start,out fc);
Point bp = FindColorAlongRay(lb,a,px,py,true,start,out bc);
double len = Dist(fp.X,fp.Y,bp.X,bp.Y);
if (len < bestlen) {
bestang = a;
bestlen = len;
bestfc = Between(fc,start,0.5);
bestbc = Between(bc,start,0.5);
bestfpx = fp;
bestbpx = bp;
double flen = Dist(px,py,fp.X,fp.Y);
bestratio = flen/len;
}
}
alow = bestang - Math.PI/3/tries;
ahigh = bestang + Math.PI/3/tries;
}
//if (Math.Abs(px - bestfpx.X) < 2 && Math.Abs(py - bestfpx.Y) < 2) {
// return start;
//}
//if (Math.Abs(px - bestbpx.X) < 2 && Math.Abs(py - bestbpx.Y) < 2) {
// return start;
//}
if (bestfc == start && bestbc == start) {
return start;
}
if (bestratio > 0.5) {
return Between(start,bestbc,(bestratio-0.5)*2);
} else {
return Between(bestfc,start,bestratio*2);
}
}
static Color Between(Color a, Color b, double ratio)
{
int nr = (int)Math.Round((1-ratio)*a.R + ratio*b.R,0);
int ng = (int)Math.Round((1-ratio)*a.G + ratio*b.G,0);
int nb = (int)Math.Round((1-ratio)*a.B + ratio*b.B,0);
int na = (int)Math.Round((1-ratio)*a.A + ratio*b.A,0);
return Color.FromArgb(na,nr,ng,nb);
}
//http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
public static double Sqrt(double z)
{
if (z == 0) { return 0; }
DoubleLongUnion u;
u.tmp = 0;
u.f = z;
//Console.WriteLine("a "+Convert.ToString(u.tmp,2));
u.tmp -= 1L << 52; /* Subtract 2^m. */
///Console.WriteLine("b "+Convert.ToString(u.tmp,2));
u.tmp >>= 1; /* Divide by 2. */
//Console.WriteLine("c "+Convert.ToString(u.tmp,2));
u.tmp += 1L << 61; /* Add ((b + 1) / 2) * 2^m. */
//Console.WriteLine("d "+Convert.ToString(u.tmp,2));
return u.f;
}
[StructLayout(LayoutKind.Explicit)]
private struct DoubleLongUnion
{
[FieldOffset(0)]
public double f;
[FieldOffset(0)]
public long tmp;
}
//public static float Sqrt(float z)
//{
// if (z == 0) return 0;
// FloatIntUnion u;
// u.tmp = 0;
// u.f = z;
// //Console.WriteLine("a "+Convert.ToString(u.tmp,2));
// u.tmp -= 1 << 23; /* Subtract 2^m. */
// //Console.WriteLine("b "+Convert.ToString(u.tmp,2));
// u.tmp >>= 1; /* Divide by 2. */
// //Console.WriteLine("c "+Convert.ToString(u.tmp,2));
// u.tmp += 1 << 29; /* Add ((b + 1) / 2) * 2^m. */
// //Console.WriteLine("d "+Convert.ToString(u.tmp,2));
// return u.f;
//}
//[StructLayout(LayoutKind.Explicit)]
//private struct FloatIntUnion
//{
// [FieldOffset(0)]
// public float f;
// [FieldOffset(0)]
// public int tmp;
//}
static double Dist(int x1,int y1,int x2,int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
//return Math.Sqrt(dx*dx + dy*dy);
return Sqrt((double)dx*dx + (double)dy*dy);
}
static Point FindColorAlongRay(LockBitmap lb, double a, int px, int py, bool back, Color start, out Color c)
{
double r=1;
c = start;
bool done = false;
double cosa = Math.Cos(a) * (back ? -1 : 1);
double sina = Math.Sin(a) * (back ? -1 : 1);
int maxx = lb.Width -1;
int maxy = lb.Height -1;
while(true) {
int fx = (int)(cosa * r) + px;
int fy = (int)(sina * r) + py;
if (fx < 0 || fy < 0 || fx > maxx || fy > maxy) {
done = true;
}
if (!done) {
Color f = lb.GetPixel(fx,fy);
if (f != start) {
c = f;
done = true;
}
}
if (done) {
return new Point(
fx < 0 ? 0 : fx > maxx ? maxx : fx
,fy < 0 ? 0 : fy > maxy ? maxy : fy
);
}
r+=1;
}
}
//static Color SmoothPixel(LockBitmap lb,int px, int py)
//{
// Color cc = lb.GetPixel(px,py);
// Color gt = cc, lt = cc;
// int rad = 1;
// int d = 0, count = 0;
// int x = px, y = py;
// int ht = lb.Height;
// int wt = lb.Width;
// double ccmag = ColorMag(cc);
// double gtmag = ccmag, ltmag = ccmag;
// while(gt == cc && lt == cc)
// {
// while (count < rad)
// {
// switch(d)
// {
// case 0: y--; break;
// case 1: x--; break;
// case 2: y++; break;
// case 3: x++; break;
// }
// if (x > 0 && x < wt && y > 0 && y < ht)
// {
// Color c = lb.GetPixel(x,y);
// double cmag = ColorMag(c);
// if (cmag < ccmag && (ccmag - cmag) > ltmag) {
// lt = c;
// ltmag = ColorMag(lt);
// }
// if (cmag > ccmag && (cmag - ccmag) < gtmag) {
// gt = c;
// gtmag = ColorMag(gt);
// }
// }
// }
// count = 0;
// d++;
// if (d > 3) {
// rad++;
// d = 0;
// }
// if (rad > Math.Max(ht,wt)) {
// break;
// }
// }
//}
//static double ColorMag(Color c)
//{
// return Math.Sqrt(c.R*c.R + c.G*c.G + c.B*c.B);
//}
}
}