- allow freeing TaskInfos
[FaRetSys.git] / Plugins / Euclid / Euclid.cs
bloba734c7ad669b159d03f0437939f7c7b767c55884
1 using System;
2 using System.Collections;
3 using System.Threading;
4 using System.Xml;
5 using Mono.Unix;
7 namespace Eithne
9 public class EuclidInfo : IInfo
11 public override string Name
13 get { return Catalog.GetString("Euclid metric"); }
16 public override string ShortName
18 get { return "L2"; }
21 public override string Author
23 get { return "Bartosz Taudul"; }
26 public override string Description
28 get { return Catalog.GetString("This plugin calculates Euclid metric between images."); }
32 public class EuclidFactory : IFactory
34 IInfo _info = new EuclidInfo();
35 public IInfo Info
37 get { return _info; }
40 public IType Type
42 get { return IType.Comparator; }
45 public void Initialize()
49 public IPlugin Create()
51 return new EuclidPlugin();
55 public class TaskInfo
57 private IResult[] a_out;
58 private IImage[] a_in1;
59 private IImage[] a_in2;
60 private int start;
61 private int end;
62 private int progress = 0;
64 public TaskInfo(IResult[] a_out, IImage[] a_in1, IImage[] a_in2, int start, int end)
66 this.a_out = a_out;
67 this.a_in1 = a_in1;
68 this.a_in2 = a_in2;
69 this.start = start;
70 this.end = end;
73 public void TaskWork()
75 for(int i=start; i<end; i++)
77 double[] data = new double[a_in1.Length];
79 for(int j=0; j<a_in1.Length; j++)
81 data[j] = Compare(a_in1[j], a_in2[i]);
82 progress++;
85 a_out[i] = new IResult(data);
89 private double Compare(IImage img1, IImage img2)
91 if(img1.BPP != img2.BPP)
92 throw new PluginException(Catalog.GetString("Images BPP do not match."));
93 if(img1.H != img2.H || img1.W != img2.W)
94 throw new PluginException(Catalog.GetString("Images dimensions do not match."));
96 double sum = 0;
98 if(img1.BPP == BPP.Grayscale)
99 for(int i=0; i<img1.Data.Length; i++)
101 int diff = img1.Data[i] - img2.Data[i];
102 sum += diff * diff;
104 else
105 for(int y=0; y<img1.H; y++)
106 for(int x=0; x<img1.W; x++)
108 float diff = (float)img1[x, y] - (float)img2[x, y];
109 sum += diff * diff;
113 return Math.Sqrt(sum);
116 public int Progress
118 get { return progress; }
122 public class EuclidPlugin : IComparatorPlugin
124 private ArrayList tasks = new ArrayList();
125 private int totalImages;
127 public EuclidPlugin()
129 _info = new EuclidInfo();
132 public override void Setup()
136 public override bool HasSetup
138 get { return false; }
141 public override void Work()
143 tasks.Clear();
145 bool MultiThreading = Eithne.Config.Get("engine/blockthreads", false);
147 ICommImage socket1 = _in[0] as ICommImage;
148 ICommImage socket2 = _in[1] as ICommImage;
150 IImage[] img1 = socket1.Images;
151 IImage[] img2 = socket2.Images;
153 _out = new CommSocket(1);
155 IResult[] res = new IResult[img2.Length];
157 totalImages = img1.Length * img2.Length;
159 if(MultiThreading)
161 TaskInfo ti1 = new TaskInfo(res, img1, img2, 0, img2.Length/2);
162 TaskInfo ti2 = new TaskInfo(res, img1, img2, img2.Length/2, img2.Length);
164 tasks.Add(ti1);
165 tasks.Add(ti2);
167 Thread t1 = new Thread(ti1.TaskWork);
168 Thread t2 = new Thread(ti2.TaskWork);
170 t1.Start();
171 t2.Start();
173 t1.Join();
174 t2.Join();
176 else
178 TaskInfo t = new TaskInfo(res, img1, img2, 0, img2.Length);
179 tasks.Add(t);
180 t.TaskWork();
183 _out[0] = new ICommResult(res, 0, socket1.OriginalImages, socket2.OriginalImages, socket1.Categories, socket2.Categories);
185 tasks.Clear();
187 _workdone = true;
190 public override int NumIn { get { return 2; } }
191 public override int NumOut { get { return 1; } }
193 public override string DescIn(int n)
195 if(n == 0)
196 return Catalog.GetString("Base images.");
197 else
198 return Catalog.GetString("Test images.");
201 public override string DescOut(int n)
203 return Catalog.GetString("Calculated Euclid metric.");
206 private static string[] matchin = new string[] { "image/grayscale", "image/float" };
207 private static string[] matchout = new string[] { "result" };
209 public override string[] MatchIn { get { return matchin; } }
210 public override string[] MatchOut { get { return matchout; } }
212 public override float Progress
216 int done = 0;
218 if(tasks.Count == 0)
219 return 1;
221 for(int i=0; i<tasks.Count; i++)
223 done += ((TaskInfo)tasks[i]).Progress;
226 return (float)done/totalImages;