@@ -12,12 +12,11 @@ namespace SixLabors.ImageSharp.Formats.Qoi;
1212/// </summary>
1313public class QoiEncoderCore : IImageEncoderInternals
1414{
15+ private readonly QoiEncoder encoder ;
1516 /// <summary>
1617 /// Initializes a new instance of the <see cref="QoiEncoderCore"/> class.
1718 /// </summary>
18- public QoiEncoderCore ( )
19- {
20- }
19+ public QoiEncoderCore ( QoiEncoder encoder ) => this . encoder = encoder ;
2120
2221 /// <inheritdoc />
2322 public void Encode < TPixel > ( Image < TPixel > image , Stream stream , CancellationToken cancellationToken )
@@ -26,23 +25,21 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
2625 Guard . NotNull ( image , nameof ( image ) ) ;
2726 Guard . NotNull ( stream , nameof ( stream ) ) ;
2827
29- WriteHeader ( image , stream ) ;
28+ this . WriteHeader ( image , stream ) ;
3029 WritePixels ( image , stream ) ;
3130 WriteEndOfStream ( stream ) ;
3231 stream . Flush ( ) ;
3332 }
3433
35- private static void WriteHeader ( Image image , Stream stream )
34+ private void WriteHeader ( Image image , Stream stream )
3635 {
3736 // Get metadata
3837 Span < byte > width = stackalloc byte [ 4 ] ;
3938 Span < byte > height = stackalloc byte [ 4 ] ;
4039 BinaryPrimitives . WriteUInt32BigEndian ( width , ( uint ) image . Width ) ;
4140 BinaryPrimitives . WriteUInt32BigEndian ( height , ( uint ) image . Height ) ;
42- QoiChannels qoiChannels = image . PixelType . BitsPerPixel == 24 ? QoiChannels . Rgb : QoiChannels . Rgba ;
43-
44- // I need to check this, how do I check it with the pixel type or metadata of the original image?
45- const QoiColorSpace qoiColorSpace = QoiColorSpace . SrgbWithLinearAlpha ;
41+ QoiChannels qoiChannels = this . encoder . Channels ?? QoiChannels . Rgba ;
42+ QoiColorSpace qoiColorSpace = this . encoder . ColorSpace ?? QoiColorSpace . SrgbWithLinearAlpha ;
4643
4744 // Write header to the stream
4845 stream . Write ( QoiConstants . Magic ) ;
@@ -58,11 +55,9 @@ private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
5855 // Start image encoding
5956 Rgba32 [ ] previouslySeenPixels = new Rgba32 [ 64 ] ;
6057 Rgba32 previousPixel = new ( 0 , 0 , 0 , 255 ) ;
61- int pixelArrayPosition = GetArrayPosition ( previousPixel ) ;
62- previouslySeenPixels [ pixelArrayPosition ] = previousPixel ;
63-
64- Buffer2D < TPixel > pixels = image . Frames [ 0 ] . PixelBuffer ;
6558 Rgba32 currentRgba32 = default ;
59+ Buffer2D < TPixel > pixels = image . Frames [ 0 ] . PixelBuffer ;
60+
6661 for ( int i = 0 ; i < pixels . Height ; i ++ )
6762 {
6863 for ( int j = 0 ; j < pixels . Width && i < pixels . Height ; j ++ )
@@ -105,7 +100,7 @@ private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
105100 while ( currentRgba32 . Equals ( previousPixel ) && repetitions < 62 ) ;
106101
107102 j -- ;
108- stream . WriteByte ( ( byte ) ( ( byte ) QoiChunkEnum . QoiOpRun | ( repetitions - 1 ) ) ) ;
103+ stream . WriteByte ( ( byte ) ( ( byte ) QoiChunk . QoiOpRun | ( repetitions - 1 ) ) ) ;
109104
110105 /* If it's a QOI_OP_RUN, we don't overwrite the previous pixel since
111106 * it will be taken and compared on the next iteration
@@ -115,7 +110,7 @@ private static void WritePixels<TPixel>(Image<TPixel> image, Stream stream)
115110
116111 // else, we check if it exists in the previously seen pixels
117112 // If so, we do a QOI_OP_INDEX
118- pixelArrayPosition = GetArrayPosition ( currentRgba32 ) ;
113+ int pixelArrayPosition = GetArrayPosition ( currentRgba32 ) ;
119114 if ( previouslySeenPixels [ pixelArrayPosition ] . Equals ( currentPixel ) )
120115 {
121116 stream . WriteByte ( ( byte ) pixelArrayPosition ) ;
@@ -140,7 +135,7 @@ diffBlue is > -3 and < 2 &&
140135 byte dr = ( byte ) ( diffRed + 2 ) ,
141136 dg = ( byte ) ( diffGreen + 2 ) ,
142137 db = ( byte ) ( diffBlue + 2 ) ,
143- valueToWrite = ( byte ) ( ( byte ) QoiChunkEnum . QoiOpDiff | ( dr << 4 ) | ( dg << 2 ) | db ) ;
138+ valueToWrite = ( byte ) ( ( byte ) QoiChunk . QoiOpDiff | ( dr << 4 ) | ( dg << 2 ) | db ) ;
144139 stream . WriteByte ( valueToWrite ) ;
145140 }
146141 else
@@ -156,7 +151,7 @@ diffBlueGreen is > -9 and < 8 &&
156151 {
157152 byte dr_dg = ( byte ) ( diffRedGreen + 8 ) ,
158153 db_dg = ( byte ) ( diffBlueGreen + 8 ) ,
159- byteToWrite1 = ( byte ) ( ( byte ) QoiChunkEnum . QoiOpLuma | ( diffGreen + 32 ) ) ,
154+ byteToWrite1 = ( byte ) ( ( byte ) QoiChunk . QoiOpLuma | ( diffGreen + 32 ) ) ,
160155 byteToWrite2 = ( byte ) ( ( dr_dg << 4 ) | db_dg ) ;
161156 stream . WriteByte ( byteToWrite1 ) ;
162157 stream . WriteByte ( byteToWrite2 ) ;
@@ -167,15 +162,15 @@ diffBlueGreen is > -9 and < 8 &&
167162 // If so, we do a QOI_OP_RGB
168163 if ( currentRgba32 . A == previousPixel . A )
169164 {
170- stream . WriteByte ( ( byte ) QoiChunkEnum . QoiOpRgb ) ;
165+ stream . WriteByte ( ( byte ) QoiChunk . QoiOpRgb ) ;
171166 stream . WriteByte ( currentRgba32 . R ) ;
172167 stream . WriteByte ( currentRgba32 . G ) ;
173168 stream . WriteByte ( currentRgba32 . B ) ;
174169 }
175170 else
176171 {
177172 // else, we do a QOI_OP_RGBA
178- stream . WriteByte ( ( byte ) QoiChunkEnum . QoiOpRgba ) ;
173+ stream . WriteByte ( ( byte ) QoiChunk . QoiOpRgba ) ;
179174 stream . WriteByte ( currentRgba32 . R ) ;
180175 stream . WriteByte ( currentRgba32 . G ) ;
181176 stream . WriteByte ( currentRgba32 . B ) ;
0 commit comments