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,52 +18,93 @@ public class TerminalImageViewer {
return; return;
} }
int start = 0;
int w = 80 * 4; int w = 80 * 4;
if (args[0].equals("-w") && args.length > 2) {
w = 4 * Integer.parseInt(args[1]);
start = 2;
}
for (int i = 0; i < args.length; i++) { if (start == args.length - 1) {
String name = args[i]; String name = args[start];
if (name.equals("-w")) {
w = 4 * Integer.parseInt(args[++i]);
continue;
}
BufferedImage original; BufferedImage original = loadImage(args[start]);
if (name.startsWith("http://") || name.startsWith("https://")) {
URL url = new URL(name);
original = ImageIO.read(url);
} 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 {
ImageData imageData = new ImageData(w, h); // Directory-style rendering.
byte[] data = imageData.data; int index = 0;
int[] rgbArray = new int[w]; int cw = (w - 2 * 3 * 4) / 16;
for (int y = 0; y < image.getHeight(); y++) { int tw = cw * 4;
image.getRGB(0, y, image.getWidth(), 1, rgbArray, 0, w); while (index < args.length) {
int pos = y * w * 4; BufferedImage image = new BufferedImage(tw * 4 + 24, tw, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < w; x++) { Graphics2D graphics = image.createGraphics();
int rgb = rgbArray[x]; int count = 0;
data[pos++] = (byte) (rgb >> 16); StringBuilder sb = new StringBuilder();
data[pos++] = (byte) (rgb >> 8); while (index < args.length && count < 4) {
data[pos++] = (byte) rgb; String name = args[index++];
pos++; 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();
} }
System.out.println(imageData.dump());
} }
} }
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;
int[] rgbArray = new int[w];
for (int y = 0; y < image.getHeight(); y++) {
image.getRGB(0, y, image.getWidth(), 1, rgbArray, 0, w);
int pos = y * w * 4;
for (int x = 0; x < w; x++) {
int rgb = rgbArray[x];
data[pos++] = (byte) (rgb >> 16);
data[pos++] = (byte) (rgb >> 8);
data[pos++] = (byte) rgb;
pos++;
}
}
System.out.print(imageData.dump());
}
/** /**
* ANSI control code helpers * ANSI control code helpers
*/ */