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()
92 int xs5 = xS(5), xs10 = xS(10), xs50 = xS(50);
93 int ys5 = yS(5), ys10 = yS(10);
94 int x = xs10, y = ys10;
97 add_tool(title = new BC_Title(x, y, _("X Offset:")));
98 add_tool(x_offset = new InterpolatePixelsOffset(this,
99 x + title->get_w() + xs5,
102 y += MAX(x_offset->get_h(), title->get_h()) + ys5;
103 add_tool(title = new BC_Title(x, y, _("Y Offset:")));
104 add_tool(y_offset = new InterpolatePixelsOffset(this,
105 x + title->get_w() + xs5,
108 y += MAX(y_offset->get_h(), title->get_h()) + ys5;
127 InterpolatePixelsConfig::InterpolatePixelsConfig()
133 int InterpolatePixelsConfig::equivalent(InterpolatePixelsConfig &that)
135 return x == that.x && y == that.y;
138 void InterpolatePixelsConfig::copy_from(InterpolatePixelsConfig &that)
144 void InterpolatePixelsConfig::interpolate(InterpolatePixelsConfig &prev,
145 InterpolatePixelsConfig &next,
146 int64_t prev_position,
147 int64_t next_position,
148 int64_t current_position)
159 InterpolatePixelsMain::InterpolatePixelsMain(PluginServer *server)
160 : PluginVClient(server)
162 out_temp = out_frame = 0;
166 InterpolatePixelsMain::~InterpolatePixelsMain()
172 const char* InterpolatePixelsMain::plugin_title() { return N_("Interpolate Bayer"); }
173 int InterpolatePixelsMain::is_realtime() { return 1; }
176 NEW_WINDOW_MACRO(InterpolatePixelsMain, InterpolatePixelsWindow)
179 void InterpolatePixelsMain::update_gui()
183 int changed = load_configuration();
186 thread->window->lock_window("InterpolatePixelsMain::update_gui");
187 ((InterpolatePixelsWindow*)thread->window)->x_offset->update(config.x);
188 ((InterpolatePixelsWindow*)thread->window)->y_offset->update(config.y);
189 thread->window->unlock_window();
195 LOAD_CONFIGURATION_MACRO(InterpolatePixelsMain, InterpolatePixelsConfig)
198 void InterpolatePixelsMain::save_data(KeyFrame *keyframe)
202 // cause data to be stored directly in text
203 output.set_shared_output(keyframe->xbuf);
204 output.tag.set_title("INTERPOLATEPIXELS");
205 output.tag.set_property("X", config.x);
206 output.tag.set_property("Y", config.y);
208 output.tag.set_title("/INTERPOLATEPIXELS");
210 output.append_newline();
211 output.terminate_string();
214 void InterpolatePixelsMain::read_data(KeyFrame *keyframe)
218 input.set_shared_input(keyframe->xbuf);
224 result = input.read_tag();
228 if(input.tag.title_is("INTERPOLATEPIXELS"))
230 config.x = input.tag.get_property("X", config.x);
231 config.y = input.tag.get_property("Y", config.y);
237 int InterpolatePixelsMain::process_buffer(VFrame *frame,
238 int64_t start_position,
241 load_configuration();
243 // Set aggregation parameters
244 frame->get_params()->update("INTERPOLATEPIXELS_X", config.x);
245 frame->get_params()->update("INTERPOLATEPIXELS_Y", config.y);
252 //frame->dump_params();
256 // Aggregate with gamma
257 if(next_effect_is(_("Gamma")) ||
258 next_effect_is(_("Histogram")) ||
259 next_effect_is(_("Color Balance")))
266 int color_model = frame->get_color_model();
267 int active_model = BC_CModels::has_alpha(color_model) ?
268 BC_RGBA_FLOAT : BC_RGB_FLOAT;
269 new_temp(frame->get_w(), frame->get_h(), active_model);
270 get_temp()->transfer_from(frame);
272 out_frame = get_output();
273 color_model = out_frame->get_color_model();
274 active_model = BC_CModels::has_alpha(color_model) ?
275 BC_RGBA_FLOAT : BC_RGB_FLOAT;
276 if( active_model != color_model ) {
277 int w = out_frame->get_w(), h = out_frame->get_h();
278 if( out_temp && ( out_temp->get_color_model() != active_model ||
279 out_temp->get_w() != w || out_temp->get_w() != h ) ) {
280 delete out_temp; out_temp = 0;
283 out_temp = new VFrame(w, h, active_model, 0);
284 out_frame = out_temp;
288 engine = new InterpolatePixelsEngine(this);
289 engine->process_packages();
291 if( out_frame != get_output() ) {
292 if( BC_CModels::has_alpha(out_frame->get_color_model()) ) {
293 unsigned char **out_rows = out_frame->get_rows();
294 int w = out_frame->get_w(), h = out_frame->get_h();
295 if( BC_CModels::has_alpha(get_temp()->get_color_model()) ) {
296 unsigned char **in_rows = get_temp()->get_rows();
297 for( int y=0; y<h; ++y ) {
298 float *inp = (float *)in_rows[y];
299 float *outp = (float *)out_rows[y];
300 for( int x=0; x<w; ++x,inp+=4,outp+=4 ) outp[3] = inp[3];
304 for( int y=0; y<h; ++y ) {
305 float *outp = (float *)out_rows[y];
306 for( int x=0; x<w; ++x,outp+=4 ) outp[3] = 1;
310 get_output()->transfer_from(out_frame);
319 // And is offset to recreate the 4 possibilities.
320 // The color values are interpolated in the most basic way.
321 // No adaptive algorithm is used.
323 int InterpolatePixelsMain::handle_opengl()
325 //printf("InterpolatePixelsMain::handle_opengl\n");
329 get_output()->to_texture();
330 get_output()->enable_opengl();
332 const char *shader_stack[16];
333 memset(shader_stack,0, sizeof(shader_stack));
334 int current_shader = 0;
336 INTERPOLATE_COMPILE(shader_stack, current_shader);
338 shader_stack[current_shader] = 0;
339 unsigned int shader = VFrame::make_shader(shader_stack);
341 glUseProgram(shader);
342 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
343 INTERPOLATE_UNIFORMS(shader);
346 get_output()->init_screen();
347 get_output()->bind_texture(0);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
352 get_output()->draw_texture();
353 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
354 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
356 get_output()->set_opengl_state(VFrame::SCREEN);
374 InterpolatePixelsPackage::InterpolatePixelsPackage()
385 InterpolatePixelsUnit::InterpolatePixelsUnit(InterpolatePixelsEngine *server, InterpolatePixelsMain *plugin)
388 this->plugin = plugin;
389 this->server = server;
392 void InterpolatePixelsUnit::process_package(LoadPackage *package)
394 InterpolatePixelsPackage *pkg = (InterpolatePixelsPackage*)package;
395 int h = plugin->get_temp()->get_h();
396 int w = plugin->get_temp()->get_w();
397 int pattern_offset_x = plugin->config.x;
398 int pattern_offset_y = plugin->config.y;
401 int components = BC_CModels::components(plugin->out_frame->get_color_model());
402 float color_matrix[9];
403 memcpy(color_matrix, server->color_matrix, sizeof(color_matrix));
405 // printf("InterpolatePixelsUnit::process_package %d color_matrix=", __LINE__);
406 // for(int i = 0; i < 9; i++)
408 // printf("%f ", color_matrix[i]);
415 // Only supports float because it's useless in any other colormodel.
416 for(int i = y1; i < y2; i++)
418 int pattern_coord_y = (i - pattern_offset_y) % 2;
419 float *prev_row = (float*)plugin->get_temp()->get_rows()[i - 1];
420 float *current_row = (float*)plugin->get_temp()->get_rows()[i];
421 float *next_row = (float*)plugin->get_temp()->get_rows()[i + 1];
422 float *out_row = (float*)plugin->out_frame->get_rows()[i];
424 prev_row += components;
425 current_row += components;
426 next_row += components;
427 out_row += components;
437 if(pattern_coord_y == 0)
439 for(int j = 1; j < w - 1; j++)
441 int pattern_coord_x = (j - pattern_offset_x) % 2;
443 if(pattern_coord_x == 0)
445 r = (prev_row[RED] + next_row[RED]) / 2;
446 g = current_row[GREEN];
447 b = (current_row[-components + BLUE] + current_row[components + BLUE]) / 2;
452 r = (prev_row[-components + RED] +
453 prev_row[components + RED] +
454 next_row[-components + RED] +
455 next_row[components + RED]) / 4;
456 g = (current_row[-components + GREEN] +
458 current_row[components + GREEN] +
459 next_row[GREEN]) / 4;
460 b = current_row[BLUE];
463 // out_row[0] = r * color_matrix[0] + g * color_matrix[1] + b * color_matrix[2];
464 // out_row[1] = r * color_matrix[3] + g * color_matrix[4] + b * color_matrix[5];
465 // out_row[2] = r * color_matrix[6] + g * color_matrix[7] + b * color_matrix[8];
471 prev_row += components;
472 current_row += components;
473 next_row += components;
474 out_row += components;
479 for(int j = 1; j < w - 1; j++)
481 int pattern_coord_x = (j - pattern_offset_x) % 2;
483 if(pattern_coord_x == 0)
485 r = current_row[RED];
486 g = (current_row[-components + GREEN] +
488 current_row[components + GREEN] +
489 next_row[GREEN]) / 4;
490 b = (prev_row[-components + BLUE] +
491 prev_row[components + BLUE] +
492 next_row[-components + BLUE] +
493 next_row[components + BLUE]) / 4;
496 // Bottom right pixel
498 r = (current_row[-components + RED] + current_row[components + RED]) / 2;
499 g = current_row[GREEN];
500 b = (prev_row[BLUE] + next_row[BLUE]) / 2;
503 // out_row[0] = r * color_matrix[0] + g * color_matrix[1] + b * color_matrix[2];
504 // out_row[1] = r * color_matrix[3] + g * color_matrix[4] + b * color_matrix[5];
505 // out_row[2] = r * color_matrix[6] + g * color_matrix[7] + b * color_matrix[8];
511 prev_row += components;
512 current_row += components;
513 next_row += components;
514 out_row += components;
523 InterpolatePixelsEngine::InterpolatePixelsEngine(InterpolatePixelsMain *plugin)
524 : LoadServer(plugin->get_project_smp() + 1, plugin->get_project_smp() + 1)
526 this->plugin = plugin;
529 void InterpolatePixelsEngine::init_packages()
531 for( int i=0; i<9; ++i ) color_matrix[i] = 0;
532 for( int i=0; i<3; ++i ) color_matrix[i*3 + i] = 1;
533 char string[BCTEXTLEN];
535 plugin->get_output()->get_params()->get("DCRAW_MATRIX", string);
537 // printf("InterpolatePixelsEngine::init_packages %d\n", __LINE__);
538 // plugin->get_output()->dump_params();
541 "%f %f %f %f %f %f %f %f %f",
551 for(int i = 0; i < get_total_packages(); i++)
553 InterpolatePixelsPackage *package = (InterpolatePixelsPackage*)get_package(i);
554 package->y1 = plugin->get_temp()->get_h() * i / get_total_packages();
555 package->y2 = plugin->get_temp()->get_h() * (i + 1) / get_total_packages();
560 LoadClient* InterpolatePixelsEngine::new_client()
562 return new InterpolatePixelsUnit(this, plugin);
566 LoadPackage* InterpolatePixelsEngine::new_package()
568 return new InterpolatePixelsPackage;