New Utils main classes + ImageUtils fixes
authorNiki Roo <niki@nikiroo.be>
Fri, 6 Apr 2018 14:46:19 +0000 (16:46 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 6 Apr 2018 14:46:19 +0000 (16:46 +0200)
ImageUtils.sh [new file with mode: 0755]
changelog.md
src/be/nikiroo/utils/IOUtils.java
src/be/nikiroo/utils/main/StartImageUtils.java [new file with mode: 0644]
src/be/nikiroo/utils/main/StartStringUtils.java [new file with mode: 0644]
src/be/nikiroo/utils/test/Test.java
src/be/nikiroo/utils/ui/ImageTextAwt.java

diff --git a/ImageUtils.sh b/ImageUtils.sh
new file mode 100755 (executable)
index 0000000..8e12b66
--- /dev/null
@@ -0,0 +1,3 @@
+#!/bin/sh
+exec java -cp nikiroo-utils.jar be.nikiroo.utils.main.StartImageUtils "$@"
+
index d1a53caa38ebe0a1c0770aecdebcd8e559c328b2..5a2ecd0a17b73091da02b068848318878661e173 100644 (file)
@@ -1,5 +1,10 @@
 # nikiroo-utils
 
+## Version WIP
+
+- New: Utils now have a main class
+- Image to text converion fixes
+
 ## Version 4.3.0
 
 - New: IOUtils.Unzip()
index 3e2f6e761481fbf7c0e741539c7f811dbf272daa..0d9bc378a83b856599eae7f2afaec30fa3711332 100644 (file)
@@ -213,7 +213,23 @@ public class IOUtils {
                        dir.mkdirs();
                }
 
-               FileWriter writerVersion = new FileWriter(new File(dir, filename));
+               writeSmallFile(new File(dir, filename), content);
+       }
+
+       /**
+        * Write the {@link String} content to {@link File}.
+        * 
+        * @param file
+        *            the {@link File} to write
+        * @param content
+        *            the content
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public static void writeSmallFile(File file, String content)
+                       throws IOException {
+               FileWriter writerVersion = new FileWriter(file);
                try {
                        writerVersion.write(content);
                } finally {
diff --git a/src/be/nikiroo/utils/main/StartImageUtils.java b/src/be/nikiroo/utils/main/StartImageUtils.java
new file mode 100644 (file)
index 0000000..99f753c
--- /dev/null
@@ -0,0 +1,131 @@
+package be.nikiroo.utils.main;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.Image;
+import be.nikiroo.utils.ui.ImageTextAwt;
+import be.nikiroo.utils.ui.ImageTextAwt.Mode;
+import be.nikiroo.utils.ui.ImageUtilsAwt;
+
+public class StartImageUtils {
+       /**
+        * ImageUtils main entry point.
+        * <p>
+        * Syntax: see --help
+        * 
+        * @param args
+        */
+       public static void main(String[] args) {
+               Dimension size = null;
+               Mode mode = null;
+               boolean invert = false;
+               List<String> inputs = new ArrayList<String>();
+               File output = null;
+
+               String lastArg = "";
+               try {
+                       int height = -1;
+                       int width = -1;
+
+                       for (String arg : args) {
+                               lastArg = arg;
+
+                               if (arg.startsWith("--mode=")) {
+                                       mode = Mode.valueOf(arg.substring("--mode=".length()));
+                               } else if (arg.startsWith("--width=")) {
+                                       width = Integer
+                                                       .parseInt(arg.substring("--width=".length()));
+                               } else if (arg.startsWith("--height=")) {
+                                       height = Integer.parseInt(arg.substring("--height="
+                                                       .length()));
+                               } else if (arg.startsWith("--size=")) {
+                                       String content = arg.substring("--size=".length())
+                                                       .replace("X", "x");
+                                       width = Integer.parseInt(content.split("x")[0]);
+                                       height = Integer.parseInt(content.split("x")[1]);
+                               } else if (arg.startsWith("--ouput=")) {
+                                       if (!arg.equals("--output=-")) {
+                                               output = new File(arg.substring("--output=".length()));
+                                       }
+                               } else if (arg.equals("--invert")) {
+                                       invert = true;
+                               } else if (arg.equals("--help")) {
+                                       System.out
+                                                       .println("Syntax: (--mode=MODE) (--width=WIDTH) (--height=HEIGHT) (--size=SIZE) (--output=OUTPUT) (--invert) (--help)");
+                                       System.out.println("\t --help: will show this screen");
+                                       System.out
+                                                       .println("\t --invert: will invert the 'colours'");
+                                       System.out
+                                                       .println("\t --mode: will select the rendering mode (default: ASCII):");
+                                       System.out
+                                                       .println("\t\t ASCII: ASCI output mode, that is, characters \" .-+=o8#\"");
+                                       System.out
+                                                       .println("\t\t DITHERING: Use 5 different \"colours\" which are actually"
+                                                                       + "\n\t\t Unicode characters \" ░▒▓█\"");
+                                       System.out
+                                                       .println("\t\t DOUBLE_RESOLUTION: Use \"block\" Unicode characters up to quarter"
+                                                                       + "\n\t\t blocks, thus in effect doubling the resolution both in vertical"
+                                                                       + "\n\t\t and horizontal space."
+                                                                       + "\n\t\t Note that since 2 characters next to each other are square,"
+                                                                       + "\n\t\t 4 blocks per 2 blocks for w/h resolution.");
+                                       System.out
+                                                       .println("\t\t DOUBLE_DITHERING: Use characters from both DOUBLE_RESOLUTION"
+                                                                       + "\n\t\t and DITHERING");
+                                       return;
+                               } else {
+                                       inputs.add(arg);
+                               }
+                       }
+
+                       size = new Dimension(width, height);
+                       if (inputs.size() == 0) {
+                               inputs.add("-"); // by default, stdin
+                       }
+               } catch (Exception e) {
+                       System.err.println("Syntax error: \"" + lastArg + "\" is invalid");
+                       System.exit(1);
+               }
+
+               try {
+                       if (mode == null) {
+                               mode = Mode.ASCII;
+                       }
+
+                       for (String input : inputs) {
+                               InputStream in = null;
+
+                               try {
+                                       if (input.equals("-")) {
+                                               in = System.in;
+                                       } else {
+                                               in = new FileInputStream(input);
+                                       }
+                                       BufferedImage image = ImageUtilsAwt
+                                                       .fromImage(new Image(in));
+                                       ImageTextAwt img = new ImageTextAwt(image, size, mode,
+                                                       invert);
+                                       if (output == null) {
+                                               System.out.println(img.getText());
+                                       } else {
+                                               IOUtils.writeSmallFile(output, img.getText());
+                                       }
+                               } finally {
+                                       if (!input.equals("-")) {
+                                               in.close();
+                                       }
+                               }
+                       }
+               } catch (IOException e) {
+                       e.printStackTrace();
+                       System.exit(2);
+               }
+       }
+}
diff --git a/src/be/nikiroo/utils/main/StartStringUtils.java b/src/be/nikiroo/utils/main/StartStringUtils.java
new file mode 100644 (file)
index 0000000..ba5ccc6
--- /dev/null
@@ -0,0 +1,5 @@
+package be.nikiroo.utils.main;
+
+public class StartStringUtils {
+
+}
index dd6bf611792c302fc239082c8557583fff111271..7fe9bbc7c7a1c488e2fd2b1c56eff0ef0fd39f48 100644 (file)
@@ -2,6 +2,8 @@ package be.nikiroo.utils.test;
 
 import be.nikiroo.utils.Cache;
 import be.nikiroo.utils.Downloader;
