# Cannot set pixbuf pixels - how to modify an image?

**URL:** <https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707>\
**Category:** Ruby-Gnome 2\
**Created:** [July 26, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707 "2009-07-26T13:41:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [July 26, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/1 "2009-07-26T13:41:09Z")

</div>

Hello

I am using:

libgtk2-ruby1.8 0.19.0-3

I get:

image.rb:27:in `step': undefined method `pixels=’ for  
#\<Gdk::Pixbuf:0xb790c970 ptr=0x8ff1c38\>  
from image.rb:32:in `on_button_clicked' from image.rb:5:in `to\_proc’  
from image.rb:77:in `call' from image.rb:77:in `main’

the pixels= method is documented here:

[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3APixbuf](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3APixbuf)

Without reloading the pixels the image seems unchanged.

Any advice on modifying images welcome.

I want to change individual pixels programatically and view the  
results in realtime, hence the pixels accessor. I just want to  
visually verify what is done to the image, I am not particularly  
interested in performance. While it would be possible to draw the  
image programatically as well I would prefer loading a test pattern  
from a file so that it can be replaced easily.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [July 28, 2009, 3:14am UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/2 "2009-07-28T03:14:03Z")

</div>

On 26/07/2009, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> from image.rb:32:in `on_button_clicked'
> from image.rb:5:in `to_proc'
> from image.rb:77:in `call'
> from image.rb:77:in `main'
> 
> ```
> 
> the pixels= method is documented here:
> 
> [http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3APixbuf](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3APixbuf)
> 
> Without reloading the pixels the image seems unchanged.

Which I resolved by patching the pixbuf bindings.

This still leaves some problems. After the pixel data is changed it  
should be also invalidated on the image widget and its window redrawn.  
Although I am drawing in the idle loop no redrawing occurs if I only  
schedule it. I have to force redraw. The remaining issue is how to  
find the pixel to be invalidated. For some reason the image is offset  
inside the window. Perhaps the easiest solution would be packing it  
into an eventbox for now.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [August 2, 2009, 4:44am UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/3 "2009-08-02T04:44:24Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] cannot set pixbuf pixels - how to modify  
an image?” on Tue, 28 Jul 2009 03:13:00 +0200,  
Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > #\<Gdk::Pixbuf:0xb790c970 ptr=0x8ff1c38\>
> 
> Which I resolved by patching the pixbuf bindings.

set\_pixels:

- We should use RSTRING\_PTR() instead of RSTRING()-\>ptr  
and RSTRING\_LEN() instead of RSTRING()-\>len.
- We should return self for setter method:  
[http://ruby-gnome2.sourceforge.jp/hiki.cgi?Naming+and+Conversion+Rules#Accessors+(Setter%2FGetter+methods)](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Naming+and+Conversion+Rules#Accessors+%28Setter%2FGetter+methods%29)
- Why memmove()? Should we use memcpy()?
- Should we require “pixbuf.pixels.size == new\_pixels.size”?

get\_pixel, set\_pixel:

- Are they needed?

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [August 2, 2009, 11:15am UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/4 "2009-08-02T11:15:26Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] cannot set pixbuf pixels - how to modify  
an image?” on Sun, 2 Aug 2009 08:37:15 +0200,  
Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> whatever was described in README.EXT)  
> Please.

> > - Why memmove()? Should we use memcpy()?
> 
> memmove is the safer variant. If the user got to the original pointer  
> somehow the copied regions might overlap. While it’s unlikely one  
> more check should not impact performance and should avoid some  
> interesting bugs.

It seems that the user can’t get the original pointer.  
If the user can get the original pointer, the user doesn’t  
need to set pixels. The user can only change the original  
pointer.

So I think that we can use memcpy().

> > - Should we require “pixbuf.pixels.size == new\_pixels.size”?
> 
> I guess it’s OK to set only part of the pixels. I am not sure  
> returning error is appropriate if the sizes do not match.

If we provide an ‘offset’ argument, it will be reasonable.  
But set\_pixels doesn’t provide it.

So I think that the sizes should be matched.

> > get\_pixel, set\_pixel:
> > 
> > - Are they needed?
> 
> When you do not want to set all pixels of a large bitmap they might be  
> faster. And they give the high-level interface so they should be the  
> primary access method for high-level language like ruby.
> 
> They should be probably converted to match the color format used by  
> other methods,though.

When they work as you said, we should consider about  
{get,set}\_pixel. Is it OK?

Anyway we should treat get\_pixels and {get,set}\_pixel with  
separated patches.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [August 2, 2009, 8:37am UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/5 "2009-08-02T08:37:59Z")

</div>

2009/8/2 Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > >
> 
> Â \* We should use RSTRING\_PTR() instead of RSTRING()-\>ptr  
> Â Â and RSTRING\_LEN() instead of RSTRING()-\>len.  
> Â \* We should return self for setter method:  
> Â Â [http://ruby-gnome2.sourceforge.jp/hiki.cgi?Naming+and+Conversion+Rules#Accessors+(Setter%2FGetter+methods)](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Naming+and+Conversion+Rules#Accessors+%28Setter%2FGetter+methods%29)

OK, these are easy to change, I just used whatever worked (and  
whatever was described in README.EXT)

> Â \* Why memmove()? Should we use memcpy()?

memmove is the safer variant. If the user got to the original pointer  
somehow the copied regions might overlap. While it’s unlikely one  
more check should not impact performance and should avoid some  
interesting bugs.

> Â \* Should we require “pixbuf.pixels.size == new\_pixels.size”?

I guess it’s OK to set only part of the pixels. I am not sure  
returning error is appropriate if the sizes do not match.

> get\_pixel, set\_pixel:
> 
> Â \* Are they needed?

When you do not want to set all pixels of a large bitmap they might be  
faster. And they give the high-level interface so they should be the  
primary access method for high-level language like ruby.

They should be probably converted to match the color format used by  
other methods,though.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [August 3, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/6 "2009-08-03T14:22:38Z")

</div>

2009/8/2 Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Â \* We should use RSTRING\_PTR() instead of RSTRING()-\>ptr
> 
> So I think that we can use memcpy().
> 
> > > Â \* Should we require “pixbuf.pixels.size == new\_pixels.size”?
> > 
> > I guess it’s OK to set only part of the pixels. I am not sure  
> > returning error is appropriate if the sizes do not match.
> 
> If we provide an ‘offset’ argument, it will be reasonable.  
> But set\_pixels doesn’t provide it.
> 
> So I think that the sizes should be matched.

Perhaps a interface would be better than pixels and pixels= then?

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [August 4, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/cannot-set-pixbuf-pixels-how-to-modify-an-image/172707/7 "2009-08-04T12:55:59Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] cannot set pixbuf pixels - how to modify  
an image?” on Mon, 3 Aug 2009 14:19:52 +0200,  
Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Perhaps a interface would be better than pixels and pixels= then?

I don’t think so.  
It will be used for pixel and pixel= but they are the next  
step. They should be considered after pixels=. OK?

## Thanks,

kou
