22// Licensed under the Apache License, Version 2.0.
33
44using System . IO ;
5+
56using SixLabors . ImageSharp . Formats . Bmp ;
67using SixLabors . ImageSharp . Metadata ;
78using SixLabors . ImageSharp . PixelFormats ;
89using SixLabors . ImageSharp . Processing ;
10+ using SixLabors . ImageSharp . Tests . TestUtilities . ImageComparison ;
911
1012using Xunit ;
1113using Xunit . Abstractions ;
@@ -110,11 +112,7 @@ public void Encode_WorksWithDifferentSizes<TPixel>(TestImageProvider<TPixel> pro
110112 [ WithFile ( Bit32Rgba , PixelTypes . Rgba32 | PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel32 ) ]
111113 [ WithFile ( WinBmpv4 , PixelTypes . Rgba32 | PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel32 ) ]
112114 [ WithFile ( WinBmpv5 , PixelTypes . Rgba32 | PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel32 ) ]
113- // WinBmpv3 is a 24 bits per pixel image
114- [ WithFile ( WinBmpv3 , PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel24 ) ]
115- [ WithFile ( Rgb16 , PixelTypes . Bgra5551 , BmpBitsPerPixel . Pixel16 ) ]
116- [ WithFile ( Bit16 , PixelTypes . Bgra5551 , BmpBitsPerPixel . Pixel16 ) ]
117- public void Encode_WithV3Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
115+ public void Encode_32Bit_WithV3Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
118116 // if supportTransparency is false, a v3 bitmap header will be written
119117 where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : false ) ;
120118
@@ -123,32 +121,92 @@ public void Encode_WithV3Header_Works<TPixel>(TestImageProvider<TPixel> provider
123121 [ WithFile ( Bit32Rgba , PixelTypes . Rgba32 | PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel32 ) ]
124122 [ WithFile ( WinBmpv4 , PixelTypes . Rgba32 | PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel32 ) ]
125123 [ WithFile ( WinBmpv5 , PixelTypes . Rgba32 | PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel32 ) ]
124+ public void Encode_32Bit_WithV4Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
125+ where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : true ) ;
126+
127+ [ Theory ]
128+ // WinBmpv3 is a 24 bits per pixel image
129+ [ WithFile ( WinBmpv3 , PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel24 ) ]
130+ [ WithFile ( F , PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel24 ) ]
131+ public void Encode_24Bit_WithV3Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
132+ where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : false ) ;
133+
134+ [ Theory ]
126135 [ WithFile ( WinBmpv3 , PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel24 ) ]
136+ [ WithFile ( F , PixelTypes . Rgb24 , BmpBitsPerPixel . Pixel24 ) ]
137+ public void Encode_24Bit_WithV4Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
138+ where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : true ) ;
139+
140+
141+ [ Theory ]
142+ [ WithFile ( Rgb16 , PixelTypes . Bgra5551 , BmpBitsPerPixel . Pixel16 ) ]
143+ [ WithFile ( Bit16 , PixelTypes . Bgra5551 , BmpBitsPerPixel . Pixel16 ) ]
144+ public void Encode_16Bit_WithV3Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
145+ where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : false ) ;
146+
147+ [ Theory ]
127148 [ WithFile ( Rgb16 , PixelTypes . Bgra5551 , BmpBitsPerPixel . Pixel16 ) ]
128149 [ WithFile ( Bit16 , PixelTypes . Bgra5551 , BmpBitsPerPixel . Pixel16 ) ]
129- public void Encode_WithV4Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
150+ public void Encode_16Bit_WithV4Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
151+ where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : true ) ;
152+
153+ [ Theory ]
154+ [ WithFile ( WinBmpv5 , PixelTypes . Rgba32 , BmpBitsPerPixel . Pixel8 ) ]
155+ [ WithFile ( Bit8Palette4 , PixelTypes . Rgba32 , BmpBitsPerPixel . Pixel8 ) ]
156+ public void Encode_8Bit_WithV3Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
157+ where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : false ) ;
158+
159+ [ Theory ]
160+ [ WithFile ( WinBmpv5 , PixelTypes . Rgba32 , BmpBitsPerPixel . Pixel8 ) ]
161+ [ WithFile ( Bit8Palette4 , PixelTypes . Rgba32 , BmpBitsPerPixel . Pixel8 ) ]
162+ public void Encode_8Bit_WithV4Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
130163 where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : true ) ;
131164
165+ [ Theory ]
166+ [ WithFile ( Bit8Gs , PixelTypes . Gray8 , BmpBitsPerPixel . Pixel8 ) ]
167+ public void Encode_8BitGray_WithV3Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
168+ where TPixel : struct , IPixel < TPixel > =>
169+ TestBmpEncoderCore (
170+ provider ,
171+ bitsPerPixel ,
172+ supportTransparency : false ,
173+ ImageComparer . TolerantPercentage ( 0.01f ) ) ;
174+
175+ [ Theory ]
176+ [ WithFile ( Bit8Gs , PixelTypes . Gray8 , BmpBitsPerPixel . Pixel8 ) ]
177+ public void Encode_8BitGray_WithV4Header_Works < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
178+ where TPixel : struct , IPixel < TPixel > =>
179+ TestBmpEncoderCore (
180+ provider ,
181+ bitsPerPixel ,
182+ supportTransparency : true ,
183+ ImageComparer . TolerantPercentage ( 0.01f ) ) ;
184+
132185 [ Theory ]
133186 [ WithFile ( TestImages . Png . GrayAlpha2BitInterlaced , PixelTypes . Rgba32 , BmpBitsPerPixel . Pixel32 ) ]
187+ [ WithFile ( Bit32Rgba , PixelTypes . Rgba32 , BmpBitsPerPixel . Pixel32 ) ]
134188 public void Encode_PreservesAlpha < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel )
135189 where TPixel : struct , IPixel < TPixel > => TestBmpEncoderCore ( provider , bitsPerPixel , supportTransparency : true ) ;
136190
137- private static void TestBmpEncoderCore < TPixel > ( TestImageProvider < TPixel > provider , BmpBitsPerPixel bitsPerPixel , bool supportTransparency = true )
191+ private static void TestBmpEncoderCore < TPixel > (
192+ TestImageProvider < TPixel > provider ,
193+ BmpBitsPerPixel bitsPerPixel ,
194+ bool supportTransparency = true ,
195+ ImageComparer customComparer = null )
138196 where TPixel : struct , IPixel < TPixel >
139197 {
140198 using ( Image < TPixel > image = provider . GetImage ( ) )
141199 {
142- // There is no alpha in bmp with 24 bits per pixels, so the reference image will be made opaque.
143- if ( bitsPerPixel == BmpBitsPerPixel . Pixel24 )
200+ // There is no alpha in bmp with less then 32 bits per pixels, so the reference image will be made opaque.
201+ if ( bitsPerPixel != BmpBitsPerPixel . Pixel32 )
144202 {
145203 image . Mutate ( c => c . MakeOpaque ( ) ) ;
146204 }
147205
148206 var encoder = new BmpEncoder { BitsPerPixel = bitsPerPixel , SupportTransparency = supportTransparency } ;
149207
150208 // Does DebugSave & load reference CompareToReferenceInput():
151- image . VerifyEncoder ( provider , "bmp" , bitsPerPixel , encoder ) ;
209+ image . VerifyEncoder ( provider , "bmp" , bitsPerPixel , encoder , customComparer ) ;
152210 }
153211 }
154212 }
0 commit comments