Forum: Ruby-Gnome 2 imlib2 rotate problem

7f28f9db46bfe8e9b8b5fcd90b2f1302?d=identicon&s=25 J. K. (j_k)
on 2011-02-08 21:52
hello there -
  i've been searching for ways to rotate images by 'arbitrary' angles.
i'm basically trying to rotate an image based on a time thread - it
seems that Gdk::Pixbuf only allows for rotations of 90, 180, or 270
degrees - am i wrong?  man i hope so...  that would make things so very
much easier.  i've played around with the Cairo context rotate method,
which accepts any angle, and provides a way to define the center of the
rotation - but i've had a million headaches trying to incorporate
cairo's need to be painted when the window is realized into a
Gtk::Layout or Gnome::Canvas, with no luck.
  assuming that in my hours of searching on google and here on the forum
i haven't overlooked some obvious methods that would solve my problems,
i've been left trying various image libraries.  i've tried RMagick which
has a handy rotate method, but is too slow to be used in the way i would
like to.
  i stumbled upon Imlib2, which is quick and well documented and seems
like it should do everything i need - BUT....

this code:

##############
require 'gtk2'
require 'imlib2'

win = Gtk::Window.new()
image = Imlib2::Image.load_image("TradyBlix.png")

image.rotate! 0

image.save("temp.png")

gtkimg = Gtk::Image.new("temp.png")

win.add(gtkimg)

win.show_all

Gtk.main

#############

  ...which ought to rotate my image by, well, 0 degrees -
rotates it instead to what seems like a random angle.  if i run the
program several times in succession it rotates the image differently
each time...

  any ideas what the heck is happening here?  i've searched and
searched, and can't seem to find any explanation.

  any other ideas for ways to rotate an image in gtk would be
appreciated too.  relying on the library to rotate and then save a temp
file to later load it under gtk seems barbaric, but i can't figure out a
better way to do it.

  thanks in advance -

  jk
Eed8337939641eac5ad0b570bd6acf96?d=identicon&s=25 marcin@saepia.net (Guest)
on 2011-02-09 14:43
(Received via mailing list)
Have you tried cairo already?

m.
7f28f9db46bfe8e9b8b5fcd90b2f1302?d=identicon&s=25 J. K. (j_k)
on 2011-02-09 16:16
marcin@saepia.net wrote in post #980555:
> Have you tried cairo already?
>
> m.

i have indeed, and it works nicely...  i believe the problem i have
implementing cairo in what i want is that i don't fully understand how
it works.  i've got this:

#########
require 'gtk2'

file = "/home/jk/ruby/TradyBlix.png"
image = Gdk::Pixbuf.new(file, 100, 100)

win = Gtk::Window.new
win.set_default_size(500, 500)

draw_area = Gtk::DrawingArea.new
win.add(draw_area)

x = 10

draw_area.signal_connect("expose_event"){|widget, event|
  @cc= widget.window.create_cairo_context
  @cc.translate(200, 200)
  @cc.rotate(x * Math::PI / 180)
  @cc.set_source_pixbuf(image, 0, 0)
  @cc.paint
  }
win.show_all
Gtk.main
############

  ...and i've written something longer that ties it into a timer,
rotating the image 6 degrees per second.  this works just fine, and
seems like the best solution.  the problem i'm having is this:  imagine
that you wanted to use one image as the second hand of a stopwatch,
rotating it on top of another image - this is similar to what i want to
do.  what i have tried is placing the Gtk::DrawingArea defined above
along with another image into a Gtk::Layout, or a Gnome::Canvas.
neither of these work.  it seems i lose the expose_event when i place
the DrawingArea into the Layout...  is this true?  i've tried figuring
out how to pass an expose signal through the layout to the drawing area,
but have had no luck.
  do you know of a way to accomplish what i'm trying to do?  i would
prefer to do it with cairo, and not rely on any other image library.

  thanks for getting back to me....

  j
Eed8337939641eac5ad0b570bd6acf96?d=identicon&s=25 marcin@saepia.net (Guest)
on 2011-02-09 22:43
(Received via mailing list)
Attachment: peakmeter.rb (4 KB)
Well, I am not so experienced in GTK widgets internals that I could
reply to your questions...

But I was able to do something working - it's a volume peakmeter.
Usage is quite simple:

1. it takes current volume level from GStreamer's "volume" element
every 1/n second, but of course there could be any other source of the
data,
2. sets @peak and @rms variables to some negative values in the dB,
3. call on the widget's "update" method.

And it works well in many of my projects.

I don't remember where I found the inspiration for that, I mean the
usage of expose signal and queue_draw(), but it works so I hadn't
checked the internals... Like regular script kiddie, uh :/

Please see the attached file, maybe it will be helpful. Please ask in
case of any questions,

m.
7f28f9db46bfe8e9b8b5fcd90b2f1302?d=identicon&s=25 J. K. (j_k)
on 2011-02-10 12:05
marcin@saepia.net wrote in post #980665:
>
> ...But I was able to do something working - it's a volume peakmeter.
> Usage is quite simple...

hi m -
  thanks for getting back - going to have a good look at your script
when i get some free time...  i was thinking of working on a peakmeter
myself, so i'm very interested to see what you've got there.  sounds
like there could be some clues to solving my other problem too - thanks
again!
   - i'll get back to you after looking at the script...
  ciao -

  j
7f28f9db46bfe8e9b8b5fcd90b2f1302?d=identicon&s=25 J. K. (j_k)
on 2011-02-13 04:02
hi marcin - jk here again...
  thanks for the post - looks like it was just the #.queue_draw method
that i was looking for!  rewrote something small using cairo that
rotates the images when a button is pushed - easy enough to tie into a
signal from the timer i've got.
  the peakmeter looks great.  if you don't mind i'll try to work it into
the audio player i'm writing (certainly give you credit when and if i
post the thing...)

  thanks again -

  j
Eed8337939641eac5ad0b570bd6acf96?d=identicon&s=25 marcin@saepia.net (Guest)
on 2011-02-13 21:47
(Received via mailing list)
Of course, feel free to use it. Just please delete Radion module from
the module tree - it is reserved for my project's name :D

m.

2011/2/13 J. K. <ruby-forum-incoming@andreas-s.net>:
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.