Skip to content

Commit 1e48674

Browse files
committed
Optimization in Rgba16161616TiffColor, split a code into 2 loops to avoid a condtion for each pixel
1 parent 0582e85 commit 1e48674

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

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

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3+
34
#nullable disable
45

56
using System.Buffers;
@@ -48,31 +49,53 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4849

4950
using IMemoryOwner<Vector4> vectors = hasAssociatedAlpha ? this.memoryAllocator.Allocate<Vector4>(width) : null;
5051
Span<Vector4> vectorsSpan = hasAssociatedAlpha ? vectors.GetSpan() : [];
51-
for (int y = top; y < top + height; y++)
52-
{
53-
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
5452

55-
if (this.isBigEndian)
53+
if (this.isBigEndian)
54+
{
55+
if (hasAssociatedAlpha)
5656
{
57-
for (int x = 0; x < pixelRow.Length; x++)
57+
for (int y = top; y < top + height; y++)
5858
{
59-
ushort r = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
60-
offset += 2;
61-
ushort g = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
62-
offset += 2;
63-
ushort b = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
64-
offset += 2;
65-
ushort a = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
66-
offset += 2;
67-
68-
pixelRow[x] = hasAssociatedAlpha
69-
? TiffUtilities.ColorFromRgba64Premultiplied<TPixel>(r, g, b, a)
70-
: TPixel.FromRgba64(new Rgba64(r, g, b, a));
59+
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
60+
61+
for (int x = 0; x < pixelRow.Length; x++)
62+
{
63+
ushort r = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
64+
ushort g = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset + 2, 2));
65+
ushort b = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset + 4, 2));
66+
ushort a = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset + 6, 2));
67+
offset += 8;
68+
69+
pixelRow[x] = TiffUtilities.ColorFromRgba64Premultiplied<TPixel>(r, g, b, a);
70+
}
7171
}
7272
}
7373
else
7474
{
75+
for (int y = top; y < top + height; y++)
76+
{
77+
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
78+
79+
for (int x = 0; x < pixelRow.Length; x++)
80+
{
81+
ushort r = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset, 2));
82+
ushort g = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset + 2, 2));
83+
ushort b = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset + 4, 2));
84+
ushort a = TiffUtilities.ConvertToUShortBigEndian(data.Slice(offset + 6, 2));
85+
offset += 8;
86+
87+
pixelRow[x] = TPixel.FromRgba64(new Rgba64(r, g, b, a));
88+
}
89+
}
90+
}
91+
}
92+
else
93+
{
94+
for (int y = top; y < top + height; y++)
95+
{
96+
Span<TPixel> pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width);
7597
int byteCount = pixelRow.Length * 8;
98+
7699
PixelOperations<TPixel>.Instance.FromRgba64Bytes(
77100
this.configuration,
78101
data.Slice(offset, byteCount),

0 commit comments

Comments
 (0)