|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using SixLabors.ImageSharp.Formats; |
| 5 | +using SixLabors.ImageSharp.Memory; |
| 6 | +using SixLabors.ImageSharp.PixelFormats; |
| 7 | +using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
| 8 | +using static SixLabors.ImageSharp.Tests.TestImages.Tiff; |
| 9 | + |
| 10 | +namespace SixLabors.ImageSharp.Tests.Formats.Tiff; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Tests strip allocation and directory traversal with stored TIFF inputs. |
| 14 | +/// </summary> |
| 15 | +[Trait("Format", "Tiff")] |
| 16 | +[ValidateDisposedMemoryAllocations] |
| 17 | +public class TiffDecoderStripLayoutTests |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// A missing RowsPerStrip or a value greater than the image height describes one strip per plane. |
| 21 | + /// </summary> |
| 22 | + /// <param name="path">The stored TIFF input.</param> |
| 23 | + [Theory] |
| 24 | + [InlineData(ChunkyRowsPerStripMissing)] |
| 25 | + [InlineData(ChunkyRowsPerStripLarge)] |
| 26 | + [InlineData(ChunkyRowsPerStripMax)] |
| 27 | + [InlineData(PlanarRowsPerStripMissing)] |
| 28 | + [InlineData(PlanarRowsPerStripLarge)] |
| 29 | + [InlineData(PlanarRowsPerStripMax)] |
| 30 | + public void Decode_RowsPerStripMissingOrAboveHeight_DecodesAllRows(string path) |
| 31 | + { |
| 32 | + using Image<Rgb24> expected = Image.Load<Rgb24>(TestFile.Create(StripLayoutRgbReference).Bytes); |
| 33 | + byte[] data = TestFile.Create(path).Bytes; |
| 34 | + |
| 35 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 36 | + { |
| 37 | + DecoderOptions options = new() { SegmentIntegrityHandling = integrity }; |
| 38 | + using Image<Rgb24> actual = Image.Load<Rgb24>(options, data); |
| 39 | + |
| 40 | + ImageComparer.Exact.VerifySimilarity(expected, actual); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Reading an uncompressed strip in rows preserves every 16-bit component. |
| 46 | + /// </summary> |
| 47 | + /// <param name="path">The stored single-strip or multiple-strip input.</param> |
| 48 | + [Theory] |
| 49 | + [InlineData(ChunkyRgb16SingleStrip)] |
| 50 | + [InlineData(ChunkyRgb16MultipleStrips)] |
| 51 | + [InlineData(PlanarRgb16SingleStrip)] |
| 52 | + [InlineData(PlanarRgb16MultipleStrips)] |
| 53 | + public void Decode_UncompressedStripAboveBufferLimit_DecodesAllRows(string path) |
| 54 | + { |
| 55 | + byte[] data = TestFile.Create(path).Bytes; |
| 56 | + using Image<Rgb48> expected = Image.Load<Rgb48>(data); |
| 57 | + Configuration configuration = Configuration.Default.Clone(); |
| 58 | + configuration.MemoryAllocator = MemoryAllocator.Create(new MemoryAllocatorOptions { SingleBufferAllocationLimitMegabytes = 1 }); |
| 59 | + |
| 60 | + // Each full strip exceeds 1 MiB, including each separate plane. A row fits within the limit. |
| 61 | + // The final strip in the multiple-strip inputs contains only the remaining 100 rows. |
| 62 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 63 | + { |
| 64 | + DecoderOptions options = new() { Configuration = configuration, SegmentIntegrityHandling = integrity }; |
| 65 | + using Image<Rgb48> actual = Image.Load<Rgb48>(options, data); |
| 66 | + |
| 67 | + ImageComparer.Exact.VerifySimilarity(expected, actual); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Subsampled chroma planes require fewer stored samples than their expanded destination buffers. |
| 73 | + /// </summary> |
| 74 | + [Fact] |
| 75 | + public void Decode_SubsampledPlanarYCbCr_DoesNotRequireExpandedChromaInFile() |
| 76 | + { |
| 77 | + byte[] data = TestFile.Create(PlanarYCbCrSubsampled).Bytes; |
| 78 | + |
| 79 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 80 | + { |
| 81 | + DecoderOptions options = new() { SegmentIntegrityHandling = integrity }; |
| 82 | + using Image<Rgb24> image = Image.Load<Rgb24>(options, data); |
| 83 | + |
| 84 | + // Y = 64 with neutral Cb and Cr = 128 represents gray 64 in every pixel. |
| 85 | + Assert.Equal(new Size(4, 2), image.Size); |
| 86 | + for (int y = 0; y < image.Height; y++) |
| 87 | + { |
| 88 | + for (int x = 0; x < image.Width; x++) |
| 89 | + { |
| 90 | + Assert.Equal(new Rgb24(64, 64, 64), image[x, y]); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// A zero strip height is invalid in every integrity mode. |
| 98 | + /// </summary> |
| 99 | + /// <param name="path">The stored TIFF with RowsPerStrip set to zero.</param> |
| 100 | + [Theory] |
| 101 | + [InlineData(ChunkyRowsPerStripZero)] |
| 102 | + [InlineData(PlanarRowsPerStripZero)] |
| 103 | + public void Decode_ZeroRowsPerStrip_ThrowsImageFormatException(string path) |
| 104 | + { |
| 105 | + byte[] data = TestFile.Create(path).Bytes; |
| 106 | + |
| 107 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 108 | + { |
| 109 | + DecoderOptions options = new() { SegmentIntegrityHandling = integrity }; |
| 110 | + Assert.Throws<ImageFormatException>(() => |
| 111 | + { |
| 112 | + using Image<Rgb24> image = Image.Load<Rgb24>(options, data); |
| 113 | + }); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Short image data follows the integrity policy without reading past the declared strip or reusing old samples. |
| 119 | + /// </summary> |
| 120 | + /// <param name="path">The stored TIFF whose strip ends halfway through its second row.</param> |
| 121 | + /// <param name="limitBuffer">Whether to force row-by-row reads.</param> |
| 122 | + [Theory] |
| 123 | + [InlineData(ChunkyRgb16ShortStrip, false)] |
| 124 | + [InlineData(ChunkyRgb16ShortStrip, true)] |
| 125 | + [InlineData(PlanarRgb16ShortStrip, false)] |
| 126 | + [InlineData(PlanarRgb16ShortStrip, true)] |
| 127 | + public void Decode_ShortUncompressedStrip_UsesImageDataIntegrityPolicy(string path, bool limitBuffer) |
| 128 | + { |
| 129 | + byte[] data = TestFile.Create(path).Bytes; |
| 130 | + Configuration configuration = Configuration.Default.Clone(); |
| 131 | + if (limitBuffer) |
| 132 | + { |
| 133 | + configuration.MemoryAllocator = MemoryAllocator.Create(new MemoryAllocatorOptions { SingleBufferAllocationLimitMegabytes = 1 }); |
| 134 | + } |
| 135 | + |
| 136 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 137 | + { |
| 138 | + DecoderOptions options = new() { Configuration = configuration, SegmentIntegrityHandling = integrity }; |
| 139 | + if (integrity != SegmentIntegrityHandling.IgnoreImageData) |
| 140 | + { |
| 141 | + Assert.Throws<InvalidImageContentException>(() => |
| 142 | + { |
| 143 | + using Image<Rgb48> image = Image.Load<Rgb48>(options, data); |
| 144 | + }); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + using Image<Rgb48> image = Image.Load<Rgb48>(options, data); |
| 149 | + |
| 150 | + // The fixtures contain more bytes after StripByteCounts. Those bytes must not become image samples. |
| 151 | + // Samples before the declared end survive. Missing samples are zero in both read paths. |
| 152 | + Assert.Equal(new Rgb48(0, 13621, 27242), image[0, 0]); |
| 153 | + Assert.Equal(new Rgb48(25957, 39578, 53199), image[0, 1]); |
| 154 | + Assert.Equal(default, image[1023, 1]); |
| 155 | + Assert.Equal(default, image[0, 2]); |
| 156 | + Assert.Equal(default, image[1023, 699]); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// A failed image allocation reports the declared dimensions. |
| 163 | + /// </summary> |
| 164 | + [Fact] |
| 165 | + public void Decode_FrameAboveAllocationLimit_ThrowsWithRealDimensions() |
| 166 | + { |
| 167 | + byte[] data = TestFile.Create(FrameAboveAllocationLimit).Bytes; |
| 168 | + Configuration configuration = Configuration.Default.Clone(); |
| 169 | + configuration.MemoryAllocator = MemoryAllocator.Create(new MemoryAllocatorOptions { AllocationLimitMegabytes = 100 }); |
| 170 | + DecoderOptions options = new() { Configuration = configuration }; |
| 171 | + |
| 172 | + InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() => |
| 173 | + { |
| 174 | + using Image<L8> image = Image.Load<L8>(options, data); |
| 175 | + }); |
| 176 | + |
| 177 | + Assert.Contains("20000x15000", ex.Message, StringComparison.Ordinal); |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// Directory links determine frame order even when their file offsets decrease. |
| 182 | + /// </summary> |
| 183 | + /// <param name="path">The stored TIFF or BigTIFF with reversed directory offsets.</param> |
| 184 | + [Theory] |
| 185 | + [InlineData(ReverseDirectories)] |
| 186 | + [InlineData(BigTiffReverseDirectories)] |
| 187 | + public void Decode_DirectoriesStoredInReverseFileOrder_DecodesAllFrames(string path) |
| 188 | + { |
| 189 | + byte[] data = TestFile.Create(path).Bytes; |
| 190 | + |
| 191 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 192 | + { |
| 193 | + DecoderOptions options = new() { SegmentIntegrityHandling = integrity }; |
| 194 | + using Image<L8> image = Image.Load<L8>(options, data); |
| 195 | + ImageInfo info = Image.Identify(options, data); |
| 196 | + |
| 197 | + Assert.Equal(2, image.Frames.Count); |
| 198 | + Assert.Equal(2, info.FrameMetadataCollection.Count); |
| 199 | + Assert.Equal(new Size(2, 2), image.Size); |
| 200 | + Assert.Equal(new L8(10), image.Frames[0][0, 0]); |
| 201 | + Assert.Equal(new L8(11), image.Frames[0][1, 0]); |
| 202 | + Assert.Equal(new L8(12), image.Frames[0][0, 1]); |
| 203 | + Assert.Equal(new L8(13), image.Frames[0][1, 1]); |
| 204 | + Assert.Equal(new L8(20), image.Frames[1][0, 0]); |
| 205 | + Assert.Equal(new L8(21), image.Frames[1][1, 0]); |
| 206 | + Assert.Equal(new L8(22), image.Frames[1][0, 1]); |
| 207 | + Assert.Equal(new L8(23), image.Frames[1][1, 1]); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + /// <summary> |
| 212 | + /// A repeated directory offset is a structural error in every integrity mode. |
| 213 | + /// </summary> |
| 214 | + /// <param name="path">The stored TIFF or BigTIFF with a circular directory chain.</param> |
| 215 | + [Theory] |
| 216 | + [InlineData(DirectoryLoopOne)] |
| 217 | + [InlineData(DirectoryLoopTwo)] |
| 218 | + [InlineData(DirectoryLoopTwoReverse)] |
| 219 | + [InlineData(DirectoryLoopThreeReverse)] |
| 220 | + [InlineData(BigTiffDirectoryLoopOne)] |
| 221 | + [InlineData(BigTiffDirectoryLoopTwo)] |
| 222 | + [InlineData(BigTiffDirectoryLoopTwoReverse)] |
| 223 | + [InlineData(BigTiffDirectoryLoopThreeReverse)] |
| 224 | + public void Decode_DirectoryChainRevisitsDirectory_ThrowsImageFormatException(string path) |
| 225 | + { |
| 226 | + byte[] data = TestFile.Create(path).Bytes; |
| 227 | + |
| 228 | + foreach (SegmentIntegrityHandling integrity in Enum.GetValues<SegmentIntegrityHandling>()) |
| 229 | + { |
| 230 | + DecoderOptions options = new() { SegmentIntegrityHandling = integrity }; |
| 231 | + ImageFormatException identifyError = Assert.Throws<ImageFormatException>(() => Image.Identify(options, data)); |
| 232 | + ImageFormatException decodeError = Assert.Throws<ImageFormatException>(() => |
| 233 | + { |
| 234 | + using Image<L8> image = Image.Load<L8>(options, data); |
| 235 | + }); |
| 236 | + |
| 237 | + Assert.Contains("circular", identifyError.Message, StringComparison.Ordinal); |
| 238 | + Assert.Contains("circular", decodeError.Message, StringComparison.Ordinal); |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments