示例#1
0
        public int CreateBMP(string fileName, out List <BmpInfo> list)
        {
            var array = new BmpInfo[300];

            var ret = CreateBmp(fileName, array);

            list = new List <BmpInfo>();

            for (int i = 0; i < ret; i++)
            {
                list.Add(array[i]);
            }

            return(ret);
        }
        private static BmpInfo ReadBmpInfo(byte[] data, ref int offset)
        {
            var ret = new BmpInfo
            {
                BiSize          = ByteArrayReader.ReadUInt(data, ref offset),
                BiWidth         = ByteArrayReader.ReadInt(data, ref offset),
                BiHeight        = ByteArrayReader.ReadInt(data, ref offset),
                BiPlanes        = ByteArrayReader.ReadUShort(data, ref offset),
                BiBitCount      = ByteArrayReader.ReadUShort(data, ref offset),
                BiCompression   = ByteArrayReader.ReadUInt(data, ref offset),
                BiSizeImage     = ByteArrayReader.ReadUInt(data, ref offset),
                BiXPelsPerMeter = ByteArrayReader.ReadUInt(data, ref offset),
                BiYPelsPerMeter = ByteArrayReader.ReadUInt(data, ref offset),
                BiClrUsed       = ByteArrayReader.ReadUInt(data, ref offset),
                BiClrImportant  = ByteArrayReader.ReadUInt(data, ref offset)
            };

            return(ret);
        }
示例#3
0
        public void CSTestMethod()
        {
            try
            {
                var array = new BmpInfo[10];

                var cnt = CreateBmp("test", array);

                Console.WriteLine($"count in test: {cnt}");

                foreach (var bmp in array)
                {
                    Console.WriteLine($"fileName: {bmp.FileName} offset:{bmp.Offset}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Assert.Fail();
            }
        }
示例#4
0
        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);
        }
示例#5
0
        public static BmpInfo?ParseHeader(ImageBinReader s, ReadState ri)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }
            if (ri == null)
            {
                throw new ArgumentNullException(nameof(ri));
            }

            Span <byte> tmp = stackalloc byte[HeaderSize];

            if (!s.TryReadBytes(tmp))
            {
                return(null);
            }

            if (!Test(tmp))
            {
                throw new StbImageReadException(ErrorCode.UnknownFormat);
            }

            var info = new BmpInfo();

            s.ReadInt32LE();
            s.ReadInt16LE();
            s.ReadInt16LE();

            info.offset     = s.ReadInt32LE();
            info.headerSize = s.ReadInt32LE();
            info.mr         = info.mg = info.mb = info.ma = 0;

            if (info.headerSize != 12 &&
                info.headerSize != 40 &&
                info.headerSize != 56 &&
                info.headerSize != 108 &&
                info.headerSize != 124)
            {
                throw new StbImageReadException(ErrorCode.UnknownHeader);
            }

            if (info.headerSize == 12)
            {
                ri.Width  = s.ReadInt16LE();
                ri.Height = s.ReadInt16LE();
            }
            else
            {
                ri.Width  = s.ReadInt32LE();
                ri.Height = s.ReadInt32LE();
            }

            ri.Orientation = ri.Height > 0
                ? ImageOrientation.BottomLeftOrigin
                : ImageOrientation.TopLeftOrigin;

            ri.Height = Math.Abs(ri.Height);

            if (s.ReadInt16LE() != 1)
            {
                throw new StbImageReadException(ErrorCode.BadColorPlane);
            }

            info.bitsPerPixel = s.ReadInt16LE();
            if (info.bitsPerPixel == 1)
            {
                throw new StbImageReadException(ErrorCode.MonochromeNotSupported);
            }

            if (info.headerSize != 12)
            {
                int compress = s.ReadInt32LE();
                if ((compress == 1) || (compress == 2))
                {
                    throw new StbImageReadException(ErrorCode.RLENotSupported);
                }

                s.ReadInt32LE();
                s.ReadInt32LE();
                s.ReadInt32LE();
                s.ReadInt32LE();
                s.ReadInt32LE();

                if (info.headerSize == 40 ||
                    info.headerSize == 56)
                {
                    if (info.headerSize == 56)
                    {
                        s.ReadInt32LE();
                        s.ReadInt32LE();
                        s.ReadInt32LE();
                        s.ReadInt32LE();
                    }

                    if ((info.bitsPerPixel == 16) || (info.bitsPerPixel == 32))
                    {
                        if (compress == 0)
                        {
                            if (info.bitsPerPixel == 32)
                            {
                                info.mr = 0xffu << 16;
                                info.mg = 0xffu << 8;
                                info.mb = 0xffu << 0;
                                info.ma = 0xffu << 24;
                            }
                            else
                            {
                                info.mr = 31 << 10;
                                info.mg = 31 << 5;
                                info.mb = 31 << 0;
                            }
                        }
                        else if (compress == 3)
                        {
                            info.mr = s.ReadUInt32LE();
                            info.mg = s.ReadUInt32LE();
                            info.mb = s.ReadUInt32LE();

                            if ((info.mr == info.mg) && (info.mg == info.mb))
                            {
                                throw new StbImageReadException(ErrorCode.BadMasks);
                            }
                        }
                        else
                        {
                            throw new StbImageReadException(ErrorCode.BadCompression);
                        }
                    }
                }
                else
                {
                    if (info.headerSize != 108 &&
                        info.headerSize != 124)
                    {
                        throw new StbImageReadException(ErrorCode.UnknownHeader);
                    }

                    info.mr = s.ReadUInt32LE();
                    info.mg = s.ReadUInt32LE();
                    info.mb = s.ReadUInt32LE();
                    info.ma = s.ReadUInt32LE();

                    s.ReadInt32LE();

                    for (int i = 0; i < 12; ++i)
                    {
                        s.ReadInt32LE();
                    }

                    if (info.headerSize == 124)
                    {
                        for (int i = 0; i < 4; ++i)
                        {
                            s.ReadInt32LE();
                        }
                    }
                }
            }

            if (info.bitsPerPixel == 24 &&
                info.ma == 0xffu << 24)
            {
                ri.Components = 3;
            }
            else
            {
                ri.Components = info.ma != 0 ? 4 : 3;
            }

            ri.Depth = info.bitsPerPixel / ri.Components;

            return(info);
        }
示例#6
0
 public FrmHisgram(FastBitmap fBmp)
 {
     InitializeComponent();
     bmp     = fBmp;
     bmpInfo = new BmpInfo();
 }