Browse Source

Cleanup implicit operators

Former-commit-id: 32778027aa323d13eb7889099e7a9f3c81e830a9
Former-commit-id: de756ae4ea95b20120cea8e91300b0026684c5ba
Former-commit-id: 4509569039b8c45479b9f7cd99f2bba9583d0901
pull/17/head
James Jackson-South 11 years ago
parent
commit
a8bd09ddac
  1. 102
      src/ImageProcessor/Colors/Bgra.cs
  2. 72
      src/ImageProcessor/Colors/Hsv.cs
  3. 29
      src/ImageProcessor/Colors/YCbCr.cs
  4. 6
      src/ImageProcessor/Filters/Contrast.cs
  5. 10
      src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs

102
src/ImageProcessor/Colors/Bgra.cs

@ -18,7 +18,7 @@ namespace ImageProcessor
/// <summary>
/// Represents a <see cref="Bgra"/> that has B, G, R, and A values set to zero.
/// </summary>
public static readonly Bgra Empty;
public static readonly Bgra Empty = default(Bgra);
/// <summary>
/// Represents a transparent <see cref="Bgra"/> that has B, G, R, and A values set to 255, 255, 255, 0.
@ -65,6 +65,11 @@ namespace ImageProcessor
[FieldOffset(0)]
public readonly int BGRA;
/// <summary>
/// The epsilon for comparing floating point numbers.
/// </summary>
private const float Epsilon = 0.0001f;
/// <summary>
/// Initializes a new instance of the <see cref="Bgra"/> struct.
/// </summary>
@ -173,6 +178,101 @@ namespace ImageProcessor
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => this.B == 0 && this.G == 0 && this.R == 0 && this.A == 0;
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="Hsv"/> to a
/// <see cref="Bgra"/>.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Hsv"/> to convert.
/// </param>
/// <returns>
/// An instance of <see cref="Bgra"/>.
/// </returns>
public static implicit operator Bgra(Hsv color)
{
float s = color.S / 100;
float v = color.V / 100;
if (Math.Abs(s) < Epsilon)
{
byte component = (byte)(v * 255);
return new Bgra(component, component, component, 255);
}
float h = (Math.Abs(color.H - 360) < Epsilon) ? 0 : color.H / 60;
int i = (int)Math.Truncate(h);
float f = h - i;
float p = v * (1.0f - s);
float q = v * (1.0f - (s * f));
float t = v * (1.0f - (s * (1.0f - f)));
float r, g, b;
switch (i)
{
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
r = v;
g = p;
b = q;
break;
}
return new Bgra((byte)Math.Round(b * 255), (byte)Math.Round(g * 255), (byte)Math.Round(r * 255));
}
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="YCbCr"/> to a
/// <see cref="Bgra"/>.
/// </summary>
/// <param name="color">
/// The instance of <see cref="YCbCr"/> to convert.
/// </param>
/// <returns>
/// An instance of <see cref="Bgra"/>.
/// </returns>
public static implicit operator Bgra(YCbCr color)
{
float y = color.Y;
float cb = color.Cb - 128;
float cr = color.Cr - 128;
byte b = (y + (1.772 * cb)).ToByte();
byte g = (y - (0.34414 * cb) - (0.71414 * cr)).ToByte();
byte r = (y + (1.402 * cr)).ToByte();
return new Bgra(b, g, r, 255);
}
/// <summary>
/// Compares two <see cref="Bgra"/> objects. The result specifies whether the values
/// of the <see cref="Bgra.B"/>, <see cref="Bgra.G"/>, <see cref="Bgra.R"/>, and <see cref="Bgra.A"/>

72
src/ImageProcessor/Colors/Hsv.cs

