2 using System
.Collections
;
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
);
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
);
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
);
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;
58 public static Pixbuf
CreatePixbuf(IImage img
)
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
;
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
];
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)
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);
120 for(int j
=1; j
<r
[i
].Length
; j
++)
121 if(r
.Difference(i
, j
) < min
)
123 min
= r
.Difference(i
, j
);
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
++)
145 Array
.Sort(res
[i
], rs
);
152 class ResultSorter
: IComparer
154 private double identity
;
155 private double[] res
;
157 public ResultSorter(double identity
, double[] res
)
159 this.identity
= identity
;
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
]);