private static void ReadBmpHeader(BinaryReader fileIn, BmpHeader bmpHeader)
 {
     bmpHeader.BfType[0]       = fileIn.ReadByte();
     bmpHeader.BfType[1]       = fileIn.ReadByte();
     bmpHeader.BfSize          = fileIn.ReadInt32();
     bmpHeader.Reserved        = fileIn.ReadInt32();
     bmpHeader.BfOffbits       = fileIn.ReadInt32();
     bmpHeader.BiSize          = fileIn.ReadInt32();
     bmpHeader.BiWidth         = fileIn.ReadInt32();
     bmpHeader.BiHeight        = fileIn.ReadInt32();
     bmpHeader.BiPlanes        = fileIn.ReadInt16();
     bmpHeader.BiBitCount      = fileIn.ReadInt16();
     bmpHeader.BiCompression   = fileIn.ReadInt32();
     bmpHeader.BiSizeImage     = fileIn.ReadInt32();
     bmpHeader.BiXPelsPerMeter = fileIn.ReadInt32();
     bmpHeader.BiYPelsPerMeter = fileIn.ReadInt32();
     bmpHeader.BiClrUsed       = fileIn.ReadInt32();
     bmpHeader.BiClrImportant  = fileIn.ReadInt32();
 }
        private static void ReadPixels(BinaryReader fileIn, RGB[,] data, BmpHeader bmpHeader)
        {
            for (int i = 0; i < bmpHeader.BiHeight; i++)
            {
                for (int j = 0; j < bmpHeader.BiWidth; j++)
                {
                    data[i, j] = new RGB(new byte[] { fileIn.ReadByte(), fileIn.ReadByte(), fileIn.ReadByte() });
                    if (bmpHeader.BiBitCount == 32)
                    {
                        fileIn.ReadByte();
                    }
                }
                fileIn.ReadBytes(bmpHeader.GetPadding());
            }

            while (fileIn.PeekChar() > -1)
            {
                bmpHeader.ResidualBytes.Add(fileIn.ReadByte());
            }
        }
Beispiel #3
0
        private void WriteHeader(int width, int depth)
        {
            var header = BmpHeader.GetBase(width, depth);

            _buffer.Write(Encoding.UTF8.GetBytes(header.Flag));
            _buffer.Write(BitConverter.GetBytes(header.FileSize));
            _buffer.Write(header.Reserved);
            _buffer.Write(BitConverter.GetBytes(header.HeaderSize));
            _buffer.Write(BitConverter.GetBytes(header.InfoSize));
            _buffer.Write(BitConverter.GetBytes(header.Width));
            _buffer.Write(BitConverter.GetBytes(header.Depth));
            _buffer.Write(BitConverter.GetBytes(header.BiPlanes));
            _buffer.Write(BitConverter.GetBytes(header.Bits));
            _buffer.Write(BitConverter.GetBytes(header.BiCompression));
            _buffer.Write(BitConverter.GetBytes(header.BiSizeImage));
            _buffer.Write(BitConverter.GetBytes(header.BiXPelsPerMeter));
            _buffer.Write(BitConverter.GetBytes(header.BiYPelsPerMeter));
            _buffer.Write(BitConverter.GetBytes(header.BiClrUsed));
            _buffer.Write(BitConverter.GetBytes(header.BiClrImportant));
        }
 private void ReadHeader()
 {
     _header = new BmpHeader
     {
         Flag            = String(2),
         FileSize        = Int(),
         Reserved        = Bytes(4),
         HeaderSize      = Int(),
         InfoSize        = Int(),
         Width           = Int(),
         Depth           = Int(),
         BiPlanes        = Short(),
         Bits            = Short(),
         BiCompression   = Int(),
         BiSizeImage     = Int(),
         BiXPelsPerMeter = Int(),
         BiYPelsPerMeter = Int(),
         BiClrUsed       = Int(),
         BiClrImportant  = Int()
     };
 }
