private static void WriteBitmap(string fileName, byte[] buff, int width, int height, int depthBytes, RGBQUAD[] palArray, bool skipPalette) { var bmf = new BITMAPFILEHEADER(); var bmi = new BITMAPINFOHEADER(); bmf.bfType = 0x4D42; var paletteSize = skipPalette ? 0 : (Marshal.SizeOf <RGBQUAD>() * (palArray?.Length ?? 0)); var offBits = Marshal.SizeOf <BITMAPFILEHEADER>() + Marshal.SizeOf <BITMAPINFOHEADER>() + paletteSize; bmf.bfSize = (uint)(offBits + buff.Length); bmf.bfOffBits = (uint)offBits; bmi.biSize = (uint)Marshal.SizeOf <BITMAPINFOHEADER>(); bmi.biWidth = width; bmi.biHeight = height; bmi.biPlanes = 1; bmi.biBitCount = (ushort)(depthBytes * 8); var fileStream = OpenFileOrDie(fileName, FileMode.Create); fileStream.Write(StructToBytes(bmf), 0, Marshal.SizeOf <BITMAPFILEHEADER>()); fileStream.Write(StructToBytes(bmi), 0, Marshal.SizeOf <BITMAPINFOHEADER>()); var paletteAndMap = depthBytes == 1 && !skipPalette ? (palArray ?? throw new ArgumentNullException(nameof(palArray), "A palette must exist for 8Bpp image.")) .SelectMany(StructToBytes).Concat(buff).ToArray() : buff; fileStream.Write(paletteAndMap, 0, paletteAndMap.Length); fileStream.Close(); }
public DecodingData(BITMAPFILEHEADER bmf, BITMAPINFOHEADER bmi, byte[] encodedData, RGBQUAD[] pal, byte[] alphaBuff, byte[] decodedData) { Bmf = bmf; Bmi = bmi; EncodedData = encodedData?.ToArray(); PalArray = pal?.ToArray(); AlphaBuff = alphaBuff?.ToArray(); DecodedData = decodedData?.ToArray(); }
private static void ReadBitmap(string fileName, out byte[] buffer, out long length, out BITMAPFILEHEADER bmf, out BITMAPINFOHEADER bmi) { var fileStream = OpenFileOrDie(fileName, FileMode.Open); try { var bmfB = new byte[Marshal.SizeOf <BITMAPFILEHEADER>()]; var bmiB = new byte[Marshal.SizeOf <BITMAPINFOHEADER>()]; length = fileStream.Length - (bmfB.Length + bmiB.Length); buffer = new byte[length]; fileStream.Read(bmfB, 0, Marshal.SizeOf <BITMAPFILEHEADER>()); fileStream.Read(bmiB, 0, Marshal.SizeOf <BITMAPINFOHEADER>()); fileStream.Read(buffer, 0, (int)length); // ReSharper disable once UnusedVariable bmf = ByteArrayToStructure <BITMAPFILEHEADER>(bmfB); bmi = ByteArrayToStructure <BITMAPINFOHEADER>(bmiB); } finally { fileStream.Close(); } }
public DecodingData(BITMAPFILEHEADER bmf, BITMAPINFOHEADER bmi, byte[] encodedData, RGBQUAD[] pal, byte[] alphaBuff, byte[] decodedData, AGFHDR agfHeader, ACIFHDR acifHeader) : this(bmf, bmi, encodedData, pal, alphaBuff, decodedData) { AgfHeader = agfHeader; AcifHeader = acifHeader; }