render thumnails for multiple files

This commit is contained in:
Stefan Haustein 2016-04-09 10:54:10 +02:00
parent 188e1a2506
commit 238b92c9a3

View file

@ -18,35 +18,77 @@ public class TerminalImageViewer {
return; return;
} }
int start = 0;
int w = 80 * 4; int w = 80 * 4;
if (args[0].equals("-w") && args.length > 2) {
for (int i = 0; i < args.length; i++) { w = 4 * Integer.parseInt(args[1]);
String name = args[i]; start = 2;
if (name.equals("-w")) {
w = 4 * Integer.parseInt(args[++i]);
continue;
} }
BufferedImage original; if (start == args.length - 1) {
if (name.startsWith("http://") || name.startsWith("https://")) { String name = args[start];
URL url = new URL(name);
original = ImageIO.read(url); BufferedImage original = loadImage(args[start]);
} else {
original = ImageIO.read(new File(args[0]));
}
int ow = original.getWidth(); int ow = original.getWidth();
int oh = original.getHeight(); int oh = original.getHeight();
int h = oh * w / ow; int h = oh * w / ow;
BufferedImage image = original; if (w == ow) {
if (w != ow) { dump(original);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } else {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics(); Graphics2D graphics = image.createGraphics();
graphics.drawImage(original, 0, 0, w, h, null); graphics.drawImage(original, 0, 0, w, h, null);
dump(image);
}
} else {
// Directory-style rendering.
int index = 0;
int cw = (w - 2 * 3 * 4) / 16;
int tw = cw * 4;
while (index < args.length) {
BufferedImage image = new BufferedImage(tw * 4 + 24, tw, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
int count = 0;
StringBuilder sb = new StringBuilder();
while (index < args.length && count < 4) {
String name = args[index++];
try {
BufferedImage original = loadImage(name);
int cut = name.lastIndexOf('/');
sb.append(name.substring(cut + 1));
int th = original.getHeight() * tw / original.getWidth();
graphics.drawImage(original, count * (tw + 8), (tw - th) / 2, tw, th, null);
count++;
int sl = count * (cw + 2);
while (sb.length() < sl - 2) {
sb.append(' ');
}
sb.setLength(sl - 2);
sb.append(" ");
} catch (Exception e) {
// Probably no image; ignore.
}
}
dump(image);
System.out.println(sb.toString());
System.out.println();
}
}
} }
ImageData imageData = new ImageData(w, h); static BufferedImage loadImage(String name) throws IOException {
if (name.startsWith("http://") || name.startsWith("https://")) {
URL url = new URL(name);
return ImageIO.read(url);
}
return ImageIO.read(new File(name));
}
static void dump(BufferedImage image) {
int w = image.getWidth();
ImageData imageData = new ImageData(w, image.getHeight());
byte[] data = imageData.data; byte[] data = imageData.data;
int[] rgbArray = new int[w]; int[] rgbArray = new int[w];
for (int y = 0; y < image.getHeight(); y++) { for (int y = 0; y < image.getHeight(); y++) {
@ -60,8 +102,7 @@ public class TerminalImageViewer {
pos++; pos++;
} }
} }
System.out.println(imageData.dump()); System.out.print(imageData.dump());
}
} }
/** /**