Example #1
0
 /// <summary>
 /// Writes the bitmap header data to the binary stream.
 /// </summary>
 /// <param name="writer">
 /// The <see cref="EndianBinaryWriter"/> containing the stream to write to.
 /// </param>
 /// <param name="fileHeader">
 /// The <see cref="BmpFileHeader"/> containing the header data.
 /// </param>
 private static void WriteHeader(EndianBinaryWriter writer, BmpFileHeader fileHeader)
 {
     writer.Write(fileHeader.Type);
     writer.Write(fileHeader.FileSize);
     writer.Write(fileHeader.Reserved);
     writer.Write(fileHeader.Offset);
 }
Example #2
0
        /// <summary>
        /// Reads the <see cref="BmpFileHeader"/> from the stream.
        /// </summary>
        private void ReadFileHeader()
        {
            byte[] data = new byte[BmpFileHeader.Size];

            this.currentStream.Read(data, 0, BmpFileHeader.Size);

            this.fileHeader = new BmpFileHeader
            {
                Type     = BitConverter.ToInt16(data, 0),
                FileSize = BitConverter.ToInt32(data, 2),
                Reserved = BitConverter.ToInt32(data, 6),
                Offset   = BitConverter.ToInt32(data, 10)
            };
        }
        /// <inheritdoc/>
        public void Encode(ImageBase image, Stream stream)
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            int rowWidth = image.Width;

            int amount = (image.Width * 3) % 4;
            if (amount != 0)
            {
                rowWidth += 4 - amount;
            }

            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                BmpFileHeader fileHeader = new BmpFileHeader
                {
                    Type = 19778, // BM
                    Offset = 54,
                    FileSize = 54 + (image.Height * rowWidth * 3)
                };

                WriteHeader(writer, fileHeader);

                BmpInfoHeader infoHeader = new BmpInfoHeader
                {
                    HeaderSize = 40,
                    Height = image.Height,
                    Width = image.Width,
                    BitsPerPixel = 24,
                    Planes = 1,
                    Compression = BmpCompression.RGB,
                    ImageSize = image.Height * rowWidth * 3,
                    ClrUsed = 0,
                    ClrImportant = 0
                };

                WriteInfo(writer, infoHeader);

                this.WriteImage(writer, image);

                writer.Flush();
            }
        }
Example #4
0
        /// <summary>
        /// Encodes the image to the specified stream from the <see cref="ImageBase{T,TP}"/>.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="image">The <see cref="ImageBase{T,TP}"/> to encode from.</param>
        /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
        /// <param name="bitsPerPixel">The <see cref="BmpBitsPerPixel"/></param>
        public void Encode <T, TP>(ImageBase <T, TP> image, Stream stream, BmpBitsPerPixel bitsPerPixel)
            where T : IPackedVector <TP>
            where TP : struct
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            this.bmpBitsPerPixel = bitsPerPixel;

            // Cast to int will get the bytes per pixel
            short bpp          = (short)(8 * (int)bitsPerPixel);
            int   bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32);

            this.padding = bytesPerLine - (image.Width * (int)bitsPerPixel);

            // Do not use IDisposable pattern here as we want to preserve the stream.
            EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream);

            BmpInfoHeader infoHeader = new BmpInfoHeader
            {
                HeaderSize   = BmpInfoHeader.Size,
                Height       = image.Height,
                Width        = image.Width,
                BitsPerPixel = bpp,
                Planes       = 1,
                ImageSize    = image.Height * bytesPerLine,
                ClrUsed      = 0,
                ClrImportant = 0
            };

            BmpFileHeader fileHeader = new BmpFileHeader
            {
                Type     = 19778, // BM
                Offset   = 54,
                FileSize = 54 + infoHeader.ImageSize
            };

            WriteHeader(writer, fileHeader);
            this.WriteInfo(writer, infoHeader);
            this.WriteImage(writer, image);

            writer.Flush();
        }
 /// <summary>
 /// Writes the bitmap header data to the binary stream.
 /// </summary>
 /// <param name="writer">
 /// The <see cref="BinaryWriter"/> containing the stream to write to.
 /// </param>
 /// <param name="fileHeader">
 /// The <see cref="BmpFileHeader"/> containing the header data.
 /// </param>
 private static void WriteHeader(BinaryWriter writer, BmpFileHeader fileHeader)
 {
     writer.Write(fileHeader.Type);
     writer.Write(fileHeader.FileSize);
     writer.Write(fileHeader.Reserved);
     writer.Write(fileHeader.Offset);
 }
        /// <summary>
        /// Reads the <see cref="BmpFileHeader"/> from the stream.
        /// </summary>
        private void ReadFileHeader()
        {
            byte[] data = new byte[BmpFileHeader.Size];

            this.currentStream.Read(data, 0, BmpFileHeader.Size);

            this.fileHeader = new BmpFileHeader
            {
                Type = BitConverter.ToInt16(data, 0),
                FileSize = BitConverter.ToInt32(data, 2),
                Reserved = BitConverter.ToInt32(data, 6),
                Offset = BitConverter.ToInt32(data, 10)
            };
        }