public static SampledImage LoadFromFile(string fileName) { SampledImage sampledImage = new SampledImage(); using (StreamReader reader = new StreamReader(fileName)) { try { sampledImage.Width = Int32.Parse(reader.ReadLine()); sampledImage.Height = Int32.Parse(reader.ReadLine()); string line = reader.ReadLine(); while ((line != null) && (line.Length != 0)) { string[] sampleFields = line.Split(new char[] { ' ' }, 3); int x = Int32.Parse(sampleFields[0]); int y = Int32.Parse(sampleFields[1]); int argb = Int32.Parse(sampleFields[2]); Color color = Color.FromArgb(argb); sampledImage.AddSample(new ImageSample(x, y, color)); line = reader.ReadLine(); } } catch (FormatException ex) { Console.Error.WriteLine(ex.Message); return null; } } return sampledImage; }
protected int GenerateCluster(Bitmap image, int sampleCount, SampledImage sampledImage, int samplePercent, int percentDone, int maxIterations) { for (int i = 0; (sampledImage.Samples.Count < sampleCount) && (i < maxIterations); i++) { int x; int y; if (!Next2DWithinImage(out x, out y)) { continue; } Color color; if (canReadImageDirectly) { // faster, only for BGR or BGRA unsafe { byte* imagePtr = imagePtrBase + (y * imageData.Stride) + x * bands; byte b = imagePtr[0]; byte g = imagePtr[1]; byte r = imagePtr[2]; byte a = 255; if (hasAlpha) { a = imagePtr[3]; } color = Color.FromArgb(a, r, g, b); } } else { // slower, more general color = image.GetPixel(x, y); } sampledImage.AddSample(new ImageSample(x, y, color)); if ((samplePercent > 500) && (sampledImage.Samples.Count % samplePercent == 0)) { percentDone++; Progress.ReportProgress(percentDone); } } return percentDone; }