gpt4 book ai didi

c++ - 在 GTK+3 应用程序中显示 Magick++ 图像的首选方法是什么?

转载 作者:搜寻专家 更新时间:2023-10-31 01:29:53 24 4
gpt4 key购买 nike

我正在为 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?

只要 ImageMagick 具有格式委托(delegate),您就应该能够绘制 GtkWidget 图像。

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

GtkImage from Magick++

混合 C/C++ 方法的原始答案。

使用以下 Magick++ 方法签名将像素数据导出到内存中。

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)

使用以下 GTK 方法从上面导出的像素创建 GdkPixBuf。

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.

最后,使用以下方法从 PixBuf 构建一个 GtkImage。

GtkWidget * gtk_image_new_from_pixbuf (GdkPixbuf *pixbuf);

关于c++ - 在 GTK+3 应用程序中显示 Magick++ 图像的首选方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49066611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com