画像のアルファ合成
概要
cairommを使って2つの画像のアルファ合成をおこなう。基本的にはファイルから画像を読み込んで表示と同様に行うが、 2つ目の画像を描画するときCairo::Context::paint()の代わりに Cairo::Context::paint_with_alpha()を使用する。
ソース
cairoimg2.cpp#include <gtkmm.h> class MyDrawArea : public Gtk::DrawingArea { Glib::RefPtr< Gdk::Pixbuf > m_pixbuf; Glib::RefPtr< Gdk::Pixbuf > m_pixbuf2; public: MyDrawArea(); protected: virtual bool on_expose_event( GdkEventExpose* e ); }; MyDrawArea::MyDrawArea() { // 画像読み込み try { m_pixbuf = Gdk::Pixbuf::create_from_file( "./test.jpg" ); m_pixbuf2 = Gdk::Pixbuf::create_from_file( "./test2.jpg" ); } catch(...) { Glib::exception_handlers_invoke(); exit(1); } } bool MyDrawArea::on_expose_event( GdkEventExpose* event ) { const double alpha = 0.5; // ブレンド比率 int width = get_width(); int height = get_height(); Cairo::RefPtr< Cairo::Context > cairoctx = get_window()->create_cairo_context(); Gdk::Cairo::set_source_pixbuf( cairoctx, m_pixbuf->scale_simple( width, height, Gdk::INTERP_NEAREST ), 0, 0 ); cairoctx->paint(); Gdk::Cairo::set_source_pixbuf( cairoctx, m_pixbuf2->scale_simple( width, height, Gdk::INTERP_NEAREST ), 0, 0 ); cairoctx->paint_with_alpha( alpha ); return true; } class MainWin : public Gtk::Window { MyDrawArea m_drawarea; public: MainWin(){ add( m_drawarea ); show_all_children(); } }; int main( int argc, char *argv[] ) { Gtk::Main kit( argc, argv ); MainWin mainwin; Gtk::Main::run( mainwin ); return 0; }
g++ cairoimg2.cpp -o cairoimg2 `pkg-config gtkmm-2.4 --cflags --libs`
結果
