コード例 #1
0
            public virtual void ConstructMatrix(PngImageData pngImageData)
            {
                PngChromaticities pngChromaticities = pngImageData.GetPngChromaticities();
                float             z = pngChromaticities.GetYW() * ((pngChromaticities.GetXG() - pngChromaticities.GetXB()) * pngChromaticities
                                                                   .GetYR() - (pngChromaticities.GetXR() - pngChromaticities.GetXB()) * pngChromaticities.GetYG() + (pngChromaticities
                                                                                                                                                                     .GetXR() - pngChromaticities.GetXG()) * pngChromaticities.GetYB());
                float YA = pngChromaticities.GetYR() * ((pngChromaticities.GetXG() - pngChromaticities.GetXB()) * pngChromaticities
                                                        .GetYW() - (pngChromaticities.GetXW() - pngChromaticities.GetXB()) * pngChromaticities.GetYG() + (pngChromaticities
                                                                                                                                                          .GetXW() - pngChromaticities.GetXG()) * pngChromaticities.GetYB()) / z;
                float XA = YA * pngChromaticities.GetXR() / pngChromaticities.GetYR();
                float ZA = YA * ((1 - pngChromaticities.GetXR()) / pngChromaticities.GetYR() - 1);
                float YB = -pngChromaticities.GetYG() * ((pngChromaticities.GetXR() - pngChromaticities.GetXB()) * pngChromaticities
                                                         .GetYW() - (pngChromaticities.GetXW() - pngChromaticities.GetXB()) * pngChromaticities.GetYR() + (pngChromaticities
                                                                                                                                                           .GetXW() - pngChromaticities.GetXR()) * pngChromaticities.GetYB()) / z;
                float XB = YB * pngChromaticities.GetXG() / pngChromaticities.GetYG();
                float ZB = YB * ((1 - pngChromaticities.GetXG()) / pngChromaticities.GetYG() - 1);
                float YC = pngChromaticities.GetYB() * ((pngChromaticities.GetXR() - pngChromaticities.GetXG()) * pngChromaticities
                                                        .GetYW() - (pngChromaticities.GetXW() - pngChromaticities.GetXG()) * pngChromaticities.GetYW() + (pngChromaticities
                                                                                                                                                          .GetXW() - pngChromaticities.GetXR()) * pngChromaticities.GetYG()) / z;
                float XC = YC * pngChromaticities.GetXB() / pngChromaticities.GetYB();
                float ZC = YC * ((1 - pngChromaticities.GetXB()) / pngChromaticities.GetYB() - 1);
                float XW = XA + XB + XC;
                float YW = 1;
                float ZW = ZA + ZB + ZC;

                float[] wpa = new float[3];
                wpa[0]  = XW;
                wpa[1]  = YW;
                wpa[2]  = ZW;
                this.wp = JavaUtil.ArraysCopyOf(wpa, 3);
                float[] matrix = new float[9];
                matrix[0]   = XA;
                matrix[1]   = YA;
                matrix[2]   = ZA;
                matrix[3]   = XB;
                matrix[4]   = YB;
                matrix[5]   = ZB;
                matrix[6]   = XC;
                matrix[7]   = YC;
                matrix[8]   = ZC;
                this.matrix = JavaUtil.ArraysCopyOf(matrix, 9);
            }
コード例 #2
0
 private static PdfObject GetColorSpaceInfo(PngImageData pngImageData)
 {
     if (pngImageData.GetProfile() != null)
     {
         if (pngImageData.IsGrayscaleImage())
         {
             return(PdfName.DeviceGray);
         }
         else
         {
             return(PdfName.DeviceRGB);
         }
     }
     if (pngImageData.GetGamma() == 1f && !pngImageData.IsHasCHRM())
     {
         if (pngImageData.IsGrayscaleImage())
         {
             return(PdfName.DeviceGray);
         }
         else
         {
             return(PdfName.DeviceRGB);
         }
     }
     else
     {
         PdfArray      array = new PdfArray();
         PdfDictionary map   = new PdfDictionary();
         if (pngImageData.IsGrayscaleImage())
         {
             if (pngImageData.GetGamma() == 1f)
             {
                 return(PdfName.DeviceGray);
             }
             array.Add(PdfName.CalGray);
             map.Put(PdfName.Gamma, new PdfNumber(pngImageData.GetGamma()));
             map.Put(PdfName.WhitePoint, new PdfArray(new int[] { 1, 1, 1 }));
         }
         else
         {
             float[] wp = new float[] { 1, 1, 1 };
             array.Add(PdfName.CalRGB);
             float gamma = pngImageData.GetGamma();
             if (gamma != 1f)
             {
                 float[] gm = new float[3];
                 gm[0] = gamma;
                 gm[1] = gamma;
                 gm[2] = gamma;
                 map.Put(PdfName.Gamma, new PdfArray(gm));
             }
             if (pngImageData.IsHasCHRM())
             {
                 PdfImageXObject.PngChromaticitiesHelper helper = new PdfImageXObject.PngChromaticitiesHelper();
                 helper.ConstructMatrix(pngImageData);
                 wp = helper.wp;
                 map.Put(PdfName.Matrix, new PdfArray(helper.matrix));
             }
             map.Put(PdfName.WhitePoint, new PdfArray(wp));
         }
         array.Add(map);
         return(array);
     }
 }
コード例 #3
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 && "JPXDecode".Equals(filter) && 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);
            }
            if (!(image is PngImageData))
            {
                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 = null;

            if (image is PngImageData)
            {
                PngImageData pngImage = (PngImageData)image;
                if (pngImage.IsIndexed())
                {
                    PdfArray colorspace = new PdfArray();
                    colorspace.Add(PdfName.Indexed);
                    colorspace.Add(GetColorSpaceInfo(pngImage));
                    if ((pngImage.GetColorPalette() != null) && (pngImage.GetColorPalette().Length > 0))
                    {
                        //Each palette entry is a three-byte series, so the number of entries is calculated as the length
                        //of the stream divided by 3. The number below specifies the maximum valid index value (starting from 0 up)
                        colorspace.Add(new PdfNumber(pngImage.GetColorPalette().Length / 3 - 1));
                    }
                    if (pngImage.GetColorPalette() != null)
                    {
                        colorspace.Add(new PdfString(PdfEncodings.ConvertToString(pngImage.GetColorPalette(), null)));
                    }
                    stream.Put(PdfName.ColorSpace, colorspace);
                }
                else
                {
                    stream.Put(PdfName.ColorSpace, GetColorSpaceInfo(pngImage));
                }
            }
            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);
        }