//constructors; public Filler(DMSImage Source) : base(new Size(Source.Width, Source.Height), Source, Color.Gray) { m_pixelindex = new Dictionary<int, int>(); m_pixels = new List<PixelInfo>(); m_resolution = START_RESOLUTION; m_rand = new Random(); double delta = 0; int iter = 0; while (m_pixels.Count < MIN_PIXELS || delta > DELTA_THRESHOLD && iter < MAX_ITERS) { if (iter == MAX_ITERS || delta < DELTA_THRESHOLD) { iter = 0; m_resolution >>= 1; if (m_resolution == 0) break; Console.Write("\n" + m_resolution); //bring on new pixels if necessary for (int u = 0; u < Source.Width; u += m_resolution) { for (int v = 0; v < Source.Height; v += m_resolution) { if (GetPixelHelper(u, v) == null) { PixelInfo newpixel = new PixelInfo(u, v, new Point3D()); newpixel.SetNeighbors(m_Source, m_resolution); SetAvgColour(newpixel); m_pixels.Add(newpixel); m_pixelindex.Add(v * Source.Width + u, m_pixels.Count() - 1); } } } } else { iter++; } Console.Write('.'); //do the averaging! delta = 0.0; if (m_pixels.Count() == 0) continue; for (int i = 0; i < 10000; i++) { int idx = m_rand.Next(m_pixelindex.Count()); idx = m_pixelindex.Keys.ToArray()[idx]; int u = idx % Source.Width; int v = idx / Source.Width; int pixelidx = m_pixelindex[idx]; delta += SetAvgColour(m_pixels[pixelidx]); } } Console.WriteLine(); }
private double SetAvgColour(PixelInfo pixel) { Point3D NewColor = new Point3D(); Point3D tmp; int count = 0; pixel.SetNeighbors(m_Source, m_resolution); if (pixel.Up != 0) { tmp = GetPixelHelper(pixel.U, pixel.V - pixel.Up); if (tmp != null) { NewColor += tmp; count++; } } if (pixel.Down != 0) { tmp = GetPixelHelper(pixel.U, pixel.V + pixel.Down); if (tmp != null) { NewColor += tmp; count++; } } if (pixel.Left != 0) { tmp = GetPixelHelper(pixel.U - pixel.Left, pixel.V); if (tmp != null) { NewColor += tmp; count++; } } if (pixel.Up != 0) { tmp = GetPixelHelper(pixel.U + pixel.Right, pixel.V); if (tmp != null) { NewColor += tmp; count++; } } if (count == 0) NewColor = new Point3D(128, 128, 128); else NewColor /= (double)count; double Result = (pixel.color - NewColor).R; pixel.color = NewColor; return Result; }