- fixed clean rule
[FaRetSys.git] / Utility.cs
blobd064b4935b25785101c45fc6bf287f219c4d1a70
1 using System;
2 using System.Collections;
3 using Gdk;
5 namespace Eithne
7 public class Utility
9 public static unsafe int GetPixel(Pixbuf buf, int x, int y)
11 byte *ptr = (byte*)buf.Pixels;
13 int r = *(ptr + y * buf.Rowstride + x * buf.NChannels);
14 int g = *(ptr + y * buf.Rowstride + x * buf.NChannels + 1);
15 int b = *(ptr + y * buf.Rowstride + x * buf.NChannels + 2);
17 return (r << 16) + (g << 8) + b;
20 public static IImage CreateImage(Pixbuf buf, int bpp)
22 byte[] data = new byte[buf.Height * buf.Width * bpp];
23 IImage img = new IImage(bpp, buf.Width, buf.Height, data);
25 if(bpp == 1)
27 for(int y=0; y<buf.Height; y++)
28 for(int x=0; x<buf.Width; x++)
29 img[x, y] = (byte)Utility.GetPixel(buf, x, y);
31 else
32 for(int y=0; y<buf.Height; y++)
33 for(int x=0; x<buf.Width; x++)
34 img[x, y] = Utility.GetPixel(buf, x, y);
36 return img;
39 // strasznie durna metoda, ale pixbuf tylko rgb umie przechowywać
40 public static bool IsBW(Pixbuf buf)
42 for(int y=0; y<buf.Height; y++)
43 for(int x=0; x<buf.Width; x++)
45 int color = Utility.GetPixel(buf, x, y);
47 int r = (color & 0xFF0000) >> 16;
48 int g = (color & 0x00FF00) >> 8;
49 int b = color & 0x0000FF;
51 if(r != g || r != b)
52 return false;
55 return true;
58 public static Pixbuf CreatePixbuf(IImage img)
60 byte[] data;
62 if(img.BPP == 1)
64 // konwersja na RGB
65 data = new byte[img.H * img.W * 3];
67 for(int y=0; y<img.H; y++)
68 for(int x=0; x<img.W; x++)
70 byte color = (byte)img[x, y];
72 data[(x + y*img.W)*3] = color;
73 data[(x + y*img.W)*3 + 1] = color;
74 data[(x + y*img.W)*3 + 2] = color;
77 else if(img.BPP == 3)
79 data = img.Data;
81 else
83 data = new byte[img.H * img.W * 3];
85 for(int y=0; y<img.H; y++)
86 for(int x=0; x<img.W; x++)
88 float val = (float)img[x, y];
90 if(val>255f)
91 val = 255f;
93 byte color = (byte)val;
95 data[(x + y*img.W)*3] = color;
96 data[(x + y*img.W)*3 + 1] = color;
97 data[(x + y*img.W)*3 + 2] = color;
102 Pixbuf tmp = new Pixbuf(data, false, 8, img.W, img.H, img.W * 3, null);
104 // wyżej robiony jest wrapper na dane, dane po konwersji są tymczasowe, więc trzeba zrobić kopię
105 if(img.BPP == 1 || img.BPP == 4)
106 tmp = tmp.Copy();
108 return tmp;
111 public static int[] FindResultsSimple(ICommResult r)
113 int[] res = new int[r.Length];
115 for(int i=0; i<r.Length; i++)
117 double min = r.Difference(i, 0);
118 int n = 0;
120 for(int j=1; j<r[i].Length; j++)
121 if(r.Difference(i, j) < min)
123 min = r.Difference(i, j);
124 n = j;
127 res[i] = n;
130 return res;
133 public static int[][] FindResults(ICommResult r)
135 int[][] res = new int[r.Length][];
137 for(int i=0; i<r.Length; i++)
139 ResultSorter rs = new ResultSorter(r.Identity, r[i].Data);
141 res[i] = new int[r[i].Length];
142 for(int j=0; j<r[i].Length; j++)
143 res[i][j] = j;
145 Array.Sort(res[i], rs);
148 return res;
152 class ResultSorter : IComparer
154 private double identity;
155 private double[] res;
157 public ResultSorter(double identity, double[] res)
159 this.identity = identity;
160 this.res = res;
163 public int Compare(object x, object y)
165 double vx = Math.Abs(identity - res[(int)x]);
166 double vy = Math.Abs(identity - res[(int)y]);
168 if(vx < vy)
169 return -1;
170 else if(vx > vy)
171 return 1;
172 else
173 return 0;