Example #1
0
        /// <summary>
        /// Crops an image to the given width and height with the given source rectangle.
        /// <remarks>
        /// If the source rectangle is smaller than the target dimensions then the
        /// area within the source is resized performing a zoomed crop.
        /// </remarks>
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image to crop.</param>
        /// <param name="width">The target image width.</param>
        /// <param name="height">The target image height.</param>
        /// <param name="sourceRectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image"/></returns>
        public static Image <T, TP> Crop <T, TP>(this Image <T, TP> source, int width, int height, Rectangle sourceRectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            Guard.MustBeGreaterThan(width, 0, nameof(width));
            Guard.MustBeGreaterThan(height, 0, nameof(height));

            if (sourceRectangle.Width < width || sourceRectangle.Height < height)
            {
                // If the source rectangle is smaller than the target perform a
                // cropped zoom.
                source = source.Resize(sourceRectangle.Width, sourceRectangle.Height);
            }

            CropProcessor <T, TP> processor = new CropProcessor <T, TP>();

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(width, height, sourceRectangle, new Rectangle(0, 0, width, height), processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #2
0
        /// <summary>
        /// Applies the given colorblindness simulator to the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="colorBlindness">The type of color blindness simulator to apply.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> ColorBlindness <T, TP>(this Image <T, TP> source, ColorBlindness colorBlindness, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            IImageProcessor <T, TP> processor;

            switch (colorBlindness)
            {
            case ImageProcessorCore.ColorBlindness.Achromatomaly:
                processor = new AchromatomalyProcessor <T, TP>();
                break;

            case ImageProcessorCore.ColorBlindness.Achromatopsia:
                processor = new AchromatopsiaProcessor <T, TP>();
                break;

            case ImageProcessorCore.ColorBlindness.Deuteranomaly:
                processor = new DeuteranomalyProcessor <T, TP>();
                break;

            case ImageProcessorCore.ColorBlindness.Deuteranopia:
                processor = new DeuteranopiaProcessor <T, TP>();
                break;

            case ImageProcessorCore.ColorBlindness.Protanomaly:
                processor = new ProtanomalyProcessor <T, TP>();
                break;

            case ImageProcessorCore.ColorBlindness.Protanopia:
                processor = new ProtanopiaProcessor <T, TP>();
                break;

            case ImageProcessorCore.ColorBlindness.Tritanomaly:
                processor = new TritanomalyProcessor <T, TP>();
                break;

            default:
                processor = new TritanopiaProcessor <T, TP>();
                break;
            }

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #3
0
        /// <summary>
        /// Detects any edges within the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="filter">The filter for detecting edges.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> DetectEdges <T, TP>(this Image <T, TP> source, Rectangle rectangle, IEdgeDetectorFilter <T, TP> filter, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            filter.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, filter));
            }
            finally
            {
                filter.OnProgress -= progressHandler;
            }
        }
Example #4
0
        /// <summary>
        /// Replaces the background color of image with the given one.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="color">The color to set as the background.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> BackgroundColor <T, TP>(this Image <T, TP> source, T color, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            BackgroundColorProcessor <T, TP> processor = new BackgroundColorProcessor <T, TP>(color);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(source.Bounds, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #5
0
        /// <summary>
        /// Combines the given image together with the current one by blending their pixels.
        /// </summary>
        /// <param name="source">The image this method extends.</param>
        /// <param name="image">The image to blend with the currently processing image.</param>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="percent">The opacity of the image image to blend. Must be between 0 and 100.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Blend <T, TP>(this Image <T, TP> source, ImageBase <T, TP> image, int percent, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            BlendProcessor <T, TP> processor = new BlendProcessor <T, TP>(image, percent);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #6
0
        /// <summary>
        /// Crops an image to the area of greatest entropy.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image to crop.</param>
        /// <param name="threshold">The threshold for entropic density.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image"/></returns>
        public static Image <T, TP> EntropyCrop <T, TP>(this Image <T, TP> source, float threshold = .5f, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            EntropyCropProcessor <T, TP> processor = new EntropyCropProcessor <T, TP>(threshold);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(source.Width, source.Height, source.Bounds, Rectangle.Empty, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #7
0
        /// <summary>
        /// Applies a box blur to the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="radius">The 'radius' value representing the size of the area to sample.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> BoxBlur <T, TP>(this Image <T, TP> source, int radius, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            BoxBlurProcessor <T, TP> processor = new BoxBlurProcessor <T, TP>(radius);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #8
0
        /// <summary>
        /// Alters the contrast component of the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="amount">The new contrast of the image. Must be between -100 and 100.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Contrast <T, TP>(this Image <T, TP> source, int amount, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            ContrastProcessor <T, TP> processor = new ContrastProcessor <T, TP>(amount);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #9
0
        /// <summary>
        /// Alters the hue component of the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="degrees">The angle in degrees to adjust the image.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Hue <T, TP>(this Image <T, TP> source, float degrees, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            HueProcessor <T, TP> processor = new HueProcessor <T, TP>(degrees);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #10
0
        /// <summary>
        /// Applies a Guassian blur to the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="sigma">The 'sigma' value representing the weight of the blur.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> GuassianBlur <T, TP>(this Image <T, TP> source, float sigma, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            GuassianBlurProcessor <T, TP> processor = new GuassianBlurProcessor <T, TP>(sigma);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #11
0
        /// <summary>
        /// Alters the colors of the image recreating an old Lomograph camera effect.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Lomograph <T, TP>(this Image <T, TP> source, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            LomographProcessor <T, TP> processor = new LomographProcessor <T, TP>();

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #12
0
        /// <summary>
        /// Flips an image by the given instructions.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image to rotate, flip, or both.</param>
        /// <param name="flipType">The <see cref="FlipType"/> to perform the flip.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image"/></returns>
        public static Image <T, TP> Flip <T, TP>(this Image <T, TP> source, FlipType flipType, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            FlipProcessor <T, TP> processor = new FlipProcessor <T, TP>(flipType);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(source.Width, source.Height, source.Bounds, source.Bounds, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #13
0
        /// <summary>
        /// Applies binerization to the image splitting the pixels at the given threshold.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="threshold">The threshold to apply binerization of the image. Must be between 0 and 1.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> BinaryThreshold <T, TP>(this Image <T, TP> source, float threshold, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            BinaryThresholdProcessor <T, TP> processor = new BinaryThresholdProcessor <T, TP>(threshold);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #14
0
        /// <summary>
        /// Skews an image by the given angles in degrees.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image to skew.</param>
        /// <param name="degreesX">The angle in degrees to perform the rotation along the x-axis.</param>
        /// <param name="degreesY">The angle in degrees to perform the rotation along the y-axis.</param>
        /// <param name="expand">Whether to expand the image to fit the skewed result.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image"/></returns>
        public static Image <T, TP> Skew <T, TP>(this Image <T, TP> source, float degreesX, float degreesY, bool expand, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            SkewProcessor <T, TP> processor = new SkewProcessor <T, TP> {
                AngleX = degreesX, AngleY = degreesY, Expand = expand
            };

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(source.Width, source.Height, source.Bounds, source.Bounds, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #15
0
        /// <summary>
        /// Applies Grayscale toning to the image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="mode">The formula to apply to perform the operation.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Grayscale <T, TP>(this Image <T, TP> source, Rectangle rectangle, GrayscaleMode mode = GrayscaleMode.Bt709, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            IImageProcessor <T, TP> processor = mode == GrayscaleMode.Bt709
                ? (IImageProcessor <T, TP>) new GrayscaleBt709Processor <T, TP>()
                : new GrayscaleBt601Processor <T, TP>();

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #16
0
        /// <summary>
        /// Resizes an image to the given width and height with the given sampler and
        /// source rectangle.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image to resize.</param>
        /// <param name="width">The target image width.</param>
        /// <param name="height">The target image height.</param>
        /// <param name="sampler">The <see cref="IResampler"/> to perform the resampling.</param>
        /// <param name="sourceRectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to draw.
        /// </param>
        /// <param name="targetRectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the target image object to draw to.
        /// </param>
        /// <param name="compand">Whether to compress and expand the image color-space to gamma correct the image during processing.</param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/></returns>
        /// <remarks>Passing zero for one of height or width will automatically preserve the aspect ratio of the original image</remarks>
        public static Image <T, TP> Resize <T, TP>(this Image <T, TP> source, int width, int height, IResampler sampler, Rectangle sourceRectangle, Rectangle targetRectangle, bool compand = false, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            if (width == 0 && height > 0)
            {
                width = source.Width * height / source.Height;
                targetRectangle.Width = width;
            }

            if (height == 0 && width > 0)
            {
                height = source.Height * width / source.Width;
                targetRectangle.Height = height;
            }

            Guard.MustBeGreaterThan(width, 0, nameof(width));
            Guard.MustBeGreaterThan(height, 0, nameof(height));

            ResamplingWeightedProcessor <T, TP> processor;

            if (compand)
            {
                processor = new CompandingResizeProcessor <T, TP>(sampler);
            }
            else
            {
                processor = new ResizeProcessor <T, TP>(sampler);
            }

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(width, height, sourceRectangle, targetRectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #17
0
        /// <summary>
        /// Pixelates and image with the given pixel size.
        /// </summary>
        /// <param name="source">The image this method extends.</param>
        /// <param name="size">The size of the pixels.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Pixelate <T, TP>(this Image <T, TP> source, int size, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            if (size <= 0 || size > source.Height || size > source.Width)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }

            PixelateProcessor <T, TP> processor = new PixelateProcessor <T, TP>(size);

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }
Example #18
0
        /// <summary>
        /// Applies a radial glow effect to an image.
        /// </summary>
        /// <typeparam name="T">The pixel format.</typeparam>
        /// <typeparam name="TP">The packed format. <example>long, float.</example></typeparam>
        /// <param name="source">The image this method extends.</param>
        /// <param name="color">The color to set as the glow.</param>
        /// <param name="radius">The the radius.</param>
        /// <param name="rectangle">
        /// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
        /// </param>
        /// <param name="progressHandler">A delegate which is called as progress is made processing the image.</param>
        /// <returns>The <see cref="Image{T,TP}"/>.</returns>
        public static Image <T, TP> Glow <T, TP>(this Image <T, TP> source, T color, float radius, Rectangle rectangle, ProgressEventHandler progressHandler = null)
            where T : IPackedVector <TP>
            where TP : struct
        {
            GlowProcessor <T, TP> processor = new GlowProcessor <T, TP> {
                Radius = radius,
            };

            if (!color.Equals(default(T)))
            {
                processor.GlowColor = color;
            }

            processor.OnProgress += progressHandler;

            try
            {
                return(source.Process(rectangle, processor));
            }
            finally
            {
                processor.OnProgress -= progressHandler;
            }
        }