From 15ef2d9e7f52c865f499b61de65e17f61246bc21 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Thu, 12 Aug 2021 07:38:57 +0200 Subject: [PATCH] Change rows per strip calculation: Jpeg = one strip, compression = use larger strip size --- src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index d7c9848a44..16b3158a4d 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -223,7 +223,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff entriesCollector, (int)this.BitsPerPixel); - int rowsPerStrip = this.CalcRowsPerStrip(frame.Height, colorWriter.BytesPerRow); + int rowsPerStrip = this.CalcRowsPerStrip(frame.Height, colorWriter.BytesPerRow, this.CompressionType); colorWriter.Write(compressor, rowsPerStrip); @@ -245,13 +245,22 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// /// The height of the image. /// The number of bytes per row. + /// The compression used. /// Number of rows per strip. - private int CalcRowsPerStrip(int height, int bytesPerRow) + private int CalcRowsPerStrip(int height, int bytesPerRow, TiffCompression? compression) { DebugGuard.MustBeGreaterThan(height, 0, nameof(height)); DebugGuard.MustBeGreaterThan(bytesPerRow, 0, nameof(bytesPerRow)); - int rowsPerStrip = TiffConstants.DefaultStripSize / bytesPerRow; + // Jpeg compressed images should be written in one strip. + if (compression is TiffCompression.Jpeg) + { + return height; + } + + // If compression is used, change stripSizeInBytes heuristically to a larger value to not write to many strips. + int stripSizeInBytes = compression is TiffCompression.Deflate || compression is TiffCompression.Lzw ? TiffConstants.DefaultStripSize * 2 : TiffConstants.DefaultStripSize; + int rowsPerStrip = stripSizeInBytes / bytesPerRow; if (rowsPerStrip > 0) {