Skip to content

Commit e1c8b0f

Browse files
authored
Merge branch 'master' into bp/tiffLeastSignificantBitFirst
2 parents 1d8b910 + 566babf commit e1c8b0f

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.ConvertToShortLittleEndian(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.ConvertToShortLittleEndian(data.Slice(offset, 2));
59-
offset += 2;
60-
ulong g = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
61-
offset += 2;
62-
ulong b = TiffUtils.ConvertToShortLittleEndian(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
{
@@ -55,12 +55,12 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
5555
case TiffColorType.BlackIsZero8:
5656
DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 8, "bitsPerSample");
5757
DebugGuard.IsTrue(colorMap == null, "colorMap");
58-
return new BlackIsZero8TiffColor<TPixel>();
58+
return new BlackIsZero8TiffColor<TPixel>(configuration);
5959

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

6565
case TiffColorType.Rgb:
6666
DebugGuard.IsTrue(colorMap == null, "colorMap");
@@ -94,7 +94,7 @@ public static TiffBaseColorDecoder<TPixel> Create(TiffColorType colorType, TiffB
9494
&& bitsPerSample.Channel0 == 8,
9595
"bitsPerSample");
9696
DebugGuard.IsTrue(colorMap == null, "colorMap");
97-
return new Rgb888TiffColor<TPixel>();
97+
return new Rgb888TiffColor<TPixel>(configuration);
9898

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

139139
case TiffColorType.PaletteColor:
140140
DebugGuard.NotNull(colorMap, "colorMap");

src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private void DecodeStripsChunky<TPixel>(ImageFrame<TPixel> frame, int rowsPerStr
330330
this.FaxCompressionOptions,
331331
this.FillOrder);
332332

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

335335
for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++)
336336
{

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)