drawing scaled images with cairomm
- From: Christopher Harvey <chris basementcode com>
- To: gtkmm-list gnome org
- Subject: drawing scaled images with cairomm
- Date: Tue, 28 Dec 2010 23:53:51 -0500
I've got the following code (pasted below) that stores a
Cairo::RefPtr<Cairo::ImageSurface> created from a Gdk::PixBuf. When I
draw this image it is about 30x30, and I want to be able to scale it up.
I don't care too much about the filtering, but it would be cool if
cariomm could do it for me rather than allocating tons of memory and
scaling myself just to display a larger version of a 30x30 image. Again,
I don't care about the filtering algorithm, I just can't figure it out
in general from the docs....
Thanks.
The term PDF here means probability distribution function, not portable
document format.
Cairo::RefPtr<Cairo::ImageSurface> currentImage;
Window *window = NULL;
bool updatePDF(double dt) {
const unsigned int w(g_pf->getWidth());
const unsigned int h(g_pf->getHeight());
std::vector<unsigned char> data(w*h*3);
for(unsigned int i = 0;i<data.size();i+=3) {
data[i] = 255;
data[i+1] = 0;
data[i+2] = 0;
}
Glib::RefPtr<Gdk::Pixbuf> pb;
pb = Gdk::Pixbuf::create_from_data(&data[0], Gdk::COLORSPACE_RGB,
false, 8, w, h, w*3);
currentImage = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24,
pb->get_width(), pb->get_height());
Cairo::RefPtr<Cairo::Context> image_context_ptr =
Cairo::Context::create(currentImage);
// Draw the image on the new Context
Gdk::Cairo::set_source_pixbuf(image_context_ptr, pb, 0.0, 0.0);
image_context_ptr->paint();
window->queue_draw();
return true;
}
bool drawPDF(GdkEventExpose *e, DrawingArea *area) {
// This is where we draw on the window
Glib::RefPtr<Gdk::Window> window = area->get_window();
if(window) {
Gtk::Allocation allocation = area->get_allocation();
int width = allocation.get_width();
int height = allocation.get_height();
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
if(e) {
// clip to the area indicated by the expose event so that we only
// redraw the portion of the window that needs to be redrawn
cr->rectangle(e->area.x, e->area.y,
e->area.width, e->area.height);
cr->clip();
}
double line_width = 0.05;
if(currentImage)
cr->set_source(currentImage, 0.0, 0.0);
else
cr->set_source_rgb(0.0, 0.0, 1.0);
cr->rectangle(0,0,width,height);
cr->fill();
}
return true;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]