/// <summary>
        /// Changes the rendering intent of the color profile to the specified <see cref="RenderingIntent" /> and saves the profile to a new file.
        /// </summary>
        /// <param name="profilePath">The path of the color profile.</param>
        /// <param name="renderingIntent">The rendering intent to use for the color profile.</param>
        /// <param name="newProfileFileName">The path of the color profile with the new rendering intent.</param>
        /// <returns><c>true</c> if the rendering intent was changed; otherwise, <c>false</c>.</returns>
        /// <exception cref="LCMSException">
        /// The color profile could not be opened.
        /// -or-
        /// An error occurred saving the profile with the new rendering intent.
        /// </exception>
        internal static bool ChangeProfileRenderingIntent(string profilePath, RenderingIntent renderingIntent, out string newProfileFileName)
        {
            newProfileFileName = null;
            bool renderingIntentChanged = false;

            using (LCMSProfileHandle profile = LCMSHelper.OpenColorProfile(profilePath))
            {
                if (profile.IsInvalid)
                {
                    throw new LCMSException(Resources.OpenDestiniationProfileError);
                }

                RenderingIntent profileRenderingIntent = LCMSHelper.GetProfileRenderingIntent(profile);

                if (profileRenderingIntent != renderingIntent)
                {
                    LCMSHelper.SetProfileRenderingIntent(profile, renderingIntent);

                    newProfileFileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                    if (!LCMSHelper.SaveColorProfile(profile, newProfileFileName))
                    {
                        throw new LCMSException(Resources.ChangeRenderingIntentError);
                    }

                    renderingIntentChanged = true;
                }
            }

            return(renderingIntentChanged);
        }
예제 #2
0
 internal static LCMSEnums.ConvertProfileStatus ConvertToProfile(
     LCMSProfileHandle inputProfile,
     LCMSProfileHandle outputProfile,
     RenderingIntent renderingIntent,
     LCMSEnums.TransformFlags flags,
     ref LCMSStructs.BitmapData input,
     ref LCMSStructs.BitmapData output
     )
 {
     if (IntPtr.Size == 8)
     {
         return(LCMS_64.ConvertToProfile(
                    inputProfile,
                    outputProfile,
                    renderingIntent,
                    flags,
                    ref input,
                    ref output
                    ));
     }
     else
     {
         return(LCMS_86.ConvertToProfile(
                    inputProfile,
                    outputProfile,
                    renderingIntent,
                    flags,
                    ref input,
                    ref output
                    ));
     }
 }
예제 #3
0
        /// <summary>
        /// Create a new <see cref="InlineImage"/>.
        /// </summary>
        internal InlineImage(PdfRectangle bounds, int widthInSamples, int heightInSamples, int bitsPerComponent, bool isImageMask,
                             RenderingIntent renderingIntent,
                             bool interpolate,
                             ColorSpace?colorSpace,
                             IReadOnlyList <decimal> decode,
                             IReadOnlyList <byte> bytes,
                             IReadOnlyList <IFilter> filters,
                             DictionaryToken streamDictionary)
        {
            Bounds           = bounds;
            WidthInSamples   = widthInSamples;
            HeightInSamples  = heightInSamples;
            ColorSpace       = colorSpace;
            Decode           = decode;
            BitsPerComponent = bitsPerComponent;
            IsImageMask      = isImageMask;
            RenderingIntent  = renderingIntent;
            Interpolate      = interpolate;

            RawBytes     = bytes;
            bytesFactory = new Lazy <IReadOnlyList <byte> >(() =>
            {
                var b = bytes.ToArray();
                for (var i = 0; i < filters.Count; i++)
                {
                    var filter = filters[i];
                    b          = filter.Decode(b, streamDictionary, i);
                }

                return(b);
            });
        }
예제 #4
0
        internal static bool UseBlackPointCompensation(bool blackPointCompensation, RenderingIntent intent)
        {
            // Black point compensation should not be used when rendering in Absolute Colorimetric according to
            // https://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/AdobeBPC.pdf

            return(blackPointCompensation && intent != RenderingIntent.AbsoluteColorimetric);
        }
