How to use gtk_image (was: Re: gtk_image)




I'll give a short overview how I use GtkImages in my program. Please
correct if I'm totally wrong in some place.

In fact it is quite simple as the gdk does a really good job in
hiding the core X11. All of the following information is from
the gtk doc^H^H^Hsource ;-).


  Torsten.

-----------------------------------------------------------------------------

the variables:
--------------

    GdkGC         *_gc;
    GdkVisual     *_visual;
    GdkImage      *_image;
    GdkColormap   *_colormap;
    GdkColor       _col[8];
    GtkWidget     *_canvas;

creating the image:
-------------------

    _visual = gdk_visual_get_system();
    _image  = gdk_image_new(GDK_IMAGE_FASTEST, _visual,
		            CANVAS_WIDTH, CANVAS_HEIGHT);
    _gc = gdk_gc_new(GTK_WIDGET(_main.canvas)->window);

    NOTE: this GDK_IMAGE_FASTEST gives a shared image if this is
          available, possible values (in gdk/gdktypes.h):
	  GDK_IMAGE_NORMAL
          GDK_IMAGE_SHARED
          GDK_IMAGE_FASTEST

allocating some colors:
-----------------------

    char *color_names[] = {
        "black",
        "red",
        "rgb:d0/00/00",
        "rgb:d0/00/d0",
        "rgb:00/d0/00",
        "rgb:00/d0/d0",
        "rgb:d0/00/00",
        "rgb:d0/d0/d0"
    }

    _colormap = gdk_colormap_get_system();
    for (a = 0;a < 8;a++) {
        gdk_color_parse(color_names[a], &_col[a]);
        gdk_color_alloc(_colormap, &_col[a]);
    }

    NOTE: this doesn't check if the color is really allocated :-(

drawing into the image:
-----------------------

    gdk_image_put_pixel(_image, x, y, _col[0].pixel);

    NOTE: Hmm, I don't know if it's possible to use the gdk_draw_xxx
          functions too. I'm always using put_pixel in my emulator...
	  Most likely you have to figure out the type of the image
          and write directly into the memory that is allocated for the
          image.

          [gdk/gdktypes.h:]

	  struct _GdkImage
	  {
	    GdkImageType  type;
	    GdkVisual    *visual;     /* visual used to create the image */
	    GdkByteOrder  byte_order;
	    guint16       width;
	    guint16       height;
	    guint16       depth;
	    guint16       bpp;        /* bytes per pixel */
	    guint16       bpl;        /* bytes per line */
	    gpointer      mem;
	  };

display on screen:
------------------

    gdk_draw_image(GTK_WIDGET(_canvas)->window, _gc, _image,
                       0, 0, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

    NOTE: I'm not sure if the specified drawable is given in the right
          way (but it works ;-)
	  canvas is a GtkDrawingArea -> _canvas = gtk_drawing_area_new();


-- 
Torsten Paul				Es ist leichter, einen Atomkern zu
paul@os.inf.tu-dresden.de	//		spalten als ein Vorurteil.
__________________________ooO_(+ +)_Ooo________Albert Einstein_(1879-1955)
				U



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