File: soft_focus.cc

package info (click to toggle)
timfx 0.2.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,428 kB
  • ctags: 359
  • sloc: sh: 8,432; cpp: 2,061; makefile: 81
file content (220 lines) | stat: -rw-r--r-- 7,151 bytes parent folder | download
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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 "soft_focus.h"
#include "kino_plugin_types.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 <deque>
#include <numeric>
#include <vector>

namespace
{

class soft_focus :
	public GDKImageFilter
{
public:
	soft_focus() :
		m_radius(15),
		m_mix(0.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(false);
		m_radius_spin_button.set_value(m_radius);

		m_mix_spin_button.set_adjustment(*manage(new Gtk::Adjustment(m_mix, 0.0, 1.0, 0.01, 0.1)));
		m_mix_spin_button.set_numeric(true);
		m_mix_spin_button.set_digits(2);
		m_mix_spin_button.set_wrap(false);
		m_mix_spin_button.set_snap_to_ticks(false);

		Gtk::HBox* const options_group = new Gtk::HBox(false, 0);
		options_group->pack_start(*manage(new Gtk::Label("Softness:")), false, true);
		options_group->pack_start(m_radius_spin_button, true, true);
		Gtk::HBox* const options_group2 = new Gtk::HBox(false, 0);
		options_group2->pack_start(*manage(new Gtk::Label("Amount:")), false, true);
		options_group2->pack_start(m_mix_spin_button, true, true);

		Gtk::VBox* const vbox = new Gtk::VBox(false, 0);
		vbox->pack_start(*manage(options_group), false, true);
		vbox->pack_start(*manage(options_group2), false, true);
		vbox->show_all();

		m_window.add(*manage(vbox));
	}

	char *GetDescription( ) const
	{
		return "Soft Focus"; 
	}

	void FilterFrame(uint8_t* pixels, int width, int height, double position, double frame_delta)
	{
		// Create a copy of the source image for later mixing with the blurred version ...	
		kino::basic_bitmap<kino::basic_rgb<uint8_t> > blurred_image(pixels, width, height);
	
		// Create an N-pixel filter kernel
		kino::convolve_filter<kino::basic_rgb<double> > filter;
		unsigned int n = m_radius * 2;
		
/* Gaussian distribution		
		for(unsigned int i = 0; i <= n; ++i)
			filter.push_weight(factorial(n) / (factorial(i) * factorial(n - i)));
*/

/* Box distribution 
		for(unsigned int i = 0; i <= n; ++i)
			filter.push_weight(1);
*/

/* Triangle distribution */
		for(int i = 0; i <= n; ++i)
			filter.push_weight((n * 0.5) - fabs(i - (n * 0.5)));

		// Filter horizontally ...
		for(int row = 0; row < height; ++row)
			{
				kino::basic_rgb<uint8_t>* const a = blurred_image.begin() + (row * width);
				kino::basic_rgb<uint8_t>* const b = a + filter.neighbors();
				kino::basic_rgb<uint8_t>* const c = a + width - filter.neighbors();
				kino::basic_rgb<uint8_t>* const d = a + width;
				
				// Initialize the kernel ...
				for(const kino::basic_rgb<uint8_t>* neighbor = a; neighbor != b; ++neighbor)
					filter.push_value(*neighbor);
				
				// First border zone ...
				unsigned int start = filter.middle();
				for(kino::basic_rgb<uint8_t>* current = a; current != b; ++current, --start)
					{
						filter.push_value(*(current + filter.neighbors()));
						*current = filter.get_value(start, filter.width());
					}

				// Image body ...
				for(kino::basic_rgb<uint8_t>* current = b; current != c; ++current)
					{
						filter.push_value(*(current + filter.neighbors()));
						*current = filter.get_value();
					}

				// Second border zone ...
				unsigned int remaining_width = filter.width()-1;
				for(kino::basic_rgb<uint8_t>* current = c; current != d; ++current, --remaining_width)
					{
						filter.push_value(kino::basic_rgb<uint8_t>());
						*current = filter.get_value(0, remaining_width);
					}
			}

		// Filter vertically ...
		for(int column = 0; column < width; ++column)
			{
				kino::basic_rgb<uint8_t>* const a = blurred_image.begin() + column;
				kino::basic_rgb<uint8_t>* const b = a + filter.neighbors() * width;
				kino::basic_rgb<uint8_t>* const c = a + (height - filter.neighbors()) * width;
				kino::basic_rgb<uint8_t>* const d = a + height * width;
				
				// Initialize the kernel ...
				for(const kino::basic_rgb<uint8_t>* neighbor = a; neighbor != b; neighbor += width)
					filter.push_value(*neighbor);
				
				// First border zone ...
				unsigned int start = filter.middle();
				for(kino::basic_rgb<uint8_t>* current = a; current != b; current += width, --start)
					{
						filter.push_value(*(current + filter.neighbors() * width));
						*current = filter.get_value(start, filter.width());
					}

				// Image body ...
				for(kino::basic_rgb<uint8_t>* current = b; current != c; current += width)
					{
						filter.push_value(*(current + filter.neighbors() * width));
						*current = filter.get_value();
					}

				// Second border zone ...
				unsigned int remaining_width = filter.width() - 1;
				for(kino::basic_rgb<uint8_t>* current = c; current != d; current += width, --remaining_width)
					{
						filter.push_value(kino::basic_rgb<uint8_t>());
						*current = filter.get_value(0, remaining_width);
					}
			}
			
		// Mix the blurred image with the original ...
		kino::basic_rgb<uint8_t>* original_pixel = reinterpret_cast<kino::basic_rgb<uint8_t>*>(pixels);
		for(kino::basic_bitmap<kino::basic_rgb<uint8_t> >::const_iterator blurred_pixel = blurred_image.begin(); blurred_pixel != blurred_image.end(); ++blurred_pixel, ++original_pixel)
			{
				original_pixel->red = kino::lerp(original_pixel->red, blurred_pixel->red, m_mix);
				original_pixel->green = kino::lerp(original_pixel->green, blurred_pixel->green, m_mix);
				original_pixel->blue = kino::lerp(original_pixel->blue, blurred_pixel->blue, m_mix);
			}
	}

	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_mix = m_mix_spin_button.get_value();
	}

private:
	Gtk::SpinButton m_radius_spin_button;
	Gtk::SpinButton m_mix_spin_button;
	
	Gtk::Window m_window;
	
	unsigned int m_radius;
	double m_mix;
};

} // namespace

GDKImageFilter* soft_focus_factory()
{
	return new soft_focus();
}