From: Niki Roo Date: Fri, 6 Apr 2018 14:46:19 +0000 (+0200) Subject: New Utils main classes + ImageUtils fixes X-Git-Tag: fanfix-swing-0.0.1~21^2~215 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=ce060f5a4fcbea33cbecf2e8696fde7db183bff0;p=fanfix-swing.git New Utils main classes + ImageUtils fixes --- diff --git a/ImageUtils.sh b/ImageUtils.sh new file mode 100755 index 00000000..8e12b665 --- /dev/null +++ b/ImageUtils.sh @@ -0,0 +1,3 @@ +#!/bin/sh +exec java -cp nikiroo-utils.jar be.nikiroo.utils.main.StartImageUtils "$@" + diff --git a/changelog.md b/changelog.md index d1a53caa..5a2ecd0a 100644 --- a/changelog.md +++ b/changelog.md @@ -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() diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index 3e2f6e76..0d9bc378 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -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 index 00000000..99f753c2 --- /dev/null +++ b/src/be/nikiroo/utils/main/StartImageUtils.java @@ -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. + *

+ * Syntax: see --help + * + * @param args + */ + public static void main(String[] args) { + Dimension size = null; + Mode mode = null; + boolean invert = false; + List inputs = new ArrayList(); + 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 index 00000000..ba5ccc60 --- /dev/null +++ b/src/be/nikiroo/utils/main/StartStringUtils.java @@ -0,0 +1,5 @@ +package be.nikiroo.utils.main; + +public class StartStringUtils { + +} diff --git a/src/be/nikiroo/utils/test/Test.java b/src/be/nikiroo/utils/test/Test.java index dd6bf611..7fe9bbc7 100644 --- a/src/be/nikiroo/utils/test/Test.java +++ b/src/be/nikiroo/utils/test/Test.java @@ -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; } /** diff --git a/src/be/nikiroo/utils/ui/ImageTextAwt.java b/src/be/nikiroo/utils/ui/ImageTextAwt.java index ee4f58c9..8fdaa866 100644 --- a/src/be/nikiroo/utils/ui/ImageTextAwt.java +++ b/src/be/nikiroo/utils/ui/ImageTextAwt.java @@ -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));