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
.bits
.Cell
;
34 import jexer
.event
.TCommandEvent
;
35 import jexer
.event
.TKeypressEvent
;
36 import jexer
.event
.TMouseEvent
;
37 import jexer
.event
.TResizeEvent
;
38 import static jexer
.TCommand
.*;
39 import static jexer
.TKeypress
.*;
42 * TImage renders a piece of a bitmap image on screen.
44 public class TImage
extends TWidget
implements EditMenuUser
{
46 // ------------------------------------------------------------------------
47 // Constants --------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * Selections for fitting the image to the text cells.
60 * Stretch/shrink the image in both directions to fully fill the text
66 * Scale the image, preserving aspect ratio, to fill the text area
67 * width/height (like letterbox). The background color for the
68 * letterboxed area is specified in scaleBackColor.
73 // ------------------------------------------------------------------------
74 // Variables --------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Scaling strategy to use.
80 private Scale scale
= Scale
.NONE
;
83 * Scaling strategy to use.
85 private java
.awt
.Color scaleBackColor
= java
.awt
.Color
.BLACK
;
88 * The action to perform when the user clicks on the image.
90 private TAction clickAction
;
93 * The image to display.
95 private BufferedImage image
;
98 * The original image from construction time.
100 private BufferedImage originalImage
;
103 * The current scaling factor for the image.
105 private double scaleFactor
= 1.0;
108 * The current clockwise rotation for the image.
110 private int clockwise
= 0;
113 * If true, this widget was resized and a new scaled image must be
116 private boolean resized
= false;
119 * Left column of the image. 0 is the left-most column.
124 * Top row of the image. 0 is the top-most row.
129 * The cells containing the broken up image pieces.
131 private Cell cells
[][];
134 * The number of rows in cells[].
136 private int cellRows
;
139 * The number of columns in cells[].
141 private int cellColumns
;
144 * Last text width value.
146 private int lastTextWidth
= -1;
149 * Last text height value.
151 private int lastTextHeight
= -1;
153 // ------------------------------------------------------------------------
154 // Constructors -----------------------------------------------------------
155 // ------------------------------------------------------------------------
158 * Public constructor.
160 * @param parent parent widget
161 * @param x column relative to parent
162 * @param y row relative to parent
163 * @param width number of text cells for width of the image
164 * @param height number of text cells for height of the image
165 * @param image the image to display
166 * @param left left column of the image. 0 is the left-most column.
167 * @param top top row of the image. 0 is the top-most row.
169 public TImage(final TWidget parent
, final int x
, final int y
,
170 final int width
, final int height
,
171 final BufferedImage image
, final int left
, final int top
) {
173 this(parent
, x
, y
, width
, height
, image
, left
, top
, null);
177 * Public constructor.
179 * @param parent parent widget
180 * @param x column relative to parent
181 * @param y row relative to parent
182 * @param width number of text cells for width of the image
183 * @param height number of text cells for height of the image
184 * @param image the image to display
185 * @param left left column of the image. 0 is the left-most column.
186 * @param top top row of the image. 0 is the top-most row.
187 * @param clickAction function to call when mouse is pressed
189 public TImage(final TWidget parent
, final int x
, final int y
,
190 final int width
, final int height
,
191 final BufferedImage image
, final int left
, final int top
,
192 final TAction clickAction
) {
194 // Set parent and window
195 super(parent
, x
, y
, width
, height
);
197 setCursorVisible(false);
198 this.originalImage
= image
;
201 this.clickAction
= clickAction
;
206 // ------------------------------------------------------------------------
207 // Event handlers ---------------------------------------------------------
208 // ------------------------------------------------------------------------
211 * Handle mouse press events.
213 * @param mouse mouse button press event
216 public void onMouseDown(final TMouseEvent mouse
) {
217 if (clickAction
!= null) {
218 clickAction
.DO(this);
226 * @param keypress keystroke event
229 public void onKeypress(final TKeypressEvent keypress
) {
230 if (!keypress
.getKey().isFnKey()) {
231 if (keypress
.getKey().getChar() == '+') {
232 // Make the image bigger.
238 if (keypress
.getKey().getChar() == '-') {
239 // Make the image smaller.
246 if (keypress
.equals(kbAltUp
)) {
247 // Make the image bigger.
253 if (keypress
.equals(kbAltDown
)) {
254 // Make the image smaller.
260 if (keypress
.equals(kbAltRight
)) {
268 if (keypress
.equals(kbAltLeft
)) {
269 // Rotate counter-clockwise.
279 if (keypress
.equals(kbShiftLeft
)) {
282 setScaleType(Scale
.SCALE
);
285 setScaleType(Scale
.NONE
);
288 setScaleType(Scale
.STRETCH
);
292 if (keypress
.equals(kbShiftRight
)) {
295 setScaleType(Scale
.STRETCH
);
298 setScaleType(Scale
.SCALE
);
301 setScaleType(Scale
.NONE
);
306 // Pass to parent for the things we don't care about.
307 super.onKeypress(keypress
);
311 * Handle resize events.
313 * @param event resize event
316 public void onResize(final TResizeEvent event
) {
317 // Get my width/height set correctly.
318 super.onResize(event
);
320 if (scale
== Scale
.NONE
) {
328 * Handle posted command events.
330 * @param command command event
333 public void onCommand(final TCommandEvent command
) {
334 if (command
.equals(cmCopy
)) {
335 // Copy image to clipboard.
336 getClipboard().copyImage(image
);
341 // ------------------------------------------------------------------------
342 // TWidget ----------------------------------------------------------------
343 // ------------------------------------------------------------------------
352 // We have already broken the image up, just draw the last set of
354 for (int x
= 0; (x
< getWidth()) && (x
+ left
< cellColumns
); x
++) {
355 if ((left
+ x
) * lastTextWidth
> image
.getWidth()) {
359 for (int y
= 0; (y
< getHeight()) && (y
+ top
< cellRows
); y
++) {
360 if ((top
+ y
) * lastTextHeight
> image
.getHeight()) {
363 assert (x
+ left
< cellColumns
);
364 assert (y
+ top
< cellRows
);
366 getWindow().putCharXY(x
, y
, cells
[x
+ left
][y
+ top
]);
372 // ------------------------------------------------------------------------
373 // TImage -----------------------------------------------------------------
374 // ------------------------------------------------------------------------
377 * Size cells[][] according to the screen font size.
379 * @param always if true, always resize the cells
381 private void sizeToImage(final boolean always
) {
382 int textWidth
= getScreen().getTextWidth();
383 int textHeight
= getScreen().getTextHeight();
386 image
= rotateImage(originalImage
, clockwise
);
387 image
= scaleImage(image
, scaleFactor
, getWidth(), getHeight(),
388 textWidth
, textHeight
);
391 if ((always
== true) ||
394 && (textWidth
!= lastTextWidth
)
396 && (textHeight
!= lastTextHeight
))
400 cellColumns
= image
.getWidth() / textWidth
;
401 if (cellColumns
* textWidth
< image
.getWidth()) {
404 cellRows
= image
.getHeight() / textHeight
;
405 if (cellRows
* textHeight
< image
.getHeight()) {
409 // Break the image up into an array of cells.
410 cells
= new Cell
[cellColumns
][cellRows
];
412 for (int x
= 0; x
< cellColumns
; x
++) {
413 for (int y
= 0; y
< cellRows
; y
++) {
415 int width
= textWidth
;
416 if ((x
+ 1) * textWidth
> image
.getWidth()) {
417 width
= image
.getWidth() - (x
* textWidth
);
419 int height
= textHeight
;
420 if ((y
+ 1) * textHeight
> image
.getHeight()) {
421 height
= image
.getHeight() - (y
* textHeight
);
424 Cell cell
= new Cell();
425 if ((width
!= textWidth
) || (height
!= textHeight
)) {
426 BufferedImage newImage
;
427 newImage
= new BufferedImage(textWidth
, textHeight
,
428 BufferedImage
.TYPE_INT_ARGB
);
430 java
.awt
.Graphics gr
= newImage
.getGraphics();
431 gr
.drawImage(image
.getSubimage(x
* textWidth
,
432 y
* textHeight
, width
, height
),
435 cell
.setImage(newImage
);
437 cell
.setImage(image
.getSubimage(x
* textWidth
,
438 y
* textHeight
, width
, height
));
445 lastTextWidth
= textWidth
;
446 lastTextHeight
= textHeight
;
449 if ((left
+ getWidth()) > cellColumns
) {
450 left
= cellColumns
- getWidth();
455 if ((top
+ getHeight()) > cellRows
) {
456 top
= cellRows
- getHeight();
464 * Get the top corner to render.
466 * @return the top row
468 public int getTop() {
473 * Set the top corner to render.
475 * @param top the new top row
477 public void setTop(final int top
) {
479 if (this.top
> cellRows
- getHeight()) {
480 this.top
= cellRows
- getHeight();
488 * Get the left corner to render.
490 * @return the left column
492 public int getLeft() {
497 * Set the left corner to render.
499 * @param left the new left column
501 public void setLeft(final int left
) {
503 if (this.left
> cellColumns
- getWidth()) {
504 this.left
= cellColumns
- getWidth();
512 * Get the number of text cell rows for this image.
514 * @return the number of rows
516 public int getRows() {
521 * Get the number of text cell columns for this image.
523 * @return the number of columns
525 public int getColumns() {
530 * Get the raw (unprocessed) image.
534 public BufferedImage
getImage() {
535 return originalImage
;
539 * Set the raw image, and reprocess to make the visible image.
541 * @param image the new image
543 public void setImage(final BufferedImage image
) {
544 this.originalImage
= image
;
550 * Get the visible (processed) image.
552 * @return the image that is currently on screen
554 public BufferedImage
getVisibleImage() {
559 * Get the scaling strategy.
561 * @return Scale.NONE, Scale.STRETCH, etc.
563 public Scale
getScaleType() {
568 * Set the scaling strategy.
570 * @param scale Scale.NONE, Scale.STRETCH, etc.
572 public void setScaleType(final Scale scale
) {
579 * Get the scale factor.
581 * @return the scale factor
583 public double getScaleFactor() {
588 * Set the scale factor. 1.0 means no scaling.
590 * @param scaleFactor the new scale factor
592 public void setScaleFactor(final double scaleFactor
) {
593 this.scaleFactor
= scaleFactor
;
599 * Get the rotation, as degrees.
601 * @return the rotation in degrees
603 public int getRotation() {
614 // Don't know how this happened, but fix it.
623 * Set the rotation, as degrees clockwise.
625 * @param rotation 0, 90, 180, or 270
627 public void setRotation(final int rotation
) {
642 // Don't know how this happened, but fix it.
652 * Scale an image by to be scaleFactor size.
654 * @param image the image to scale
655 * @param factor the scale to make the new image
656 * @param width the number of text cell columns for the destination image
657 * @param height the number of text cell rows for the destination image
658 * @param textWidth the width in pixels for one text cell
659 * @param textHeight the height in pixels for one text cell
661 private BufferedImage
scaleImage(final BufferedImage image
,
662 final double factor
, final int width
, final int height
,
663 final int textWidth
, final int textHeight
) {
665 if ((scale
== Scale
.NONE
) && (Math
.abs(factor
- 1.0) < 0.03)) {
666 // If we are within 3% of 1.0, just return the original image.
675 BufferedImage newImage
= null;
679 destWidth
= (int) (image
.getWidth() * factor
);
680 destHeight
= (int) (image
.getHeight() * factor
);
681 newImage
= new BufferedImage(destWidth
, destHeight
,
682 BufferedImage
.TYPE_INT_ARGB
);
685 destWidth
= width
* textWidth
;
686 destHeight
= height
* textHeight
;
687 newImage
= new BufferedImage(destWidth
, destHeight
,
688 BufferedImage
.TYPE_INT_ARGB
);
691 double a
= (double) image
.getWidth() / image
.getHeight();
692 double b
= (double) (width
* textWidth
) / (height
* textHeight
);
697 System.err.println("Scale: original " + image.getWidth() +
698 "x" + image.getHeight());
699 System.err.println(" screen " + (width * textWidth) +
700 "x" + (height * textHeight));
701 System.err.println("A " + a + " B " + b);
705 // Horizontal letterbox
706 destWidth
= width
* textWidth
;
707 destHeight
= (int) (destWidth
/ a
);
708 y
= ((height
* textHeight
) - destHeight
) / 2;
711 System.err.println("Horizontal letterbox: " + destWidth +
712 "x" + destHeight + ", Y offset " + y);
715 // Vertical letterbox
716 destHeight
= height
* textHeight
;
717 destWidth
= (int) (destHeight
* a
);
718 x
= ((width
* textWidth
) - destWidth
) / 2;
721 System.err.println("Vertical letterbox: " + destWidth +
722 "x" + destHeight + ", X offset " + x);
725 newImage
= new BufferedImage(width
* textWidth
, height
* textHeight
,
726 BufferedImage
.TYPE_INT_ARGB
);
730 java
.awt
.Graphics gr
= newImage
.createGraphics();
731 if (scale
== Scale
.SCALE
) {
732 gr
.setColor(scaleBackColor
);
733 gr
.fillRect(0, 0, width
* textWidth
, height
* textHeight
);
735 gr
.drawImage(image
, x
, y
, destWidth
, destHeight
, null);
741 * Rotate an image either clockwise or counterclockwise.
743 * @param image the image to scale
744 * @param clockwise number of turns clockwise
746 private BufferedImage
rotateImage(final BufferedImage image
,
747 final int clockwise
) {
749 if (clockwise
% 4 == 0) {
753 BufferedImage newImage
= null;
755 if (clockwise
% 4 == 1) {
756 // 90 degrees clockwise
757 newImage
= new BufferedImage(image
.getHeight(), image
.getWidth(),
758 BufferedImage
.TYPE_INT_ARGB
);
759 for (int x
= 0; x
< image
.getWidth(); x
++) {
760 for (int y
= 0; y
< image
.getHeight(); y
++) {
761 newImage
.setRGB(y
, x
,
762 image
.getRGB(x
, image
.getHeight() - 1 - y
));
765 } else if (clockwise
% 4 == 2) {
766 // 180 degrees clockwise
767 newImage
= new BufferedImage(image
.getWidth(), image
.getHeight(),
768 BufferedImage
.TYPE_INT_ARGB
);
769 for (int x
= 0; x
< image
.getWidth(); x
++) {
770 for (int y
= 0; y
< image
.getHeight(); y
++) {
771 newImage
.setRGB(x
, y
,
772 image
.getRGB(image
.getWidth() - 1 - x
,
773 image
.getHeight() - 1 - y
));
776 } else if (clockwise
% 4 == 3) {
777 // 270 degrees clockwise
778 newImage
= new BufferedImage(image
.getHeight(), image
.getWidth(),
779 BufferedImage
.TYPE_INT_ARGB
);
780 for (int x
= 0; x
< image
.getWidth(); x
++) {
781 for (int y
= 0; y
< image
.getHeight(); y
++) {
782 newImage
.setRGB(y
, x
,
783 image
.getRGB(image
.getWidth() - 1 - x
, y
));
791 // ------------------------------------------------------------------------
792 // EditMenuUser -----------------------------------------------------------
793 // ------------------------------------------------------------------------
796 * Check if the cut menu item should be enabled.
798 * @return true if the cut menu item should be enabled
800 public boolean isEditMenuCut() {
805 * Check if the copy menu item should be enabled.
807 * @return true if the copy menu item should be enabled
809 public boolean isEditMenuCopy() {
814 * Check if the paste menu item should be enabled.
816 * @return true if the paste menu item should be enabled
818 public boolean isEditMenuPaste() {
823 * Check if the clear menu item should be enabled.
825 * @return true if the clear menu item should be enabled
827 public boolean isEditMenuClear() {