@Override public TextGraphics drawImage( TerminalPosition topLeft, TextImage image, TerminalPosition sourceImageTopLeft, TerminalSize sourceImageSize) { // If the source image position is negative, offset the whole image if (sourceImageTopLeft.getColumn() < 0) { topLeft = topLeft.withRelativeColumn(-sourceImageTopLeft.getColumn()); sourceImageSize = sourceImageSize.withRelativeColumns(sourceImageTopLeft.getColumn()); sourceImageTopLeft = sourceImageTopLeft.withColumn(0); } if (sourceImageTopLeft.getRow() < 0) { topLeft = topLeft.withRelativeRow(-sourceImageTopLeft.getRow()); sourceImageSize = sourceImageSize.withRelativeRows(sourceImageTopLeft.getRow()); sourceImageTopLeft = sourceImageTopLeft.withRow(0); } // cropping specified image-subrectangle to the image itself: int fromRow = Math.max(sourceImageTopLeft.getRow(), 0); int untilRow = Math.min( sourceImageTopLeft.getRow() + sourceImageSize.getRows(), image.getSize().getRows()); int fromColumn = Math.max(sourceImageTopLeft.getColumn(), 0); int untilColumn = Math.min( sourceImageTopLeft.getColumn() + sourceImageSize.getColumns(), image.getSize().getColumns()); // difference between position in image and position on target: int diffRow = topLeft.getRow() - sourceImageTopLeft.getRow(); int diffColumn = topLeft.getColumn() - sourceImageTopLeft.getColumn(); // top/left-crop at target(TextGraphics) rectangle: (only matters, if topLeft has a negative // coordinate) fromRow = Math.max(fromRow, -diffRow); fromColumn = Math.max(fromColumn, -diffColumn); // bot/right-crop at target(TextGraphics) rectangle: (only matters, if topLeft has a negative // coordinate) untilRow = Math.min(untilRow, getSize().getRows() - diffRow); untilColumn = Math.min(untilColumn, getSize().getColumns() - diffColumn); if (fromRow >= untilRow || fromColumn >= untilColumn) { return this; } for (int row = fromRow; row < untilRow; row++) { for (int column = fromColumn; column < untilColumn; column++) { setCharacter(column + diffColumn, row + diffRow, image.getCharacterAt(column, row)); } } return this; }
@Override public TextGraphics drawImage(TerminalPosition topLeft, TextImage image) { return drawImage(topLeft, image, TerminalPosition.TOP_LEFT_CORNER, image.getSize()); }