我正在使用Gdk::Pixbuf在C++中显示带有Gdk::Cairo的图像:
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 */
}这个图像是黑白的,我需要带出一些亮度高于某个阈值的像素。为此,我想对这些像素进行着色。
首先,我不知道(我在网上找不到)如何获得我的Pixbuf图像的某个像素的亮度。
其次,除了画一条长度为1的线(这是一种丑陋的解决方案)之外,我找不到其他方法来绘制像素。
你能在这方面帮我吗?如果可能的话,我想避免更改库...
谢谢
发布于 2013-08-31 03:23:48
您可以使用get pixels()函数。
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
}https://stackoverflow.com/questions/16785886
复制相似问题