4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcdisplayinfo.h"
24 #include "bccmodels.h"
26 #include "aggregated.h"
28 #include "interpolate.h"
34 REGISTER_PLUGIN(InterpolatePixelsMain)
41 InterpolatePixelsOffset::InterpolatePixelsOffset(InterpolatePixelsWindow *window,
55 this->window = window;
56 this->output = output;
59 InterpolatePixelsOffset::~InterpolatePixelsOffset()
63 int InterpolatePixelsOffset::handle_event()
65 *output = get_value();
66 window->client->send_configure_change();
75 InterpolatePixelsWindow::InterpolatePixelsWindow(InterpolatePixelsMain *client)
76 : PluginClientWindow(client,
83 this->client = client;
86 InterpolatePixelsWindow::~InterpolatePixelsWindow()
90 void InterpolatePixelsWindow::create_objects()
95 add_tool(title = new BC_Title(x, y, _("X Offset:")));
96 add_tool(x_offset = new InterpolatePixelsOffset(this,
97 x + title->get_w() + 5,
100 y += MAX(x_offset->get_h(), title->get_h()) + 5;
101 add_tool(title = new BC_Title(x, y, _("Y Offset:")));
102 add_tool(y_offset = new InterpolatePixelsOffset(this,
103 x + title->get_w() + 5,
106 y += MAX(y_offset->get_h(), title->get_h()) + 5;
125 InterpolatePixelsConfig::InterpolatePixelsConfig()
131 int InterpolatePixelsConfig::equivalent(InterpolatePixelsConfig &that)
133 return x == that.x && y == that.y;
136 void InterpolatePixelsConfig::copy_from(InterpolatePixelsConfig &that)
142 void InterpolatePixelsConfig::interpolate(InterpolatePixelsConfig &prev,
143 InterpolatePixelsConfig &next,
144 int64_t prev_position,
145 int64_t next_position,
146 int64_t current_position)
157 InterpolatePixelsMain::InterpolatePixelsMain(PluginServer *server)
158 : PluginVClient(server)
160 out_temp = out_frame = 0;
164 InterpolatePixelsMain::~InterpolatePixelsMain()
170 const char* InterpolatePixelsMain::plugin_title() { return _("Interpolate Pixels"); }
171 int InterpolatePixelsMain::is_realtime() { return 1; }
174 NEW_WINDOW_MACRO(InterpolatePixelsMain, InterpolatePixelsWindow)
177 void InterpolatePixelsMain::update_gui()
181 int changed = load_configuration();
184 thread->window->lock_window("InterpolatePixelsMain::update_gui");
185 ((InterpolatePixelsWindow*)thread->window)->x_offset->update(config.x);
186 ((InterpolatePixelsWindow*)thread->window)->y_offset->update(config.y);
187 thread->window->unlock_window();
193 LOAD_CONFIGURATION_MACRO(InterpolatePixelsMain, InterpolatePixelsConfig)
196 void InterpolatePixelsMain::save_data(KeyFrame *keyframe)
200 // cause data to be stored directly in text
201 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
202 output.tag.set_title("INTERPOLATEPIXELS");
203 output.tag.set_property("X", config.x);
204 output.tag.set_property("Y", config.y);
206 output.terminate_string();
209 void InterpolatePixelsMain::read_data(KeyFrame *keyframe)
213 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
219 result = input.read_tag();
223 if(input.tag.title_is("INTERPOLATEPIXELS"))
225 config.x = input.tag.get_property("X", config.x);
226 config.y = input.tag.get_property("Y", config.y);
232 int InterpolatePixelsMain::process_buffer(VFrame *frame,
233 int64_t start_position,
236 load_configuration();
238 // Set aggregation parameters
239 frame->get_params()->update("INTERPOLATEPIXELS_X", config.x);
240 frame->get_params()->update("INTERPOLATEPIXELS_Y", config.y);
247 //frame->dump_params();
251 // Aggregate with gamma
252 if(next_effect_is(_("Gamma")) ||
253 next_effect_is(_("Histogram")) ||
254 next_effect_is(_("Color Balance")))
261 int color_model = frame->get_color_model();
262 int active_model = BC_CModels::has_alpha(color_model) ?
263 BC_RGBA_FLOAT : BC_RGB_FLOAT;
264 new_temp(frame->get_w(), frame->get_h(), active_model);
265 get_temp()->transfer_from(frame);
267 out_frame = get_output();
268 color_model = out_frame->get_color_model();
269 active_model = BC_CModels::has_alpha(color_model) ?
270 BC_RGBA_FLOAT : BC_RGB_FLOAT;
271 if( active_model != color_model ) {
272 int w = out_frame->get_w(), h = out_frame->get_h();
273 if( out_temp && ( out_temp->get_color_model() != active_model ||
274 out_temp->get_w() != w || out_temp->get_w() != h ) ) {
275 delete out_temp; out_temp = 0;
278 out_temp = new VFrame(0, -1, w, h, active_model, -1);
279 out_frame = out_temp;
283 engine = new InterpolatePixelsEngine(this);
284 engine->process_packages();
286 if( out_frame != get_output() ) {
287 if( BC_CModels::has_alpha(out_frame->get_color_model()) ) {
288 unsigned char **out_rows = out_frame->get_rows();
289 int w = out_frame->get_w(), h = out_frame->get_h();
290 if( BC_CModels::has_alpha(get_temp()->get_color_model()) ) {
291 unsigned char **in_rows = get_temp()->get_rows();
292 for( int y=0; y<h; ++y ) {
293 float *inp = (float *)in_rows[y];
294 float *outp = (float *)out_rows[y];
295 for( int x=0; x<w; ++x,inp+=4,outp+=4 ) outp[3] = inp[3];
299 for( int y=0; y<h; ++y ) {
300 float *outp = (float *)out_rows[y];
301 for( int x=0; x<w; ++x,outp+=4 ) outp[3] = 1;
305 get_output()->transfer_from(out_frame);
314 // And is offset to recreate the 4 possibilities.
315 // The color values are interpolated in the most basic way.
316 // No adaptive algorithm is used.
318 int InterpolatePixelsMain::handle_opengl()
320 //printf("InterpolatePixelsMain::handle_opengl\n");
324 get_output()->to_texture();
325 get_output()->enable_opengl();
327 const char *shader_stack[] = { 0, 0, 0 };
328 int current_shader = 0;
329 INTERPOLATE_COMPILE(shader_stack, current_shader)
330 unsigned int frag = VFrame::make_shader(0,
336 glUniform1i(glGetUniformLocation(frag, "tex"), 0);
337 INTERPOLATE_UNIFORMS(frag)
341 get_output()->init_screen();
342 get_output()->bind_texture(0);
343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
347 get_output()->draw_texture();
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
351 get_output()->set_opengl_state(VFrame::SCREEN);
369 InterpolatePixelsPackage::InterpolatePixelsPackage()
380 InterpolatePixelsUnit::InterpolatePixelsUnit(InterpolatePixelsEngine *server, InterpolatePixelsMain *plugin)
383 this->plugin = plugin;
384 this->server = server;
387 void InterpolatePixelsUnit::process_package(LoadPackage *package)
389 InterpolatePixelsPackage *pkg = (InterpolatePixelsPackage*)package;
390 int h = plugin->get_temp()->get_h();
391 int w = plugin->get_temp()->get_w();
392 int pattern_offset_x = plugin->config.x;
393 int pattern_offset_y = plugin->config.y;
396 int components = BC_CModels::components(plugin->out_frame->get_color_model());
397 float color_matrix[9];
398 memcpy(color_matrix, server->color_matrix, sizeof(color_matrix));
403 // Only supports float because it's useless in any other colormodel.
404 for(int i = y1; i < y2; i++)
406 int pattern_coord_y = (i - pattern_offset_y) % 2;
407 float *prev_row = (float*)plugin->get_temp()->get_rows()[i - 1];
408 float *current_row = (float*)plugin->get_temp()->get_rows()[i];
409 float *next_row = (float*)plugin->get_temp()->get_rows()[i + 1];
410 float *out_row = (float*)plugin->out_frame->get_rows()[i];
412 prev_row += components;
413 current_row += components;
414 next_row += components;
415 out_row += components;
425 if(pattern_coord_y == 0)
427 for(int j = 1; j < w - 1; j++)
429 int pattern_coord_x = (j - pattern_offset_x) % 2;
431 if(pattern_coord_x == 0)
433 r = (prev_row[RED] + next_row[RED]) / 2;
434 g = current_row[GREEN];
435 b = (current_row[-components + BLUE] + current_row[components + BLUE]) / 2;
440 r = (prev_row[-components + RED] +
441 prev_row[components + RED] +
442 next_row[-components + RED] +
443 next_row[components + RED]) / 4;
444 g = (current_row[-components + GREEN] +
446 current_row[components + GREEN] +
447 next_row[GREEN]) / 4;
448 b = current_row[BLUE];
451 out_row[0] = r * color_matrix[0] + g * color_matrix[1] + b * color_matrix[2];
452 out_row[1] = r * color_matrix[3] + g * color_matrix[4] + b * color_matrix[5];
453 out_row[2] = r * color_matrix[6] + g * color_matrix[7] + b * color_matrix[8];
454 prev_row += components;
455 current_row += components;
456 next_row += components;
457 out_row += components;
462 for(int j = 1; j < w - 1; j++)
464 int pattern_coord_x = (j - pattern_offset_x) % 2;
466 if(pattern_coord_x == 0)
468 r = current_row[RED];
469 g = (current_row[-components + GREEN] +
471 current_row[components + GREEN] +
472 next_row[GREEN]) / 4;
473 b = (prev_row[-components + BLUE] +
474 prev_row[components + BLUE] +
475 next_row[-components + BLUE] +
476 next_row[components + BLUE]) / 4;
479 // Bottom right pixel
481 r = (current_row[-components + RED] + current_row[components + RED]) / 2;
482 g = current_row[GREEN];
483 b = (prev_row[BLUE] + next_row[BLUE]) / 2;
486 out_row[0] = r * color_matrix[0] + g * color_matrix[1] + b * color_matrix[2];
487 out_row[1] = r * color_matrix[3] + g * color_matrix[4] + b * color_matrix[5];
488 out_row[2] = r * color_matrix[6] + g * color_matrix[7] + b * color_matrix[8];
489 prev_row += components;
490 current_row += components;
491 next_row += components;
492 out_row += components;
501 InterpolatePixelsEngine::InterpolatePixelsEngine(InterpolatePixelsMain *plugin)
502 : LoadServer(plugin->get_project_smp() + 1, plugin->get_project_smp() + 1)
504 this->plugin = plugin;
507 void InterpolatePixelsEngine::init_packages()
509 for( int i=0; i<9; ++i ) color_matrix[i] = 0;
510 for( int i=0; i<3; ++i ) color_matrix[i*3 + i] = 1;
511 char string[BCTEXTLEN];
513 plugin->get_output()->get_params()->get("DCRAW_MATRIX", string);
515 "%f %f %f %f %f %f %f %f %f",
525 for(int i = 0; i < get_total_packages(); i++)
527 InterpolatePixelsPackage *package = (InterpolatePixelsPackage*)get_package(i);
528 package->y1 = plugin->get_temp()->get_h() * i / get_total_packages();
529 package->y2 = plugin->get_temp()->get_h() * (i + 1) / get_total_packages();
534 LoadClient* InterpolatePixelsEngine::new_client()
536 return new InterpolatePixelsUnit(this, plugin);
540 LoadPackage* InterpolatePixelsEngine::new_package()
542 return new InterpolatePixelsPackage;