Support for obtaining image paths from stdio
This commit is contained in:
parent
1082b5aea8
commit
4990ab5383
@ -1,7 +1,9 @@
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Matcher;
|
||||
@ -16,6 +18,10 @@ import javax.imageio.ImageIO;
|
||||
*/
|
||||
public class TerminalImageViewer {
|
||||
|
||||
static boolean grayscale = false;
|
||||
static int mode = Ansi.MODE_24BIT;
|
||||
|
||||
|
||||
/**
|
||||
* Main method, handles command line arguments and loads and scales images.
|
||||
*/
|
||||
@ -23,15 +29,15 @@ public class TerminalImageViewer {
|
||||
if (args.length == 0) {
|
||||
System.out.println(
|
||||
"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;
|
||||
}
|
||||
|
||||
int start = 0;
|
||||
int maxWidth = 80;
|
||||
int maxHeight = 24;
|
||||
int mode = Ansi.MODE_24BIT;
|
||||
boolean grayscale = false;
|
||||
boolean stdin = false;
|
||||
while (start < args.length && args[start].startsWith("-")) {
|
||||
String option = args[start];
|
||||
if (option.equals("-w") && args.length > start + 1) {
|
||||
@ -42,6 +48,8 @@ public class TerminalImageViewer {
|
||||
mode = (mode & ~Ansi.MODE_24BIT) | Ansi.MODE_256;
|
||||
} else if (option.equals("-grayscale")) {
|
||||
grayscale = true;
|
||||
} else if (option.equals("-stdin")) {
|
||||
stdin = true;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
@ -49,25 +57,17 @@ public class TerminalImageViewer {
|
||||
maxWidth *= 4;
|
||||
maxHeight *= 8;
|
||||
|
||||
if (start == args.length - 1 && (isUrl(args[start]) || !new File(args[start]).isDirectory())) {
|
||||
String name = args[start];
|
||||
|
||||
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);
|
||||
if (stdin) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
String name = reader.readLine();
|
||||
if (name == null || name.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
convert(name, maxWidth, maxHeight);
|
||||
}
|
||||
} else if (start == args.length - 1 && (isUrl(args[start]) || !new File(args[start]).isDirectory())) {
|
||||
convert(args[start], maxWidth, maxHeight);
|
||||
} else {
|
||||
// Directory-style rendering.
|
||||
int index = 0;
|
||||
@ -109,6 +109,25 @@ public class TerminalImageViewer {
|
||||
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 {
|
||||
if (isUrl(name)) {
|
||||
URL url = new URL(name);
|
||||
|
Loading…
Reference in New Issue
Block a user