Browse Source

Remove from source

pull/384/head
James Jackson-South 9 years ago
parent
commit
90d4bab8c7
  1. 4
      src/ImageSharp/Dithering/Ordered/IOrderedDither.cs
  2. 9
      src/ImageSharp/Dithering/Ordered/OrderedDitherBase.cs
  3. 10
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  4. 20
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  5. 79
      src/ImageSharp/PixelFormats/PixelConversionExtensions.cs
  6. 26
      src/ImageSharp/PixelFormats/Rgba32.cs
  7. 4
      src/ImageSharp/Processing/Processors/Binarization/OrderedDitherProcessor.cs
  8. 34
      src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs

4
src/ImageSharp/Dithering/Ordered/IOrderedDither.cs

@ -17,12 +17,12 @@ namespace SixLabors.ImageSharp.Dithering
/// <param name="source">The source pixel</param>
/// <param name="upper">The color to apply to the pixels above the threshold.</param>
/// <param name="lower">The color to apply to the pixels below the threshold.</param>
/// <param name="bytes">The byte array to pack/unpack to. Must have a length of 4. Bytes are unpacked to Xyzw order.</param>
/// <param name="rgba">The <see cref="Rgba32"/> to pack/unpack to.</param>
/// <param name="index">The component index to test the threshold against. Must range from 0 to 3.</param>
/// <param name="x">The column index.</param>
/// <param name="y">The row index.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, byte[] bytes, int index, int x, int y)
void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, ref Rgba32 rgba, int index, int x, int y)
where TPixel : struct, IPixel<TPixel>;
}
}

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

@ -26,14 +26,11 @@ namespace SixLabors.ImageSharp.Dithering.Base
}
/// <inheritdoc />
public void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, byte[] bytes, int index, int x, int y)
public void Dither<TPixel>(ImageFrame<TPixel> image, TPixel source, TPixel upper, TPixel lower, ref Rgba32 rgba, int index, int x, int y)
where TPixel : struct, IPixel<TPixel>
{
// TODO: This doesn't really cut it for me.
// I'd rather be using float but we need to add some sort of normalization vector methods to all IPixel implementations
// before we can do that as the vectors all cover different ranges.
source.ToXyzwBytes(bytes, 0);
image[x, y] = this.matrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
source.ToRgba32(ref rgba);
image[x, y] = this.matrix[y % 3, x % 3] >= rgba[index] ? lower : upper;
}
}
}

10
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -351,16 +351,16 @@ namespace SixLabors.ImageSharp.Formats.Gif
// Get max colors for bit depth.
int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3;
byte[] colorTable = ArrayPool<byte>.Shared.Rent(colorTableLength);
var rgb = default(Rgb24);
try
{
for (int i = 0; i < pixelCount; i++)
{
int offset = i * 3;
image.Palette[i].ToXyzBytes(this.buffer, 0);
colorTable[offset] = this.buffer[0];
colorTable[offset + 1] = this.buffer[1];
colorTable[offset + 2] = this.buffer[2];
image.Palette[i].ToRgb24(ref rgb);
colorTable[offset] = rgb.R;
colorTable[offset + 1] = rgb.G;
colorTable[offset + 2] = rgb.B;
}
writer.Write(colorTable, 0, colorTableLength);

20
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -318,6 +318,7 @@ namespace SixLabors.ImageSharp.Formats.Png
where TPixel : struct, IPixel<TPixel>
{
byte[] rawScanlineArray = this.rawScanline.Array;
var rgba = default(Rgba32);
// Copy the pixels across from the image.
// Reuse the chunk type buffer.
@ -326,8 +327,8 @@ namespace SixLabors.ImageSharp.Formats.Png
// Convert the color to YCbCr and store the luminance
// Optionally store the original color alpha.
int offset = x * this.bytesPerPixel;
rowSpan[x].ToXyzwBytes(this.chunkTypeBuffer, 0);
byte luminance = (byte)((0.299F * this.chunkTypeBuffer[0]) + (0.587F * this.chunkTypeBuffer[1]) + (0.114F * this.chunkTypeBuffer[2]));
rowSpan[x].ToRgba32(ref rgba);
byte luminance = (byte)((0.299F * rgba.R) + (0.587F * rgba.G) + (0.114F * rgba.B));
for (int i = 0; i < this.bytesPerPixel; i++)
{
@ -337,7 +338,7 @@ namespace SixLabors.ImageSharp.Formats.Png
}
else
{
rawScanlineArray[offset + i] = this.chunkTypeBuffer[3];
rawScanlineArray[offset + i] = rgba.A;
}
}
}
@ -518,7 +519,7 @@ namespace SixLabors.ImageSharp.Formats.Png
int colorTableLength = (int)Math.Pow(2, header.BitDepth) * 3;
byte[] colorTable = ArrayPool<byte>.Shared.Rent(colorTableLength);
byte[] alphaTable = ArrayPool<byte>.Shared.Rent(pixelCount);
byte[] bytes = ArrayPool<byte>.Shared.Rent(4);
var rgba = default(Rgba32);
bool anyAlpha = false;
try
{
@ -527,13 +528,13 @@ namespace SixLabors.ImageSharp.Formats.Png
if (quantized.Pixels.Contains(i))
{
int offset = i * 3;
palette[i].ToXyzwBytes(bytes, 0);
palette[i].ToRgba32(ref rgba);
byte alpha = bytes[3];
byte alpha = rgba.A;
colorTable[offset] = bytes[0];
colorTable[offset + 1] = bytes[1];
colorTable[offset + 2] = bytes[2];
colorTable[offset] = rgba.R;
colorTable[offset + 1] = rgba.G;
colorTable[offset + 2] = rgba.B;
if (alpha > this.threshold)
{
@ -557,7 +558,6 @@ namespace SixLabors.ImageSharp.Formats.Png
{
ArrayPool<byte>.Shared.Return(colorTable);
ArrayPool<byte>.Shared.Return(alphaTable);
ArrayPool<byte>.Shared.Return(bytes);
}
return quantized;

79
src/ImageSharp/PixelFormats/PixelConversionExtensions.cs

@ -1,79 +0,0 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.PixelFormats
{
/// <summary>
/// Extension methods for copying single pixel data into byte Spans.
/// TODO: This utility class exists for legacy reasons. Need to do a lot of chore work to remove it (mostly in test classes).
/// </summary>
internal static class PixelConversionExtensions
{
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to X-> Y-> Z order. Equivalent to R-> G-> B in <see cref="Rgb24"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToXyzBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Rgb24 dest = ref bytes.GetRgb24(startIndex);
pixel.ToRgb24(ref dest);
}
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to X-> Y-> Z-> W order. Equivalent to R-> G-> B-> A in <see cref="Rgba32"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToXyzwBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Rgba32 dest = ref Unsafe.As<byte, Rgba32>(ref bytes[startIndex]);
pixel.ToRgba32(ref dest);
}
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to Z-> Y-> X order. Equivalent to B-> G-> R in <see cref="Bgr24"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToZyxBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Bgr24 dest = ref Unsafe.As<byte, Bgr24>(ref bytes[startIndex]);
pixel.ToBgr24(ref dest);
}
/// <summary>
/// Expands the packed representation into a given byte array.
/// Output is expanded to Z-> Y-> X-> W order. Equivalent to B-> G-> R-> A in <see cref="Bgra32"/>
/// </summary>
/// <typeparam name="TPixel">The pixel type.</typeparam>
/// <param name="pixel">The pixel to copy the data from.</param>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToZyxwBytes<TPixel>(this TPixel pixel, Span<byte> bytes, int startIndex)
where TPixel : struct, IPixel<TPixel>
{
ref Bgra32 dest = ref Unsafe.As<byte, Bgra32>(ref bytes[startIndex]);
pixel.ToBgra32(ref dest);
}
}
}

