- wersja 0.4.0
[FaRetSys.git] / IPlugin.cs
blob85bf16eff02631da450e00070e5504bf811452cf
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);
145 public abstract class IInPlugin : IPlugin
147 public override int NumIn { get { return 0; } }
148 public override string DescIn(int n) { return null; }
151 public abstract class IOutPlugin : IPlugin
153 public override void Setup()
156 public override bool HasSetup
158 get { return false; }
161 public abstract void DisplayResults();
163 public override int NumOut { get { return 0; } }
164 public override string DescOut(int n) { return null; }
167 public abstract class IImgProcPlugin : IPlugin
170 public abstract class IResProcPlugin : IPlugin
173 public abstract class IComparatorPlugin : IPlugin
176 public abstract class IOtherPlugin : IPlugin
179 public class CommSocket
181 private ICommObject[] obj;
183 public CommSocket(int n)
185 obj = new ICommObject[n];
188 public ICommObject this [int n]
190 get { return obj[n]; }
191 set { obj[n] = value; }
194 public int Length
196 get { return obj.Length; }
200 public interface ICommObject
203 public class IImage
205 private int w, h;
206 private byte[] data;
207 private int bpp;
209 public IImage(int bpp, int w, int h, byte[] data)
211 RecreateImage(bpp, w, h, data, false);
214 public IImage(int bpp, int w, int h, byte[] data, bool copy)
216 RecreateImage(bpp, w, h, data, copy);
219 private void RecreateImage(int bpp, int w, int h, byte[] data, bool copy)
221 if(bpp != 1 && bpp != 3 && bpp != 4)
222 throw new Exception(Catalog.GetString("BPP must be 1, 3 or 4"));
224 this.bpp = bpp;
225 this.w = w;
226 this.h = h;
228 if(copy)
230 this.data = new byte[w * h * bpp];
231 for(int i=0; i<w*h*bpp; i++)
232 this.data[i] = data[i];
234 else
235 this.data = data;
238 public IImage(int bpp, int w, int h)
240 if(bpp != 1 && bpp != 3 && bpp != 4)
241 throw new Exception(Catalog.GetString("BPP must be 1, 3 or 4"));
243 this.bpp = bpp;
244 this.w = w;
245 this.h = h;
247 data = new byte[w * h * bpp];
250 public void Invert()
252 if(bpp == 1)
253 for(int x=0; x<w; x++)
254 for(int y=0; y<h; y++)
255 PutPixel(x, y, (byte)(255 - (byte)GetPixel(x, y)));
256 else if(bpp == 3)
257 for(int x=0; x<w; x++)
258 for(int y=0; y<h; y++)
260 int c = (int)GetPixel(x, y);
262 byte r = (byte)((c & 0xFF0000) >> 16);
263 byte g = (byte)((c & 0x00FF00) >> 8);
264 byte b = (byte)(c & 0x0000FF);
266 r = (byte)(255 - r);
267 g = (byte)(255 - g);
268 b = (byte)(255 - b);
270 PutPixel(x, y, (r << 16) + (g << 8) + b);
272 else
273 throw new Exception(Catalog.GetString("Image inversion not supported for floating point data"));
276 // zwracamy object, bo nie wiadomo czy będzie bajt, czy int, czy float, ale wewnątrz wszystko
277 // jest jako int traktowane
278 public object this [int x, int y]
280 get { return GetPixel(x, y); }
281 set { PutPixel(x, y, value); }
284 public int W
286 get { return w; }
289 public int H
291 get { return h; }
294 public int BPP
296 get { return bpp; }
299 public byte[] Data
301 get { return data; }
304 private unsafe object GetPixel(int x, int y)
306 if(bpp == 1)
307 return data[x + w*y];
308 else if(bpp == 3)
309 return (data[(x + w*y)*3] << 16) + (data[(x + w*y)*3 + 1] << 8) + data[(x + w*y)*3 + 2];
310 else
311 fixed(byte *ptr = data)
313 return *(((float*)ptr) + x + w*y);
317 private unsafe void PutPixel(int x, int y, object val)
319 if(bpp == 1)
320 data[x + w*y] = (byte)val;
321 else if (bpp == 3)
323 data[(x + w*y)*3] = (byte)(((int)val & 0xFF0000) >> 16);
324 data[(x + w*y)*3 + 1] = (byte)(((int)val & 0x00FF00) >> 8);
325 data[(x + w*y)*3 + 2] = (byte)((int)val & 0x0000FF);
327 else
328 fixed(byte *ptr = data)
330 *(((float*)ptr) + x + w*y) = (float)val;
335 public class ICommImage : ICommObject
337 private readonly IImage[] images;
338 private readonly IImage[] orig;
339 private readonly int[] categories;
341 public ICommImage(IImage[] images, IImage[] orig, int[] categories)
343 this.images = images;
344 this.orig = orig;
345 this.categories = categories;
348 public IImage this [int n]
350 get { return images[n]; }
351 set { images[n] = value; }
354 public int Length
356 get { return images.Length; }
359 public IImage[] Images
361 get { return images; }
364 public IImage[] OriginalImages
366 get { return orig; }
369 public IImage OriginalImage(int n)
371 return orig[n];
374 public int[] Categories
376 get { return categories; }
379 public int Category(int n)
381 return categories[n];
385 public class IResult
387 private readonly double[] data;
389 public IResult(double[] data)
391 this.data = data;
394 public double this [int i]
396 get { return data[i]; }
399 public int Length
401 get { return data.Length; }
404 public double[] Data
406 get { return data; }
410 public class ICommResult : ICommObject
412 private readonly double identity;
413 private readonly IImage[] origbase;
414 private readonly IImage[] origtest;
415 private readonly IResult[] res;
416 private readonly int[] catbase;
417 private readonly int[] cattest;
418 private readonly bool[] match;
420 public ICommResult(IResult[] res, double identity, IImage[] origbase, IImage[] origtest, int[] catbase, int[] cattest)
422 this.identity = identity;
423 this.res = res;
424 this.origbase = origbase;
425 this.origtest = origtest;
426 this.catbase = catbase;
427 this.cattest = cattest;
429 match = new bool[res.Length];
431 for(int i=0; i<res.Length; i++)
432 match[i] = true;
435 public ICommResult(IResult[] res, double identity, IImage[] origbase, IImage[] origtest, int[] catbase, int[] cattest,
436 bool[] match)
438 this.identity = identity;
439 this.res = res;
440 this.origbase = origbase;
441 this.origtest = origtest;
442 this.catbase = catbase;
443 this.cattest = cattest;
444 this.match = match;
447 public double this [int itest, int ibase]
449 get { return res[itest][ibase]; }
452 public IResult this [int n]
454 get { return res[n]; }
457 public int Length
459 get { return res.Length; }
462 public double Identity
464 get { return identity; }
467 public IImage[] OriginalBaseImages
469 get { return origbase; }
472 public IImage[] OriginalTestImages
474 get { return origtest; }
477 public int[] TestCategories
479 get { return cattest; }
482 public int[] BaseCategories
484 get { return catbase; }
487 public bool[] Match
489 get { return match; }
492 public int TestCategory(int n)
494 return cattest[n];
497 public int BaseCategory(int n)
499 return catbase[n];
502 public double Difference(int itest, int ibase)
504 return Math.Abs(identity - this[itest, ibase]);