// Copy Pixels from the bitmap source into a PixelColor array, i.e., flattens out those pixels into one huge array public static unsafe void CopyPixelsEx(this BitmapSource source, PixelColor[] pixels, int stride, int offset) { fixed (PixelColor* buffer = &pixels[0]) source.CopyPixels( new System.Windows.Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), (IntPtr)(buffer + offset), pixels.Length * sizeof(PixelColor), stride); }
public static void WritePixels(this WriteableBitmap bitmap, PixelColor[,] pixels, int x, int y) { int width = pixels.GetLength(0); int height = pixels.GetLength(1); bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), pixels, width * 4 /* bpp of incoming pixels */, x, y); }
// Given three images, return an image that highlights the differences in common betwen the main image and the first image, // and the main image and a second image. //public static PixelBitmap Difference(PixelBitmap mainImg, PixelBitmap img1, PixelBitmap img2, byte threshold) public static PixelBitmap Difference(BitmapSource mainImgBM, BitmapSource img1BM, BitmapSource img2BM, byte threshold) { PixelBitmap mainImg = new PixelBitmap((BitmapSource)mainImgBM); PixelBitmap img1 = new PixelBitmap((BitmapSource)img1BM); PixelBitmap img2 = new PixelBitmap((BitmapSource)img2BM); // if images are not same size , use the smaller of their dimensions int width = Math.Min(mainImg.Width, img1.Width); width = Math.Min(width, img2.Width); int height = Math.Min(mainImg.Height, img1.Height); height = Math.Min(height, img2.Height); PixelColor[] pixelColor = new PixelColor[width * height]; //CHECK THIS - BOUNDS MAY NOT BE RIGHT for (int i = 0; i < pixelColor.Length; i++) { byte b1 = (byte)Math.Abs(mainImg.Pixels[i].Blue - img1.Pixels[i].Blue); byte b2 = (byte)Math.Abs(mainImg.Pixels[i].Blue - img2.Pixels[i].Blue); byte b = PixelBitmap.TempCalc(threshold, b1, b2); byte g1 = (byte)Math.Abs(mainImg.Pixels[i].Green - img1.Pixels[i].Green); byte g2 = (byte)Math.Abs(mainImg.Pixels[i].Green - img2.Pixels[i].Green); byte g = PixelBitmap.TempCalc(threshold, g1, g2); byte r1 = (byte)Math.Abs(mainImg.Pixels[i].Red - img1.Pixels[i].Red); byte r2 = (byte)Math.Abs(mainImg.Pixels[i].Red - img2.Pixels[i].Red); byte r = PixelBitmap.TempCalc(threshold, r1, r2); byte a = byte.MaxValue; // opaque var diff = (byte)(b / 3 + g / 3 + r / 3); var pixel = new PixelColor() { Alpha = a, Red = diff, Blue = diff, Green = diff }; pixelColor[i] = pixel; } return new PixelBitmap() { Width = width, Height = height, Pixels = pixelColor }; }
// Copy pixels form a source into the PixelColor array unsafe void copyPixels(BitmapSource source, PixelColor[] pixels, int stride, int offset) { // fixed holds the item in memory while I am working on it fixed (PixelColor* buffer = &pixels[0]) { source.CopyPixels( new System.Windows.Int32Rect(0, 0, source.PixelWidth, source.PixelHeight), (IntPtr)(buffer + offset), pixels.Length * sizeof(PixelColor), stride); } }
// Given two images, return an image containing the visual difference between them public static PixelBitmap operator -(PixelBitmap image1, PixelBitmap image2) { // if image1 is not same size as image2, use the smaller of their dimensions int width = Math.Min (image1.Width, image2.Width) ; int height = Math.Min (image1.Height, image2.Height); //PixelColor[] pixelColor = new PixelColor[image1.Pixels.Length]; PixelColor[] pixelColor = new PixelColor[width * height]; for (int i = 0; i < pixelColor.Length; i++) { byte b = (byte) Math.Abs(image1.Pixels[i].Blue - image2.Pixels[i].Blue); byte g = (byte)Math.Abs(image1.Pixels[i].Green - image2.Pixels[i].Green); byte r = (byte)Math.Abs(image1.Pixels[i].Red - image2.Pixels[i].Red); byte a = byte.MaxValue; // opaque byte diff = (byte)(b / 3 + g / 3 + r / 3); // Average the differences // Add that pixel to the image PixelColor pixel = new PixelColor() { Alpha = a, Red = diff, Blue = diff, Green = diff }; pixelColor[i] = pixel; } return new PixelBitmap() { Width = width, Height = height, Pixels = pixelColor }; }