예제 #1
0
        private static PdfStream CreatePdfStream(ImageData image, iText.Kernel.Pdf.Xobject.PdfImageXObject imageMask
                                                 )
        {
            PdfStream stream;

            if (image.GetOriginalType() == ImageType.RAW)
            {
                RawImageHelper.UpdateImageAttributes((RawImageData)image, null);
            }
            stream = new PdfStream(image.GetData());
            String filter = image.GetFilter();

            if (filter != null && filter.Equals("JPXDecode") && image.GetColorSpace() <= 0)
            {
                stream.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
                image.SetBpc(0);
            }
            stream.Put(PdfName.Type, PdfName.XObject);
            stream.Put(PdfName.Subtype, PdfName.Image);
            PdfDictionary decodeParms = CreateDictionaryFromMap(stream, image.GetDecodeParms());

            if (decodeParms != null)
            {
                stream.Put(PdfName.DecodeParms, decodeParms);
            }
            PdfName colorSpace;

            switch (image.GetColorSpace())
            {
            case 1: {
                colorSpace = PdfName.DeviceGray;
                break;
            }

            case 3: {
                colorSpace = PdfName.DeviceRGB;
                break;
            }

            default: {
                colorSpace = PdfName.DeviceCMYK;
                break;
            }
            }
            stream.Put(PdfName.ColorSpace, colorSpace);
            if (image.GetBpc() != 0)
            {
                stream.Put(PdfName.BitsPerComponent, new PdfNumber(image.GetBpc()));
            }
            if (image.GetFilter() != null)
            {
                stream.Put(PdfName.Filter, new PdfName(image.GetFilter()));
            }
            //TODO: return to this later
            //        if (image.getLayer() != null)
            //            put(PdfName.OC, image.getLayer().getRef());
            if (image.GetColorSpace() == -1)
            {
                stream.Remove(PdfName.ColorSpace);
            }
            PdfDictionary additional = CreateDictionaryFromMap(stream, image.GetImageAttributes());

            if (additional != null)
            {
                stream.PutAll(additional);
            }
            IccProfile iccProfile = image.GetProfile();

            if (iccProfile != null)
            {
                PdfStream iccProfileStream   = PdfCieBasedCs.IccBased.GetIccProfileStream(iccProfile);
                PdfArray  iccBasedColorSpace = new PdfArray();
                iccBasedColorSpace.Add(PdfName.ICCBased);
                iccBasedColorSpace.Add(iccProfileStream);
                PdfObject colorSpaceObject          = stream.Get(PdfName.ColorSpace);
                bool      iccProfileShouldBeApplied = true;
                if (colorSpaceObject != null)
                {
                    PdfColorSpace cs = PdfColorSpace.MakeColorSpace(colorSpaceObject);
                    if (cs == null)
                    {
                        LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_COLOR_SPACE_OVERRIDDEN_BY_ICC_PROFILE
                                                                                                     );
                    }
                    else
                    {
                        if (cs is PdfSpecialCs.Indexed)
                        {
                            PdfColorSpace baseCs = ((PdfSpecialCs.Indexed)cs).GetBaseCs();
                            if (baseCs == null)
                            {
                                LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_BASE_COLOR_SPACE_IN_INDEXED_COLOR_SPACE_OVERRIDDEN_BY_ICC_PROFILE
                                                                                                             );
                            }
                            else
                            {
                                if (baseCs.GetNumberOfComponents() != iccProfile.GetNumComponents())
                                {
                                    LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS_COMPARED_TO_BASE_COLOR_SPACE_IN_INDEXED_COLOR_SPACE
                                                                                                                 );
                                    iccProfileShouldBeApplied = false;
                                }
                                else
                                {
                                    iccProfileStream.Put(PdfName.Alternate, baseCs.GetPdfObject());
                                }
                            }
                            if (iccProfileShouldBeApplied)
                            {
                                ((PdfArray)colorSpaceObject).Set(1, iccBasedColorSpace);
                                iccProfileShouldBeApplied = false;
                            }
                        }
                        else
                        {
                            if (cs.GetNumberOfComponents() != iccProfile.GetNumComponents())
                            {
                                LogManager.GetLogger(typeof(iText.Kernel.Pdf.Xobject.PdfImageXObject)).Error(iText.IO.LogMessageConstant.IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS_COMPARED_TO_COLOR_SPACE
                                                                                                             );
                                iccProfileShouldBeApplied = false;
                            }
                            else
                            {
                                iccProfileStream.Put(PdfName.Alternate, colorSpaceObject);
                            }
                        }
                    }
                }
                if (iccProfileShouldBeApplied)
                {
                    stream.Put(PdfName.ColorSpace, iccBasedColorSpace);
                }
            }
            if (image.IsMask() && (image.GetBpc() == 1 || image.GetBpc() > 0xff))
            {
                stream.Put(PdfName.ImageMask, PdfBoolean.TRUE);
            }
            if (imageMask != null)
            {
                if (imageMask.softMask)
                {
                    stream.Put(PdfName.SMask, imageMask.GetPdfObject());
                }
                else
                {
                    if (imageMask.mask)
                    {
                        stream.Put(PdfName.Mask, imageMask.GetPdfObject());
                    }
                }
            }
            ImageData mask = image.GetImageMask();

            if (mask != null)
            {
                if (mask.IsSoftMask())
                {
                    stream.Put(PdfName.SMask, new iText.Kernel.Pdf.Xobject.PdfImageXObject(image.GetImageMask()).GetPdfObject(
                                   ));
                }
                else
                {
                    if (mask.IsMask())
                    {
                        stream.Put(PdfName.Mask, new iText.Kernel.Pdf.Xobject.PdfImageXObject(image.GetImageMask()).GetPdfObject()
                                   );
                    }
                }
            }
            if (image.GetDecode() != null)
            {
                stream.Put(PdfName.Decode, new PdfArray(image.GetDecode()));
            }
            if (image.IsMask() && image.IsInverted())
            {
                stream.Put(PdfName.Decode, new PdfArray(new float[] { 1, 0 }));
            }
            if (image.IsInterpolation())
            {
                stream.Put(PdfName.Interpolate, PdfBoolean.TRUE);
            }
            // deal with transparency
            int[] transparency = image.GetTransparency();
            if (transparency != null && !image.IsMask() && imageMask == null)
            {
                PdfArray t = new PdfArray();
                foreach (int transparencyItem in transparency)
                {
                    t.Add(new PdfNumber(transparencyItem));
                }
                stream.Put(PdfName.Mask, t);
            }
            stream.Put(PdfName.Width, new PdfNumber(image.GetWidth()));
            stream.Put(PdfName.Height, new PdfNumber(image.GetHeight()));
            return(stream);
        }