26
src/ImageSharp/PixelFormats/Rgba32.cs

@ -220,6 +220,32 @@ 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>

4
src/ImageSharp/Processing/Processors/Binarization/OrderedDitherProcessor.cs

@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
int startX = interest.X;
int endX = interest.Right;
byte[] bytes = new byte[4];
var rgba = default(Rgba32);
for (int y = startY; y < endY; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y);
@ -85,7 +85,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
for (int x = startX; x < endX; x++)
{
TPixel sourceColor = row[x];
this.Dither.Dither(source, sourceColor, this.UpperColor, this.LowerColor, bytes, this.Index, x, y);
this.Dither.Dither(source, sourceColor, this.UpperColor, this.LowerColor, ref rgba, this.Index, x, y);
}
}
}

34
src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs

@ -60,11 +60,6 @@ namespace SixLabors.ImageSharp.Quantizers
/// </summary>
private const int TableLength = IndexCount * IndexCount * IndexCount * IndexAlphaCount;
/// <summary>
/// A buffer for storing pixels
/// </summary>
private readonly byte[] rgbaBuffer = new byte[4];
/// <summary>
/// A lookup table for colors
/// </summary>
@ -199,19 +194,15 @@ namespace SixLabors.ImageSharp.Quantizers
{
// Add the color to a 3-D color histogram.
// Colors are expected in r->g->b->a format
pixel.ToXyzwBytes(this.rgbaBuffer, 0);
byte r = this.rgbaBuffer[0];
byte g = this.rgbaBuffer[1];
byte b = this.rgbaBuffer[2];
byte a = this.rgbaBuffer[3];
var rgba = default(Rgba32);
pixel.ToRgba32(ref rgba);
int inr = r >> (8 - IndexBits);
int ing = g >> (8 - IndexBits);
int inb = b >> (8 - IndexBits);
int ina = a >> (8 - IndexAlphaBits);
int r = rgba.R >> 2; // 8 - IndexBits
int g = rgba.G >> 2;
int b = rgba.B >> 2;
int a = rgba.A >> 5; // 8 - IndexAlphaBits
int ind = GetPaletteIndex(inr + 1, ing + 1, inb + 1, ina + 1);
int ind = GetPaletteIndex(r + 1, g + 1, b + 1, a + 1);
this.vwt[ind]++;
this.vmr[ind] += r;
@ -840,12 +831,13 @@ namespace SixLabors.ImageSharp.Quantizers
}
// Expected order r->g->b->a
pixel.ToXyzwBytes(this.rgbaBuffer, 0);
var rgba = default(Rgba32);
pixel.ToRgba32(ref rgba);
int r = this.rgbaBuffer[0] >> (8 - IndexBits);
int g = this.rgbaBuffer[1] >> (8 - IndexBits);
int b = this.rgbaBuffer[2] >> (8 - IndexBits);
int a = this.rgbaBuffer[3] >> (8 - IndexAlphaBits);
int r = rgba.R >> (8 - IndexBits);
int g = rgba.G >> (8 - IndexBits);
int b = rgba.B >> (8 - IndexBits);
int a = rgba.A >> (8 - IndexAlphaBits);
return this.tag[GetPaletteIndex(r + 1, g + 1, b + 1, a + 1)];
}

Loading…
Cancel
Save