private static ProcessImageResult processImage(PipelineContext ctx, Stream ostm)
        {
            var frame = (WicImageFrame)ctx.ImageContainer.GetFrame(ctx.Settings.FrameIndex);

            ctx.AddFrameDisposer();
            ctx.ImageFrame = frame;
            ctx.Source     = frame.Source;

            MagicTransforms.AddGifFrameBuffer(ctx);

            ctx.FinalizeSettings();

            WicTransforms.AddColorProfileReader(ctx);
            WicTransforms.AddNativeScaler(ctx);
            WicTransforms.AddExifFlipRotator(ctx);
            WicTransforms.AddCropper(ctx);
            WicTransforms.AddPixelFormatConverter(ctx);
            WicTransforms.AddHybridScaler(ctx);
            WicTransforms.AddScaler(ctx);
            WicTransforms.AddColorspaceConverter(ctx);
            MagicTransforms.AddMatte(ctx);
            MagicTransforms.AddPad(ctx);
            WicTransforms.AddIndexedColorConverter(ctx);

            using var enc = new WicImageEncoder(ctx.Settings.SaveFormat, ostm);
            using var frm = new WicImageEncoderFrame(ctx, enc);
            frm.WriteSource(ctx);
            enc.Commit();

            return(new ProcessImageResult(ctx.UsedSettings, ctx.Stats));
        }
Exemple #2
0
        private static ProcessImageResult processImage(WicDecoder dec, WicProcessingContext ctx, Stream ostm)
        {
            var frm = new WicFrameReader(ctx);

            WicTransforms.AddMetadataReader(ctx);

            ctx.FinalizeSettings();

            WicTransforms.AddNativeScaler(ctx);
            WicTransforms.AddExifRotator(ctx);
            WicTransforms.AddConditionalCache(ctx);
            WicTransforms.AddCropper(ctx);
            WicTransforms.AddPixelFormatConverter(ctx);
            WicTransforms.AddScaler(ctx);
            WicTransforms.AddColorspaceConverter(ctx);
            MagicTransforms.AddMatte(ctx);
            MagicTransforms.AddPad(ctx);
            WicTransforms.AddIndexedColorConverter(ctx);

            var enc = new WicEncoder(ctx, ostm.AsIStream());

            enc.WriteSource(ctx);

            return(new ProcessImageResult {
                Settings = ctx.UsedSettings, Stats = ctx.Stats
            });
        }
        public static void AddColorspaceConverter(WicProcessingContext ctx)
        {
            if (ctx.SourceColorProfile == ctx.DestColorProfile)
            {
                if ((ctx.SourceColorProfile == ColorProfile.sRGB || ctx.SourceColorProfile == ColorProfile.sGrey) && ctx.SourceColorContext != null)
                {
                    AddExternalFormatConverter(ctx);
                    WicTransforms.AddColorspaceConverter(ctx);
                }

                return;
            }

            if (ctx.Source.Format.NumericRepresentation == PixelNumericRepresentation.Float && ctx.Source.Format.Colorspace != PixelColorspace.LinearRgb)
            {
                AddExternalFormatConverter(ctx);
            }

            AddInternalFormatConverter(ctx, forceLinear: true);

            if (ctx.Source.Format.ColorRepresentation != PixelColorRepresentation.Bgr)
            {
                return;
            }

            var matrix = ctx.SourceColorProfile.Matrix * ctx.DestColorProfile.InverseMatrix;

            if (matrix == default || matrix.IsIdentity)
            {
                return;
            }

            ctx.Source = new ColorMatrixTransformInternal(ctx.Source, matrix);
        }
Exemple #4
0
        internal ProcessingPipeline(WicProcessingContext ctx)
        {
            Context = ctx;
            source  = new Lazy <IPixelSource>(() => {
                MagicTransforms.AddExternalFormatConverter(Context);
                WicTransforms.AddPixelFormatConverter(Context, false);

                return(Context.Source.AsIPixelSource());
            });
        }
