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"
26 #include "filesystem.h"
29 #include "overlayframe.h"
32 #include "shapewipe.h"
39 #define SHAPE_SEARCHPATH "/shapes"
40 #define DEFAULT_SHAPE "circle"
41 // feather slider range log2 = -10 .. -1 == 9.8e-4 .. 0.5
42 #define SHAPE_FLOG_MIN -10.
43 #define SHAPE_FLOG_MAX -1.
44 #define SHAPE_FMIN expf(M_LN2*SHAPE_FLOG_MIN)
45 #define SHAPE_FMAX expf(M_LN2*SHAPE_FLOG_MAX)
47 REGISTER_PLUGIN(ShapeWipeMain)
49 ShapeWipeConfig::ShapeWipeConfig()
54 strcpy(shape_name, DEFAULT_SHAPE);
56 ShapeWipeConfig::~ShapeWipeConfig()
60 void ShapeWipeConfig::read_xml(KeyFrame *keyframe)
63 input.set_shared_input(keyframe->xbuf);
65 while( !input.read_tag() ) {
66 if( input.tag.title_is("SHAPEWIPE") ) {
67 direction = input.tag.get_property("DIRECTION", direction);
68 feather = input.tag.get_property("FEATHER", feather);
69 preserve_aspect = input.tag.get_property("PRESERVE_ASPECT", preserve_aspect);
70 input.tag.get_property("SHAPE_NAME", shape_name);
74 void ShapeWipeConfig::save_xml(KeyFrame *keyframe)
77 output.set_shared_output(keyframe->xbuf);
78 output.tag.set_title("SHAPEWIPE");
79 output.tag.set_property("DIRECTION", direction);
80 output.tag.set_property("FEATHER", feather);
81 output.tag.set_property("PRESERVE_ASPECT", preserve_aspect);
82 output.tag.set_property("SHAPE_NAME", shape_name);
84 output.tag.set_title("/SHAPEWIPE");
86 output.terminate_string();
90 ShapeWipeW2B::ShapeWipeW2B(ShapeWipeMain *plugin,
91 ShapeWipeWindow *window, int x, int y)
92 : BC_Radial(x, y, plugin->config.direction == 0, _("White to Black"))
94 this->plugin = plugin;
95 this->window = window;
98 int ShapeWipeW2B::handle_event()
101 plugin->config.direction = 0;
102 window->right->update(0);
103 plugin->send_configure_change();
107 ShapeWipeB2W::ShapeWipeB2W(ShapeWipeMain *plugin,
108 ShapeWipeWindow *window, int x, int y)
109 : BC_Radial(x, y, plugin->config.direction == 1, _("Black to White"))
111 this->plugin = plugin;
112 this->window = window;
115 int ShapeWipeB2W::handle_event()
118 plugin->config.direction = 1;
119 window->left->update(0);
120 plugin->send_configure_change();
125 ShapeWipePreserveAspectRatio::ShapeWipePreserveAspectRatio(ShapeWipeMain *plugin,
126 ShapeWipeWindow *window, int x, int y)
127 : BC_CheckBox (x, y, plugin->config.preserve_aspect, _("Preserve shape aspect ratio"))
129 this->plugin = plugin;
130 this->window = window;
133 int ShapeWipePreserveAspectRatio::handle_event()
135 plugin->config.preserve_aspect = get_value();
136 plugin->send_configure_change();
141 ShapeWipeTumble::ShapeWipeTumble(ShapeWipeMain *client,
142 ShapeWipeWindow *window, int x, int y)
145 this->client = client;
146 this->window = window;
150 int ShapeWipeTumble::handle_up_event()
152 window->prev_shape();
156 int ShapeWipeTumble::handle_down_event()
158 window->next_shape();
163 ShapeWipeFeather::ShapeWipeFeather(ShapeWipeMain *client,
164 ShapeWipeWindow *window, int x, int y)
165 : BC_TumbleTextBox(window,
166 bclip(client->config.feather, SHAPE_FMIN, SHAPE_FMAX),
167 SHAPE_FMIN, SHAPE_FMAX, x, y, 64, 3)
169 this->client = client;
170 this->window = window;
173 int ShapeWipeFeather::handle_event()
175 float v = atof(get_text());
176 bclamp(v, SHAPE_FMIN, SHAPE_FMAX);
177 client->config.feather = v;
178 float sv = log(v)/M_LN2;
179 window->shape_fslider->update(sv);
180 client->send_configure_change();
184 ShapeWipeFSlider::ShapeWipeFSlider(ShapeWipeMain *client,
185 ShapeWipeWindow *window, int x, int y, int w)
186 : BC_FSlider(x, y, 0, w, w, SHAPE_FLOG_MIN, SHAPE_FLOG_MAX,
187 log(bclip(client->config.feather, SHAPE_FMIN, SHAPE_FMAX))/M_LN2)
189 this->client = client;
190 this->window = window;
191 set_precision(0.001);
192 set_pagination(0.01, 0.1);
193 enable_show_value(0);
196 int ShapeWipeFSlider::handle_event()
198 float v = get_value();
199 float vv = exp(M_LN2*v);
200 client->config.feather = vv;
201 window->shape_feather->update(vv);
202 client->send_configure_change();
206 ShapeWipeReset::ShapeWipeReset(ShapeWipeMain *client,
207 ShapeWipeWindow *window, int x, int y)
208 : BC_Button(x, y, client->get_theme()->get_image_set("reset_button"))
210 this->client = client;
211 this->window = window;
212 set_tooltip(_("Reset feather"));
215 int ShapeWipeReset::handle_event()
217 window->shape_fslider->update(SHAPE_FLOG_MIN);
218 float v = SHAPE_FMIN;
219 window->shape_feather->update(v);
220 client->config.feather = v;
221 client->send_configure_change();
225 int ShapeWipeReset::calculate_w(ShapeWipeMain *client)
227 VFrame **reset_images = client->get_theme()->get_image_set("reset_button");
228 return reset_images[0]->get_w();
232 ShapeWipeShape::ShapeWipeShape(ShapeWipeMain *client,
233 ShapeWipeWindow *window, int x, int y,
234 int text_w, int list_h)
235 : BC_PopupTextBox(window, &window->shapes, client->config.shape_name,
236 x, y, text_w, list_h)
238 this->client = client;
239 this->window = window;
242 int ShapeWipeShape::handle_event()
244 strcpy(client->config.shape_name, get_text());
245 client->send_configure_change();
250 ShapeWipeWindow::ShapeWipeWindow(ShapeWipeMain *plugin)
251 : PluginClientWindow(plugin, 425, 215, 425, 215, 0)
253 this->plugin = plugin;
257 ShapeWipeWindow::~ShapeWipeWindow()
259 shapes.remove_all_objects();
260 delete shape_feather;
264 void ShapeWipeWindow::create_objects()
267 lock_window("ShapeWipeWindow::create_objects");
268 int pad = 10, margin = 10;
269 int x = margin, y = margin;
270 int ww = get_w() - 2*margin;
272 plugin->init_shapes();
273 for( int i=0; i<plugin->shape_titles.size(); ++i ) {
274 shapes.append(new BC_ListBoxItem(plugin->shape_titles.get(i)));
278 add_subwindow(bar = new BC_TitleBar(x, y, ww, x+ww/12,
279 pad, _("Wipe"), MEDIUMFONT));
280 y += bar->get_h() + pad;
282 add_subwindow(title = new BC_Title(x, y, _("Shape:")));
285 int tw = ww - x1 - ShapeWipeTumble::calculate_w() - pad -
286 BC_WindowBase::get_resources()->listbox_button[0]->get_w();
287 shape_text = new ShapeWipeShape(plugin, this, x1, y, tw, 200);
288 shape_text->create_objects();
289 x += shape_text->get_w() + pad;
290 add_subwindow(new ShapeWipeTumble(plugin,
292 y += shape_text->get_h() + pad;
295 add_subwindow(title = new BC_Title(x, y, _("Feather:")));
297 shape_feather = new ShapeWipeFeather(plugin, this, x, y);
298 shape_feather->create_objects();
299 shape_feather->set_log_floatincrement(1);
300 x += shape_feather->get_w() + 2*pad;
301 int sw = ww - ShapeWipeReset::calculate_w(plugin) - pad - x;
302 add_subwindow(shape_fslider = new ShapeWipeFSlider(plugin, this, x, y, sw));
303 x += shape_fslider->get_w() + 2*pad;
304 add_subwindow(shape_reset = new ShapeWipeReset(plugin, this, x, y));
305 y += shape_fslider->get_h() + pad;
308 ShapeWipePreserveAspectRatio *aspect_ratio;
309 add_subwindow(aspect_ratio = new ShapeWipePreserveAspectRatio(
310 plugin, this, x, y));
311 y += aspect_ratio->get_h() + pad;
313 add_subwindow(bar = new BC_TitleBar(x, y, ww, x+ww/12,
314 pad, _("Direction"), MEDIUMFONT));
315 y += bar->get_h() + pad;
317 add_subwindow(left = new ShapeWipeW2B(plugin, this, x, y));
319 add_subwindow(right = new ShapeWipeB2W(plugin, this, x, y));
325 void ShapeWipeWindow::next_shape()
327 ShapeWipeConfig &config = plugin->config;
328 int k = plugin->shape_titles.size();
329 while( --k>=0 && strcmp(plugin->shape_titles.get(k),config.shape_name) );
332 if( ++k >= plugin->shape_titles.size() ) k = 0;
333 strcpy(config.shape_name, plugin->shape_titles.get(k));
334 shape_text->update(config.shape_name);
336 client->send_configure_change();
339 void ShapeWipeWindow::prev_shape()
341 ShapeWipeConfig &config = plugin->config;
342 int k = plugin->shape_titles.size();
343 while( --k>=0 && strcmp(plugin->shape_titles.get(k),config.shape_name) );
346 if( --k < 0 ) k = plugin->shape_titles.size()-1;
347 strcpy(config.shape_name, plugin->shape_titles.get(k));
348 shape_text->update(config.shape_name);
350 client->send_configure_change();
354 ShapeWipeMain::ShapeWipeMain(PluginServer *server)
355 : PluginVClient(server)
360 current_filename[0] = '\0';
365 last_preserve_aspect = 0;
366 shapes_initialized = 0;
367 shape_paths.set_array_delete();
368 shape_titles.set_array_delete();
371 ShapeWipeMain::~ShapeWipeMain()
373 reset_pattern_image();
374 shape_paths.remove_all_objects();
375 shape_titles.remove_all_objects();
379 const char* ShapeWipeMain::plugin_title() { return N_("Shape Wipe"); }
380 int ShapeWipeMain::is_transition() { return 1; }
381 int ShapeWipeMain::uses_gui() { return 1; }
383 NEW_WINDOW_MACRO(ShapeWipeMain, ShapeWipeWindow);
385 void ShapeWipeMain::read_data(KeyFrame *keyframe)
387 config.read_xml(keyframe);
389 void ShapeWipeMain::save_data(KeyFrame *keyframe)
391 config.save_xml(keyframe);
394 void ShapeWipeMain::init_shapes()
396 if( !shapes_initialized ) {
398 fs.set_filter("*.png");
399 char shape_path[BCTEXTLEN];
400 sprintf(shape_path, "%s%s", get_plugin_dir(), SHAPE_SEARCHPATH);
401 fs.update(shape_path);
403 for( int i=0; i<fs.total_files(); ++i ) {
404 FileItem *file_item = fs.get_entry(i);
405 if( !file_item->get_is_dir() ) {
406 shape_paths.append(cstrdup(file_item->get_path()));
407 char *ptr = cstrdup(file_item->get_name());
408 char *ptr2 = strrchr(ptr, '.');
410 shape_titles.append(ptr);
414 shapes_initialized = 1;
419 int ShapeWipeMain::load_configuration()
421 read_data(get_prev_keyframe(get_source_position()));
425 int ShapeWipeMain::read_pattern_image(char *shape_name,
426 int new_frame_width, int new_frame_height)
437 png_structp png_ptr = 0;
438 png_infop info_ptr = 0;
439 png_infop end_info = 0;
440 png_bytep *image = 0;
442 frame_width = new_frame_width;
443 frame_height = new_frame_height;
446 // Convert name to filename
447 int k = shape_paths.size();
448 while( --k>=0 && strcmp(shape_titles[k], shape_name) );
451 strcpy(current_filename, shape_paths[k]);
452 fp = fopen(current_filename, "rb");
456 fread(header, 1, 8, fp);
457 is_png = !png_sig_cmp(header, 0, 8);
458 if( !is_png ) ret = 1;
461 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
462 if( !png_ptr ) ret = 1;
465 /* Tell libpng we already checked the first 8 bytes */
466 png_set_sig_bytes(png_ptr, 8);
467 info_ptr = png_create_info_struct(png_ptr);
468 if( !info_ptr ) ret = 1;
471 end_info = png_create_info_struct(png_ptr);
472 if( !end_info ) ret = 1;
475 png_init_io(png_ptr, fp);
476 png_read_info(png_ptr, info_ptr);
478 color_type = png_get_color_type(png_ptr, info_ptr);
479 bit_depth = png_get_bit_depth(png_ptr, info_ptr);
480 width = png_get_image_width (png_ptr, info_ptr);
481 height = png_get_image_height(png_ptr, info_ptr);
483 /* Skip the alpha channel if present
484 * stripping alpha currently doesn't work in conjunction with
485 * converting to grayscale in libpng */
486 pixel_width = color_type & PNG_COLOR_MASK_ALPHA ? 2 : 1;
487 /* Convert 16 bit data to 8 bit */
488 if( bit_depth == 16 ) png_set_strip_16(png_ptr);
489 /* Expand to 1 pixel per byte if necessary */
490 if( bit_depth < 8 ) png_set_packing(png_ptr);
492 /* Convert to grayscale */
493 if( color_type == PNG_COLOR_TYPE_RGB ||
494 color_type == PNG_COLOR_TYPE_RGB_ALPHA )
495 png_set_rgb_to_gray_fixed(png_ptr, 1, -1, -1);
497 /* Allocate memory to hold the original png image */
498 image = (png_bytep*)new png_bytep[height];
499 for( row=0; row<(int)height; ++row )
500 image[row] = new png_byte[width*pixel_width];
502 /* Allocate memory for the pattern image that will actually be
503 * used for the wipe */
504 pattern_image = new unsigned char*[frame_height];
506 png_read_image(png_ptr, image);
507 png_read_end(png_ptr, end_info);
509 double row_factor, col_factor;
510 double row_offset = 0.5, col_offset = 0.5; // for rounding
512 if( config.preserve_aspect && aspect_w && aspect_h ) {
513 row_factor = (height-1)/aspect_h;
514 col_factor = (width-1)/aspect_w;
515 if( row_factor < col_factor )
516 col_factor = row_factor;
518 row_factor = col_factor;
519 row_factor *= aspect_h/(double)(frame_height-1);
520 col_factor *= aspect_w/(double)(frame_width-1);
522 // center the pattern over the frame
523 row_offset += (height-1-(frame_height-1)*row_factor)/2;
524 col_offset += (width-1-(frame_width-1)*col_factor)/2;
527 // Stretch (or shrink) the pattern image to fill the frame
528 row_factor = (double)(height-1)/(double)(frame_height-1);
529 col_factor = (double)(width-1)/(double)(frame_width-1);
531 // first, determine range min..max
532 for( int y=0; y<frame_height; ++y ) {
533 row = (int)(row_factor*y + row_offset);
534 for( int x=0; x<frame_width; ++x ) {
535 col = (int)(col_factor*x + col_offset)*pixel_width;
536 value = image[row][col];
537 if( value < min_value ) min_value = value;
538 if( value > max_value ) max_value = value;
541 int range = max_value - min_value;
542 if( !range ) range = 1;
543 // scale to fade normalized pattern_image
544 for( int y=0; y<frame_height; ++y ) {
545 row = (int)(row_factor*y + row_offset);
546 pattern_image[y] = new unsigned char[frame_width];
547 for( int x=0; x<frame_width; ++x ) {
548 col = (int)(col_factor*x + col_offset)*pixel_width;
549 value = image[row][col];
550 pattern_image[y][x] = 0xff*(value - min_value) / range;
555 if( png_ptr || info_ptr || end_info )
556 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
560 for( row=0; row<(int)height; ++row )
561 delete [] image[row];
567 void ShapeWipeMain::reset_pattern_image()
569 if( pattern_image ) {
570 for( int y=0; y<frame_height; ++y )
571 delete [] pattern_image[y];
572 delete [] pattern_image; pattern_image = 0;
574 max_value = 0; // updated in read_pattern_image
578 #define SHAPEBLEND(type, components, tmp_type) { \
579 float scale = feather ? 1/feather : 0xff; \
580 type **in_rows = (type**)input->get_rows(); \
581 type **out_rows = (type**)output->get_rows(); \
582 for( int y=y1; y<y2; ++y ) { \
583 type *in_row = (type*) in_rows[y]; \
584 type *out_row = (type*)out_rows[y]; \
585 unsigned char *pattern_row = pattern_image[y]; \
586 for( int x=0; x<w; ++x ) { \
587 tmp_type d = (pattern_row[x] - threshold) * scale; \
588 if( d > 0xff ) d = 0xff; \
589 else if( d < -0xff ) d = -0xff; \
590 tmp_type a = (d + 0xff) / 2, b = 0xff - a; \
591 for( int i=0; i<components; ++i ) { \
592 type ic = in_row[i], oc = out_row[i]; \
593 out_row[i] = (ic * a + oc * b) / 0xff; \
595 in_row += components; out_row += components; \
600 int ShapeWipeMain::process_realtime(VFrame *input, VFrame *output)
603 this->output = output;
604 int w = input->get_w();
605 int h = input->get_h();
607 load_configuration();
609 if( strncmp(config.shape_name, current_name, BCTEXTLEN) ||
610 config.preserve_aspect != last_preserve_aspect ) {
611 reset_pattern_image();
613 if ( !pattern_image ) {
614 if( read_pattern_image(config.shape_name, w, h) ) {
615 fprintf(stderr, _("Shape Wipe: cannot load shape %s\n"),
617 current_filename[0] = 0;
620 strncpy(current_name, config.shape_name, BCTEXTLEN);
621 last_preserve_aspect = config.preserve_aspect;
624 float fade = (float)PluginClient::get_source_position() /
625 (float)PluginClient::get_total_len();
626 if( !config.direction ) fade = 1 - fade;
627 threshold = fade * 0xff;
629 int slices = w*h/0x40000+1;
630 int max_slices = BC_Resources::machine_cpus/2;
631 if( slices > max_slices ) slices = max_slices;
632 if( slices < 1 ) slices = 1;
633 if( engine && engine->get_total_clients() != slices ) {
634 delete engine; engine = 0;
637 engine = new ShapeEngine(this, slices, slices);
639 engine->process_packages();
644 ShapePackage::ShapePackage()
649 ShapeUnit::ShapeUnit(ShapeEngine *server) : LoadClient(server)
651 this->server = server;
654 ShapeUnit::~ShapeUnit()
658 void ShapeUnit::process_package(LoadPackage *package)
660 VFrame *input = server->plugin->input;
661 VFrame *output = server->plugin->output;
662 int w = input->get_w();
664 unsigned char **pattern_image = server->plugin->pattern_image;
665 unsigned char threshold = server->plugin->threshold;
666 float feather = server->plugin->config.feather;
667 ShapePackage *pkg = (ShapePackage*)package;
668 int y1 = pkg->y1, y2 = pkg->y2;
670 switch(input->get_color_model()) {
672 SHAPEBLEND(float, 3, float)
676 SHAPEBLEND(unsigned char, 3, int)
679 SHAPEBLEND(float, 4, float)
683 SHAPEBLEND(unsigned char, 4, int)
687 SHAPEBLEND(uint16_t, 3, int64_t)
689 case BC_RGBA16161616:
690 case BC_YUVA16161616:
691 SHAPEBLEND(uint16_t, 4, int64_t)
697 ShapeEngine::ShapeEngine(ShapeWipeMain *plugin,
698 int total_clients, int total_packages)
699 : LoadServer(total_clients, total_packages)
701 this->plugin = plugin;
704 ShapeEngine::~ShapeEngine()
709 void ShapeEngine::init_packages()
711 int y = 0, h1 = plugin->input->get_h()-1;
712 int total_packages = get_total_packages();
713 for(int i = 0; i<total_packages; ) {
714 ShapePackage *pkg = (ShapePackage*)get_package(i++);
716 y = h1 * i / total_packages;
721 LoadClient* ShapeEngine::new_client()
723 return new ShapeUnit(this);
726 LoadPackage* ShapeEngine::new_package()
728 return new ShapePackage;