Browse Source

Use pre-existing pixel conversion methods

pull/2062/head
James Jackson-South 5 years ago
parent
commit
db1bf31674
  1. 5
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16161616TiffColor{TPixel}.cs
  2. 5
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba8888TiffColor{TPixel}.cs
  3. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaPlanarTiffColor{TPixel}.cs
  4. 2
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaTiffColor{TPixel}.cs
  5. 31
      src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs
  6. 2
      tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

5
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba16161616TiffColor{TPixel}.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Numerics; using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -50,7 +51,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
int offset = 0; int offset = 0;
using System.Buffers.IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null; using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty; Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -86,7 +87,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
if (hasAssociatedAlpha) if (hasAssociatedAlpha)
{ {
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan); PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan);
TiffUtils.UnPremultiplyRow(vectorsSpan, pixelRow, color); PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Premultiply | PixelConversionModifiers.Scale);
} }
offset += byteCount; offset += byteCount;

5
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba8888TiffColor{TPixel}.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Numerics; using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
@ -36,7 +37,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
var color = default(TPixel); var color = default(TPixel);
color.FromVector4(TiffUtils.Vector4Default); color.FromVector4(TiffUtils.Vector4Default);
using System.Buffers.IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null; using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty; Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty;
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
@ -51,7 +52,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
if (hasAssociatedAlpha) if (hasAssociatedAlpha)
{ {
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan); PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan);
TiffUtils.UnPremultiplyRow(vectorsSpan, pixelRow, color); PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Premultiply | PixelConversionModifiers.Scale);
} }
offset += byteCount; offset += byteCount;

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaPlanarTiffColor{TPixel}.cs

@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
var vec = new Vector4(r, g, b, a); var vec = new Vector4(r, g, b, a);
if (hasAssociatedAlpha) if (hasAssociatedAlpha)
{ {
color = TiffUtils.UnPremultiply(vec, color); color = TiffUtils.UnPremultiply(ref vec, color);
} }
else else
{ {

2
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbaTiffColor{TPixel}.cs

@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
var vec = new Vector4(r, g, b, a); var vec = new Vector4(r, g, b, a);
if (hasAssociatedAlpha) if (hasAssociatedAlpha)
{ {
pixelRow[x] = TiffUtils.UnPremultiply(vec, color); pixelRow[x] = TiffUtils.UnPremultiply(ref vec, color);
} }
else else
{ {

31
src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs

@ -69,7 +69,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
{ {
rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48); rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48);
var vec = rgba.ToVector4(); var vec = rgba.ToVector4();
return UnPremultiply(vec, color); return UnPremultiply(ref vec, color);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -85,7 +85,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, a * Scale24Bit); Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
color.FromVector4(colorVector); color.FromVector4(colorVector);
return color; return color;
} }
@ -94,8 +94,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, a * Scale24Bit); Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
return UnPremultiply(colorVector, color); return UnPremultiply(ref colorVector, color);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -111,7 +111,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, a * Scale32Bit); Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit;
color.FromVector4(colorVector); color.FromVector4(colorVector);
return color; return color;
} }
@ -120,8 +120,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color) public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, a * Scale32Bit); Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit;
return UnPremultiply(colorVector, color); return UnPremultiply(ref colorVector, color);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -152,22 +152,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnPremultiplyRow<TPixel>(Span<Vector4> vectors, Span<TPixel> pixelRow, TPixel color) public static TPixel UnPremultiply<TPixel>(ref Vector4 vector, TPixel color)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
for (int x = 0; x < vectors.Length; x++) Numerics.UnPremultiply(ref vector);
{ color.FromVector4(vector);
Vector4 vec = vectors[x];
pixelRow[x] = UnPremultiply(vec, color);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel UnPremultiply<TPixel>(Vector4 vec, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
float invW = 1.0f / vec.W;
color.FromVector4(new Vector4(vec.X * invW, vec.Y * invW, vec.Z * invW, vec.W));
return color; return color;
} }

2
tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

@ -362,7 +362,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
} }
// Note: Using tolerant comparer here, because there is a small difference to the reference decoder probably due to floating point rounding issues. // Note: Using tolerant comparer here, because there is a small difference to the reference decoder probably due to floating point rounding issues.
TestTiffDecoder(provider, useExactComparer: false); TestTiffDecoder(provider, useExactComparer: false, compareTolerance: 0.004F);
} }
[Theory] [Theory]

Loading…
Cancel
Save