Exemplo n.º 1
0
        public override void Init(Stream stream)
        {
            using (var bh = new ByteHelper(stream))
            {
                bh.Seek(0, SeekOrigin.Begin);

                // https://en.wikipedia.org/wiki/BMP_file_format
                IsValid = bh.ReadAscii(2) == "BM";

                if (!IsValid)
                {
                    return;
                }

                bh.Seek(18, SeekOrigin.Begin);
                Width  = (int)bh.ReadUint();
                Height = (int)bh.ReadUint();

                bh.Seek(38, SeekOrigin.Begin);
                var pixelsPerMeterH = (int)bh.ReadUint();
                DpiH = PixelsPerMeterToPixelsPerInch(pixelsPerMeterH);
                var pixelsPerMeterV = (int)bh.ReadUint();
                DpiV = PixelsPerMeterToPixelsPerInch(pixelsPerMeterV);
            }
        }
Exemplo n.º 2
0
        public override void Init(Stream stream)
        {
            using (var bh = new ByteHelper(stream))
            {
                bh.IsLsbf = false;
                bh.Seek(0, SeekOrigin.Begin);

                // http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html
                var signature = bh.ReadBytes(8);

                IsValid = signature[0] == 137 && signature[1] == 80 && signature[2] == 78 && signature[3] == 71 &&
                          signature[4] == 13 && signature[5] == 10 && signature[6] == 26 && signature[7] == 10;

                if (!IsValid)
                {
                    return;
                }

                DpiH = DpiV = 96;

                while (stream.Position < stream.Length)
                {
                    var chunkDataLength = bh.ReadUint();
                    var chunkType       = bh.ReadAscii(4);

                    switch (chunkType)
                    {
                    case "IHDR":
                        Width  = (int)bh.ReadUint();
                        Height = (int)bh.ReadUint();
                        bh.Seek(5);
                        break;

                    case "pHYs":
                        var pixelsPerUnitX = (int)bh.ReadUint();
                        var pixelsPerUnitY = (int)bh.ReadUint();
                        var unit           = bh.ReadByte();
                        if (unit == 1)
                        {
                            // Unit is the meter.
                            DpiH = PixelsPerMeterToPixelsPerInch(pixelsPerUnitX);
                            DpiV = PixelsPerMeterToPixelsPerInch(pixelsPerUnitY);
                        }

                        return;

                    default:
                        bh.Seek(chunkDataLength, SeekOrigin.Current);
                        break;
                    }

                    // Skip CRC.
                    bh.Seek(4, SeekOrigin.Current);
                }
            }
        }
Exemplo n.º 3
0
        public override void Init(Stream stream)
        {
            ushort resolutionUnit = 0;

            using (var bh = new ByteHelper(stream))
            {
                bh.Seek(0, SeekOrigin.Begin);

                var byteOrder = bh.ReadAscii(2);
                if (byteOrder != "II" && byteOrder != "MM")
                {
                    return;
                }

                bh.IsLsbf = byteOrder == "II";

                bh.Seek(2);

                var ifdOffset = bh.ReadUint();
                bh.Seek(ifdOffset, SeekOrigin.Begin);

                var entryCount = bh.ReadUshort();

                for (ushort i = 0; i < entryCount; ++i)
                {
                    var entryTag           = bh.ReadUshort();
                    var fieldType          = bh.ReadUshort();
                    var numberOfComponents = bh.ReadUint();

                    switch (entryTag)
                    {
                    case (int)Tags.ImageWidth:
                        switch (fieldType)
                        {
                        case (ushort)FieldTypes.Short:
                            Width = bh.ReadUshort();
                            bh.Seek(2);
                            break;

                        case (ushort)FieldTypes.Long:
                            Width = (int)bh.ReadUint();
                            break;
                        }

                        break;

                    case (int)Tags.ImageHeight:
                        switch (fieldType)
                        {
                        case (ushort)FieldTypes.Short:
                            Height = bh.ReadUshort();
                            bh.Seek(2);
                            break;

                        case (ushort)FieldTypes.Long:
                            Height = (int)bh.ReadUint();
                            break;
                        }

                        break;

                    case (int)Tags.XResolution:
                    {
                        // Field type is always rational.
                        var tagDataOffset = bh.ReadUint();
                        var currentOffset = stream.Position;
                        stream.Seek(tagDataOffset, SeekOrigin.Begin);
                        var numerator   = bh.ReadUint();
                        var denominator = bh.ReadUint();
                        DpiH = (int)Math.Round((double)numerator / denominator);
                        stream.Seek(currentOffset, SeekOrigin.Begin);
                        break;
                    }

                    case (int)Tags.YResolution:
                    {
                        // Field type is always rational.
                        var tagDataOffset = bh.ReadUint();
                        var currentOffset = stream.Position;
                        stream.Seek(tagDataOffset, SeekOrigin.Begin);
                        var numerator   = bh.ReadUint();
                        var denominator = bh.ReadUint();
                        DpiV = (int)Math.Round((double)numerator / denominator);
                        stream.Seek(currentOffset, SeekOrigin.Begin);
                        break;
                    }

                    case (int)Tags.ResolutionUnit:
                        resolutionUnit = bh.ReadUshort();
                        bh.Seek(2);
                        break;

                    default:
                        bh.Seek(4);
                        break;
                    }
                }
            }

            // https://www.awaresystems.be/imaging/tiff/tifftags/resolutionunit.html
            if (resolutionUnit == 0 || resolutionUnit == 1)
            {
                DpiH = DpiV = 72;
            }
            else if (resolutionUnit == 3)
            {
                DpiH = PixelsPerMeterToPixelsPerInch(DpiH * 100);
                DpiV = PixelsPerMeterToPixelsPerInch(DpiV * 100);
            }

            IsValid = true;
        }