public static void SetPixelDataArgb(this PixelData pixelData, int[] data, int width = -1, int height = -1) { if (width < 0) { width = pixelData.Width; } if (height < 0) { height = pixelData.Height; } if (data.Length != width * height) { throw new ArgumentException("Data length doesn't match width * height", "pixelData"); } ColorRgba[] tempData = new ColorRgba[width * height]; Parallel.ForEach(Partitioner.Create(0, tempData.Length), range => { for (int i = range.Item1; i < range.Item2; i++) { tempData[i].A = (byte)((data[i] & 0xFF000000) >> 24); tempData[i].R = (byte)((data[i] & 0x00FF0000) >> 16); tempData[i].G = (byte)((data[i] & 0x0000FF00) >> 8); tempData[i].B = (byte)((data[i] & 0x000000FF) >> 0); } }); pixelData.SetData(tempData, width, height); }
/// <summary> /// Retrieves the pixel data that is currently stored in video memory. /// </summary> /// <param name="target">The target image to store the retrieved pixel data in.</param> /// <param name="targetIndex">The <see cref="Targets"/> index to read from.</param> /// <param name="x">The x position of the rectangular area to read.</param> /// <param name="y">The y position of the rectangular area to read.</param> /// <param name="width">The width of the rectangular area to read. Defaults to the <see cref="RenderTarget"/> <see cref="Width"/>.</param> /// <param name="height">The height of the rectangular area to read. Defaults to the <see cref="RenderTarget"/> <see cref="Height"/>.</param> public void GetPixelData(PixelData target, int targetIndex = 0, int x = 0, int y = 0, int width = -1, int height = -1) { NormalizeReadRect(ref x, ref y, ref width, ref height, this.Width, this.Height); ColorRgba[] data = new ColorRgba[width * height]; this.GetPixelDataInternal(data, targetIndex, x, y, width, height); target.SetData(data, width, height); }
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 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); }
/// <summary> /// Retrieves the pixel data that is currently stored in video memory. /// </summary> /// <param name="target">The target image to store the retrieved pixel data in.</param> public void GetPixelData(PixelData target) { ColorRgba[] data = new ColorRgba[this.texWidth * this.texHeight]; this.GetPixelDataInternal(data); target.SetData(data, this.texWidth, this.texHeight); }