GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Aug 28, 2014 9:05 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Getting an Image onto a Cairo Surface
PostPosted: Wed Nov 13, 2013 8:12 am 
Offline
Familiar Face

Joined: Fri Aug 05, 2011 8:05 am
Posts: 22
You may have noticed that GTK+3 is a bit maimed when it comes to getting the content of a bitmapped graphics file onto a Cairo surface. There is some support for PNG files, though it is described as a 'toy interface'. This has probably been fixed in 3.10, but many systems in use probably won't have that for a while.

In the meantime I need to be able to incorporate JPEG and other file types in graphical compositions, so I came up with a workaround that might suffice until 3.10 is commonplace.

I've written a short article that might be useful.


Top
 Profile  
 
 Post subject: Re: Getting an Image onto a Cairo Surface
PostPosted: Thu Nov 14, 2013 9:17 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hello,

The Cairo library is a 2D vector drawing library and is most unlikely to have support for loading raster images (except for the toy interface for PNG files). The documentation even recommends using other libraries for loading and saving of raster images. Suggestions in the documentation are gdk-pixbuf and libpng.

The library gdk-pixbuf is part of GNOME libraries and can load/save many different image formats so is probably the one you may want to use.

An example taken directly from the gtkmm documentation shows how to load an image an then display it in a window. This is in C++ and will only work with gtkmm v3. Note that although the image in the example indicates a PNG file it could have been anything supported by gdk-pixbuf. A look at the source shows that some of the formats include tiff, png, ico, jpeg and gif.

- myarea.h
Code:
#ifndef GTKMM_EXAMPLE_MYAREA_H
#define GTKMM_EXAMPLE_MYAREA_H

#include <gtkmm/drawingarea.h>
#include <gdkmm/pixbuf.h>

class MyArea : public Gtk::DrawingArea
{
public:
  MyArea();
  virtual ~MyArea();

protected:
  //Override default signal handler:
  virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);

  Glib::RefPtr<Gdk::Pixbuf> m_image;
};

#endif // GTKMM_EXAMPLE_MYAREA_H

- myarea.cc
Code:
#include "myarea.h"
#include <cairomm/context.h>
#include <gdkmm/general.h> // set_source_pixbuf()
#include <glibmm/fileutils.h>
#include <iostream>

MyArea::MyArea()
{
  try
  {
    // The fractal image has been created by the XaoS program.
    // http://xaos.sourceforge.net
    m_image = Gdk::Pixbuf::create_from_file("fractal_image.png");
  }
  catch(const Glib::FileError& ex)
  {
    std::cerr << "FileError: " << ex.what() << std::endl;
  }
  catch(const Gdk::PixbufError& ex)
  {
    std::cerr << "PixbufError: " << ex.what() << std::endl;
  }

  // Show at least a quarter of the image.
  if (m_image)
    set_size_request(m_image->get_width()/2, m_image->get_height()/2);
}

MyArea::~MyArea()
{
}

bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
  if (!m_image)
    return false;

  Gtk::Allocation allocation = get_allocation();
  const int width = allocation.get_width();
  const int height = allocation.get_height();

  // Draw the image in the middle of the drawing area, or (if the image is
  // larger than the drawing area) draw the middle part of the image.
  Gdk::Cairo::set_source_pixbuf(cr, m_image,
    (width - m_image->get_width())/2, (height - m_image->get_height())/2);
  cr->paint();

  return true;
}

- main.cc
Code:
#include "myarea.h"
#include <gtkmm/application.h>
#include <gtkmm/window.h>

int main(int argc, char** argv)
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  Gtk::Window win;
  win.set_title("DrawingArea");
  win.set_default_size(300, 200);

  MyArea area;
  win.add(area);
  area.show();

  return app->run(win);
}

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group