2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import java
.awt
.image
.BufferedImage
;
33 import jexer
.backend
.ECMA48Terminal
;
34 import jexer
.backend
.MultiScreen
;
35 import jexer
.backend
.SwingTerminal
;
36 import jexer
.bits
.Cell
;
37 import jexer
.event
.TKeypressEvent
;
38 import jexer
.event
.TMouseEvent
;
39 import jexer
.event
.TResizeEvent
;
40 import static jexer
.TKeypress
.*;
43 * TImage renders a piece of a bitmap image on screen.
45 public class TImage
extends TWidget
{
47 // ------------------------------------------------------------------------
48 // Constants --------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * Selections for fitting the image to the text cells.
61 * Stretch/shrink the image in both directions to fully fill the text
67 * Scale the image, preserving aspect ratio, to fill the text area
68 * width/height (like letterbox). The background color for the
69 * letterboxed area is specified in scaleBackColor.
74 // ------------------------------------------------------------------------
75 // Variables --------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Scaling strategy to use.
81 private Scale scale
= Scale
.NONE
;
84 * Scaling strategy to use.
86 private java
.awt
.Color scaleBackColor
= java
.awt
.Color
.BLACK
;
89 * The action to perform when the user clicks on the image.
91 private TAction clickAction
;
94 * The image to display.
96 private BufferedImage image
;
99 * The original image from construction time.
101 private BufferedImage originalImage
;
104 * The current scaling factor for the image.
106 private double scaleFactor
= 1.0;
109 * The current clockwise rotation for the image.
111 private int clockwise
= 0;
114 * If true, this widget was resized and a new scaled image must be
117 private boolean resized
= false;
120 * Left column of the image. 0 is the left-most column.
125 * Top row of the image. 0 is the top-most row.
130 * The cells containing the broken up image pieces.
132 private Cell cells
[][];
135 * The number of rows in cells[].
137 private int cellRows
;
140 * The number of columns in cells[].
142 private int cellColumns
;
145 * Last text width value.
147 private int lastTextWidth
= -1;
150 * Last text height value.
152 private int lastTextHeight
= -1;
154 // ------------------------------------------------------------------------
155 // Constructors -----------------------------------------------------------
156 // ------------------------------------------------------------------------
159 * Public constructor.
161 * @param parent parent widget
162 * @param x column relative to parent
163 * @param y row relative to parent
164 * @param width number of text cells for width of the image
165 * @param height number of text cells for height of the image
166 * @param image the image to display
167 * @param left left column of the image. 0 is the left-most column.
168 * @param top top row of the image. 0 is the top-most row.
170 public TImage(final TWidget parent
, final int x
, final int y
,
171 final int width
, final int height
,
172 final BufferedImage image
, final int left
, final int top
) {
174 this(parent
, x
, y
, width
, height
, image
, left
, top
, null);
178 * Public constructor.
180 * @param parent parent widget
181 * @param x column relative to parent
182 * @param y row relative to parent
183 * @param width number of text cells for width of the image
184 * @param height number of text cells for height of the image
185 * @param image the image to display
186 * @param left left column of the image. 0 is the left-most column.
187 * @param top top row of the image. 0 is the top-most row.
188 * @param clickAction function to call when mouse is pressed
190 public TImage(final TWidget parent
, final int x
, final int y
,
191 final int width
, final int height
,
192 final BufferedImage image
, final int left
, final int top
,
193 final TAction clickAction
) {
195 // Set parent and window
196 super(parent
, x
, y
, width
, height
);
198 setCursorVisible(false);
199 this.originalImage
= image
;
202 this.clickAction
= clickAction
;
207 // ------------------------------------------------------------------------
208 // Event handlers ---------------------------------------------------------
209 // ------------------------------------------------------------------------
212 * Handle mouse press events.
214 * @param mouse mouse button press event
217 public void onMouseDown(final TMouseEvent mouse
) {
218 if (clickAction
!= null) {
219 clickAction
.DO(this);
227 * @param keypress keystroke event
230 public void onKeypress(final TKeypressEvent keypress
) {
231 if (!keypress
.getKey().isFnKey()) {
232 if (keypress
.getKey().getChar() == '+') {
233 // Make the image bigger.
239 if (keypress
.getKey().getChar() == '-') {
240 // Make the image smaller.
247 if (keypress
.equals(kbAltUp
)) {
248 // Make the image bigger.
254 if (keypress
.equals(kbAltDown
)) {
255 // Make the image smaller.
261 if (keypress
.equals(kbAltRight
)) {
269 if (keypress
.equals(kbAltLeft
)) {
270 // Rotate counter-clockwise.
280 if (keypress
.equals(kbShiftLeft
)) {
283 setScaleType(Scale
.SCALE
);
286 setScaleType(Scale
.NONE
);
289 setScaleType(Scale
.STRETCH
);
293 if (keypress
.equals(kbShiftRight
)) {
296 setScaleType(Scale
.STRETCH
);
299 setScaleType(Scale
.SCALE
);
302 setScaleType(Scale
.NONE
);
307 // Pass to parent for the things we don't care about.
308 super.onKeypress(keypress
);
312 * Handle resize events.
314 * @param event resize event
317 public void onResize(final TResizeEvent event
) {
318 // Get my width/height set correctly.
319 super.onResize(event
);
321 if (scale
== Scale
.NONE
) {
328 // ------------------------------------------------------------------------
329 // TWidget ----------------------------------------------------------------
330 // ------------------------------------------------------------------------
339 // We have already broken the image up, just draw the last set of
341 for (int x
= 0; (x
< getWidth()) && (x
+ left
< cellColumns
); x
++) {
342 if ((left
+ x
) * lastTextWidth
> image
.getWidth()) {
346 for (int y
= 0; (y
< getHeight()) && (y
+ top
< cellRows
); y
++) {
347 if ((top
+ y
) * lastTextHeight
> image
.getHeight()) {
350 assert (x
+ left
< cellColumns
);
351 assert (y
+ top
< cellRows
);
353 getWindow().putCharXY(x
, y
, cells
[x
+ left
][y
+ top
]);
359 // ------------------------------------------------------------------------
360 // TImage -----------------------------------------------------------------
361 // ------------------------------------------------------------------------
364 * Size cells[][] according to the screen font size.
366 * @param always if true, always resize the cells
368 private void sizeToImage(final boolean always
) {
369 int textWidth
= getScreen().getTextWidth();
370 int textHeight
= getScreen().getTextHeight();
373 image
= rotateImage(originalImage
, clockwise
);
374 image
= scaleImage(image
, scaleFactor
, getWidth(), getHeight(),
375 textWidth
, textHeight
);
378 if ((always
== true) ||
381 && (textWidth
!= lastTextWidth
)
383 && (textHeight
!= lastTextHeight
))
387 cellColumns
= image
.getWidth() / textWidth
;
388 if (cellColumns
* textWidth
< image
.getWidth()) {
391 cellRows
= image
.getHeight() / textHeight
;
392 if (cellRows
* textHeight
< image
.getHeight()) {
396 // Break the image up into an array of cells.
397 cells
= new Cell
[cellColumns
][cellRows
];
399 for (int x
= 0; x
< cellColumns
; x
++) {
400 for (int y
= 0; y
< cellRows
; y
++) {
402 int width
= textWidth
;
403 if ((x
+ 1) * textWidth
> image
.getWidth()) {
404 width
= image
.getWidth() - (x
* textWidth
);
406 int height
= textHeight
;
407 if ((y
+ 1) * textHeight
> image
.getHeight()) {
408 height
= image
.getHeight() - (y
* textHeight
);
411 Cell cell
= new Cell();
412 cell
.setImage(image
.getSubimage(x
* textWidth
,
413 y
* textHeight
, width
, height
));
419 lastTextWidth
= textWidth
;
420 lastTextHeight
= textHeight
;
423 if ((left
+ getWidth()) > cellColumns
) {
424 left
= cellColumns
- getWidth();
429 if ((top
+ getHeight()) > cellRows
) {
430 top
= cellRows
- getHeight();
438 * Get the top corner to render.
440 * @return the top row
442 public int getTop() {
447 * Set the top corner to render.
449 * @param top the new top row
451 public void setTop(final int top
) {
453 if (this.top
> cellRows
- getHeight()) {
454 this.top
= cellRows
- getHeight();
462 * Get the left corner to render.
464 * @return the left column
466 public int getLeft() {
471 * Set the left corner to render.
473 * @param left the new left column
475 public void setLeft(final int left
) {
477 if (this.left
> cellColumns
- getWidth()) {
478 this.left
= cellColumns
- getWidth();
486 * Get the number of text cell rows for this image.
488 * @return the number of rows
490 public int getRows() {
495 * Get the number of text cell columns for this image.
497 * @return the number of columns
499 public int getColumns() {
504 * Get the raw (unprocessed) image.
508 public BufferedImage
getImage() {
509 return originalImage
;
513 * Set the raw image, and reprocess to make the visible image.
515 * @param image the new image
517 public void setImage(final BufferedImage image
) {
518 this.originalImage
= image
;
524 * Get the visible (processed) image.
526 * @return the image that is currently on screen
528 public BufferedImage
getVisibleImage() {
533 * Get the scaling strategy.
535 * @return Scale.NONE, Scale.STRETCH, etc.
537 public Scale
getScaleType() {
542 * Set the scaling strategy.
544 * @param scale Scale.NONE, Scale.STRETCH, etc.
546 public void setScaleType(final Scale scale
) {
553 * Get the scale factor.
555 * @return the scale factor
557 public double getScaleFactor() {
562 * Set the scale factor. 1.0 means no scaling.
564 * @param scaleFactor the new scale factor
566 public void setScaleFactor(final double scaleFactor
) {
567 this.scaleFactor
= scaleFactor
;
573 * Get the rotation, as degrees.
575 * @return the rotation in degrees
577 public int getRotation() {
588 // Don't know how this happened, but fix it.
597 * Set the rotation, as degrees clockwise.
599 * @param rotation 0, 90, 180, or 270
601 public void setRotation(final int rotation
) {
616 // Don't know how this happened, but fix it.
626 * Scale an image by to be scaleFactor size.
628 * @param image the image to scale
629 * @param factor the scale to make the new image
630 * @param width the number of text cell columns for the destination image
631 * @param height the number of text cell rows for the destination image
632 * @param textWidth the width in pixels for one text cell
633 * @param textHeight the height in pixels for one text cell
635 private BufferedImage
scaleImage(final BufferedImage image
,
636 final double factor
, final int width
, final int height
,
637 final int textWidth
, final int textHeight
) {
639 if ((scale
== Scale
.NONE
) && (Math
.abs(factor
- 1.0) < 0.03)) {
640 // If we are within 3% of 1.0, just return the original image.
649 BufferedImage newImage
= null;
653 destWidth
= (int) (image
.getWidth() * factor
);
654 destHeight
= (int) (image
.getHeight() * factor
);
655 newImage
= new BufferedImage(destWidth
, destHeight
,
656 BufferedImage
.TYPE_INT_ARGB
);
659 destWidth
= width
* textWidth
;
660 destHeight
= height
* textHeight
;
661 newImage
= new BufferedImage(destWidth
, destHeight
,
662 BufferedImage
.TYPE_INT_ARGB
);
665 double a
= (double) image
.getWidth() / image
.getHeight();
666 double b
= (double) (width
* textWidth
) / (height
* textHeight
);
671 System.err.println("Scale: original " + image.getWidth() +
672 "x" + image.getHeight());
673 System.err.println(" screen " + (width * textWidth) +
674 "x" + (height * textHeight));
675 System.err.println("A " + a + " B " + b);
679 // Horizontal letterbox
680 destWidth
= width
* textWidth
;
681 destHeight
= (int) (destWidth
/ a
);
682 y
= ((height
* textHeight
) - destHeight
) / 2;
685 System.err.println("Horizontal letterbox: " + destWidth +
686 "x" + destHeight + ", Y offset " + y);
689 // Vertical letterbox
690 destHeight
= height
* textHeight
;
691 destWidth
= (int) (destHeight
* a
);
692 x
= ((width
* textWidth
) - destWidth
) / 2;
695 System.err.println("Vertical letterbox: " + destWidth +
696 "x" + destHeight + ", X offset " + x);
699 newImage
= new BufferedImage(width
* textWidth
, height
* textHeight
,
700 BufferedImage
.TYPE_INT_ARGB
);
704 java
.awt
.Graphics gr
= newImage
.createGraphics();
705 if (scale
== Scale
.SCALE
) {
706 gr
.setColor(scaleBackColor
);
707 gr
.fillRect(0, 0, width
* textWidth
, height
* textHeight
);
709 gr
.drawImage(image
, x
, y
, destWidth
, destHeight
, null);
715 * Rotate an image either clockwise or counterclockwise.
717 * @param image the image to scale
718 * @param clockwise number of turns clockwise
720 private BufferedImage
rotateImage(final BufferedImage image
,
721 final int clockwise
) {
723 if (clockwise
% 4 == 0) {
727 BufferedImage newImage
= null;
729 if (clockwise
% 4 == 1) {
730 // 90 degrees clockwise
731 newImage
= new BufferedImage(image
.getHeight(), image
.getWidth(),
732 BufferedImage
.TYPE_INT_ARGB
);
733 for (int x
= 0; x
< image
.getWidth(); x
++) {
734 for (int y
= 0; y
< image
.getHeight(); y
++) {
735 newImage
.setRGB(y
, x
,
736 image
.getRGB(x
, image
.getHeight() - 1 - y
));
739 } else if (clockwise
% 4 == 2) {
740 // 180 degrees clockwise
741 newImage
= new BufferedImage(image
.getWidth(), image
.getHeight(),
742 BufferedImage
.TYPE_INT_ARGB
);
743 for (int x
= 0; x
< image
.getWidth(); x
++) {
744 for (int y
= 0; y
< image
.getHeight(); y
++) {
745 newImage
.setRGB(x
, y
,
746 image
.getRGB(image
.getWidth() - 1 - x
,
747 image
.getHeight() - 1 - y
));
750 } else if (clockwise
% 4 == 3) {
751 // 270 degrees clockwise
752 newImage
= new BufferedImage(image
.getHeight(), image
.getWidth(),
753 BufferedImage
.TYPE_INT_ARGB
);
754 for (int x
= 0; x
< image
.getWidth(); x
++) {
755 for (int y
= 0; y
< image
.getHeight(); y
++) {
756 newImage
.setRGB(y
, x
,
757 image
.getRGB(image
.getWidth() - 1 - x
, y
));