mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-10 18:22:39 +00:00
Add importing PNG files as icon_states
This commit is contained in:
Binary file not shown.
@@ -8,6 +8,9 @@ public class DMIException extends Exception {
|
||||
desc = descriptor;
|
||||
this.line = line;
|
||||
}
|
||||
public DMIException(String what) {
|
||||
super(what);
|
||||
}
|
||||
public DMIException(String what, Exception cause) {
|
||||
super(what, cause);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import java.util.Arrays;
|
||||
import ar.com.hjg.pngj.ImageInfo;
|
||||
import ar.com.hjg.pngj.ImageLineInt;
|
||||
import ar.com.hjg.pngj.PngWriter;
|
||||
import ar.com.hjg.pngj.PngReader;
|
||||
import ar.com.hjg.pngj.PngjInputException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class IconState {
|
||||
@@ -144,4 +147,95 @@ public class IconState {
|
||||
}
|
||||
out.end();
|
||||
}
|
||||
}
|
||||
|
||||
public static IconState importFromPNG(DMI dmi, InputStream inS, String name) throws DMIException {
|
||||
int w = dmi.w;
|
||||
int h = dmi.h;
|
||||
|
||||
PngReader in;
|
||||
try {
|
||||
in = new PngReader(inS);
|
||||
} catch(PngjInputException pie) {
|
||||
throw new DMIException("Bad file format!", pie);
|
||||
}
|
||||
int pxW = in.imgInfo.cols;
|
||||
int pxH = in.imgInfo.rows;
|
||||
int frames = pxW / w;
|
||||
int dirs = pxH / h;
|
||||
|
||||
// make sure the size is an integer multiple
|
||||
if(frames * w != pxW || frames==0) throw new DMIException("Illegal image size!");
|
||||
if(dirs * h != pxH || dirs==0) throw new DMIException("Illegal image size!");
|
||||
|
||||
int[][] px = new int[pxH][];
|
||||
for(int i=0; i<pxH; i++) {
|
||||
ImageLineInt ili = (ImageLineInt)in.readRow();
|
||||
int[] sl = ili.getScanline();
|
||||
px[i] = sl.clone();
|
||||
}
|
||||
|
||||
Image[] images = new Image[frames*dirs];
|
||||
for(int imageY=0; imageY<dirs; imageY++) {
|
||||
for(int imageX=0; imageX<frames; imageX++) {
|
||||
RGBA[][] pixels = new RGBA[h][w];
|
||||
for(int pixelY=0; pixelY<h; pixelY++) {
|
||||
for(int pixelX=0; pixelX<w; pixelX++) {
|
||||
int bY = imageY*h + pixelY;
|
||||
int bX = imageX*4*w + 4*pixelX;
|
||||
pixels[pixelY][pixelX] = new RGBA(px[bY][bX ],
|
||||
px[bY][bX + 1],
|
||||
px[bY][bX + 2],
|
||||
px[bY][bX + 3]);
|
||||
}
|
||||
}
|
||||
images[imageY + imageX*dirs] = new NonPalettedImage(w, h, pixels);
|
||||
}
|
||||
}
|
||||
|
||||
float[] delays = null;
|
||||
if(frames != 1) {
|
||||
delays = new float[frames];
|
||||
for(int i=0; i<delays.length; i++) {
|
||||
delays[i] = 1;
|
||||
}
|
||||
}
|
||||
//public IconState(String name, int dirs, int frames, Image[] images, float[] delays, boolean rewind, int loop, String hotspot, boolean movement) {
|
||||
return new IconState(name, dirs, frames, images, delays, false, -1, null, false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -40,6 +40,9 @@ public class Main {
|
||||
"\tdirection specifier can be a single direction, or direction-direction\n" +
|
||||
"\tdirection can be 0-7 or S, N, E, W, SE, SW, NE, NW (non-case-sensitive)\n" +
|
||||
|
||||
"import [file] [state] [in]\n" +
|
||||
"\timport a PNG image from [in] into [file], with the name [state]\n" +
|
||||
"\tinput should be in the same format given by the 'extract' command with no direction or frame arguments\n" +
|
||||
"";
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException, IOException, DMIException {
|
||||
@@ -65,7 +68,7 @@ public class Main {
|
||||
String op = argq.pollFirst();
|
||||
|
||||
switch(op) {
|
||||
case "diff":
|
||||
case "diff": {
|
||||
if(argq.size() < 2) {
|
||||
System.out.println("Insufficient arguments for command!");
|
||||
System.out.println(helpStr);
|
||||
@@ -85,7 +88,8 @@ public class Main {
|
||||
DMIDiff dmid = new DMIDiff(dmi, dmi2);
|
||||
System.out.println(dmid);
|
||||
break;
|
||||
case "sort":
|
||||
}
|
||||
case "sort": {
|
||||
if(argq.size() < 1) {
|
||||
System.out.println("Insufficient arguments for command!");
|
||||
System.out.println(helpStr);
|
||||
@@ -94,13 +98,14 @@ public class Main {
|
||||
String f = argq.pollFirst();
|
||||
|
||||
if(VERBOSITY >= 0) System.out.println("Loading " + f);
|
||||
dmi = doDMILoad(f);
|
||||
DMI dmi = doDMILoad(f);
|
||||
if(VERBOSITY >= 0) dmi.printInfo();
|
||||
|
||||
if(VERBOSITY >= 0) System.out.println("Saving " + f);
|
||||
dmi.writeDMI(new FileOutputStream(f), true);
|
||||
break;
|
||||
case "merge":
|
||||
}
|
||||
case "merge": {
|
||||
if(argq.size() < 4) {
|
||||
System.out.println("Insufficient arguments for command!");
|
||||
System.out.println(helpStr);
|
||||
@@ -145,7 +150,8 @@ public class Main {
|
||||
System.exit(0);
|
||||
}
|
||||
break;
|
||||
case "extract":
|
||||
}
|
||||
case "extract": {
|
||||
if(argq.size() < 3) {
|
||||
System.out.println("Insufficient arguments for command!");
|
||||
System.out.println(helpStr);
|
||||
@@ -155,7 +161,7 @@ public class Main {
|
||||
state = argq.pollFirst(),
|
||||
outFile = argq.pollFirst();
|
||||
|
||||
dmi = doDMILoad(file);
|
||||
DMI dmi = doDMILoad(file);
|
||||
if(VERBOSITY >= 0) dmi.printInfo();
|
||||
|
||||
IconState is = dmi.getIconState(state);
|
||||
@@ -231,7 +237,44 @@ public class Main {
|
||||
}
|
||||
is.dumpToPNG(new FileOutputStream(outFile), mDir, Mdir, mFrame, Mframe);
|
||||
break;
|
||||
case "verify":
|
||||
}
|
||||
case "import": {
|
||||
if(argq.size() < 3) {
|
||||
System.out.println("Insufficient arguments for command!");
|
||||
System.out.println(helpStr);
|
||||
return;
|
||||
}
|
||||
String dmiFile = argq.pollFirst(),
|
||||
stateName = argq.pollFirst(),
|
||||
pngFile = argq.pollFirst();
|
||||
|
||||
boolean noDup = false;
|
||||
if(!argq.isEmpty()) {
|
||||
switch(argq.pollFirst().toLowerCase()) {
|
||||
case "nodup":
|
||||
noDup = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(VERBOSITY >= 0) System.out.println("Loading " + dmiFile);
|
||||
DMI toImportTo = doDMILoad(dmiFile);
|
||||
if(VERBOSITY >= 0) toImportTo.printInfo();
|
||||
//public static IconState importFromPNG(DMI dmi, InputStream inS, String name) throws DMIException {
|
||||
IconState is = IconState.importFromPNG(toImportTo, new FileInputStream(pngFile), stateName);
|
||||
|
||||
|
||||
if(!(noDup && toImportTo.setIconState(is))) {
|
||||
toImportTo.addIconState(null, is);
|
||||
}
|
||||
|
||||
if(VERBOSITY >= 0) toImportTo.printInfo();
|
||||
|
||||
if(VERBOSITY >= 0) System.out.println("Saving " + dmiFile);
|
||||
toImportTo.writeDMI(new FileOutputStream(dmiFile));
|
||||
break;
|
||||
}
|
||||
case "verify": {
|
||||
if(argq.size() < 1) {
|
||||
System.out.println("Insufficient arguments for command!");
|
||||
System.out.println(helpStr);
|
||||
@@ -242,6 +285,7 @@ public class Main {
|
||||
DMI v = doDMILoad(vF);
|
||||
if(VERBOSITY >= 0) v.printInfo();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
System.out.println("Command '" + op + "' not found!");
|
||||
case "help":
|
||||
|
||||
Reference in New Issue
Block a user