Image Viewer

Represents an image that is being rendered under a view-port:

To create an image viewer use ImageViewer::new method (with 4 parameters: an image, a layout, render options, and initialization flags). To understand more on how an image is being rendered or constructed read the Images chapter.

let i = ImageViewer::new(Image::from_str(...).unwrap(), 
                         layout!("x:10,y:5,w:15"),
                         image::RenderOptionsBuilder::new()
                             .scale(image::Scale::Scale50)
                             .character_set(image::CharacterSet::AsciiArt)
                             .build(),
                         imageviewer::Flags::None);

Or use the macro imageviewer!:

let i1 = imageviewer!("x:10,y:5,w:15,scale:50%,charset:AsciiArt");
let i2 = imageviewer!("image:'|R..|,|.R.|,|..R|',x:10,y:5,w:15");

An image viewer supports all common parameters (as they are described in Instantiate via Macros section). Besides them, the following named parameters are also accepted:

Parameter nameTypePositional parameterPurpose
imageStringNoA string representation of an image as described in Images (Building from a string) chapter
scalePercentageNoThe scaling percentage. Acceptable values are: 100%, 50%, 33%, 25%, 20%, 10% and 5%
charset or char_setEnum valuesNoThe character set for rendering. Can be: SmallBlocks, LargeBlocks, DitheredShades, Braille, AsciiArt
color_schema or colorschema or csEnum valuesNoThe color schema for rendering. Can be: Auto, Color16, TrueColors, GrayScale4, GrayScaleTrueColors, BlackAndWhite
luminance_threshold or ltPercentageNoThe luminance threshold percentage (0-100) for black/white conversion
flagsStringNoImage viewer initialization flags
back or backgroundchar! formatNoA character as described in Macro Builds - the same as with the char! macro format
lsm or left-scroll-marginNumericNoThe left margin of the bottom scroll bar in characters. If not provided the default value is 0. This should be a positive number and it only has an effect if the flag Scrollbars was set up.
tsm or top-scroll-marginNumericNoThe top margin of the right scroll bar in characters. If not provided the default value is 0. This should be a positive number and it only has an effect if the flag Scrollbars was set up.

An image viewer supports the following initialization flags:

  • imageviewer::Flags::ScrollBars or ScrollBars (for macro initialization) - this enables a set of scrollbars that can be used to change the view of the inner surface, but only when the control has focus, as described in Components section.

Some examples that use these parameters:

  1. An image viewer with a background that consists of the character X with Aqua and DarkBlue colors.
    let img = imageviewer!("x:10,y:5,w:15,back={X,fore:aqua,back:darkblue}");
    
  2. An image viewer with scrollbars with different margins
    let img = imageviewer!("x:10,y:5,w:15,flags:Scrollbars,lsm:5,tsm:1");
    
  3. An ASCII art image with scrollbars with different margins and 50% scaling:
    let img = imageviewer!("image:'...',x:10,y:5,w:15,flags:Scrollbars,lsm:5,tsm:1,scale:50%,charset:AsciiArt");
    
  4. An image viewer with custom color schema and luminance threshold:
    let img = imageviewer!("image:'|RGB|,|YWr|',x:10,y:5,w:15,color_schema:BlackAndWhite,luminance_threshold:30%");
    

Events

An image viewer control emits no events.

Methods

Besides the Common methods for all Controls an image viewer also has the following additional methods:

MethodPurpose
set_image(...)Sets a new image to be displayed in the image viewer
render_options()Returns the current render options of the image viewer
set_render_options(...)Sets new render options for the image viewer
set_background(...)Sets the character used for background
clear_background()Remove the background character making the background transparent.

Key association

The following keys are processed by an image viewer control if it has focus:

KeyPurpose
Left,Right,Up,DownMove the view port to a specified direction by one character.
Shift+LeftMoves the horizontal view port coordinate to 0
Shift+UpMoves the vertical view port coordinate to 0
Shift+RightMoves the horizontal view port coordinate so that the right side of the inner surface is displayed
Shift+DownMoves the vertical view port coordinate so that the bottom side of the inner surface is displayed
Ctrl+{Left,Right,Up,Down}Move the view port to a specified direction by a number of characters that is equal to the width for Left/Right or height for Up/Down.
PageUp, PageDownhas the same effect as Ctrl+{Up or Down}
HomeMoves the view port to the coordinates (0,0)
EndMoves the view port so that the bottom-right part of the inner surface is visible

Example

The following code draws a heart with different colors using an ImageViewer:

use appcui::prelude::*;

fn main() -> Result<(), appcui::system::Error> {
    let mut a = App::new().build()?;
    let mut w = window!("Heart,a:c,w:15,h:7");
    let heart = Image::from_str(r#"
        |.............|
        |...rr...rr...|
        |..rrrr.rrrr..|
        |.rrrrrrrrrrr.|
        |.raaaaaaaaar.|
        |..ryyyyyyyr..|
        |   rwwwwwr   |
        |....rwwwr....|
        |.....rwr.....|
        |......r......|
    "#).unwrap();
    w.add(ImageViewer::new(
        heart,
        layout!("d:f"),
        image::RenderOptionsBuilder::new()
            .scale(image::Scale::Scale50)
            .character_set(image::CharacterSet::AsciiArt)
            .color_schema(image::ColorSchema::Color16)
            .build(),
        imageviewer::Flags::None,
    ));
    a.add_window(w);
    a.run();
    Ok(())
}