问题描述
我正在使用Gdk::Pixbuf
在C ++中显示带有Gdk::Cairo
的图像:
I'm using a Gdk::Pixbuf
to display an image with Gdk::Cairo
in C++ :
virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_file(filename);
Gdk::Cairo::set_source_pixbuf(cr, image, (width - image->get_width())/2, (height - image->get_height())/2);
cr->paint();
/* other displaying stuffs */
}
此图像为黑白图像,我需要带出一些亮度高于特定阈值的像素.为此,我想为那些像素着色.
This image is in B&W and I need to bring out some pixels whose luminance is above a certain threshold. For that, I would like to color those pixels.
首先,我不知道(而且我在网上找不到)如何获取Pixbuf图像中某个像素的亮度.
First, I don't know (and I cannot find on the web) how to get the luminance of a certain pixel of my Pixbuf image.
第二,除了绘制一条长度为1的线(这是种丑陋的解决方案)之外,我没有找到另一种绘制像素的方法.
Second, I don't find another way to draw the pixel than drawing a line of length 1 (which is kind of ugly solution).
您能帮我吗?如果可能的话,我想避免更改库...
Could you help me on this? If possible, I would like to avoid changing library...
谢谢
推荐答案
您可以使用 get pixels()
函数.
You can use the get pixels()
function.
void access_pixel( Glib::RefPtr<Gdk::Pixbuf> imageptr, int x, int y )
{
if ( !imageptr ) return;
Gdk::Pixbuf & image = *imageptr.operator->(); // just for convenience
if ( ! image.get_colorspace() == Gdk::COLORSPACE_RGB ) return;
if ( ! image.get_bits_per_sample() == 8 ) return;
if ( !( x>=0 && y>=0 && x<image.get_width() && y<image.get_height() ) ) return;
int offset = y*image.get_rowstride() + x*image.get_n_channels();
guchar * pixel = &image.get_pixels()[ offset ]; // get pixel pointer
if ( pixel[0]>128 ) pixel[1] = 0; // conditionally modify the green channel
queue_draw(); // redraw after modify
}
这篇关于获取Gdk :: Pixbuf上的像素值,使用Gdk :: Cairo设置像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!