Exemplo n.º 1
0
        public static Stream ProcessImage(WicTransform prev, WicProcessingContext ctx, Stream ostm)
        {
            bool savePlanar = ctx.Settings.SaveFormat == FileFormat.Jpeg && ctx.SourceColorContext == null;

            using (var rot = new WicExifRotator(prev))
                using (var pln = new WicPlanarCache(rot))
                    using (var ply = new WicPlanarSplitter(pln, WicPlane.Luma))
                        using (var lll = new WicGammaExpand(ply))
                            using (var mmm = new WicHighQualityScaler(lll))
                                using (var ggg = new WicGammaCompress(mmm))
                                    using (var sss = new WicUnsharpMask(ggg))

                                        using (var enc = new WicEncoder(ostm.AsIStream(), ctx))
                                            using (var plc = new WicPlanarSplitter(pln, WicPlane.Chroma))
                                            {
                                                if (savePlanar)
                                                {
                                                    var subsample = ctx.Settings.JpegSubsampleMode;
                                                    if (subsample == ChromaSubsampleMode.Subsample420)
                                                    {
                                                        ctx.Settings.Height = (int)Math.Ceiling(ctx.Settings.Height / 2d);
                                                    }

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

                                                    using (var res = new WicScaler(plc))
                                                        using (var pen = new WicPlanarEncoder(enc))
                                                            pen.WriteSource(sss, res);
                                                }
                                                else
                                                {
                                                    using (var res = new WicScaler(plc))
                                                        using (var con = new WicPlanarConverter(sss, res))
                                                            using (var pal = new WicPaletizer(con, 256))
                                                                enc.WriteSource(pal);
                                                }
                                            }

            return(ostm);
        }
Exemplo n.º 2
0
        private static void processImage(WicDecoder dec, WicProcessingContext ctx, Stream ostm)
        {
            using (var frm = new WicFrameReader(dec, ctx))
                using (var met = new WicMetadataReader(frm))
                {
                    if (!ctx.Settings.Normalized)
                    {
                        ctx.Settings.Fixup((int)ctx.Width, (int)ctx.Height, ctx.IsRotated90);
                    }

                    bool mod1   = (!ctx.IsSubsampled || ctx.Settings.HybridScaleRatio > 1d || (ctx.Settings.Crop.Width % 2 == 0 && ctx.Settings.Crop.Height % 2 == 0) || (ctx.Settings.Crop.Width == ctx.Width && ctx.Settings.Crop.Height == ctx.Height));
                    bool planar = ctx.SupportsPlanar && mod1;
                    if (planar && ctx.Settings.HybridMode != HybridScaleMode.Off)
                    {
                        MagicPlanarImageProcessor.ProcessImage(met, ctx, ostm);
                        return;
                    }

                    using (var qsc = new WicNativeScaler(met))
                        using (var rot = new WicExifRotator(qsc))
                            using (var cac = new WicConditionalCache(rot))
                                using (var crp = new WicCropper(cac))
                                    using (var pix = new WicPixelFormatConverter(crp))
                                        using (var cmy = new WicCmykConverter(pix))
                                            using (var res = new WicScaler(cmy, true))
                                                using (var lll = new WicGammaExpand(res))
                                                    using (var mmm = new WicHighQualityScaler(lll))
                                                        using (var mat = new WicMatteTransform(mmm))
                                                            using (var ggg = new WicGammaCompress(mat))
                                                                using (var csc = new WicColorspaceConverter(ggg))
                                                                    using (var sss = new WicUnsharpMask(csc))
                                                                        using (var dit = new WicPaletizer(sss, 256))
                                                                            using (var enc = new WicEncoder(ostm.AsIStream(), ctx))
                                                                                enc.WriteSource(dit);
                }
        }