private static void AdjustQuantizerAndDitherer(IBitmapData target, ref IQuantizer quantizer, ref IDitherer ditherer) { if (quantizer != null || ditherer == null) { return; } if (target.PixelFormat.CanBeDithered()) { quantizer = PredefinedColorsQuantizer.FromBitmapData(target); } else { ditherer = null; } }
public void ClearWithDitheringTest(PixelFormat pixelFormat, uint argb, bool errorDiffusion) { const int size = 512; Color color = Color.FromArgb((int)argb); var ditherer = errorDiffusion ? (IDitherer)ErrorDiffusionDitherer.FloydSteinberg : OrderedDitherer.Bayer8x8; new PerformanceTest { TestName = $"{pixelFormat} {size}x{size} {(errorDiffusion ? "Error Diffusion" : "Ordered Dithering")}", Iterations = 10, CpuAffinity = null } .AddCase(() => { using var bmp = new Bitmap(size, size, pixelFormat); using IBitmapDataInternal acc = BitmapDataFactory.CreateBitmapData(bmp, ImageLockMode.ReadWrite); IQuantizer quantizer = PredefinedColorsQuantizer.FromBitmapData(acc); var c = new Color32(color); using (IQuantizingSession quantizingSession = quantizer.Initialize(acc)) using (IDitheringSession ditheringSession = ditherer.Initialize(acc, quantizingSession)) { IReadWriteBitmapDataRow row = acc.DoGetRow(0); do { for (int x = 0; x < acc.Width; x++) { row[x] = ditheringSession.GetDitheredColor(c, x, row.Index); } } while (row.MoveNextRow()); } }, "Sequential clear") .AddCase(() => { using var bmp = new Bitmap(size, size, pixelFormat); bmp.Clear(color, ditherer); }, "BitmapDataAccessor.Clear") .DoTest() .DumpResults(Console.Out); }