Howto remove alpha of png use GdkPixbuf

remove-alpha.c:

/**
 * gcc -g -o remove-alpha remove-alpha.c `pkg-config --cflags --libs gtk+-2.0` 
 */
#include <gtk/gtk.h>

GdkPixbuf*
make_gdk_pixbuf_background (const GdkPixbuf *source)
 {
   GdkPixbuf *dst;
   guchar    *dst_pixels;
   guchar    *pixdst;
   gint       dst_row_stride;
   gint       width;
   gint       height;
   gint       i, j;

   g_return_val_if_fail (GDK_IS_PIXBUF (source), NULL);
  /* determine source parameters */
   width = gdk_pixbuf_get_width (source);
   height = gdk_pixbuf_get_height (source);

   /* allocate the destination pixbuf */
   dst = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (source), FALSE, gdk_pixbuf_get_bits_per_sample (source), width, height);

   dst_row_stride = gdk_pixbuf_get_rowstride (dst);
   dst_pixels = gdk_pixbuf_get_pixels (dst);

   /* fill white to dst image as background */
   if (G_LIKELY (gdk_pixbuf_get_has_alpha (source)))
     {
       for (i = height; --i >= 0; )
         {
           pixdst = dst_pixels + i * dst_row_stride;
           for (j = width; --j >= 0; )
             {
               *pixdst++ = 0xFF;
               *pixdst++ = 0xFF;
               *pixdst++ = 0xFF;
             }
         }
     }
   return dst;
 }
void destroy( GtkWidget *widget,
              gpointer   data )
{
    gtk_main_quit ();
}

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
    GtkWidget *image;

    if (argc != 3) {
      g_message ("usage: ./remove-alpha /home/user/Pictures/alpha.png /home/user/Pictures/noalpha.bmp");
      return 0;
    }

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    g_signal_connect (G_OBJECT (window), "destroy",
                      G_CALLBACK (destroy), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file (argv[1], NULL);

    if (gdk_pixbuf_get_has_alpha (src_pixbuf)) {
      GdkPixbuf *pixbuf = make_gdk_pixbuf_background (src_pixbuf);

      int width = gdk_pixbuf_get_width (src_pixbuf);
      int height = gdk_pixbuf_get_height (src_pixbuf);

      gdk_pixbuf_composite (src_pixbuf, pixbuf,
                            0, 0,
                            width,
                            height,
                            0, 0,
                            1, 1,
                            GDK_INTERP_BILINEAR, 255);

      gdk_pixbuf_save (pixbuf, argv[2], "bmp", NULL, NULL);
      image = gtk_image_new_from_pixbuf (pixbuf);
      g_object_unref (pixbuf);

    } else {
      image = gtk_image_new_from_pixbuf (src_pixbuf);
    }

    gtk_container_add (GTK_CONTAINER (window), image);
    g_object_unref (src_pixbuf);
    gtk_widget_show_all (window);

    gtk_main ();

    return 0;
}

你可能感兴趣的:(C++,c,C#,gcc,J#)