gtk.gdk.Pixbuf.scale() unexpected behavior when offset != 0



Hi!

I'm developing a pygtk application where I need to show images zoomed in so that the user can see individual pixels. gtk.gdk.Pixbuf.scale() seemed ideal for this, but if I set offset_x and offset_y to anything other than 0, the resulting image is heavily distorted and the offset is wrong. I've searched the internet for any snippet of code that uses this function with nonzero offset, but even after several hours of searching I've still come up blank. I think this may be a bug, but since it seems so fundamental I think it's way more likely that I've misunderstood something, so I thought I'd pass this by c.l.p first. Any help is greatly appreciated.

I wrote a test program to show off this behavior. Please find attached an image of David Hasselhoff with some puppies to help facilitate this demonstration. (feel free to use any image, but who doesn't like Hasselhoff and puppies?)

show_hasselhoff.py
#-------------------------------------------------------
import gtk

original = gtk.gdk.pixbuf_new_from_file('hasselhoff.jpeg')
w = original.get_width()
h = original.get_height()
interp = gtk.gdk.INTERP_NEAREST

nice = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h)
ugly = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h)
original.scale(nice, 0, 0, w, h, 0, 0, 2, 2, interp)
original.scale(ugly, 0, 0, w, h, w/2, h/2, 2, 2, interp)

outtake = original.subpixbuf(w/4, h/4, w/2, w/2)
expected = outtake.scale_simple(w, h, interp)

w = gtk.Window()
hbox = gtk.HBox()
hbox.add(gtk.image_new_from_pixbuf(original))
hbox.add(gtk.image_new_from_pixbuf(nice))
hbox.add(gtk.image_new_from_pixbuf(ugly))
hbox.add(gtk.image_new_from_pixbuf(expected))
w.add(hbox)
w.show_all()
w.connect('destroy', gtk.main_quit)
gtk.main()
#-------------------------------------------------------

When you run this, you should see 4 images in a window. From left to right: original, nice, ugly and expected. nice, ugly and expected are scaled/cropped copies of original, but ugly and expected are offset to show less mullet and more face. expected is what I expected ugly to turn out like judging from the pygtk docs.

Things to note about ugly:
* The topleft pixel of original has been stretched to the area of offset_x * offset_y.
* The first offset_x - 1 top pixels of original have been scaled by a factor 2 horizontally and then stretched vertically to the height of offset_y.
* Vice versa for the first offset_y - 1 leftmost pixels of original.
* The remaining area of ugly is a scaled version of original(1,1,width/2-1,height/2-1).

Things to note about the methods:
* This behavior is constant for all interpolation methods.
* This behavior is identical in gtk.gdk.Pixbuf.compose().

This can't possibly be how this is supposed to work! Have I misunderstood something, or is this a bug?

Cheers!
/Joel Hedlund

JPEG image



Relevant Pages

  • RE: 2.6.19-rc4-mm2
    ... 0x1002 is ATI's PCI vendor id. ... have to pretend that unpacked 24-bit pixels were twice as many 16-bit ... How much is each line offset when you have the garbled stuff? ... My card is a dual-head card, but I'm only using one head. ...
    (Linux-Kernel)
  • Re: [fitsbits] Images getting shifted and wrapping on themselves...?
    ... gberz3 wrote: ... FITS block following the header block that contains the END keyword. ... offset issue, then it would simply chop off the remainder, not wrap it ... If there are extra bytes at the beginning of the image data array, this will shift the image in all the rows to the right, and the pixels that fall off the right hand edge will be wrapped around and appear on the left side of the image on the next row. ...
    (sci.astro.fits)
  • Re: imaging_analysis
    ... If you know the offset in the different directions in pixels, ... large mask to the small image you use for the repmat. ... topLeftX = min)); ...
    (comp.soft-sys.matlab)
  • Re: Movie is offset within Quicktime view?
    ... the movie & its associated controller are offset to the ... right (about 100 pixels, in this case) from the left edge of the view. ... within the view was offset by 100 pixels. ... I have a fullscreen window showing a Flash movie (in a WebView), ...
    (comp.sys.mac.programmer.help)
  • Movie is offset within Quicktime view?
    ... right (about 100 pixels, in this case) from the left edge of the view. ... within the view was offset by 100 pixels. ... NSMovieView showing a Quicktime movie in a borderless window hovering ...
    (comp.sys.mac.programmer.help)