Inconsistent behavior in Gtk Images




I had my program working so that I would load an image, display it, and make changes to the image. When I right clicked on the image, it would change to the previous image, then back to the current image when I released the right mouse button. This worked fine for quite some time. Then I recently changed some code, though I don't believe I touched the code affecting this, and the right click no longer changes the image. I have put cout's in my functions to know that the appropriate functions are being called, and they are.

The only two things I can think are this: Either my method of setting the pixbuf is not valid, and I've just been getting lucky to have it work so far, or something is happening so that the secondary pixbuf is getting loaded with the first pixbuf. Of course, I can't see where or how that happens. I'm also open to other explanations.

Does anyone have any thoughts?  Beuller?  Beuller?  Beuller?

The GdkPixbufs get set up the first time in the code below. From then on, the buffers that fill them get modified, and that has been enough to change the image in the past (in fact, it still works for the primary buffer). I have also tried explicitly setting the pixbufs again (one way was to just comment out the "if (firsttime)" line (JUST that line) at the end of prepDisplay() just above the return, so that the block of code is no longer encapsulated as an "if" statement and therefore always runs.

GdkPixbuf* Image::prepDisplay()
{
    int r,g,b;
    int firsttime=0;
if (NULL==gtkbuffer) // gtkbuffer initialized to NULL in constructor
    {
        gtkbuffer = g_new(guchar,3*width*height);
    }
if (NULL==gtkbuffer2) // gtkbuffer2 initialized to NULL in constructor
    {
        gtkbuffer2 = g_new(guchar,3*width*height);
        firsttime=1;
    }

    for (int j=(height-1);j>=0;j--)
    {
        for (int i=0;i<width;i++)
        {
            r = (int)(0.5 + pixels[i][j].r);
            g = (int)(0.5 + pixels[i][j].g);
            b = (int)(0.5 + pixels[i][j].b);
        
            if (firsttime)  // put same thing in both buffers if first time
            {
                gtkbuffer2[(i*3+0)+(width*j*3)] = (guchar)r;
                gtkbuffer2[(i*3+1)+(width*j*3)] = (guchar)g;
                gtkbuffer2[(i*3+2)+(width*j*3)] = (guchar)b;
            }
            else  // put gtkbuffer values in gtkbuffer2 if not first time
            {
                gtkbuffer2[(i*3+0)+(width*j*3)] = gtkbuffer[(i*3+0)+(width*j*3)];
                gtkbuffer2[(i*3+1)+(width*j*3)] = gtkbuffer[(i*3+1)+(width*j*3)];
                gtkbuffer2[(i*3+2)+(width*j*3)] = gtkbuffer[(i*3+2)+(width*j*3)];
            }
            gtkbuffer[(i*3+0)+(width*j*3)] = (guchar)r;
            gtkbuffer[(i*3+1)+(width*j*3)] = (guchar)g;
            gtkbuffer[(i*3+2)+(width*j*3)] = (guchar)b;
        }
    }
    if (firsttime)
    {
primary = gdk_pixbuf_new_from_data(gtkbuffer,GDK_COLORSPACE_RGB, FALSE, 8, width, height, width*3, NULL, NULL); secondary = gdk_pixbuf_new_from_data(gtkbuffer2,GDK_COLORSPACE_RGB, FALSE, 8, width, height, width*3, NULL, NULL);
    }
    return primary;
}

Here are my callbacks. I've tried putting cout's in the if statements to confirm that it's getting the callback and it's getting the right button.


gboolean mouse_release_on_image(GtkWidget *window, GdkEventButton *event, gpointer user_data)
{
    Image *theImage;
    theImage = (Image*)user_data;
    gint whichButton = event->button;

    if (3==whichButton)
    {
gtk_image_set_from_pixbuf (GTK_IMAGE(theImage->window), theImage->getPrimary());
    }

    return TRUE;
}

gboolean mouse_press_on_image(GtkWidget *img, GdkEventButton *event, gpointer user_data)
{
    Image *theImage;
    theImage = (Image*)user_data;
    gint whichButton = event->button;
    GdkPixbuf *temp;

    if (3==whichButton)
    {
        temp = theImage->getSecondary();
        gtk_image_set_from_pixbuf (GTK_IMAGE(theImage->window), temp);
    }

    return TRUE;
}

GdkPixbuf* Image::getSecondary()
{
    return secondary;
}

The function getPrimary() is the same as getSecondary(), with the obvious change that it returns primary instead of secondary. I also tried cout << getPrimary() << ", " << getSecondary() to see if I was getting the same address, and I got different numbers (though I may not be getting what I think I'm getting--I'm not 100% sure that this should work).

It's been making me crazy for 24 hours, now. If you don't have the answer, I'd even happily take the random long shot.

Thanks,
Brian














[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]