[Solved] Using Magick++ in a GTK app
Posted: 2014-06-13T15:18:13-07:00
Hello everyone,
I was for a while trying to load this image file with Magick++ and then show it in a simple GTK app.
But in my attempt I get a runtime error of GTK when load a blob in a pixbuf.
Here's my code until now:
This compile's with:
Is there a way to convert an image to xpm and pass it to create_from_xpm_data() properly?
I really appreciate any clue you can provide.
I was for a while trying to load this image file with Magick++ and then show it in a simple GTK app.
But in my attempt I get a runtime error of GTK when load a blob in a pixbuf.
Here's my code until now:
Code: Select all
#include <Magick++.h>
#include <gtkmm.h>
#include <iostream>
using namespace std;
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;
};
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("magick++ && gtkmm");
win.set_default_size(445, 287);
MyArea area;
win.add(area);
area.show();
return app->run(win);
}
MyArea::MyArea()
{
try
{
Magick::Image image( "fractal_image.png" );
Magick::Blob blob;
image.magick( "XPM" ); // using "JPEG" or "PNG" cause a segmentation fault //
image.write( &blob );
const void *pblob = blob.data();
const char *pdata;
const char **ppdata;
pdata = static_cast<const char*>(pblob);
ppdata = &pdata;
m_image = Gdk::Pixbuf::create_from_xpm_data(ppdata); // This try to load the blob to a pixbuf //
}
catch(const Glib::FileError& ex)
{
std::cerr << "FileError: " << ex.what() << std::endl;
}
catch(const Gdk::PixbufError& ex)
{
std::cerr << "PixbufError: " << ex.what() << std::endl;
}
}
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;
}
But, again. GTK gives a runtime error saying "Inline XPM data is Broken: Invalid XPM header".g++ `Magick++-config --cxxflags --cppflags` main.cpp -o main `pkg-config gtkmm-3.0 --cflags --libs` `Magick++-config --ldflags --libs`
Is there a way to convert an image to xpm and pass it to create_from_xpm_data() properly?
I really appreciate any clue you can provide.