2011년 7월 17일 일요일

Use GdkPixbuf in GTK+

At first it needs to create GtkImage on GtkViewport. Then, we can use GdkPixbuf-embedded GtkImage.

static void gdk_pixbuf_clear(GdkPixbuf *pixbuf)
{
   int x, y;
   int width, height, rowstride, n_channels;
   guchar *pixels, *p;

   n_channels = gdk_pixbuf_get_n_channels(pixbuf);
   g_assert(gdk_pixbuf_get_colorspace(pixbuf) == GDK_COLORSPACE_RGB);
   g_assert(gdk_pixbuf_get_bits_per_sample(pixbuf) == 8);
   g_assert(gdk_pixbuf_get_has_alpha(pixbuf));
   g_assert(n_channels == 4);

   width = gdk_pixbuf_get_width(pixbuf);
   height = gdk_pixbuf_get_height(pixbuf);
   rowstride = gdk_pixbuf_get_rowstride(pixbuf);
   pixels = gdk_pixbuf_get_pixels(pixbuf);

   for (y = 0; y < height; y++) {
      for (x = 0; x < width; x++) {
         p = pixels + y*rowstride + x*n_channels;
         *(p+0) = 255; // red channel
         *(p+1) = 0; // green channel
         *(p+2) = 0; // blue channel
         *(p+3) = 255; // alpha channel
      }
   }
}


....

image = gtk_image_new();

....

pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
g_assert(pixbuf);

gdk_pixbuf_clear(pixbuf);

gtk_image_set_from_pixbuf(image, pixbuf);

...

댓글 없음:

댓글 쓰기