Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2005:
[Freeciv-Dev] (PR#12067) tool for converting B&W to alpha
Home

[Freeciv-Dev] (PR#12067) tool for converting B&W to alpha

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12067) tool for converting B&W to alpha
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 31 Jan 2005 21:17:15 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12067 >

Attached is (for the record) a program that will convert from a 
grayscale image into a solid-black image that takes its alpha level from 
the color of the original image.

This is of use for civ3gfx.

-jason

/* By Jason Dorje Short.  Distributed under the GPL. */

/* Compile as
      gcc `pkg-config --cflags --libs gtk+-2.0` alpha.c -o alpha
 */

#include <gtk/gtk.h>

int main(int argc, char **argv)
{
  GdkPixbuf *fog;
  int x, y, width, height;
  char *in, *out;

  if (argc < 3) {
    printf("Usage: %s <input file> <output PNG>\n", argv[0]);
    printf("\n");
    printf("The input file is assumed to be a grayscale image.  It is\n");
    printf("converted into an all-black image with an alpha level.  The\n");
    printf("alpha is chosen based on the input: white is transparent,\n");
    printf("black solid.\n");
    exit(1);
  }

  in = argv[1];
  out = argv[2];
  printf("Converting %s to %s.\n", in, out);

  gtk_init(&argc, &argv);

  fog = gdk_pixbuf_new_from_file(in, NULL);

  gdk_pixbuf_add_alpha(fog, TRUE, 200, 0, 200);
  width = gdk_pixbuf_get_width(fog);
  height = gdk_pixbuf_get_height(fog);

  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
      guchar *pixel = gdk_pixbuf_get_pixels(fog)
        + y * gdk_pixbuf_get_rowstride(fog)
        + x * gdk_pixbuf_get_n_channels(fog);

      if (pixel[3] == 0 || pixel[0] != pixel[1]) {
        pixel[3] = 0;
      } else {
        pixel[3] = 255 - pixel[0];
      }
      pixel[0] = pixel[1] = pixel[2] = 0;
    }
  }

  gdk_pixbuf_save(fog, out, "png", NULL, NULL);
  return 0;
}

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12067) tool for converting B&W to alpha, Jason Short <=