Browse Source

Move indexing to method.

af/merge-core
James Jackson-South 9 years ago
parent
commit
5313a6ea9a
  1. 21
      src/ImageSharp/Dithering/Ordered/OrderedDitherBase.cs
  2. 26
      src/ImageSharp/PixelFormats/Rgba32.cs

21
src/ImageSharp/Dithering/Ordered/OrderedDitherBase.cs

@ -1,13 +1,14 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Dithering.Base
{
/// <summary>
/// The base class for performing ordered ditheroing using a 4x4 matrix.
/// The base class for performing ordered dithering using a 4x4 matrix.
/// </summary>
public abstract class OrderedDitherBase : IOrderedDither
{
@ -30,7 +31,23 @@ namespace SixLabors.ImageSharp.Dithering.Base
where TPixel : struct, IPixel<TPixel>
{
source.ToRgba32(ref rgba);
image[x, y] = this.matrix[y % 3, x % 3] >= rgba[index] ? lower : upper;
switch (index)
{
case 0:
image[x, y] = this.matrix[y % 3, x % 3] >= rgba.R ? lower : upper;
return;
case 1:
image[x, y] = this.matrix[y % 3, x % 3] >= rgba.G ? lower : upper;
return;
case 2:
image[x, y] = this.matrix[y % 3, x % 3] >= rgba.B ? lower : upper;
return;
case 3:
image[x, y] = this.matrix[y % 3, x % 3] >= rgba.A ? lower : upper;
return;
}
throw new ArgumentOutOfRangeException(nameof(index), "Index should be between 0 and 3 inclusive.");
}
}
}

26
src/ImageSharp/PixelFormats/Rgba32.cs

@ -220,32 +220,6 @@ namespace SixLabors.ImageSharp
set => this.Rgba = value;
}
/// <summary>
/// Gets the component value at the given index
/// </summary>
/// <param name="index">The component index</param>
/// <returns>The <see cref="byte"/></returns>
public byte this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
DebugGuard.MustBeGreaterThanOrEqualTo(index, 0, nameof(index));
DebugGuard.MustBeLessThanOrEqualTo(index, 3, nameof(index));
switch (index)
{
case 0:
return this.R;
case 1:
return this.G;
case 2:
return this.B;
default:
return this.A;
}
}
}
/// <summary>
/// Compares two <see cref="Rgba32"/> objects for equality.
/// </summary>

Loading…
Cancel
Save