Commits

Anonymous committed 569ac0a

added conversions for black-background and grayscale tilesets

Comments (0)

Files changed (2)

src/main/java/net/fishbulb/jcod/demo/Demo.java

 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Input;
 import com.badlogic.gdx.InputAdapter;
-import com.badlogic.gdx.graphics.Color;
-import com.badlogic.gdx.graphics.FPSLogger;
-import com.badlogic.gdx.graphics.GL10;
-import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.*;
 import com.badlogic.gdx.scenes.scene2d.Stage;
 import com.badlogic.gdx.scenes.scene2d.actions.Actions;
 import net.fishbulb.jcod.Console;
 import net.fishbulb.jcod.display.TileDisplay;
 import net.fishbulb.jcod.display.Tileset;
 import net.fishbulb.jcod.util.BlendMode;
+import net.fishbulb.jcod.util.ImageUtils;
 
 import java.util.Random;
 
         Gdx.input.setInputProcessor(this);
         stage.getRoot().setTransform(false);
 
-        Texture tiles = new Texture(Gdx.files.internal("tiles/consolas10x10_gs_tc.png"));
+        Pixmap tpm = new Pixmap(Gdx.files.internal("tiles/consolas10x10_gs_tc.png"));
+        tpm = ImageUtils.grayScaleToAlpha(tpm);
+        Texture tiles = new Texture(tpm);
         Tileset tileset = new ImageTileset(tiles, 10, 10, ImageTileset.Layout.TCOD);
 
         TileDisplay display = new TileDisplay(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT, 10, 10);

src/main/java/net/fishbulb/jcod/util/ImageUtils.java

+package net.fishbulb.jcod.util;
+
+
+import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.graphics.Pixmap;
+
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+
+public class ImageUtils {
+
+    public static Pixmap makeColorTransparent(Pixmap pixmap, Color color) {
+        Pixmap.Format format = pixmap.getFormat();
+
+        if (format == Pixmap.Format.RGB888) {
+            // promote it to a new map
+            Pixmap p2 = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Pixmap.Format.RGBA8888);
+            p2.drawPixmap(pixmap,0,0);
+            pixmap = p2;
+        } else if (format != Pixmap.Format.RGBA8888) {
+            throw new IllegalArgumentException(
+                    "Invalid format " + pixmap.getFormat() + " -- makeColorTransparent only works on RGBA8888 pixmaps");
+        }
+
+        int trans = Color.rgba8888(color);
+
+        ByteBuffer bytes = pixmap.getPixels();
+        IntBuffer pixels = bytes.asIntBuffer();
+        pixels.rewind();
+
+        while (pixels.hasRemaining()) {
+
+            pixels.mark();
+            int pixel = pixels.get();
+            if (pixel == trans) {
+                pixels.reset();
+                pixels.put(0);
+            }
+        }
+        return pixmap;
+    }
+
+    public static Pixmap grayScaleToAlpha(Pixmap pixmap, boolean premultiplied) {
+        Pixmap.Format format = pixmap.getFormat();
+
+        if (format == Pixmap.Format.RGB888) {
+            // promote it to a new map
+            Pixmap p2 = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Pixmap.Format.RGBA8888);
+            p2.drawPixmap(pixmap,0,0);
+            pixmap = p2;
+        } else if (format != Pixmap.Format.RGBA8888) {
+            throw new IllegalArgumentException(
+                    "Invalid format " + pixmap.getFormat() + " -- makeColorTransparent only works on RGBA8888 pixmaps");
+        }
+
+        ByteBuffer pixels = pixmap.getPixels();
+        pixels.rewind();
+
+        while (pixels.hasRemaining()) {
+            pixels.mark();
+            byte r = pixels.get();
+            byte g = pixels.get();
+            byte b = pixels.get();
+            byte a = pixels.get();
+
+            if  (r == g && g == b && a != r) {
+                a = r;
+                if (!premultiplied) {
+                    r = g = b = (byte)255;
+                }
+                pixels.reset();
+                pixels.put(r);
+                pixels.put(g);
+                pixels.put(b);
+                pixels.put(a);
+            }
+        }
+        pixels.position(0);
+        return pixmap;
+    }
+
+    public static Pixmap grayScaleToAlpha(Pixmap pixmap) {
+        return grayScaleToAlpha(pixmap, false);     // default blend mode in SpriteBatch assumes non-premultiplied
+    }
+
+
+}