Skip to content

Commit 6b538f8

Browse files
committed
Check for isBigEndian per row not per pixel
1 parent a1ee0d6 commit 6b538f8

File tree

5 files changed

+105
-33
lines changed

5 files changed

+105
-33
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,25 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3232
var l16 = default(L16);
3333
for (int y = top; y < top + height; y++)
3434
{
35-
for (int x = left; x < left + width; x++)
35+
if (this.isBigEndian)
3636
{
37-
ushort intensity = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
38-
offset += 2;
37+
for (int x = left; x < left + width; x++)
38+
{
39+
ushort intensity = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
40+
offset += 2;
3941

40-
l16.PackedValue = intensity;
41-
color.FromL16(l16);
42+
pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
43+
}
44+
}
45+
else
46+
{
47+
for (int x = left; x < left + width; x++)
48+
{
49+
ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
50+
offset += 2;
4251

43-
pixels[x, y] = color;
52+
pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
53+
}
4454
}
4555
}
4656
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,33 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3434
{
3535
Span<TPixel> pixelRow = pixels.GetRowSpan(y);
3636

37-
for (int x = left; x < left + width; x++)
37+
if (this.isBigEndian)
3838
{
39-
ulong r = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
40-
offset += 2;
41-
ulong g = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
42-
offset += 2;
43-
ulong b = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
44-
offset += 2;
39+
for (int x = left; x < left + width; x++)
40+
{
41+
ulong r = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
42+
offset += 2;
43+
ulong g = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
44+
offset += 2;
45+
ulong b = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
46+
offset += 2;
4547

46-
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
47-
color.FromRgba64(rgba);
48+
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
49+
}
50+
}
51+
else
52+
{
53+
for (int x = left; x < left + width; x++)
54+
{
55+
ulong r = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
56+
offset += 2;
57+
ulong g = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
58+
offset += 2;
59+
ulong b = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
60+
offset += 2;
4861

49-
pixelRow[x] = color;
62+
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
63+
}
5064
}
5165
}
5266
}

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,31 @@ public override void Decode(IMemoryOwner<byte>[] data, Buffer2D<TPixel> pixels,
3535
var rgba = default(Rgba64);
3636
for (int y = top; y < top + height; y++)
3737
{
38-
for (int x = left; x < left + width; x++)
38+
if (this.isBigEndian)
3939
{
40-
ulong r = TiffUtils.ConvertToShort(redData.Slice(offset, 2), this.isBigEndian);
41-
ulong g = TiffUtils.ConvertToShort(greenData.Slice(offset, 2), this.isBigEndian);
42-
ulong b = TiffUtils.ConvertToShort(blueData.Slice(offset, 2), this.isBigEndian);
40+
for (int x = left; x < left + width; x++)
41+
{
42+
ulong r = TiffUtils.ConvertToShortBigEndian(redData.Slice(offset, 2));
43+
ulong g = TiffUtils.ConvertToShortBigEndian(greenData.Slice(offset, 2));
44+
ulong b = TiffUtils.ConvertToShortBigEndian(blueData.Slice(offset, 2));
4345

44-
offset += 2;
46+
offset += 2;
4547

46-
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
47-
color.FromRgba64(rgba);
48-
pixels[x, y] = color;
48+
pixels[x, y] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
49+
}
50+
}
51+
else
52+
{
53+
for (int x = left; x < left + width; x++)
54+
{
55+
ulong r = TiffUtils.ConvertToShortLittleEndian(redData.Slice(offset, 2));
56+
ulong g = TiffUtils.ConvertToShortLittleEndian(greenData.Slice(offset, 2));
57+
ulong b = TiffUtils.ConvertToShortLittleEndian(blueData.Slice(offset, 2));
58+
59+
offset += 2;
60+
61+
pixels[x, y] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
62+
}
4963
}
5064
}
5165
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,25 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
3232
var l16 = default(L16);
3333
for (int y = top; y < top + height; y++)
3434
{
35-
for (int x = left; x < left + width; x++)
35+
if (this.isBigEndian)
3636
{
37-
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian));
38-
offset += 2;
37+
for (int x = left; x < left + width; x++)
38+
{
39+
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)));
40+
offset += 2;
3941

40-
l16.PackedValue = intensity;
41-
color.FromL16(l16);
42+
pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
43+
}
44+
}
45+
else
46+
{
47+
for (int x = left; x < left + width; x++)
48+
{
49+
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)));
50+
offset += 2;
4251

43-
pixels[x, y] = color;
52+
pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
53+
}
4454
}
4555
}
4656
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System;
55
using System.Buffers.Binary;
6+
using System.Runtime.CompilerServices;
7+
using SixLabors.ImageSharp.PixelFormats;
68

79
namespace SixLabors.ImageSharp.Formats.Tiff.Utils
810
{
@@ -11,8 +13,30 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
1113
/// </summary>
1214
internal static class TiffUtils
1315
{
14-
public static ushort ConvertToShort(ReadOnlySpan<byte> buffer, bool isBigEndian) => isBigEndian
15-
? BinaryPrimitives.ReadUInt16BigEndian(buffer)
16-
: BinaryPrimitives.ReadUInt16LittleEndian(buffer);
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17+
public static ushort ConvertToShortBigEndian(ReadOnlySpan<byte> buffer) =>
18+
BinaryPrimitives.ReadUInt16BigEndian(buffer);
19+
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public static ushort ConvertToShortLittleEndian(ReadOnlySpan<byte> buffer) =>
22+
BinaryPrimitives.ReadUInt16LittleEndian(buffer);
23+
24+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
25+
public static TPixel ColorFromL16<TPixel>(L16 l16, ushort intensity, TPixel color)
26+
where TPixel : unmanaged, IPixel<TPixel>
27+
{
28+
l16.PackedValue = intensity;
29+
color.FromL16(l16);
30+
return color;
31+
}
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public static TPixel ColorFromRgba64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color)
35+
where TPixel : unmanaged, IPixel<TPixel>
36+
{
37+
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
38+
color.FromRgba64(rgba);
39+
return color;
40+
}
1741
}
1842
}

0 commit comments

Comments
 (0)