Example #1
0
        public virtual void SRGBImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "sRGBImage.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(50, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(50, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(96, img.GetDpiX());
            NUnit.Framework.Assert.AreEqual(96, img.GetDpiY());
            NUnit.Framework.Assert.AreEqual(2.2, ((PngImageData)img).GetGamma(), 0.0001f);
            PngChromaticities pngChromaticities = ((PngImageData)img).GetPngChromaticities();

            NUnit.Framework.Assert.AreEqual(0.3127f, pngChromaticities.GetXW(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.329f, pngChromaticities.GetYW(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.64f, pngChromaticities.GetXR(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.33f, pngChromaticities.GetYR(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.3f, pngChromaticities.GetXG(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.6f, pngChromaticities.GetYG(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.15f, pngChromaticities.GetXB(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.06f, pngChromaticities.GetYB(), 0.0001f);
        }
Example #2
0
 public virtual void SRGBImageTest()
 {
     using (FileStream fis = new FileStream(sourceFolder + "sRGBImage.png", FileMode.Open, FileAccess.Read)) {
         byte[] imageBytes = StreamUtil.InputStreamToArray(fis);
         // Test a more specific entry point
         ImageData img = ImageDataFactory.CreatePng(imageBytes);
         NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
         NUnit.Framework.Assert.AreEqual(50, img.GetWidth(), 0);
         NUnit.Framework.Assert.AreEqual(50, img.GetHeight(), 0);
         NUnit.Framework.Assert.AreEqual(96, img.GetDpiX());
         NUnit.Framework.Assert.AreEqual(96, img.GetDpiY());
         NUnit.Framework.Assert.AreEqual(2.2, ((PngImageData)img).GetGamma(), 0.0001f);
         PngChromaticities pngChromaticities = ((PngImageData)img).GetPngChromaticities();
         NUnit.Framework.Assert.AreEqual(0.3127f, pngChromaticities.GetXW(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.329f, pngChromaticities.GetYW(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.64f, pngChromaticities.GetXR(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.33f, pngChromaticities.GetYR(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.3f, pngChromaticities.GetXG(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.6f, pngChromaticities.GetYG(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.15f, pngChromaticities.GetXB(), 0.0001f);
         NUnit.Framework.Assert.AreEqual(0.06f, pngChromaticities.GetYB(), 0.0001f);
     }
 }
Example #3
0
        private static void ReadPng(Stream pngStream, PngImageHelper.PngParameters png)
        {
            for (int i = 0; i < PNGID.Length; i++)
            {
                if (PNGID[i] != pngStream.Read())
                {
                    throw new System.IO.IOException("file.is.not.a.valid.png");
                }
            }
            byte[] buffer = new byte[TRANSFERSIZE];
            while (true)
            {
                int    len    = GetInt(pngStream);
                String marker = GetString(pngStream);
                if (len < 0 || !CheckMarker(marker))
                {
                    throw new System.IO.IOException("corrupted.png.file");
                }
                if (IDAT.Equals(marker))
                {
                    int size;
                    while (len != 0)
                    {
                        size = pngStream.JRead(buffer, 0, Math.Min(len, TRANSFERSIZE));
                        if (size < 0)
                        {
                            return;
                        }
                        png.idat.Write(buffer, 0, size);
                        len -= size;
                    }
                }
                else
                {
                    if (tRNS.Equals(marker))
                    {
                        switch (png.image.GetColorType())
                        {
                        case 0: {
                            if (len >= 2)
                            {
                                len -= 2;
                                int gray = GetWord(pngStream);
                                if (png.bitDepth == 16)
                                {
                                    png.transRedGray = gray;
                                }
                                else
                                {
                                    png.additional.Put(PngImageHelperConstants.MASK, MessageFormatUtil.Format("[{0} {1}]", gray, gray));
                                }
                            }
                            break;
                        }

                        case 2: {
                            if (len >= 6)
                            {
                                len -= 6;
                                int red   = GetWord(pngStream);
                                int green = GetWord(pngStream);
                                int blue  = GetWord(pngStream);
                                if (png.bitDepth == 16)
                                {
                                    png.transRedGray = red;
                                    png.transGreen   = green;
                                    png.transBlue    = blue;
                                }
                                else
                                {
                                    png.additional.Put(PngImageHelperConstants.MASK, MessageFormatUtil.Format("[{0} {1} {2} {3} {4} {5}]", red
                                                                                                              , red, green, green, blue, blue));
                                }
                            }
                            break;
                        }

                        case 3: {
                            if (len > 0)
                            {
                                png.trans = new byte[len];
                                for (int k = 0; k < len; ++k)
                                {
                                    png.trans[k] = (byte)pngStream.Read();
                                }
                                len = 0;
                            }
                            break;
                        }
                        }
                        StreamUtil.Skip(pngStream, len);
                    }
                    else
                    {
                        if (IHDR.Equals(marker))
                        {
                            png.width    = GetInt(pngStream);
                            png.height   = GetInt(pngStream);
                            png.bitDepth = pngStream.Read();
                            png.image.SetColorType(pngStream.Read());
                            png.compressionMethod = pngStream.Read();
                            png.filterMethod      = pngStream.Read();
                            png.interlaceMethod   = pngStream.Read();
                        }
                        else
                        {
                            if (PLTE.Equals(marker))
                            {
                                if (png.image.IsIndexed())
                                {
                                    ByteBuffer colorTableBuf = new ByteBuffer();
                                    while ((len--) > 0)
                                    {
                                        colorTableBuf.Append(pngStream.Read());
                                    }
                                    png.image.SetColorPalette(colorTableBuf.ToByteArray());
                                }
                                else
                                {
                                    StreamUtil.Skip(pngStream, len);
                                }
                            }
                            else
                            {
                                if (pHYs.Equals(marker))
                                {
                                    int dx   = GetInt(pngStream);
                                    int dy   = GetInt(pngStream);
                                    int unit = pngStream.Read();
                                    if (unit == 1)
                                    {
                                        png.dpiX = (int)(dx * 0.0254f + 0.5f);
                                        png.dpiY = (int)(dy * 0.0254f + 0.5f);
                                    }
                                    else
                                    {
                                        if (dy != 0)
                                        {
                                            png.XYRatio = (float)dx / (float)dy;
                                        }
                                    }
                                }
                                else
                                {
                                    if (cHRM.Equals(marker))
                                    {
                                        PngChromaticities pngChromaticities = new PngChromaticities(GetInt(pngStream) / 100000f, GetInt(pngStream)
                                                                                                    / 100000f, GetInt(pngStream) / 100000f, GetInt(pngStream) / 100000f, GetInt(pngStream) / 100000f, GetInt
                                                                                                        (pngStream) / 100000f, GetInt(pngStream) / 100000f, GetInt(pngStream) / 100000f);
                                        if (!(Math.Abs(pngChromaticities.GetXW()) < 0.0001f || Math.Abs(pngChromaticities.GetYW()) < 0.0001f || Math
                                              .Abs(pngChromaticities.GetXR()) < 0.0001f || Math.Abs(pngChromaticities.GetYR()) < 0.0001f || Math.Abs
                                                  (pngChromaticities.GetXG()) < 0.0001f || Math.Abs(pngChromaticities.GetYG()) < 0.0001f || Math.Abs(pngChromaticities
                                                                                                                                                     .GetXB()) < 0.0001f || Math.Abs(pngChromaticities.GetYB()) < 0.0001f))
                                        {
                                            png.image.SetPngChromaticities(pngChromaticities);
                                        }
                                    }
                                    else
                                    {
                                        if (sRGB.Equals(marker))
                                        {
                                            int ri = pngStream.Read();
                                            png.intent = intents[ri];
                                            png.image.SetGamma(2.2f);
                                            PngChromaticities pngChromaticities = new PngChromaticities(0.3127f, 0.329f, 0.64f, 0.33f, 0.3f, 0.6f, 0.15f
                                                                                                        , 0.06f);
                                            png.image.SetPngChromaticities(pngChromaticities);
                                        }
                                        else
                                        {
                                            if (gAMA.Equals(marker))
                                            {
                                                int gm = GetInt(pngStream);
                                                if (gm != 0)
                                                {
                                                    png.image.SetGamma(100000f / gm);
                                                    if (!png.image.IsHasCHRM())
                                                    {
                                                        PngChromaticities pngChromaticities = new PngChromaticities(0.3127f, 0.329f, 0.64f, 0.33f, 0.3f, 0.6f, 0.15f
                                                                                                                    , 0.06f);
                                                        png.image.SetPngChromaticities(pngChromaticities);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (iCCP.Equals(marker))
                                                {
                                                    do
                                                    {
                                                        --len;
                                                    }while (pngStream.Read() != 0);
                                                    pngStream.Read();
                                                    --len;
                                                    byte[] icccom = new byte[len];
                                                    int    p      = 0;
                                                    while (len > 0)
                                                    {
                                                        int r = pngStream.JRead(icccom, p, len);
                                                        if (r < 0)
                                                        {
                                                            throw new System.IO.IOException("premature.end.of.file");
                                                        }
                                                        p   += r;
                                                        len -= r;
                                                    }
                                                    byte[] iccp = FilterUtil.FlateDecode(icccom, true);
                                                    icccom = null;
                                                    try {
                                                        png.iccProfile = IccProfile.GetInstance(iccp);
                                                    }
                                                    catch (Exception) {
                                                        png.iccProfile = null;
                                                    }
                                                }
                                                else
                                                {
                                                    if (IEND.Equals(marker))
                                                    {
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        StreamUtil.Skip(pngStream, len);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                StreamUtil.Skip(pngStream, 4);
            }
        }