Skip to content

Commit

Permalink
Version 1.5.0
Browse files Browse the repository at this point in the history
Added ability to chose which resolutions go into the icns file in the GUI.
  • Loading branch information
EasyG0ing1 committed Mar 6, 2024
1 parent 91a0a6b commit b5244f3
Show file tree
Hide file tree
Showing 24 changed files with 559 additions and 178 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Then, try to process your image again but this time when it exits, it should kic
The program takes your 1024 x 1024 image file and it creates a folder where it then converts your image into the different sizes that are needed for the final `.icns` file. Then it calls `iconutil` to do the conversion. It will use the original name of your file but it will have `.icns` as the extension name and it will drop it into the same folder that your selected image file resides in.

## Updates
* 1.5.0
* Added ability to chose which resolutions go into the icns file in the GUI.
* 1.4.0
* Facelift - along with major functionality enhancements
* 1.3.2
Expand Down
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<properties>
<maven.compiler.release>20</maven.compiler.release>
<versions-maven-plugin>2.16.2</versions-maven-plugin>
<mainClass>com.simtechdata.Main</mainClass>
<mainClass>com.simtechdata.MainGUI</mainClass>
<maven-surefire-plugin>3.2.2</maven-surefire-plugin>
<native-maven-plugin>0.9.28</native-maven-plugin>
<javapackager>1.7.2</javapackager>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>com.simtechdata</groupId>
<artifactId>MacIcns</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.release>20</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>com.simtechdata.Main</mainClass>
<mainClass>com.simtechdata.MainGUI</mainClass>
<javafx.version>21-ea+24</javafx.version>
<javapackager>1.7.2</javapackager>
<maven-jar-plugin>3.3.0</maven-jar-plugin>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/simtechdata/ImageChecker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.simtechdata;

