Skip to content

Commit 47c6755

Browse files
committed
Add test cases for tiled 32 bit gray tiff's
1 parent f11fbc1 commit 47c6755

File tree

5 files changed

+64
-26
lines changed

5 files changed

+64
-26
lines changed

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

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ public static void UndoRow(Span<byte> pixelBytes, int width, int y, TiffColorTyp
8383
UndoGray16BitLittleEndianRow(pixelBytes, width, y);
8484
}
8585

86+
break;
87+
case TiffColorType.BlackIsZero32:
88+
case TiffColorType.WhiteIsZero32:
89+
if (isBigEndian)
90+
{
91+
UndoGray32BitBigEndianRow(pixelBytes, width, y);
92+
}
93+
else
94+
{
95+
UndoGray32BitLittleEndianRow(pixelBytes, width, y);
96+
}
97+
8698
break;
8799
case TiffColorType.Rgb888:
88100
case TiffColorType.CieLab:
@@ -266,6 +278,46 @@ private static void UndoGray16Bit(Span<byte> pixelBytes, int width, bool isBigEn
266278
}
267279
}
268280

281+
private static void UndoGray32BitBigEndianRow(Span<byte> pixelBytes, int width, int y)
282+
{
283+
int rowBytesCount = width * 4;
284+
int height = pixelBytes.Length / rowBytesCount;
285+
286+
int offset = 0;
287+
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
288+
uint pixelValue = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
289+
offset += 4;
290+
291+
for (int x = 1; x < width; x++)
292+
{
293+
Span<byte> rowSpan = rowBytes.Slice(offset, 4);
294+
uint diff = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
295+
pixelValue += diff;
296+
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, pixelValue);
297+
offset += 4;
298+
}
299+
}
300+
301+
private static void UndoGray32BitLittleEndianRow(Span<byte> pixelBytes, int width, int y)
302+
{
303+
int rowBytesCount = width * 4;
304+
int height = pixelBytes.Length / rowBytesCount;
305+
306+
int offset = 0;
307+
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
308+
uint pixelValue = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
309+
offset += 4;
310+
311+
for (int x = 1; x < width; x++)
312+
{
313+
Span<byte> rowSpan = rowBytes.Slice(offset, 4);
314+
uint diff = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
315+
pixelValue += diff;
316+
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, pixelValue);
317+
offset += 4;
318+
}
319+
}
320+
269321
private static void UndoGray32Bit(Span<byte> pixelBytes, int width, bool isBigEndian)
270322
{
271323
int rowBytesCount = width * 4;
@@ -274,38 +326,14 @@ private static void UndoGray32Bit(Span<byte> pixelBytes, int width, bool isBigEn
274326
{
275327
for (int y = 0; y < height; y++)
276328
{
277-
int offset = 0;
278-
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
279-
uint pixelValue = TiffUtilities.ConvertToUIntBigEndian(rowBytes.Slice(offset, 4));
280-
offset += 4;
281-
282-
for (int x = 1; x < width; x++)
283-
{
284-
Span<byte> rowSpan = rowBytes.Slice(offset, 4);
285-
uint diff = TiffUtilities.ConvertToUIntBigEndian(rowSpan);
286-
pixelValue += diff;
287-
BinaryPrimitives.WriteUInt32BigEndian(rowSpan, pixelValue);
288-
offset += 4;
289-
}
329+
UndoGray32BitBigEndianRow(pixelBytes, width, y);
290330
}
291331
}
292332
else
293333
{
294334
for (int y = 0; y < height; y++)
295335
{
296-
int offset = 0;
297-
Span<byte> rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount);
298-
uint pixelValue = TiffUtilities.ConvertToUIntLittleEndian(rowBytes.Slice(offset, 4));
299-
offset += 4;
300-
301-
for (int x = 1; x < width; x++)
302-
{
303-
Span<byte> rowSpan = rowBytes.Slice(offset, 4);
304-
uint diff = TiffUtilities.ConvertToUIntLittleEndian(rowSpan);
305-
pixelValue += diff;
306-
BinaryPrimitives.WriteUInt32LittleEndian(rowSpan, pixelValue);
307-
offset += 4;
308-
}
336+
UndoGray32BitLittleEndianRow(pixelBytes, width, y);
309337
}
310338
}
311339
}

tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public void TiffDecoder_CanDecode_Planar<TPixel>(TestImageProvider<TPixel> provi
9292
[WithFile(TiledGrayDeflateCompressedWithPredictor, PixelTypes.Rgba32)]
9393
[WithFile(TiledGray16BitLittleEndianDeflateCompressedWithPredictor, PixelTypes.Rgba32)]
9494
[WithFile(TiledGray16BitBigEndianDeflateCompressedWithPredictor, PixelTypes.Rgba32)]
95+
[WithFile(TiledGray32BitLittleEndianDeflateCompressedWithPredictor, PixelTypes.Rgba32)]
96+
[WithFile(TiledGray32BitBigEndianDeflateCompressedWithPredictor, PixelTypes.Rgba32)]
9597
public void TiffDecoder_CanDecode_Tiled<TPixel>(TestImageProvider<TPixel> provider)
9698
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
9799

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,8 @@ public static class Tiff
991991
public const string TiledGrayDeflateCompressedWithPredictor = "Tiff/tiled_gray_deflate_compressed_predictor.tiff";
992992
public const string TiledGray16BitLittleEndianDeflateCompressedWithPredictor = "Tiff/tiled_gray_16bit_little_endian_deflate_compressed_predictor.tiff";
993993
public const string TiledGray16BitBigEndianDeflateCompressedWithPredictor = "Tiff/tiled_gray_16bit_big_endian_deflate_compressed_predictor.tiff";
994+
public const string TiledGray32BitLittleEndianDeflateCompressedWithPredictor = "Tiff/tiled_gray_32bit_little_endian_deflate_compressed_predictor.tiff";
995+
public const string TiledGray32BitBigEndianDeflateCompressedWithPredictor = "Tiff/tiled_gray_32bit_big_endian_deflate_compressed_predictor.tiff";
994996

995997
// Images with alpha channel.
996998
public const string Rgba2BitUnassociatedAlpha = "Tiff/RgbaUnassociatedAlpha2bit.tiff";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:b674439ce46a39377279755ffa372c5bd863ac7787165e95590c0a9c6c5d681e
3+
size 82469
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:3acfecebb3db8b549b7a20a3fdd967ba5a4cbb95cbb9b4a59d332fa6ec809278
3+
size 82519

0 commit comments

Comments
 (0)