示例#1
0
        /// <summary>
        /// Creates a new chunk with the specified type.
        /// </summary>
        /// <param name="type">The type of chunk.</param>
        /// <param name="image">The parent image.</param>
        public Chunk(string type, PNGImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("The image must not be null.");
            }

            _image = image;

            this.AssertChunkType(type);
            _type = type;
        }
示例#2
0
        public IHDRChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            this.AssertDataLength(data, 13);

            this.Width             = Utils.BytesToUInt(data, 4, Utils.Endianness.Big);
            this.Height            = Utils.BytesToUInt(data, 8, Utils.Endianness.Big);
            this.ColorType         = (ColorType)data[13];
            this.BitDepth          = data[12];
            this.CompressionMethod = (CompressionMethod)data[14];
            this.FilterMethod      = (FilterMethod)data[15];
            this.InterlaceMethod   = (InterlaceMethod)data[16];
        }
示例#3
0
        public tIMEChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            this.AssertDataLength(data, 7);

            _time = new DateTime(
                Utils.BytesToUShort(data, 4, Utils.Endianness.Big),
                data[6],
                data[7],
                data[8],
                data[9],
                data[10]);
        }
示例#4
0
        public tEXtChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            int i = 0;

            while (data[i + 4] != 0)
            {
                i++;
            }

            this.Keyword = ASCIIEncoding.ASCII.GetString(data, 4, i);
            this.Text    = ASCIIEncoding.ASCII.GetString(data, i + 5, data.Length - 4 - i - 1);
        }
示例#5
0
        public cHRMChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            this.AssertDataLength(data, 32);

            _whiteX = ((float)Utils.BytesToUInt(data, 4, Utils.Endianness.Big)) / 100000;
            _whiteY = ((float)Utils.BytesToUInt(data, 8, Utils.Endianness.Big)) / 100000;
            _redX   = ((float)Utils.BytesToUInt(data, 12, Utils.Endianness.Big)) / 100000;
            _redY   = ((float)Utils.BytesToUInt(data, 16, Utils.Endianness.Big)) / 100000;
            _greenX = ((float)Utils.BytesToUInt(data, 20, Utils.Endianness.Big)) / 100000;
            _greenY = ((float)Utils.BytesToUInt(data, 24, Utils.Endianness.Big)) / 100000;
            _blueX  = ((float)Utils.BytesToUInt(data, 28, Utils.Endianness.Big)) / 100000;
            _blueY  = ((float)Utils.BytesToUInt(data, 32, Utils.Endianness.Big)) / 100000;
        }
示例#6
0
        public hISTChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            if (data.Length - 4 != this.GetAssertPLTEChunk().Entries.Length * 2)
            {
                throw new InvalidChunkLengthException("Chunk length must equal to the number of palette entries times 2.");
            }

            _histogram = new ushort[(data.Length - 4) / 2];

            for (int i = 0; i < (data.Length - 4) / 2; i++)
            {
                _histogram[i] = Utils.BytesToUShort(data, i * 2 + 4, Utils.Endianness.Big);
            }
        }
示例#7
0
        public tRNSChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            IHDRChunk hdr = this.GetAssertIHDRChunk();

            switch (hdr.ColorType)
            {
            case ColorType.Grayscale:
                this.AssertDataLength(data, 2);
                _grayValue = Utils.BytesToUShort(data, 4, Utils.Endianness.Big);
                break;

            case ColorType.GrayscaleWithAlpha:
                throw new InvalidChunkDataException("The tRNS chunk cannot appear for the grayscale with alpha color type.");

            case ColorType.Truecolor:
                this.AssertDataLength(data, 6);
                _redValue   = Utils.BytesToUShort(data, 4, Utils.Endianness.Big);
                _greenValue = Utils.BytesToUShort(data, 6, Utils.Endianness.Big);
                _blueValue  = Utils.BytesToUShort(data, 8, Utils.Endianness.Big);
                break;

            case ColorType.TruecolorWithAlpha:
                throw new InvalidChunkDataException("The tRNS chunk cannot appear for the truecolor with alpha color type.");

            case ColorType.IndexedColor:
                if (data.Length - 4 > this.GetAssertPLTEChunk().Entries.Length)
                {
                    throw new InvalidChunkLengthException("tRNS chunk must contain the same amount of entries as the palette chunk or less.");
                }

                _paletteAlpha = new byte[data.Length - 4];

                for (int i = 0; i < data.Length - 4; i++)
                {
                    _paletteAlpha[i] = data[i + 4];
                }

                break;

            default:
                throw new InvalidChunkDataException("Invalid color type.");
            }
        }
