Advertisement

GdkImage + GdkVisual question

Started by December 21, 2002 06:20 AM
0 comments, last by stefu 21 years, 8 months ago
Hello. I'm writing an editor in GTK that uses textures in opengl. I want current texture to be shown in GtkImage widget. This is what I do:
  
void set_current_image_tex()
{
  GdkVisual *visual = gdk_visual_get_best();
  ETexture *t = getDocument()->getTexture(selected_texture);
  if (t) {
    Image &img = t->img;
    GdkImage *image = gdk_image_new(GDK_IMAGE_NORMAL, visual, img.getWidth(),img.getHeight());
    for (int x=0; x<img.getWidth(); x++)
      for (int y=0; y<img.getHeight(); y++)
      {
        uint value  = img.getPixel(x,y);
        uchar red   = uint2red(value);
        uchar green = uint2green(value);
        uchar blue  = uint2blue(value);
        gdk_image_put_pixel(image,x,y, pixel_from_rgb(visual, red, green, blue));
      }
    // set new image to GtkImage widget

    gtk_image_set(GTK_IMAGE(image_tex),image,0);
  }
}

// And the pixel_from_rgb I found from DevHelp

guint pixel_from_rgb (GdkVisual *visual, guchar r, guchar b, guchar g)
{
  return ((r >> (16 - visual->red_prec))   << visual->red_shift) |
         ((g >> (16 - visual->green_prec)) << visual->green_shift) |
         ((r >> (16 - visual->blue_prec))  << visual->blue_shift);
}
  
The problem is that this doesnt work. The image is always black. I don't know how to get right visual (I get the best). I can get it to work if I use these (I tried):
    
guint pixel_from_rgb (GdkVisual *visual, guchar r, guchar b, guchar g)
{
  // This works on my Linux desktop (32 bit display)

  return rgb2uint(g,b,r);
  // And this on Windows with Voodoo3 (16 bit display)

  return (g>>3) | ((b>>2)<<5) | ((r>>3)<<11);
    
My question is how to write a pixel_from_rgb that works in all cases? What visual to get? Should it be same as is the graphics mode? Thanks. [edited by - stefu on December 21, 2002 7:21:07 AM]
Try 8 - visual->x_prec. If the precision is 8, 16 - 8 = 8, so the right shift will set the value to 0.

This topic is closed to new replies.

Advertisement