Created
July 7, 2014 07:01
-
-
Save Unh0lyTigg/2f768a66313b9ff181c2 to your computer and use it in GitHub Desktop.
MinecraftAsciiSpacer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import javax.imageio.ImageIO; | |
public class MinecraftAsciiSpacer { | |
private static SimpleDateFormat dateFormat = new SimpleDateFormat("[hh:mm:ss]: "); | |
public static void main(String[] args) throws IOException { | |
File originFile = new File("E:/Misc/Minecraft_ascii_clear_background.png"); | |
File outputFile = new File("E:/Misc/Minecraft_ascii_padded.png"); | |
File outputFile1_5 = new File("E:/Misc/Minecraft_ascii_p_o_debug.png"); | |
File outputFile2 = new File("E:/Misc/Minecraft_ascii_padded_outline_x5.png"); | |
log("Starting..."); | |
log("\tReading input file."); | |
BufferedImage origin = ImageIO.read(originFile); | |
BufferedImage output = new BufferedImage(160, 160, BufferedImage.TYPE_INT_ARGB); | |
log("\tDeleting and re-creating output file."); | |
outputFile.delete(); | |
outputFile.createNewFile(); | |
log("\tGenerating output image."); | |
for (int index = 0; index < 256; index++) { | |
Object[] data = getChar(origin, index); | |
int[] newCoords = convertOriginToOutput((int)data[0], (int)data[1]); | |
BufferedImage charImg = (BufferedImage)data[2]; | |
for (int x = newCoords[0], sX = 0; sX < 8; x++, sX++) | |
for (int y = newCoords[1], sY = 0; sY < 8; y++, sY++) | |
output.setRGB(x, y, charImg.getRGB(sX, sY)); | |
} | |
log("\tWriting output image."); | |
ImageIO.write(output, "png", outputFile); | |
log(" Done with 1st output, now working on 2nd image (output at 500% with all dark pixels that aren't next to a light pixel removed)."); | |
BufferedImage output2 = new BufferedImage(800,800, output.getType()); | |
log("\tDeleting and re-creating 2nd output file."); | |
outputFile2.delete(); | |
outputFile2.createNewFile(); | |
log("\tGenerating 2nd output image."); | |
log("\t\tCreating x5 size version."); | |
for (int x = 0; x < 160; x++) | |
for (int y = 0; y < 160; y++) { | |
int colorVal = output.getRGB(x, y); | |
for (int x1 = 0; x1 < 5; x1++) | |
for (int y1 = 0; y1 < 5; y1++) | |
output2.setRGB(x1 + (x*5), y1 + (y*5), colorVal); | |
} | |
log("\t\tRemoving unneeded pixels."); | |
ImageIO.write(output2, "png", outputFile1_5); | |
removeUnNeeded(output2); | |
log("\tWriting 2nd output image."); | |
ImageIO.write(output2, "png", outputFile2); | |
log("Done."); | |
} | |
private static Object[] getChar(BufferedImage origin, int index) { | |
BufferedImage output = new BufferedImage(8, 8, origin.getType()); | |
int xIndex = index % 16; | |
int yIndex = index / 16; | |
int imgXStart = xIndex * 8; | |
int imgYStart = yIndex * 8; | |
for (int sx = imgXStart, dx = 0; sx < (imgXStart + 8); sx++, dx++) | |
for (int sy = imgYStart, dy = 0; sy < (imgYStart + 8); sy++, dy++) | |
output.setRGB(dx, dy, origin.getRGB(sx, sy)); | |
return new Object[]{xIndex, yIndex, output}; | |
} | |
private static int[] convertOriginToOutput(int x, int y) { | |
int[] output = new int[2]; | |
output[0] = 1+(x*10); | |
output[1] = 1+(y*10); | |
return output; | |
} | |
private static void removeUnNeeded(BufferedImage image) throws FileNotFoundException { | |
Set<Loc> whitePixels = new HashSet<Loc>(); | |
Set<Loc> blackPixelsToRemove = new HashSet<Loc>(); | |
int width = image.getWidth(), height = image.getHeight(); | |
for (int x = 0; x < width; x++) | |
for (int y = 0; y < height; y++) | |
if ((image.getRGB(x, y) & 0x00FFFFFF) != 0) { | |
whitePixels.add(new Loc(x, y)); | |
} else { | |
blackPixelsToRemove.add(new Loc(x, y)); | |
} | |
log("\t\t\t\tWhitePixelCount: " + whitePixels.size()); | |
int totalPossible = (width * height) - whitePixels.size(); | |
int count = 0; | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
Loc a = new Loc(x, y); | |
if (whitePixels.contains(a)) | |
continue; | |
List<Loc> toSearch = (a).getDirectNeighbors(width - 1, height - 1); | |
log("\t\t\tSearching neighbors of (" + a + ") at " + toSearch + " | #" + count + " of possible " + totalPossible + " | " + (double)((double)((double)count / (double)totalPossible) * 100.0D) + "%"); | |
count++; | |
neighborSearch: | |
for (Loc l : toSearch) | |
if (whitePixels.contains(l)) { | |
blackPixelsToRemove.remove(a); | |
break neighborSearch; | |
} | |
} | |
} | |
log("\t\t\tFound " + blackPixelsToRemove.size() + " pixels to remove"); | |
PrintWriter out = new PrintWriter(new File("E:/Misc/bptr.log")); | |
for (Loc l : blackPixelsToRemove) { | |
out.println("" + l.x + "," + l.y); | |
image.setRGB(l.x, l.y, 0x00FFFFFF); | |
} | |
out.flush(); | |
out.close(); | |
} | |
private static class Loc { | |
public int x, y; | |
public Loc(int x, int y) { | |
this.x = x; this.y = y; | |
} | |
public List<Loc> getDirectNeighbors(int maxX, int maxY) { | |
List<Loc> list = new ArrayList<Loc>(); | |
if (x != 0) | |
list.add(new Loc(x - 1, y)); | |
if (y != 0) | |
list.add(new Loc(x, y - 1)); | |
if (x != maxX) | |
list.add(new Loc(x + 1, y)); | |
if (y != maxY) | |
list.add(new Loc(x, y + 1)); | |
return list; | |
} | |
@Override | |
public String toString() { | |
return "Loc [x=" + x + ", y=" + y + "]"; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + x; | |
result = prime * result + y; | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null) return false; | |
if (getClass() != obj.getClass()) return false; | |
Loc other = (Loc) obj; | |
if (x != other.x) return false; | |
if (y != other.y) return false; | |
return true; | |
} | |
} | |
private static void log(String text) { | |
System.out.println(dateFormat.format(new Date()) + text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment