public static BitmapHeader ReadHeader(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!BitConverter.IsLittleEndian) { throw new InvalidOperationException("Unsupported system endianness."); } byte[] array = new byte[54]; if (stream.Read(array, 0, array.Length) != array.Length) { throw new InvalidOperationException("Invalid BMP header."); } BitmapHeader result = Utilities.ArrayToStructure <BitmapHeader>(array); if (result.Type1 != 66 || result.Type2 != 77) { throw new InvalidOperationException("Invalid BMP header."); } if (result.Compression != 0) { throw new InvalidOperationException("Unsupported BMP compression."); } return(result); }
private void CreateImageData(string inputfile) { int num = (int)new FileInfo(inputfile).Length; int num2 = (int)Math.Sqrt((double)num) - 1; num *= 3; num += 54; BMPHeader = new BitmapHeader { sig = 19778, size = num, reserved = 0U, offset = 54U }; IMGHeader = new ImgInfoHeader { size = 40, width = num2, height = num2, bitPlanes = 1572865, compression = 0, imageSize = 0, xRes = 0, yRes = 0, nColors = 0, impColors = 0 }; }
public void ReadBMP(string path) { if (File.Exists(path)) { BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open)); HeaderBMP = new BitmapHeader { BfType = reader.ReadUInt16(), BfSize = reader.ReadUInt32(), BfReserved1 = reader.ReadUInt16(), BfReserved2 = reader.ReadUInt16(), BfOffBits = reader.ReadUInt32() }; HeaderInfoBMP = new BitmapHeaderInfo { Size = reader.ReadUInt32(), Width = reader.ReadUInt32(), Height = reader.ReadUInt32(), Planes = reader.ReadUInt16(), BitCount = reader.ReadUInt16(), Compression = reader.ReadUInt32(), SizeImage = reader.ReadUInt32(), XPelsPerMeter = reader.ReadUInt32(), YPelsPerMeter = reader.ReadUInt32(), ColorsUsed = reader.ReadUInt32(), ColorsImportant = reader.ReadUInt32() }; ColorTable = new byte[HeaderBMP.BfOffBits - 54]; for (int i = 0; i < HeaderBMP.BfOffBits - 54; i++) { ColorTable[i] = reader.ReadByte(); } width = HeaderInfoBMP.Width; height = HeaderInfoBMP.Height;; pixels = new byte[height, width, 3]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { pixels[i, j, 0] = reader.ReadByte(); pixels[i, j, 1] = reader.ReadByte(); pixels[i, j, 2] = reader.ReadByte(); } } reader.Close(); } else { throw new Exception("Can't read file."); } }
public static Image Load(Stream stream) { BitmapHeader bitmapHeader = ReadHeader(stream); Image image = new Image(bitmapHeader.Width, MathUtils.Abs(bitmapHeader.Height)); if (bitmapHeader.BitCount == 32) { byte[] array = new byte[4 * image.Width]; for (int i = 0; i < image.Height; i++) { if (stream.Read(array, 0, array.Length) != array.Length) { throw new InvalidOperationException("BMP data truncated."); } int num = (bitmapHeader.Height < 0) ? (image.Width * (image.Height - i - 1)) : (image.Width * i); int j = 0; int num2 = 0; for (; j < image.Width; j++) { byte b = array[num2++]; byte g = array[num2++]; byte r = array[num2++]; byte a = array[num2++]; image.Pixels[num++] = new Color(r, g, b, a); } } } else { if (bitmapHeader.BitCount != 24) { throw new InvalidOperationException("Unsupported BMP pixel format."); } byte[] array2 = new byte[(3 * image.Width + 3) / 4 * 4]; for (int k = 0; k < image.Height; k++) { if (stream.Read(array2, 0, array2.Length) != array2.Length) { throw new InvalidOperationException("BMP data truncated."); } int num3 = (bitmapHeader.Height < 0) ? (image.Width * (image.Height - k - 1)) : (image.Width * k); int l = 0; int num4 = 0; for (; l < image.Width; l++) { byte b2 = array2[num4++]; byte g2 = array2[num4++]; byte r2 = array2[num4++]; image.Pixels[num3++] = new Color(r2, g2, b2); } } } return(image); }
public static BmpInfo GetInfo(Stream stream) { BitmapHeader bitmapHeader = ReadHeader(stream); BmpInfo result = default(BmpInfo); result.Width = bitmapHeader.Width; result.Height = bitmapHeader.Height; if (bitmapHeader.BitCount == 32) { result.Format = Format.RGBA8; } else { if (bitmapHeader.BitCount != 24) { throw new InvalidOperationException("Unsupported BMP pixel format."); } result.Format = Format.RGB8; } return(result); }
private Byte[] CreateHeader() { BinaryReader reader = new BinaryReader(this._rtBitmap); Int32 headerSize = reader.ReadInt32(); Int32 pixelSize = (Int32)this._rtBitmap.Length - headerSize; Int32 fileSize = 14 + headerSize + pixelSize; /* Get the palette size * The Palette size is stored as an int32 at offset 32 * Actually stored as number of colours, so multiply by 4*/ this._rtBitmap.Position = 32; Int32 paletteSize = 4 * reader.ReadInt32(); // Get the palette size from the bbp if none was specified if (paletteSize == 0) { // Get the bits per pixel. The bits per pixel is store as an int16 at offset 14 this._rtBitmap.Position = 14; int bpp = reader.ReadInt16(); // Only set the palette size if the bpp < 16 if (bpp < 16) { paletteSize = 4 * (2 << (bpp - 1)); } } this._rtBitmap.Position = 0; BitmapHeader header = new BitmapHeader() { magic = new Byte[] { (Byte)'B', (Byte)'M' }, fileSize = fileSize, Unk1 = 0, headerSize = 14 + headerSize + paletteSize }; return(PinnedBufferReader.StructureToArray <BitmapHeader>(header)); }
public static void Save(Image image, Stream stream, Format format) { BitmapHeader structure = default(BitmapHeader); structure.Type1 = 66; structure.Type2 = 77; structure.Reserved1 = 0; structure.Reserved2 = 0; structure.OffBits = 54; structure.Size2 = 40; structure.Width = image.Width; structure.Height = -image.Height; structure.Planes = 1; structure.Compression = 0; structure.SizeImage = 0; structure.XPelsPerMeter = 3780; structure.YPelsPerMeter = 3780; structure.ClrUsed = 0; structure.ClrImportant = 0; if (format == Format.RGBA8) { structure.Size = 54 + 4 * image.Width * image.Height; structure.BitCount = 32; } else { structure.Size = 54 + (3 * image.Width + 3) / 4 * 4 * image.Height; structure.BitCount = 24; } byte[] array = Utilities.StructureToArray(structure); stream.Write(array, 0, array.Length); if (format == Format.RGBA8) { byte[] array2 = new byte[4 * image.Width]; for (int i = 0; i < image.Height; i++) { int num = image.Width * i; int j = 0; int num2 = 0; for (; j < image.Width; j++) { Color color = image.Pixels[num++]; array2[num2++] = color.B; array2[num2++] = color.G; array2[num2++] = color.R; array2[num2++] = color.A; } stream.Write(array2, 0, array2.Length); } return; } byte[] array3 = new byte[(3 * image.Width + 3) / 4 * 4]; for (int k = 0; k < image.Height; k++) { int num3 = image.Width * k; int l = 0; int num4 = 0; for (; l < image.Width; l++) { Color color2 = image.Pixels[num3++]; array3[num4++] = color2.B; array3[num4++] = color2.G; array3[num4++] = color2.R; } stream.Write(array3, 0, array3.Length); } }