2222
2323import net .sf .image4j .codec .ico .ICODecoder ;
2424import org .osgl .$ ;
25+ import org .osgl .util .E ;
2526import org .osgl .util .IO ;
2627
2728import javax .imageio .ImageIO ;
2829import java .awt .*;
2930import java .awt .geom .AffineTransform ;
3031import java .awt .image .BufferedImage ;
3132import java .io .File ;
33+ import java .net .MalformedURLException ;
3234import 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