Skip to content

Commit 850e878

Browse files
Merge pull request #2914 from SixLabors/backport/v3-2878
V3: Undo horizontal prediction for each tile row in case of tiled tiff's
2 parents 9593e08 + 1cc3588 commit 850e878

File tree

44 files changed

+806
-369
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+806
-369
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ internal sealed class DeflateTiffCompression : TiffBaseDecompressor
2222

2323
private readonly TiffColorType colorType;
2424

25+
private readonly bool isTiled;
26+
27+
private readonly int tileWidth;
28+
29+
private readonly int tileHeight;
30+
2531
/// <summary>
2632
/// Initializes a new instance of the <see cref="DeflateTiffCompression" /> class.
2733
/// </summary>
@@ -31,11 +37,17 @@ internal sealed class DeflateTiffCompression : TiffBaseDecompressor
3137
/// <param name="colorType">The color type of the pixel data.</param>
3238
/// <param name="predictor">The tiff predictor used.</param>
3339
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
34-
public DeflateTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffColorType colorType, TiffPredictor predictor, bool isBigEndian)
40+
/// <param name="isTiled">Flag indicates, if the image is a tiled image.</param>
41+
/// <param name="tileWidth">Number of pixels in a tile row.</param>
42+
/// <param name="tileHeight">Number of rows in a tile.</param>
43+
public DeflateTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffColorType colorType, TiffPredictor predictor, bool isBigEndian, bool isTiled, int tileWidth, int tileHeight)
3544
: base(memoryAllocator, width, bitsPerPixel, predictor)
3645
{
3746
this.colorType = colorType;
3847
this.isBigEndian = isBigEndian;
48+
this.isTiled = isTiled;
49+
this.tileWidth = tileWidth;
50+
this.tileHeight = tileHeight;
3951
}
4052

4153
/// <inheritdoc/>
@@ -70,7 +82,15 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
7082

7183
if (this.Predictor == TiffPredictor.Horizontal)
7284
{
73-
HorizontalPredictor.Undo(buffer, this.Width, this.colorType, this.isBigEndian);
85+
if (this.isTiled)
86+
{
87+
// When the image is tiled, undoing the horizontal predictor will be done for each tile row.
88+
HorizontalPredictor.UndoTile(buffer, this.tileWidth, this.tileHeight, this.colorType, this.isBigEndian);
89+
}
90+
else
91+
{
92+
HorizontalPredictor.Undo(buffer, this.Width, this.colorType, this.isBigEndian);
93+
}
7494
}
7595
}
7696

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ internal sealed class LzwTiffCompression : TiffBaseDecompressor
1717

1818
private readonly TiffColorType colorType;
1919

20+
private readonly bool isTiled;
21+
22+
private readonly int tileWidth;
23+
24+
private readonly int tileHeight;
25+
2026
/// <summary>
2127
/// Initializes a new instance of the <see cref="LzwTiffCompression" /> class.
2228
/// </summary>
@@ -26,11 +32,17 @@ internal sealed class LzwTiffCompression : TiffBaseDecompressor
2632
/// <param name="colorType">The color type of the pixel data.</param>
2733
/// <param name="predictor">The tiff predictor used.</param>
2834
/// <param name="isBigEndian">if set to <c>true</c> decodes the pixel data as big endian, otherwise as little endian.</param>
29-
public LzwTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffColorType colorType, TiffPredictor predictor, bool isBigEndian)
35+
/// <param name="isTiled">Flag indicates, if the image is a tiled image.</param>
36+
/// <param name="tileWidth">Number of pixels in a tile row.</param>
37+
/// <param name="tileHeight">Number of rows in a tile.</param>
38+
public LzwTiffCompression(MemoryAllocator memoryAllocator, int width, int bitsPerPixel, TiffColorType colorType, TiffPredictor predictor, bool isBigEndian, bool isTiled, int tileWidth, int tileHeight)
3039
: base(memoryAllocator, width, bitsPerPixel, predictor)
3140
{
3241
this.colorType = colorType;
3342
this.isBigEndian = isBigEndian;
43+
this.isTiled = isTiled;
44+
this.tileWidth = tileWidth;
45+
this.tileHeight = tileHeight;
3446
}
3547

3648
/// <inheritdoc/>
@@ -41,7 +53,15 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
4153

4254
if (this.Predictor == TiffPredictor.Horizontal)
4355
{
44-
HorizontalPredictor.Undo(buffer, this.Width, this.colorType, this.isBigEndian);
56+
if (this.isTiled)
57+
{
58+
// When the image is tiled, undoing the horizontal predictor will be done for each tile row.
59+
HorizontalPredictor.UndoTile(buffer, this.tileWidth, this.tileHeight, this.colorType, this.isBigEndian);
60+
}
61+
else
62+
{
63+
HorizontalPredictor.Undo(buffer, this.Width, this.colorType, this.isBigEndian);
64+
}
4565
}
4666
}
4767

0 commit comments

Comments
 (0)