1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// timfx
// Copyright 2002, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "gl_blur.h"
#include <kino/image_filters.h>
#include <gtkmm/adjustment.h>
#include <gtkmm/box.h>
#include <gtkmm/checkbutton.h>
#include <gtkmm/label.h>
#include <gtkmm/main.h>
#include <gtkmm/spinbutton.h>
#include <gtkmm/window.h>
#include <algorithm>
#include <memory>
#include <numeric>
#include <sstream>
#include "kino_opengl_utility.h"
namespace
{
class gl_blur :
public GDKImageFilter
{
public:
gl_blur() :
m_radius(5)
{
Gtk::Main main(0, 0);
m_radius_spin_button.set_adjustment(*manage(new Gtk::Adjustment(m_radius, 1, 1000, 1, 10)));
m_radius_spin_button.set_numeric(true);
m_radius_spin_button.set_digits(0);
m_radius_spin_button.set_wrap(false);
m_radius_spin_button.set_snap_to_ticks(true);
m_radius_spin_button.set_value(m_radius);
Gtk::HBox* const options_group = new Gtk::HBox(false, 0);
options_group->pack_start(*manage(new Gtk::Label("Radius:")), false, true);
options_group->pack_start(m_radius_spin_button, true, true);
Gtk::VBox* const vbox = new Gtk::VBox(false, 0);
vbox->pack_start(*manage(options_group), false, true);
vbox->show_all();
m_window.add(*manage(vbox));
}
char *GetDescription( ) const
{
return "Blur (OpenGL)";
}
void FilterFrame(uint8_t* pixels, int width, int height, double position, double frame_delta)
{
std::cout << position << ": " << frame_delta << std::endl;
// Uncomment this to disable pbuffer rendering
// kino::gl::use_pbuffers(false);
if(0 == m_buffer.get())
m_buffer.reset(new kino::gl::render_buffer(width, height));
// Start drawing ...
m_buffer->start_render();
// Check to be sure we haven't exceeded the limits of this implementation
GLint max_width = 0;
GLint max_height = 0;
glGetConvolutionParameteriv(GL_SEPARABLE_2D, GL_MAX_CONVOLUTION_WIDTH, &max_width);
glGetConvolutionParameteriv(GL_SEPARABLE_2D, GL_MAX_CONVOLUTION_HEIGHT, &max_height);
if(m_radius > std::min(max_width, max_height))
{
static std::ostringstream error_message;
error_message.str().clear();
error_message << "Blur radius exceeds local OpenGL limit of " << std::min(max_width, max_height) << " pixels";
throw error_message.str().c_str();
}
// Setup our convolution ...
std::vector<GLfloat> unweighted_filter(m_radius, 1.0); // box filter
std::vector<GLfloat> filter ;
std::transform(unweighted_filter.begin(), unweighted_filter.end(), std::back_inserter(filter), std::bind2nd(std::divides<GLfloat>(), std::accumulate(unweighted_filter.begin(), unweighted_filter.end(), 0.0)));
glSeparableFilter2D(GL_SEPARABLE_2D, GL_INTENSITY, m_radius, m_radius, GL_LUMINANCE, GL_FLOAT, &filter[0], &filter[0]);
glConvolutionParameteri(GL_SEPARABLE_2D, GL_CONVOLUTION_BORDER_MODE, GL_REPLICATE_BORDER);
glEnable(GL_SEPARABLE_2D);
// Draw the background ...
m_buffer->draw_background(width, height, pixels);
// Finish drawing ...
m_buffer->finish_render();
// Copy rendered image back to Kino
glDisable(GL_SEPARABLE_2D);
m_buffer->read_pixels(width, height, pixels);
}
void AttachWidgets(GtkBin* bin)
{
gtk_widget_reparent( ( GTK_BIN( m_window.gobj() ) )->child, GTK_WIDGET( bin ) );
}
void DetachWidgets(GtkBin* bin)
{
gtk_widget_reparent( ( GTK_BIN( bin ) )->child, GTK_WIDGET( m_window.gobj() ) );
}
void InterpretWidgets( GtkBin *bin )
{
m_radius = m_radius_spin_button.get_value_as_int();
m_buffer.reset(0);
}
private:
Gtk::SpinButton m_radius_spin_button;
Gtk::Window m_window;
unsigned int m_radius;
std::auto_ptr<kino::gl::render_buffer> m_buffer;
};
} // namespace
GDKImageFilter* gl_blur_factory()
{
return new gl_blur();
}
|