Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2005:
[Freeciv-Dev] (PR#12522) program to convert an alpha-level PNG to a no-a
Home

[Freeciv-Dev] (PR#12522) program to convert an alpha-level PNG to a no-a

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12522) program to convert an alpha-level PNG to a no-alpha one
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 15 Mar 2005 20:47:11 -0800
Reply-to: bugs@xxxxxxxxxxx

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

This little program (which I put here for archiving) will convert a PNG 
file with alpha levels to one without.  It does so completely at random, 
making each pixel either solid or transparent depending on its alpha 
level.  Try compiling it and running as

   alpha fog.png fog.png

to convert an alpha-level fog to a B&W one.  Or run as

   for file in `find . -name "*.png"`; do
     ~/alpha $file $file
   done

to convert all PNGs in a directory (note this takes a while).  Or run as

   for file in `find . -name "*.png"`;
     do ~/alpha $file $file;
     file2=`echo $file | sed s/.png//`;
     pngquant $file 2>/dev/null && mv $file2-fs8.png $file;
   done

to get rid of alpha and convert PNGs to indexed (useful for XAW-client 
users).


Unfortunately it doesn't give very good results ;-).  The result for 
darkness is pretty good but for repetitive fog is rather poor.  See

   http://freeciv.org/~jdorje/crazyfog2.png
   http://freeciv.org/~jdorje/crazyfog.png

-jason

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

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

#include <assert.h>
#include <stdlib.h>
#include <time.h>

#include <gtk/gtk.h>

int main(int argc, char **argv)
{
  char *infile, *outfile;
  GdkPixbuf *alpha;
  int x, y;
  gboolean changed = FALSE;

  if (argc < 3) {
    printf("Usage: %s <input PNG> <output PNG>\n"
           "\n"
           "Give this program an input PNG and an output PNG name.  It\n"
           "will read the input file and convert at random any pixels that\n"
           "have an alpha level to either a solid or a transparent pixel.\n",
           argv[0]);
    exit(1);
  }

  infile = argv[1];
  outfile = argv[2];

  gtk_init(&argc, &argv);

  alpha = gdk_pixbuf_new_from_file(infile, NULL);
  if (!alpha) {
    printf("Could not load %s.\n", infile);
    exit(1);
  }

  printf("Converting %s to %s\n", infile, outfile);

  srandom(time(NULL));

  if (gdk_pixbuf_get_has_alpha(alpha)) {
    for (x = 0; x < gdk_pixbuf_get_width(alpha); x++) {
      for (y = 0; y < gdk_pixbuf_get_height(alpha); y++) {
        guchar *pixel = gdk_pixbuf_get_pixels(alpha)
          + y * gdk_pixbuf_get_rowstride(alpha)
          + x * gdk_pixbuf_get_n_channels(alpha);

        if (pixel[3] == 255 || pixel[3] == 0) {
          continue;
        }

        changed = TRUE;
        if (random() % 256 < pixel[3]) {
          pixel[3] = 255;
        } else {
          pixel[3] = 0;
        }
      }
    }
  }

  if (changed) {
    gdk_pixbuf_save(alpha, outfile, "png", NULL, NULL);
  }
  return 0;
}

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12522) program to convert an alpha-level PNG to a no-alpha one, Jason Short <=