예제 #5
0
 /// <summary>
 /// Creates a new <see cref="XObjectImage"/>.
 /// </summary>
 internal XObjectImage(PdfRectangle bounds, int widthInSamples, int heightInSamples, int bitsPerComponent,
                       ColorSpace?colorSpace,
                       bool isJpxEncoded,
                       bool isImageMask,
                       RenderingIntent renderingIntent,
                       bool interpolate,
                       IReadOnlyList <decimal> decode,
                       DictionaryToken imageDictionary,
                       IReadOnlyList <byte> rawBytes,
                       Lazy <IReadOnlyList <byte> > bytes)
 {
     Bounds           = bounds;
     WidthInSamples   = widthInSamples;
     HeightInSamples  = heightInSamples;
     BitsPerComponent = bitsPerComponent;
     ColorSpace       = colorSpace;
     IsJpxEncoded     = isJpxEncoded;
     IsImageMask      = isImageMask;
     RenderingIntent  = renderingIntent;
     Interpolate      = interpolate;
     Decode           = decode;
     ImageDictionary  = imageDictionary ?? throw new ArgumentNullException(nameof(imageDictionary));
     RawBytes         = rawBytes;
     bytesFactory     = bytes;
 }
예제 #6
0
 internal static extern LCMSTransformHandleX64 CreateProofingTransformBGRA8(
     LCMSProfileHandle input,
     LCMSProfileHandle display,
     RenderingIntent displayIntent,
     LCMSProfileHandle proofing,
     RenderingIntent proofingIntent,
     LCMSEnums.TransformFlags flags
     );
예제 #7
0
 internal static extern LCMSEnums.ConvertProfileStatus ConvertToProfile(
     LCMSProfileHandle inputProfile,
     LCMSProfileHandle outputProfile,
     RenderingIntent renderingIntent,
     LCMSEnums.TransformFlags flags,
     [In()] ref LCMSStructs.BitmapData input,
     [In()] ref LCMSStructs.BitmapData output
     );
예제 #8
0
 public XObjectContentRecord(XObjectType type, StreamToken stream, TransformationMatrix appliedTransformation,
                             RenderingIntent defaultRenderingIntent)
 {
     Type   = type;
     Stream = stream ?? throw new ArgumentNullException(nameof(stream));
     AppliedTransformation  = appliedTransformation;
     DefaultRenderingIntent = defaultRenderingIntent;
 }
예제 #9
0
 /// <summary>
 /// Converts the PCS color into the device color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="pcs">The PCS color (has to match the profiles PCS color type)</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <returns>The converted color in the device color type</returns>
 public Color ToDevice(ICC Profile, Color pcs, RenderingIntent PrefRenderingIntent)
 {
     InValues = pcs.ColorArray;
     InModel = pcs.Model;
     this.Profile = Profile;
     PreferredRenderingIntent = PrefRenderingIntent;
     IsDefault = true;
     return Do_PCS();
 }
예제 #10
0
 /// <summary>
 /// Converts the device color into the PCS color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="inColor">The device color (has to match the profiles device color type)</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <returns>The converted color in the PCS color type</returns>
 public Color ToPCS(ICC Profile, Color inColor, RenderingIntent PrefRenderingIntent)
 {
     InValues                 = inColor.ColorArray;
     InModel                  = inColor.Model;
     this.Profile             = Profile;
     PreferredRenderingIntent = PrefRenderingIntent;
     IsDefault                = true;
     return(Do_Device());
 }
예제 #11
0
 /// <summary>
 /// Converts the PCS color into the device color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="pcs">The PCS color (has to match the profiles PCS color type)</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <returns>The converted color in the device color type</returns>
 public Color ToDevice(ICC Profile, Color pcs, RenderingIntent PrefRenderingIntent)
 {
     InValues                 = pcs.ColorArray;
     InModel                  = pcs.Model;
     this.Profile             = Profile;
     PreferredRenderingIntent = PrefRenderingIntent;
     IsDefault                = true;
     return(Do_PCS());
 }
예제 #12
0
 internal static void SetProfileRenderingIntent(LCMSProfileHandle hProfile, RenderingIntent newRenderingIntent)
 {
     if (IntPtr.Size == 8)
     {
         LCMS_64.SetProfileRenderingIntent(hProfile, newRenderingIntent);
     }
     else
     {
         LCMS_86.SetProfileRenderingIntent(hProfile, newRenderingIntent);
     }
 }
