Skip to content

Commit 71f7d86

Browse files
committed
Add method UndoTile in HorizontalPredictor
1 parent 9954f50 commit 71f7d86

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal sealed class DeflateTiffCompression : TiffBaseDecompressor
2424

2525
private readonly bool isTiled;
2626

27+
private readonly int tileWidth;
28+
2729
/// <summary>
2830
/// Initializes a new instance of the <see cref="DeflateTiffCompression" /> class.
2931
/// </summary>
@@ -34,12 +36,14 @@ internal sealed class DeflateTiffCompression : TiffBaseDecompressor
3436
/// <param name="predictor">The tiff predictor used.</param>
3537
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
3638
/// <param name="isTiled">Flag indicates, if the image is a tiled image.</param>
37-
public DeflateTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffColorType colorType, TiffPredictor predictor, bool isBigEndian, bool isTiled)
39+
/// <param name="tileWidth">Number of pixels in a tile row.</param>
40+
public DeflateTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffColorType colorType, TiffPredictor predictor, bool isBigEndian, bool isTiled, int tileWidth)
3841
: base(memoryAllocator, width, bitsPerPixel, predictor)
3942
{
4043
this.colorType = colorType;
4144
this.isBigEndian = isBigEndian;
4245
this.isTiled = isTiled;
46+
this.tileWidth = tileWidth;
4347
}
4448

4549
/// <inheritdoc/>
@@ -73,9 +77,16 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
7377
}
7478

7579
// When the image is tiled, undoing the horizontal predictor will be done for each tile row in the DecodeTilesChunky() method.
76-
if (this.Predictor == TiffPredictor.Horizontal && !this.isTiled)
80+
if (this.Predictor == TiffPredictor.Horizontal)
7781
{
78-
HorizontalPredictor.Undo(buffer, this.Width, this.colorType, this.isBigEndian);
82+
if (this.isTiled)
83+
{
84+
HorizontalPredictor.UndoTile(buffer, this.tileWidth, this.colorType, this.isBigEndian);
85+
}
86+
else
87+
{
88+
HorizontalPredictor.Undo(buffer, this.Width, this.colorType, this.isBigEndian);
89+
}
7990
}
8091
}
8192

src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public static void Undo(Span<byte> pixelBytes, int width, TiffColorType colorTyp
6262
}
6363
}
6464

65+
public static void UndoTile(Span<byte> pixelBytes, int tileWidth, TiffColorType colorType, bool isBigEndian)
66+
{
67+
for (int y = 0; y < tileWidth; y++)
68+
{
69+
UndoRow(pixelBytes, tileWidth, y, colorType, isBigEndian);
70+
}
71+
}
72+
6573
/// <summary>
6674
/// Inverts the horizontal predictor for one row.
6775
/// </summary>

src/ImageSharp/Formats/Tiff/Compression/TiffDecompressorsFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors;
55
using SixLabors.ImageSharp.Formats.Tiff.Constants;
66
using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
7-
using SixLabors.ImageSharp.Formats.Webp;
87
using SixLabors.ImageSharp.Memory;
98

109
namespace SixLabors.ImageSharp.Formats.Tiff.Compression;
@@ -25,7 +24,8 @@ public static TiffBaseDecompressor Create(
2524
uint oldJpegStartOfImageMarker,
2625
TiffFillOrder fillOrder,
2726
ByteOrder byteOrder,
28-
bool isTiled = false)
27+
bool isTiled = false,
28+
int tileWidth = 0)
2929
{
3030
switch (method)
3131
{
@@ -41,7 +41,7 @@ public static TiffBaseDecompressor Create(
4141

4242
case TiffDecoderCompressionType.Deflate:
4343
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "No fax compression options are expected");
44-
return new DeflateTiffCompression(allocator, width, bitsPerPixel, colorType, predictor, byteOrder == ByteOrder.BigEndian, isTiled);
44+
return new DeflateTiffCompression(allocator, width, bitsPerPixel, colorType, predictor, byteOrder == ByteOrder.BigEndian, isTiled, tileWidth);
4545

4646
case TiffDecoderCompressionType.Lzw:
4747
DebugGuard.IsTrue(faxOptions == FaxCompressionOptions.None, "No fax compression options are expected");

src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,7 @@ private void DecodeTilesChunky<TPixel>(
679679
using IMemoryOwner<byte> tileBuffer = this.memoryAllocator.Allocate<byte>(bytesPerTileRow * tileLength, AllocationOptions.Clean);
680680
Span<byte> tileBufferSpan = tileBuffer.GetSpan();
681681

682-
bool isTiled = true;
683-
using TiffBaseDecompressor decompressor = this.CreateDecompressor<TPixel>(frame.Width, bitsPerPixel, isTiled);
682+
using TiffBaseDecompressor decompressor = this.CreateDecompressor<TPixel>(frame.Width, bitsPerPixel, true, tileWidth);
684683
TiffBaseColorDecoder<TPixel> colorDecoder = this.CreateChunkyColorDecoder<TPixel>();
685684

686685
int tileIndex = 0;
@@ -748,7 +747,7 @@ private TiffBasePlanarColorDecoder<TPixel> CreatePlanarColorDecoder<TPixel>()
748747
this.YcbcrSubSampling,
749748
this.byteOrder);
750749

751-
private TiffBaseDecompressor CreateDecompressor<TPixel>(int frameWidth, int bitsPerPixel, bool isTiled = false)
750+
private TiffBaseDecompressor CreateDecompressor<TPixel>(int frameWidth, int bitsPerPixel, bool isTiled = false, int tileWidth = 0)
752751
where TPixel : unmanaged, IPixel<TPixel> =>
753752
TiffDecompressorsFactory.Create(
754753
this.Options,
@@ -764,7 +763,8 @@ private TiffBaseDecompressor CreateDecompressor<TPixel>(int frameWidth, int bits
764763
this.OldJpegCompressionStartOfImageMarker.GetValueOrDefault(),
765764
this.FillOrder,
766765
this.byteOrder,
767-
isTiled);
766+
isTiled,
767+
tileWidth);
768768

769769
private IMemoryOwner<ulong> ConvertNumbers(Array array, out Span<ulong> span)
770770
{

tests/ImageSharp.Tests/Formats/Tiff/Compression/DeflateTiffCompressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void Compress_Decompress_Roundtrip_Works(byte[] data)
2323
using BufferedReadStream stream = CreateCompressedStream(data);
2424
byte[] buffer = new byte[data.Length];
2525

26-
using var decompressor = new DeflateTiffCompression(Configuration.Default.MemoryAllocator, 10, 8, TiffColorType.BlackIsZero8, TiffPredictor.None, false, false);
26+
using var decompressor = new DeflateTiffCompression(Configuration.Default.MemoryAllocator, 10, 8, TiffColorType.BlackIsZero8, TiffPredictor.None, false, false, 0);
2727

2828
decompressor.Decompress(stream, 0, (uint)stream.Length, 1, buffer, default);
2929

0 commit comments

Comments
 (0)