public void GetBackgroundColorDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.bKGD);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagBackgroundColor, new byte[] { 52 });
            Assert.Equal("Palette Index 52", descriptor.GetBackgroundColorDescription());
            Assert.Equal("Palette Index 52", directory.GetDescription(PngDirectory.TagBackgroundColor));
            directory.Set(PngDirectory.TagBackgroundColor, new byte[] { 0, 52 });
            Assert.Equal("Greyscale Level 52", descriptor.GetBackgroundColorDescription());
            Assert.Equal("Greyscale Level 52", directory.GetDescription(PngDirectory.TagBackgroundColor));
            directory.Set(PngDirectory.TagBackgroundColor, new byte[] { 0, 50, 0, 51, 0, 52 });
            Assert.Equal("R 50, G 51, B 52", descriptor.GetBackgroundColorDescription());
            Assert.Equal("R 50, G 51, B 52", directory.GetDescription(PngDirectory.TagBackgroundColor));
        }
        public void GetTextualDataDescription()
        {
            var latin1Encoding = Encoding.GetEncoding("iso-8859-1"); // Latin-1

            var         textPairs = new List <KeyValuePair>();
            StringValue value     = new StringValue(latin1Encoding.GetBytes("value"), latin1Encoding);

            textPairs.Add(new KeyValuePair("keyword", value));

            PngDirectory  directory  = new PngDirectory(PngChunkType.tEXt);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagTextualData, textPairs);
            Assert.Equal("keyword: value", descriptor.GetTextualDataDescription());
            Assert.Equal("keyword: value", directory.GetDescription(PngDirectory.TagTextualData));

            directory  = new PngDirectory(PngChunkType.zTXt);
            descriptor = new PngDescriptor(directory);
            directory.Set(PngDirectory.TagTextualData, textPairs);
            Assert.Equal("keyword: value", descriptor.GetTextualDataDescription());
            Assert.Equal("keyword: value", directory.GetDescription(PngDirectory.TagTextualData));

            directory  = new PngDirectory(PngChunkType.iTXt);
            descriptor = new PngDescriptor(directory);
            directory.Set(PngDirectory.TagTextualData, textPairs);
            Assert.Equal("keyword: value", descriptor.GetTextualDataDescription());
            Assert.Equal("keyword: value", directory.GetDescription(PngDirectory.TagTextualData));
        }
        public void GetUnitSpecifierDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.pHYs);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagUnitSpecifier, 1);
            Assert.Equal("Metres", descriptor.GetUnitSpecifierDescription());
            Assert.Equal("Metres", directory.GetDescription(PngDirectory.TagUnitSpecifier));
        }
        public void GetIsSrgbColorSpaceDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.sRGB);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagSrgbRenderingIntent, 0);
            Assert.Equal("Perceptual", descriptor.GetIsSrgbColorSpaceDescription());
            Assert.Equal("Perceptual", directory.GetDescription(PngDirectory.TagSrgbRenderingIntent));
        }
        public void GetPaletteHasTransparencyDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.tRNS);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagPaletteHasTransparency, 1);
            Assert.Equal("Yes", descriptor.GetPaletteHasTransparencyDescription());
            Assert.Equal("Yes", directory.GetDescription(PngDirectory.TagPaletteHasTransparency));
        }
        public void GetInterlaceMethodDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.IHDR);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagInterlaceMethod, 1);
            Assert.Equal("Adam7 Interlace", descriptor.GetInterlaceMethodDescription());
            Assert.Equal("Adam7 Interlace", directory.GetDescription(PngDirectory.TagInterlaceMethod));
        }
        public void GetFilterMethodDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.IHDR);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagFilterMethod, 0);
            Assert.Equal("Adaptive", descriptor.GetFilterMethodDescription());
            Assert.Equal("Adaptive", directory.GetDescription(PngDirectory.TagFilterMethod));
        }
        public void GetCompressionTypeDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.IHDR);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagCompressionType, 0);
            Assert.Equal("Deflate", descriptor.GetCompressionTypeDescription());
            Assert.Equal("Deflate", directory.GetDescription(PngDirectory.TagCompressionType));
        }
        public void GetColorTypeDescription()
        {
            PngDirectory  directory  = new PngDirectory(PngChunkType.IHDR);
            PngDescriptor descriptor = new PngDescriptor(directory);

            directory.Set(PngDirectory.TagColorType, 6);
            Assert.Equal("True Color with Alpha", descriptor.GetColorTypeDescription());
            Assert.Equal("True Color with Alpha", directory.GetDescription(PngDirectory.TagColorType));
        }
示例#10
0
    public static Metadata ParsePngDirectory(PngDirectory directory, Metadata metadata)
    {
        if (directory.TryGetInt32(PngDirectory.TagImageWidth, out var width))
        {
            metadata.Image.Width = width;
        }

        if (directory.TryGetInt32(PngDirectory.TagImageHeight, out var height))
        {
            metadata.Image.Height = height;
        }

        if (directory.TryGetInt32(PngDirectory.TagColorType, out var colorTypeId))
        {
            var colorType = PngColorType.FromNumericValue(colorTypeId);
            metadata.Image.PngColorType = colorType.Description;

            int?numberOfComponents = null;

            if (colorType == PngColorType.Greyscale)
            {
                numberOfComponents = 1;
            }
            else if (colorType == PngColorType.TrueColor)
            {
                numberOfComponents = 3;
            }
            else if (colorType == PngColorType.GreyscaleWithAlpha)
            {
                numberOfComponents = 2;
            }
            else if (colorType == PngColorType.TrueColorWithAlpha)
            {
                numberOfComponents = 4;
            }

            int?dataPrecision = null;
            if (directory.TryGetInt32(PngDirectory.TagBitsPerSample, out var bitsPerSample))
            {
                dataPrecision = bitsPerSample;
            }

            if (dataPrecision != null)
            {
                if (numberOfComponents != null)
                {
                    metadata.Image.BitDepth = (int)(numberOfComponents * dataPrecision);
                }
                else
                {
                    metadata.Image.DataPrecision = (int)dataPrecision;
                }
            }
        }

        if (directory.TryGetDouble(PngDirectory.TagGamma, out var gamma))
        {
            metadata.Image.Gamma = gamma;
        }

        if (directory.ContainsTag(PngDirectory.TagCompressionType))
        {
            metadata.Image.CompressionType = directory.GetDescription(PngDirectory.TagCompressionType);
        }

        if (directory.ContainsTag(PngDirectory.TagInterlaceMethod))
        {
            metadata.Image.InterlaceMethod = directory.GetDescription(PngDirectory.TagInterlaceMethod);
        }

        return(metadata);
    }