1 /* === S Y N F I G ========================================================= */
3 ** \brief Preview implementation file
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
34 #include "audiocontainer.h"
35 #include <gtkmm/stock.h>
36 #include <gtkmm/separator.h>
38 #include <synfig/target_scanline.h>
39 #include <synfig/surface.h>
42 #include "asyncrenderer.h"
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
50 using namespace studio;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 /* === E N T R Y P O I N T ================================================= */
62 class studio::Preview::Preview_Target : public Target_Scanline
66 sigc::signal<void, const Preview_Target *> signal_frame_done_;
81 nframes = curframe = 0;
84 const RendDesc &get_rend_desc() const { return desc; }
86 virtual bool set_rend_desc(RendDesc *r)
88 if(Target_Scanline::set_rend_desc(r))
90 /*synfig::warning("Succeeded in setting the desc to new one: %d x %d, %.2f fps [%.2f,%.2f]",
91 desc.get_w(),desc.get_h(),desc.get_frame_rate(),
92 (float)desc.get_time_start(),(float)desc.get_time_end());*/
94 surface.set_wh(desc.get_w(),desc.get_h());
97 nframes = (int)floor((desc.get_time_end() - desc.get_time_start())*desc.get_frame_rate());
99 tbegin = desc.get_time_start();
100 tend = tbegin + nframes/desc.get_frame_rate();
107 virtual bool start_frame(ProgressCallback *cb=NULL)
112 virtual void end_frame()
114 //ok... notify our subscribers...
115 signal_frame_done_(this);
117 //synfig::warning("Finished the frame stuff, and changed time to %.3f",t);
120 virtual Color * start_scanline(int scanline)
122 return surface[scanline];
125 virtual bool end_scanline() {return true;}
127 sigc::signal<void, const Preview_Target *> &signal_frame_done() {return signal_frame_done_;}
129 const Surface &get_surface() const {return surface;}
131 float get_time() const
133 double time = ((nframes-curframe)/(double)nframes)*tbegin
134 + ((curframe)/(double)nframes)*tend;
139 studio::Preview::Preview(const studio::CanvasView::LooseHandle &h, float zoom, float f)
140 :canvasview(h),zoom(zoom),fps(f)
146 void studio::Preview::set_canvasview(const studio::CanvasView::LooseHandle &h)
152 //perhaps reset override values...
153 const RendDesc &r = canvasview->get_canvas()->rend_desc();
154 if(r.get_frame_rate())
156 float rate = 1/r.get_frame_rate();
157 overbegin = false; begintime = r.get_time_start() + r.get_frame_start()*rate;
158 overend = false; endtime = r.get_time_start() + r.get_frame_end()*rate;
163 studio::Preview::~Preview()
165 signal_destroyed_(this); //tell anything that attached to us, we're dying
168 void studio::Preview::render()
172 //render using the preview target
173 etl::handle<Preview_Target> target = new Preview_Target;
175 //connect our information to his...
176 //synfig::warning("Connecting to the end frame function...");
177 target->signal_frame_done().connect(sigc::mem_fun(*this,&Preview::frame_finish));
180 //synfig::warning("Setting Canvas");
181 target->set_canvas(get_canvas());
182 target->set_quality(quality);
185 RendDesc desc = get_canvas()->rend_desc();
187 //set the global fps of the preview
188 set_global_fps(desc.get_frame_rate());
192 int neww = (int)floor(desc.get_w()*zoom+0.5),
193 newh = (int)floor(desc.get_h()*zoom+0.5);
196 /*synfig::warning("Setting the render description: %d x %d, %f fps, [%f,%f]",
197 neww,newh,newfps, overbegin?begintime:(float)desc.get_time_start(),
198 overend?endtime:(float)desc.get_time_end());*/
202 desc.set_frame_rate(newfps);
206 desc.set_time_start(std::max(begintime,(float)desc.get_time_start()));
207 //synfig::warning("Set start time to %.2f...",(float)desc.get_time_start());
211 desc.set_time_end(std::min(endtime,(float)desc.get_time_end()));
212 //synfig::warning("Set end time to %.2f...",(float)desc.get_time_end());
215 //setting the description
217 //HACK - BECAUSE THE RENDERER CAN'T RENDER INCLUDING THE LAST FRAME
218 desc.set_time_end(desc.get_time_end() + 1.3/fps);
220 target->set_rend_desc(&desc);
222 //... first we must clear our current selves of space
225 //now tell it to go... with inherited prog. reporting...
226 //synfig::info("Rendering Asynchronously...");
227 if(renderer) renderer->stop();
228 renderer = new AsyncRenderer(target);
233 static void free_guint8(const guint8 *mem)
238 void studio::Preview::frame_finish(const Preview_Target *targ)
240 //copy image with time to next frame (can just push back)
242 float time = targ->get_time();
243 const Surface &surf = targ->get_surface();
244 const RendDesc& r = targ->get_rend_desc();
246 //synfig::warning("Finished a frame at %f s",time);
249 PixelFormat pf(PF_RGB);
250 const int total_bytes(r.get_w()*r.get_h()*synfig::channels(pf));
252 //synfig::warning("Creating a buffer");
253 unsigned char *buffer((unsigned char*)malloc(total_bytes));
258 //convert all the pixles to the pixbuf... buffer... thing...
259 //synfig::warning("Converting...");
260 convert_color_format(buffer, surf[0], surf.get_w()*surf.get_h(), pf, App::gamma);
264 //uses and manages the memory for the buffer...
265 //synfig::warning("Create a pixmap...");
267 Gdk::Pixbuf::create_from_data(
268 buffer, // pointer to the data
269 Gdk::COLORSPACE_RGB, // the colorspace
270 ((pf&PF_A)==PF_A), // has alpha?
271 8, // bits per sample
272 surf.get_w(), // width
273 surf.get_h(), // height
274 surf.get_w()*synfig::channels(pf), // stride (pitch)
275 sigc::ptr_fun(free_guint8)
278 //add the flipbook element to the list (assume time is correct)
279 //synfig::info("Prev: Adding %f s to the list", time);
280 frames.push_back(fe);
285 #define IMAGIFY_BUTTON(button,stockid,tooltip) \
286 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::ICON_SIZE_BUTTON)); \
287 button->add(*icon); \
288 tooltips.set_tip(*button,tooltip); \
289 icon->set_padding(0,0);\
292 Widget_Preview::Widget_Preview()
293 :Gtk::Table(5,5,false),
294 adj_time_scrub(0,0,1000,1,10,0),
295 scr_time_scrub(adj_time_scrub),
296 b_loop(/*_("Loop")*/),
303 //connect to expose events
304 //signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Preview::redraw));
306 //manage all the change in values etc...
307 adj_time_scrub.signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Preview::slider_move));
308 scr_time_scrub.signal_event().connect(sigc::mem_fun(*this,&Widget_Preview::scroll_move_event));
309 draw_area.signal_expose_event().connect(sigc::mem_fun(*this,&Widget_Preview::redraw));
311 disp_sound.set_time_adjustment(&adj_sound);
314 //Set up signals to modify time value as it should be...
315 disp_sound.signal_start_scrubbing().connect(sigc::mem_fun(*this,&Widget_Preview::scrub_updated));
316 disp_sound.signal_scrub().connect(sigc::mem_fun(*this,&Widget_Preview::scrub_updated));
319 ---------------------------------
327 ---------------------------------
328 |loop|play|stop | hbox
329 |lastl|lastt|rerender|haltrend | hbox
335 Gtk::Button *button = 0;
336 Gtk::Image *icon = 0;
338 //should set up the dialog using attach etc.
339 attach(draw_area, 0, 1, 0, 1);
340 attach(scr_time_scrub, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
345 hbox = manage(new Gtk::HBox);
348 IMAGIFY_BUTTON(button,Gtk::Stock::REFRESH,"Toggle Looping");
349 hbox->pack_start(b_loop,Gtk::PACK_SHRINK,0);
350 //attach(b_loop,0,1,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
352 button = manage(new Gtk::Button(/*_("Play")*/));
353 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::play));
354 IMAGIFY_BUTTON(button,Gtk::Stock::GO_FORWARD,"Play");
355 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
356 //attach(*button,1,2,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
358 button = manage(new Gtk::Button(/*_("Stop")*/));
359 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::stop));
360 IMAGIFY_BUTTON(button,Gtk::Stock::NO,"Stop");
361 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
362 //attach(*button,2,3,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
364 //attack the stop render and erase all buttons to same line...
366 Gtk::VSeparator *vsep = manage(new Gtk::VSeparator);
367 hbox->pack_start(*vsep,Gtk::PACK_SHRINK,0);
370 button = manage(new Gtk::Button(/*_("Halt Render")*/));
371 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::stoprender));
372 IMAGIFY_BUTTON(button,Gtk::Stock::STOP,"Halt Render");
373 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
374 //attach(*button,2,3,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
376 button = manage(new Gtk::Button(/*_("Re-Preview")*/));
377 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::repreview));
378 IMAGIFY_BUTTON(button,Gtk::Stock::CONVERT,"Re-Preview");
379 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
380 //attach(*button,0,2,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
382 button = manage(new Gtk::Button(/*_("Erase All")*/));
383 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::eraseall));
384 IMAGIFY_BUTTON(button,Gtk::Stock::DELETE,"Erase All");
385 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
386 //attach(*button,2,3,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
389 attach(*hbox,0,1,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK|Gtk::FILL);
392 hbox = manage(new Gtk::HBox);
394 Gtk::Label *label = manage(new Gtk::Label("Last Rendered: "));
396 hbox->pack_start(*label,Gtk::PACK_SHRINK,10);
397 //attach(*manage(new Gtk::Label("Last Rendered: ")),0,1,3,4,Gtk::SHRINK,Gtk::SHRINK);
400 hbox->pack_start(l_lasttime,Gtk::PACK_SHRINK,0);
402 attach(*hbox,0,1,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
403 //attach(l_lasttime,0,1,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
406 disp_sound.set_size_request(-1,32);
407 attach(disp_sound,0,1,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
411 //if(draw_area.get_window()) gc_area = Gdk::GC::create(draw_area.get_window());
415 studio::Widget_Preview::~Widget_Preview()
419 void studio::Widget_Preview::update()
421 //the meat goes in this locker...
422 double time = adj_time_scrub.get_value();
424 //find the frame and display it...
427 //synfig::warning("Updating at %.3f s",time);
429 //use time to find closest frame...
430 studio::Preview::FlipBook::const_iterator beg = preview->begin(),end = preview->end();
431 studio::Preview::FlipBook::const_iterator i;
435 //go to current hint if need be...
436 if(currentindex >= 0 && currentindex < (int)preview->numframes())
438 i = beg+currentindex;
441 //we can't have a picture if there are none to get
444 //don't bother with binary search it will just be slower...
446 //synfig::info("Search for time %f",time);
448 //incrementally go in either direction
449 //(bias downward towards beg, because that's what we want)
452 //synfig::info("Look at %f",i->t);
453 if(i->t > time) break;
454 //synfig::info("Go past...");
459 //bias down, so we can't be at end... and it still is valid...
463 //synfig::info("Look at %f",i->t);
464 if(i->t <= time) break;
465 //synfig::info("Go past...");
468 /*i = preview->begin(); end = preview->end();
472 for(;i != end; j = i++)
474 if(i->t > time) break;
477 //we should be at a valid edge since we biased downward
479 //don't get the closest, round down... (if we can)
482 synfig::error("i == end....");
490 currentindex = i-beg;
494 //synfig::warning("Update at: %f seconds (%f s)",time,timedisp);
496 //synfig::warning("success!");
502 if(disp_sound.get_profile() && adj_sound.get_value() != time)
506 //Set the position of the sound (short circuited for sound modifying the time)
508 disp_sound.set_position(time);
509 disp_sound.queue_draw();
512 void studio::Widget_Preview::preview_draw()
514 draw_area.queue_draw();//on_expose_event();
517 bool studio::Widget_Preview::redraw(GdkEventExpose *heh)
519 //And render the drawing area
520 Glib::RefPtr<Gdk::Pixbuf> pxnew, px = currentbuf;
522 if(!px || draw_area.get_height() == 0
523 || px->get_height() == 0 || px->get_width() == 0 /*|| is_visible()*/) //made not need this line
526 //figure out the scaling factors...
530 sx = draw_area.get_width() / (float)px->get_width();
531 sy = draw_area.get_height() / (float)px->get_height();
533 //synfig::info("widget_preview redraw: now to scale the bitmap: %.3f x %.3f",sx,sy);
535 //round to smallest scale (fit entire thing in window without distortion)
539 //scale to a new pixmap and then copy over to the window
540 nw = (int)(px->get_width()*sx);
541 nh = (int)(px->get_height()*sx);
543 if(nw == 0 || nh == 0)return true;
545 pxnew = px->scale_simple(nw,nh,Gdk::INTERP_NEAREST);
547 //synfig::info("Now to draw to the window...");
549 Glib::RefPtr<Gdk::Window> wind = draw_area.get_window();
550 Glib::RefPtr<Gdk::Drawable> surf = Glib::RefPtr<Gdk::Drawable>::cast_static(wind);
551 Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(wind);
554 Gdk::Rectangle r(0,0,draw_area.get_width(),draw_area.get_height());
555 draw_area.get_window()->begin_paint_rect(r);
558 if(!wind) synfig::warning("The destination window is broken...");
559 if(!surf) synfig::warning("The destination is not drawable...");
563 /* Options for drawing...
564 1) store with alpha, then clear and render with alpha every frame
565 - more time consuming
567 2) store with just pixel info
570 + better memory footprint
572 //px->composite(const Glib::RefPtr<Gdk::Pixbuf>& dest, int dest_x, int dest_y, int dest_width, int dest_height, double offset_x, double offset_y, double scale_x, double scale_y, InterpType interp_type, int overall_alpha) const
577 0, 0, // Source X and Y
578 0, 0, // Dest X and Y
579 -1,-1, // Width and Height
580 Gdk::RGB_DITHER_NONE, // RgbDither
581 0, 0 // Dither offset X and Y
586 Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
587 Glib::ustring timecode(Time((double)timedisp).round(preview->get_global_fps())
588 .get_string(preview->get_global_fps(),
589 App::get_time_format()));
590 //synfig::info("Time for preview draw is: %s for time %g", timecode.c_str(), adj_time_scrub.get_value());
592 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
593 layout->set_text(timecode);
594 surf->draw_layout(gc,4,4,layout);
598 draw_area.get_window()->end_paint();
600 //synfig::warning("Refresh the draw area");
601 //make sure the widget refreshes
606 bool studio::Widget_Preview::play_update()
608 float diff = timer.pop_time();
609 //synfig::info("Play update: diff = %.2f",diff);
613 //we go to the next one...
614 double time = adj_time_scrub.get_value() + diff;
616 //adjust it to be synced with the audio if it can...
618 double newtime = audiotime;
619 if(audio && audio->is_playing()) audio->get_current_time(newtime);
621 if(newtime != audiotime)
623 //synfig::info("Adjusted time from %.3lf to %.3lf", time,newtime);
624 time = audiotime = newtime;
628 //Looping conditions...
629 if(time >= adj_time_scrub.get_upper())
633 time = adj_time_scrub.get_lower();// + time-adj_time_scrub.get_upper();
637 time = adj_time_scrub.get_upper();
638 adj_time_scrub.set_value(time);
642 //synfig::info("Play Stopped: time set to %f",adj_time_scrub.get_value());
647 //set the new time...
648 adj_time_scrub.set_value(time);
649 adj_time_scrub.value_changed();
651 //update the window to the correct image we might want to do this later...
653 //synfig::warning("Did update pu");
658 void studio::Widget_Preview::slider_move()
663 //synfig::warning("Did update sm");
667 //for other things updating the value changed signal...
668 void studio::Widget_Preview::scrub_updated(double t)
672 //Attempt at being more accurate... the time is adjusted to be exactly where the sound says it is
676 if(!audio->isPaused())
678 audio->get_current_time(t);
682 //synfig::info("Scrubbing to %.3f, setting adj to %.3f",oldt,t);
684 if(adj_time_scrub.get_value() != t)
686 adj_time_scrub.set_value(t);
687 adj_time_scrub.value_changed();
691 void studio::Widget_Preview::disconnect_preview(Preview *prev)
696 prevchanged.disconnect();
700 void studio::Widget_Preview::set_preview(handle<Preview> prev)
704 synfig::info("Setting preview");
706 //stop playing the mini animation...
711 //set the internal values
712 float rate = preview->get_fps();
713 synfig::info(" FPS = %f",rate);
716 float start = preview->get_begintime();
717 float end = preview->get_endtime();
721 adj_time_scrub.set_lower(start);
722 adj_time_scrub.set_upper(end);
723 adj_time_scrub.set_value(start);
724 adj_time_scrub.set_step_increment(rate);
725 adj_time_scrub.set_page_increment(10*rate);
727 //if the begin time and the end time are the same there is only a single frame
728 singleframe = end==start;
731 adj_time_scrub.set_lower(0);
732 adj_time_scrub.set_upper(0);
733 adj_time_scrub.set_value(0);
734 adj_time_scrub.set_step_increment(0);
735 adj_time_scrub.set_page_increment(0);
739 //connect so future information will be found...
740 prevchanged = prev->signal_changed().connect(sigc::mem_fun(*this,&Widget_Preview::whenupdated));
741 prev->signal_destroyed().connect(sigc::mem_fun(*this,&Widget_Preview::disconnect_preview));
743 //synfig::warning("Did update sp");
748 void studio::Widget_Preview::whenupdated()
750 l_lasttime.set_text((Time((double)(--preview->end())->t)
751 .round(preview->get_global_fps())
752 .get_string(preview->get_global_fps(),App::get_time_format())));
756 void studio::Widget_Preview::clear()
759 prevchanged.disconnect();
762 void studio::Widget_Preview::play()
764 if(preview && !playing)
766 //synfig::info("Playing at %lf",adj_time_scrub.get_value());
767 //audiotime = adj_time_scrub.get_value();
770 //adj_time_scrub.set_value(adj_time_scrub.get_lower());
771 update(); //we don't want to call play update because that will try to advance the timer
772 //synfig::warning("Did update p");
774 //approximate length of time in seconds, right?
775 double rate = /*std::min(*/adj_time_scrub.get_step_increment()/*,1/30.0)*/;
776 int timeout = (int)floor(1000*rate);
778 //synfig::info(" rate = %.3lfs = %d ms",rate,timeout);
780 signal_play_(adj_time_scrub.get_value());
783 if(audio) audio->play(adj_time_scrub.get_value());
785 timecon = Glib::signal_timeout().connect(sigc::mem_fun(*this,&Widget_Preview::play_update),timeout);
791 void studio::Widget_Preview::play_stop()
795 if(audio) audio->stop(); //!< stop the audio
796 //synfig::info("Stopping...");
799 void studio::Widget_Preview::stop()
801 //synfig::warning("stopping");
803 timecon.disconnect();
806 bool studio::Widget_Preview::scroll_move_event(GdkEvent *event)
810 case GDK_BUTTON_PRESS:
812 if(event->button.button == 1 || event->button.button == 3)
824 void studio::Widget_Preview::set_audioprofile(etl::handle<AudioProfile> p)
826 disp_sound.set_profile(p);
829 void studio::Widget_Preview::set_audio(etl::handle<AudioContainer> a)
833 //disconnect any previous signals
834 scrstartcon.disconnect(); scrstopcon.disconnect(); scrubcon.disconnect();
836 //connect the new signals
837 scrstartcon = disp_sound.signal_start_scrubbing().connect(sigc::mem_fun(*a,&AudioContainer::start_scrubbing));
838 scrstopcon = disp_sound.signal_stop_scrubbing().connect(sigc::mem_fun(*a,&AudioContainer::stop_scrubbing));
839 scrubcon = disp_sound.signal_scrub().connect(sigc::mem_fun(*a,&AudioContainer::scrub));
842 void studio::Widget_Preview::seek(float t)
845 adj_time_scrub.set_value(t);
848 void studio::Widget_Preview::repreview()
854 preview->get_canvasview()->preview_option();
858 void studio::Widget_Preview::stoprender()
862 preview->renderer.detach();
866 void studio::Widget_Preview::eraseall()