- moved all plugin-related classes to Eithne.Plugin namespace
[FaRetSys.git] / Plugins / Crop / Crop.cs
blobac89e8f5e18458e0081006055882a78f358fd0f7
1 using System;
2 using System.Xml;
3 using Mono.Unix;
5 namespace Eithne
7 public class CropInfo : IInfo
9 public override string Name
11 get { return Catalog.GetString("Crop"); }
14 public override string ShortName
16 get { return "Crop"; }
19 public override string Author
21 get { return "Bartosz Taudul"; }
24 public override string Description
26 get { return Catalog.GetString("This plugin crops image."); }
30 public class CropFactory : IFactory
32 IInfo _info = new CropInfo();
33 public IInfo Info
35 get { return _info; }
38 public IType Type
40 get { return IType.ImgProc; }
43 public void Initialize()
47 public Plugin.Base Create()
49 return new CropPlugin();
53 public class CropPlugin : Plugin.ImgProc
55 private int x = 10, y = 10;
56 private bool type = true; // T - top-left, F - center
58 public CropPlugin()
60 _info = new CropInfo();
63 public override XmlNode Config
65 get { return GetConfig(); }
66 set { LoadConfig(value); }
69 private void UpdateValue(int x, int y, bool type)
71 this.x = x;
72 this.y = y;
73 this.type = type;
75 _block.Invalidate();
78 public override void Setup()
80 new CropSetup(x, y, type, UpdateValue);
83 public override void Work()
85 ICommImage socket = _in[0] as ICommImage;
86 IImage[] img = socket.Images;
87 IImage[] ret = new IImage[img.Length];
89 for(int i=0; i<img.Length; i++)
90 ret[i] = Crop(img[i]);
92 _out = new CommSocket(1);
93 _out[0] = new ICommImage(ret, socket.OriginalImages, socket.Categories);
95 _workdone = true;
98 private IImage Crop(IImage img)
100 IImage ret;
102 if(type) // topleft
104 int sx = Math.Min(x, img.W);
105 int sy = Math.Min(y, img.H);
107 ret = new IImage(img.BPP, sx, sy);
109 for(int i=0; i<sx; i++)
110 for(int j=0; j<sy; j++)
111 ret[i, j] = img[i, j];
113 else // center
115 int sx = Math.Min(x, img.W);
116 int xoff = (img.W - sx)/2;
118 int sy = Math.Min(y, img.H/2);
119 int yoff = (img.H+1)/2;
121 ret = new IImage(img.BPP, sx, sy);
123 for(int i=0; i<sx; i++)
124 for(int j=0; j<sy; j++)
125 ret[i, j] = img[i+xoff, j+yoff];
128 return ret;
131 private XmlNode GetConfig()
133 XmlNode root = _xmldoc.CreateNode(XmlNodeType.Element, "config", "");
135 XmlNode n = _xmldoc.CreateNode(XmlNodeType.Element, "x", "");
136 n.InnerText = x.ToString();
137 root.AppendChild(n);
139 n = _xmldoc.CreateNode(XmlNodeType.Element, "y", "");
140 n.InnerText = y.ToString();
141 root.AppendChild(n);
143 n = _xmldoc.CreateNode(XmlNodeType.Element, "type", "");
144 if(type)
145 n.InnerText = "topleft";
146 else
147 n.InnerText = "center";
148 root.AppendChild(n);
150 return root;
153 private void LoadConfig(XmlNode root)
155 int x, y;
156 bool type;
158 XmlNode n = root.SelectSingleNode("x");
159 x = Int32.Parse(n.InnerText);
161 n = root.SelectSingleNode("y");
162 y = Int32.Parse(n.InnerText);
164 n = root.SelectSingleNode("type");
165 if(n.InnerText == "topleft")
166 type = true;
167 else
168 type = false;
170 UpdateValue(x, y, type);
173 public override int NumIn { get { return 1; } }
174 public override int NumOut { get { return 1; } }
176 public override string DescIn(int n)
178 return Catalog.GetString("Input image.");
181 public override string DescOut(int n)
183 return Catalog.GetString("Cropped image.");
186 private static string[] matchin = new string[] { "image" };
187 private static string[] matchout = new string[] { "image/rgb", "image/grayscale", "image/float" };
189 public override string[] MatchIn { get { return matchin; } }
190 public override string[] MatchOut { get { return matchout; } }