Beispiel #1
0
 private static void UpdateAttributes(ImageData image)
 {
     image.filter = "DCTDecode";
     if (image.GetColorTransform() == 0)
     {
         IDictionary <String, Object> decodeParms = new Dictionary <String, Object>();
         decodeParms.Put("ColorTransform", 0);
         image.decodeParms = decodeParms;
     }
     if (image.GetColorSpace() != 1 && image.GetColorSpace() != 3 && image.IsInverted())
     {
         image.decode = new float[] { 1, 0, 1, 0, 1, 0, 1, 0 };
     }
 }
Beispiel #2
0
        public virtual void Graya8BpcAddColorToAlphaImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "graya8BpcAddColorToAlpha.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(100, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(100, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
            NUnit.Framework.Assert.AreEqual(1, img.GetColorSpace());
        }
Beispiel #3
0
        public virtual void Rgb8BpcDepthImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "rgb8Bpc.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(100, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(100, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
            NUnit.Framework.Assert.AreEqual(3, img.GetColorSpace());
        }
Beispiel #4
0
        public virtual void Grayscale16BpcDepthImageTest()
        {
            // iText explicitly processes 16bit images as 8bit
            ImageData img = ImageDataFactory.Create(sourceFolder + "grayscale16Bpc.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(100, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(100, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
            NUnit.Framework.Assert.AreEqual(1, img.GetColorSpace());
            NUnit.Framework.Assert.AreEqual(0, ((PngImageData)img).GetColorType());
        }
Beispiel #5
0
        public virtual void Indexed2BpcWithAlphaChannelTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "indexed2BpcWithAlphaChannel.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(346, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(49, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(2, img.GetBpc());
            //Indexed colorspace contains one component indeed
            NUnit.Framework.Assert.AreEqual(1, img.GetColorSpace());
            NUnit.Framework.Assert.AreEqual(3, ((PngImageData)img).GetColorType());
        }
Beispiel #6
0
        public virtual void RgbWithoutSaveColorProfileImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "rgbWithoutSaveColorProfile.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(100, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(100, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
            NUnit.Framework.Assert.AreEqual(3, img.GetColorSpace());
            NUnit.Framework.Assert.AreEqual(2, ((PngImageData)img).GetColorType());
            NUnit.Framework.Assert.IsNull(img.GetProfile());
        }
Beispiel #7
0
        public virtual void GrayscaleSimpleTransparencyImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "grayscaleSimpleTransparencyImage.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(200, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(200, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
            NUnit.Framework.Assert.AreEqual(1, img.GetColorSpace());
            NUnit.Framework.Assert.AreEqual(0, ((PngImageData)img).GetColorType());
            NUnit.Framework.Assert.IsNotNull(img.GetImageAttributes());
            NUnit.Framework.Assert.AreEqual("[0 0]", img.GetImageAttributes().Get(PngImageHelperConstants.MASK));
        }
Beispiel #8
0
        public virtual void Graya8BpcDepthImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "graya8Bpc.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(100, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(100, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
            NUnit.Framework.Assert.AreEqual(1, img.GetColorSpace());
            NUnit.Framework.Assert.AreEqual(4, ((PngImageData)img).GetColorType());
            NUnit.Framework.Assert.IsNotNull(img.GetImageMask());
            NUnit.Framework.Assert.AreEqual(1, img.GetImageMask().GetColorSpace());
            NUnit.Framework.Assert.AreEqual(8, img.GetImageMask().GetBpc());
        }
Beispiel #9
0
        public virtual void IndexedAddColorToAlphaImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "indexedAddColorToAlpha.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(346, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(49, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(2, img.GetBpc());
            //Indexed colorspace contains one component indeed
            NUnit.Framework.Assert.AreEqual(1, img.GetColorSpace());
            NUnit.Framework.Assert.AreEqual(3, ((PngImageData)img).GetColorType());
            NUnit.Framework.Assert.IsNotNull(img.GetImageAttributes());
            NUnit.Framework.Assert.AreEqual(0, ((int[])img.GetImageAttributes().Get(PngImageHelperConstants.MASK))[0]);
            NUnit.Framework.Assert.AreEqual(0, ((int[])img.GetImageAttributes().Get(PngImageHelperConstants.MASK))[1]);
        }
Beispiel #10
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) {
                }
            }
        }