From b73fc652a8bae52fb3cf653d628cf749dce20324 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Wed, 6 Nov 2019 15:17:10 -0600 Subject: [PATCH] #65 stretch images to fit into text cells --- src/jexer/backend/ECMA48Terminal.java | 88 +++++++++++++++++++-------- src/jexer/backend/SwingTerminal.java | 6 +- src/jexer/demos/Demo6.java | 2 +- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/jexer/backend/ECMA48Terminal.java b/src/jexer/backend/ECMA48Terminal.java index 3933d22f..ed2715c8 100644 --- a/src/jexer/backend/ECMA48Terminal.java +++ b/src/jexer/backend/ECMA48Terminal.java @@ -3192,12 +3192,10 @@ public class ECMA48Terminal extends LogicalScreen int imageWidth = cells.get(0).getImage().getWidth(); int imageHeight = cells.get(0).getImage().getHeight(); - // cells.get(x).getImage() has a dithered bitmap containing indexes - // into the color palette. Piece these together into one larger - // image for final rendering. + // Piece these together into one larger image for final rendering. int totalWidth = 0; - int fullWidth = cells.size() * getTextWidth(); - int fullHeight = getTextHeight(); + int fullWidth = cells.size() * imageWidth; + int fullHeight = imageHeight; for (int i = 0; i < cells.size(); i++) { totalWidth += cells.get(i).getImage().getWidth(); } @@ -3207,10 +3205,8 @@ public class ECMA48Terminal extends LogicalScreen int [] rgbArray; for (int i = 0; i < cells.size() - 1; i++) { - int tileWidth = Math.min(cells.get(i).getImage().getWidth(), - imageWidth); - int tileHeight = Math.min(cells.get(i).getImage().getHeight(), - imageHeight); + int tileWidth = imageWidth; + int tileHeight = imageHeight; if (false && cells.get(i).isInvertedImage()) { // I used to put an all-white cell over the cursor, don't do @@ -3278,7 +3274,7 @@ public class ECMA48Terminal extends LogicalScreen image.setRGB((cells.size() - 1) * imageWidth, 0, totalWidth, imageHeight, rgbArray, 0, totalWidth); - if (totalWidth < getTextWidth()) { + if (totalWidth < imageWidth) { int backgroundColor = cells.get(cells.size() - 1).getBackground().getRGB(); for (int imageX = image.getWidth() - totalWidth; @@ -3290,6 +3286,22 @@ public class ECMA48Terminal extends LogicalScreen } } + if ((image.getWidth() != cells.size() * getTextWidth()) + || (image.getHeight() != getTextHeight()) + ) { + // Rescale the image to fit the text cells it is going into. + BufferedImage newImage; + newImage = new BufferedImage(cells.size() * getTextWidth(), + getTextHeight(), BufferedImage.TYPE_INT_ARGB); + + java.awt.Graphics gr = newImage.getGraphics(); + gr.drawImage(image, 0, 0, newImage.getWidth(), + newImage.getHeight(), null, null); + gr.dispose(); + image = newImage; + fullHeight = image.getHeight(); + } + // Dither the image. It is ok to lose the original here. if (palette == null) { palette = new SixelPalette(); @@ -3507,8 +3519,8 @@ public class ECMA48Terminal extends LogicalScreen // Piece cells.get(x).getImage() pieces together into one larger // image for final rendering. int totalWidth = 0; - int fullWidth = cells.size() * getTextWidth(); - int fullHeight = getTextHeight(); + int fullWidth = cells.size() * imageWidth; + int fullHeight = imageHeight; for (int i = 0; i < cells.size(); i++) { totalWidth += cells.get(i).getImage().getWidth(); } @@ -3518,10 +3530,8 @@ public class ECMA48Terminal extends LogicalScreen int [] rgbArray; for (int i = 0; i < cells.size() - 1; i++) { - int tileWidth = Math.min(cells.get(i).getImage().getWidth(), - imageWidth); - int tileHeight = Math.min(cells.get(i).getImage().getHeight(), - imageHeight); + int tileWidth = imageWidth; + int tileHeight = imageHeight; if (false && cells.get(i).isInvertedImage()) { // I used to put an all-white cell over the cursor, don't do // that anymore. @@ -3588,7 +3598,7 @@ public class ECMA48Terminal extends LogicalScreen image.setRGB((cells.size() - 1) * imageWidth, 0, totalWidth, imageHeight, rgbArray, 0, totalWidth); - if (totalWidth < getTextWidth()) { + if (totalWidth < imageWidth) { int backgroundColor = cells.get(cells.size() - 1).getBackground().getRGB(); for (int imageX = image.getWidth() - totalWidth; @@ -3600,6 +3610,22 @@ public class ECMA48Terminal extends LogicalScreen } } + if ((image.getWidth() != cells.size() * getTextWidth()) + || (image.getHeight() != getTextHeight()) + ) { + // Rescale the image to fit the text cells it is going into. + BufferedImage newImage; + newImage = new BufferedImage(cells.size() * getTextWidth(), + getTextHeight(), BufferedImage.TYPE_INT_ARGB); + + java.awt.Graphics gr = newImage.getGraphics(); + gr.drawImage(image, 0, 0, newImage.getWidth(), + newImage.getHeight(), null, null); + gr.dispose(); + image = newImage; + fullHeight = image.getHeight(); + } + /* * From https://iterm2.com/documentation-images.html: * @@ -3756,8 +3782,8 @@ public class ECMA48Terminal extends LogicalScreen // Piece cells.get(x).getImage() pieces together into one larger // image for final rendering. int totalWidth = 0; - int fullWidth = cells.size() * getTextWidth(); - int fullHeight = getTextHeight(); + int fullWidth = cells.size() * imageWidth; + int fullHeight = imageHeight; for (int i = 0; i < cells.size(); i++) { totalWidth += cells.get(i).getImage().getWidth(); } @@ -3767,10 +3793,8 @@ public class ECMA48Terminal extends LogicalScreen int [] rgbArray; for (int i = 0; i < cells.size() - 1; i++) { - int tileWidth = Math.min(cells.get(i).getImage().getWidth(), - imageWidth); - int tileHeight = Math.min(cells.get(i).getImage().getHeight(), - imageHeight); + int tileWidth = imageWidth; + int tileHeight = imageHeight; if (false && cells.get(i).isInvertedImage()) { // I used to put an all-white cell over the cursor, don't do // that anymore. @@ -3837,7 +3861,7 @@ public class ECMA48Terminal extends LogicalScreen image.setRGB((cells.size() - 1) * imageWidth, 0, totalWidth, imageHeight, rgbArray, 0, totalWidth); - if (totalWidth < getTextWidth()) { + if (totalWidth < imageWidth) { int backgroundColor = cells.get(cells.size() - 1).getBackground().getRGB(); for (int imageX = image.getWidth() - totalWidth; @@ -3849,6 +3873,22 @@ public class ECMA48Terminal extends LogicalScreen } } + if ((image.getWidth() != cells.size() * getTextWidth()) + || (image.getHeight() != getTextHeight()) + ) { + // Rescale the image to fit the text cells it is going into. + BufferedImage newImage; + newImage = new BufferedImage(cells.size() * getTextWidth(), + getTextHeight(), BufferedImage.TYPE_INT_ARGB); + + java.awt.Graphics gr = newImage.getGraphics(); + gr.drawImage(image, 0, 0, newImage.getWidth(), + newImage.getHeight(), null, null); + gr.dispose(); + image = newImage; + fullHeight = image.getHeight(); + } + if (jexerImageOption == JexerImageOption.PNG) { // Encode as PNG ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(1024); diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index c2b335f4..eb1616bf 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -1244,9 +1244,11 @@ public class SwingTerminal extends LogicalScreen BufferedImage image = cell.getImage(); if (image != null) { if (swing.getFrame() != null) { - gr.drawImage(image, xPixel, yPixel, swing.getFrame()); + gr.drawImage(image, xPixel, yPixel, getTextWidth(), + getTextHeight(), swing.getFrame()); } else { - gr.drawImage(image, xPixel, yPixel, swing.getComponent()); + gr.drawImage(image, xPixel, yPixel, getTextWidth(), + getTextHeight(),swing.getComponent()); } return; } diff --git a/src/jexer/demos/Demo6.java b/src/jexer/demos/Demo6.java index db0b5c9d..41d1f2c3 100644 --- a/src/jexer/demos/Demo6.java +++ b/src/jexer/demos/Demo6.java @@ -111,7 +111,7 @@ public class Demo6 { * Make a new Swing window for the second application. */ SwingBackend monitorBackend = new SwingBackend(width + 5, - height + 5, 16); + height + 5, 20); /* * Setup the second application, give it the basic file and -- 2.27.0