Beispiel #1
0
        public TPdfImage(TImage aImage, int aId, Stream aImageData, long transparentColor, bool defaultToJpg)
        {
            Id                = aId;
            FMask             = null;
            FSMask            = null;
            FTransparentColor = transparentColor;

            if (aImageData != null && TPdfPng.IsOkPng(aImageData))
            {
                aImageData.Position = 0;
                ReadPng(aImageData);
            }
            else
            {
                TImage NewImage = aImage;
                try
                {
                    if (aImage == null && aImageData != null)
                    {
                        NewImage = TImage.FromStream(aImageData);
                    }


                    //Convert to PNG / JPEG
                    if (defaultToJpg || NewImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                    {
                        ReadJpeg(NewImage, aImageData);
                    }
                    else
                    {
                        ReadPng(NewImage);
                    }
                }
                finally
                {
                    if (NewImage != null && NewImage != aImage)
                    {
                        NewImage.Dispose();
                    }
                }
            }
        }
Beispiel #2
0
        private void ReadPng(Stream Ms)
        {
            using (MemoryStream OutMs = new MemoryStream())
            {
                TPdfPngData ImgParsedData = new TPdfPngData(OutMs);
                TPdfPng.ProcessPng(Ms, ImgParsedData);

                FImage            = OutMs.ToArray();
                FImageWidth       = (int)ImgParsedData.Width;
                FImageHeight      = (int)ImgParsedData.Height;
                FBitsPerComponent = ImgParsedData.BitDepth;

                if (FBitsPerComponent == 16)
                {
                    PdfMessages.ThrowException(PdfErr.ErrInvalidPngImage);
                }

                int Colors = 1;
                switch (ImgParsedData.ColorType)
                {
                case 0:                         //GrayScale
                    FColorSpace = TPdfTokens.GetString(TPdfToken.DeviceGrayName);
                    if (ImgParsedData.tRNS != null && ImgParsedData.tRNS.Length >= 2)
                    {
                        FMask =
                            TPdfTokens.GetString(TPdfToken.OpenArray) +
                            String.Format(CultureInfo.InvariantCulture, "{0} {0}", (ImgParsedData.tRNS[0] << 8) + ImgParsedData.tRNS[1]) +
                            TPdfTokens.GetString(TPdfToken.CloseArray);
                    }

                    break;

                case 2:                          //TrueColor
                    FColorSpace = TPdfTokens.GetString(TPdfToken.DeviceRGBName);
                    Colors      = 3;
                    if (ImgParsedData.tRNS != null && ImgParsedData.tRNS.Length >= 6)
                    {
                        FMask =
                            TPdfTokens.GetString(TPdfToken.OpenArray) +
                            String.Format(CultureInfo.InvariantCulture,
                                          "{0} {0} {1} {1} {2} {2}", (ImgParsedData.tRNS[0] << 8) + ImgParsedData.tRNS[1],
                                          (ImgParsedData.tRNS[2] << 8) + ImgParsedData.tRNS[3],
                                          (ImgParsedData.tRNS[4] << 8) + ImgParsedData.tRNS[5]
                                          ) +
                            TPdfTokens.GetString(TPdfToken.CloseArray);
                    }
                    break;

                case 3:                         //Indexed Color
                    FColorSpace = GetPalette(ImgParsedData.PLTE);
                    if (ImgParsedData.tRNS != null && ImgParsedData.tRNS.Length > 0)
                    {
                        ImgParsedData.SMask = TPdfPng.GetIndexedSMask(FImage, FImageWidth, FImageHeight, ImgParsedData.tRNS, FBitsPerComponent);
                    }
                    break;

                case 4:                          //GrayScale + Alpha
                    FColorSpace = TPdfTokens.GetString(TPdfToken.DeviceGrayName);
                    break;

                case 6:                         //TrueColor + Alpha
                    FColorSpace = TPdfTokens.GetString(TPdfToken.DeviceRGBName);
                    Colors      = 3;
                    break;

                default: PdfMessages.ThrowException(PdfErr.ErrInvalidPngImage); break;
                }

                FSMask     = ImgParsedData.SMask;
                OneBitMask = ImgParsedData.OneBitMask;

                if (ImgParsedData.InterlaceMethod != 0)
                {
                    PdfMessages.ThrowException(PdfErr.ErrInvalidPngImage);
                }

                FFilterName = TPdfTokens.GetString(TPdfToken.FlateDecodeName);

                FDecodeParmsName =
                    TPdfTokens.GetString(TPdfToken.StartDictionary) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.PredictorName) + " {0} ", 15) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.ColorsName) + " {0} ", Colors) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.BitsPerComponentName) + " {0} ", FBitsPerComponent) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.ColumnsName) + " {0} ", FImageWidth) +
                    TPdfTokens.GetString(TPdfToken.EndDictionary);

                FDecodeParmsSMask =
                    TPdfTokens.GetString(TPdfToken.StartDictionary) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.PredictorName) + " {0} ", 15) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.ColorsName) + " {0} ", 1) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.BitsPerComponentName) + " {0} ", 8) +
                    String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.ColumnsName) + " {0} ", FImageWidth) +
                    TPdfTokens.GetString(TPdfToken.EndDictionary);
            }
        }