Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions 1.20.2/src/main/java/com/mojang/slicer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ private static InputFile clip(final String name, final Box box) {
private static InputFile clip(final String inputName, final String outputName, final Box box) {
final String inputPath = nameToPath("minecraft", inputName);
final String outputPath = nameToPath("minecraft", outputName);
final Box imageBox = new Box(0, 0, box.totalW(), box.totalH(), box.totalW(), box.totalH());
return new InputFile(inputPath).outputs(new OutputFile(outputPath, imageBox).apply(image -> {
return new InputFile(inputPath).outputs(new OutputFile(outputPath, box).apply(image -> {
final int imageWidth = image.getWidth();
final int imageHeight = image.getHeight();
final int x = box.scaleX(imageWidth);
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/mojang/slicer/InputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@ParametersAreNonnullByDefault
public class InputFile {
Expand All @@ -30,25 +31,27 @@ public InputFile outputs(final OutputFile... files) {
return this;
}

public void process(final Path inputRoot, final Path outputRoot, @Nullable final Path leftoverRoot) throws IOException {
public void process(final Path inputRoot, final Path outputRoot, final Map<String, BufferedImage> leftovers) throws IOException {
final Path inputPath = inputRoot.resolve(this.path);
if (Files.exists(inputPath)) {
try (final InputStream is = Files.newInputStream(inputPath)) {
final BufferedImage image = ImageIO.read(is);
final BufferedImage leftoverImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics2D leftoverGraphics = leftoverImage.createGraphics();
leftoverGraphics.drawImage(image, 0, 0, null);
BufferedImage leftoverImage = leftovers.get(this.path);
final Graphics2D leftoverGraphics;
if (leftoverImage == null) {
leftoverImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
leftovers.put(this.path, leftoverImage);
leftoverGraphics = leftoverImage.createGraphics();
leftoverGraphics.drawImage(image, 0, 0, null);
} else {
leftoverGraphics = leftoverImage.createGraphics();
}

for (final OutputFile outputFile : outputs) {
outputFile.process(outputRoot, inputPath, image, leftoverGraphics);
}

leftoverGraphics.dispose();

if (leftoverRoot != null) {
final Path leftoverPath = leftoverRoot.resolve(this.path);
Slicer.writeImage(leftoverPath, leftoverImage);
}
}
} else {
System.err.println("Input file " + inputPath.toAbsolutePath() + " not found, skipping!");
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/mojang/slicer/Slicer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;

@ParametersAreNonnullByDefault
public class Slicer {
Expand Down Expand Up @@ -67,8 +69,18 @@ public void process(final Collection<InputFile> inputs) throws IOException {
}

private static void process(final Collection<InputFile> inputs, final Path inputPath, final Path outputPath, @Nullable final Path leftoverPath) throws IOException {
final Map<String, BufferedImage> leftovers = new HashMap();
for (final InputFile input : inputs) {
input.process(inputPath, outputPath, leftoverPath);
input.process(inputPath, outputPath, leftovers);
}

if (leftoverPath != null) {
for (final Map.Entry<String, BufferedImage> entry : leftovers.entrySet()) {
final String path = entry.getKey();
final BufferedImage leftoverImage = entry.getValue();
final Path leftoverImagePath = leftoverPath.resolve(path);
Slicer.writeImage(leftoverImagePath, leftoverImage);
}
}
}
}