Browse Source

Fix rounding.

Former-commit-id: 24e1ec9e1df29e3885756dd126fd6da2590b1476
Former-commit-id: 92c20b66787ce9498b1f3c00e0c24e2c947af06b
Former-commit-id: 76604e21bb8ae94dbec01b8849919d4b6ae2a27c
pull/17/head
James Jackson-South 10 years ago
parent
commit
65bb422a03
  1. 4
      src/ImageProcessor/Common/Extensions/ComparableExtensions.cs
  2. 56
      src/ImageProcessor/Common/Helpers/PixelOperations.cs
  3. 2
      src/ImageProcessor/ImageProcessor.csproj
  4. 4
      tests/ImageProcessor.Tests/Processors/Filters/FilterTests.cs
  5. 17
      tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs
  6. 2
      tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs
  7. 1
      tests/ImageProcessor.Tests/TestImages/Formats/Jpg/greyscale.jpg.REMOVED.git-id

4
src/ImageProcessor/Common/Extensions/ComparableExtensions.cs

@ -127,7 +127,7 @@ namespace ImageProcessor
/// <returns>The <see cref="byte"/></returns>
public static byte ToByte(this float value)
{
return (byte)(value.Clamp(0, 255) + 0.5f);
return (byte)(value.Clamp(0, 255));
}
/// <summary>
@ -138,7 +138,7 @@ namespace ImageProcessor
/// <returns>The <see cref="byte"/></returns>
public static byte ToByte(this double value)
{
return (byte)(value.Clamp(0, 255) + 0.5d);
return (byte)(value.Clamp(0, 255));
}
}
}

56
src/ImageProcessor/Common/Helpers/PixelOperations.cs

@ -24,6 +24,18 @@ namespace ImageProcessor
/// </summary>
private static readonly Lazy<byte[]> SrgbBytes = new Lazy<byte[]>(GetSrgbBytes);
/// <summary>
/// The array of bytes representing each possible value of color component
/// converted from gamma to the linear color space.
/// </summary>
private static readonly Lazy<byte[]> LinearGammaBytes = new Lazy<byte[]>(GetLinearGammaBytes);
/// <summary>
/// The array of bytes representing each possible value of color component
/// converted from linear to the gamma color space.
/// </summary>
private static readonly Lazy<byte[]> GammaLinearBytes = new Lazy<byte[]>(GetGammaLinearBytes);
/// <summary>
/// Converts an pixel from an sRGB color-space to the equivalent linear color-space.
/// </summary>
@ -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
/// <returns>
/// The <see cref="float"/>.
/// </returns>
internal static float SrgbToLinear(float signal)
private static float SrgbToLinear(float signal)
{
float a = 0.055f;
@ -126,7 +140,7 @@ namespace ImageProcessor
/// <returns>
/// The <see cref="float"/>.
/// </returns>
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;
}
/// <summary>
/// Gets an array of bytes representing each possible value of color component
/// converted from gamma to the linear color space.
/// </summary>
/// <returns>
/// The <see cref="T:byte[]"/>.
/// </returns>
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;
}
/// <summary>
/// Gets an array of bytes representing each possible value of color component
/// converted from linear to the gamma color space.
/// </summary>
/// <returns>
/// The <see cref="T:byte[]"/>.
/// </returns>
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;
}
}
}

2
src/ImageProcessor/ImageProcessor.csproj

@ -77,6 +77,7 @@
<Compile Include="Formats\Png\Zlib\StreamManipulator.cs" />
<Compile Include="Formats\Png\Zlib\ZipConstants.cs" />
<Compile Include="ImageExtensions.cs" />
<Compile Include="Numerics\Rectangle.cs" />
<Compile Include="ParallelImageProcessor.cs" />
<Compile Include="IImageProcessor.cs" />
<Compile Include="Colors\Hsv.cs" />
@ -210,7 +211,6 @@
<Compile Include="ImageBase.cs" />
<Compile Include="Numerics\Point.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Numerics\Rectangle.cs" />
<Compile Include="Numerics\Size.cs" />
<Compile Include="Samplers\Resamplers\Lanczos5Resampler.cs" />
<Compile Include="Samplers\Resamplers\Lanczos8Resampler.cs" />

4
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() },
};

17
tests/ImageProcessor.Tests/Processors/ProcessorTestBase.cs

@ -19,14 +19,15 @@ namespace ImageProcessor.Tests
/// </summary>
public static readonly List<string> Files = new List<string>
{
"../../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" },

2
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");

1
tests/ImageProcessor.Tests/TestImages/Formats/Jpg/greyscale.jpg.REMOVED.git-id

@ -0,0 +1 @@
aca934c81771312563d8db52eedd95bb01164048
Loading…
Cancel
Save