Get pixel value in Gdk :: Pixbuf, Set pixel value with Gdk :: Cairo

I use 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 output some pixels whose brightness exceeds a certain threshold. For this, I would like to color these pixels.

Firstly, I do not know (and cannot find on the Internet) how to get the brightness of a specific pixel in my Pixbuf image.

Secondly, I find no other way to draw a pixel than to draw a line of length 1 (which is a kind of ugly solution).

could you help me? If possible, I would like to avoid changing the library ...

thanks

+4
source share
1 answer

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 } 
+3
source

Source: https://habr.com/ru/post/1483091/


All Articles