protected override RequestedAction PostRenderImage(ImageState state) { var enabled = DetermineEnabled(state); if (!enabled) { return(RequestedAction.None); } long colors = 0; byte dither = GetDitherSetting(state); bool debug = DetermineDebug(state); var bitmap = state.destBitmap; try { var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); var analyzer = new PaletteAnalyzer(data); bitmap.UnlockBits(data); colors = analyzer.Colors; } catch (Exception) { // ignored } if (colors > byte.MaxValue) { var ditherMax = (byte)(_maxDither * (Math.Min(_maxColors, colors) / _maxColors)); dither = Math.Min(ditherMax, dither); colors = byte.MaxValue; } else { dither = 0; } var quantizer = new DitheredLuminanceQuantizer(bitmap.Width, (byte)colors, _ditherThreshold, dither, debug); try { var processedBitmap = (Bitmap)quantizer.QuantizeImage(bitmap, 1, 1); state.destBitmap = processedBitmap; bitmap.Dispose(); } catch (Exception) { } return(RequestedAction.None); }
protected override RequestedAction PostRenderImage(ImageState state) { var enabled = DetermineEnabled(state); if (!enabled) { return(RequestedAction.None); } // WuQuantizer leads to unwanted effects // Color vibrancy loss in saturated colors (fixed by introducing luminance in palette calculation) // Smooth transparent areas are jarry (adjusted by dithering below set threshold on alpha channel only) long colors = 0; byte ditherAmount = 0; var bitmap = state.destBitmap; try { var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); var analyzer = new PaletteAnalyzer(data); bitmap.UnlockBits(data); colors = analyzer.Colors; } catch (Exception) { // ignored } if (colors > 256) { ditherAmount = (byte)(_maxdither * (Math.Min(_maxcolors, colors) / _maxcolors)); colors = 256; } var quantizer = new DitheredLuminanceQuantizer(state.destBitmap.Width, state.destBitmap.Height, (byte)colors, _ditherThreshold, ditherAmount, DetermineDebug(state)); try { state.destBitmap = (Bitmap)quantizer.QuantizeImage(state.destBitmap, 1, 1); } catch (Exception) { // ignored } return(RequestedAction.None); }