@@ -49,6 +49,11 @@ internal sealed class GifEncoderCore
4949 /// </summary>
5050 private int bitDepth ;
5151
52+ /// <summary>
53+ /// The pixel sampling strategy for global quantization.
54+ /// </summary>
55+ private IPixelSamplingStrategy pixelSamplingStrategy ;
56+
5257 /// <summary>
5358 /// Initializes a new instance of the <see cref="GifEncoderCore"/> class.
5459 /// </summary>
@@ -60,6 +65,7 @@ public GifEncoderCore(Configuration configuration, IGifEncoderOptions options)
6065 this . memoryAllocator = configuration . MemoryAllocator ;
6166 this . quantizer = options . Quantizer ;
6267 this . colorTableMode = options . ColorTableMode ;
68+ this . pixelSamplingStrategy = options . GlobalPixelSamplingStrategy ;
6369 }
6470
6571 /// <summary>
@@ -81,9 +87,18 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream)
8187
8288 // Quantize the image returning a palette.
8389 IndexedImageFrame < TPixel > quantized ;
84- using ( IFrameQuantizer < TPixel > frameQuantizer = this . quantizer . CreateFrameQuantizer < TPixel > ( this . configuration ) )
90+
91+ using ( IQuantizer < TPixel > frameQuantizer = this . quantizer . CreatePixelSpecificQuantizer < TPixel > ( this . configuration ) )
8592 {
86- quantized = frameQuantizer . QuantizeFrame ( image . Frames . RootFrame , image . Bounds ( ) ) ;
93+ if ( useGlobalTable )
94+ {
95+ frameQuantizer . BuildPalette ( this . pixelSamplingStrategy , image ) ;
96+ quantized = frameQuantizer . QuantizeFrame ( image . Frames . RootFrame , image . Bounds ( ) ) ;
97+ }
98+ else
99+ {
100+ quantized = frameQuantizer . BuildPaletteAndQuantizeFrame ( image . Frames . RootFrame , image . Bounds ( ) ) ;
101+ }
87102 }
88103
89104 // Get the number of bits.
@@ -154,7 +169,7 @@ private void EncodeGlobal<TPixel>(Image<TPixel> image, IndexedImageFrame<TPixel>
154169 pixelMap = new EuclideanPixelMap < TPixel > ( this . configuration , quantized . Palette ) ;
155170 }
156171
157- using var paletteFrameQuantizer = new PaletteFrameQuantizer < TPixel > ( this . configuration , this . quantizer . Options , pixelMap ) ;
172+ using var paletteFrameQuantizer = new PaletteQuantizer < TPixel > ( this . configuration , this . quantizer . Options , pixelMap ) ;
158173 using IndexedImageFrame < TPixel > paletteQuantized = paletteFrameQuantizer . QuantizeFrame ( frame , frame . Bounds ( ) ) ;
159174 this . WriteImageData ( paletteQuantized , stream ) ;
160175 }
@@ -184,13 +199,13 @@ private void EncodeLocal<TPixel>(Image<TPixel> image, IndexedImageFrame<TPixel>
184199 MaxColors = frameMetadata . ColorTableLength
185200 } ;
186201
187- using IFrameQuantizer < TPixel > frameQuantizer = this . quantizer . CreateFrameQuantizer < TPixel > ( this . configuration , options ) ;
188- quantized = frameQuantizer . QuantizeFrame ( frame , frame . Bounds ( ) ) ;
202+ using IQuantizer < TPixel > frameQuantizer = this . quantizer . CreatePixelSpecificQuantizer < TPixel > ( this . configuration , options ) ;
203+ quantized = frameQuantizer . BuildPaletteAndQuantizeFrame ( frame , frame . Bounds ( ) ) ;
189204 }
190205 else
191206 {
192- using IFrameQuantizer < TPixel > frameQuantizer = this . quantizer . CreateFrameQuantizer < TPixel > ( this . configuration ) ;
193- quantized = frameQuantizer . QuantizeFrame ( frame , frame . Bounds ( ) ) ;
207+ using IQuantizer < TPixel > frameQuantizer = this . quantizer . CreatePixelSpecificQuantizer < TPixel > ( this . configuration ) ;
208+ quantized = frameQuantizer . BuildPaletteAndQuantizeFrame ( frame , frame . Bounds ( ) ) ;
194209 }
195210 }
196211
0 commit comments