예제 #2
0
        protected internal static PdfStream CreatePdfStream(ImageData image, iText.Kernel.Pdf.Xobject.PdfImageXObject
                                                            imageMask)
        {
            PdfStream stream;

            if (image.GetOriginalType() == ImageType.RAW)
            {
                RawImageHelper.UpdateImageAttributes((RawImageData)image, null);
            }
            stream = new PdfStream(image.GetData());
            String filter = image.GetFilter();

            if (filter != null && filter.Equals("JPXDecode") && image.GetColorSpace() <= 0)
            {
                stream.SetCompressionLevel(CompressionConstants.NO_COMPRESSION);
                image.SetBpc(0);
            }
            stream.Put(PdfName.Type, PdfName.XObject);
            stream.Put(PdfName.Subtype, PdfName.Image);
            PdfDictionary decodeParms = CreateDictionaryFromMap(stream, image.GetDecodeParms());

            if (decodeParms != null)
            {
                stream.Put(PdfName.DecodeParms, decodeParms);
            }
            PdfName colorSpace;

            switch (image.GetColorSpace())
            {
            case 1: {
                colorSpace = PdfName.DeviceGray;
                break;
            }

            case 3: {
                colorSpace = PdfName.DeviceRGB;
                break;
            }

            default: {
                colorSpace = PdfName.DeviceCMYK;
                break;
            }
            }
            stream.Put(PdfName.ColorSpace, colorSpace);
            if (image.GetBpc() != 0)
            {
                stream.Put(PdfName.BitsPerComponent, new PdfNumber(image.GetBpc()));
            }
            if (image.GetFilter() != null)
            {
                stream.Put(PdfName.Filter, new PdfName(image.GetFilter()));
            }
            //TODO: return to this later
            //        if (image.getLayer() != null)
            //            put(PdfName.OC, image.getLayer().getRef());
            if (image.GetColorSpace() == -1)
            {
                stream.Remove(PdfName.ColorSpace);
            }
            PdfDictionary additional = CreateDictionaryFromMap(stream, image.GetImageAttributes());

            if (additional != null)
            {
                stream.PutAll(additional);
            }
            if (image.IsMask() && (image.GetBpc() == 1 || image.GetBpc() > 0xff))
            {
                stream.Put(PdfName.ImageMask, PdfBoolean.TRUE);
            }
            if (imageMask != null)
            {
                if (imageMask.softMask)
                {
                    stream.Put(PdfName.SMask, imageMask.GetPdfObject());
                }
                else
                {
                    if (imageMask.mask)
                    {
                        stream.Put(PdfName.Mask, imageMask.GetPdfObject());
                    }
                }
            }
            ImageData mask = image.GetImageMask();

            if (mask != null)
            {
                if (mask.IsSoftMask())
                {
                    stream.Put(PdfName.SMask, new iText.Kernel.Pdf.Xobject.PdfImageXObject(image.GetImageMask()).GetPdfObject(
                                   ));
                }
                else
                {
                    if (mask.IsMask())
                    {
                        stream.Put(PdfName.Mask, new iText.Kernel.Pdf.Xobject.PdfImageXObject(image.GetImageMask()).GetPdfObject()
                                   );
                    }
                }
            }
            if (image.GetDecode() != null)
            {
                stream.Put(PdfName.Decode, new PdfArray(image.GetDecode()));
            }
            if (image.IsMask() && image.IsInverted())
            {
                stream.Put(PdfName.Decode, new PdfArray(new float[] { 1, 0 }));
            }
            if (image.IsInterpolation())
            {
                stream.Put(PdfName.Interpolate, PdfBoolean.TRUE);
            }
            // deal with transparency
            int[] transparency = image.GetTransparency();
            if (transparency != null && !image.IsMask() && imageMask == null)
            {
                PdfArray t = new PdfArray();
                foreach (int transparencyItem in transparency)
                {
                    t.Add(new PdfNumber(transparencyItem));
                }
                stream.Put(PdfName.Mask, t);
            }
            stream.Put(PdfName.Width, new PdfNumber(image.GetWidth()));
            stream.Put(PdfName.Height, new PdfNumber(image.GetHeight()));
            return(stream);
        }