99f753c2d83958a4e7cf0f13a928139815bc1687
[nikiroo-utils.git] / src / be / nikiroo / utils / main / StartImageUtils.java
1 package be.nikiroo.utils.main;
2
3 import java.awt.Dimension;
4 import java.awt.image.BufferedImage;
5 import java.io.File;
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;
11
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;
17
18 public class StartImageUtils {
19 /**
20 * ImageUtils main entry point.
21 * <p>
22 * Syntax: see --help
23 *
24 * @param args
25 */
26 public static void main(String[] args) {
27 Dimension size = null;
28 Mode mode = null;
29 boolean invert = false;
30 List<String> inputs = new ArrayList<String>();
31 File output = null;
32
33 String lastArg = "";
34 try {
35 int height = -1;
36 int width = -1;
37
38 for (String arg : args) {
39 lastArg = arg;
40
41 if (arg.startsWith("--mode=")) {
42 mode = Mode.valueOf(arg.substring("--mode=".length()));
43 } else if (arg.startsWith("--width=")) {
44 width = Integer
45 .parseInt(arg.substring("--width=".length()));
46 } else if (arg.startsWith("--height=")) {
47 height = Integer.parseInt(arg.substring("--height="
48 .length()));
49 } else if (arg.startsWith("--size=")) {
50 String content = arg.substring("--size=".length())
51 .replace("X", "x");
52 width = Integer.parseInt(content.split("x")[0]);
53 height = Integer.parseInt(content.split("x")[1]);
54 } else if (arg.startsWith("--ouput=")) {
55 if (!arg.equals("--output=-")) {
56 output = new File(arg.substring("--output=".length()));
57 }
58 } else if (arg.equals("--invert")) {
59 invert = true;
60 } else if (arg.equals("--help")) {
61 System.out
62 .println("Syntax: (--mode=MODE) (--width=WIDTH) (--height=HEIGHT) (--size=SIZE) (--output=OUTPUT) (--invert) (--help)");
63 System.out.println("\t --help: will show this screen");
64 System.out
65 .println("\t --invert: will invert the 'colours'");
66 System.out
67 .println("\t --mode: will select the rendering mode (default: ASCII):");
68 System.out
69 .println("\t\t ASCII: ASCI output mode, that is, characters \" .-+=o8#\"");
70 System.out
71 .println("\t\t DITHERING: Use 5 different \"colours\" which are actually"
72 + "\n\t\t Unicode characters \" ░▒▓█\"");
73 System.out
74 .println("\t\t DOUBLE_RESOLUTION: Use \"block\" Unicode characters up to quarter"
75 + "\n\t\t blocks, thus in effect doubling the resolution both in vertical"
76 + "\n\t\t and horizontal space."
77 + "\n\t\t Note that since 2 characters next to each other are square,"
78 + "\n\t\t 4 blocks per 2 blocks for w/h resolution.");
79 System.out
80 .println("\t\t DOUBLE_DITHERING: Use characters from both DOUBLE_RESOLUTION"
81 + "\n\t\t and DITHERING");
82 return;
83 } else {
84 inputs.add(arg);
85 }
86 }
87
88 size = new Dimension(width, height);
89 if (inputs.size() == 0) {
90 inputs.add("-"); // by default, stdin
91 }
92 } catch (Exception e) {
93 System.err.println("Syntax error: \"" + lastArg + "\" is invalid");
94 System.exit(1);
95 }
96
97 try {
98 if (mode == null) {
99 mode = Mode.ASCII;
100 }
101
102 for (String input : inputs) {
103 InputStream in = null;
104
105 try {
106 if (input.equals("-")) {
107 in = System.in;
108 } else {
109 in = new FileInputStream(input);
110 }
111 BufferedImage image = ImageUtilsAwt
112 .fromImage(new Image(in));
113 ImageTextAwt img = new ImageTextAwt(image, size, mode,
114 invert);
115 if (output == null) {
116 System.out.println(img.getText());
117 } else {
118 IOUtils.writeSmallFile(output, img.getText());
119 }
120 } finally {
121 if (!input.equals("-")) {
122 in.close();
123 }
124 }
125 }
126 } catch (IOException e) {
127 e.printStackTrace();
128 System.exit(2);
129 }
130 }
131 }