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 Sep 18, 2014 5:39 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [Solved] Using Magick++ in a GTK app
PostPosted: Fri Jun 13, 2014 5:50 am 
Offline

Joined: Thu Jun 12, 2014 8:53 pm
Posts: 4
Hello everyone,

I was for a while trying to load an image file with Magick++ and then show it in a simple GTK app, but the best I get is this:
Image

The original image:
Image

Sometimes with other images only got a black screen.

Here is my code until now:
Code:
#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 );

   gint width;
   gint height;
   width = image.columns();
   height = image.rows();
   Glib::RefPtr<Gdk::Pixbuf> pixbuf;
   int rowstride;

   const void *pblob = blob.data();
   const guint8 *pdata;
   pdata = static_cast<const guint8*>(pblob);

   pixbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, width, height);
   rowstride = pixbuf->Gdk::Pixbuf::get_rowstride();
   m_image = Gdk::Pixbuf::create_from_data(pdata, Gdk::COLORSPACE_RGB,
                                           false, 8, width, height, rowstride);

  }
  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;
}

Quote:
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 show an image from Magick++ without having to save it into the HDD before?
If not, is there another image processing library to do this?

Thanks.


Last edited by danieru on Sun Jun 15, 2014 11:06 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Using Magick++ in a GTK app
PostPosted: Fri Jun 13, 2014 6:59 pm 
Offline
Familiar Face

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 42
Location: Belgium
So if I understand correctly you load an image from disk with the Magick library.
I'm sure you can find a function in a gnome library to load an image directly in a GdkPixbuf. In the gdk-pixbuf library itself for example…
Or you can store your image in a GResource, and load the GdkPixbuf from the resource.


Top
 Profile  
 
 Post subject: Solved
PostPosted: Sun Jun 15, 2014 9:44 pm 
Offline

Joined: Thu Jun 12, 2014 8:53 pm
Posts: 4
Problem solved. I was just making two mistakes:

1. When I call write() I was saving the PNG header too, but Gdk::Pixbuf::create_from_data() expects only the RGB values.

2. The rowstride variable did not have the correct value (which is image width * bits per pixel).

After correcting this, finally I was able to load an image with Magick++ and then load it into a Gdk::Pixbuf.
Image

Here's a function that takes a Magick::Image object and returns a Gdk::Pixbuf, just for the record:
Code:
#include <Magick++.h>
#include <gtkmm.h>

Glib::RefPtr<Gdk::Pixbuf> im_to_pixbuf(Magick::Image image)
{
   int bits_per_pixel = 3;
   int width = image.columns();
   int height = image.rows();
   int rowstride = width*bits_per_pixel;
   unsigned char *block = (unsigned char*)malloc(width*height*bits_per_pixel);
   image.write(0, 0, width, height, "RGB", Magick::CharPixel, block);
   guint8 *pdata;
   pdata = static_cast<guint8*>(block);

   return Gdk::Pixbuf::create_from_data(pdata, Gdk::COLORSPACE_RGB, false, 8, width, height, rowstride);
}


Thanks for your time.


Top
 Profile  
 
 Post subject: Re: Using Magick++ in a GTK app
PostPosted: Sun Jun 15, 2014 10:15 pm 
Offline
Familiar Face

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 42
Location: Belgium
Ok, but why do you need Magick++?
It's better and easier to use GObject-based libraries when coding with GTK+.


Top
 Profile  
 
 Post subject: Re: [Solved] Using Magick++ in a GTK app
PostPosted: Mon Jun 16, 2014 1:28 am 
Offline

Joined: Thu Jun 12, 2014 8:53 pm
Posts: 4
The advantage is that Magick++ is a library of image manipulation. You can apply effects, resize, crop, etc.. All with Magick++!
So, I'm doing this because now I have the ability to change the image easily. For example:
Here I apply a simple blur filter:
Image
And that with only:
Code:
   Magick::Image image( "fractal_image.png" );
   image.blur(32,16);
   mypixbuf = im_to_pixbuf(image);

Of course memory improvements are needed, but you get the idea.

(I'm assuming this is easier with Magick++ than with cairomm, but I may be wrong)


Top
 Profile  
 
 Post subject: Re: [Solved] Using Magick++ in a GTK app
PostPosted: Mon Jun 16, 2014 11:44 am 
Offline
Familiar Face

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 42
Location: Belgium
The GIMP uses GEGL:
http://www.gegl.org/

It's maybe worth a try.


Top
 Profile  
 
 Post subject: Re: [Solved] Using Magick++ in a GTK app
PostPosted: Mon Jun 16, 2014 6:37 pm 
Offline

Joined: Thu Jun 12, 2014 8:53 pm
Posts: 4
Well.. I can not find documentation for the geglmm API's. And I dont know if you can use GEGL with gtkmm.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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