Skip to content
Prev Previous commit
Next Next commit
ImageMaths => ImageMath
  • Loading branch information
JimBobSquarePants committed Nov 22, 2020
commit 4565fda8c197ee04b388c0acaf49b2e3c374089c
14 changes: 7 additions & 7 deletions src/ImageSharp/Color/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ namespace SixLabors.ImageSharp
private Color(byte r, byte g, byte b, byte a)
{
this.data = new Rgba64(
ImageMaths.UpscaleFrom8BitTo16Bit(r),
ImageMaths.UpscaleFrom8BitTo16Bit(g),
ImageMaths.UpscaleFrom8BitTo16Bit(b),
ImageMaths.UpscaleFrom8BitTo16Bit(a));
ImageMath.UpscaleFrom8BitTo16Bit(r),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to Numerics too. Or to a separate ColorNumerics (or similarly named) class.

ImageMath.UpscaleFrom8BitTo16Bit(g),
ImageMath.UpscaleFrom8BitTo16Bit(b),
ImageMath.UpscaleFrom8BitTo16Bit(a));
}

[MethodImpl(InliningOptions.ShortMethod)]
private Color(byte r, byte g, byte b)
{
this.data = new Rgba64(
ImageMaths.UpscaleFrom8BitTo16Bit(r),
ImageMaths.UpscaleFrom8BitTo16Bit(g),
ImageMaths.UpscaleFrom8BitTo16Bit(b),
ImageMath.UpscaleFrom8BitTo16Bit(r),
ImageMath.UpscaleFrom8BitTo16Bit(g),
ImageMath.UpscaleFrom8BitTo16Bit(b),
ushort.MaxValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Provides common mathematical methods used for image processing.
/// </summary>
internal static class ImageMaths
internal static class ImageMath
{
/// <summary>
/// Vector for converting pixel to gray value as specified by ITU-R Recommendation BT.709.
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out b
{
case BmpFileMarkerType.Bitmap:
colorMapSizeBytes = this.fileHeader.Offset - BmpFileHeader.Size - this.infoHeader.HeaderSize;
int colorCountForBitDepth = ImageMaths.GetColorCountForBitDepth(this.infoHeader.BitsPerPixel);
int colorCountForBitDepth = ImageMath.GetColorCountForBitDepth(this.infoHeader.BitsPerPixel);
bytesPerColorMapEntry = colorMapSizeBytes / colorCountForBitDepth;

// Edge case for less-than-full-sized palette: bytesPerColorMapEntry should be at least 3.
Expand All @@ -1399,7 +1399,7 @@ private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out b
case BmpFileMarkerType.Pointer:
// OS/2 bitmaps always have 3 colors per color palette entry.
bytesPerColorMapEntry = 3;
colorMapSizeBytes = ImageMaths.GetColorCountForBitDepth(this.infoHeader.BitsPerPixel) * bytesPerColorMapEntry;
colorMapSizeBytes = ImageMath.GetColorCountForBitDepth(this.infoHeader.BitsPerPixel) * bytesPerColorMapEntry;
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Formats/Gif/GifEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
}

// Get the number of bits.
this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(quantized.Palette.Length);
this.bitDepth = ImageMath.GetBitsNeededForColorDepth(quantized.Palette.Length);

// Write the header.
this.WriteHeader(stream);
Expand Down Expand Up @@ -212,7 +212,7 @@ private void EncodeLocal<TPixel>(Image<TPixel> image, IndexedImageFrame<TPixel>
}
}

this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(quantized.Palette.Length);
this.bitDepth = ImageMath.GetBitsNeededForColorDepth(quantized.Palette.Length);
this.WriteGraphicalControlExtension(frameMetadata, this.GetTransparentIndex(quantized), stream);
this.WriteImageDescriptor(frame, true, stream);
this.WriteColorTable(quantized, stream);
Expand Down Expand Up @@ -468,7 +468,7 @@ private void WriteColorTable<TPixel>(IndexedImageFrame<TPixel> image, Stream str
where TPixel : unmanaged, IPixel<TPixel>
{
// The maximum number of colors for the bit depth
int colorTableLength = ImageMaths.GetColorCountForBitDepth(this.bitDepth) * Unsafe.SizeOf<Rgb24>();
int colorTableLength = ImageMath.GetColorCountForBitDepth(this.bitDepth) * Unsafe.SizeOf<Rgb24>();

using IManagedByteBuffer colorTable = this.memoryAllocator.AllocateManagedByteBuffer(colorTableLength, AllocationOptions.Clean);
PixelOperations<TPixel>.Instance.ToRgb24Bytes(
Expand Down
6 changes: 3 additions & 3 deletions src/ImageSharp/Formats/Png/PngEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void CollectGrayscaleBytes<TPixel>(ReadOnlySpan<TPixel> rowSpan)
rowSpan.Length,
AllocationOptions.Clean))
{
int scaleFactor = 255 / (ImageMaths.GetColorCountForBitDepth(this.bitDepth) - 1);
int scaleFactor = 255 / (ImageMath.GetColorCountForBitDepth(this.bitDepth) - 1);
Span<byte> tempSpan = temp.GetSpan();

// We need to first create an array of luminance bytes then scale them down to the correct bit depth.
Expand Down Expand Up @@ -314,7 +314,7 @@ private void CollectGrayscaleBytes<TPixel>(ReadOnlySpan<TPixel> rowSpan)
for (int x = 0, o = 0; x < rgbaSpan.Length; x++, o += 4)
{
Rgba64 rgba = Unsafe.Add(ref rgbaRef, x);
ushort luminance = ImageMaths.Get16BitBT709Luminance(rgba.R, rgba.G, rgba.B);
ushort luminance = ImageMath.Get16BitBT709Luminance(rgba.R, rgba.G, rgba.B);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), luminance);
BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), rgba.A);
}
Expand All @@ -329,7 +329,7 @@ private void CollectGrayscaleBytes<TPixel>(ReadOnlySpan<TPixel> rowSpan)
{
Unsafe.Add(ref rowSpanRef, x).ToRgba32(ref rgba);
Unsafe.Add(ref rawScanlineSpanRef, o) =
ImageMaths.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
ImageMath.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B);
Unsafe.Add(ref rawScanlineSpanRef, o + 1) = rgba.A;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static IndexedImageFrame<TPixel> CreateQuantizedFrame<TPixel>(
if (options.Quantizer is null)
{
byte bits = (byte)options.BitDepth;
var maxColors = ImageMaths.GetColorCountForBitDepth(bits);
var maxColors = ImageMath.GetColorCountForBitDepth(bits);
options.Quantizer = new WuQuantizer(new QuantizerOptions { MaxColors = maxColors });
}

Expand All @@ -101,7 +101,7 @@ public static byte CalculateBitDepth<TPixel>(
byte bitDepth;
if (options.ColorType == PngColorType.Palette)
{
byte quantizedBits = (byte)ImageMaths.GetBitsNeededForColorDepth(quantizedFrame.Palette.Length).Clamp(1, 8);
byte quantizedBits = (byte)ImageMath.GetBitsNeededForColorDepth(quantizedFrame.Palette.Length).Clamp(1, 8);
byte bits = Math.Max((byte)options.BitDepth, quantizedBits);

// Png only supports in four pixel depths: 1, 2, 4, and 8 bits when using the PLTE chunk
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void ProcessGrayscaleScanline<TPixel>(
TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
int scaleFactor = 255 / (ImageMaths.GetColorCountForBitDepth(header.BitDepth) - 1);
int scaleFactor = 255 / (ImageMath.GetColorCountForBitDepth(header.BitDepth) - 1);

if (!hasTrans)
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public static void ProcessInterlacedGrayscaleScanline<TPixel>(
TPixel pixel = default;
ref byte scanlineSpanRef = ref MemoryMarshal.GetReference(scanlineSpan);
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
int scaleFactor = 255 / (ImageMaths.GetColorCountForBitDepth(header.BitDepth) - 1);
int scaleFactor = 255 / (ImageMath.GetColorCountForBitDepth(header.BitDepth) - 1);

if (!hasTrans)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Tga/TgaEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public static int GetLuminance<TPixel>(TPixel sourcePixel)
where TPixel : unmanaged, IPixel<TPixel>
{
var vector = sourcePixel.ToVector4();
return ImageMaths.GetBT709Luminance(ref vector, 256);
return ImageMath.GetBT709Luminance(ref vector, 256);
}
}
}
2 changes: 1 addition & 1 deletion src/ImageSharp/PixelFormats/PixelImplementations/A8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public partial struct A8 : IPixel<A8>, IPackedVector<byte>

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromLa32(La32 source) => this.PackedValue = ImageMaths.DownScaleFrom16BitTo8Bit(source.A);
public void FromLa32(La32 source) => this.PackedValue = ImageMath.DownScaleFrom16BitTo8Bit(source.A);

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
Expand Down
20 changes: 10 additions & 10 deletions src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void FromL8(L8 source)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromL16(L16 source)
{
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.PackedValue);
byte rgb = ImageMath.DownScaleFrom16BitTo8Bit(source.PackedValue);
this.R = rgb;
this.G = rgb;
this.B = rgb;
Expand All @@ -265,11 +265,11 @@ public void FromLa16(La16 source)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromLa32(La32 source)
{
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.L);
byte rgb = ImageMath.DownScaleFrom16BitTo8Bit(source.L);
this.R = rgb;
this.G = rgb;
this.B = rgb;
this.A = ImageMaths.DownScaleFrom16BitTo8Bit(source.A);
this.A = ImageMath.DownScaleFrom16BitTo8Bit(source.A);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -306,20 +306,20 @@ public void ToRgba32(ref Rgba32 dest)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgb48(Rgb48 source)
{
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
this.R = ImageMath.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMath.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMath.DownScaleFrom16BitTo8Bit(source.B);
this.A = byte.MaxValue;
}

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgba64(Rgba64 source)
{
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
this.A = ImageMaths.DownScaleFrom16BitTo8Bit(source.A);
this.R = ImageMath.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMath.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMath.DownScaleFrom16BitTo8Bit(source.B);
this.A = ImageMath.DownScaleFrom16BitTo8Bit(source.A);
}

/// <inheritdoc/>
Expand Down
16 changes: 8 additions & 8 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void FromL8(L8 source)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromL16(L16 source)
{
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.PackedValue);
byte rgb = ImageMath.DownScaleFrom16BitTo8Bit(source.PackedValue);
this.R = rgb;
this.G = rgb;
this.B = rgb;
Expand All @@ -170,7 +170,7 @@ public void FromLa16(La16 source)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromLa32(La32 source)
{
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.L);
byte rgb = ImageMath.DownScaleFrom16BitTo8Bit(source.L);
this.R = rgb;
this.G = rgb;
this.B = rgb;
Expand Down Expand Up @@ -203,18 +203,18 @@ public void ToRgba32(ref Rgba32 dest)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgb48(Rgb48 source)
{
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
this.R = ImageMath.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMath.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMath.DownScaleFrom16BitTo8Bit(source.B);
}

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgba64(Rgba64 source)
{
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
this.R = ImageMath.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMath.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMath.DownScaleFrom16BitTo8Bit(source.B);
}

/// <inheritdoc/>
Expand Down
20 changes: 10 additions & 10 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void FromL8(L8 source)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromL16(L16 source)
{
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.PackedValue);
byte rgb = ImageMath.DownScaleFrom16BitTo8Bit(source.PackedValue);
this.R = rgb;
this.G = rgb;
this.B = rgb;
Expand All @@ -218,11 +218,11 @@ public void FromLa16(La16 source)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromLa32(La32 source)
{
byte rgb = ImageMaths.DownScaleFrom16BitTo8Bit(source.L);
byte rgb = ImageMath.DownScaleFrom16BitTo8Bit(source.L);
this.R = rgb;
this.G = rgb;
this.B = rgb;
this.A = ImageMaths.DownScaleFrom16BitTo8Bit(source.A);
this.A = ImageMath.DownScaleFrom16BitTo8Bit(source.A);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -259,20 +259,20 @@ public void ToRgba32(ref Rgba32 dest)
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgb48(Rgb48 source)
{
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
this.R = ImageMath.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMath.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMath.DownScaleFrom16BitTo8Bit(source.B);
this.A = byte.MaxValue;
}

/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void FromRgba64(Rgba64 source)
{
this.R = ImageMaths.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMaths.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMaths.DownScaleFrom16BitTo8Bit(source.B);
this.A = ImageMaths.DownScaleFrom16BitTo8Bit(source.A);
this.R = ImageMath.DownScaleFrom16BitTo8Bit(source.R);
this.G = ImageMath.DownScaleFrom16BitTo8Bit(source.G);
this.B = ImageMath.DownScaleFrom16BitTo8Bit(source.B);
this.A = ImageMath.DownScaleFrom16BitTo8Bit(source.A);
}

/// <inheritdoc/>
Expand Down
Loading