Skip to content

Commit 51e1870

Browse files
committed
Merge remote-tracking branch 'origin/master' into bp/tiff24bit
# Conflicts: # src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs # src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
2 parents 4e5dec9 + 566babf commit 51e1870

File tree

8 files changed

+62
-51
lines changed

8 files changed

+62
-51
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ internal class BlackIsZero16TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
1616
{
1717
private readonly bool isBigEndian;
1818

19+
private readonly Configuration configuration;
20+
1921
/// <summary>
2022
/// Initializes a new instance of the <see cref="BlackIsZero16TiffColor{TPixel}" /> class.
2123
/// </summary>
24+
/// <param name="configuration">The configuration.</param>
2225
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
23-
public BlackIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
26+
public BlackIsZero16TiffColor(Configuration configuration, bool isBigEndian)
27+
{
28+
this.configuration = configuration;
29+
this.isBigEndian = isBigEndian;
30+
}
2431

2532
/// <inheritdoc/>
2633
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
@@ -47,13 +54,14 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
4754
}
4855
else
4956
{
50-
for (int x = 0; x < pixelRow.Length; x++)
51-
{
52-
ushort intensity = TiffUtils.ConvertToUShortLittleEndian(data.Slice(offset, 2));
53-
offset += 2;
57+
int byteCount = pixelRow.Length * 2;
58+
PixelOperations<TPixel>.Instance.FromL16Bytes(
59+
this.configuration,
60+
data.Slice(offset, byteCount),
61+
pixelRow,
62+
pixelRow.Length);
5463

55-
pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
56-
}
64+
offset += byteCount;
5765
}
5866
}
5967
}

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

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

44
using System;
5-
using SixLabors.ImageSharp.Formats.Tiff.Utils;
65
using SixLabors.ImageSharp.Memory;
76
using SixLabors.ImageSharp.PixelFormats;
87

@@ -14,22 +13,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
1413
internal class BlackIsZero8TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
1514
where TPixel : unmanaged, IPixel<TPixel>
1615
{
16+
private readonly Configuration configuration;
17+
18+
public BlackIsZero8TiffColor(Configuration configuration) => this.configuration = configuration;
19+
1720
/// <inheritdoc/>
1821
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
1922
{
20-
var color = default(TPixel);
21-
2223
int offset = 0;
2324

24-
var l8 = default(L8);
2525
for (int y = top; y < top + height; y++)
2626
{
2727
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
28-
for (int x = 0; x < pixelRow.Length; x++)
29-
{
30-
byte intensity = data[offset++];
31-
pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color);
32-
}
28+
int byteCount = pixelRow.Length;
29+
PixelOperations<TPixel>.Instance.FromL8Bytes(
30+
this.configuration,
31+
data.Slice(offset, byteCount),
32+
pixelRow,
33+
pixelRow.Length);
34+
35+
offset += byteCount;
3336
}
3437
}
3538
}

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ internal class Rgb161616TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
1616
{
1717
private readonly bool isBigEndian;
1818

19+
private readonly Configuration configuration;
20+
1921
/// <summary>
2022
/// Initializes a new instance of the <see cref="Rgb161616TiffColor{TPixel}" /> class.
2123
/// </summary>
24+
/// <param name="configuration">The configuration.</param>
2225
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
23-
public Rgb161616TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
26+
public Rgb161616TiffColor(Configuration configuration, bool isBigEndian)
27+
{
28+
this.configuration = configuration;
29+
this.isBigEndian = isBigEndian;
30+
}
2431

2532
/// <inheritdoc/>
2633
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
@@ -53,17 +60,14 @@ public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, in
5360
}
5461
else
5562
{
56-
for (int x = 0; x < pixelRow.Length; x++)
57-
{
58-
ulong r = TiffUtils.ConvertToUShortLittleEndian(data.Slice(offset, 2));
59-
offset += 2;
60-
ulong g = TiffUtils.ConvertToUShortLittleEndian(data.Slice(offset, 2));
61-
offset += 2;
62-
ulong b = TiffUtils.ConvertToUShortLittleEndian(data.Slice(offset, 2));
63-
offset += 2;
63+
int byteCount = pixelRow.Length * 6;
64+
PixelOperations<TPixel>.Instance.FromRgb48Bytes(
65+
this.configuration,
66+
data.Slice(offset, byteCount),
67+
pixelRow,
68+
pixelRow.Length);
6469

65-
pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
66-
}
70+
offset += byteCount;
6771
}
6872
}
6973
}

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

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

