【问题标题】:Get pixel value on Gdk::Pixbuf, Set pixel value with Gdk::Cairo在 Gdk::Pixbuf 上获取像素值,使用 Gdk::Cairo 设置像素值
【发布时间】:2013-05-28 07:11:45
【问题描述】:

我在 C++ 中使用Gdk::Pixbuf 显示带有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 的线之外,我没有找到另一种绘制像素的方法(这是一种丑陋的解决方案)。

你能帮我解决这个问题吗?如果可能的话,我想避免改变图书馆......

谢谢

【问题讨论】:

    标签: c++ cairo gtkmm gdk gdkpixbuf


    【解决方案1】:

    您可以使用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
       }
    

    【讨论】:

    • 您能给我们举个例子,如何将更改后的像素缓冲区分配回上下文并重绘吗?
    • @binaryBigInt:Drawing Images 的示例与我当时所做的相匹配。基本上,我打电话给set_source_pixbuf,然后是paint。这也是OP在问题中显示的内容。
    猜你喜欢
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多