Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / be / nikiroo / utils / main / img2aa.java
CommitLineData
ce060f5a
NR
1package be.nikiroo.utils.main;
2
3import java.awt.Dimension;
4import java.awt.image.BufferedImage;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.util.ArrayList;
10import java.util.List;
11
12import be.nikiroo.utils.IOUtils;
13import be.nikiroo.utils.Image;
14import be.nikiroo.utils.ui.ImageTextAwt;
15import be.nikiroo.utils.ui.ImageTextAwt.Mode;
16import be.nikiroo.utils.ui.ImageUtilsAwt;
17
9e50696e
NR
18/**
19 * Image to ASCII conversion.
20 *
21 * @author niki
22 */
23public class img2aa {
ce060f5a 24 /**
9e50696e
NR
25 * Syntax: (--mode=MODE) (--width=WIDTH) (--height=HEIGHT) (--size=SIZE)
26 * (--output=OUTPUT) (--invert) (--help)
ce060f5a 27 * <p>
9e50696e 28 * See "--help".
ce060f5a
NR
29 *
30 * @param args
31 */
32 public static void main(String[] args) {
33 Dimension size = null;
34 Mode mode = null;
35 boolean invert = false;
36 List<String> inputs = new ArrayList<String>();
37 File output = null;
38
39 String lastArg = "";
40 try {
41 int height = -1;
42 int width = -1;
43
44 for (String arg : args) {
45 lastArg = arg;
46
47 if (arg.startsWith("--mode=")) {
48 mode = Mode.valueOf(arg.substring("--mode=".length()));
49 } else if (arg.startsWith("--width=")) {
50 width = Integer
51 .parseInt(arg.substring("--width=".length()));
52 } else if (arg.startsWith("--height=")) {
53 height = Integer.parseInt(arg.substring("--height="
54 .length()));
55 } else if (arg.startsWith("--size=")) {
9e50696e
NR
56 String content = arg.substring("--size=".length()).replace(
57 "X", "x");
ce060f5a
NR
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()));
63 }
64 } else if (arg.equals("--invert")) {
65 invert = true;
66 } else if (arg.equals("--help")) {
67 System.out
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");
70 System.out
71 .println("\t --invert: will invert the 'colours'");
72 System.out
73 .println("\t --mode: will select the rendering mode (default: ASCII):");
74 System.out
75 .println("\t\t ASCII: ASCI output mode, that is, characters \" .-+=o8#\"");
76 System.out
77 .println("\t\t DITHERING: Use 5 different \"colours\" which are actually"
78 + "\n\t\t Unicode characters \" ░▒▓█\"");
79 System.out
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.");
85 System.out
86 .println("\t\t DOUBLE_DITHERING: Use characters from both DOUBLE_RESOLUTION"
87 + "\n\t\t and DITHERING");
88 return;
89 } else {
90 inputs.add(arg);
91 }
92 }
93
94 size = new Dimension(width, height);
95 if (inputs.size() == 0) {
96 inputs.add("-"); // by default, stdin
97 }
98 } catch (Exception e) {
99 System.err.println("Syntax error: \"" + lastArg + "\" is invalid");
100 System.exit(1);
101 }
102
103 try {
104 if (mode == null) {
105 mode = Mode.ASCII;
106 }
107
108 for (String input : inputs) {
109 InputStream in = null;
110
111 try {
112 if (input.equals("-")) {
113 in = System.in;
114 } else {
115 in = new FileInputStream(input);
116 }
117 BufferedImage image = ImageUtilsAwt
118 .fromImage(new Image(in));
119 ImageTextAwt img = new ImageTextAwt(image, size, mode,
120 invert);
121 if (output == null) {
122 System.out.println(img.getText());
123 } else {
124 IOUtils.writeSmallFile(output, img.getText());
125 }
126 } finally {
127 if (!input.equals("-")) {
128 in.close();
129 }
130 }
131 }
132 } catch (IOException e) {
133 e.printStackTrace();
134 System.exit(2);
135 }
136 }
137}