Skip to content

Commit 6d1ad71

Browse files
committed
StitchTool v3.3.6
- Fixed a bug where smart stitching and waifuing would give errors - Fixed a bug where config did not store location of Waifu2X - Minor fixes
1 parent 44c0219 commit 6d1ad71

File tree

2 files changed

+99
-86
lines changed

2 files changed

+99
-86
lines changed

Diff for: src/Controller.java

+98-85
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
116116

117117
// Final values
118118
final String[] files = new String[]{"PNG", "JPG"};
119-
final String[] waifus = new String[]{"CAFFE", "VULKAN"};
120119
caffeModels = new String[]{"cunet", "upresnet10", "upconv_7_anime_style_art_rgb", "upconv_7_photo", "anime_style_art_rgb", "photo", "anime_style_art_y"};
121120
vulkanModels = new String[]{"models-cunet", "models-upconv_7_anime_style_art_rgb", "models-upconv_7_photo"};
122121
final String[] actions = new String[]{"STITCHSPLIT", "STITCH", "SPLIT", "SMARTSPLIT"};
@@ -130,22 +129,22 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
130129
File f4 = new File(watermarkPath);
131130

132131
// Checks to make sure the config is not null or has any invalid values
133-
if (inputPath == null || f1 == null || !f1.isDirectory()) {
132+
if (inputPath == null || !f1.isDirectory()) {
134133
inputPath = System.getProperty("user.home") + File.separator;
135134
setConfig("inputPath", inputPath);
136135
}
137-
if (outputPath == null || f2 == null || !f2.isDirectory()) {
136+
if (outputPath == null || !f2.isDirectory()) {
138137
outputPath = System.getProperty("user.home") + File.separator;
139138
setConfig("outputPath", outputPath);
140139
}
141140
if (getConfig("waifuPath") != null) {
142141
waifuField.setText(waifuPath);
143142
}
144-
if (imagePath == null || f3 == null || !f3.isDirectory()) {
143+
if (imagePath == null || !f3.isDirectory()) {
145144
imagePath = System.getProperty("user.home") + File.separator;
146145
setConfig("imagePath", imagePath);
147146
}
148-
if (watermarkPath == null || f4 == null || !f4.isDirectory()) {
147+
if (watermarkPath == null || !f4.isDirectory()) {
149148
watermarkPath = System.getProperty("user.home") + File.separator;
150149
setConfig("watermarkPath", watermarkPath);
151150
}
@@ -165,7 +164,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
165164
fileOption = "PNG";
166165
setConfig("fileOption", "PNG");
167166
}
168-
if (waifuPath == null || !Arrays.asList(waifus).contains(waifuPath)) {
167+
if (waifuPath == null) {
169168
waifuPath = "NOT FOUND";
170169
setConfig("waifuPath", "NOT FOUND");
171170
waifuField.setText("");
@@ -306,6 +305,10 @@ else if (files.size() == 1 && (stitchSplit.isSelected() || stitch.isSelected()))
306305
inputImagesBTN.setPrefWidth(100.0);
307306
inputImagesBTN.setTranslateX(0);
308307
inputImagesBTN.setTranslateY(0);
308+
309+
if (stitchSplit.isSelected()) {
310+
stitchSplitOptions.setDisable(false);
311+
}
309312
}
310313

311314
// No image has been selected yet
@@ -737,15 +740,15 @@ public void onWatermark() throws IOException {
737740
if (greyOption.isSelected()) {
738741
ImageFilter imageFilter = new GrayFilter(true, 5);
739742
ImageProducer producer = new FilteredImageSource(watermark.getSource(), imageFilter);
740-
watermark = toBufferedImage(Toolkit.getDefaultToolkit().createImage(producer));
743+
watermark = (BufferedImage) Toolkit.getDefaultToolkit().createImage(producer);
741744
}
742745

743746
// Rescales the image
744747
double ratio = (double) watermark.getWidth() / watermark.getHeight();
745748
Image tempImage = watermark.getScaledInstance(image.getWidth() / 5,
746749
(int)(image.getWidth() / 5 * ratio), Image.SCALE_DEFAULT);
747750

748-
watermark = toBufferedImage(tempImage);
751+
watermark = (BufferedImage) tempImage;
749752

750753
// Asks how many times the watermark will show up
751754
TextInputDialog input = new TextInputDialog();
@@ -826,9 +829,9 @@ else if (option.equalsIgnoreCase(options[2]) || option.equalsIgnoreCase(options[
826829
splitType = 2;
827830
}
828831

829-
int num = askSplitImages();
830832
// Split horizontally or vertically
831833
if (splitType == 1 || splitType == 2) {
834+
int num = askSplitImages();
832835
splitHelper(image, num, splitType == 1);
833836

834837
// Finished message
@@ -842,9 +845,18 @@ else if (option.equalsIgnoreCase(options[2]) || option.equalsIgnoreCase(options[
842845

843846
// Smart split
844847
if (splitType == 0) {
845-
doSmartSplit(image);
848+
smartSplitHelper(image);
849+
Alert a = new Alert(Alert.AlertType.INFORMATION);
850+
a.setHeaderText("Stitching and Splitting Complete!");
851+
if (denoiseBox.isSelected() && scaleBox.isSelected()) {
852+
a.setContentText("Images have been stitched and split and been denoised with level " + (int) denoiseSlider.getValue() + " and scaled by " + (int) Math.pow(2, scaleSlider.getValue()) + "!");
853+
} else if (denoiseBox.isSelected() && !scaleBox.isSelected()) {
854+
a.setContentText("Images have been stitched and split and denoised with level " + (int) denoiseSlider.getValue());
855+
} else if (!denoiseBox.isSelected() && scaleBox.isSelected()) {
856+
a.setContentText("Images have been stitched and split and been scaled by " + scaleSlider.getValue() + "!");
857+
}
858+
a.showAndWait();
846859
}
847-
848860
}
849861

850862
// Stitches an ArrayList of images vertically/horizontally
@@ -862,7 +874,7 @@ public void doStitch(ArrayList<BufferedImage> images) throws IOException {
862874
}
863875

864876
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
865-
a.setHeaderText(null);
877+
a.setHeaderText("Stitching Complete!");
866878
a.setContentText(files.size() + " images were successfully stitched!");
867879
a.showAndWait();
868880
}
@@ -898,62 +910,22 @@ public void doImageSplit(BufferedImage image) throws IOException {
898910

899911
// Finished message
900912
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
901-
a.setHeaderText(null);
913+
a.setHeaderText("Splitting Complete!");
902914
a.setContentText("Successfully split into " + num + " images!");
903915
a.showAndWait();
904916
}
905917

906918
// Splits a BufferedImage by space (row of same colored pixels)
907-
public void doSmartSplit(BufferedImage source) throws IOException {
908-
int num = askSplitImages();
909-
910-
// Approximate size of each image
911-
int startHeight = source.getHeight() / num;
912-
ArrayList<Integer> splitHeights = new ArrayList<>();
913-
splitHeights.add(0);
914-
915-
for (int i = startHeight; i < source.getHeight(); i++ ) {
916-
for (int j = 0; j < source.getWidth() - 1; j++) {
917-
Color c1 = getColor(source.getRGB(j, i));
918-
Color c2 = getColor(source.getRGB(j + 1, i));
919-
920-
// Checks if the row of pixels is the same color, if not, moves 1 pixel down
921-
if (!(c1.getAlpha() == c2.getAlpha() && c1.getRed() == c2.getRed() && c1.getGreen() == c2.getGreen() && c1.getBlue() == c2.getBlue())) {
922-
break;
923-
}
924-
// Otherwise, it saves the height to split, and moves to the next part
925-
if (j+1 == source.getWidth() - 1) {
926-
splitHeights.add(i);
927-
i += startHeight;
928-
break;
929-
}
930-
}
931-
}
932-
933-
// Adds the remainder of the image to the array
934-
if (splitHeights.get(splitHeights.size() - 1) != source.getHeight()) {
935-
splitHeights.add(source.getHeight());
936-
}
937-
938-
new File(outputPath + File.separator + "StitchTool" + File.separator).mkdirs();
939-
940-
// Exports the images into a folder
941-
for (int i = 0; i < splitHeights.size() - 1; i++) {
942-
File f = new File(outputPath + File.separator + "StitchTool" + File.separator + nameField.getText() + (i+1) + "." + fileOption);
943-
ImageIO.write(source.getSubimage(0, splitHeights.get(i), source.getWidth(), splitHeights.get(i+1) - splitHeights.get(i)), fileOption, f);
944-
if (waifuHelper(f)) {
945-
f.delete();
946-
}
947-
}
919+
public void doSmartSplit(BufferedImage image) throws IOException {
920+
int numImages = smartSplitHelper(image);
948921

949922
// Finished message
950923
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
951-
a.setHeaderText(null);
952-
int numImages = splitHeights.size() - 1;
924+
a.setHeaderText("Smart Split");
953925
if (numImages == 1) {
954926
a.setContentText("Unable to smart split!");
955927
} else {
956-
a.setContentText(splitHeights.size() - 1 + " images were successfully created!");
928+
a.setContentText(numImages + " images were successfully created!");
957929
}
958930
a.showAndWait();
959931
}
@@ -1105,6 +1077,62 @@ public void splitHelper(BufferedImage image, int num, boolean hor) {
11051077
}
11061078
}
11071079

1080+
// Smart split and waifus as necessary
1081+
public int smartSplitHelper(BufferedImage image) {
1082+
int c = 0;
1083+
int num = askSplitImages();
1084+
1085+
// Approximate size of each image
1086+
int startHeight = image.getHeight() / num;
1087+
ArrayList<Integer> splitHeights = new ArrayList<>();
1088+
splitHeights.add(0);
1089+
1090+
for (int i = startHeight; i < image.getHeight(); i++ ) {
1091+
for (int j = 0; j < image.getWidth() - 1; j++) {
1092+
Color c1 = getColor(image.getRGB(j, i));
1093+
Color c2 = getColor(image.getRGB(j + 1, i));
1094+
1095+
// Checks if the row of pixels is the same color, if not, moves 1 pixel down
1096+
if (!(c1.getAlpha() == c2.getAlpha() && c1.getRed() == c2.getRed() && c1.getGreen() == c2.getGreen() && c1.getBlue() == c2.getBlue())) {
1097+
break;
1098+
}
1099+
// Otherwise, it saves the height to split, and moves to the next part
1100+
if (j+1 == image.getWidth() - 1) {
1101+
splitHeights.add(i);
1102+
i += startHeight;
1103+
c++;
1104+
break;
1105+
}
1106+
}
1107+
}
1108+
1109+
// Adds the remainder of the image to the array
1110+
if (splitHeights.get(splitHeights.size() - 1) != image.getHeight()) {
1111+
splitHeights.add(image.getHeight());
1112+
}
1113+
1114+
outputPath = outputPath + File.separator + "StitchTool" + File.separator;
1115+
new File(outputPath).mkdirs();
1116+
outputField.setText(outputPath);
1117+
1118+
// Exports the images into a folder
1119+
try {
1120+
for (int i = 0; i < splitHeights.size() - 1; i++) {
1121+
nameField.setText(nameField.getText() + "_" + (i + 1));
1122+
File f = new File(outputPath + nameField.getText() + "." + fileOption);
1123+
1124+
ImageIO.write(image.getSubimage(0, splitHeights.get(i), image.getWidth(), splitHeights.get(i + 1) - splitHeights.get(i)), fileOption, f);
1125+
if (waifuHelper(f)) {
1126+
f.delete();
1127+
}
1128+
nameField.setText(nameField.getText().substring(0, nameField.getLength() - 2));
1129+
}
1130+
} catch (IOException ex) {
1131+
ex.printStackTrace();
1132+
}
1133+
return c;
1134+
}
1135+
11081136
// Waifu2X the image
11091137
public boolean waifuHelper(File f) {
11101138
// If the waifu2x.exe is not found
@@ -1157,7 +1185,7 @@ public boolean waifuHelper(File f) {
11571185
cmd.add("-i");
11581186
cmd.add("\"" + f.getAbsolutePath() + "\"");
11591187
cmd.add("-o");
1160-
cmd.add("\"" + outputField.getText() + nameField.getText() + "." + fileOption + "\"");
1188+
cmd.add("\"" + outputField.getText() + nameField.getText() + "_waifu." + fileOption + "\"");
11611189

11621190
if (waifuPath.contains("CAFFE")) {
11631191
// NOISE AND SCALE
@@ -1224,16 +1252,18 @@ else if (waifuPath.contains("VULKAN")) {
12241252
a.showAndWait();
12251253
}
12261254

1227-
Alert a = new Alert(Alert.AlertType.INFORMATION);
1228-
a.setHeaderText(null);
1229-
if (denoiseBox.isSelected() && scaleBox.isSelected()) {
1230-
a.setContentText("Image has been denoised with level " + (int)denoiseSlider.getValue() + " and scaled by " + (int)Math.pow(2, sf) + "!");
1231-
} else if (denoiseBox.isSelected() && !scaleBox.isSelected()) {
1232-
a.setContentText("Image has been denoised with level " + (int)denoiseSlider.getValue());
1233-
} else if (!denoiseBox.isSelected() && scaleBox.isSelected()) {
1234-
a.setContentText("Image has been scaled by " + sf + "!");
1255+
if (!stitchSplit.isSelected() && !smartSplit.isSelected()) {
1256+
Alert a = new Alert(Alert.AlertType.INFORMATION);
1257+
a.setHeaderText(null);
1258+
if (denoiseBox.isSelected() && scaleBox.isSelected()) {
1259+
a.setContentText("Image has been denoised with level " + (int) denoiseSlider.getValue() + " and scaled by " + (int) Math.pow(2, sf) + "!");
1260+
} else if (denoiseBox.isSelected() && !scaleBox.isSelected()) {
1261+
a.setContentText("Image has been denoised with level " + (int) denoiseSlider.getValue());
1262+
} else if (!denoiseBox.isSelected() && scaleBox.isSelected()) {
1263+
a.setContentText("Image has been scaled by " + sf + "!");
1264+
}
1265+
a.showAndWait();
12351266
}
1236-
a.showAndWait();
12371267
return true;
12381268
}
12391269

@@ -1280,23 +1310,6 @@ public static final class FileNameComparator implements Comparator<String> {
12801310
}
12811311
}
12821312

1283-
// Converts Image to BufferedImage
1284-
public BufferedImage toBufferedImage(Image img) {
1285-
if (img instanceof BufferedImage) {
1286-
return (BufferedImage) img;
1287-
}
1288-
1289-
// Create a buffered image with transparency
1290-
BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
1291-
1292-
// Draw the image on to the buffered image
1293-
Graphics2D bGr = image.createGraphics();
1294-
bGr.drawImage(img, 0, 0, null);
1295-
bGr.dispose();
1296-
1297-
return image;
1298-
}
1299-
13001313
// Creates a new JFrame that uses the ScrollPanel
13011314
public void previewImage(JFrame f, BufferedImage source, int num) {
13021315
int height = source.getHeight();
@@ -1309,7 +1322,7 @@ public void previewImage(JFrame f, BufferedImage source, int num) {
13091322
previewImage.getGraphics().drawImage(tempImage, 0, 0, null);
13101323

13111324
// Creates a new JFrame window
1312-
JPanel p = new ScrollPanel(toBufferedImage(previewImage), num);
1325+
JPanel p = new ScrollPanel(previewImage, num);
13131326
f.setContentPane(p);
13141327
f.setResizable(false);
13151328
f.setSize(700, 700);

Diff for: src/Stitch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.Properties;
1515

1616
public class Stitch extends Application {
17-
private final String version = "3.3.1";
17+
private final String version = "3.3.6";
1818

1919
@Override
2020
public void start(Stage window) throws Exception{

0 commit comments

Comments
 (0)