예제 #13
0
 /// <summary>
 /// Converts the PCS color into the device color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="pcs">The PCS color (has to match the profiles PCS color type)</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <param name="ConversionMethod">The method of conversion</param>
 /// <param name="ConversionType">The type of conversion</param>
 /// <returns>The converted color in the device color type</returns>
 public Color ToDevice(ICC Profile, Color pcs, RenderingIntent PrefRenderingIntent, ICCconversionMethod ConversionMethod, ICCconversionType ConversionType)
 {
     InValues                 = pcs.ColorArray;
     InModel                  = pcs.Model;
     this.Profile             = Profile;
     PreferredRenderingIntent = PrefRenderingIntent;
     this.ConversionMethod    = ConversionMethod;
     this.ConversionType      = ConversionType;
     IsDefault                = false;
     return(Do_PCS());
 }
예제 #14
0
 /// <summary>
 /// Converts the device color into the PCS color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="inColor">The device color (has to match the profiles device color type)</param>
 /// <param name="ConversionMethod">The method of conversion</param>
 /// <param name="ConversionType">The type of conversion</param>
 /// <returns>The converted color in the PCS color type</returns>
 public Color ToPCS(ICC Profile, Color inColor, ICCconversionMethod ConversionMethod, ICCconversionType ConversionType)
 {
     InValues                 = inColor.ColorArray;
     InModel                  = inColor.Model;
     this.Profile             = Profile;
     PreferredRenderingIntent = ColorConverter.PreferredRenderingIntent;
     this.ConversionMethod    = ConversionMethod;
     this.ConversionType      = ConversionType;
     IsDefault                = false;
     return(Do_Device());
 }
예제 #15
0
        internal static LCMSTransformHandle CreateProofingTransformBGRA8(
            LCMSProfileHandle input,
            LCMSProfileHandle display,
            RenderingIntent displayIntent,
            LCMSProfileHandle proofing,
            RenderingIntent proofingIntent,
            bool checkGamut,
            Color gamutWarningColor,
            bool blackPointCompensation
            )
        {
            LCMSEnums.TransformFlags flags = LCMSEnums.TransformFlags.None;

            if (checkGamut)
            {
                flags |= LCMSEnums.TransformFlags.GamutCheck;
                SetGamutWarningColor(gamutWarningColor);
            }
            else
            {
                flags |= LCMSEnums.TransformFlags.SoftProofing;
            }

            if (UseBlackPointCompensation(blackPointCompensation, proofingIntent))
            {
                flags |= LCMSEnums.TransformFlags.BlackPointCompensation;
            }

            if (IntPtr.Size == 8)
            {
                return(LCMS_64.CreateProofingTransformBGRA8(
                           input,
                           display,
                           displayIntent,
                           proofing,
                           proofingIntent,
                           flags
                           ));
            }
            else
            {
                return(LCMS_86.CreateProofingTransformBGRA8(
                           input,
                           display,
                           displayIntent,
                           proofing,
                           proofingIntent,
                           flags
                           ));
            }
        }
예제 #16
0
        /// <summary>
        /// Create a new <see cref="InlineImage"/>.
        /// </summary>
        internal InlineImage(PdfRectangle bounds, int widthInSamples, int heightInSamples, int bitsPerComponent, bool isImageMask,
                             RenderingIntent renderingIntent,
                             bool interpolate,
                             ColorSpace?colorSpace,
                             IReadOnlyList <decimal> decode,
                             IReadOnlyList <byte> bytes,
                             IReadOnlyList <IFilter> filters,
                             DictionaryToken streamDictionary,
                             ColorSpaceDetails colorSpaceDetails)
        {
            Bounds           = bounds;
            WidthInSamples   = widthInSamples;
            HeightInSamples  = heightInSamples;
            ColorSpace       = colorSpace;
            Decode           = decode;
            BitsPerComponent = bitsPerComponent;
            IsImageMask      = isImageMask;
            RenderingIntent  = renderingIntent;
            Interpolate      = interpolate;
            ImageDictionary  = streamDictionary;

            RawBytes          = bytes;
            ColorSpaceDetails = colorSpaceDetails;

            var supportsFilters = true;

            foreach (var filter in filters)
            {
                if (!filter.IsSupported)
                {
                    supportsFilters = false;
                    break;
                }
            }

            bytesFactory = supportsFilters ? new Lazy <IReadOnlyList <byte> >(() =>
            {
                var b = bytes.ToArray();
                for (var i = 0; i < filters.Count; i++)
                {
                    var filter = filters[i];
                    b          = filter.Decode(b, streamDictionary, i);
                }

                return(b);
            }) : null;
        }
