Skip to content

Commit e44899f

Browse files
committed
support png type favicon
1 parent 55cf597 commit e44899f

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

src/main/java/act/util/Banner.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public static String cachedBanner() {
6060
public static String banner() {
6161
String bannerText = null;
6262

63-
6463
String udfBanner = udfBanner();
6564
if (null != udfBanner) {
6665
bannerText = S.concat(udfBanner, "\n");
@@ -130,17 +129,29 @@ private static void addFavicon(S.Buffer buffer, String favicon, int maxWidth, in
130129
}
131130

132131
private static String favicon() {
133-
URL url = Banner.class.getResource("/asset/favicon.ico");
132+
boolean isIcon = true;
133+
URL url = Banner.class.getResource("/asset/favicon.png");
134134
if (null == url) {
135-
url = Banner.class.getResource("/asset/img/favicon.ico");
135+
url = Banner.class.getResource("/asset/img/favicon.png");
136+
if (null == url) {
137+
url = Banner.class.getResource("/asset/image/favicon.png");
138+
}
139+
}
140+
if (null != url) {
141+
isIcon = false;
142+
} else {
143+
url = Banner.class.getResource("/asset/favicon.ico");
136144
if (null == url) {
137-
url = Banner.class.getResource("/asset/image/favicon.ico");
145+
url = Banner.class.getResource("/asset/img/favicon.ico");
146+
if (null == url) {
147+
url = Banner.class.getResource("/asset/image/favicon.ico");
148+
}
138149
}
139150
}
140151
if (null == url) {
141152
return "";
142153
}
143-
return Image2ascii.render(url, true);
154+
return Image2ascii.render(url, true, isIcon);
144155
}
145156

146157
private static String asciiArt(String s) {

src/main/java/ascii/Image2ascii.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
import net.sf.image4j.codec.ico.ICODecoder;
2424
import org.osgl.$;
25+
import org.osgl.util.E;
2526
import org.osgl.util.IO;
2627

2728
import javax.imageio.ImageIO;
2829
import java.awt.*;
2930
import java.awt.geom.AffineTransform;
3031
import java.awt.image.BufferedImage;
3132
import java.io.File;
33+
import java.net.MalformedURLException;
3234
import java.net.URL;
3335

3436
// Adapted from https://github.com/netwinder-dev/ASCIIConversion/blob/master/src/ASCIIConvert/ASCIIConversions.java
@@ -153,21 +155,32 @@ private static BufferedImage scale(BufferedImage original, boolean favicon) {
153155
return $.T3(max, (int)(height * factor), factor);
154156
}
155157

156-
public static String render(URL imageSource) {
157-
return render(imageSource, false);
158+
public static String render(File imageSource, boolean favicon) {
159+
try {
160+
URL url = imageSource.toURI().toURL();
161+
boolean isIcon = imageSource.getName().endsWith(".ico");
162+
return render(url, favicon, isIcon);
163+
} catch (MalformedURLException e) {
164+
// this is never gonna happen
165+
throw E.unexpected(e);
166+
}
167+
}
168+
169+
public static String render(URL imageSource, boolean iconFile) {
170+
return render(imageSource, false, iconFile);
158171
}
159172

160-
public static String render(URL imageSource, boolean favicon) {
173+
public static String render(URL imageSource, boolean favicon, boolean iconFile) {
161174
try {
162-
BufferedImage image = read(imageSource, favicon);
175+
BufferedImage image = read(imageSource, iconFile);
163176
return new Image2ascii().convert(image, favicon);
164177
} catch (Exception e) {
165178
return "";
166179
}
167180
}
168181

169-
private static BufferedImage read(URL imageSource, boolean favicon) throws Exception {
170-
if (favicon) {
182+
private static BufferedImage read(URL imageSource, boolean isIcon) throws Exception {
183+
if (isIcon) {
171184
java.util.List<BufferedImage> images = ICODecoder.read(imageSource.openStream());
172185
return images.get(0);
173186
} else {
@@ -179,7 +192,7 @@ public static void main(String[] args) throws Exception {
179192
Image2ascii convert = new Image2ascii();
180193
java.util.List<BufferedImage> images = ICODecoder.read(new File("/home/luog/favicon.ico"));
181194
String s = convert.convert(images.get(0), true);
182-
if (images.size() > 1) {
195+
if (images.size() > 0) {
183196
for (int i = 0; i < images.size(); ++i) {
184197
ImageIO.write(images.get(i), "png", new File("/home/luog/f" + i + ".png"));
185198
}

0 commit comments

Comments
 (0)