Support for obtaining image paths from stdio

This commit is contained in:
Stefan Haustein 2016-07-11 20:15:35 +02:00
parent 1082b5aea8
commit 4990ab5383

View file

@ -1,7 +1,9 @@
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -16,6 +18,10 @@ import javax.imageio.ImageIO;
*/ */
public class TerminalImageViewer { public class TerminalImageViewer {
static boolean grayscale = false;
static int mode = Ansi.MODE_24BIT;
/** /**
* Main method, handles command line arguments and loads and scales images. * Main method, handles command line arguments and loads and scales images.
*/ */
@ -23,15 +29,15 @@ public class TerminalImageViewer {
if (args.length == 0) { if (args.length == 0) {
System.out.println( System.out.println(
"Image file name required.\n\n - Use -w and -h to set the maximum width and height in characters" + "Image file name required.\n\n - Use -w and -h to set the maximum width and height in characters" +
" (defaults: 80, 24).\n - Use -256 for 256 color mode and -grayscale for grayscale.\n"); " (defaults: 80, 24).\n - Use -256 for 256 color mode, -grayscale for grayscale and -stdin to" +
" obtain file names from stdin.\n");
return; return;
} }
int start = 0; int start = 0;
int maxWidth = 80; int maxWidth = 80;
int maxHeight = 24; int maxHeight = 24;
int mode = Ansi.MODE_24BIT; boolean stdin = false;
boolean grayscale = false;
while (start < args.length && args[start].startsWith("-")) { while (start < args.length && args[start].startsWith("-")) {
String option = args[start]; String option = args[start];
if (option.equals("-w") && args.length > start + 1) { if (option.equals("-w") && args.length > start + 1) {
@ -42,6 +48,8 @@ public class TerminalImageViewer {
mode = (mode & ~Ansi.MODE_24BIT) | Ansi.MODE_256; mode = (mode & ~Ansi.MODE_24BIT) | Ansi.MODE_256;
} else if (option.equals("-grayscale")) { } else if (option.equals("-grayscale")) {
grayscale = true; grayscale = true;
} else if (option.equals("-stdin")) {
stdin = true;
} }
start++; start++;
} }
@ -49,25 +57,17 @@ public class TerminalImageViewer {
maxWidth *= 4; maxWidth *= 4;
maxHeight *= 8; maxHeight *= 8;
if (start == args.length - 1 && (isUrl(args[start]) || !new File(args[start]).isDirectory())) { if (stdin) {
String name = args[start]; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
BufferedImage original = loadImage(name); String name = reader.readLine();
if (name == null || name.isEmpty()) {
float originalWidth = original.getWidth(); break;
float originalHeight = original.getHeight();
float scale = Math.min(maxWidth / originalWidth, maxHeight / originalHeight);
int height = (int) (originalHeight * scale);
int width = (int) (originalWidth * scale);
if (originalWidth == width && !grayscale) {
dump(original, mode);
} else {
BufferedImage image = new BufferedImage(width, height, grayscale ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.drawImage(original, 0, 0, width, height, null);
dump(image, mode);
} }
convert(name, maxWidth, maxHeight);
}
} else if (start == args.length - 1 && (isUrl(args[start]) || !new File(args[start]).isDirectory())) {
convert(args[start], maxWidth, maxHeight);
} else { } else {
// Directory-style rendering. // Directory-style rendering.
int index = 0; int index = 0;
@ -109,6 +109,25 @@ public class TerminalImageViewer {
return name.startsWith("http://") || name.startsWith("https://"); return name.startsWith("http://") || name.startsWith("https://");
} }
static void convert(String name, int maxWidth, int maxHeight) throws IOException {
BufferedImage original = loadImage(name);
float originalWidth = original.getWidth();
float originalHeight = original.getHeight();
float scale = Math.min(maxWidth / originalWidth, maxHeight / originalHeight);
int height = (int) (originalHeight * scale);
int width = (int) (originalWidth * scale);
if (originalWidth == width && !grayscale) {
dump(original, mode);
} else {
BufferedImage image = new BufferedImage(width, height, grayscale ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.drawImage(original, 0, 0, width, height, null);
dump(image, mode);
}
}
static BufferedImage loadImage(String name) throws IOException { static BufferedImage loadImage(String name) throws IOException {
if (isUrl(name)) { if (isUrl(name)) {
URL url = new URL(name); URL url = new URL(name);