示例#8
0
        /// <summary>
        /// Creates a new chunk with the specified data (which must include the chunk type in
        /// the first four bytes).
        /// </summary>
        /// <param name="data">The chunk data.</param>
        /// <param name="image">The parent image.</param>
        public Chunk(byte[] data, PNGImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("The image must not be null.");
            }

            _image = image;

            ByteStreamReader bs = new ByteStreamReader(data);

            _type = Utils.ReadString(bs, 4);
            this.AssertChunkType(_type);

            if (!image.Classes.ContainsKey(_type))
            {
                _data = data;
            }
        }
示例#9
0
        public zTXtChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            int i = 0;

            while (data[i + 4] != 0)
            {
                i++;
            }

            this.Keyword           = ASCIIEncoding.ASCII.GetString(data, 4, i);
            this.CompressionMethod = (CompressionMethod)data[i + 5];

            ByteStreamReader bs = new ByteStreamReader(data);

            bs.Seek(i + 6, SeekOrigin.Begin);

            InflaterInputStream iis = new InflaterInputStream(bs, new Inflater(), data.Length - 4 - i - 2);
            StringBuilder       sb  = new StringBuilder();

            while (iis.Available == 1)
            {
                int b = iis.ReadByte();

                if (b == -1)
                {
                    break;
                }

                sb.Append(System.Text.ASCIIEncoding.ASCII.GetChars(new byte[] { (byte)b })[0]);
            }

            this.Text = sb.ToString();

            iis.Close();
            bs.Close();
        }
示例#10
0
        public PLTEChunk(byte[] data, PNGImage image)
            : base(data, image)
        {
            if (data.Length - 4 < 3)
            {
                throw new InvalidChunkLengthException("There must be at least one palette entry.");
            }
            if ((data.Length - 4) % 3 != 0)
            {
                throw new InvalidChunkLengthException("Chunk length must be divisible by 3.");
            }
            if ((data.Length - 4) > 3 * 256)
            {
                throw new InvalidChunkLengthException("There must be 256 or less palette entries.");
            }

            _entries = new Color[(data.Length - 4) / 3];

            for (int i = 0; i < (data.Length - 4) / 3; i++)
            {
                _entries[i] = Color.FromArgb(
                    data[i * 3 + 4], data[i * 3 + 5], data[i * 3 + 6]);
            }
        }
示例#11
0
 public bKGDChunk(PNGImage image)
     : base("bKGD", image)
 {
     _backgroundColor = new BackgroundColor();
 }
示例#12
0
 public hISTChunk(PNGImage image)
     : base("hIST", image)
 {
     _histogram = new ushort[this.GetAssertPLTEChunk().Entries.Length];
 }
示例#13
0
 public zTXtChunk(PNGImage image)
     : base("zTXt", image)
 {
 }
示例#14
0
 public cHRMChunk(PNGImage image) : base("cHRM", image)
 {
 }
示例#15
0
 public sRGBChunk(PNGImage image)
     : base("sRGB", image)
 {
     _ri = RenderingIntent.Perceptual;
 }
示例#16
0
 public gAMAChunk(PNGImage image)
     : base("gAMA", image)
 {
     _gamma = 1;
 }
示例#17
0
        /// <summary>
        /// Reads a PNG file from the specified stream.
        /// </summary>
        /// <param name="s">The stream to read from.</param>
        public PNGBitmap(Stream s)
        {
            _image = new PNGImage(s);

            this.Read(this.Decompress());
        }
示例#18
0
 public pHYsChunk(PNGImage image)
     : base("pHYs", image)
 {
 }
示例#19
0
 public IENDChunk(PNGImage image)
     : base("IEND", image)
 {
 }
示例#20
0
 public tRNSChunk(PNGImage image)
     : base("tRNS", image)
 {
     _paletteAlpha = new byte[0];
 }
示例#21
0
 public IDATChunk(byte[] data, PNGImage image)
     : base(data, image)
 {
     _data = new byte[data.Length - 4];
     Array.Copy(data, 4, _data, 0, data.Length - 4);
 }
示例#22
0
 public IDATChunk(PNGImage image)
     : base("IDAT", image)
 {
 }
示例#23
0
 public tEXtChunk(PNGImage image)
     : base("tEXt", image)
 {
 }
示例#24
0
 public sBITChunk(PNGImage image)
     : base("sBIT", image)
 {
     _sb = new SignificantBits();
 }
示例#25
0
 public tIMEChunk(PNGImage image)
     : base("tIME", image)
 {
 }
示例#26
0
 public IENDChunk(byte[] data, PNGImage image)
     : base(data, image)
 {
     this.AssertDataLength(data, 0);
 }
示例#27
0
 public PLTEChunk(PNGImage image)
     : base("PLTE", image)
 {
     _entries = new Color[0];
 }