Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{
private readonly bool isBigEndian;

private readonly Configuration configuration;

/// <summary>
/// Initializes a new instance of the <see cref="BlackIsZero16TiffColor{TPixel}" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
public BlackIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
public BlackIsZero16TiffColor(Configuration configuration, bool isBigEndian)
{
this.configuration = configuration;
this.isBigEndian = isBigEndian;
}

/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
Expand All @@ -47,13 +54,14 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
}
else
{
for (int x = 0; x < pixelRow.Length; x++)
{
ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
offset += 2;
int byteCount = pixelRow.Length * 2;
PixelOperations<TPixel>.Instance.FromL16Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);

pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
}
offset += byteCount;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0.

using System;
using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

Expand All @@ -14,22 +13,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
internal class BlackIsZero8TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly Configuration configuration;

public BlackIsZero8TiffColor(Configuration configuration) => this.configuration = configuration;

/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{
var color = default(TPixel);

int offset = 0;

var l8 = default(L8);
for (int y = top; y < top + height; y++)
{
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
for (int x = 0; x < pixelRow.Length; x++)
{
byte intensity = data[offset++];
pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color);
}
int byteCount = pixelRow.Length;
PixelOperations<TPixel>.Instance.FromL8Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);

offset += byteCount;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
{
private readonly bool isBigEndian;

private readonly Configuration configuration;

/// <summary>
/// Initializes a new instance of the <see cref="Rgb161616TiffColor{TPixel}" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
public Rgb161616TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
public Rgb161616TiffColor(Configuration configuration, bool isBigEndian)
{
this.configuration = configuration;
this.isBigEndian = isBigEndian;
}

/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
Expand Down Expand Up @@ -53,17 +60,14 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
}
else
{
for (int x = 0; x < pixelRow.Length; 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;
int byteCount = pixelRow.Length * 6;
PixelOperations<TPixel>.Instance.FromRgb48Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);

pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
}
offset += byteCount;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0.

using System;

using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;

Expand All @@ -14,29 +13,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
internal class Rgb888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly Configuration configuration;

public Rgb888TiffColor(Configuration configuration) => this.configuration = configuration;

/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
{
var color = default(TPixel);

int offset = 0;

var rgba = default(Rgba32);
for (int y = top; y < top + height; y++)
{
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);

for (int x = 0; x < pixelRow.Length; x++)
{
byte r = data[offset++];
byte g = data[offset++];
byte b = data[offset++];

rgba.PackedValue = (uint)(r | (g << 8) | (b << 16) | (0xff << 24));
color.FromRgba32(rgba);

pixelRow[x] = color;
}
int byteCount = pixelRow.Length * 3;
PixelOperations<TPixel>.Instance.FromRgb24Bytes(
this.configuration,
data.Slice(offset, byteCount),
pixelRow,
pixelRow.Length);

offset += byteCount;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
internal static class TiffColorDecoderFactory<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder)
public static TiffBaseColorDecoder<TPixel> Create(Configuration configuration, TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder)
{
switch (colorType)
{
Expand Down Expand Up @@ -55,12 +55,12 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
case TiffColorType.BlackIsZero8:
DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 8, "bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new BlackIsZero8TiffColor<TPixel>();
return new BlackIsZero8TiffColor<TPixel>(configuration);

case TiffColorType.BlackIsZero16:
DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 16, "bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new BlackIsZero16TiffColor<TPixel>(byteOrder == ByteOrder.BigEndian);
return new BlackIsZero16TiffColor<TPixel>(configuration, byteOrder == ByteOrder.BigEndian);

case TiffColorType.Rgb:
DebugGuard.IsTrue(colorMap == null, "colorMap");
Expand Down Expand Up @@ -94,7 +94,7 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
&& bitsPerSample.Channel0 == 8,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new Rgb888TiffColor<TPixel>();
return new Rgb888TiffColor<TPixel>(configuration);

case TiffColorType.Rgb101010:
DebugGuard.IsTrue(
Expand Down Expand Up @@ -134,7 +134,7 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
&& bitsPerSample.Channel0 == 16,
"bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new Rgb161616TiffColor<TPixel>(isBigEndian: byteOrder == ByteOrder.BigEndian);
return new Rgb161616TiffColor<TPixel>(configuration, isBigEndian: byteOrder == ByteOrder.BigEndian);

case TiffColorType.PaletteColor:
DebugGuard.NotNull(colorMap, "colorMap");
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private void DecodeStripsChunky<TPixel>(ImageFrame<TPixel> frame, int rowsPerStr
this.Predictor,
this.FaxCompressionOptions);

TiffBaseColorDecoder<TPixel> colorDecoder = TiffColorDecoderFactory<TPixel>.Create(this.ColorType, this.BitsPerSample, this.ColorMap, this.byteOrder);
TiffBaseColorDecoder<TPixel> colorDecoder = TiffColorDecoderFactory<TPixel>.Create(this.Configuration, this.ColorType, this.BitsPerSample, this.ColorMap, this.byteOrder);

for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void Decode_WritesPixelData_8Bit(byte[] inputData, int bitsPerSample, int
{
AssertDecode(expectedResult, pixels =>
{
new BlackIsZero8TiffColor<Rgba32>().Decode(inputData, pixels, left, top, width, height);
new BlackIsZero8TiffColor<Rgba32>(Configuration.Default).Decode(inputData, pixels, left, top, width, height);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void Decode_WritesPixelData_8Bit(byte[] inputData, TiffBitsPerSample bits
{
AssertDecode(expectedResult, pixels =>
{
new Rgb888TiffColor<Rgba32>().Decode(inputData, pixels, left, top, width, height);
new Rgb888TiffColor<Rgba32>(Configuration.Default).Decode(inputData, pixels, left, top, width, height);
});
}
}
Expand Down