@@ -111,7 +111,13 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
111111 this . InitQuantizationTables ( componentCount , jpegMetadata , out Block8x8F luminanceQuantTable , out Block8x8F chrominanceQuantTable ) ;
112112
113113 // Write the Start Of Image marker.
114- this . WriteApplicationHeader ( metadata ) ;
114+ this . WriteStartOfImage ( ) ;
115+
116+ // Do not write APP0 marker for RGB colorspace.
117+ if ( this . colorType != JpegColorType . Rgb )
118+ {
119+ this . WriteJfifApplicationHeader ( metadata ) ;
120+ }
115121
116122 // Write Exif, ICC and IPTC profiles
117123 this . WriteProfiles ( metadata ) ;
@@ -212,52 +218,60 @@ private static void WriteDataToDqt(byte[] dqt, ref int offset, QuantIndex i, ref
212218 }
213219
214220 /// <summary>
215- /// Writes the application header containing the JFIF identifier plus extra data .
221+ /// Write the start of image marker .
216222 /// </summary>
217- /// <param name="meta">The image metadata.</param>
218- private void WriteApplicationHeader ( ImageMetadata meta )
223+ private void WriteStartOfImage ( )
219224 {
220- // Write the start of image marker. Markers are always prefixed with 0xff.
225+ // Markers are always prefixed with 0xff.
221226 this . buffer [ 0 ] = JpegConstants . Markers . XFF ;
222227 this . buffer [ 1 ] = JpegConstants . Markers . SOI ;
223228
229+ this . outputStream . Write ( this . buffer , 0 , 2 ) ;
230+ }
231+
232+ /// <summary>
233+ /// Writes the application header containing the JFIF identifier plus extra data.
234+ /// </summary>
235+ /// <param name="meta">The image metadata.</param>
236+ private void WriteJfifApplicationHeader ( ImageMetadata meta )
237+ {
224238 // Write the JFIF headers
225- this . buffer [ 2 ] = JpegConstants . Markers . XFF ;
226- this . buffer [ 3 ] = JpegConstants . Markers . APP0 ; // Application Marker
227- this . buffer [ 4 ] = 0x00 ;
228- this . buffer [ 5 ] = 0x10 ;
229- this . buffer [ 6 ] = 0x4a ; // J
239+ this . buffer [ 0 ] = JpegConstants . Markers . XFF ;
240+ this . buffer [ 1 ] = JpegConstants . Markers . APP0 ; // Application Marker
241+ this . buffer [ 2 ] = 0x00 ;
242+ this . buffer [ 3 ] = 0x10 ;
243+ this . buffer [ 4 ] = 0x4a ; // J
244+ this . buffer [ 5 ] = 0x46 ; // F
245+ this . buffer [ 6 ] = 0x49 ; // I
230246 this . buffer [ 7 ] = 0x46 ; // F
231- this . buffer [ 8 ] = 0x49 ; // I
232- this . buffer [ 9 ] = 0x46 ; // F
233- this . buffer [ 10 ] = 0x00 ; // = "JFIF",'\0'
234- this . buffer [ 11 ] = 0x01 ; // versionhi
235- this . buffer [ 12 ] = 0x01 ; // versionlo
247+ this . buffer [ 8 ] = 0x00 ; // = "JFIF",'\0'
248+ this . buffer [ 9 ] = 0x01 ; // versionhi
249+ this . buffer [ 10 ] = 0x01 ; // versionlo
236250
237251 // Resolution. Big Endian
238- Span < byte > hResolution = this . buffer . AsSpan ( 14 , 2 ) ;
239- Span < byte > vResolution = this . buffer . AsSpan ( 16 , 2 ) ;
252+ Span < byte > hResolution = this . buffer . AsSpan ( 12 , 2 ) ;
253+ Span < byte > vResolution = this . buffer . AsSpan ( 14 , 2 ) ;
240254
241255 if ( meta . ResolutionUnits == PixelResolutionUnit . PixelsPerMeter )
242256 {
243257 // Scale down to PPI
244- this . buffer [ 13 ] = ( byte ) PixelResolutionUnit . PixelsPerInch ; // xyunits
258+ this . buffer [ 11 ] = ( byte ) PixelResolutionUnit . PixelsPerInch ; // xyunits
245259 BinaryPrimitives . WriteInt16BigEndian ( hResolution , ( short ) Math . Round ( UnitConverter . MeterToInch ( meta . HorizontalResolution ) ) ) ;
246260 BinaryPrimitives . WriteInt16BigEndian ( vResolution , ( short ) Math . Round ( UnitConverter . MeterToInch ( meta . VerticalResolution ) ) ) ;
247261 }
248262 else
249263 {
250264 // We can simply pass the value.
251- this . buffer [ 13 ] = ( byte ) meta . ResolutionUnits ; // xyunits
265+ this . buffer [ 11 ] = ( byte ) meta . ResolutionUnits ; // xyunits
252266 BinaryPrimitives . WriteInt16BigEndian ( hResolution , ( short ) Math . Round ( meta . HorizontalResolution ) ) ;
253267 BinaryPrimitives . WriteInt16BigEndian ( vResolution , ( short ) Math . Round ( meta . VerticalResolution ) ) ;
254268 }
255269
256270 // No thumbnail
257- this . buffer [ 18 ] = 0x00 ; // Thumbnail width
258- this . buffer [ 19 ] = 0x00 ; // Thumbnail height
271+ this . buffer [ 16 ] = 0x00 ; // Thumbnail width
272+ this . buffer [ 17 ] = 0x00 ; // Thumbnail height
259273
260- this . outputStream . Write ( this . buffer , 0 , 20 ) ;
274+ this . outputStream . Write ( this . buffer , 0 , 18 ) ;
261275 }
262276
263277 /// <summary>
0 commit comments