Browse Source

Check for isBigEndian per row not per pixel

pull/1720/head
Brian Popow 5 years ago
parent
commit
6b538f8967
  1. 22
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
  2. 34
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
  3. 30
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs
  4. 22
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs
  5. 30
      src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs

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

@ -32,15 +32,25 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
var l16 = default(L16); var l16 = default(L16);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x++) if (this.isBigEndian)
{ {
ushort intensity = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian); for (int x = left; x < left + width; x++)
offset += 2; {
ushort intensity = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
offset += 2;
l16.PackedValue = intensity; pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
color.FromL16(l16); }
}
else
{
for (int x = left; x < left + width; x++)
{
ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
offset += 2;
pixels[x, y] = color; pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
}
} }
} }
} }

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

@ -34,19 +34,33 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
{ {
Span<TPixel> pixelRow = pixels.GetRowSpan(y); Span<TPixel> pixelRow = pixels.GetRowSpan(y);
for (int x = left; x < left + width; x++) if (this.isBigEndian)
{ {
ulong r = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian); for (int x = left; x < left + width; x++)
offset += 2; {
ulong g = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian); ulong r = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong b = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian); ulong g = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
offset += 2; offset += 2;
ulong b = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
offset += 2;
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48); pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
color.FromRgba64(rgba); }
}
else
{
for (int x = left; x < left + width; x++)
{
ulong r = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
offset += 2;
ulong g = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
offset += 2;
ulong b = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
offset += 2;
pixelRow[x] = color; pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
}
} }
} }
} }

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

@ -35,17 +35,31 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
var rgba = default(Rgba64); var rgba = default(Rgba64);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x++) if (this.isBigEndian)
{ {
ulong r = TiffUtils.ConvertToShort(redData.Slice(offset, 2), this.isBigEndian); for (int x = left; x < left + width; x++)
ulong g = TiffUtils.ConvertToShort(greenData.Slice(offset, 2), this.isBigEndian); {
ulong b = TiffUtils.ConvertToShort(blueData.Slice(offset, 2), this.isBigEndian); ulong r = TiffUtils.ConvertToShortBigEndian(redData.Slice(offset, 2));
ulong g = TiffUtils.ConvertToShortBigEndian(greenData.Slice(offset, 2));
ulong b = TiffUtils.ConvertToShortBigEndian(blueData.Slice(offset, 2));
offset += 2; offset += 2;
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48); pixels[x, y] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
color.FromRgba64(rgba); }
pixels[x, y] = color; }
else
{
for (int x = left; x < left + width; x++)
{
ulong r = TiffUtils.ConvertToShortLittleEndian(redData.Slice(offset, 2));
ulong g = TiffUtils.ConvertToShortLittleEndian(greenData.Slice(offset, 2));
ulong b = TiffUtils.ConvertToShortLittleEndian(blueData.Slice(offset, 2));
offset += 2;
pixels[x, y] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
}
} }
} }
} }

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

@ -32,15 +32,25 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
var l16 = default(L16); var l16 = default(L16);
for (int y = top; y < top + height; y++) for (int y = top; y < top + height; y++)
{ {
for (int x = left; x < left + width; x++) if (this.isBigEndian)
{ {
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian)); for (int x = left; x < left + width; x++)
offset += 2; {
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)));
offset += 2;
l16.PackedValue = intensity; pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
color.FromL16(l16); }
}
else
{
for (int x = left; x < left + width; x++)
{
ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)));
offset += 2;
pixels[x, y] = color; pixels[x, y] = TiffUtils.ColorFromL16(l16, intensity, color);
}
} }
} }
} }

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

@ -3,6 +3,8 @@
using System; using System;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Tiff.Utils namespace SixLabors.ImageSharp.Formats.Tiff.Utils
{ {
@ -11,8 +13,30 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Utils
/// </summary> /// </summary>
internal static class TiffUtils internal static class TiffUtils
{ {
public static ushort ConvertToShort(ReadOnlySpan<byte> buffer, bool isBigEndian) => isBigEndian [MethodImpl(MethodImplOptions.AggressiveInlining)]
? BinaryPrimitives.ReadUInt16BigEndian(buffer) public static ushort ConvertToShortBigEndian(ReadOnlySpan<byte> buffer) =>
: BinaryPrimitives.ReadUInt16LittleEndian(buffer); BinaryPrimitives.ReadUInt16BigEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort ConvertToShortLittleEndian(ReadOnlySpan<byte> buffer) =>
BinaryPrimitives.ReadUInt16LittleEndian(buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromL16<TPixel>(L16 l16, ushort intensity, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
l16.PackedValue = intensity;
color.FromL16(l16);
return color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TPixel ColorFromRgba64<TPixel>(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color)
where TPixel : unmanaged, IPixel<TPixel>
{
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
color.FromRgba64(rgba);
return color;
}
} }
} }

Loading…
Cancel
Save