Forum: Ruby-Gnome 2 cannot set pixbuf pixels - how to modify an image?

Posted by Michal Suchanek (Guest)
on 2009-07-26 13:41
(Received via mailing list)
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

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
Posted by Michal Suchanek (Guest)
on 2009-07-28 03:14
Attachment: pixbuf-patch.diff (4,5 KB)
(Received via mailing list)
On 26/07/2009, Michal Suchanek <hramrach@centrum.cz> 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
>
>  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
Posted by Kouhei Sutou (Guest)
on 2009-08-02 04:44
(Received via mailing list)
Hi,

In <a5d587fb0907271813q406e79a7u95311469c9ea1c39@mail.gmail.com>
  "Re: [ruby-gnome2-devel-en] cannot set pixbuf pixels - how to modify 
an image?" on Tue, 28 Jul 2009 03:13:00 +0200,
  Michal Suchanek <hramrach@centrum.cz> 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+%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
Posted by Michal Suchanek (Guest)
on 2009-08-02 08:37
(Received via mailing list)
2009/8/2 Kouhei Sutou <kou@cozmixng.org>:
>>>
>>>
>  * 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+%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
Posted by Kouhei Sutou (Guest)
on 2009-08-02 11:15
(Received via mailing list)
Hi,

In <a5d587fb0908012337j58e8b741kafd9da37eb684f8f@mail.gmail.com>
  "Re: [ruby-gnome2-devel-en] cannot set pixbuf pixels - how to modify 
an image?" on Sun, 2 Aug 2009 08:37:15 +0200,
  Michal Suchanek <hramrach@centrum.cz> 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
Posted by Michal Suchanek (Guest)
on 2009-08-03 14:22
(Received via mailing list)
2009/8/2 Kouhei Sutou <kou@cozmixng.org>:
>>>  * 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
Posted by Kouhei Sutou (Guest)
on 2009-08-04 12:55
(Received via mailing list)
Hi,

In <a5d587fb0908030519q6b3dbd19o39b92a91b9afd2ee@mail.gmail.com>
  "Re: [ruby-gnome2-devel-en] cannot set pixbuf pixels - how to modify 
an image?" on Mon, 3 Aug 2009 14:19:52 +0200,
  Michal Suchanek <hramrach@centrum.cz> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.