我正在开发一个适用于 Linux 的 C++ 图像查看器,它是使用 GTK+3 (gtkmm) 创建的用于 GUI 和 Magick++ 用于图像处理。我的目标是支持尽可能多的图像文件格式,包括动画 GIF。
获取 Magick++ 图像并将其绘制在 GTK+3 小部件中的最佳方法是什么,以便它适用于(几乎)任何图像文件格式?
What is the best approach to take a Magick++ Image and draw it in a GTK+3 widget, such that it would work for (just about) any image file format?
As long as ImageMagick has the format delegate, you should be able to draw the GtkWidget image.
image = Gtk::manage(new Gtk::Image());
// Load image into ImageMagick
Magick::Image img("wizard:");
/// Calculate how much memory to allocate
size_t to_allocate = img.columns() * img.rows() * 3;
// Create a buffer
guint8 * buffer = new guint8[to_allocate];
// Write pixel data to buffer.
img.write(0, 0, img.columns(), img.rows(), "RGB", Magick::CharPixel, buffer);
// Build a Pixbuf from pixel data in memory.
Glib::RefPtr<Gdk::Pixbuf> pBuff = Gdk::Pixbuf::create_from_data(buffer, Gdk::COLORSPACE_RGB, false, 8, img.columns(), img.rows(), img.columns()*3 );
// Set GtkImage from Pixbuf
image->set(pBuff);
Use the following Magick++ method signature to export the pixel data into memory.
Magick::Image.write(const ssize_t x_,
const ssize_t y_,
const size_t columns_,
const size_t rows_,
const std::string &map_, //<= Usually "RGB"
const StorageType type_, //<= Usually CharType
void *pixels_) //<= Be sure to allocate _all_ the memory required (size of storage * number of channels * columns * rows)
Create a GdkPixBuf from the pixels exported above with the following GTK method.
GdkPixbuf *
gdk_pixbuf_new_from_bytes (GBytes *data, //<= Same as pixels_.
GdkColorspace colorspace, //<= Match colorspace channels from map_.
gboolean has_alpha, //<= Usually no.
int bits_per_sample, //<= Match StorageType bits
int width, //<= Same as columns_.
int height, //<= Same as rows_.
int rowstride); //<= size of data-type * number of channel * width.
Finally, build a GtkImage from the PixBuf with the following method.
GtkWidget * gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf);
我想@emcconville 适合 ImageMagick 部分,因为我对此一无所知。
不过,对于 GTKmm 部分,您需要坚持使用 C++ API,因此请使用Gdk::Pixbuf::create_from_data从 ImageMagick 读取图像。
此外,在创建图像查看器时,您需要更改由Gtk::Image
. 所以在启动时只需使用用Gtk::Image::Image(或 Glade 和)Gtk::Image
创建的空图像,然后用Gtk::Image::set更改其中显示的图像,并将其传递给您的 pixbuf。Gtk::Builder