public unsafe void GetIndexedDataLinearTest() { Bitmap Bitmap = new Bitmap(3, 3, PixelFormat.Format8bppIndexed); Bitmap.LockBitsUnlock(PixelFormat.Format8bppIndexed, (BitmapData) => { byte *Data = (byte *)BitmapData.Scan0.ToPointer(); for (int n = 0; n < 9; n++) Data[n] = (byte)n; }); Assert.AreEqual( "000102030405060708", Bitmap.GetIndexedDataLinear().ToHexString() ); }
/// <summary> /// /// </summary> /// <param name="Bitmap"></param> /// <param name="AlphaThresold"></param> /// <returns></returns> static public bool[,] GetMask(Bitmap Bitmap, byte AlphaThresold = 1) { var Width = Bitmap.Width; var Height = Bitmap.Height; var Mask = new bool[Bitmap.Width, Bitmap.Height]; Bitmap.LockBitsUnlock(System.Drawing.Imaging.PixelFormat.Format32bppArgb, (BitmapData) => { for (int y = 0; y < Height; y++) { var Ptr = ((byte *)BitmapData.Scan0.ToPointer()) + BitmapData.Stride * y; Ptr += 3; for (int x = 0; x < Width; x++) { Mask[x, y] = (*Ptr >= AlphaThresold); Ptr += 4; //Console.Write(Mask[x, y] ? 1 : 0); } //Console.WriteLine(""); } }); return Mask; }
public Bitmap ToBitmap() { var Bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); Bitmap.LockBitsUnlock(PixelFormat.Format32bppArgb, (BitmapData) => { int Count = Width * Height; var Output = (OutputPixel*)BitmapData.Scan0; PixelFormatDecoder.Decode( GuPixelFormat, Address, Output, Width, Height, IgnoreAlpha: true ); for (int n = 0; n < Count; n++) { var Color = Output[n]; Output[n].R = Color.B; Output[n].G = Color.G; Output[n].B = Color.R; Output[n].A = 0xFF; } }); return Bitmap; }
public static unsafe void TransferChannelsDataLinear(Rectangle Rectangle, Bitmap Bitmap, byte[] NewData, Direction Direction, params BitmapChannel[] Channels) { int WidthHeight = Bitmap.Width * Bitmap.Height; var FullRectangle = Bitmap.GetFullRectangle(); if (!FullRectangle.Contains(Rectangle.Location)) throw(new InvalidProgramException()); if (!FullRectangle.Contains(Rectangle.Location + Rectangle.Size - new Size(1, 1))) throw (new InvalidProgramException()); int NumberOfChannels = 1; foreach (var Channel in Channels) { if (Channel != BitmapChannel.Indexed) { NumberOfChannels = 4; break; } } Bitmap.LockBitsUnlock((NumberOfChannels == 1) ? PixelFormat.Format8bppIndexed : PixelFormat.Format32bppArgb, (BitmapData) => { byte* BitmapDataScan0 = (byte*)BitmapData.Scan0.ToPointer(); int Width = Bitmap.Width; int Height = Bitmap.Height; int Stride = BitmapData.Stride; if (NumberOfChannels == 1) { Stride = Width; } int RectangleTop = Rectangle.Top; int RectangleBottom = Rectangle.Bottom; int RectangleLeft = Rectangle.Left; int RectangleRight = Rectangle.Right; fixed (byte* OutputPtrStart = &NewData[0]) { byte* OutputPtr = OutputPtrStart; foreach (var Channel in Channels) { for (int y = RectangleTop; y < RectangleBottom; y++) { byte* InputPtr = BitmapDataScan0 + Stride * y; if (NumberOfChannels != 1) { InputPtr = InputPtr + (int)Channel; } InputPtr += NumberOfChannels * RectangleLeft; for (int x = RectangleLeft; x < RectangleRight; x++) { if (Direction == Direction.FromBitmapToData) { *OutputPtr = *InputPtr; } else { *InputPtr = * OutputPtr; } OutputPtr++; InputPtr += NumberOfChannels; } } } } }); }
public static unsafe void TransferChannelsDataInterleaved(Rectangle Rectangle, Bitmap Bitmap, byte* NewDataPtr, Direction Direction, params BitmapChannel[] Channels) { int NumberOfChannels = 1; foreach (var Channel in Channels) { if (Channel != BitmapChannel.Indexed) { NumberOfChannels = 4; break; } } Bitmap.LockBitsUnlock(Rectangle, (NumberOfChannels == 1) ? PixelFormat.Format8bppIndexed : PixelFormat.Format32bppArgb, (BitmapData) => { for (int y = 0; y < BitmapData.Height; y++) { byte* BitmapPtr = ((byte*)BitmapData.Scan0.ToPointer()) + BitmapData.Stride * y; byte* DataPtr = NewDataPtr + (NumberOfChannels * BitmapData.Width) * y; int z = 0; for (int x = 0; x < BitmapData.Width; x++) { for (int c = 0; c < NumberOfChannels; c++) { byte* DataPtrPtr = &DataPtr[z + c]; byte* BitmapPtrPtr = &BitmapPtr[z + (int)Channels[c]]; if (Direction == BitmapUtils.Direction.FromBitmapToData) { *DataPtrPtr = *BitmapPtrPtr; } else { *BitmapPtrPtr = * DataPtrPtr; } } z += NumberOfChannels; } } }); }
/// <summary> /// /// </summary> /// <param name="DistanceMap"></param> /// <returns></returns> static public Bitmap BitmapFromDistanceMap(DistanceEntry[,] DistanceMap) { var Bitmap = new Bitmap(DistanceMap.GetLength(0), DistanceMap.GetLength(1)); var Width = Bitmap.Width; var Height = Bitmap.Height; Bitmap.LockBitsUnlock(System.Drawing.Imaging.PixelFormat.Format32bppArgb, (BitmapData) => { for (int y = 0; y < Height; y++) { var Ptr = ((byte *)BitmapData.Scan0.ToPointer()) + BitmapData.Stride * y; for (int x = 0; x < Width; x++) { byte Distance = (byte)MathUtils.FastClamp((int)(DistanceMap[x, y].Distance * 4), 0, 255); *Ptr++ = Distance; *Ptr++ = Distance; *Ptr++ = Distance; *Ptr++ = 0xFF; } } }); return Bitmap; }
public Texture Load(string FileName) { var Bitmap = new Bitmap(Image.FromFile(FileName)); Bitmap.LockBitsUnlock(System.Drawing.Imaging.PixelFormat.Format32bppArgb, (BitmapData) => { this.SetData((OutputPixel *)BitmapData.Scan0, BitmapData.Width, BitmapData.Height); }); return this; }