88using SixLabors . ImageSharp . Formats . Jpeg ;
99using SixLabors . ImageSharp . Formats . Png ;
1010using SixLabors . ImageSharp . Formats . Tiff ;
11+ using SixLabors . ImageSharp . Formats . Tiff . Constants ;
1112using SixLabors . ImageSharp . Formats . Webp ;
1213using SixLabors . ImageSharp . PixelFormats ;
1314using SixLabors . ImageSharp . Processing ;
2122using System . Net . Http ;
2223using System . Runtime . CompilerServices ;
2324using System . Runtime . InteropServices ;
24- using System . Runtime . InteropServices . ComTypes ;
2525using System . Threading . Tasks ;
2626
2727namespace IronSoftware . Drawing
@@ -50,6 +50,7 @@ public partial class AnyBitmap : IDisposable
5050 private Image Image { get ; set ; }
5151 private byte [ ] Binary { get ; set ; }
5252 private IImageFormat Format { get ; set ; }
53+ private TiffCompression TiffCompression { get ; set ; } = TiffCompression . Lzw ;
5354
5455 /// <summary>
5556 /// Width of the image.
@@ -292,7 +293,10 @@ public void ExportStream(
292293 ImageFormat . Gif => new GifEncoder ( ) ,
293294 ImageFormat . Png => new PngEncoder ( ) ,
294295 ImageFormat . Webp => new WebpEncoder ( ) { Quality = lossy } ,
295- ImageFormat . Tiff => new TiffEncoder ( ) ,
296+ ImageFormat . Tiff => new TiffEncoder ( )
297+ {
298+ Compression = TiffCompression
299+ } ,
296300 _ => new BmpEncoder ( )
297301 {
298302 BitsPerPixel = BmpBitsPerPixel . Pixel32 ,
@@ -1321,7 +1325,7 @@ public static implicit operator Image(AnyBitmap bitmap)
13211325 {
13221326 try
13231327 {
1324- return Image . Load < Rgba32 > ( bitmap . Binary ) ;
1328+ return Image . Load ( bitmap . Binary ) ;
13251329 }
13261330 catch ( DllNotFoundException e )
13271331 {
@@ -2203,23 +2207,25 @@ private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes)
22032207 using MemoryStream tiffStream = new ( bytes . ToArray ( ) ) ;
22042208
22052209 // open a TIFF stored in the stream
2206- using ( Tiff tif = Tiff . ClientOpen ( "in-memory" , "r" , tiffStream , new TiffStream ( ) ) )
2210+ using ( Tiff tiff = Tiff . ClientOpen ( "in-memory" , "r" , tiffStream , new TiffStream ( ) ) )
22072211 {
2208- short num = tif . NumberOfDirectories ( ) ;
2212+ SetTiffCompression ( tiff ) ;
2213+
2214+ short num = tiff . NumberOfDirectories ( ) ;
22092215 for ( short i = 0 ; i < num ; i ++ )
22102216 {
2211- _ = tif . SetDirectory ( i ) ;
2217+ _ = tiff . SetDirectory ( i ) ;
22122218
2213- if ( IsThumbnail ( tif ) )
2219+ if ( IsThumbnail ( tiff ) )
22142220 {
22152221 continue ;
22162222 }
22172223
2218- var ( width , height ) = SetWidthHeight ( tif , i , ref imageWidth , ref imageHeight ) ;
2224+ var ( width , height ) = SetWidthHeight ( tiff , i , ref imageWidth , ref imageHeight ) ;
22192225
22202226 // Read the image into the memory buffer
22212227 int [ ] raster = new int [ height * width ] ;
2222- if ( ! tif . ReadRGBAImage ( width , height , raster ) )
2228+ if ( ! tiff . ReadRGBAImage ( width , height , raster ) )
22232229 {
22242230 throw new NotSupportedException ( "Could not read image" ) ;
22252231 }
@@ -2231,7 +2237,7 @@ private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes)
22312237 images . Add ( Image . LoadPixelData < Rgba32 > ( bits , bmp . Width , bmp . Height ) ) ;
22322238 }
22332239 }
2234-
2240+
22352241 // find max
22362242 FindMaxWidthAndHeight ( images , out int maxWidth , out int maxHeight ) ;
22372243
@@ -2259,7 +2265,7 @@ private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes)
22592265
22602266 // dispose images past the first
22612267 images [ i ] . Dispose ( ) ;
2262- }
2268+ }
22632269
22642270 // get raw binary
22652271 using var memoryStream = new MemoryStream ( ) ;
@@ -2281,14 +2287,35 @@ private void OpenTiffToImageSharp(ReadOnlySpan<byte> bytes)
22812287 }
22822288 }
22832289
2290+ private void SetTiffCompression ( Tiff tiff )
2291+ {
2292+ Compression tiffCompression = tiff . GetField ( TiffTag . COMPRESSION ) != null && tiff . GetField ( TiffTag . COMPRESSION ) . Length > 0
2293+ ? ( Compression ) tiff . GetField ( TiffTag . COMPRESSION ) [ 0 ] . ToInt ( )
2294+ : Compression . NONE ;
2295+
2296+ TiffCompression = tiffCompression switch
2297+ {
2298+ Compression . CCITTRLE => TiffCompression . Ccitt1D ,
2299+ Compression . CCITTFAX3 => TiffCompression . CcittGroup3Fax ,
2300+ Compression . CCITTFAX4 => TiffCompression . CcittGroup4Fax ,
2301+ Compression . JPEG => TiffCompression . Jpeg ,
2302+ Compression . OJPEG => TiffCompression . OldJpeg ,
2303+ Compression . NEXT => TiffCompression . NeXT ,
2304+ Compression . PACKBITS => TiffCompression . PackBits ,
2305+ Compression . THUNDERSCAN => TiffCompression . ThunderScan ,
2306+ Compression . DEFLATE => TiffCompression . Deflate ,
2307+ _ => TiffCompression . Lzw
2308+ } ;
2309+ }
2310+
22842311 /// <summary>
22852312 /// Determines if a TIFF frame contains a thumbnail.
22862313 /// </summary>
2287- /// <param name="tif ">The <see cref="Tiff"/> which set number of directory to analyze.</param>
2314+ /// <param name="tiff ">The <see cref="Tiff"/> which set number of directory to analyze.</param>
22882315 /// <returns>True if the frame contains a thumbnail, otherwise false.</returns>
2289- private bool IsThumbnail ( Tiff tif )
2316+ private bool IsThumbnail ( Tiff tiff )
22902317 {
2291- FieldValue [ ] subFileTypeFieldValue = tif . GetField ( TiffTag . SUBFILETYPE ) ;
2318+ FieldValue [ ] subFileTypeFieldValue = tiff . GetField ( TiffTag . SUBFILETYPE ) ;
22922319
22932320 // Current thumbnail identification relies on the SUBFILETYPE tag with a value of FileType.REDUCEDIMAGE.
22942321 // This may need refinement in the future to include additional checks
@@ -2319,13 +2346,13 @@ private ReadOnlySpan<byte> PrepareByteArray(Image<Rgba32> bmp, int[] raster, int
23192346 return bits ;
23202347 }
23212348
2322- private ( int width , int height ) SetWidthHeight ( Tiff tif , short index , ref int imageWidth , ref int imageHeight )
2349+ private ( int width , int height ) SetWidthHeight ( Tiff tiff , short index , ref int imageWidth , ref int imageHeight )
23232350 {
23242351 // Find the width and height of the image
2325- FieldValue [ ] value = tif . GetField ( TiffTag . IMAGEWIDTH ) ;
2352+ FieldValue [ ] value = tiff . GetField ( TiffTag . IMAGEWIDTH ) ;
23262353 int width = value [ 0 ] . ToInt ( ) ;
23272354
2328- value = tif . GetField ( TiffTag . IMAGELENGTH ) ;
2355+ value = tiff . GetField ( TiffTag . IMAGELENGTH ) ;
23292356 int height = value [ 0 ] . ToInt ( ) ;
23302357
23312358 if ( index == 0 )
0 commit comments