]> git.sesse.net Git - movit/blobdiff - deconvolution_sharpen_effect.cpp
Remove some unneeded conversions from ResampleEffect. Speeds up texture generation...
[movit] / deconvolution_sharpen_effect.cpp
index d570eaadbe639ba60528c7f6557314c7dd5ad471..2db15dc1bc98af9b3e340551028b99b29574321e 100644 (file)
@@ -2,17 +2,24 @@
 // Since all of our signals are symmetrical, discrete correlation and convolution
 // is the same operation, and so we won't make a difference in notation.
 
-
-#include <math.h>
-#include <assert.h>
 #include <Eigen/Dense>
 #include <Eigen/Cholesky>
+#include <epoxy/gl.h>
+#include <assert.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <algorithm>
+#include <new>
 
 #include "deconvolution_sharpen_effect.h"
+#include "effect_util.h"
 #include "util.h"
-#include "opengl.h"
 
 using namespace Eigen;
+using namespace std;
+
+namespace movit {
 
 DeconvolutionSharpenEffect::DeconvolutionSharpenEffect()
        : R(5),
@@ -24,7 +31,8 @@ DeconvolutionSharpenEffect::DeconvolutionSharpenEffect()
          last_circle_radius(-1.0f),
          last_gaussian_radius(-1.0f),
          last_correlation(-1.0f),
-         last_noise(-1.0f)
+         last_noise(-1.0f),
+         uniform_samples(NULL)
 {
        register_int("matrix_size", &R);
        register_float("circle_radius", &circle_radius);
@@ -33,7 +41,12 @@ DeconvolutionSharpenEffect::DeconvolutionSharpenEffect()
        register_float("noise", &noise);
 }
 
-std::string DeconvolutionSharpenEffect::output_fragment_shader()
+DeconvolutionSharpenEffect::~DeconvolutionSharpenEffect()
+{
+       delete[] uniform_samples;
+}
+
+string DeconvolutionSharpenEffect::output_fragment_shader()
 {
        char buf[256];
        sprintf(buf, "#define R %u\n", R);
@@ -41,6 +54,9 @@ std::string DeconvolutionSharpenEffect::output_fragment_shader()
        assert(R >= 1);
        assert(R <= 25);  // Same limit as Refocus.
 
+       uniform_samples = new float[4 * (R + 1) * (R + 1)];
+       register_uniform_vec4_array("samples", uniform_samples, (R + 1) * (R + 1));
+
        last_R = R;
        return buf + read_file("deconvolution_sharpen_effect.frag");
 }
@@ -166,10 +182,10 @@ MatrixXf convolve(const MatrixXf &a, const MatrixXf &b)
                        int xa_max = xr;
 
                        // Now fit to the first demand.
-                       ya_min = std::max<int>(ya_min, 0);
-                       ya_max = std::min<int>(ya_max, a.rows() - 1);
-                       xa_min = std::max<int>(xa_min, 0);
-                       xa_max = std::min<int>(xa_max, a.cols() - 1);
+                       ya_min = max<int>(ya_min, 0);
+                       ya_max = min<int>(ya_max, a.rows() - 1);
+                       xa_min = max<int>(xa_min, 0);
+                       xa_max = min<int>(xa_max, a.cols() - 1);
 
                        assert(ya_max >= ya_min);
                        assert(xa_max >= xa_min);
@@ -216,10 +232,10 @@ MatrixXf central_convolve(const MatrixXf &a, const MatrixXf &b)
                        int xa_max = xr;
 
                        // Now fit to the first demand.
-                       ya_min = std::max<int>(ya_min, 0);
-                       ya_max = std::min<int>(ya_max, a.rows() - 1);
-                       xa_min = std::max<int>(xa_min, 0);
-                       xa_max = std::min<int>(xa_max, a.cols() - 1);
+                       ya_min = max<int>(ya_min, 0);
+                       ya_max = min<int>(ya_max, a.rows() - 1);
+                       xa_min = max<int>(xa_min, 0);
+                       xa_max = min<int>(xa_max, a.cols() - 1);
 
                        assert(ya_max >= ya_min);
                        assert(xa_max >= xa_min);
@@ -236,26 +252,10 @@ MatrixXf central_convolve(const MatrixXf &a, const MatrixXf &b)
        return result;
 }
 
