1 package be
.nikiroo
.utils
.main
;
3 import java
.awt
.Dimension
;
4 import java
.awt
.image
.BufferedImage
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
12 import be
.nikiroo
.utils
.IOUtils
;
13 import be
.nikiroo
.utils
.Image
;
14 import be
.nikiroo
.utils
.ui
.ImageTextAwt
;
15 import be
.nikiroo
.utils
.ui
.ImageTextAwt
.Mode
;
16 import be
.nikiroo
.utils
.ui
.ImageUtilsAwt
;
19 * Image to ASCII conversion.
25 * Syntax: (--mode=MODE) (--width=WIDTH) (--height=HEIGHT) (--size=SIZE)
26 * (--output=OUTPUT) (--invert) (--help)
32 public static void main(String
[] args
) {
33 Dimension size
= null;
35 boolean invert
= false;
36 List
<String
> inputs
= new ArrayList
<String
>();
44 for (String arg
: args
) {
47 if (arg
.startsWith("--mode=")) {
48 mode
= Mode
.valueOf(arg
.substring("--mode=".length()));
49 } else if (arg
.startsWith("--width=")) {
51 .parseInt(arg
.substring("--width=".length()));
52 } else if (arg
.startsWith("--height=")) {
53 height
= Integer
.parseInt(arg
.substring("--height="
55 } else if (arg
.startsWith("--size=")) {
56 String content
= arg
.substring("--size=".length()).replace(
58 width
= Integer
.parseInt(content
.split("x")[0]);
59 height
= Integer
.parseInt(content
.split("x")[1]);
60 } else if (arg
.startsWith("--ouput=")) {
61 if (!arg
.equals("--output=-")) {
62 output
= new File(arg
.substring("--output=".length()));
64 } else if (arg
.equals("--invert")) {
66 } else if (arg
.equals("--help")) {
68 .println("Syntax: (--mode=MODE) (--width=WIDTH) (--height=HEIGHT) (--size=SIZE) (--output=OUTPUT) (--invert) (--help)");
69 System
.out
.println("\t --help: will show this screen");
71 .println("\t --invert: will invert the 'colours'");
73 .println("\t --mode: will select the rendering mode (default: ASCII):");
75 .println("\t\t ASCII: ASCI output mode, that is, characters \" .-+=o8#\"");
77 .println("\t\t DITHERING: Use 5 different \"colours\" which are actually"
78 + "\n\t\t Unicode characters \" ░▒▓█\"");
80 .println("\t\t DOUBLE_RESOLUTION: Use \"block\" Unicode characters up to quarter"
81 + "\n\t\t blocks, thus in effect doubling the resolution both in vertical"
82 + "\n\t\t and horizontal space."
83 + "\n\t\t Note that since 2 characters next to each other are square,"
84 + "\n\t\t 4 blocks per 2 blocks for w/h resolution.");
86 .println("\t\t DOUBLE_DITHERING: Use characters from both DOUBLE_RESOLUTION"
87 + "\n\t\t and DITHERING");
94 size
= new Dimension(width
, height
);
95 if (inputs
.size() == 0) {
96 inputs
.add("-"); // by default, stdin
98 } catch (Exception e
) {
99 System
.err
.println("Syntax error: \"" + lastArg
+ "\" is invalid");
108 for (String input
: inputs
) {
109 InputStream in
= null;
112 if (input
.equals("-")) {
115 in
= new FileInputStream(input
);
117 BufferedImage image
= ImageUtilsAwt
118 .fromImage(new Image(in
));
119 ImageTextAwt img
= new ImageTextAwt(image
, size
, mode
,
121 if (output
== null) {
122 System
.out
.println(img
.getText());
124 IOUtils
.writeSmallFile(output
, img
.getText());
127 if (!input
.equals("-")) {
132 } catch (IOException e
) {