+import be.nikiroo.utils.main.StartImageUtils;
+import be.nikiroo.utils.main.StartStringUtils;
 
 /**
  * Tests for nikiroo-utils.
@@ -31,6 +33,10 @@ public class Test extends TestLauncher {
                // TODO: test cache and downloader
                Cache cache = null;
                Downloader downloader = null;
+
+               // To include the sources:
+               StartImageUtils siu;
+               StartStringUtils ssu;
        }
 
        /**
index ee4f58c934502f234852d03acb735f6cef626965..8fdaa866b2357aa30aec72e1ba1dff042feb7d41 100644 (file)
@@ -165,30 +165,48 @@ public class ImageTextAwt {
                                mult = 2;
                        }
 
+                       Dimension srcSize = getSize(image);
+                       srcSize = new Dimension(srcSize.width * 2, srcSize.height);
+                       int x = 0;
+                       int y = 0;
+
                        int w = size.width * mult;
                        int h = size.height * mult;
 
+                       // Default = original ratio or original size if none
+                       if (w < 0 || h < 0) {
+                               if (w < 0 && h < 0) {
+                                       w = srcSize.width * mult;
+                                       h = srcSize.height * mult;
+                               } else {
+                                       double ratioSrc = (double) srcSize.width
+                                                       / (double) srcSize.height;
+                                       if (w < 0) {
+                                               w = (int) Math.round(h * ratioSrc);
+                                       } else {
+                                               h = (int) Math.round(w / ratioSrc);
+                                       }
+                               }
+                       }
+
+                       // Fail safe: we consider this to be too much
+                       if (w > 1000 || h > 1000) {
+                               return "[IMAGE TOO BIG]";
+                       }
+
                        BufferedImage buff = new BufferedImage(w, h,
                                        BufferedImage.TYPE_INT_ARGB);
 
                        Graphics gfx = buff.getGraphics();
 
-                       Dimension srcSize = getSize(image);
-                       srcSize = new Dimension(srcSize.width * 2, srcSize.height);
-                       int x = 0;
-                       int y = 0;
-
+                       double ratioAsked = (double) (w) / (double) (h);
+                       double ratioSrc = (double) srcSize.height / (double) srcSize.width;
+                       double ratio = ratioAsked * ratioSrc;
                        if (srcSize.width < srcSize.height) {
-                               double ratio = (double) size.width / (double) size.height;
-                               ratio *= (double) srcSize.height / (double) srcSize.width;
-
                                h = (int) Math.round(ratio * h);
                                y = (buff.getHeight() - h) / 2;
                        } else {
-                               double ratio = (double) size.height / (double) size.width;
-                               ratio *= (double) srcSize.width / (double) srcSize.height;
-
-                               w = (int) Math.round(ratio * w);
+                               w = (int) Math.round(w / ratio);
                                x = (buff.getWidth() - w) / 2;
                        }
 
@@ -214,12 +232,12 @@ public class ImageTextAwt {
 
                        StringBuilder builder = new StringBuilder();
 
-                       for (int row = 0; row < buff.getHeight(); row += mult) {
+                       for (int row = 0; row + (mult - 1) < buff.getHeight(); row += mult) {
                                if (row > 0) {
                                        builder.append('\n');
                                }
 
-                               for (int col = 0; col < buff.getWidth(); col += mult) {
+                               for (int col = 0; col + (mult - 1) < buff.getWidth(); col += mult) {
                                        if (mult == 1) {
                                                char car = ' ';
                                                float brightness = getBrightness(buff.getRGB(col, row));