Exemple #1
0
        public static void ResizeImageAdvanced(string path, int scaleValue, Upscale.ScaleMode scaleMode, Upscale.Filter filter, bool onlyDownscale, bool raw = true)
        {
            Image img = Image.Load(path);

            ResizeImageAdvanced(img, path, scaleValue, scaleMode, filter, onlyDownscale, raw);
        }
Exemple #2
0
        public static void ResizeImageAdvanced(Image img, string outpath, int scaleValue, Upscale.ScaleMode scaleMode, Upscale.Filter filter, bool onlyDownscale, bool raw = true)
        {
            PngEncoder enc = new PngEncoder();

            enc.CompressionLevel = PngCompressionLevel.DefaultCompression;
            if (raw)
            {
                enc.CompressionLevel = PngCompressionLevel.NoCompression;
            }
            IResampler resampler;

            resampler = KnownResamplers.MitchellNetravali;
            if (filter == Upscale.Filter.Bicubic)
            {
                resampler = KnownResamplers.Bicubic;
            }
            if (filter == Upscale.Filter.NearestNeighbor)
            {
                resampler = KnownResamplers.NearestNeighbor;
            }

            bool heightLonger = img.Height > img.Width;
            bool widthLonger  = img.Width > img.Height;
            bool square       = (img.Height == img.Width);

            if (scaleMode == Upscale.ScaleMode.Percent)
            {
                Logger.Log("[ImgSharp] Scaling to " + scaleValue + "% with filter " + filter + "...");
                img.Mutate(x => x.Resize((img.Width * scaleValue / 100f).RoundToInt(), (img.Height * scaleValue / 100f).RoundToInt(), resampler));
                img.Save(outpath, enc);
                return;
            }

            // Scale HEIGHT in the following cases:
            bool useSquare     = square && scaleMode != Upscale.ScaleMode.Percent;
            bool useHeight     = scaleMode == Upscale.ScaleMode.PixelsHeight;
            bool useLongerOnH  = (scaleMode == Upscale.ScaleMode.PixelsLongerSide && heightLonger);
            bool useShorterOnW = (scaleMode == Upscale.ScaleMode.PixelsShorterSide && widthLonger);

            if (useSquare || useHeight || useLongerOnH || useShorterOnW)
            {
                if (onlyDownscale && (img.Height <= scaleValue))
                {
                    return;     // don't scale
                }
                Logger.Log("[ImgSharp] Scaling to " + scaleValue + "px height with filter " + filter + "...");
                float wFactor = (float)scaleValue / img.Height;
                img.Mutate(x => x.Resize((img.Width * wFactor).RoundToInt(), scaleValue, resampler));
                img.Save(outpath, enc);
                return;
            }

            // Scale WIDTH in the following cases:
            bool useWidth      = scaleMode == Upscale.ScaleMode.PixelsWidth;
            bool useLongerOnW  = (scaleMode == Upscale.ScaleMode.PixelsLongerSide && widthLonger);
            bool useShorterOnH = (scaleMode == Upscale.ScaleMode.PixelsShorterSide && heightLonger);

            if (useWidth || useLongerOnW || useShorterOnH)
            {
                if (onlyDownscale && (img.Width <= scaleValue))
                {
                    return;     // don't scale
                }
                Logger.Log("[ImgSharp] Scaling to " + scaleValue + "px width with filter " + filter + "...");
                float hFactor = (float)scaleValue / img.Width;
                img.Mutate(x => x.Resize(scaleValue, (img.Height * hFactor).RoundToInt(), resampler));
                img.Save(outpath, enc);
                return;
            }
        }
Exemple #3
0
        public static MagickImage ResizeImageAdvancedMagick(MagickImage img, int scaleValue, Upscale.ScaleMode scaleMode, Upscale.Filter filter, bool onlyDownscale)
        {
            img.FilterType = FilterType.Mitchell;
            if (filter == Upscale.Filter.Bicubic)
            {
                img.FilterType = FilterType.Catrom;
            }
            if (filter == Upscale.Filter.NearestNeighbor)
            {
                img.FilterType = FilterType.Point;
            }

            bool heightLonger = img.Height > img.Width;
            bool widthLonger  = img.Width > img.Height;
            bool square       = (img.Height == img.Width);

            if (scaleMode == Upscale.ScaleMode.Percent)
            {
                Logger.Log("[ImgProc] Scaling to " + scaleValue + "% with filter " + filter + "...");
                img.Resize(new Percentage(scaleValue));
                return(img);
            }

            // Scale HEIGHT in the following cases:
            bool useSquare     = square && scaleMode != Upscale.ScaleMode.Percent;
            bool useHeight     = scaleMode == Upscale.ScaleMode.PixelsHeight;
            bool useLongerOnH  = (scaleMode == Upscale.ScaleMode.PixelsLongerSide && heightLonger);
            bool useShorterOnW = (scaleMode == Upscale.ScaleMode.PixelsShorterSide && widthLonger);

            if (useSquare || useHeight || useLongerOnH || useShorterOnW)
            {
                if (onlyDownscale && (img.Height <= scaleValue))
                {
                    return(img);     // Return image without scaling
                }
                Logger.Log("[ImgProc] Scaling to " + scaleValue + "px height with filter " + filter + "...");
                MagickGeometry geom = new MagickGeometry("x" + scaleValue);
                img.Resize(geom);
            }

            // Scale WIDTH in the following cases:
            bool useWidth      = scaleMode == Upscale.ScaleMode.PixelsWidth;
            bool useLongerOnW  = (scaleMode == Upscale.ScaleMode.PixelsLongerSide && widthLonger);
            bool useShorterOnH = (scaleMode == Upscale.ScaleMode.PixelsShorterSide && heightLonger);

            if (useWidth || useLongerOnW || useShorterOnH)
            {
                if (onlyDownscale && (img.Width <= scaleValue))
                {
                    return(img);     // Return image without scaling
                }
                Logger.Log("[ImgProc] Scaling to " + scaleValue + "px width with filter " + filter + "...");
                MagickGeometry geom = new MagickGeometry(scaleValue + "x");
                img.Resize(geom);
            }

            return(img);
        }