-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASCEE.java
217 lines (197 loc) · 10.2 KB
/
ASCEE.java
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.nio.file.Files;
import java.nio.file.Path;
public class ASCEE {
//user exposed variables
private static boolean inversion = false; //inverts the black and white colour space.
private static float compressionfactor = 4f; // higher the number, smaller the ascii art.
private static byte mode = 2; // 1 = high bit depth mode. 2 = low bit depth mode
//lookup constants
private static final char strLOW[] = {'@','%','#','*','+','=','-',':','.',' '};
//private static final char str[] = {'$','@','B','%','8','&','W','M','#','*','o','a','h','b','d','p','q','w','m','Z','O','0','L','C','J','U','Y','X','z','c','v','u','n','x','r','j','f','t','/',(char)92,'(',')','1','{','}','[',']','?','-','+','~','<','>','i','!','l',';',':',',','"','^',(char)39,'.',' '};
private static final char str[] = {'$' ,'$' ,'$' ,'$' ,'@' ,'@' ,'@' ,'@' ,'B' ,'B' ,'B' ,'B' ,'%' ,'%' ,'%' ,'%' ,'8' ,'8' ,'8' ,'8' ,'&' ,'&' ,'&' ,'&' ,'W' ,'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,'Z' ,'Z' ,'Z' ,'m' ,'m' ,'m' ,'m' ,'w' ,'w' ,'w' ,'w' ,'q' ,'q' ,'q' ,'q' ,'p' ,'p' ,'p' ,'p' ,'d' ,'d' ,'d' ,'d' ,'b' ,'b' ,'b' ,'b' ,'h' ,'h' ,'h' ,'h' ,'a' ,'a' ,'a' ,'a' ,'o' ,'o' ,'o' ,'o' ,'*' ,'*' ,'*' ,'*' ,'#' ,'#' ,'#' ,'#' ,'M' ,'M' ,'M' ,'M' ,'W' ,'W' ,'W' ,'W' ,'&' ,'&' ,'&' ,'&' ,'8' ,'8' ,'8' ,'8' ,'%' ,'%' ,'%' ,'%' ,'B' ,'B' ,'B' ,'B' ,'@' ,'@' ,'@' ,'@' ,'$' ,'$' ,'$' ,'$'};
//working variables. used by program
private static BufferedImage image;
private static int width;
private static int height;
private static String ext;
//non exposed variable. meant for developer use
private static int scaledownConstant = 400; //(this also determines the tolerence for small images. Lower the number, lesser will be the tolerence for skipping scaledwon process of small images)
public static void main(String args[]) throws IOException {
initialise();
load();
scale();
decolour();
art();
printsample(); //optional
System.out.println("Ready.");
}
private static void initialise(){
BufferedImage image = null;
width = 0;
height = 0;
ext = "";
}
private static void load()throws IOException{
//load
System.out.println("Loadig Image...");
try {
File input = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "image.jpg");
ext = ".jpg";
if(!(input.exists())){
input = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "image.jpeg");
ext = ".jpeg";
}
if(!(input.exists())){
input = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "image.png");
ext = ".png";
}
if(!(input.exists())){
input = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "image.gif");
ext = ".gif";
}if(!(input.exists())){
System.out.println("No image file with name \"image\" found.\nNOTE: Only JPG/JPEG, PNG and GIF is supported.");
System.exit(1);
}
//File input = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "image.jpg");
//File input = new File(System.getProperty("user.dir") + File.separator + "image.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
}
catch (IOException e) {
System.out.println("Error: " + e);
}
System.out.println("Image loaded.");
}
private static void scale(){
//scaleing
System.out.println("Scaling.");
float scalefactor = 1;
scalefactor = ((Math.max(width,height)/scaledownConstant)*compressionfactor);
if(scalefactor<1)
image = scale(image, width, height);
else{
image = scale(image, (int)(width/scalefactor), (int)(height/scalefactor));
}
System.out.println("Finished Scaling.");
}
private static void decolour(){
//decolouring
System.out.println("Decolouring.");
width = image.getWidth();
height = image.getHeight();
for(int y = 0; y<height; y++){
for(int x = 0; x<width; x++){
int p = image.getRGB(x,y);
int a = (p>>24)&0xff;
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p&0xff;
int avg = (r+g+b)/3;
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
image.setRGB(x, y, p);
}
}
System.out.println("Finished decolouring.");
}
private static void art() throws IOException {
//create text file
File textascii = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "ascii.txt");
System.out.println("Text file created.");
//creating ascii
System.out.println("Creating ASCII Art...");
String s = "";
if(mode==1){
for(int y = 0; y<height; y++){
for(int x = 0; x<width; x++){
int p = image.getRGB(x,y);
int r = (p>>16)&0xff;
s = s+""+decideHIGH(r);
s = s+ " ";
}
s=s+"\n";
}
}
else{
for(int y = 0; y<height; y++){
for(int x = 0; x<width; x++){
int p = image.getRGB(x,y);
int r = (p>>16)&0xff;
s = s+""+decideLOW(r);
s = s+ " ";
}
s=s+"\n";
}
}
System.out.println("Finished creating.");
try{
//write text file
Files.writeString(Path.of(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "ascii.txt"),s);
}catch (IOException e) {
System.out.println("Error: " + e);
}
System.out.println("Finished writing.");
}
private static void printsample(){
//image output sample
try {
File output_file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "imageCOMPLETE" + ext);
ImageIO.write(image, "png", output_file);
System.out.println("Image printed.");
}
catch (IOException e) {
System.out.println("Error: " + e);
}
}
private static char decideHIGH(double g){
if(inversion){
return(strinv[(int)g]);
}
else
return(str[(int)g]);
}
private static char decideLOW(double g){
int b = -1;
int a = 0;
if(inversion){
b = 1;
a = 9;
}
if (g >= 229.5)
return strLOW[(a-9)*b];
else if (g >= 204)
return strLOW[(a-8)*b];
else if (g >= 178.5)
return strLOW[(a-7)*b];
else if (g >= 153)
return strLOW[(a-6)*b];
else if (g >= 127.5)
return strLOW[(a-5)*b];
else if (g >= 102)
return strLOW[(a-4)*b];
else if (g >= 76.5)
return strLOW[(a-3)*b];
else if (g >= 51)
return strLOW[(a-2)*b];
else if (g >= 25.5)
return strLOW[(a-1)*b];
else
return strLOW[(a-0)*b];
}
private static BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) {
BufferedImage scaledImage = null;
if (imageToScale != null) {
scaledImage = new BufferedImage(dWidth, dHeight, imageToScale.getType());
Graphics2D graphics2D = scaledImage.createGraphics();
graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
graphics2D.dispose();
}
return scaledImage;
}
}