]> git.sesse.net Git - movit/commitdiff
Add an unsharp mask effect, to complement the more expensive deconvolution sharpening.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Oct 2012 20:30:35 +0000 (22:30 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 10 Oct 2012 20:30:35 +0000 (22:30 +0200)
Makefile
unsharp_mask_effect.cpp [new file with mode: 0644]
unsharp_mask_effect.h [new file with mode: 0644]

index c33d20a387e5d5d233956c79f349940499407210..301aa2fe297b7561e6d61bcad6e64bee09ceb636 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,6 +22,7 @@ OBJS += mirror_effect.o
 OBJS += blur_effect.o
 OBJS += diffusion_effect.o
 OBJS += glow_effect.o
+OBJS += unsharp_mask_effect.o
 OBJS += mix_effect.o
 OBJS += resize_effect.o
 OBJS += deconvolution_sharpen_effect.o
diff --git a/unsharp_mask_effect.cpp b/unsharp_mask_effect.cpp
new file mode 100644 (file)
index 0000000..db8ccd2
--- /dev/null
@@ -0,0 +1,40 @@
+#include <math.h>
+#include <assert.h>
+
+#include "unsharp_mask_effect.h"
+#include "blur_effect.h"
+#include "mix_effect.h"
+#include "effect_chain.h"
+#include "util.h"
+
+UnsharpMaskEffect::UnsharpMaskEffect()
+       : blur(new BlurEffect),
+         mix(new MixEffect)
+{
+       mix->set_float("strength_first", 1.0f);
+       mix->set_float("strength_second", -0.3f);
+}
+
+void UnsharpMaskEffect::rewrite_graph(EffectChain *graph, Node *self)
+{
+       assert(self->incoming_links.size() == 1);
+       Node *input = self->incoming_links[0];
+
+       Node *blur_node = graph->add_node(blur);
+       Node *mix_node = graph->add_node(mix);
+       graph->replace_receiver(self, mix_node);
+       graph->connect_nodes(input, blur_node);
+       graph->connect_nodes(blur_node, mix_node);
+       graph->replace_sender(self, mix_node);
+
+       self->disabled = true;
+}
+
+bool UnsharpMaskEffect::set_float(const std::string &key, float value) {
+       printf("%s = %f\n", key.c_str(), value);
+       if (key == "amount") {
+               bool ok = mix->set_float("strength_first", 1.0f + value);
+               return ok && mix->set_float("strength_second", -value);
+       }
+       return blur->set_float(key, value);
+}
diff --git a/unsharp_mask_effect.h b/unsharp_mask_effect.h
new file mode 100644 (file)
index 0000000..45a04f0
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef _UNSHARP_MASK_EFFECT_H
+#define _UNSHARP_MASK_EFFECT_H 1
+
+// Unsharp mask is probably the most popular way of doing sharpening today,
+// although it does not always deliver the best results (it is very prone
+// to haloing). It simply consists of removing a blurred copy of the image from
+// itself (multiplied by some strength factor). In this aspect, it's similar to
+// glow, except by subtracting instead of adding.
+//
+// See DeconvolutionSharpenEffect for a different, possibly better
+// sharpening algorithm.
+
+#include "effect.h"
+
+class BlurEffect;
+class MixEffect;
+
+class UnsharpMaskEffect : public Effect {
+public:
+       UnsharpMaskEffect();
+       virtual std::string effect_type_id() const { return "UnsharpMaskEffect"; }
+
+       virtual bool needs_srgb_primaries() const { return false; }
+
+       virtual void rewrite_graph(EffectChain *graph, Node *self);
+       virtual bool set_float(const std::string &key, float value);
+
+       virtual std::string output_fragment_shader() {
+               assert(false);
+       }
+       virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
+               assert(false);
+       }
+
+private:
+       BlurEffect *blur;
+       MixEffect *mix;
+};
+
+#endif // !defined(_UNSHARP_MASK_EFFECT_H)