forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage2ascii.java
More file actions
202 lines (183 loc) · 7.15 KB
/
Image2ascii.java
File metadata and controls
202 lines (183 loc) · 7.15 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package ascii;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import net.sf.image4j.codec.ico.ICODecoder;
import org.osgl.$;
import org.osgl.util.E;
import org.osgl.util.IO;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
// Adapted from https://github.com/netwinder-dev/ASCIIConversion/blob/master/src/ASCIIConvert/ASCIIConversions.java
public class Image2ascii {
private static final int MAX_WIDTH = 80;
private int[] statsArray = new int[12];
char[] convRefArray = {' ', '~', '-', ':', '+', '%', '=', 'W', '@', '$', '#', '▆'};
char[] imgArray;
private long dStart;
private long dEnd;
/**
* The main conversion method.
* @param image A BufferedImage containing the image that needs to be converted
* @param favicon When ico is true it will convert ico file into 16 x 16 size
* @return A string containing the ASCII version of the original image.
*/
public String convert(BufferedImage image, boolean favicon) {
// Reset statistics before anything
statsArray = new int[12];
// Begin the timer
dStart = System.nanoTime();
// Scale the image
image = scale(image, favicon);
// The +1 is for the newline characters
StringBuilder sb = new StringBuilder((image.getWidth() + 1) * image.getHeight());
for (int y = 0; y < image.getHeight(); y++) {
// At the end of each line, add a newline character
if (sb.length() != 0) sb.append("\n");
for (int x = 0; x < image.getWidth(); x++) {
//
Color pixelColor = new Color(image.getRGB(x, y), true);
int alpha = pixelColor.getAlpha();
boolean isTransient = alpha < 0.1;
double gValue = isTransient ? 250 : ((double) pixelColor.getRed() * 0.2989 + (double) pixelColor.getBlue() * 0.5870 + (double) pixelColor.getGreen() * 0.1140) / ((double)alpha / (double)250);
final char s = gValue < 130 ? darkGrayScaleMap(gValue) : lightGrayScaleMap(gValue);
sb.append(s);
}
}
imgArray = sb.toString().toCharArray();
dEnd = System.nanoTime();
return sb.toString();
}
/**
* Image scale method
* @param imageToScale The image to be scaled
* @param dWidth Desired width, the new image object is created to this size
* @param dHeight Desired height, the new image object is created to this size
* @param fWidth What to multiply the width by. value < 1 scales down, and value > one scales up
* @param fHeight What to multiply the height by. value < 1 scales down, and value > one scales up
* @return A scaled image
*/
private static BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight, double fWidth, double fHeight) {
BufferedImage dbi = null;
// Needed to create a new BufferedImage object
int imageType = imageToScale.getType();
if (imageToScale != null) {
dbi = new BufferedImage(dWidth, dHeight, imageType);
Graphics2D g = dbi.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance(fWidth, fHeight);
g.drawRenderedImage(imageToScale, at);
}
return dbi;
}
private char darkGrayScaleMap(double g) {
char str;
if (g >= 120.0) {
str = '=';
} else if (g >= 100.0) {
str = 'W';
} else if (g >= 80.0) {
str = '@';
} else if (g >= 70.0) {
str = '$';
} else if (g >= 30.0) {
str = '#';
} else if (g >= 12) {
str = '▓';
} else {
str = '¶';
}
return str;
}
private char lightGrayScaleMap(double g) {
char str;
// Light
if (g >= 240.0) {
str = ' ';
} else if (g >= 220) {
str = '~';
} else if (g >= 200.0) {
str = '-';
} else if (g >= 180.0) {
str = ':';
} else if (g >= 160.0) {
str = '+';
} else {
str = '%';
}
return str;
}
private static BufferedImage scale(BufferedImage original, boolean favicon) {
$.T3<Integer, Integer, Double> params = calcScaleParams(original, favicon);
return scale(original, params._1, params._2 / 2, params._3, params._3 / 2);
}
private static $.T3<Integer, Integer, Double> calcScaleParams(BufferedImage original, boolean favicon) {
int width = original.getWidth(), height = original.getHeight();
int max = favicon ? 16 : MAX_WIDTH;
if (width <= max) {
return $.T3(width, height, 1.0d);
}
double factor = ((double) max / (double) width);
return $.T3(max, (int)(height * factor), factor);
}
public static String render(File imageSource, boolean favicon) {
try {
URL url = imageSource.toURI().toURL();
boolean isIcon = imageSource.getName().endsWith(".ico");
return render(url, favicon, isIcon);
} catch (MalformedURLException e) {
// this is never gonna happen
throw E.unexpected(e);
}
}
public static String render(URL imageSource, boolean iconFile) {
return render(imageSource, false, iconFile);
}
public static String render(URL imageSource, boolean favicon, boolean iconFile) {
try {
BufferedImage image = read(imageSource, iconFile);
return new Image2ascii().convert(image, favicon);
} catch (Exception e) {
return "";
}
}
private static BufferedImage read(URL imageSource, boolean isIcon) throws Exception {
if (isIcon) {
java.util.List<BufferedImage> images = ICODecoder.read(imageSource.openStream());
return images.get(0);
} else {
return ImageIO.read(imageSource);
}
}
public static void main(String[] args) throws Exception {
Image2ascii convert = new Image2ascii();
java.util.List<BufferedImage> images = ICODecoder.read(new File("/home/luog/favicon.ico"));
String s = convert.convert(images.get(0), true);
if (images.size() > 0) {
for (int i = 0; i < images.size(); ++i) {
ImageIO.write(images.get(i), "png", new File("/home/luog/f" + i + ".png"));
}
}
IO.writeContent(s, new File("/home/luog/a.txt"));
}
}