import javafx.scene.image.Image;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -13,7 +15,10 @@ public static boolean isValid(File file) {
int bytesRead = fis.read(header);
if (bytesRead >= 8) {
if (isPNG(header) || isJPEG(header) || isGIF(header) || isTIFF(header) || isBMP(header) || isSVG(file)) {
return true;
Image image = new Image(file.toPath().toUri().toString());
double imgWidth = image.getWidth();
double imgHeight = image.getHeight();
return imgWidth == 1024.0 && imgHeight == 1024.0;
}
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.nio.file.Paths;


public class Main extends Application {
public class MainGUI extends Application {

private static Path mainPath;

Expand All @@ -19,7 +19,7 @@ public static void main(String[] args) {
}
if (mainPath != null) {
if (mainPath.toFile().exists()) {
new ProcessFile(mainPath, false).run();
new ProcessFile(mainPath,null).run();
}
else {
System.out.println("No file exists at this path: " + args[0]);
Expand Down
193 changes: 87 additions & 106 deletions src/main/java/com/simtechdata/ProcessFile.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.simtechdata;

import com.simtechdata.build.Job;
import com.simtechdata.build.Selections;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.glavo.png.javafx.PNGJavaFXUtils;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,16 +16,18 @@

public class ProcessFile {

public ProcessFile(Path selectedImageFile, boolean inWindow) {
public ProcessFile(Path selectedImageFile, Selections selections) {
this.selectedImageFile = selectedImageFile;
this.inWindow = inWindow;
this.selections = selections;
}

private final Path selectedImageFile;
private final boolean inWindow;
private final Selections selections;

public Response run() {
return processFile();
if(selections == null)
return processFileShell();
return processFileGUI();
}

private static String icnsFilePath;
Expand All @@ -35,24 +36,75 @@ public static String getIcnsFilePath() {
return icnsFilePath;
}

private Response processFile() {
private Response processFileGUI() {
Response response = new Response();
if (selectedImageFile != null) {
Map<Integer, Job> jobMap = selections.getJobMap();
if(jobMap == null) {
System.out.println("NULL");
System.exit(0);
}
try {
File file = selectedImageFile.toFile();
Image original = new Image(file.toURI().toString());
double width = original.getWidth();
double height = original.getHeight();
String msg;
if (!ImageChecker.isValid(file)) {
msg = "Specified file is not a valid image type.\nMust be: PNG, JPEG, GIF, TIFF, BMP or SVG";
response.set(false, msg);
return response;
}
if (width != 1024 || height != 1024) {
msg = "Specified image is not 1024 x 1024 in size";
response.set(false, msg);
return response;
}
if(selections.makeParentFolder()) {
for (int index : jobMap.keySet()) {
jobMap.get(index).saveFile();
}
int exitCode = new Run(selections.getIconFolder(), selections.getIcnsFilePath()).run();
if (exitCode != 0) {
response.set(false, "Error occurred while running the icon creation process");
}
else {
FileUtils.deleteDirectory(selections.getIconFolder().toFile());
response.set(true, "");
}
}
else {
msg = "Could not create the icon workspace folder, permission issue?";
response.set(false, msg);
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
return response;
}

private Response processFileShell() {
Response response = new Response();
if (selectedImageFile != null) {
String folder = selectedImageFile.toFile().getParentFile().getAbsolutePath();
Path iconFolder = Paths.get(folder, "icon.iconset");
String iconFilename = FilenameUtils.getBaseName(selectedImageFile.toString()) + ".icns";
Path icnsFile = Paths.get(folder, iconFilename);
icnsFilePath = icnsFile.toAbsolutePath().toString();
Path Image01 = Paths.get(iconFolder.toString(), "[email protected]");
Path Image02 = Paths.get(iconFolder.toString(), "icon_512x512.png");
Path Image03 = Paths.get(iconFolder.toString(), "[email protected]");
Path Image04 = Paths.get(iconFolder.toString(), "icon_256x256.png");
Path Image05 = Paths.get(iconFolder.toString(), "[email protected]");
Path Image06 = Paths.get(iconFolder.toString(), "icon_128x128.png");
Path Image07 = Paths.get(iconFolder.toString(), "[email protected]");
Path Image08 = Paths.get(iconFolder.toString(), "icon_32x32.png");
Path Image09 = Paths.get(iconFolder.toString(), "[email protected]");
Path Image10 = Paths.get(iconFolder.toString(), "icon_16x16.png");
Path Image01 = Paths.get(iconFolder.toString(), "[email protected]"); //01
Path Image02 = Paths.get(iconFolder.toString(), "icon_512x512.png"); //02
Path Image03 = Paths.get(iconFolder.toString(), "[email protected]"); //03
Path Image04 = Paths.get(iconFolder.toString(), "icon_256x256.png"); //04
Path Image05 = Paths.get(iconFolder.toString(), "[email protected]"); //05
Path Image06 = Paths.get(iconFolder.toString(), "icon_128x128.png"); //06
Path Image07 = Paths.get(iconFolder.toString(), "[email protected]"); //07
Path Image08 = Paths.get(iconFolder.toString(), "icon_64x64.png"); //08
Path Image09 = Paths.get(iconFolder.toString(), "[email protected]"); //09
Path Image10 = Paths.get(iconFolder.toString(), "icon_32x32.png"); //10
Path Image11 = Paths.get(iconFolder.toString(), "[email protected]"); //11
Path Image12 = Paths.get(iconFolder.toString(), "icon_16x16.png"); //12

Map<Integer, Job> jobMap = new HashMap<>();

Expand All @@ -64,21 +116,13 @@ private Response processFile() {
String msg;
if (!ImageChecker.isValid(file)) {
msg = "Specified file is not a valid image type.\nMust be: PNG, JPEG, GIF, TIFF, BMP or SVG";
if (!inWindow) {
System.out.println(msg);
System.exit(0);
}
response.set(false, msg);
return response;
System.out.println(msg);
System.exit(0);
}
if (width != 1024 || height != 1024) {
msg = "Specified image is not 1024 x 1024 in size";
if (!inWindow) {
System.out.println(msg);
System.exit(0);
}
response.set(false, msg);
return response;
System.out.println(msg);
System.exit(0);
}
if (FileUtils.createParentDirectories(Image01.toFile()).exists()) {
jobMap.put(1, new Job(original, Image01, 1024));
Expand All @@ -87,10 +131,12 @@ private Response processFile() {
jobMap.put(4, new Job(original, Image04, 256));
jobMap.put(5, new Job(original, Image05, 256));
jobMap.put(6, new Job(original, Image06, 128));
jobMap.put(7, new Job(original, Image07, 64));
jobMap.put(8, new Job(original, Image08, 32));
jobMap.put(9, new Job(original, Image09, 32));
jobMap.put(10, new Job(original, Image10, 16));
jobMap.put(7, new Job(original, Image07, 128));
jobMap.put(8, new Job(original, Image08, 64));
jobMap.put(9, new Job(original, Image09, 64));
jobMap.put(10, new Job(original, Image10, 32));
jobMap.put(11, new Job(original, Image11, 32));
jobMap.put(12, new Job(original, Image12, 16));

for (int index : jobMap.keySet()) {
jobMap.get(index).saveFile();
Expand All @@ -102,88 +148,23 @@ private Response processFile() {
}
else {
FileUtils.forceDeleteOnExit(iconFolder.toFile());
if (!inWindow) {
System.out.println("Icon file created: " + icnsFile);
System.exit(0);
}
System.out.println("Icon file created: " + icnsFile);
System.exit(0);
response.set(true, "");
}
}
else {
msg = "Could not create the icon workspace folder, permission issue?";
response.set(false, msg);
if (!inWindow) {
System.out.println(msg);
System.exit(0);
}
System.out.println(msg);
System.exit(0);
}
} catch (IOException e) {
System.err.println("There was an error. If the following information does not help you figure out the problem, copy and paste the text below the line and create an issue on https://github.com/EasyG0ing1/MacIcns\n---------------------------------------------------------------\nProcessFile.processFile()\n\n");
throw new RuntimeException(e);
}
}
return response;
}

private static class Job {
public Job(Image original, Path path, int size) {
this.original = original;
this.path = path;
this.size = size;
}

public static WritableImage lastImage;
private final Image original;
private final Path path;
private final int size;
private int lastSize = 0;

public void saveFile() {
try {
PNGJavaFXUtils.writeImage(getImage(), path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private WritableImage getImage() {
try {
WritableImage writableImage;
if(lastImage == null) {
writableImage = resizeImage(original, size, size);
lastImage = writableImage;
lastSize = size;
}
else {
if(size == lastSize) {
writableImage = lastImage;
}
else {
writableImage = resizeImage(lastImage, size, size);
lastSize = size;
}
}
return writableImage;
} catch (Exception e) {
catch (IOException e) {
System.err.println("There was an error. If the following information does not help you figure out the problem, copy and paste the text below the line and create an issue on https://github.com/EasyG0ing1/MacIcns\n---------------------------------------------------------------\nProcessFile.processFileShell()\n\n");
throw new RuntimeException(e);
}
}

private WritableImage resizeImage(Image originalImage, int targetWidth, int targetHeight) {
int width = (int) originalImage.getWidth();
int height = (int) originalImage.getHeight();

WritableImage resizedImage = new WritableImage(targetWidth, targetHeight);
for (int x = 0; x < targetWidth; x++) {
for (int y = 0; y < targetHeight; y++) {
double sourceX = x * width / (double) targetWidth;
double sourceY = y * height / (double) targetHeight;
Color color = originalImage.getPixelReader().getColor((int) sourceX, (int) sourceY);
resizedImage.getPixelWriter().setColor(x, y, color);
}
}
return resizedImage;
}
return response;
}

}
Loading

0 comments on commit b5244f3

Please sign in to comment.