Beispiel #1
0
        public virtual void Size300Px72DpiImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "size300Px72Dpi.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(300, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(300, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(72, img.GetDpiX());
            NUnit.Framework.Assert.AreEqual(72, img.GetDpiY());
        }
Beispiel #2
0
        public virtual void Size300Px300DpiImageTest()
        {
            // Test a more specific entry point
            ImageData img = ImageDataFactory.CreatePng(UrlUtil.ToURL(sourceFolder + "size300Px300Dpi.png"));

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(300, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(300, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(300, img.GetDpiX());
            NUnit.Framework.Assert.AreEqual(300, img.GetDpiY());
        }
Beispiel #3
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);
        }
Beispiel #4
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);
     }
 }
Beispiel #5
0
        /// <summary>This method checks if the image is a valid JPEG and processes some parameters.</summary>
        /// <exception cref="iText.IO.IOException"/>
        /// <exception cref="System.IO.IOException"/>
        private static void ProcessParameters(Stream jpegStream, String errorID, ImageData image)
        {
            byte[][] icc = null;
            if (jpegStream.Read() != 0xFF || jpegStream.Read() != 0xD8)
            {
                throw new iText.IO.IOException(iText.IO.IOException._1IsNotAValidJpegFile).SetMessageParams(errorID);
            }
            bool firstPass = true;
            int  len;

            while (true)
            {
                int v = jpegStream.Read();
                if (v < 0)
                {
                    throw new iText.IO.IOException(iText.IO.IOException.PrematureEofWhileReadingJpeg);
                }
                if (v == 0xFF)
                {
                    int marker = jpegStream.Read();
                    if (firstPass && marker == M_APP0)
                    {
                        firstPass = false;
                        len       = GetShort(jpegStream);
                        if (len < 16)
                        {
                            StreamUtil.Skip(jpegStream, len - 2);
                            continue;
                        }
                        byte[] bcomp = new byte[JFIF_ID.Length];
                        int    r     = jpegStream.Read(bcomp);
                        if (r != bcomp.Length)
                        {
                            throw new iText.IO.IOException(iText.IO.IOException._1CorruptedJfifMarker).SetMessageParams(errorID);
                        }
                        bool found = true;
                        for (int k = 0; k < bcomp.Length; ++k)
                        {
                            if (bcomp[k] != JFIF_ID[k])
                            {
                                found = false;
                                break;
                            }
                        }
                        if (!found)
                        {
                            StreamUtil.Skip(jpegStream, len - 2 - bcomp.Length);
                            continue;
                        }
                        StreamUtil.Skip(jpegStream, 2);
                        int units = jpegStream.Read();
                        int dx    = GetShort(jpegStream);
                        int dy    = GetShort(jpegStream);
                        if (units == 1)
                        {
                            image.SetDpi(dx, dy);
                        }
                        else
                        {
                            if (units == 2)
                            {
                                image.SetDpi((int)(dx * 2.54f + 0.5f), (int)(dy * 2.54f + 0.5f));
                            }
                        }
                        StreamUtil.Skip(jpegStream, len - 2 - bcomp.Length - 7);
                        continue;
                    }
                    if (marker == M_APPE)
                    {
                        len = GetShort(jpegStream) - 2;
                        byte[] byteappe = new byte[len];
                        for (int k = 0; k < len; ++k)
                        {
                            byteappe[k] = (byte)jpegStream.Read();
                        }
                        if (byteappe.Length >= 12)
                        {
                            String appe = iText.IO.Util.JavaUtil.GetStringForBytes(byteappe, 0, 5, "ISO-8859-1");
                            if (appe.Equals("Adobe"))
                            {
                                image.SetInverted(true);
                            }
                        }
                        continue;
                    }
                    if (marker == M_APP2)
                    {
                        len = GetShort(jpegStream) - 2;
                        byte[] byteapp2 = new byte[len];
                        for (int k = 0; k < len; ++k)
                        {
                            byteapp2[k] = (byte)jpegStream.Read();
                        }
                        if (byteapp2.Length >= 14)
                        {
                            String app2 = iText.IO.Util.JavaUtil.GetStringForBytes(byteapp2, 0, 11, "ISO-8859-1");
                            if (app2.Equals("ICC_PROFILE"))
                            {
                                int order = byteapp2[12] & 0xff;
                                int count = byteapp2[13] & 0xff;
                                // some jpeg producers don't know how to count to 1
                                if (order < 1)
                                {
                                    order = 1;
                                }
                                if (count < 1)
                                {
                                    count = 1;
                                }
                                if (icc == null)
                                {
                                    icc = new byte[count][];
                                }
                                icc[order - 1] = byteapp2;
                            }
                        }
                        continue;
                    }
                    if (marker == M_APPD)
                    {
                        len = GetShort(jpegStream) - 2;
                        byte[] byteappd = new byte[len];
                        for (int k = 0; k < len; k++)
                        {
                            byteappd[k] = (byte)jpegStream.Read();
                        }
                        // search for '8BIM Resolution' marker
                        int k_1;
                        for (k_1 = 0; k_1 < len - PS_8BIM_RESO.Length; k_1++)
                        {
                            bool found = true;
                            for (int j = 0; j < PS_8BIM_RESO.Length; j++)
                            {
                                if (byteappd[k_1 + j] != PS_8BIM_RESO[j])
                                {
                                    found = false;
                                    break;
                                }
                            }
                            if (found)
                            {
                                break;
                            }
                        }
                        k_1 += PS_8BIM_RESO.Length;
                        if (k_1 < len - PS_8BIM_RESO.Length)
                        {
                            // "PASCAL String" for name, i.e. string prefix with length byte
                            // padded to be even length; 2 null bytes if empty
                            byte namelength = byteappd[k_1];
                            // add length byte
                            namelength++;
                            // add padding
                            if (namelength % 2 == 1)
                            {
                                namelength++;
                            }
                            // just skip name
                            k_1 += namelength;
                            // size of the resolution data
                            int resosize = (byteappd[k_1] << 24) + (byteappd[k_1 + 1] << 16) + (byteappd[k_1 + 2] << 8) + byteappd[k_1
                                                                                                                                   + 3];
                            // should be 16
                            if (resosize != 16)
                            {
                                // fail silently, for now
                                //System.err.println("DEBUG: unsupported resolution IRB size");
                                continue;
                            }
                            k_1 += 4;
                            int dx = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            k_1 += 2;
                            // skip 2 unknown bytes
                            k_1 += 2;
                            int unitsx = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            k_1 += 2;
                            // skip 2 unknown bytes
                            k_1 += 2;
                            int dy = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            k_1 += 2;
                            // skip 2 unknown bytes
                            k_1 += 2;
                            int unitsy = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            if (unitsx == 1 || unitsx == 2)
                            {
                                dx = (unitsx == 2 ? (int)(dx * 2.54f + 0.5f) : dx);
                                // make sure this is consistent with JFIF data
                                if (image.GetDpiX() != 0 && image.GetDpiX() != dx)
                                {
                                    ILog logger = LogManager.GetLogger(typeof(JpegImageHelper));
                                    logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiX: {0} vs {1})", image.GetDpiX(), dx));
                                }
                                else
                                {
                                    image.SetDpi(dx, image.GetDpiY());
                                }
                            }
                            if (unitsy == 1 || unitsy == 2)
                            {
                                dy = (unitsy == 2 ? (int)(dy * 2.54f + 0.5f) : dy);
                                // make sure this is consistent with JFIF data
                                if (image.GetDpiY() != 0 && image.GetDpiY() != dy)
                                {
                                    ILog logger = LogManager.GetLogger(typeof(JpegImageHelper));
                                    logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiY: {0} vs {1})", image.GetDpiY(), dy));
                                }
                                else
                                {
                                    image.SetDpi(image.GetDpiX(), dx);
                                }
                            }
                        }
                        continue;
                    }
                    firstPass = false;
                    int markertype = Marker(marker);
                    if (markertype == VALID_MARKER)
                    {
                        StreamUtil.Skip(jpegStream, 2);
                        if (jpegStream.Read() != 0x08)
                        {
                            throw new iText.IO.IOException(iText.IO.IOException._1MustHave8BitsPerComponent).SetMessageParams(errorID);
                        }
                        image.SetHeight(GetShort(jpegStream));
                        image.SetWidth(GetShort(jpegStream));
                        image.SetColorSpace(jpegStream.Read());
                        image.SetBpc(8);
                        break;
                    }
                    else
                    {
                        if (markertype == UNSUPPORTED_MARKER)
                        {
                            throw new iText.IO.IOException(iText.IO.IOException._1UnsupportedJpegMarker2).SetMessageParams(errorID, JavaUtil.IntegerToString
                                                                                                                               (marker));
                        }
                        else
                        {
                            if (markertype != NOPARAM_MARKER)
                            {
                                StreamUtil.Skip(jpegStream, GetShort(jpegStream) - 2);
                            }
                        }
                    }
                }
            }
            if (icc != null)
            {
                int total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    if (icc[k] == null)
                    {
                        icc = null;
                        return;
                    }
                    total += icc[k].Length - 14;
                }
                byte[] ficc = new byte[total];
                total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14);
                    total += icc[k].Length - 14;
                }
                try {
                    image.SetProfile(IccProfile.GetInstance(ficc, image.GetColorSpace()));
                }
                catch (ArgumentException) {
                }
            }
        }