public PixelData GetPixelData() { ColorRgba[] colors = new ColorRgba[Data.Length / 4]; for (int i = 0, j = 0; i < Data.Length; i += 4, j += 1) { colors[j] = new ColorRgba(Data[i], Data[i + 1], Data[i + 2], Data[i + 3]); } PixelData pixelData = new PixelData(); pixelData.SetData(colors, Width, Height); pixelData.ColorTransparentPixels(); return(pixelData); }
public unsafe PixelData Read(Stream stream) { ColorRgba[] rawColorData; int width, height; using (Bitmap bitmap = BitmapFactory.DecodeStream(stream, null, new BitmapFactory.Options { InPremultiplied = false, InScaled = false })) { Bitmap.Config config = bitmap.GetConfig(); if (config != Bitmap.Config.Argb8888) { throw new NotSupportedException(); } width = bitmap.Width; height = bitmap.Height; IntPtr ptr = bitmap.LockPixels(); int stride = bitmap.RowBytes / sizeof(int); rawColorData = new ColorRgba[width * height]; Parallel.ForEach(Partitioner.Create(0, height), range => { for (int y = range.Item1; y < range.Item2; y++) { for (int x = 0; x < width; x++) { int argbValue = ((int *)ptr)[x + y * stride]; int i = x + y * width; rawColorData[i].A = (byte)((argbValue & 0xFF000000) >> 24); rawColorData[i].B = (byte)((argbValue & 0x00FF0000) >> 16); rawColorData[i].G = (byte)((argbValue & 0x0000FF00) >> 8); rawColorData[i].R = (byte)((argbValue & 0x000000FF) >> 0); } } }); bitmap.UnlockPixels(); } PixelData pixelData = new PixelData(); pixelData.SetData(rawColorData, width, height); pixelData.ColorTransparentPixels(); return(pixelData); }
public static void FromBitmap(this PixelData pixelData, Bitmap bm) { // Retrieve data BitmapData data = bm.LockBits( new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int pixels = data.Width * data.Height; int[] argbValues = new int[pixels]; Marshal.Copy(data.Scan0, argbValues, 0, pixels); bm.UnlockBits(data); pixelData.SetPixelDataArgb(argbValues, bm.Width, bm.Height); pixelData.ColorTransparentPixels(); }
public PixelData Read(Stream stream) { ColorRgba[] rawColorData; int width; int height; PixelData pixelData = new PixelData(); using (Bitmap bitmap = Bitmap.FromStream(stream) as Bitmap) { // Retrieve data BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int pixelCount = bitmapData.Width * bitmapData.Height; int[] argbValues = new int[pixelCount]; Marshal.Copy(bitmapData.Scan0, argbValues, 0, pixelCount); bitmap.UnlockBits(bitmapData); width = bitmapData.Width; height = bitmapData.Height; rawColorData = new ColorRgba[width * height]; Parallel.ForEach(Partitioner.Create(0, rawColorData.Length), range => { for (int i = range.Item1; i < range.Item2; i++) { rawColorData[i].A = (byte)((argbValues[i] & 0xFF000000) >> 24); rawColorData[i].R = (byte)((argbValues[i] & 0x00FF0000) >> 16); rawColorData[i].G = (byte)((argbValues[i] & 0x0000FF00) >> 8); rawColorData[i].B = (byte)((argbValues[i] & 0x000000FF) >> 0); } }); } pixelData.SetData(rawColorData, width, height); pixelData.ColorTransparentPixels(); return(pixelData); }