1 /* === S Y N F I G ========================================================= */
3 ** \brief Preview implementation file
5 ** $Id: preview.cpp,v 1.2 2005/01/10 08:13:44 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #include "audiocontainer.h"
34 #include <gtkmm/stock.h>
35 #include <gtkmm/separator.h>
37 #include <synfig/target_scanline.h>
38 #include <synfig/surface.h>
41 #include "asyncrenderer.h"
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
49 using namespace studio;
51 /* === M A C R O S ========================================================= */
53 /* === G L O B A L S ======================================================= */
55 /* === P R O C E D U R E S ================================================= */
57 /* === M E T H O D S ======================================================= */
59 /* === E N T R Y P O I N T ================================================= */
61 class studio::Preview::Preview_Target : public Target_Scanline
65 sigc::signal<void, const Preview_Target *> signal_frame_done_;
80 nframes = curframe = 0;
83 const RendDesc &get_rend_desc() const { return desc; }
85 virtual bool set_rend_desc(RendDesc *r)
87 if(Target_Scanline::set_rend_desc(r))
89 /*synfig::warning("Succeeded in setting the desc to new one: %d x %d, %.2f fps [%.2f,%.2f]",
90 desc.get_w(),desc.get_h(),desc.get_frame_rate(),
91 (float)desc.get_time_start(),(float)desc.get_time_end());*/
93 surface.set_wh(desc.get_w(),desc.get_h());
96 nframes = (int)floor((desc.get_time_end() - desc.get_time_start())*desc.get_frame_rate());
98 tbegin = desc.get_time_start();
99 tend = tbegin + nframes/desc.get_frame_rate();
106 virtual bool start_frame(ProgressCallback *cb=NULL)
111 virtual void end_frame()
113 //ok... notify our subscribers...
114 signal_frame_done_(this);
116 //synfig::warning("Finished the frame stuff, and changed time to %.3f",t);
119 virtual Color * start_scanline(int scanline)
121 return surface[scanline];
124 virtual bool end_scanline() {return true;}
126 sigc::signal<void, const Preview_Target *> &signal_frame_done() {return signal_frame_done_;}
128 const Surface &get_surface() const {return surface;}
130 float get_time() const
132 double time = ((nframes-curframe)/(double)nframes)*tbegin
133 + ((curframe)/(double)nframes)*tend;
138 studio::Preview::Preview(const studio::CanvasView::LooseHandle &h, float zoom, float f)
139 :canvasview(h),zoom(zoom),fps(f)
145 void studio::Preview::set_canvasview(const studio::CanvasView::LooseHandle &h)
151 //perhaps reset override values...
152 const RendDesc &r = canvasview->get_canvas()->rend_desc();
153 if(r.get_frame_rate())
155 float rate = 1/r.get_frame_rate();
156 overbegin = false; begintime = r.get_time_start() + r.get_frame_start()*rate;
157 overend = false; endtime = r.get_time_start() + r.get_frame_end()*rate;
162 studio::Preview::~Preview()
164 signal_destroyed_(this); //tell anything that attached to us, we're dying
167 void studio::Preview::render()
171 //render using the preview target
172 etl::handle<Preview_Target> target = new Preview_Target;
174 //connect our information to his...
175 //synfig::warning("Connecting to the end frame function...");
176 target->signal_frame_done().connect(sigc::mem_fun(*this,&Preview::frame_finish));
179 //synfig::warning("Setting Canvas");
180 target->set_canvas(get_canvas());
181 target->set_quality(quality);
184 RendDesc desc = get_canvas()->rend_desc();
186 //set the global fps of the preview
187 set_global_fps(desc.get_frame_rate());
191 int neww = (int)floor(desc.get_w()*zoom+0.5),
192 newh = (int)floor(desc.get_h()*zoom+0.5);
195 /*synfig::warning("Setting the render description: %d x %d, %f fps, [%f,%f]",
196 neww,newh,newfps, overbegin?begintime:(float)desc.get_time_start(),
197 overend?endtime:(float)desc.get_time_end());*/
201 desc.set_frame_rate(newfps);
205 desc.set_time_start(std::max(begintime,(float)desc.get_time_start()));
206 //synfig::warning("Set start time to %.2f...",(float)desc.get_time_start());
210 desc.set_time_end(std::min(endtime,(float)desc.get_time_end()));
211 //synfig::warning("Set end time to %.2f...",(float)desc.get_time_end());
214 //setting the description
216 //HACK - BECAUSE THE RENDERER CAN'T RENDER INCLUDING THE LAST FRAME
217 desc.set_time_end(desc.get_time_end() + 1.3/fps);
219 target->set_rend_desc(&desc);
221 //... first we must clear our current selves of space
224 //now tell it to go... with inherited prog. reporting...
225 //synfig::info("Rendering Asynchronously...");
226 if(renderer) renderer->stop();
227 renderer = new AsyncRenderer(target);
232 static void free_guint8(const guint8 *mem)
237 void studio::Preview::frame_finish(const Preview_Target *targ)
239 //copy image with time to next frame (can just push back)
241 float time = targ->get_time();
242 const Surface &surf = targ->get_surface();
243 const RendDesc& r = targ->get_rend_desc();
245 //synfig::warning("Finished a frame at %f s",time);
248 PixelFormat pf(PF_RGB);
249 const int total_bytes(r.get_w()*r.get_h()*synfig::channels(pf));
251 //synfig::warning("Creating a buffer");
252 unsigned char *buffer((unsigned char*)malloc(total_bytes));
257 //convert all the pixles to the pixbuf... buffer... thing...
258 //synfig::warning("Converting...");
259 convert_color_format(buffer, surf[0], surf.get_w()*surf.get_h(), pf, App::gamma);
263 //uses and manages the memory for the buffer...
264 //synfig::warning("Create a pixmap...");
266 Gdk::Pixbuf::create_from_data(
267 buffer, // pointer to the data
268 Gdk::COLORSPACE_RGB, // the colorspace
269 ((pf&PF_A)==PF_A), // has alpha?
270 8, // bits per sample
271 surf.get_w(), // width
272 surf.get_h(), // height
273 surf.get_w()*synfig::channels(pf), // stride (pitch)
274 sigc::ptr_fun(free_guint8)
277 //add the flipbook element to the list (assume time is correct)
278 //synfig::info("Prev: Adding %f s to the list", time);
279 frames.push_back(fe);
284 #define IMAGIFY_BUTTON(button,stockid) \
285 icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::ICON_SIZE_BUTTON)); \
286 button->add(*icon); \
287 icon->set_padding(0,0);\
290 Widget_Preview::Widget_Preview()
291 :Gtk::Table(5,5,false),
292 adj_time_scrub(0,0,1000,1,10,0),
293 scr_time_scrub(adj_time_scrub),
294 b_loop(/*_("Loop")*/),
299 //connect to expose events
300 //signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Preview::redraw));
302 //manage all the change in values etc...
303 adj_time_scrub.signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Preview::slider_move));
304 scr_time_scrub.signal_event().connect(sigc::mem_fun(*this,&Widget_Preview::scroll_move_event));
305 draw_area.signal_expose_event().connect(sigc::mem_fun(*this,&Widget_Preview::redraw));
307 disp_sound.set_time_adjustment(&adj_sound);
310 //Set up signals to modify time value as it should be...
311 disp_sound.signal_start_scrubbing().connect(sigc::mem_fun(*this,&Widget_Preview::scrub_updated));
312 disp_sound.signal_scrub().connect(sigc::mem_fun(*this,&Widget_Preview::scrub_updated));
315 ---------------------------------
323 ---------------------------------
324 |loop|play|stop | hbox
325 |lastl|lastt|rerender|haltrend | hbox
331 Gtk::Button *button = 0;
332 Gtk::Image *icon = 0;
334 //should set up the dialog using attach etc.
335 attach(draw_area, 0, 1, 0, 1);
336 attach(scr_time_scrub, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
341 hbox = manage(new Gtk::HBox);
344 IMAGIFY_BUTTON(button,Gtk::Stock::REFRESH);
345 hbox->pack_start(b_loop,Gtk::PACK_SHRINK,0);
346 //attach(b_loop,0,1,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
348 button = manage(new Gtk::Button(/*_("Play")*/));
349 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::play));
350 IMAGIFY_BUTTON(button,Gtk::Stock::GO_FORWARD);
351 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
352 //attach(*button,1,2,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
354 button = manage(new Gtk::Button(/*_("Stop")*/));
355 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::stop));
356 IMAGIFY_BUTTON(button,Gtk::Stock::NO);
357 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
358 //attach(*button,2,3,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
360 //attack the stop render and erase all buttons to same line...
362 Gtk::VSeparator *vsep = manage(new Gtk::VSeparator);
363 hbox->pack_start(*vsep,Gtk::PACK_SHRINK,0);
366 button = manage(new Gtk::Button(/*_("Halt Render")*/));
367 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::stoprender));
368 IMAGIFY_BUTTON(button,Gtk::Stock::STOP);
369 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
370 //attach(*button,2,3,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
372 button = manage(new Gtk::Button(/*_("Re-Preview")*/));
373 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::repreview));
374 IMAGIFY_BUTTON(button,Gtk::Stock::CONVERT);
375 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
376 //attach(*button,0,2,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
378 button = manage(new Gtk::Button(/*_("Erase All")*/));
379 button->signal_clicked().connect(sigc::mem_fun(*this,&Widget_Preview::eraseall));
380 //IMAGIFY_BUTTON(button,Gtk::Stock::DELETE);
381 hbox->pack_start(*button,Gtk::PACK_SHRINK,0);
382 //attach(*button,2,3,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
385 attach(*hbox,0,1,2,3,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK|Gtk::FILL);
388 hbox = manage(new Gtk::HBox);
390 Gtk::Label *label = manage(new Gtk::Label("Last Rendered: "));
392 hbox->pack_start(*label,Gtk::PACK_SHRINK,10);
393 //attach(*manage(new Gtk::Label("Last Rendered: ")),0,1,3,4,Gtk::SHRINK,Gtk::SHRINK);
396 hbox->pack_start(l_lasttime,Gtk::PACK_SHRINK,0);
398 attach(*hbox,0,1,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
399 //attach(l_lasttime,0,1,3,4,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
402 disp_sound.set_size_request(-1,32);
403 attach(disp_sound,0,1,4,5,Gtk::EXPAND|Gtk::FILL,Gtk::SHRINK);
407 //if(draw_area.get_window()) gc_area = Gdk::GC::create(draw_area.get_window());
411 studio::Widget_Preview::~Widget_Preview()
415 void studio::Widget_Preview::update()
417 //the meat goes in this locker...
418 double time = adj_time_scrub.get_value();
420 //find the frame and display it...
423 //synfig::warning("Updating at %.3f s",time);
425 //use time to find closest frame...
426 studio::Preview::FlipBook::const_iterator beg = preview->begin(),end = preview->end();
427 studio::Preview::FlipBook::const_iterator i;
431 //go to current hint if need be...
432 if(currentindex >= 0 && currentindex < (int)preview->numframes())
434 i = beg+currentindex;
437 //we can't have a picture if there are none to get
440 //don't bother with binary search it will just be slower...
442 //synfig::info("Search for time %f",time);
444 //incrementally go in either direction
445 //(bias downward towards beg, because that's what we want)
448 //synfig::info("Look at %f",i->t);
449 if(i->t > time) break;
450 //synfig::info("Go past...");
455 //bias down, so we can't be at end... and it still is valid...
459 //synfig::info("Look at %f",i->t);
460 if(i->t <= time) break;
461 //synfig::info("Go past...");
464 /*i = preview->begin(); end = preview->end();
468 for(;i != end; j = i++)
470 if(i->t > time) break;
473 //we should be at a valid edge since we biased downward
475 //don't get the closest, round down... (if we can)
478 synfig::error("i == end....");
486 currentindex = i-beg;
490 //synfig::warning("Update at: %f seconds (%f s)",time,timedisp);
492 //synfig::warning("success!");
498 if(disp_sound.get_profile() && adj_sound.get_value() != time)
502 //Set the position of the sound (short circuited for sound modifying the time)
504 disp_sound.set_position(time);
505 disp_sound.queue_draw();
508 void studio::Widget_Preview::preview_draw()
510 draw_area.queue_draw();//on_expose_event();
513 bool studio::Widget_Preview::redraw(GdkEventExpose *heh)
515 //And render the drawing area
516 Glib::RefPtr<Gdk::Pixbuf> pxnew, px = currentbuf;
518 if(!px || draw_area.get_height() == 0
519 || px->get_height() == 0 || px->get_width() == 0 /*|| is_visible()*/) //made not need this line
522 //figure out the scaling factors...
526 sx = draw_area.get_width() / (float)px->get_width();
527 sy = draw_area.get_height() / (float)px->get_height();
529 //synfig::info("widget_preview redraw: now to scale the bitmap: %.3f x %.3f",sx,sy);
531 //round to smallest scale (fit entire thing in window without distortion)
535 //scale to a new pixmap and then copy over to the window
536 nw = (int)(px->get_width()*sx);
537 nh = (int)(px->get_height()*sx);
539 if(nw == 0 || nh == 0)return true;
541 pxnew = px->scale_simple(nw,nh,Gdk::INTERP_NEAREST);
543 //synfig::info("Now to draw to the window...");
545 Glib::RefPtr<Gdk::Window> wind = draw_area.get_window();
546 Glib::RefPtr<Gdk::Drawable> surf = Glib::RefPtr<Gdk::Drawable>::cast_static(wind);
547 Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(wind);
550 Gdk::Rectangle r(0,0,draw_area.get_width(),draw_area.get_height());
551 draw_area.get_window()->begin_paint_rect(r);
554 if(!wind) synfig::warning("The destination window is broken...");
555 if(!surf) synfig::warning("The destination is not drawable...");
559 /* Options for drawing...
560 1) store with alpha, then clear and render with alpha every frame
561 - more time consuming
563 2) store with just pixel info
566 + better memory footprint
568 //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
573 0, 0, // Source X and Y
574 0, 0, // Dest X and Y
575 -1,-1, // Width and Height
576 Gdk::RGB_DITHER_NONE, // RgbDither
577 0, 0 // Dither offset X and Y
582 Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
583 Glib::ustring timecode(Time((double)timedisp).round(preview->get_global_fps())
584 .get_string(preview->get_global_fps(),
585 App::get_time_format()));
586 //synfig::info("Time for preview draw is: %s for time %g", timecode.c_str(), adj_time_scrub.get_value());
588 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
589 layout->set_text(timecode);
590 surf->draw_layout(gc,4,4,layout);
594 draw_area.get_window()->end_paint();
596 //synfig::warning("Refresh the draw area");
597 //make sure the widget refreshes
602 bool studio::Widget_Preview::play_update()
604 float diff = timer.pop_time();
605 //synfig::info("Play update: diff = %.2f",diff);
609 //we go to the next one...
610 double time = adj_time_scrub.get_value() + diff;
612 //adjust it to be synced with the audio if it can...
614 double newtime = audiotime;
615 if(audio && audio->is_playing()) audio->get_current_time(newtime);
617 if(newtime != audiotime)
619 //synfig::info("Adjusted time from %.3lf to %.3lf", time,newtime);
620 time = audiotime = newtime;
624 //Looping conditions...
625 if(time >= adj_time_scrub.get_upper())
629 time = adj_time_scrub.get_lower();// + time-adj_time_scrub.get_upper();
633 time = adj_time_scrub.get_upper();
634 adj_time_scrub.set_value(time);
638 //synfig::info("Play Stopped: time set to %f",adj_time_scrub.get_value());
643 //set the new time...
644 adj_time_scrub.set_value(time);
645 adj_time_scrub.value_changed();
647 //update the window to the correct image we might want to do this later...
649 //synfig::warning("Did update pu");
654 void studio::Widget_Preview::slider_move()
659 //synfig::warning("Did update sm");
663 //for other things updating the value changed signal...
664 void studio::Widget_Preview::scrub_updated(double t)
668 //Attempt at being more accurate... the time is adjusted to be exactly where the sound says it is
672 if(!audio->isPaused())
674 audio->get_current_time(t);
678 //synfig::info("Scrubbing to %.3f, setting adj to %.3f",oldt,t);
680 if(adj_time_scrub.get_value() != t)
682 adj_time_scrub.set_value(t);
683 adj_time_scrub.value_changed();
687 void studio::Widget_Preview::disconnect_preview(Preview *prev)
692 prevchanged.disconnect();
696 void studio::Widget_Preview::set_preview(handle<Preview> prev)
700 synfig::info("Setting preview");
702 //stop playing the mini animation...
707 //set the internal values
708 float rate = preview->get_fps();
709 synfig::info(" FPS = %f",rate);
712 float start = preview->get_begintime();
713 float end = preview->get_endtime();
717 adj_time_scrub.set_lower(start);
718 adj_time_scrub.set_upper(end);
719 adj_time_scrub.set_value(start);
720 adj_time_scrub.set_step_increment(rate);
721 adj_time_scrub.set_page_increment(10*rate);
723 //if the begin time and the end time are the same there is only a single frame
724 singleframe = end==start;
727 adj_time_scrub.set_lower(0);
728 adj_time_scrub.set_upper(0);
729 adj_time_scrub.set_value(0);
730 adj_time_scrub.set_step_increment(0);
731 adj_time_scrub.set_page_increment(0);
735 //connect so future information will be found...
736 prevchanged = prev->signal_changed().connect(sigc::mem_fun(*this,&Widget_Preview::whenupdated));
737 prev->signal_destroyed().connect(sigc::mem_fun(*this,&Widget_Preview::disconnect_preview));
739 //synfig::warning("Did update sp");
744 void studio::Widget_Preview::whenupdated()
746 l_lasttime.set_text((Time((double)(--preview->end())->t)
747 .round(preview->get_global_fps())
748 .get_string(preview->get_global_fps(),App::get_time_format())));
752 void studio::Widget_Preview::clear()
755 prevchanged.disconnect();
758 void studio::Widget_Preview::play()
760 if(preview && !playing)
762 //synfig::info("Playing at %lf",adj_time_scrub.get_value());
763 //audiotime = adj_time_scrub.get_value();
766 //adj_time_scrub.set_value(adj_time_scrub.get_lower());
767 update(); //we don't want to call play update because that will try to advance the timer
768 //synfig::warning("Did update p");
770 //approximate length of time in seconds, right?
771 double rate = /*std::min(*/adj_time_scrub.get_step_increment()/*,1/30.0)*/;
772 int timeout = (int)floor(1000*rate);
774 //synfig::info(" rate = %.3lfs = %d ms",rate,timeout);
776 signal_play_(adj_time_scrub.get_value());
779 if(audio) audio->play(adj_time_scrub.get_value());
781 timecon = Glib::signal_timeout().connect(sigc::mem_fun(*this,&Widget_Preview::play_update),timeout);
787 void studio::Widget_Preview::play_stop()
791 if(audio) audio->stop(); //!< stop the audio
792 //synfig::info("Stopping...");
795 void studio::Widget_Preview::stop()
797 //synfig::warning("stopping");
799 timecon.disconnect();
802 bool studio::Widget_Preview::scroll_move_event(GdkEvent *event)
806 case GDK_BUTTON_PRESS:
808 if(event->button.button == 1 || event->button.button == 3)
820 void studio::Widget_Preview::set_audioprofile(etl::handle<AudioProfile> p)
822 disp_sound.set_profile(p);
825 void studio::Widget_Preview::set_audio(etl::handle<AudioContainer> a)
829 //disconnect any previous signals
830 scrstartcon.disconnect(); scrstopcon.disconnect(); scrubcon.disconnect();
832 //connect the new signals
833 scrstartcon = disp_sound.signal_start_scrubbing().connect(sigc::mem_fun(*a,&AudioContainer::start_scrubbing));
834 scrstopcon = disp_sound.signal_stop_scrubbing().connect(sigc::mem_fun(*a,&AudioContainer::stop_scrubbing));
835 scrubcon = disp_sound.signal_scrub().connect(sigc::mem_fun(*a,&AudioContainer::scrub));
838 void studio::Widget_Preview::seek(float t)
841 adj_time_scrub.set_value(t);
844 void studio::Widget_Preview::repreview()
850 preview->get_canvasview()->preview_option();
854 void studio::Widget_Preview::stoprender()
858 preview->renderer.detach();
862 void studio::Widget_Preview::eraseall()