예제 #17
0
 public SoftProofingConfigToken(
     ColorProfileInfo inputProfile,
     ColorProfileInfo monitorProfile,
     RenderingIntent displayIntent,
     ColorProfileInfoCollection proofingProfiles,
     int proofingProfileIndex,
     RenderingIntent proofingIntent,
     bool showGamutWarning,
     Color gamutWarningColor,
     bool blackPointCompensation
     )
 {
     this.InputColorProfile      = inputProfile;
     this.DisplayColorProfile    = monitorProfile;
     this.DisplayIntent          = displayIntent;
     this.ProofingColorProfiles  = proofingProfiles;
     this.ProofingProfileIndex   = proofingProfileIndex;
     this.ProofingIntent         = proofingIntent;
     this.ShowGamutWarning       = showGamutWarning;
     this.GamutWarningColor      = gamutWarningColor;
     this.BlackPointCompensation = blackPointCompensation;
 }
예제 #18
0
 /// <summary>
 /// Converts a color to the ICC color
 /// </summary>
 /// <param name="InColor">The color to convert</param>
 /// <param name="profile">The icc file to be used for the conversion</param>
 /// <param name="ConversionType">The type of conversion</param>
 /// <param name="ConversionMethod">The method of conversion</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <returns>The converted color</returns>
 public Color ToICC(Color InColor, ICC profile, RenderingIntent PrefRenderingIntent, ICCconversionType ConversionType, ICCconversionMethod ConversionMethod)
 {
     if (ICC_Converter.IsSameSpace(InColor.Model, profile.Header.DataColorspace)) { return ICCconverter.ToPCS(profile, InColor, PrefRenderingIntent, ConversionMethod, ConversionType); }
     else if (ICC_Converter.IsSameSpace(InColor.Model, profile.Header.PCS)) { return ICCconverter.ToDevice(profile, InColor, PrefRenderingIntent, ConversionMethod, ConversionType); }
     else { return ToICC(ToICC_PCS(InColor, profile), profile, PrefRenderingIntent, ConversionType, ConversionMethod); }
 }
예제 #19
0
 internal static extern void SetProfileRenderingIntent(LCMSProfileHandle hProfile, RenderingIntent intent);