@ -118,78 +118,6 @@ namespace ImageProcessor
return new Hsv(h, s * 100, v * 100);
}
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="Hsv"/> to a
/// <see cref="Bgra"/>.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Hsv"/> to convert.
/// </param>
/// <returns>
/// An instance of <see cref="Bgra"/>.
/// </returns>
public static implicit operator Bgra(Hsv color)
{
float s = color.S / 100;
float v = color.V / 100;
if (Math.Abs(s) < Epsilon)
{
byte component = (byte)(v * 255);
return new Bgra(component, component, component, 255);
}
float h = (Math.Abs(color.H - 360) < Epsilon) ? 0 : color.H / 60;
int i = (int)Math.Truncate(h);
float f = h - i;
float p = v * (1.0f - s);
float q = v * (1.0f - (s * f));
float t = v * (1.0f - (s * (1.0f - f)));
float r, g, b;
switch (i)
{
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
r = v;
g = p;
b = q;
break;
}
return new Bgra((byte)Math.Round(b * 255), (byte)Math.Round(g * 255), (byte)Math.Round(r * 255));
}
/// <summary>
/// Compares two <see cref="Hsv"/> objects. The result specifies whether the values
/// of the <see cref="Hsv.H"/>, <see cref="Hsv.S"/>, and <see cref="Hsv.V"/>

29
src/ImageProcessor/Colors/YCbCr.cs

@ -51,9 +51,9 @@ namespace ImageProcessor
/// <param name="cr">The cr chroma component.</param>
public YCbCr(float y, float cb, float cr)
{
this.Y = y.Clamp(0, 255);
this.Cb = cb.Clamp(0, 255);
this.Cr = cr.Clamp(0, 255);
this.Y = y.ToByte();
this.Cb = cb.ToByte();
this.Cr = cr.ToByte();
}
/// <summary>
@ -87,29 +87,6 @@ namespace ImageProcessor
return new YCbCr(y, cb, cr);
}
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="YCbCr"/> to a
/// <see cref="Bgra"/>.
/// </summary>
/// <param name="color">
/// The instance of <see cref="YCbCr"/> to convert.
/// </param>
/// <returns>
/// An instance of <see cref="Bgra"/>.
/// </returns>
public static implicit operator Bgra(YCbCr color)
{
float y = color.Y;
float cb = color.Cb - 128;
float cr = color.Cr - 128;
byte b = Convert.ToByte((y + (1.772 * cb)).Clamp(0, 255));
byte g = Convert.ToByte((y - (0.34414 * cb) - (0.71414 * cr)).Clamp(0, 255));
byte r = Convert.ToByte((y + (1.402 * cr)).Clamp(0, 255));
return new Bgra(b, g, r, 255);
}
/// <summary>
/// Compares two <see cref="YCbCr"/> objects. The result specifies whether the values
/// of the <see cref="YCbCr.Y"/>, <see cref="YCbCr.Cb"/>, and <see cref="YCbCr.Cr"/>

6
src/ImageProcessor/Filters/Contrast.cs

@ -51,21 +51,21 @@ namespace ImageProcessor.Filters
r *= contrast;
r += 0.5;
r *= 255;
r = r.Clamp(0, 255);
r = r.ToByte();
double g = color.G / 255.0;
g -= 0.5;
g *= contrast;
g += 0.5;
g *= 255;
g = g.Clamp(0, 255);
g = g.ToByte();
double b = color.B / 255.0;
b -= 0.5;
b *= contrast;
b += 0.5;
b *= 255;
b = b.Clamp(0, 255);
b = b.ToByte();
target[x, y] = new Bgra((byte)b, (byte)g, (byte)r, color.A);
}

10
src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs

@ -40,7 +40,7 @@ namespace ImageProcessor.Formats
}
/// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary>
/// <remarks>
/// The Octree quantizer is a two pass algorithm. The initial pass sets up the Octree,
@ -157,7 +157,7 @@ namespace ImageProcessor.Formats
private int previousColor;
/// <summary>
/// Initializes a new instance of the <see cref="Octree"/> class.
/// Initializes a new instance of the <see cref="Octree"/> class.
/// </summary>
/// <param name="maxColorBits">
/// The maximum number of significant bits in the image
@ -465,9 +465,9 @@ namespace ImageProcessor.Formats
// Consume the next palette index
this.paletteIndex = index++;
byte r = (byte)(this.red / this.pixelCount).Clamp(0, 255);
byte g = (byte)(this.green / this.pixelCount).Clamp(0, 255);
byte b = (byte)(this.blue / this.pixelCount).Clamp(0, 255);
byte r = (this.red / this.pixelCount).ToByte();
byte g = (this.green / this.pixelCount).ToByte();
byte b = (this.blue / this.pixelCount).ToByte();
// And set the color of the palette entry
palette.Add(new Bgra(b, g, r));

Loading…
Cancel
Save