diff --git a/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs b/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs index e13890898..595c727bf 100644 --- a/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs +++ b/src/ImageProcessor/Common/Extensions/ComparableExtensions.cs @@ -127,7 +127,7 @@ namespace ImageProcessor /// The public static byte ToByte(this float value) { - return (byte)(value.Clamp(0, 255) + 0.5f); + return (byte)(value.Clamp(0, 255)); } /// @@ -138,7 +138,7 @@ namespace ImageProcessor /// The public static byte ToByte(this double value) { - return (byte)(value.Clamp(0, 255) + 0.5d); + return (byte)(value.Clamp(0, 255)); } } } diff --git a/src/ImageProcessor/Common/Helpers/PixelOperations.cs b/src/ImageProcessor/Common/Helpers/PixelOperations.cs index 9767268bf..a876b45ef 100644 --- a/src/ImageProcessor/Common/Helpers/PixelOperations.cs +++ b/src/ImageProcessor/Common/Helpers/PixelOperations.cs @@ -24,6 +24,18 @@ namespace ImageProcessor /// private static readonly Lazy SrgbBytes = new Lazy(GetSrgbBytes); + /// + /// The array of bytes representing each possible value of color component + /// converted from gamma to the linear color space. + /// + private static readonly Lazy LinearGammaBytes = new Lazy(GetLinearGammaBytes); + + /// + /// The array of bytes representing each possible value of color component + /// converted from linear to the gamma color space. + /// + private static readonly Lazy GammaLinearBytes = new Lazy(GetGammaLinearBytes); + /// /// Converts an pixel from an sRGB color-space to the equivalent linear color-space. /// @@ -36,6 +48,7 @@ namespace ImageProcessor public static Bgra ToLinear(Bgra composite) { // Create only once and lazily. + // byte[] ramp = LinearGammaBytes.Value; byte[] ramp = LinearBytes.Value; return new Bgra(ramp[composite.B], ramp[composite.G], ramp[composite.R], composite.A); @@ -53,6 +66,7 @@ namespace ImageProcessor public static Bgra ToSrgb(Bgra linear) { // Create only once and lazily. + // byte[] ramp = GammaLinearBytes.Value; byte[] ramp = SrgbBytes.Value; return new Bgra(ramp[linear.B], ramp[linear.G], ramp[linear.R], linear.A); @@ -105,7 +119,7 @@ namespace ImageProcessor /// /// The . /// - internal static float SrgbToLinear(float signal) + private static float SrgbToLinear(float signal) { float a = 0.055f; @@ -126,7 +140,7 @@ namespace ImageProcessor /// /// The . /// - internal static float LinearToSrgb(float signal) + private static float LinearToSrgb(float signal) { float a = 0.055f; @@ -137,5 +151,43 @@ namespace ImageProcessor return ((float)((1 + a) * Math.Pow(signal, 1 / 2.4f))) - a; } + + /// + /// Gets an array of bytes representing each possible value of color component + /// converted from gamma to the linear color space. + /// + /// + /// The . + /// + private static byte[] GetLinearGammaBytes() + { + byte[] ramp = new byte[256]; + for (int x = 0; x < 256; ++x) + { + byte val = (255f * Math.Pow(x / 255f, 2.2)).ToByte(); + ramp[x] = val; + } + + return ramp; + } + + /// + /// Gets an array of bytes representing each possible value of color component + /// converted from linear to the gamma color space. + /// + /// + /// The . + /// + private static byte[] GetGammaLinearBytes() + { + byte[] ramp = new byte[256]; + for (int x = 0; x < 256; ++x) + { + byte val = (255f * Math.Pow(x / 255f, 1 / 2.2)).ToByte(); + ramp[x] = val; + } + + return ramp; + } } } \ No newline at end of file diff --git a/src/ImageProcessor/ImageProcessor.csproj b/src/ImageProcessor/ImageProcessor.csproj index 694ea1f8f..77ea223ad 100644 --- a/src/ImageProcessor/ImageProcessor.csproj +++ b/src/ImageProcessor/ImageProcessor.csproj @@ -77,6 +77,7 @@ + @@ -210,7 +211,6 @@ - diff --git a/tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs b/tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs index a109d02a7..f364dd3f0 100644 --- a/tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs +++ b/tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs @@ -15,12 +15,12 @@ namespace ImageProcessor.Tests //{ "Contrast-50", new Contrast(50) }, //{ "Contrast--50", new Contrast(-50) }, //{ "Alpha--50", new Alpha(50) }, - { "Invert", new Invert() }, + //{ "Invert", new Invert() }, //{ "Sepia", new Sepia() }, //{ "BlackWhite", new BlackWhite() }, //{ "Lomograph", new Lomograph() }, //{ "Polaroid", new Polaroid() }, - //{ "GreyscaleBt709", new GreyscaleBt709() }, + { "GreyscaleBt709", new GreyscaleBt709() }, //{ "GreyscaleBt601", new GreyscaleBt601() }, }; diff --git a/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs b/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs index 01c38bd43..34c8656cd 100644 --- a/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs +++ b/tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs @@ -19,14 +19,15 @@ namespace ImageProcessor.Tests /// public static readonly List Files = new List { - "../../TestImages/Formats/Jpg/Backdrop.jpg", - "../../TestImages/Formats/Jpg/Calliphora.jpg", - "../../TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg", - "../../TestImages/Formats/Bmp/Car.bmp", - "../../TestImages/Formats/Png/cmyk.png", - "../../TestImages/Formats/Png/gamma-1.0-or-2.2.png", - "../../TestImages/Formats/Gif/leaf.gif", - "../../TestImages/Formats/Gif/rings.gif" + //"../../TestImages/Formats/Jpg/Backdrop.jpg", + //"../../TestImages/Formats/Jpg/Calliphora.jpg", + //"../../TestImages/Formats/Jpg/gamma_dalai_lama_gray.jpg", + "../../TestImages/Formats/Jpg/greyscale.jpg", + //"../../TestImages/Formats/Bmp/Car.bmp", + //"../../TestImages/Formats/Png/cmyk.png", + //"../../TestImages/Formats/Png/gamma-1.0-or-2.2.png", + //"../../TestImages/Formats/Gif/leaf.gif", + //"../../TestImages/Formats/Gif/rings.gif" // { "../../TestImages/Formats/Gif/ani.gif" }, // { "../../TestImages/Formats/Gif/ani2.gif" }, diff --git a/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs b/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs index c35ff951e..49ad6576b 100644 --- a/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs +++ b/tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs @@ -47,7 +47,7 @@ namespace ImageProcessor.Tests using (FileStream output = File.OpenWrite($"Resized/{filename}")) { //image.Resize(image.Width / 2, image.Height / 2, sampler).Save(output); - image.Resize(500, 500, sampler, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0, 500, 500)).Save(output); + image.Resize(500, 750, sampler).Save(output); } Trace.WriteLine($"{name}: {watch.ElapsedMilliseconds}ms"); diff --git a/tests/ImageProcessor.Tests/TestImages/Formats/Jpg/greyscale.jpg.REMOVED.git-id b/tests/ImageProcessor.Tests/TestImages/Formats/Jpg/greyscale.jpg.REMOVED.git-id new file mode 100644 index 000000000..bd909a4b1 --- /dev/null +++ b/tests/ImageProcessor.Tests/TestImages/Formats/Jpg/greyscale.jpg.REMOVED.git-id @@ -0,0 +1 @@ +aca934c81771312563d8db52eedd95bb01164048 \ No newline at end of file