#pragma warning restore 1573

        internal static ProcessImageResult WriteOutput(PipelineContext ctx, Stream ostm)
        {
            MagicTransforms.AddExternalFormatConverter(ctx);
            WicTransforms.AddIndexedColorConverter(ctx);

            var enc = new WicEncoder(ctx, ostm.AsIStream());

            enc.WriteSource(ctx);

            return(new ProcessImageResult(ctx.UsedSettings, ctx.Stats));
        }
        private static ProcessImageResult executePipeline(WicProcessingContext ctx, Stream ostm)
        {
            WicTransforms.AddIndexedColorConverter(ctx);

            var enc = new WicEncoder(ctx, ostm.AsIStream());

            enc.WriteSource(ctx);

            return(new ProcessImageResult {
                Settings = ctx.UsedSettings, Stats = ctx.Stats
            });
        }
        private void loadInfo(WicDecoder dec, WicProcessingContext ctx)
        {
            ContainerType = ctx.Decoder.ContainerFormat;
            Frames        = new FrameInfo[ctx.Decoder.FrameCount];
            for (int i = 0; i < ctx.Decoder.FrameCount; i++)
            {
                ctx.Settings.FrameIndex = i;
                var frm = new WicFrameReader(ctx);
                WicTransforms.AddMetadataReader(ctx, basicOnly: true);

                int width  = (int)(frm.ExifOrientation.RequiresDimensionSwap() ? ctx.Source.Height : ctx.Source.Width);
                int height = (int)(frm.ExifOrientation.RequiresDimensionSwap() ? ctx.Source.Width : ctx.Source.Height);
                Frames[i] = new FrameInfo(width, height, ctx.Source.Format.AlphaRepresentation != PixelAlphaRepresentation.None, frm.ExifOrientation);
            }
        }
