@@ -24,22 +24,22 @@ internal sealed class BmpEncoderCore
2424 private int padding ;
2525
2626 /// <summary>
27- /// The mask for the alpha channel of the color for a 32 bit rgba bitmaps.
27+ /// The mask for the alpha channel of the color for 32 bit rgba bitmaps.
2828 /// </summary>
2929 private const int Rgba32AlphaMask = 0xFF << 24 ;
3030
3131 /// <summary>
32- /// The mask for the red part of the color for a 32 bit rgba bitmaps.
32+ /// The mask for the red part of the color for 32 bit rgba bitmaps.
3333 /// </summary>
3434 private const int Rgba32RedMask = 0xFF << 16 ;
3535
3636 /// <summary>
37- /// The mask for the green part of the color for a 32 bit rgba bitmaps.
37+ /// The mask for the green part of the color for 32 bit rgba bitmaps.
3838 /// </summary>
3939 private const int Rgba32GreenMask = 0xFF << 8 ;
4040
4141 /// <summary>
42- /// The mask for the blue part of the color for a 32 bit rgba bitmaps.
42+ /// The mask for the blue part of the color for 32 bit rgba bitmaps.
4343 /// </summary>
4444 private const int Rgba32BlueMask = 0xFF ;
4545
@@ -49,15 +49,23 @@ internal sealed class BmpEncoderCore
4949
5050 private BmpBitsPerPixel ? bitsPerPixel ;
5151
52+ /// <summary>
53+ /// A bitmap v4 header will only be written, if the user explicitly wants support for transparency.
54+ /// In this case the compression type BITFIELDS will be used.
55+ /// Otherwise a bitmap v3 header will be written, which is supported by almost all decoders.
56+ /// </summary>
57+ private readonly bool writeV4Header ;
58+
5259 /// <summary>
5360 /// Initializes a new instance of the <see cref="BmpEncoderCore"/> class.
5461 /// </summary>
55- /// <param name="options">The encoder options</param>
56- /// <param name="memoryAllocator">The memory manager</param>
62+ /// <param name="options">The encoder options. </param>
63+ /// <param name="memoryAllocator">The memory manager. </param>
5764 public BmpEncoderCore ( IBmpEncoderOptions options , MemoryAllocator memoryAllocator )
5865 {
5966 this . memoryAllocator = memoryAllocator ;
6067 this . bitsPerPixel = options . BitsPerPixel ;
68+ this . writeV4Header = options . SupportTransparency ;
6169 }
6270
6371 /// <summary>
@@ -112,7 +120,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
112120 }
113121 }
114122
115- int infoHeaderSize = BmpInfoHeader . SizeV4 ;
123+ int infoHeaderSize = this . writeV4Header ? BmpInfoHeader . SizeV4 : BmpInfoHeader . SizeV3 ;
116124 var infoHeader = new BmpInfoHeader (
117125 headerSize : infoHeaderSize ,
118126 height : image . Height ,
@@ -123,17 +131,15 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
123131 clrUsed : 0 ,
124132 clrImportant : 0 ,
125133 xPelsPerMeter : hResolution ,
126- yPelsPerMeter : vResolution )
127- {
128- RedMask = Rgba32RedMask ,
129- GreenMask = Rgba32GreenMask ,
130- BlueMask = Rgba32BlueMask ,
131- Compression = BmpCompression . BitFields
132- } ;
134+ yPelsPerMeter : vResolution ) ;
133135
134- if ( this . bitsPerPixel == BmpBitsPerPixel . Pixel32 )
136+ if ( this . writeV4Header && this . bitsPerPixel == BmpBitsPerPixel . Pixel32 )
135137 {
136138 infoHeader . AlphaMask = Rgba32AlphaMask ;
139+ infoHeader . RedMask = Rgba32RedMask ;
140+ infoHeader . GreenMask = Rgba32GreenMask ;
141+ infoHeader . BlueMask = Rgba32BlueMask ;
142+ infoHeader . Compression = BmpCompression . BitFields ;
137143 }
138144
139145 var fileHeader = new BmpFileHeader (
@@ -151,7 +157,14 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
151157
152158 stream . Write ( buffer , 0 , BmpFileHeader . Size ) ;
153159
154- infoHeader . WriteV4Header ( buffer ) ;
160+ if ( this . writeV4Header )
161+ {
162+ infoHeader . WriteV4Header ( buffer ) ;
163+ }
164+ else
165+ {
166+ infoHeader . WriteV3Header ( buffer ) ;
167+ }
155168
156169 stream . Write ( buffer , 0 , infoHeaderSize ) ;
157170
0 commit comments