-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathImageConvert.java
More file actions
executable file
·146 lines (143 loc) · 5.57 KB
/
Copy pathImageConvert.java
File metadata and controls
executable file
·146 lines (143 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package javaforce.utils;
import javaforce.*;
import javaforce.awt.*;
/** ImageConvert utility. */
public class ImageConvert {
public static boolean debug = false;
public static boolean quiet = false;
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage : ImageConvert filein fileout [index=#] [scale=width,height] [size=width,height] [fill=aarrggbb] [quiet=true]");
System.out.println("Suppports : jpg, png, bmp, svg, ico, icns(output only)");
System.out.println(" index : ico index (input only) (default = 0)");
System.out.println(" scale : scale image by %");
System.out.println(" size : set new image size (default svg size = 256,256)");
System.out.println(" fill : background color (default = ff000000 [opaque black])");
return;
}
int index = 0;
int width = -1;
int height = -1;
float width_percent = 100.0f;
float height_percent = 100.0f;
for(int a=2;a<args.length;a++) {
String arg = args[a];
int idx = arg.indexOf('=');
if (idx == -1) {
System.out.println("Unknown option:" + arg);
continue;
}
String key = arg.substring(0, idx);
String value = arg.substring(idx + 1);
switch (key) {
case "index": {
index = JF.atoi(value);
break;
}
case "scale": {
String[] fs = value.split("[,]");
if (fs.length < 2) {
System.out.println("Invalid scale:" + value);
continue;
}
String width_str = fs[0];
String height_str = fs[1];
width_percent = JF.atof(width_str);
height_percent = JF.atof(height_str);
break;
}
case "size": {
String[] fs = value.split("[,]");
if (fs.length < 2) {
System.out.println("Invalid scale:" + value);
continue;
}
String width_str = fs[0];
String height_str = fs[1];
width = JF.atoi(width_str);
height = JF.atoi(height_str);
break;
}
case "fill": {
int fill = Integer.parseUnsignedInt(value, 16);
JFImage.setDefaultColor(fill);
break;
}
case "quiet": {
quiet = value.equals("true");
break;
}
default: System.out.println("Unknown option:" + key);
}
}
try {
if (!quiet) JFLog.log("ImageConvert:" + args[0] + " to " + args[1]);
JFImage img = new JFImage();
String infmt = args[0].substring(args[0].lastIndexOf(".")+1).toLowerCase();
if (debug) JFLog.log("infmt=" + infmt);
if (infmt.equals("jpg")) {
if (!img.loadJPG(args[0])) throw new Exception("Load failed");
} else if (infmt.equals("png")) {
if (!img.loadPNG(args[0])) throw new Exception("Load failed");
} else if (infmt.equals("ico")) {
if (!img.loadBMP(args[0], index)) throw new Exception("Load failed");
} else if (infmt.equals("bmp")) {
if (!img.loadBMP(args[0], 0)) throw new Exception("Load failed");
} else if (infmt.equals("svg")) {
if (width == -1 || height == -1) {
width = 256;
height = 256;
}
if (!img.loadSVG(args[0], width, height)) throw new Exception("Load failed");
width = -1;
height = -1;
width_percent = 100.0f;
height_percent = 100.0f;
} else {
throw new Exception("Unsupported input type:" + infmt);
}
if (width != -1 || height != -1) {
int org_width = img.getWidth();
int org_height = img.getHeight();
int new_width = width;
int new_height = height;
if (debug) JFLog.log(String.format("Scaling:from=%dx%d,to=%dx%d", org_width, org_height, new_width, new_height));
JFImage new_img = new JFImage(new_width, new_height);
new_img.fill(0, 0, new_width, new_height, 0, true);
new_img.putJFImageScaleSmooth(img, 0, 0, new_width, new_height);
img = new_img;
}
if (width_percent != 100 || height_percent != 100) {
int org_width = img.getWidth();
int org_height = img.getHeight();
int new_width = (int)((org_width * width_percent) / 100.0f);
int new_height = (int)((org_height * height_percent) / 100.0f);
if (debug) JFLog.log(String.format("Scaling:from=%dx%d,to=%dx%d", org_width, org_height, new_width, new_height));
JFImage new_img = new JFImage(new_width, new_height);
new_img.fill(0, 0, new_width, new_height, 0, true);
new_img.putJFImageScaleSmooth(img, 0, 0, new_width, new_height);
img = new_img;
}
String outfmt = args[1].substring(args[1].lastIndexOf(".")+1).toLowerCase();
if (debug) JFLog.log("outfmt=" + outfmt);
if (outfmt.equals("jpg")) {
if (!img.saveJPG(args[1])) throw new Exception("Save failed");
} else if (outfmt.equals("png")) {
if (!img.savePNG(args[1])) throw new Exception("Save failed");
} else if (outfmt.equals("bmp")) {
if (!img.saveBMP(args[1])) throw new Exception("Save failed");
} else if (outfmt.equals("svg")) {
if (!img.saveSVG(args[1])) throw new Exception("Save failed");
} else if (outfmt.equals("ico")) {
if (!img.saveICO(args[1])) throw new Exception("Save failed");
} else if (outfmt.equals("icns")) {
if (!img.saveICNS(args[1])) throw new Exception("Save failed");
} else {
throw new Exception("Unsupported output type:" + outfmt);
}
if (debug) JFLog.log("Image Converted");
} catch (Exception e) {
JFLog.log(e);
}
}
}