private static ArgbColor[] Get32BppArgbPixels(this Bitmap bitmap) { int width; int height; BitmapData bitmapData; ArgbColor[] results; width = bitmap.Width; height = bitmap.Height; results = new ArgbColor[width * height]; bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); unsafe { ArgbColor* pixel; pixel = (ArgbColor*)bitmapData.Scan0; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { results[row * width + col] = *pixel; pixel++; } } } bitmap.UnlockBits(bitmapData); return results; }
private void Load(string fileName) { const int recordLength = 4 * 2; // four values per pixel, two bytes per value int width; int height; ArgbColor[] pixels; using (Stream stream = File.OpenRead(fileName)) { byte[] header; int length; int rowLength; byte[] buffer; header = new byte[8]; stream.Read(header, 0, header.Length); stream.Read(header, 0, header.Length); width = WordHelpers.MakeDWordBigEndian(header[0], header[1], header[2], header[3]); height = WordHelpers.MakeDWordBigEndian(header[4], header[5], header[6], header[7]); length = width * height; pixels = new ArgbColor[length]; rowLength = width * recordLength; buffer = new byte[rowLength]; for (int row = 0; row < height; row++) { stream.Read(buffer, 0, rowLength); for (int col = 0; col < width; col++) { int r; int g; int b; int a; int index; index = col * recordLength; r = WordHelpers.MakeWordBigEndian(buffer[index], buffer[index + 1]) / 256; g = WordHelpers.MakeWordBigEndian(buffer[index + 2], buffer[index + 3]) / 256; b = WordHelpers.MakeWordBigEndian(buffer[index + 4], buffer[index + 5]) / 256; a = WordHelpers.MakeWordBigEndian(buffer[index + 6], buffer[index + 7]) / 256; pixels[row * width + col] = new ArgbColor(a, r, g, b); } } } this.Width = width; this.Height = height; this.PixelData = pixels; }
public Bitmap ToBitmap() { Bitmap bitmap; BitmapData bitmapData; int width; int height; ushort[] data; width = this.Width; height = this.Height; data = this.GetData(); bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); unsafe { ArgbColor* pixelPtr; int index; pixelPtr = (ArgbColor*)bitmapData.Scan0; index = 0; for (int i = 0; i < width * height; i++) { *pixelPtr = new ArgbColor(data[index + 3] / 256, data[index] / 256, data[index + 1] / 256, data[index + 2] / 256); pixelPtr++; index += 4; } } bitmap.UnlockBits(bitmapData); return bitmap; }
public static void Test1_read_a_byte_at_a_time_original() { int count; string sampleFileName; List<ArgbColor[]> results; count = _iterations; sampleFileName = SampleFileName; results = new List<ArgbColor[]>(); for (int i = 0; i < count; i++) { using (Stream stream = File.OpenRead(sampleFileName)) { byte[] header; int width; int height; int length; ArgbColor[] pixels; header = new byte[8]; stream.Read(header, 0, header.Length); width = stream.ReadUInt32BigEndian(); height = stream.ReadUInt32BigEndian(); length = width * height; pixels = new ArgbColor[length]; for (int j = 0; j < length; j++) { int r; int g; int b; int a; r = stream.ReadUInt16BigEndian() / 256; g = stream.ReadUInt16BigEndian() / 256; b = stream.ReadUInt16BigEndian() / 256; a = stream.ReadUInt16BigEndian() / 256; pixels[j] = new ArgbColor(a, r, g, b); } results.Add(pixels); } } _testResults = results; }
public static void Test4_read_pixel_data_by_row() { const int recordLength = 4 * 2; // four values per pixel, two bytes per value int count; string sampleFileName; List<ArgbColor[]> results; count = _iterations; sampleFileName = SampleFileName; results = new List<ArgbColor[]>(); for (int i = 0; i < count; i++) { using (Stream stream = File.OpenRead(sampleFileName)) { byte[] header; int width; int height; int length; int rowLength; ArgbColor[] pixels; byte[] buffer; header = new byte[8]; stream.Read(header, 0, header.Length); stream.Read(header, 0, header.Length); width = WordHelpers.MakeDWordBigEndian(header[0], header[1], header[2], header[3]); height = WordHelpers.MakeDWordBigEndian(header[4], header[5], header[6], header[7]); length = width * height; pixels = new ArgbColor[length]; rowLength = width * recordLength; buffer = new byte[rowLength]; for (int row = 0; row < height; row++) { stream.Read(buffer, 0, rowLength); for (int col = 0; col < width; col++) { int r; int g; int b; int a; int index; index = col * recordLength; r = WordHelpers.MakeWordBigEndian(buffer[index], buffer[index + 1]) / 256; g = WordHelpers.MakeWordBigEndian(buffer[index + 2], buffer[index + 3]) / 256; b = WordHelpers.MakeWordBigEndian(buffer[index + 4], buffer[index + 5]) / 256; a = WordHelpers.MakeWordBigEndian(buffer[index + 6], buffer[index + 7]) / 256; pixels[row * width + col] = new ArgbColor(a, r, g, b); } } results.Add(pixels); } } _testResults = results; }
public static void Test3_read_one_pixel_at_a_time() { const int recordLength = 4 * 2; // four values per pixel, two bytes per value int count; string sampleFileName; List<ArgbColor[]> results; byte[] buffer; count = _iterations; sampleFileName = SampleFileName; results = new List<ArgbColor[]>(); buffer = new byte[recordLength]; for (int i = 0; i < count; i++) { using (Stream stream = File.OpenRead(sampleFileName)) { byte[] header; int width; int height; int length; ArgbColor[] pixels; header = new byte[8]; stream.Read(header, 0, header.Length); stream.Read(header, 0, header.Length); width = WordHelpers.MakeDWordBigEndian(header[0], header[1], header[2], header[3]); height = WordHelpers.MakeDWordBigEndian(header[4], header[5], header[6], header[7]); length = width * height; pixels = new ArgbColor[length]; for (int j = 0; j < length; j++) { int r; int g; int b; int a; stream.Read(buffer, 0, recordLength); r = WordHelpers.MakeWordBigEndian(buffer[0], buffer[1]) / 256; g = WordHelpers.MakeWordBigEndian(buffer[2], buffer[3]) / 256; b = WordHelpers.MakeWordBigEndian(buffer[4], buffer[5]) / 256; a = WordHelpers.MakeWordBigEndian(buffer[6], buffer[7]) / 256; pixels[j] = new ArgbColor(a, r, g, b); } results.Add(pixels); } } _testResults = results; }