- cosmetics
[FaRetSys.git] / Plugins / Random / Random.cs
blob0dba344644ab98e128831cd4ad507af88d30c39f
1 using System;
2 using System.Xml;
3 using Mono.Unix;
5 namespace Eithne
7 public class RandomInfo : IInfo
9 public override string Name
11 get { return Catalog.GetString("Random pixels"); }
14 public override string ShortName
16 get { return "Random"; }
19 public override string Author
21 get { return "Bartosz Taudul"; }
24 public override string Description
26 get { return Catalog.GetString("This plugin gets random pixels from image and forms a new one."); }
30 public class RandomFactory : IFactory
32 IInfo _info = new RandomInfo();
33 public IInfo Info
35 get { return _info; }
38 public IType Type
40 get { return IType.ImgProc; }
43 public void Initialize()
47 public IPlugin Create()
49 return new RandomPlugin();
53 public class RandomPlugin : IImgProcPlugin
55 private int x = 10, y = 10, seed;
57 public RandomPlugin()
59 Random r = new Random();
60 seed = r.Next();
61 _info = new RandomInfo();
64 public override XmlNode Config
66 get { return GetConfig(); }
67 set { LoadConfig(value); }
70 private void UpdateValue(int x, int y, int seed)
72 this.x = x;
73 this.y = y;
74 this.seed = seed;
76 _block.Invalidate();
79 public override void Setup()
81 new RandomSetup(x, y, seed, UpdateValue);
84 public override void Work()
86 ICommImage socket = _in[0] as ICommImage;
87 IImage[] img = socket.Images;
88 IImage[] ret = new IImage[img.Length];
90 for(int i=0; i<img.Length; i++)
91 ret[i] = RandomPixels(img[i]);
93 _out = new CommSocket(1);
94 _out[0] = new ICommImage(ret, socket.OriginalImages, socket.Categories);
96 _workdone = true;
99 private IImage RandomPixels(IImage img)
101 IImage res = new IImage(img.BPP, x, y);
102 Random rand = new Random(seed);
104 for(int iy=0; iy<y; iy++)
105 for(int ix=0; ix<x; ix++)
107 int rx = rand.Next(img.W);
108 int ry = rand.Next(img.H);
110 res[ix, iy] = img[rx, ry];
113 return res;
116 private XmlNode GetConfig()
118 XmlNode root = _xmldoc.CreateNode(XmlNodeType.Element, "config", "");
120 XmlNode n = _xmldoc.CreateNode(XmlNodeType.Element, "seed", "");
121 n.InnerText = seed.ToString();
122 root.AppendChild(n);
124 n = _xmldoc.CreateNode(XmlNodeType.Element, "x", "");
125 n.InnerText = x.ToString();
126 root.AppendChild(n);
128 n = _xmldoc.CreateNode(XmlNodeType.Element, "y", "");
129 n.InnerText = y.ToString();
130 root.AppendChild(n);
132 return root;
135 private void LoadConfig(XmlNode root)
137 seed = Int32.Parse(root.SelectSingleNode("seed").InnerText);
138 x = Int32.Parse(root.SelectSingleNode("x").InnerText);
139 y = Int32.Parse(root.SelectSingleNode("y").InnerText);
142 public override int NumIn { get { return 1; } }
143 public override int NumOut { get { return 1; } }
145 public override string DescIn(int n)
147 return Catalog.GetString("Input image.");
150 public override string DescOut(int n)
152 return Catalog.GetString("Random pixels.");
155 private static string[] matchin = new string[] { "image" };
156 private static string[] matchout = new string[] { "image/rgb", "image/grayscale", "image/float" };
158 public override string[] MatchIn { get { return matchin; } }
159 public override string[] MatchOut { get { return matchout; } }