Skip to content

Commit db1bf31

Browse files
Use pre-existing pixel conversion methods
1 parent 9fc42d7 commit db1bf31

File tree

6 files changed

+19
-28
lines changed

6 files changed

+19
-28
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Buffers;
56
using System.Numerics;
67
using SixLabors.ImageSharp.Formats.Tiff.Utils;
78
using SixLabors.ImageSharp.Memory;
@@ -50,7 +51,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
5051
bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData;
5152
int offset = 0;
5253

53-
using System.Buffers.IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
54+
using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
5455
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty;
5556
for (int y = top; y < top + height; y++)
5657
{
@@ -86,7 +87,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
8687
if (hasAssociatedAlpha)
8788
{
8889
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan);
89-
TiffUtils.UnPremultiplyRow(vectorsSpan, pixelRow, color);
90+
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Premultiply | PixelConversionModifiers.Scale);
9091
}
9192

9293
offset += byteCount;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Buffers;
56
using System.Numerics;
67
using SixLabors.ImageSharp.Formats.Tiff.Utils;
78
using SixLabors.ImageSharp.Memory;
@@ -36,7 +37,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3637

3738
var color = default(TPixel);
3839
color.FromVector4(TiffUtils.Vector4Default);
39-
using System.Buffers.IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
40+
using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
4041
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : Span<Vector4>.Empty;
4142
for (int y = top; y < top + height; y++)
4243
{
@@ -51,7 +52,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
5152
if (hasAssociatedAlpha)
5253
{
5354
PixelOperations<TPixel>.Instance.ToVector4(this.configuration, pixelRow, vectorsSpan);
54-
TiffUtils.UnPremultiplyRow(vectorsSpan, pixelRow, color);
55+
PixelOperations<TPixel>.Instance.FromVector4Destructive(this.configuration, vectorsSpan, pixelRow, PixelConversionModifiers.Premultiply | PixelConversionModifiers.Scale);
5556
}
5657

5758
offset += byteCount;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
8181
var vec = new Vector4(r, g, b, a);
8282
if (hasAssociatedAlpha)
8383
{
84-
color = TiffUtils.UnPremultiply(vec, color);
84+
color = TiffUtils.UnPremultiply(ref vec, color);
8585
}
8686
else
8787
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
7070
var vec = new Vector4(r, g, b, a);
7171
if (hasAssociatedAlpha)
7272
{
73-
pixelRow[x] = TiffUtils.UnPremultiply(vec, color);
73+
pixelRow[x] = TiffUtils.UnPremultiply(ref vec, color);
7474
}
7575
else
7676
{

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static TPixel ColorFromRgba64Premultiplied<TPixel>(Rgba64 rgba, ulong r,
6969
{
7070
rgba.PackedValue = r | (g << 16) | (b << 32) | (a << 48);
7171
var vec = rgba.ToVector4();
72-
return UnPremultiply(vec, color);
72+
return UnPremultiply(ref vec, color);
7373
}
7474

7575
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -85,7 +85,7 @@ public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, TPixel
8585
public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
8686
where TPixel : unmanaged, IPixel<TPixel>
8787
{
88-
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, a * Scale24Bit);
88+
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
8989
color.FromVector4(colorVector);
9090
return color;
9191
}
@@ -94,8 +94,8 @@ public static TPixel ColorScaleTo24Bit<TPixel>(ulong r, ulong g, ulong b, ulong
9494
public static TPixel ColorScaleTo24BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
9595
where TPixel : unmanaged, IPixel<TPixel>
9696
{
97-
var colorVector = new Vector4(r * Scale24Bit, g * Scale24Bit, b * Scale24Bit, a * Scale24Bit);
98-
return UnPremultiply(colorVector, color);
97+
Vector4 colorVector = new Vector4(r, g, b, a) * Scale24Bit;
98+
return UnPremultiply(ref colorVector, color);
9999
}
100100

101101
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -111,7 +111,7 @@ public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, TPixel
111111
public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
112112
where TPixel : unmanaged, IPixel<TPixel>
113113
{
114-
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, a * Scale32Bit);
114+
Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit;
115115
color.FromVector4(colorVector);
116116
return color;
117117
}
@@ -120,8 +120,8 @@ public static TPixel ColorScaleTo32Bit<TPixel>(ulong r, ulong g, ulong b, ulong
120120
public static TPixel ColorScaleTo32BitPremultiplied<TPixel>(ulong r, ulong g, ulong b, ulong a, TPixel color)
121121
where TPixel : unmanaged, IPixel<TPixel>
122122
{
123-
var colorVector = new Vector4(r * Scale32Bit, g * Scale32Bit, b * Scale32Bit, a * Scale32Bit);
124-
return UnPremultiply(colorVector, color);
123+
Vector4 colorVector = new Vector4(r, g, b, a) * Scale32Bit;
124+
return UnPremultiply(ref colorVector, color);
125125
}
126126

127127
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -152,22 +152,11 @@ public static TPixel ColorScaleTo32Bit<TPixel>(ulong intensity, TPixel color)
152152
}
153153

154154
[MethodImpl(MethodImplOptions.AggressiveInlining)]
155-
public static void UnPremultiplyRow<TPixel>(Span<Vector4> vectors, Span<TPixel> pixelRow, TPixel color)
155+
public static TPixel UnPremultiply<TPixel>(ref Vector4 vector, TPixel color)
156156
where TPixel : unmanaged, IPixel<TPixel>
157157
{
158-
for (int x = 0; x < vectors.Length; x++)
159-
{
160-
Vector4 vec = vectors[x];
161-
pixelRow[x] = UnPremultiply(vec, color);
162-
}
163-
}
164-
165-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
166-
public static TPixel UnPremultiply<TPixel>(Vector4 vec, TPixel color)
167-
where TPixel : unmanaged, IPixel<TPixel>
168-
{
169-
float invW = 1.0f / vec.W;
170-
color.FromVector4(new Vector4(vec.X * invW, vec.Y * invW, vec.Z * invW, vec.W));
158+
Numerics.UnPremultiply(ref vector);
159+
color.FromVector4(vector);
171160

172161
return color;
173162
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public void TiffDecoder_CanDecode_32Bit_WithAssociatedAlpha<TPixel>(TestImagePro
362362
}
363363

364364
// Note: Using tolerant comparer here, because there is a small difference to the reference decoder probably due to floating point rounding issues.
365-
TestTiffDecoder(provider, useExactComparer: false);
365+
TestTiffDecoder(provider, useExactComparer: false, compareTolerance: 0.004F);
366366
}
367367

368368
[Theory]

0 commit comments

Comments
 (0)