static Image ReadBitmapData(Stream input, VbpFileHeader fileHeader, VbpInformationHeader infoHeader) { input.Position = fileHeader.OffBits; int count = (infoHeader.Width * infoHeader.Height) * 2; byte[] buffer = new byte[count]; input.Read(buffer, 0, count); Image bitmap = new Image(infoHeader.Width, infoHeader.Height); int shift = GetShift(infoHeader.ColorMask.R); int num3 = GetShift(infoHeader.ColorMask.G); int num4 = GetShift(infoHeader.ColorMask.B); int num5 = 0; for (int i = 0; i < infoHeader.Height; i++) { for (int j = 0; j < infoHeader.Width; j++) { ushort num8 = BitConverter.ToUInt16(buffer, num5++ * 2); ushort red = (ushort)_colorTable5Bit[(num8 & infoHeader.ColorMask.R) >> shift]; ushort green = (ushort)_colorTable5Bit[(num8 & infoHeader.ColorMask.G) >> num3]; ushort blue = (ushort)_colorTable5Bit[(num8 & infoHeader.ColorMask.B) >> num4]; Pixel color = new Pixel((byte)red, (byte)green, (byte)blue, 255); bitmap.SetPixel((uint)j, (uint)i, color); } } return bitmap; }
static Image ReadBitmapData(Stream input, VbpFileHeader fileHeader, VbpInformationHeader infoHeader) { input.Position = fileHeader.OffBits; int count = (infoHeader.Width * infoHeader.Height) * 2; byte[] buffer = new byte[count]; input.Read(buffer, 0, count); Image bitmap = new Image(infoHeader.Width, infoHeader.Height); int shift = GetShift(infoHeader.ColorMask.R); int num3 = GetShift(infoHeader.ColorMask.G); int num4 = GetShift(infoHeader.ColorMask.B); int num5 = 0; for (int i = 0; i < infoHeader.Height; i++) { for (int j = 0; j < infoHeader.Width; j++) { ushort num8 = BitConverter.ToUInt16(buffer, num5++ *2); ushort red = (ushort)_colorTable5Bit[(num8 & infoHeader.ColorMask.R) >> shift]; ushort green = (ushort)_colorTable5Bit[(num8 & infoHeader.ColorMask.G) >> num3]; ushort blue = (ushort)_colorTable5Bit[(num8 & infoHeader.ColorMask.B) >> num4]; Pixel color = new Pixel((byte)red, (byte)green, (byte)blue, 255); bitmap.SetPixel((uint)j, (uint)i, color); } } return(bitmap); }
static VbpInformationHeader ReadInfoHeader(Stream input) { input.Position = 14L; byte[] buffer = new byte[40]; input.Read(buffer, 0, 40); VbpInformationHeader header = new VbpInformationHeader(); header.Size = BitConverter.ToUInt32(buffer, 0); header.Width = BitConverter.ToInt32(buffer, 4); header.Height = BitConverter.ToInt32(buffer, 8); header.Planes = BitConverter.ToUInt16(buffer, 12); header.BitCount = BitConverter.ToUInt16(buffer, 14); uint num = BitConverter.ToUInt32(buffer, 0x10); header.Compression = (IH_Compression)num; header.SizeImage = BitConverter.ToUInt32(buffer, 20); header.XPelsPerMeter = BitConverter.ToInt32(buffer, 0x18); header.YPelsPerMeter = BitConverter.ToInt32(buffer, 0x1c); header.ClrUsed = BitConverter.ToUInt32(buffer, 0x20); header.ClrImportant = BitConverter.ToUInt32(buffer, 0x24); if (header.Compression != IH_Compression.BI_BITFIELDS) { throw new Exception("Wrong Filetype!"); } input.Position = 0x36L; buffer = new byte[0x10]; input.Read(buffer, 0, 0x10); header.ColorMask.R = BitConverter.ToUInt32(buffer, 0); header.ColorMask.G = BitConverter.ToUInt32(buffer, 4); header.ColorMask.B = BitConverter.ToUInt32(buffer, 8); header.ColorMask.Mask = BitConverter.ToUInt32(buffer, 12); return(header); }
public static Image ReadBitmap(Stream input) { Image i = new Image(160, 120); VbpFileHeader fileHeader = ReadFileHeader(input); VbpInformationHeader infoHeader = ReadInfoHeader(input); if (infoHeader.Compression == IH_Compression.BI_BITFIELDS) { i = ReadBitmapData(input, fileHeader, infoHeader); } return(i); }
static VbpInformationHeader ReadInfoHeader(Stream input) { input.Position = 14L; byte[] buffer = new byte[40]; input.Read(buffer, 0, 40); VbpInformationHeader header = new VbpInformationHeader { Size = BitConverter.ToUInt32(buffer, 0), Width = BitConverter.ToInt32(buffer, 4), Height = BitConverter.ToInt32(buffer, 8), Planes = BitConverter.ToUInt16(buffer, 12), BitCount = BitConverter.ToUInt16(buffer, 14) }; uint num = BitConverter.ToUInt32(buffer, 0x10); header.Compression = (IH_Compression)num; header.SizeImage = BitConverter.ToUInt32(buffer, 20); header.XPelsPerMeter = BitConverter.ToInt32(buffer, 0x18); header.YPelsPerMeter = BitConverter.ToInt32(buffer, 0x1c); header.ClrUsed = BitConverter.ToUInt32(buffer, 0x20); header.ClrImportant = BitConverter.ToUInt32(buffer, 0x24); if (header.Compression != IH_Compression.BI_BITFIELDS) { throw new Exception("Wrong Filetype!"); } input.Position = 0x36L; buffer = new byte[0x10]; input.Read(buffer, 0, 0x10); header.ColorMask.R = BitConverter.ToUInt32(buffer, 0); header.ColorMask.G = BitConverter.ToUInt32(buffer, 4); header.ColorMask.B = BitConverter.ToUInt32(buffer, 8); header.ColorMask.Mask = BitConverter.ToUInt32(buffer, 12); return header; }