@@ -265,7 +265,7 @@ private void WriteJfifApplicationHeader(ImageMetadata meta)
265265 private void WriteDefineHuffmanTables ( int componentCount )
266266 {
267267 // Table identifiers.
268- Span < byte > headers = stackalloc byte [ ]
268+ ReadOnlySpan < byte > headers = new byte [ ]
269269 {
270270 0x00 ,
271271 0x10 ,
@@ -568,15 +568,17 @@ private void WriteProfiles(ImageMetadata metadata)
568568 /// <param name="componentIds">The component Id's.</param>
569569 private void WriteStartOfFrame ( int width , int height , int componentCount , ReadOnlySpan < byte > componentIds )
570570 {
571+ // This uses a C#'s compiler optimization that refers to the static data segment of the assembly,
572+ // and doesn't incur any allocation at all.
571573 // "default" to 4:2:0
572- Span < byte > subsamples = stackalloc byte [ ]
574+ ReadOnlySpan < byte > subsamples = new byte [ ]
573575 {
574576 0x22 ,
575577 0x11 ,
576578 0x11
577579 } ;
578580
579- Span < byte > chroma = stackalloc byte [ ]
581+ ReadOnlySpan < byte > chroma = new byte [ ]
580582 {
581583 0x00 ,
582584 0x01 ,
@@ -585,7 +587,7 @@ private void WriteStartOfFrame(int width, int height, int componentCount, ReadOn
585587
586588 if ( this . colorType == JpegColorType . Luminance )
587589 {
588- subsamples = stackalloc byte [ ]
590+ subsamples = new byte [ ]
589591 {
590592 0x11 ,
591593 0x00 ,
@@ -598,7 +600,7 @@ private void WriteStartOfFrame(int width, int height, int componentCount, ReadOn
598600 {
599601 case JpegColorType . YCbCrRatio444 :
600602 case JpegColorType . Rgb :
601- subsamples = stackalloc byte [ ]
603+ subsamples = new byte [ ]
602604 {
603605 0x11 ,
604606 0x11 ,
@@ -607,7 +609,7 @@ private void WriteStartOfFrame(int width, int height, int componentCount, ReadOn
607609
608610 if ( this . colorType == JpegColorType . Rgb )
609611 {
610- chroma = stackalloc byte [ ]
612+ chroma = new byte [ ]
611613 {
612614 0x00 ,
613615 0x00 ,
@@ -617,7 +619,7 @@ private void WriteStartOfFrame(int width, int height, int componentCount, ReadOn
617619
618620 break ;
619621 case JpegColorType . YCbCrRatio420 :
620- subsamples = stackalloc byte [ ]
622+ subsamples = new byte [ ]
621623 {
622624 0x22 ,
623625 0x11 ,
@@ -658,7 +660,9 @@ private void WriteStartOfFrame(int width, int height, int componentCount, ReadOn
658660 /// <param name="componentIds">The componentId's.</param>
659661 private void WriteStartOfScan ( int componentCount , ReadOnlySpan < byte > componentIds )
660662 {
661- Span < byte > huffmanId = stackalloc byte [ ]
663+ // This uses a C#'s compiler optimization that refers to the static data segment of the assembly,
664+ // and doesn't incur any allocation at all.
665+ ReadOnlySpan < byte > huffmanId = new byte [ ]
662666 {
663667 0x00 ,
664668 0x11 ,
@@ -668,7 +672,7 @@ private void WriteStartOfScan(int componentCount, ReadOnlySpan<byte> componentId
668672 // Use the same DC/AC tables for all channels for RGB.
669673 if ( this . colorType == JpegColorType . Rgb )
670674 {
671- huffmanId = stackalloc byte [ ]
675+ huffmanId = new byte [ ]
672676 {
673677 0x00 ,
674678 0x00 ,
0 commit comments