Exemple #8
0
        private static void buildPipeline(WicProcessingContext ctx, bool outputPlanar = true)
        {
            var frm = new WicFrameReader(ctx, EnablePlanarPipeline);

            WicTransforms.AddMetadataReader(ctx);

            ctx.FinalizeSettings();
            ctx.Settings.UnsharpMask       = ctx.UsedSettings.UnsharpMask;
            ctx.Settings.JpegQuality       = ctx.UsedSettings.JpegQuality;
            ctx.Settings.JpegSubsampleMode = ctx.UsedSettings.JpegSubsampleMode;

            if (ctx.DecoderFrame.SupportsPlanarPipeline)
            {
                bool savePlanar = outputPlanar &&
                                  ctx.Settings.SaveFormat == FileFormat.Jpeg &&
                                  ctx.Settings.InnerRect == ctx.Settings.OuterRect &&
                                  ctx.SourceColorContext is null;

                WicTransforms.AddExifRotator(ctx);
                WicTransforms.AddPlanarCache(ctx);

                MagicTransforms.AddHighQualityScaler(ctx);
                MagicTransforms.AddUnsharpMask(ctx);
                MagicTransforms.AddExternalFormatConverter(ctx);

                ctx.SwitchPlanarSource(WicPlane.Chroma);

                if (savePlanar)
                {
                    var subsample = ctx.Settings.JpegSubsampleMode;
                    if (subsample == ChromaSubsampleMode.Subsample420)
                    {
                        ctx.Settings.InnerRect.Height = (int)Math.Ceiling(ctx.Settings.InnerRect.Height / 2d);
                    }

                    if (subsample == ChromaSubsampleMode.Subsample420 || subsample == ChromaSubsampleMode.Subsample422)
                    {
                        ctx.Settings.InnerRect.Width = (int)Math.Ceiling(ctx.Settings.InnerRect.Width / 2d);
                    }
                }

                MagicTransforms.AddHighQualityScaler(ctx);
                MagicTransforms.AddExternalFormatConverter(ctx);

                ctx.SwitchPlanarSource(WicPlane.Luma);

                if (!savePlanar)
                {
                    WicTransforms.AddPlanarConverter(ctx);
                    MagicTransforms.AddColorspaceConverter(ctx);
                    MagicTransforms.AddPad(ctx);
                }
            }
            else
            {
                WicTransforms.AddNativeScaler(ctx);
                WicTransforms.AddExifRotator(ctx);
                WicTransforms.AddConditionalCache(ctx);
                WicTransforms.AddCropper(ctx);
                MagicTransforms.AddHighQualityScaler(ctx, true);
                WicTransforms.AddPixelFormatConverter(ctx);
                WicTransforms.AddScaler(ctx, true);
                MagicTransforms.AddHighQualityScaler(ctx);
                MagicTransforms.AddColorspaceConverter(ctx);
                MagicTransforms.AddMatte(ctx);
                MagicTransforms.AddUnsharpMask(ctx);
                MagicTransforms.AddPad(ctx);
            }
        }
        private static void buildPipeline(PipelineContext ctx, bool outputPlanar = true)
        {
            ctx.ImageFrame = ctx.ImageContainer.GetFrame(ctx.Settings.FrameIndex);

            bool processPlanar = false;
            var  wicFrame      = ctx.ImageFrame as WicImageFrame;

            if (wicFrame != null)
            {
                processPlanar = EnablePlanarPipeline && wicFrame.SupportsPlanarProcessing && ctx.Settings.Interpolation.WeightingFunction.Support >= 0.5;
                bool profilingPassThrough = processPlanar || (wicFrame.SupportsNativeScale && ctx.Settings.HybridScaleRatio > 1);
                ctx.Source = wicFrame.WicSource.AsPixelSource(nameof(IWICBitmapFrameDecode), !profilingPassThrough);
            }
            else if (ctx.ImageFrame is IYccImageFrame planarFrame)
            {
                processPlanar     = true;
                outputPlanar      = outputPlanar && planarFrame.IsFullRange && planarFrame.RgbYccMatrix.IsRouglyEqualTo(YccMatrix.Rec601);
                ctx.PlanarContext = new PipelineContext.PlanarPipelineContext(planarFrame.PixelSource.AsPixelSource(), planarFrame.PixelSourceCb.AsPixelSource(), planarFrame.PixelSourceCr.AsPixelSource());
                ctx.Source        = ctx.PlanarContext.SourceY;
            }

            MagicTransforms.AddColorProfileReader(ctx);

            ctx.FinalizeSettings();
            ctx.Settings.UnsharpMask       = ctx.UsedSettings.UnsharpMask;
            ctx.Settings.JpegQuality       = ctx.UsedSettings.JpegQuality;
            ctx.Settings.JpegSubsampleMode = ctx.UsedSettings.JpegSubsampleMode;

            var subsample = ctx.Settings.JpegSubsampleMode;

            if (processPlanar)
            {
                if (wicFrame != null && !ctx.Settings.AutoCrop && ctx.Settings.HybridScaleRatio == 1)
                {
                    var orCrop = PixelArea.FromGdiRect(ctx.Settings.Crop).DeOrient(ctx.Orientation, ctx.Source.Width, ctx.Source.Height);

                    if (wicFrame.ChromaSubsampling.IsSubsampledX() && ((orCrop.X & 1) != 0 || (orCrop.Width & 1) != 0))
                    {
                        processPlanar = false;
                    }
                    if (wicFrame.ChromaSubsampling.IsSubsampledY() && ((orCrop.Y & 1) != 0 || (orCrop.Height & 1) != 0))
                    {
                        processPlanar = false;
                    }
                }

                if (ctx.Settings.SaveFormat == FileFormat.Jpeg && ctx.Orientation.SwapsDimensions())
                {
                    if (subsample.IsSubsampledX() && (ctx.Settings.InnerSize.Width & 1) != 0)
                    {
                        outputPlanar = false;
                    }
                    if (subsample.IsSubsampledY() && (ctx.Settings.InnerSize.Height & 1) != 0)
                    {
                        outputPlanar = false;
                    }
                }
            }

            if (processPlanar)
            {
                bool savePlanar = outputPlanar &&
                                  ctx.Settings.SaveFormat == FileFormat.Jpeg &&
                                  ctx.Settings.OuterSize == ctx.Settings.InnerSize &&
                                  ctx.DestColorProfile == ctx.SourceColorProfile;

                if (wicFrame != null)
                {
                    WicTransforms.AddPlanarCache(ctx);
                }

                MagicTransforms.AddPlanarCropper(ctx);
                MagicTransforms.AddPlanarHybridScaler(ctx);
                MagicTransforms.AddPlanarHighQualityScaler(ctx, savePlanar ? subsample : ChromaSubsampleMode.Subsample444);
                MagicTransforms.AddUnsharpMask(ctx);

                if (savePlanar)
                {
                    MagicTransforms.AddPlanarExifFlipRotator(ctx);
                    MagicTransforms.AddPlanarExternalFormatConverter(ctx);
                }
                else
                {
                    MagicTransforms.AddPlanarConverter(ctx);
                    MagicTransforms.AddColorspaceConverter(ctx);
                    MagicTransforms.AddExifFlipRotator(ctx);
                    MagicTransforms.AddPad(ctx);
                }
            }
            else
            {
                WicTransforms.AddNativeScaler(ctx);
                MagicTransforms.AddCropper(ctx);
                MagicTransforms.AddHybridScaler(ctx);
                WicTransforms.AddPixelFormatConverter(ctx);
                MagicTransforms.AddHybridScaler(ctx);
                MagicTransforms.AddHighQualityScaler(ctx);
                MagicTransforms.AddColorspaceConverter(ctx);
                MagicTransforms.AddMatte(ctx);
                MagicTransforms.AddUnsharpMask(ctx);
                MagicTransforms.AddExifFlipRotator(ctx);
                MagicTransforms.AddPad(ctx);
            }
        }