Beispiel #5
0
        private static BmpPixel[,] ParseImageData(byte[] bmpBytes, BmpHeader header, BmpInfoHeader infoHeader)
        {
            if (infoHeader.Compression != 0)
            {
                var debug = "compression";
            }

            var offSet     = header.DataOffSet;
            var pixelCount = infoHeader.ImageSize > 0 ? infoHeader.ImageSize : infoHeader.Width * infoHeader.Height;

            var pixels    = new BmpPixel[infoHeader.Height, infoHeader.Width];
            var byteSteps = infoHeader.BitPerPixel / 8;

            for (var i = 0; i < pixelCount; i++)
            {
                var byteIndex = (i * byteSteps) + offSet;
                var y         = infoHeader.Height - 1 - i / infoHeader.Width;
                var x         = i % infoHeader.Width;
                var pixel     = new BmpPixel
                {
                    Blue  = bmpBytes[byteIndex + 0],
                    Green = bmpBytes[byteIndex + 1],
                    Red   = bmpBytes[byteIndex + 2]
                };
                if (infoHeader.BitPerPixel >= 32)
                {
                    pixel.Alpha = bmpBytes[byteIndex + 2];
                }
                else
                {
                    pixel.Alpha = 255;
                }

                pixels[y, x] = pixel;
            }

            return(pixels);
        }
Beispiel #6
0
        public static Bmp FromPixels(BmpPixel[,] pixels, ushort bitsPerPixel = 32)
        {
            var header = new BmpHeader {
                DataOffSet = 54,
                FileSize   = (uint)(pixels.Length * (bitsPerPixel / 8) + 54),
                Reserved   = 0,
                Signature  = 0x4d42
            };

            var height = (uint)pixels.GetLength(0);
            var width  = (uint)pixels.GetLength(1);

            var infoHeader = new BmpInfoHeader
            {
                BitPerPixel     = bitsPerPixel,
                ColorsUsed      = 0,
                Compression     = 0,
                Height          = height,
                Width           = width,
                ImageSize       = 0,
                ImportantColors = 0,
                Planes          = 1,
                Size            = 40,
                XpixelsPerM     = 3780,
                YpixelsPerM     = 3780,
            };


            var pixelData = pixels;

            return(new Bmp
            {
                Header = header,
                InfoHeader = infoHeader,
                Pixels = pixelData
            });
        }
        public static Image ReadBmp(string?file)
        {
            if (file is null)
            {
                Console.WriteLine("You have not provided the path to the reading file.");
                return(EmptyImage.CreateEmptyImage());
            }

            try
            {
                using (BinaryReader fileIn = new BinaryReader(File.Open(file, FileMode.Open)))
                {
                    BmpHeader bmpHeader = new BmpHeader();
                    ReadBmpHeader(fileIn, bmpHeader);
                    if (!bmpHeader.IsValid())
                    {
                        Console.WriteLine("The image must be in bmp format and have a color depth of 32 or 24 bits per pixel and must not be compressed");
                        return(EmptyImage.CreateEmptyImage());
                    }
                    ReadPalette(fileIn, bmpHeader);
                    RGB[,] data = new RGB[bmpHeader.BiHeight, bmpHeader.BiWidth];
                    ReadPixels(fileIn, data, bmpHeader);
                    return(new Image(bmpHeader, data));
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine($"File with path: {file} not found.");
                return(EmptyImage.CreateEmptyImage());
            }
            catch (IOException)
            {
                Console.WriteLine("Working with this file is not possible.");
                return(EmptyImage.CreateEmptyImage());
            }
        }
 private protected Image(BmpHeader bmpHeader, RGB[,] pixels)
 {
     this.bmpHeader = bmpHeader;
     this.pixels    = pixels;
 }
Beispiel #9
0
 private EmptyImage(BmpHeader bmpHeader, RGB[,] pixels) : base(bmpHeader, pixels)
 {
     filterSuccess = false;
 }