- new input/output matching system in IPlugin
[FaRetSys.git] / IPlugin.cs
blobd8b429b9bf1d802add0e494768d3a7fb87476c01
1 using System;
2 using System.Xml;
3 using Mono.Unix;
5 namespace Eithne
7 public class PluginException : Exception
9 public PluginException(string msg) : base(msg)
13 public class Program
15 public static string Version
17 get { return "0.4.0"; }
21 public enum IType
23 In,
24 Out,
25 ImgProc,
26 ResProc,
27 Comparator,
28 Other
31 public interface IBlock
33 void Invalidate();
34 void SlotsChanged();
37 public abstract class IInfo
39 public abstract string Name { get; }
40 public abstract string ShortName { get; }
41 public abstract string Author { get; }
42 public abstract string Description { get; }
44 public virtual string Version
46 get { return Program.Version; }
50 public interface IFactory
52 IInfo Info { get; }
53 IType Type { get; }
55 void Initialize(); // TODO: remove?
56 IPlugin Create();
59 public abstract class IPlugin
61 protected IInfo _info;
62 private object _source;
63 protected XmlDocument _xmldoc;
64 protected bool _workdone = false;
65 protected IBlock _block = null;
66 protected CommSocket _in = null, _out = null;
68 public virtual IInfo Info { get { return _info; } }
69 public XmlDocument XmlDoc { set { _xmldoc = value; } }
71 public object Source
73 get { return _source; }
74 set { _source = value; }
77 public virtual XmlNode Config
79 get { return null; }
80 set {}
83 public virtual bool WorkDone
85 get { return _workdone; }
86 set { _workdone = value; }
89 public IBlock Block
91 set { _block = value; }
94 public abstract void Setup();
95 public abstract void Work();
97 public virtual void Invalidate()
99 ClearInput();
100 ClearOutput();
103 protected void ClearInput()
105 if(_in != null)
107 for(int i=0; i<_in.Length; i++)
108 _in[i] = null;
109 _in = null;
113 protected void ClearOutput()
115 if(_out != null)
117 for(int i=0; i<_out.Length; i++)
118 _out[i] = null;
119 _out = null;
123 public virtual bool HasSetup
125 get { return true; }
128 public abstract int NumIn { get; }
129 public abstract int NumOut { get; }
131 public CommSocket In
133 set { _in = value; }
136 public CommSocket Out
138 get { return _out; }
141 public abstract string DescIn(int n);
142 public abstract string DescOut(int n);
144 public abstract string[] MatchIn { get; }
145 public abstract string[] MatchOut { get; }
148 public abstract class IInPlugin : IPlugin
150 public override int NumIn { get { return 0; } }
151 public override string DescIn(int n) { return null; }
152 public override string[] MatchIn { get {return null; } }
155 public abstract class IOutPlugin : IPlugin
157 public override void Setup()
160 public override bool HasSetup
162 get { return false; }
165 public abstract void DisplayResults();
167 public override int NumOut { get { return 0; } }
168 public override string DescOut(int n) { return null; }
169 public override string[] MatchOut { get { return null; } }
172 public abstract class IImgProcPlugin : IPlugin
175 public abstract class IResProcPlugin : IPlugin
178 public abstract class IComparatorPlugin : IPlugin
181 public abstract class IOtherPlugin : IPlugin
184 public class CommSocket
186 private ICommObject[] obj;
188 public CommSocket(int n)
190 obj = new ICommObject[n];
193 public ICommObject this [int n]
195 get { return obj[n]; }
196 set { obj[n] = value; }
199 public int Length
201 get { return obj.Length; }
205 public interface ICommObject
208 public class IImage
210 private int w, h;
211 private byte[] data;
212 private int bpp;
214 public IImage(int bpp, int w, int h, byte[] data)
216 RecreateImage(bpp, w, h, data, false);
219 public IImage(int bpp, int w, int h, byte[] data, bool copy)
221 RecreateImage(bpp, w, h, data, copy);
224 private void RecreateImage(int bpp, int w, int h, byte[] data, bool copy)
226 if(bpp != 1 && bpp != 3 && bpp != 4)
227 throw new Exception(Catalog.GetString("BPP must be 1, 3 or 4"));
229 this.bpp = bpp;
230 this.w = w;
231 this.h = h;
233 if(copy)
235 this.data = new byte[w * h * bpp];
236 for(int i=0; i<w*h*bpp; i++)
237 this.data[i] = data[i];
239 else
240 this.data = data;
243 public IImage(int bpp, int w, int h)
245 if(bpp != 1 && bpp != 3 && bpp != 4)
246 throw new Exception(Catalog.GetString("BPP must be 1, 3 or 4"));
248 this.bpp = bpp;
249 this.w = w;
250 this.h = h;
252 data = new byte[w * h * bpp];
255 public void Invert()
257 if(bpp == 1)
258 for(int x=0; x<w; x++)
259 for(int y=0; y<h; y++)
260 PutPixel(x, y, (byte)(255 - (byte)GetPixel(x, y)));
261 else if(bpp == 3)
262 for(int x=0; x<w; x++)
263 for(int y=0; y<h; y++)
265 int c = (int)GetPixel(x, y);
267 byte r = (byte)((c & 0xFF0000) >> 16);
268 byte g = (byte)((c & 0x00FF00) >> 8);
269 byte b = (byte)(c & 0x0000FF);
271 r = (byte)(255 - r);
272 g = (byte)(255 - g);
273 b = (byte)(255 - b);
275 PutPixel(x, y, (r << 16) + (g << 8) + b);
277 else
278 throw new Exception(Catalog.GetString("Image inversion not supported for floating point data"));
281 // zwracamy object, bo nie wiadomo czy będzie bajt, czy int, czy float, ale wewnątrz wszystko
282 // jest jako int traktowane
283 public object this [int x, int y]
285 get { return GetPixel(x, y); }
286 set { PutPixel(x, y, value); }
289 public int W
291 get { return w; }
294 public int H
296 get { return h; }
299 public int BPP
301 get { return bpp; }
304 public byte[] Data
306 get { return data; }
309 private unsafe object GetPixel(int x, int y)
311 if(bpp == 1)
312 return data[x + w*y];
313 else if(bpp == 3)
314 return (data[(x + w*y)*3] << 16) + (data[(x + w*y)*3 + 1] << 8) + data[(x + w*y)*3 + 2];
315 else
316 fixed(byte *ptr = data)
318 return *(((float*)ptr) + x + w*y);
322 private unsafe void PutPixel(int x, int y, object val)
324 if(bpp == 1)
325 data[x + w*y] = (byte)val;
326 else if (bpp == 3)
328 data[(x + w*y)*3] = (byte)(((int)val & 0xFF0000) >> 16);
329 data[(x + w*y)*3 + 1] = (byte)(((int)val & 0x00FF00) >> 8);
330 data[(x + w*y)*3 + 2] = (byte)((int)val & 0x0000FF);
332 else
333 fixed(byte *ptr = data)
335 *(((float*)ptr) + x + w*y) = (float)val;
340 public class ICommImage : ICommObject
342 private readonly IImage[] images;
343 private readonly IImage[] orig;
344 private readonly int[] categories;
346 public ICommImage(IImage[] images, IImage[] orig, int[] categories)
348 this.images = images;
349 this.orig = orig;
350 this.categories = categories;
353 public IImage this [int n]
355 get { return images[n]; }
356 set { images[n] = value; }
359 public int Length
361 get { return images.Length; }
364 public IImage[] Images
366 get { return images; }
369 public IImage[] OriginalImages
371 get { return orig; }
374 public IImage OriginalImage(int n)
376 return orig[n];
379 public int[] Categories
381 get { return categories; }
384 public int Category(int n)
386 return categories[n];
390 public class IResult
392 private readonly double[] data;
394 public IResult(double[] data)
396 this.data = data;
399 public double this [int i]
401 get { return data[i]; }
404 public int Length
406 get { return data.Length; }
409 public double[] Data
411 get { return data; }
415 public class ICommResult : ICommObject
417 private readonly double identity;
418 private readonly IImage[] origbase;
419 private readonly IImage[] origtest;
420 private readonly IResult[] res;
421 private readonly int[] catbase;
422 private readonly int[] cattest;
423 private readonly bool[] match;
425 public ICommResult(IResult[] res, double identity, IImage[] origbase, IImage[] origtest, int[] catbase, int[] cattest)
427 this.identity = identity;
428 this.res = res;
429 this.origbase = origbase;
430 this.origtest = origtest;
431 this.catbase = catbase;
432 this.cattest = cattest;
434 match = new bool[res.Length];
436 for(int i=0; i<res.Length; i++)
437 match[i] = true;
440 public ICommResult(IResult[] res, double identity, IImage[] origbase, IImage[] origtest, int[] catbase, int[] cattest,
441 bool[] match)
443 this.identity = identity;
444 this.res = res;
445 this.origbase = origbase;
446 this.origtest = origtest;
447 this.catbase = catbase;
448 this.cattest = cattest;
449 this.match = match;
452 public double this [int itest, int ibase]
454 get { return res[itest][ibase]; }
457 public IResult this [int n]
459 get { return res[n]; }
462 public int Length
464 get { return res.Length; }
467 public double Identity
469 get { return identity; }
472 public IImage[] OriginalBaseImages
474 get { return origbase; }
477 public IImage[] OriginalTestImages
479 get { return origtest; }
482 public int[] TestCategories
484 get { return cattest; }
487 public int[] BaseCategories
489 get { return catbase; }
492 public bool[] Match
494 get { return match; }
497 public int TestCategory(int n)
499 return cattest[n];
502 public int BaseCategory(int n)
504 return catbase[n];
507 public double Difference(int itest, int ibase)
509 return Math.Abs(identity - this[itest, ibase]);