Exemple #1
0
        public static void AddColorspaceConverter(PipelineContext ctx)
        {
            if (ctx.SourceColorProfile is null || ctx.DestColorProfile is null || ctx.SourceColorProfile == ctx.DestColorProfile)
            {
                return;
            }

            if (ctx.SourceColorProfile.ProfileType > ColorProfileType.Matrix || ctx.DestColorProfile.ProfileType > ColorProfileType.Matrix)
            {
                AddExternalFormatConverter(ctx);

                ctx.WicContext.SourceColorContext ??= ctx.WicContext.AddRef(WicColorProfile.CreateContextFromProfile(ctx.SourceColorProfile.ProfileBytes));
                ctx.WicContext.DestColorContext ??= ctx.WicContext.AddRef(WicColorProfile.CreateContextFromProfile(ctx.DestColorProfile.ProfileBytes));

                WicTransforms.AddColorspaceConverter(ctx);

                return;
            }

            AddInternalFormatConverter(ctx, PixelValueEncoding.Linear);

            if (ctx.Source.Format.ColorRepresentation == PixelColorRepresentation.Bgr && ctx.SourceColorProfile is MatrixProfile srcProf && ctx.DestColorProfile is MatrixProfile dstProf)
            {
                var matrix = srcProf.Matrix * dstProf.InverseMatrix;
                if (matrix != default && !matrix.IsIdentity)
                {
                    ctx.Source = new ColorMatrixTransformInternal(ctx.Source, matrix);
                }
            }
        }
        public static void AddColorProfileReader(PipelineContext ctx)
        {
            var fmt = ctx.ImageFrame is IYccImageFrame?PixelFormat.FromGuid(PixelFormats.Bgr24bpp) : ctx.Source.Format;

            if (ctx.ImageFrame is WicImageFrame wicFrame)
            {
                ctx.WicContext.SourceColorContext = wicFrame.ColorProfileSource.WicColorContext;
                ctx.SourceColorProfile            = wicFrame.ColorProfileSource.ParsedProfile;
            }
            else
            {
                var profile = ColorProfile.Cache.GetOrAdd(ctx.ImageFrame.IccProfile);
                if (profile.IsValid && profile.IsCompatibleWith(fmt) && !profile.IsSrgb)
                {
                    ctx.WicContext.SourceColorContext = ctx.WicContext.AddRef(WicColorProfile.CreateContextFromProfile(profile.ProfileBytes));
                    ctx.SourceColorProfile            = profile;
                }
                else
                {
                    var wicProfile = WicColorProfile.GetDefaultFor(fmt);
                    ctx.WicContext.SourceColorContext = wicProfile.WicColorContext;
                    ctx.SourceColorProfile            = wicProfile.ParsedProfile;
                }
            }

            ctx.WicContext.DestColorContext = ctx.Settings.ColorProfileMode <= ColorProfileMode.NormalizeAndEmbed ? WicColorProfile.GetDefaultFor(fmt).WicColorContext : ctx.WicContext.SourceColorContext;
            ctx.DestColorProfile            = ctx.Settings.ColorProfileMode <= ColorProfileMode.NormalizeAndEmbed ? ColorProfile.GetDefaultFor(fmt) : ctx.SourceColorProfile;
        }