예제 #20
0
 public sRGBChunk(PNGImage image)
     : base("sRGB", image)
 {
     _ri = RenderingIntent.Perceptual;
 }
        private bool SaveImage()
        {
            bool             result  = false;
            ColorProfileInfo profile = this.proofingProfileIndex >= 0 ? this.proofingColorProfiles[this.proofingProfileIndex] : this.inputProfile;

            if (CanSaveInColorSpace(profile.ColorSpace))
            {
                try
                {
                    if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        bool tiff = SaveFormatIsTIFF(this.saveFileDialog1.FileName);

                        using (SaveOptionsDialog optionsDialog = new SaveOptionsDialog(tiff, profile))
                        {
                            optionsDialog.BackColor = BackColor;
                            optionsDialog.ForeColor = ForeColor;

                            if (optionsDialog.ShowDialog(this) == DialogResult.OK)
                            {
                                SaveOptions options = optionsDialog.Options;

                                BitmapSource source = null;
                                Uri          destinationProfileUri = null;

                                if (profile.Equals(this.inputProfile))
                                {
                                    // No conversion is necessary when saving as the input color profile.
                                    source = GetBitmapSourceFromEffectSourceSurface(tiff, options);
                                    destinationProfileUri = new Uri(this.inputProfile.Path, UriKind.Absolute);
                                }
                                else
                                {
                                    // Convert the image to the destination color profile.
                                    PixelFormat     destinationPixelFormat = GetDestiniationPixelFormat(tiff, profile.ColorSpace);
                                    WriteableBitmap writable = new WriteableBitmap(
                                        this.EffectSourceSurface.Width,
                                        this.EffectSourceSurface.Height,
                                        options.HorizontalResolution,
                                        options.VerticalResolution,
                                        destinationPixelFormat,
                                        null
                                        );

                                    RenderingIntent destinationRenderingIntent = (RenderingIntent)this.proofingIntentCombo.SelectedIndex;

                                    ColorProfileHelper.ConvertToColorProfile(
                                        this.inputProfile.Path,
                                        profile.Path,
                                        destinationRenderingIntent,
                                        this.blackPointCheckBox.Checked,
                                        this.EffectSourceSurface,
                                        writable
                                        );

                                    source = writable;

                                    // Write a new profile with the selected rendering intent, if necessary.
                                    if (ColorProfileHelper.ChangeProfileRenderingIntent(profile.Path, destinationRenderingIntent, out this.destinationProfileTempPath))
                                    {
                                        destinationProfileUri = new Uri(this.destinationProfileTempPath, UriKind.Absolute);
                                    }
                                    else
                                    {
                                        destinationProfileUri = new Uri(profile.Path, UriKind.Absolute);
                                    }
                                }

                                BitmapEncoder encoder;
                                if (tiff)
                                {
                                    TIFFSaveOptions tiffOptions = (TIFFSaveOptions)options;
                                    encoder = new TiffBitmapEncoder()
                                    {
                                        Compression = tiffOptions.LZWCompression ? TiffCompressOption.Lzw : TiffCompressOption.None
                                    };
                                }
                                else
                                {
                                    JPEGSaveOptions jpegOptions = (JPEGSaveOptions)options;
                                    encoder = new JpegBitmapEncoder()
                                    {
                                        QualityLevel = jpegOptions.Quality,
                                    };
                                }

                                ColorContext[] colorContexts = new ColorContext[] { new ColorContext(destinationProfileUri) };

                                encoder.Frames.Add(BitmapFrame.Create(source, null, null, Array.AsReadOnly(colorContexts)));

                                using (FileStream stream = new FileStream(this.saveFileDialog1.FileName, FileMode.Create, FileAccess.Write, FileShare.None))
                                {
                                    encoder.Save(stream);
                                }

                                result = true;
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    ShowErrorMessage(ex.Message);
                }
                catch (LCMSException ex)
                {
                    ShowErrorMessage(ex.Message);
                }
                catch (OverflowException ex)
                {
                    ShowErrorMessage(ex.Message);
                }
                catch (UnauthorizedAccessException ex)
                {
                    ShowErrorMessage(ex.Message);
                }
            }

            return(result);
        }
        /// <summary>
        /// Converts the image to the specified output color profile.
        /// </summary>
        /// <param name="inputProfilePath">The path of the input color profile.</param>
        /// <param name="outputProfilePath">The path of the output color profile.</param>
        /// <param name="intent">The rendering intent to use for the conversion.</param>
        /// <param name="blackPointCompensation"><c>true</c> if black point compensation should be used; otherwise, <c>false</c>.</param>
        /// <param name="source">The source image to convert.</param>
        /// <param name="destination">The destination image to place the converted pixels.</param>
        /// <exception cref="LCMSException">
        /// The input profile could not be opened.
        /// -or-
        /// The output profile could not be opened.
        /// -or-
        /// The color transform could not be created.
        /// -or-
        /// The source and destination images are not the same size.
        /// </exception>
        internal static void ConvertToColorProfile(
            string inputProfilePath,
            string outputProfilePath,
            RenderingIntent intent,
            bool blackPointCompensation,
            Surface source,
            WriteableBitmap destination
            )
        {
            using (LCMSProfileHandle inputProfile = LCMSHelper.OpenColorProfile(inputProfilePath))
            {
                if (inputProfile.IsInvalid)
                {
                    throw new LCMSException(Resources.OpenSourceProfileError);
                }

                using (LCMSProfileHandle outputProfile = LCMSHelper.OpenColorProfile(outputProfilePath))
                {
                    if (outputProfile.IsInvalid)
                    {
                        throw new LCMSException(Resources.OpenDestiniationProfileError);
                    }

                    LCMSStructs.BitmapData sourceBitmapData = new LCMSStructs.BitmapData
                    {
                        width  = (uint)source.Width,
                        height = (uint)source.Height,
                        format = LCMSEnums.BitmapFormat.Bgra8,
                        stride = (uint)source.Stride,
                        scan0  = source.Scan0.Pointer
                    };

                    LCMSStructs.BitmapData destinationBitmapData = new LCMSStructs.BitmapData
                    {
                        width  = (uint)destination.PixelWidth,
                        height = (uint)destination.PixelHeight,
                        format = BitmapFormatFromWICPixelFormat(destination.Format),
                        stride = (uint)destination.BackBufferStride,
                        scan0  = destination.BackBuffer
                    };

                    LCMSEnums.TransformFlags flags = LCMSEnums.TransformFlags.None;

                    if (LCMSHelper.UseBlackPointCompensation(blackPointCompensation, intent))
                    {
                        flags |= LCMSEnums.TransformFlags.BlackPointCompensation;
                    }

                    LCMSEnums.ConvertProfileStatus status = LCMSHelper.ConvertToProfile(
                        inputProfile,
                        outputProfile,
                        intent,
                        flags,
                        ref sourceBitmapData,
                        ref destinationBitmapData
                        );

                    if (status != LCMSEnums.ConvertProfileStatus.Ok)
                    {
                        switch (status)
                        {
                        case LCMSEnums.ConvertProfileStatus.InvalidParameter:
                            throw new LCMSException(Resources.ConvertProfileInvalidParameter);

                        case LCMSEnums.ConvertProfileStatus.DifferentImageDimensions:
                            throw new LCMSException(Resources.DifferentImageDimensions);

                        case LCMSEnums.ConvertProfileStatus.CreateTransformFailed:
                            throw new LCMSException(Resources.CreateTransformError);

                        default:
                            break;
                        }
                    }
                }
            }
        }
예제 #23
0
 /// <summary>
 /// Converts the device color into the PCS color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="inColor">The device color (has to match the profiles device color type)</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <param name="ConversionMethod">The method of conversion</param>
 /// <param name="ConversionType">The type of conversion</param>
 /// <returns>The converted color in the PCS color type</returns>
 public Color ToPCS(ICC Profile, Color inColor, RenderingIntent PrefRenderingIntent, ICCconversionMethod ConversionMethod, ICCconversionType ConversionType)
 {
     InValues = inColor.ColorArray;
     InModel = inColor.Model;
     this.Profile = Profile;
     PreferredRenderingIntent = PrefRenderingIntent;
     this.ConversionMethod = ConversionMethod;
     this.ConversionType = ConversionType;
     IsDefault = false;
     return Do_Device();
 }
예제 #24
0
 /// <summary>
 /// Converts the device color into the PCS color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="inColor">The device color (has to match the profiles device color type)</param>
 /// <param name="PrefRenderingIntent">The preferred rendering intent</param>
 /// <returns>The converted color in the PCS color type</returns>
 public Color ToPCS(ICC Profile, Color inColor, RenderingIntent PrefRenderingIntent)
 {
     InValues = inColor.ColorArray;
     InModel = inColor.Model;
     this.Profile = Profile;
     PreferredRenderingIntent = PrefRenderingIntent;
     IsDefault = true;
     return Do_Device();
 }
예제 #25
0
        public InlineImage CreateInlineImage(TransformationMatrix transformationMatrix, IFilterProvider filterProvider,
                                             IPdfTokenScanner tokenScanner,
                                             RenderingIntent defaultRenderingIntent,
                                             IResourceStore resourceStore)
        {
            if (Properties == null || Bytes == null)
            {
                throw new InvalidOperationException($"Inline image builder not completely defined before calling {nameof(CreateInlineImage)}.");
            }

            bool TryMapColorSpace(NameToken name, out ColorSpace colorSpaceResult)
            {
                if (name.TryMapToColorSpace(out colorSpaceResult))
                {
                    return(true);
                }

                if (TryExtendedColorSpaceNameMapping(name, out colorSpaceResult))
                {
                    return(true);
                }

                if (!resourceStore.TryGetNamedColorSpace(name, out var colorSpaceNamedToken))
                {
                    return(false);
                }

                if (colorSpaceNamedToken.Name.TryMapToColorSpace(out colorSpaceResult))
                {
                    return(true);
                }

                if (TryExtendedColorSpaceNameMapping(colorSpaceNamedToken.Name, out colorSpaceResult))
                {
                    return(true);
                }

                return(false);
            }

            var bounds = transformationMatrix.Transform(new PdfRectangle(new PdfPoint(1, 1),
                                                                         new PdfPoint(0, 0)));

            var width = GetByKeys <NumericToken>(NameToken.Width, NameToken.W, true).Int;

            var height = GetByKeys <NumericToken>(NameToken.Height, NameToken.H, true).Int;

            var maskToken = GetByKeys <BooleanToken>(NameToken.ImageMask, NameToken.Im, false);

            var isMask = maskToken?.Data == true;

            var bitsPerComponent = GetByKeys <NumericToken>(NameToken.BitsPerComponent, NameToken.Bpc, !isMask)?.Int ?? 1;

            var colorSpace = default(ColorSpace?);

            if (!isMask)
            {
                var colorSpaceName = GetByKeys <NameToken>(NameToken.ColorSpace, NameToken.Cs, false);

                if (colorSpaceName == null)
                {
                    var colorSpaceArray = GetByKeys <ArrayToken>(NameToken.ColorSpace, NameToken.Cs, true);

                    if (colorSpaceArray.Length == 0)
                    {
                        throw new PdfDocumentFormatException("Empty ColorSpace array defined for inline image.");
                    }

                    if (!(colorSpaceArray.Data[0] is NameToken firstColorSpaceName))
                    {
                        throw new PdfDocumentFormatException($"Invalid ColorSpace array defined for inline image: {colorSpaceArray}.");
                    }

                    if (!TryMapColorSpace(firstColorSpaceName, out var colorSpaceMapped))
                    {
                        throw new PdfDocumentFormatException($"Invalid ColorSpace defined for inline image: {firstColorSpaceName}.");
                    }

                    colorSpace = colorSpaceMapped;
                }
                else
                {
                    if (!TryMapColorSpace(colorSpaceName, out var colorSpaceMapped))
                    {
                        throw new PdfDocumentFormatException($"Invalid ColorSpace defined for inline image: {colorSpaceName}.");
                    }

                    colorSpace = colorSpaceMapped;
                }
            }

            var renderingIntent = GetByKeys <NameToken>(NameToken.Intent, null, false)?.Data?.ToRenderingIntent() ?? defaultRenderingIntent;

            var filterNames = new List <NameToken>();

            var filterName = GetByKeys <NameToken>(NameToken.Filter, NameToken.F, false);

            if (filterName == null)
            {
                var filterArray = GetByKeys <ArrayToken>(NameToken.Filter, NameToken.F, false);

                if (filterArray != null)
                {
                    filterNames.AddRange(filterArray.Data.OfType <NameToken>());
                }
            }
            else
            {
                filterNames.Add(filterName);
            }

            var filters = filterProvider.GetNamedFilters(filterNames);

            var decodeRaw = GetByKeys <ArrayToken>(NameToken.Decode, NameToken.D, false) ?? new ArrayToken(EmptyArray <IToken> .Instance);

            var decode = decodeRaw.Data.OfType <NumericToken>().Select(x => x.Data).ToArray();

            var filterDictionaryEntries = new Dictionary <NameToken, IToken>();
            var decodeParamsDict        = GetByKeys <DictionaryToken>(NameToken.DecodeParms, NameToken.Dp, false);

            if (decodeParamsDict == null)
            {
                var decodeParamsArray = GetByKeys <ArrayToken>(NameToken.DecodeParms, NameToken.Dp, false);

                if (decodeParamsArray != null)
                {
                    filterDictionaryEntries[NameToken.DecodeParms] = decodeParamsArray;
                }
            }
            else
            {
                filterDictionaryEntries[NameToken.DecodeParms] = decodeParamsDict;
            }

            var streamDictionary = new DictionaryToken(filterDictionaryEntries);

            var interpolate = GetByKeys <BooleanToken>(NameToken.Interpolate, NameToken.I, false)?.Data ?? false;

            return(new InlineImage(bounds, width, height, bitsPerComponent, isMask, renderingIntent, interpolate, colorSpace, decode, Bytes,
                                   filters,
                                   streamDictionary));
        }
예제 #26
0
 /// <summary>
 /// Converts the PCS color into the device color
 /// </summary>
 /// <param name="Profile">The profile that will be used for the conversion</param>
 /// <param name="pcs">The PCS color (has to match the profiles PCS color type)</param>
 /// <param name="ConversionMethod">The method of conversion</param>
 /// <param name="ConversionType">The type of conversion</param>
 /// <returns>The converted color in the device color type</returns>
 public Color ToDevice(ICC Profile, Color pcs, ICCconversionMethod ConversionMethod, ICCconversionType ConversionType)
 {
     InValues = pcs.ColorArray;
     InModel = pcs.Model;
     this.Profile = Profile;
     PreferredRenderingIntent = ColorConverter.PreferredRenderingIntent;
     this.ConversionMethod = ConversionMethod;
     this.ConversionType = ConversionType;
     IsDefault = false;
     return Do_PCS();
 }