From 4842479001ddd859b777a2488f422d6d206e124e Mon Sep 17 00:00:00 2001 From: adroitwhiz Date: Thu, 25 Feb 2021 16:09:53 -0500 Subject: [PATCH] Optimize dithering (#66) --- natives/blur.cc | 10 +++++++++- natives/blurple.cc | 9 +++++++++ natives/caption.cc | 9 +++++++++ natives/caption2.cc | 8 ++++++++ natives/circle.cc | 10 +++++++++- natives/crop.cc | 10 +++++++++- natives/explode.cc | 10 +++++++++- natives/flag.cc | 10 +++++++++- natives/flip.cc | 10 +++++++++- natives/gamexplain.cc | 10 +++++++++- natives/globe.cc | 6 ++++++ natives/invert.cc | 10 +++++++++- natives/leak.cc | 10 +++++++++- natives/magik.cc | 10 +++++++++- natives/meme.cc | 10 +++++++++- natives/mirror.cc | 9 +++++++++ natives/misc.cc | 10 +++++++++- natives/motivate.cc | 9 +++++++++ natives/resize.cc | 10 +++++++++- natives/reverse.cc | 10 +++++++++- natives/scott.cc | 9 +++++++++ natives/spin.cc | 6 ++++++ natives/tile.cc | 9 +++++++++ natives/trump.cc | 9 +++++++++ natives/wall.cc | 10 +++++++++- natives/watermark.cc | 9 +++++++++ natives/wdt.cc | 9 +++++++++ 27 files changed, 236 insertions(+), 15 deletions(-) diff --git a/natives/blur.cc b/natives/blur.cc index 8f2fef9..679eeb7 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -26,7 +26,15 @@ class BlurWorker : public Napi::AsyncWorker { for_each(coalesced.begin(), coalesced.end(), magickImage(type)); optimizeTransparency(coalesced.begin(), coalesced.end()); - if (delay != 0) for_each(coalesced.begin(), coalesced.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : coalesced) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(coalesced.begin(), coalesced.end(), &blob); } diff --git a/natives/blurple.cc b/natives/blurple.cc index 9cef84e..8e85b17 100644 --- a/natives/blurple.cc +++ b/natives/blurple.cc @@ -27,6 +27,15 @@ class BlurpleWorker : public Napi::AsyncWorker { } optimizeTransparency(blurpled.begin(), blurpled.end()); + + if (type == "gif") { + for (Image &image : blurpled) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(blurpled.begin(), blurpled.end(), &blob); } diff --git a/natives/caption.cc b/natives/caption.cc index b674b44..a56b3b3 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -45,6 +45,15 @@ class CaptionWorker : public Napi::AsyncWorker { } optimizeTransparency(captioned.begin(), captioned.end()); + + if (type == "gif") { + for (Image &image : captioned) { + image.quantizeDither(false); + image.quantize(); + } + } + + writeImages(captioned.begin(), captioned.end(), &blob); } diff --git a/natives/caption2.cc b/natives/caption2.cc index 7d19ce7..9eb0a2f 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -43,6 +43,14 @@ class CaptionTwoWorker : public Napi::AsyncWorker { } optimizeTransparency(captioned.begin(), captioned.end()); + + if (type == "gif") { + for (Image &image : captioned) { + image.quantizeDither(false); + image.quantize(); + } + } + writeImages(captioned.begin(), captioned.end(), &blob); } diff --git a/natives/circle.cc b/natives/circle.cc index cef31af..af097f3 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -25,7 +25,15 @@ class CircleWorker : public Napi::AsyncWorker { } optimizeTransparency(blurred.begin(), blurred.end()); - if (delay != 0) for_each(blurred.begin(), blurred.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(blurred.begin(), blurred.end(), &blob); } diff --git a/natives/crop.cc b/natives/crop.cc index 1e4b840..b721daa 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -26,7 +26,15 @@ class CropWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/explode.cc b/natives/explode.cc index 17caaed..1a56793 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -25,7 +25,15 @@ class ExplodeWorker : public Napi::AsyncWorker { } optimizeTransparency(blurred.begin(), blurred.end()); - if (delay != 0) for_each(blurred.begin(), blurred.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(blurred.begin(), blurred.end(), &blob); } diff --git a/natives/flag.cc b/natives/flag.cc index 3ffb89f..e47cc77 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -31,7 +31,15 @@ class FlagWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/flip.cc b/natives/flip.cc index fc1d664..1d7da9c 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -25,7 +25,15 @@ class FlipWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index 1473493..23f340f 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -30,7 +30,15 @@ class GamexplainWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/globe.cc b/natives/globe.cc index 3ca2daf..7e4fe1c 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -48,6 +48,12 @@ class GlobeWorker : public Napi::AsyncWorker { } else if (type != "GIF") { for_each(mid.begin(), mid.end(), animationDelayImage(5)); } + + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/invert.cc b/natives/invert.cc index ac13c01..9ece3bf 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -27,7 +27,15 @@ class InvertWorker : public Napi::AsyncWorker { for_each(mid.begin(), mid.end(), magickImage(type)); optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/leak.cc b/natives/leak.cc index abbccf7..a2c528a 100644 --- a/natives/leak.cc +++ b/natives/leak.cc @@ -31,7 +31,15 @@ class LeakWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/magik.cc b/natives/magik.cc index a0254c2..f2c3e33 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -27,7 +27,15 @@ class MagikWorker : public Napi::AsyncWorker { } optimizeTransparency(blurred.begin(), blurred.end()); - if (delay != 0) for_each(blurred.begin(), blurred.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(blurred.begin(), blurred.end(), &blob); } diff --git a/natives/meme.cc b/natives/meme.cc index 6d7ee16..6c4b92b 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -57,7 +57,15 @@ class MemeWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/mirror.cc b/natives/mirror.cc index c0c1b67..d71ee75 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -53,6 +53,15 @@ class MirrorWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/misc.cc b/natives/misc.cc index d5fd72f..5d47fb2 100644 --- a/natives/misc.cc +++ b/natives/misc.cc @@ -25,7 +25,15 @@ class SwirlWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/motivate.cc b/natives/motivate.cc index 4c779c4..8b03fd7 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -60,6 +60,15 @@ class MotivateWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/resize.cc b/natives/resize.cc index 09b34d6..7f3f70c 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -32,7 +32,15 @@ class ResizeWorker : public Napi::AsyncWorker { } optimizeTransparency(blurred.begin(), blurred.end()); - if (delay != 0) for_each(blurred.begin(), blurred.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(blurred.begin(), blurred.end(), &blob); } diff --git a/natives/reverse.cc b/natives/reverse.cc index ca57d3b..1dca18b 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -28,7 +28,15 @@ class ReverseWorker : public Napi::AsyncWorker { for_each(coalesced.begin(), coalesced.end(), magickImage("GIF")); optimizeTransparency(coalesced.begin(), coalesced.end()); - if (delay != 0) for_each(coalesced.begin(), coalesced.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : coalesced) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(coalesced.begin(), coalesced.end(), &blob); } diff --git a/natives/scott.cc b/natives/scott.cc index 9f68d50..b5efbbe 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -35,6 +35,15 @@ class ScottWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/spin.cc b/natives/spin.cc index 4df5d11..2828b0d 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -42,6 +42,12 @@ class SpinWorker : public Napi::AsyncWorker { } else if (type != "GIF") { for_each(mid.begin(), mid.end(), animationDelayImage(5)); } + + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/tile.cc b/natives/tile.cc index 9cae75a..1126590 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -40,6 +40,15 @@ class TileWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/trump.cc b/natives/trump.cc index 7a7dd4c..ba511f4 100644 --- a/natives/trump.cc +++ b/natives/trump.cc @@ -35,6 +35,15 @@ class TrumpWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/wall.cc b/natives/wall.cc index ab8c82f..2aedeae 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -32,7 +32,15 @@ class WallWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/watermark.cc b/natives/watermark.cc index b3f2d88..cf20b39 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -51,6 +51,15 @@ class WatermarkWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); } diff --git a/natives/wdt.cc b/natives/wdt.cc index 256b3c4..20bc413 100644 --- a/natives/wdt.cc +++ b/natives/wdt.cc @@ -30,6 +30,15 @@ class WdtWorker : public Napi::AsyncWorker { } optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + writeImages(mid.begin(), mid.end(), &blob); }