4

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.

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.

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...

Thank you

Jav
  • 1,022
  • 1
  • 13
  • 32

1 Answers1

3

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
   }
Brent Bradburn
  • 40,766
  • 12
  • 126
  • 136
  • Can you give us an example how to assign the changed pixelbuffer back to the context and redraw? – binaryBigInt Jan 05 '16 at 16:27
  • @binaryBigInt: The example at [Drawing Images](https://developer.gnome.org/gtkmm-tutorial/stable/sec-draw-images.html.en) matches what I was doing at the time. Basically, I called `set_source_pixbuf` followed by `paint`. That is also what the OP has shown in the question. – Brent Bradburn Jan 05 '16 at 18:16