public static void FromBitmap(out Grid g1, out Grid g2, int width, int height, uint[] buffer) { g1 = new Grid(width, height); g2 = new Grid(width, height); Point inside = Inside; Point outside = Outside; int idx = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { uint color = buffer[idx]; bool set = (color & 0xff) > 0x7f; if (set) { g2.Put(x, y, inside); g1.Put(x, y, outside); } else { g1.Put(x, y, inside); g2.Put(x, y, outside); } idx++; } } }
public static Bitmap ToBitmap(Grid g1, Grid g2, int scalefactor, InterpolationMode interpolation) { uint[] buffer; Bitmap bmp = BitmapHelper.CreateNewManagedBitmap(g1.Width, g1.Height, out buffer); float spread = Math.Min(g1.Width, g1.Height) / (1 << scalefactor); float min = -spread; float max = spread; int width = bmp.Width; int height = bmp.Height; int idx = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float dst = g1.grid[idx].Dist() - g2.grid[idx].Dist(); dst = dst < 0 ? -128 * (dst - min) / min : 128 + 128 * dst / max; uint channel = (uint)Math.Max(0, Math.Min(255, dst)); uint val = (channel << 24) | (channel << 16) | (channel << 8) | channel; buffer[idx] = val; idx++; } } return BitmapHelper.ResizeBitmap(bmp, g1.Width >> scalefactor, g1.Height >> scalefactor, interpolation); }