To combat these issues I have tried: Making a color palette and using a loop to create each pixel which resulted in a longer load time, Making a list of colors and then a loop which increased the file size and made the load longer, Making the script produce multiple volumes that get taken off my server so the program could fit on a computer which works but it isn't optimal, and I have tried cutting out as much fat as possible by making similar pixels on the X axis one block and by using functions to minimize the length of repeated variables and methods to one character; this has also helped a lot.
Is there anything I am missing? At the very least I want to make a system for similar pixels on the Y axis to be turned into one block but I do not know how to approach this correctly, and if I am a little ambitious it would be good to have the output fit into one or two files.
Thanks.
Code:
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class ImageToGlasses {
public static void main(String args[]) throws IOException {
//Horizontal size of a box with the same pixel colors
int boxLeng = 1;
//Should it reboot once done writing to peripheral?
boolean startupProgram = false;
//Current line of output file, resets every 21600 lines
int currentLine = 0;
//Keep scanning down X for similar pixel color?
boolean keepGoing = true;
//Previous color down X to test similar color
int prevColor = 0;
//Number of files created
int fileNo = 0;
//Output program
String outputName = "outFile";
//Input PNG or JPG
String fileName = "inImage.png";
if(args.length == 1) {
outputName = args[1];
fileName = args[0];
}else{
System.out.println("Ex: java -jar ImageToGlasses.jar image.png outputLua");
}
File file = new File(fileName);
BufferedImage image = ImageIO.read(file);
Files.deleteIfExists(Paths.get(outputName));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputName, true));
//Write a header
writer.append("g = peripheral.wrap(\"top\")\n");
writer.append("function b(x,y,leng,hex,opacity)\n");
writer.append("g.addBox(x,y,leng,1,hex,opacity)\n");
writer.append("end\n");
writer.append("g.clear()\n");
//Scan Left to right; Up to Down
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int clr = image.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
int alpha = (clr & 0xff000000) >>> 24;
String hex = String.format("0x%02X%02X%02X", red, green, blue);
//Is the last pixel down X the same as the current one? If so extend length of box
if (prevColor == clr) {
keepGoing = true;
for (int ex = x; ex < image.getWidth() && keepGoing; ex++) {
if (image.getRGB(ex, y) == prevColor) {
boxLeng++;
} else {
keepGoing = false;
}
}
}
prevColor = clr;
//Use opacity if applicable, Prevents the writing of fully transparent pixels to save space
if (((Integer) alpha).doubleValue() / 255.0 > 0.0) {
writer.append("b(" + x + "," + y + "," + boxLeng + "," + hex + "," + (((Integer) alpha).doubleValue() / 255.0) + ")\n");
}
//When there are similar pixels start scanning X after the box
if (boxLeng > 1) {
x = x + boxLeng - 1;
}
boxLeng = 1;
//Limit the file size, checks every 21600 lines to see if it's larger than ~500KB
if (currentLine == 21600) {
boolean createNewFile = false;
currentLine = 0;
writer.close();
if (fileNo == 0) {
if (Files.size(Paths.get(outputName)) >= 300000) {
createNewFile = true;
writer = new BufferedWriter(new FileWriter(outputName, true));
} else {
writer = new BufferedWriter(new FileWriter(outputName, true));
}
} else {
if (Files.size(Paths.get(outputName + fileNo)) >= 300000) {
createNewFile = true;
writer = new BufferedWriter(new FileWriter(outputName + fileNo, true));
} else {
writer = new BufferedWriter(new FileWriter(outputName + fileNo, true));
}
}
//Check if file exceeded max size, closes writer and starts a new one
if (createNewFile) {
fileNo++;
writer.append("shell.run(\"paster run " + outputName + fileNo + "\")");
writer.close();
Files.deleteIfExists(Paths.get(outputName + fileNo));
writer = new BufferedWriter(new FileWriter(outputName + fileNo, true));
writer.append("g = peripheral.wrap(\"top\")\n");
writer.append("function b(x,y,leng,hex,opacity)\n");
writer.append("g.addBox(x,y,leng,1,hex,opacity)\n");
writer.append("end\n");
}
}
currentLine++;
}
}
//Add footer when done with current file
writer.append("g.sync()\n");
if (startupProgram) {
writer.append("shell.run(\"startup\")");
}
writer.close();
}
}