diff --git a/src/ImageSharp.Formats.Gif/Helpers/ComparableExtensions.cs b/src/ImageSharp.Formats.Gif/Helpers/ComparableExtensions.cs deleted file mode 100644 index 794c7d7986..0000000000 --- a/src/ImageSharp.Formats.Gif/Helpers/ComparableExtensions.cs +++ /dev/null @@ -1,39 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - - /// - /// Extension methods for classes that implement . - /// - internal static class ComparableExtensions - { - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static int Clamp(this int value, int min, int max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - } -} diff --git a/src/ImageSharp.Formats.Gif/Helpers/ImageMaths.cs b/src/ImageSharp.Formats.Gif/Helpers/ImageMaths.cs deleted file mode 100644 index 8f377d3af0..0000000000 --- a/src/ImageSharp.Formats.Gif/Helpers/ImageMaths.cs +++ /dev/null @@ -1,30 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.Linq; - using System.Numerics; - - /// - /// Provides common mathematical methods. - /// - internal static class ImageMaths - { - /// - /// Returns how many bits are required to store the specified number of colors. - /// Performs a Log2() on the value. - /// - /// The number of colors. - /// - /// The - /// - public static int GetBitsNeededForColorDepth(int colors) - { - return (int)Math.Ceiling(Math.Log(colors, 2)); - } - } -} diff --git a/src/ImageSharp.Formats.Gif/Helpers/StreamExtensions.cs b/src/ImageSharp.Formats.Gif/Helpers/StreamExtensions.cs deleted file mode 100644 index 6de94dd229..0000000000 --- a/src/ImageSharp.Formats.Gif/Helpers/StreamExtensions.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System.Buffers; - using System.IO; - - /// - /// Extension methods for the type. - /// - internal static class StreamExtensions - { - /// - /// Skips the number of bytes in the given stream. - /// - /// The stream. - /// The count. - public static void Skip(this Stream stream, int count) - { - if (count < 1) - { - return; - } - - if (stream.CanSeek) - { - stream.Position += count; - } - else - { - byte[] foo = ArrayPool.Shared.Rent(count); - try - { - stream.Read(foo, 0, count); - } - finally - { - ArrayPool.Shared.Return(foo); - } - } - } - } -} diff --git a/src/ImageSharp.Formats.Jpeg/Helpers/ComparableExtensions.cs b/src/ImageSharp.Formats.Jpeg/Helpers/ComparableExtensions.cs deleted file mode 100644 index c6c937629a..0000000000 --- a/src/ImageSharp.Formats.Jpeg/Helpers/ComparableExtensions.cs +++ /dev/null @@ -1,63 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - - /// - /// Extension methods for classes that implement . - /// - internal static class ComparableExtensions - { - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static int Clamp(this int value, int min, int max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static float Clamp(this float value, float min, float max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - } -} diff --git a/src/ImageSharp.Formats.Png/Helpers/ByteExtensions.cs b/src/ImageSharp.Formats.Png/Helpers/ByteExtensions.cs deleted file mode 100644 index fc9c29e625..0000000000 --- a/src/ImageSharp.Formats.Png/Helpers/ByteExtensions.cs +++ /dev/null @@ -1,45 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - /// - /// Extension methods for the struct. - /// - internal static class ByteExtensions - { - /// - /// Optimized reversal algorithm. - /// - /// The byte array. - public static void ReverseBytes(this byte[] source) - { - ReverseBytes(source, 0, source.Length); - } - - /// - /// Optimized reversal algorithm. - /// - /// The byte array. - /// The index. - /// The length. - /// is null. - public static void ReverseBytes(this byte[] source, int index, int length) - { - Guard.NotNull(source, nameof(source)); - - int i = index; - int j = index + length - 1; - while (i < j) - { - byte temp = source[i]; - source[i] = source[j]; - source[j] = temp; - i++; - j--; - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp.Formats.Png/Helpers/ComparableExtensions.cs b/src/ImageSharp.Formats.Png/Helpers/ComparableExtensions.cs deleted file mode 100644 index 826f397ce3..0000000000 --- a/src/ImageSharp.Formats.Png/Helpers/ComparableExtensions.cs +++ /dev/null @@ -1,52 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - - /// - /// Extension methods for classes that implement . - /// - internal static class ComparableExtensions - { - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static int Clamp(this int value, int min, int max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Swaps the references to two objects in memory. - /// - /// The first reference. - /// The second reference. - /// The type of object. - public static void Swap(ref T first, ref T second) - { - T temp = second; - second = first; - first = temp; - } - } -} diff --git a/src/ImageSharp.Formats.Png/Helpers/ImageMaths.cs b/src/ImageSharp.Formats.Png/Helpers/ImageMaths.cs deleted file mode 100644 index 8f377d3af0..0000000000 --- a/src/ImageSharp.Formats.Png/Helpers/ImageMaths.cs +++ /dev/null @@ -1,30 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.Linq; - using System.Numerics; - - /// - /// Provides common mathematical methods. - /// - internal static class ImageMaths - { - /// - /// Returns how many bits are required to store the specified number of colors. - /// Performs a Log2() on the value. - /// - /// The number of colors. - /// - /// The - /// - public static int GetBitsNeededForColorDepth(int colors) - { - return (int)Math.Ceiling(Math.Log(colors, 2)); - } - } -} diff --git a/src/ImageSharp.Formats.Png/Helpers/StreamExtensions.cs b/src/ImageSharp.Formats.Png/Helpers/StreamExtensions.cs deleted file mode 100644 index 6de94dd229..0000000000 --- a/src/ImageSharp.Formats.Png/Helpers/StreamExtensions.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System.Buffers; - using System.IO; - - /// - /// Extension methods for the type. - /// - internal static class StreamExtensions - { - /// - /// Skips the number of bytes in the given stream. - /// - /// The stream. - /// The count. - public static void Skip(this Stream stream, int count) - { - if (count < 1) - { - return; - } - - if (stream.CanSeek) - { - stream.Position += count; - } - else - { - byte[] foo = ArrayPool.Shared.Rent(count); - try - { - stream.Read(foo, 0, count); - } - finally - { - ArrayPool.Shared.Return(foo); - } - } - } - } -} diff --git a/src/ImageSharp.Processing/ComparableExtensions.cs b/src/ImageSharp.Processing/ComparableExtensions.cs deleted file mode 100644 index 4512e98b73..0000000000 --- a/src/ImageSharp.Processing/ComparableExtensions.cs +++ /dev/null @@ -1,182 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Processing -{ - using System; - - /// - /// Extension methods for classes that implement . - /// - internal static class ComparableExtensions - { - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static byte Clamp(this byte value, byte min, byte max) - { - // Order is important here as someone might set min to higher than max. - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static uint Clamp(this uint value, uint min, uint max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static int Clamp(this int value, int min, int max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static float Clamp(this float value, float min, float max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Restricts a to be within a specified range. - /// - /// The The value to clamp. - /// The minimum value. If value is less than min, min will be returned. - /// The maximum value. If value is greater than max, max will be returned. - /// - /// The representing the clamped value. - /// - public static double Clamp(this double value, double min, double max) - { - if (value > max) - { - return max; - } - - if (value < min) - { - return min; - } - - return value; - } - - /// - /// Converts an to a first restricting the value between the - /// minimum and maximum allowable ranges. - /// - /// The this method extends. - /// The - public static byte ToByte(this int value) - { - return (byte)value.Clamp(0, 255); - } - - /// - /// Converts an to a first restricting the value between the - /// minimum and maximum allowable ranges. - /// - /// The this method extends. - /// The - public static byte ToByte(this float value) - { - return (byte)value.Clamp(0, 255); - } - - /// - /// Converts an to a first restricting the value between the - /// minimum and maximum allowable ranges. - /// - /// The this method extends. - /// The - public static byte ToByte(this double value) - { - return (byte)value.Clamp(0, 255); - } - - /// - /// Swaps the references to two objects in memory. - /// - /// The first reference. - /// The second reference. - /// The type of object. - public static void Swap(ref T first, ref T second) - { - T temp = second; - second = first; - first = temp; - } - } -} diff --git a/src/ImageSharp.Processing/Constants.cs b/src/ImageSharp.Processing/Constants.cs deleted file mode 100644 index 0071471a78..0000000000 --- a/src/ImageSharp.Processing/Constants.cs +++ /dev/null @@ -1,18 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Processing -{ - /// - /// Common constants used throughout the project - /// - internal static class Constants - { - /// - /// The epsilon for comparing floating point numbers. - /// - public static readonly float Epsilon = 0.001f; - } -} diff --git a/src/ImageSharp.Processing/ImageMaths.cs b/src/ImageSharp.Processing/ImageMaths.cs deleted file mode 100644 index c2d905b171..0000000000 --- a/src/ImageSharp.Processing/ImageMaths.cs +++ /dev/null @@ -1,273 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Processing -{ - using System; - using System.Linq; - using System.Numerics; - - /// - /// Provides common mathematical methods. - /// - internal static class ImageMaths - { - /// - /// Implementation of 1D Gaussian G(x) function - /// - /// The x provided to G(x). - /// The spread of the blur. - /// The Gaussian G(x) - public static float Gaussian(float x, float sigma) - { - const float Numerator = 1.0f; - float denominator = (float)(Math.Sqrt(2 * Math.PI) * sigma); - - float exponentNumerator = -x * x; - float exponentDenominator = (float)(2 * Math.Pow(sigma, 2)); - - float left = Numerator / denominator; - float right = (float)Math.Exp(exponentNumerator / exponentDenominator); - - return left * right; - } - - /// - /// Returns the result of a B-C filter against the given value. - /// - /// - /// The value to process. - /// The B-Spline curve variable. - /// The Cardinal curve variable. - /// - /// The . - /// - public static float GetBcValue(float x, float b, float c) - { - float temp; - - if (x < 0F) - { - x = -x; - } - - temp = x * x; - if (x < 1F) - { - x = ((12 - (9 * b) - (6 * c)) * (x * temp)) + ((-18 + (12 * b) + (6 * c)) * temp) + (6 - (2 * b)); - return x / 6F; - } - - if (x < 2F) - { - x = ((-b - (6 * c)) * (x * temp)) + (((6 * b) + (30 * c)) * temp) + (((-12 * b) - (48 * c)) * x) + ((8 * b) + (24 * c)); - return x / 6F; - } - - return 0F; - } - - /// - /// Gets the result of a sine cardinal function for the given value. - /// - /// The value to calculate the result for. - /// - /// The . - /// - public static float SinC(float x) - { - if (Math.Abs(x) > Constants.Epsilon) - { - x *= (float)Math.PI; - return Clean((float)Math.Sin(x) / x); - } - - return 1.0f; - } - - /// - /// Returns the given degrees converted to radians. - /// - /// The angle in degrees. - /// - /// The representing the degree as radians. - /// - public static float DegreesToRadians(float degrees) - { - return degrees * (float)(Math.PI / 180); - } - - /// - /// Gets the bounding from the given points. - /// - /// - /// The designating the top left position. - /// - /// - /// The designating the bottom right position. - /// - /// - /// The bounding . - /// - public static Rectangle GetBoundingRectangle(Point topLeft, Point bottomRight) - { - return new Rectangle(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); - } - - /// - /// Gets the bounding from the given matrix. - /// - /// The source rectangle. - /// The transformation matrix. - /// - /// The . - /// - public static Rectangle GetBoundingRectangle(Rectangle rectangle, Matrix3x2 matrix) - { - Vector2 leftTop = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Top), matrix); - Vector2 rightTop = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Top), matrix); - Vector2 leftBottom = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Bottom), matrix); - Vector2 rightBottom = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Bottom), matrix); - - Vector2[] allCorners = { leftTop, rightTop, leftBottom, rightBottom }; - float extentX = allCorners.Select(v => v.X).Max() - allCorners.Select(v => v.X).Min(); - float extentY = allCorners.Select(v => v.Y).Max() - allCorners.Select(v => v.Y).Min(); - return new Rectangle(0, 0, (int)extentX, (int)extentY); - } - - /// - /// Finds the bounding rectangle based on the first instance of any color component other - /// than the given one. - /// - /// The pixel format. - /// The to search within. - /// The color component value to remove. - /// The channel to test against. - /// - /// The . - /// - public static Rectangle GetFilteredBoundingRectangle(ImageBase bitmap, float componentValue, RgbaComponent channel = RgbaComponent.B) - where TColor : struct, IPackedPixel, IEquatable - { - int width = bitmap.Width; - int height = bitmap.Height; - Point topLeft = default(Point); - Point bottomRight = default(Point); - - Func, int, int, float, bool> delegateFunc; - - // Determine which channel to check against - switch (channel) - { - case RgbaComponent.R: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().X - b) > Constants.Epsilon; - break; - - case RgbaComponent.G: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Y - b) > Constants.Epsilon; - break; - - case RgbaComponent.B: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Z - b) > Constants.Epsilon; - break; - - default: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().W - b) > Constants.Epsilon; - break; - } - - Func, int> getMinY = pixels => - { - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) - { - if (delegateFunc(pixels, x, y, componentValue)) - { - return y; - } - } - } - - return 0; - }; - - Func, int> getMaxY = pixels => - { - for (int y = height - 1; y > -1; y--) - { - for (int x = 0; x < width; x++) - { - if (delegateFunc(pixels, x, y, componentValue)) - { - return y; - } - } - } - - return height; - }; - - Func, int> getMinX = pixels => - { - for (int x = 0; x < width; x++) - { - for (int y = 0; y < height; y++) - { - if (delegateFunc(pixels, x, y, componentValue)) - { - return x; - } - } - } - - return 0; - }; - - Func, int> getMaxX = pixels => - { - for (int x = width - 1; x > -1; x--) - { - for (int y = 0; y < height; y++) - { - if (delegateFunc(pixels, x, y, componentValue)) - { - return x; - } - } - } - - return height; - }; - - using (PixelAccessor bitmapPixels = bitmap.Lock()) - { - topLeft.Y = getMinY(bitmapPixels); - topLeft.X = getMinX(bitmapPixels); - bottomRight.Y = (getMaxY(bitmapPixels) + 1).Clamp(0, height); - bottomRight.X = (getMaxX(bitmapPixels) + 1).Clamp(0, width); - } - - return GetBoundingRectangle(topLeft, bottomRight); - } - - /// - /// Ensures that any passed double is correctly rounded to zero - /// - /// The value to clean. - /// - /// The - /// . - private static float Clean(float x) - { - if (Math.Abs(x) < Constants.Epsilon) - { - return 0F; - } - - return x; - } - } -} diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index cc05e18005..cf307e9365 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -14,7 +14,7 @@ namespace ImageSharp /// Provides methods to protect against invalid parameters. /// [DebuggerStepThrough] - public static class Guard + internal static class Guard { /// /// Verifies, that the method parameter with specified object value is not null diff --git a/src/ImageSharp/Properties/AssemblyInfo.cs b/src/ImageSharp/Properties/AssemblyInfo.cs index 3a9fc9d7e8..2642cde738 100644 --- a/src/ImageSharp/Properties/AssemblyInfo.cs +++ b/src/ImageSharp/Properties/AssemblyInfo.cs @@ -37,4 +37,11 @@ using System.Runtime.CompilerServices; // Ensure the internals can be tested. [assembly: InternalsVisibleTo("ImageSharp.Benchmarks")] [assembly: InternalsVisibleTo("ImageSharp.Tests")] -[assembly: InternalsVisibleTo("ImageSharp.Tests46")] \ No newline at end of file + +// Ensure the other projects can see the internal helpers +[assembly: InternalsVisibleTo("ImageSharp.Drawing")] +[assembly: InternalsVisibleTo("ImageSharp.Formats.Bmp")] +[assembly: InternalsVisibleTo("ImageSharp.Formats.Gif")] +[assembly: InternalsVisibleTo("ImageSharp.Formats.Jpeg")] +[assembly: InternalsVisibleTo("ImageSharp.Formats.Png")] +[assembly: InternalsVisibleTo("ImageSharp.Processing")] \ No newline at end of file