-void print_matrix(const MatrixXf &m)
-{
-       for (int y = 0; y < m.rows(); ++y) {
-               for (int x = 0; x < m.cols(); ++x) {
-                       printf("%7.4f ", m(x, y));
-               }
-               printf("\n");
-       }
-}
-
 }  // namespace
 
 void DeconvolutionSharpenEffect::update_deconvolution_kernel()
 {
-       printf("circular blur radius: %5.3f\n", circle_radius);
-       printf("gaussian blur radius: %5.3f\n", gaussian_radius);
-       printf("correlation:          %5.3f\n", correlation);
-       printf("noise factor:         %5.3f\n", noise);
-       printf("\n");
-
        // Figure out the impulse response for the circular part of the blur.
        MatrixXf circ_h(2 * R + 1, 2 * R + 1);
        for (int y = -R; y <= R; ++y) { 
@@ -274,8 +274,7 @@ void DeconvolutionSharpenEffect::update_deconvolution_kernel()
                        if (gaussian_radius < 1e-3) {
                                val = (x == 0 && y == 0) ? 1.0f : 0.0f;
                        } else {
-                               float z = hypot(x, y) / gaussian_radius;
-                               val = exp(-z * z);
+                               val = exp(-(x*x + y*y) / (2.0 * gaussian_radius * gaussian_radius));
                        }
                        gaussian_h(y + 2 * R, x + 2 * R) = val;
                }
@@ -313,7 +312,7 @@ void DeconvolutionSharpenEffect::update_deconvolution_kernel()
        MatrixXf r_uu(8 * R + 1, 8 * R + 1);
        for (int y = -4 * R; y <= 4 * R; ++y) { 
                for (int x = -4 * R; x <= 4 * R; ++x) {
-                       r_uu(x + 4 * R, y + 4 * R) = pow(correlation, hypot(x, y));
+                       r_uu(x + 4 * R, y + 4 * R) = pow(double(correlation), hypot(x, y));
                }
        }
 
@@ -378,8 +377,7 @@ void DeconvolutionSharpenEffect::update_deconvolution_kernel()
        //   (G+H)     x0 + I x2     = y2
        //
        // This both increases accuracy and provides us with a very nice speed
-       // boost. We could have gone even further and went for 8-way symmetry
-       // like the shader does, but this is good enough right now.
+       // boost.
        MatrixXf M(MatrixXf::Zero((R + 1) * (R + 1), (R + 1) * (R + 1)));
        MatrixXf r_uv_flattened(MatrixXf::Zero((R + 1) * (R + 1), 1));
        for (int outer_i = 0; outer_i < 2 * R + 1; ++outer_i) {
@@ -431,7 +429,7 @@ void DeconvolutionSharpenEffect::update_deconvolution_kernel()
        last_noise = noise;
 }
 
-void DeconvolutionSharpenEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
+void DeconvolutionSharpenEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
 {
        Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
@@ -444,17 +442,15 @@ void DeconvolutionSharpenEffect::set_gl_state(GLuint glsl_program_num, const std
                update_deconvolution_kernel();
        }
        // Now encode it as uniforms, and pass it on to the shader.
-       // (Actually the shader only uses about half of the elements.)
-       float samples[4 * (R + 1) * (R + 1)];
        for (int y = 0; y <= R; ++y) {
                for (int x = 0; x <= R; ++x) {
                        int i = y * (R + 1) + x;
-                       samples[i * 4 + 0] = x / float(width);
-                       samples[i * 4 + 1] = y / float(height);
-                       samples[i * 4 + 2] = g(y, x);
-                       samples[i * 4 + 3] = 0.0f;
+                       uniform_samples[i * 4 + 0] = x / float(width);
+                       uniform_samples[i * 4 + 1] = y / float(height);
+                       uniform_samples[i * 4 + 2] = g(y, x);
+                       uniform_samples[i * 4 + 3] = 0.0f;
                }
        }
-
-       set_uniform_vec4_array(glsl_program_num, prefix, "samples", samples, R * R);
 }
+
+}  // namespace movit