44
using System;
5-
65
using SixLabors.ImageSharp.Memory;
76
using SixLabors.ImageSharp.PixelFormats;
87

@@ -14,29 +13,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
1413
internal class Rgb888TiffColor<TPixel> : TiffBaseColorDecoder<TPixel>
1514
where TPixel : unmanaged, IPixel<TPixel>
1615
{
16+
private readonly Configuration configuration;
17+
18+
public Rgb888TiffColor(Configuration configuration) => this.configuration = configuration;
19+
1720
/// <inheritdoc/>
1821
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)
1922
{
20-
var color = default(TPixel);
21-
2223
int offset = 0;
2324

24-
var rgba = default(Rgba32);
2525
for (int y = top; y < top + height; y++)
2626
{
2727
Span<TPixel> pixelRow = pixels.GetRowSpan(y).Slice(left, width);
28-
29-
for (int x = 0; x < pixelRow.Length; x++)
30-
{
31-
byte r = data[offset++];
32-
byte g = data[offset++];
33-
byte b = data[offset++];
34-
35-
rgba.PackedValue = (uint)(r | (g << 8) | (b << 16) | (0xff << 24));
36-
color.FromRgba32(rgba);
37-
38-
pixelRow[x] = color;
39-
}
28+
int byteCount = pixelRow.Length * 3;
29+
PixelOperations<TPixel>.Instance.FromRgb24Bytes(
30+
this.configuration,
31+
data.Slice(offset, byteCount),
32+
pixelRow,
33+
pixelRow.Length);
34+
35+
offset += byteCount;
4036
}
4137
}
4238
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
88
internal static class TiffColorDecoderFactory<TPixel>
99
where TPixel : unmanaged, IPixel<TPixel>
1010
{
11-
public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder)
11+
public static TiffBaseColorDecoder<TPixel> Create(Configuration configuration, TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder)
1212
{
1313
switch (colorType)
1414
{
@@ -65,12 +65,12 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
6565
case TiffColorType.BlackIsZero8:
6666
DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 8, "bitsPerSample");
6767
DebugGuard.IsTrue(colorMap == null, "colorMap");
68-
return new BlackIsZero8TiffColor<TPixel>();
68+
return new BlackIsZero8TiffColor<TPixel>(configuration);
6969

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

7575
case TiffColorType.BlackIsZero24:
7676
DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 24, "bitsPerSample");
@@ -114,7 +114,7 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
114114
&& bitsPerSample.Channel0 == 8,
115115
"bitsPerSample");
116116
DebugGuard.IsTrue(colorMap == null, "colorMap");
117-
return new Rgb888TiffColor<TPixel>();
117+
return new Rgb888TiffColor<TPixel>(configuration);
118118

119119
case TiffColorType.Rgb101010:
120120
DebugGuard.IsTrue(
@@ -154,7 +154,7 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
154154
&& bitsPerSample.Channel0 == 16,
155155
"bitsPerSample");
156156
DebugGuard.IsTrue(colorMap == null, "colorMap");
157-
return new Rgb161616TiffColor<TPixel>(isBigEndian: byteOrder == ByteOrder.BigEndian);
157+
return new Rgb161616TiffColor<TPixel>(configuration, isBigEndian: byteOrder == ByteOrder.BigEndian);
158158

159159
case TiffColorType.Rgb242424:
160160
DebugGuard.IsTrue(

src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private void DecodeStripsChunky<TPixel>(ImageFrame<TPixel> frame, int rowsPerStr
316316
this.Predictor,
317317
this.FaxCompressionOptions);
318318

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

321321
for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++)
322322
{

tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void Decode_WritesPixelData_8Bit(byte[] inputData, int bitsPerSample, int
188188
{
189189
AssertDecode(expectedResult, pixels =>
190190
{
191-
new BlackIsZero8TiffColor<Rgba32>().Decode(inputData, pixels, left, top, width, height);
191+
new BlackIsZero8TiffColor<Rgba32>(Configuration.Default).Decode(inputData, pixels, left, top, width, height);
192192
});
193193
}
194194
}

tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void Decode_WritesPixelData_8Bit(byte[] inputData, TiffBitsPerSample bits
179179
{
180180
AssertDecode(expectedResult, pixels =>
181181
{
182-
new Rgb888TiffColor<Rgba32>().Decode(inputData, pixels, left, top, width, height);
182+
new Rgb888TiffColor<Rgba32>(Configuration.Default).Decode(inputData, pixels, left, top, width, height);
183183
});
184184
}
185185
}

0 commit comments

Comments
 (0)