/usr/share/doc/libgtkmm-2.4-dev/examples/pixbuf-demo.cc is in libgtkmm-2.4-doc 1:2.24.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | /* GdkPixbuf library - Scaling and compositing demo
*
* Copyright (C) 2002 The gtkmm Development Team
*
* Authors: Federico Mena-Quintero <federico@gimp.org>
* gtkmm port: Daniel Elstner <daniel.kitta@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <sigc++/sigc++.h>
#include <glibmm/main.h>
#include <gdkmm/pixbuf.h>
#include <gdkmm/rectangle.h>
#include <gtkmm/drawingarea.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gdkmm/general.h>
namespace
{
/*
* These values control the smoothness and speed of the animation.
* For instance, FRAME_DELAY=25 and CYCLE_LEN=90 looks much better
* to me (and also wastes more CPU cycles, of course).
*/
enum
{
FRAME_DELAY = 48,
CYCLE_LEN = 64
};
const char * const background_name =
"gtk-demo/background.jpg";
const char * const image_names[] =
{
"gtk-demo/apple-red.png",
"gtk-demo/gnome-applets.png",
"gtk-demo/gnome-calendar.png",
"gtk-demo/gnome-foot.png",
"gtk-demo/gnome-gmush.png",
"gtk-demo/gnome-gimp.png",
"gtk-demo/gnome-gsame.png",
"gtk-demo/gnu-keys.png"
};
class DemoRenderArea : public Gtk::DrawingArea
{
public:
DemoRenderArea();
virtual ~DemoRenderArea();
protected:
virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
private:
Glib::RefPtr<const Gdk::Pixbuf> background_;
std::vector< Glib::RefPtr<const Gdk::Pixbuf> > images_;
Glib::RefPtr<Gdk::Pixbuf> current_frame_;
unsigned int frame_num_;
void generate_next_frame();
};
Glib::RefPtr<Gdk::Pixbuf> create_pixbuf(const std::string& name)
{
return Gdk::Pixbuf::create_from_file(name);
}
/*
* Load all image files, create an empty buffer for storing the current frame,
* and install a timeout handler that will be called periodically. The show
* will start as soon as Gtk::Main::run() is invoked.
*/
DemoRenderArea::DemoRenderArea()
:
frame_num_ (0)
{
background_ = Gdk::Pixbuf::create_from_file(background_name);
std::transform(
&image_names[0], &image_names[G_N_ELEMENTS(image_names)],
std::back_inserter(images_),
&create_pixbuf);
const int back_width = background_->get_width();
const int back_height = background_->get_height();
current_frame_ = Gdk::Pixbuf::create(
Gdk::COLORSPACE_RGB, false, 8, back_width, back_height);
set_size_request(back_width, back_height);
add_events(Gdk::EXPOSURE_MASK);
Glib::signal_timeout().connect(
sigc::bind_return(sigc::mem_fun(*this, &DemoRenderArea::generate_next_frame), true),
FRAME_DELAY);
}
DemoRenderArea::~DemoRenderArea()
{}
/*
* Draw handler of the widget. Just fill the exposed
* area with the corresponding pixmap data from current_frame_.
*/
bool DemoRenderArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
Gdk::Cairo::set_source_pixbuf(cr, current_frame_);
cr->paint();
return true; // stop signal emission
}
/*
* Generate the next frame of the animation and store it into current_frame_.
* The draw handler accesses that buffer to do the actual drawing.
*/
void DemoRenderArea::generate_next_frame()
{
const int back_width = background_->get_width();
const int back_height = background_->get_height();
background_->copy_area(0, 0, back_width, back_height, current_frame_, 0, 0);
const double xmid = back_width / 2.0;
const double ymid = back_height / 2.0;
const double f = 2.0 * G_PI * double(frame_num_) / CYCLE_LEN;
double r = std::min(xmid, ymid) / 2.0;
r += (r / 3.0) * std::sin(f);
for(unsigned i = 0; i < images_.size(); ++i)
{
const double ang = 2.0 * G_PI * double(i) / images_.size() - f;
const double iw = images_[i]->get_width();
const double ih = images_[i]->get_height();
const int xpos = int(std::floor(xmid + r * std::cos(ang) - iw / 2.0 + 0.5));
const int ypos = int(std::floor(ymid + r * std::sin(ang) - ih / 2.0 + 0.5));
const double depth = ((i % 2) != 0) ? std::sin(f) : std::cos(f);
const double scale = std::max(0.25, 2.0 * depth * depth);
Gdk::Rectangle rect (xpos, ypos, int(iw * scale), int(ih * scale));
rect.intersect(Gdk::Rectangle(0, 0, back_width, back_height));
if(!rect.has_zero_area())
{
const int overall_alpha = std::max(127, int(std::abs(255.0 * depth)));
images_[i]->composite(
current_frame_,
rect.get_x(), rect.get_y(), rect.get_width(), rect.get_height(),
xpos, ypos, scale, scale,
Gdk::INTERP_NEAREST,
overall_alpha);
}
}
frame_num_ = (frame_num_ + 1) % CYCLE_LEN;
// Tell GTK+ the widget should be redrawn soon. This will trigger the
// draw signal if the widget is actually mapped on the screen.
queue_draw();
}
} // anonymous namespace
int main(int argc, char** argv)
{
try
{
Gtk::Main main_instance (&argc, &argv);
Gtk::Window window;
window.add(*Gtk::manage(new DemoRenderArea()));
window.set_resizable(false);
window.show_all();
Gtk::Main::run(window);
}
catch(const Glib::Error& error)
{
std::cerr << error.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|