Open an image in a terminal window-Linux

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
gan
Posts: 1
Joined: 2019-05-29T12:56:07-07:00
Authentication code: 1152

Open an image in a terminal window-Linux

Post by gan »

Hi, I'm using Ubuntu 16.04 with the standard GNOME terminal and I wanted to know if it was possible to open an image in a terminal window?
With the following program using ncurses it opens it in a new window.

libncurses5-dev must be installed for debian-like systems:

Code: Select all

sudo apt-get install libncurses5-dev
Please compile this program like so (example.cpp):

Code: Select all

g++ -Wall -std=c++11 `Magick++-config --cxxflags --cppflags` -o example example.cpp `Magick++-config --ldflags --libs` -lncurses 

Press <ESCAPE> to end program or <ENTER> to view image.

Code: Select all

#include "../ImageMagick/Magick++/lib/Magick++.h"
#include <iostream>
#include <string>
#include <ncurses.h>

const int ENTER = 10;
const int ESCAPE = 27;

void init_curses()
{
    initscr();
    raw();
    noecho();
    cbreak();
    curs_set(0);
}

int highlight(int choice, int &selectedIn, int SIZE)
{
    int result = 0;
    switch (choice) {
        case KEY_DOWN: case 'j':
            ++selectedIn;
            if (selectedIn >= SIZE) {
                selectedIn = SIZE - 1;
            }
            break;
        case KEY_UP: case 'k':
            --selectedIn;
            if (selectedIn < 0) {
                selectedIn = 0;
            }
            break;
        case ENTER:
            result = 1;
            break;
        case ESCAPE:
            result = -1;      
            break;
        default:
            break;
    }
    return result;
}

int main(int argc, char **argv)
{
    /* Images list */
    const int size = 4;
    std::string imgs[] = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg" };
    /* ********** */
    
    /* Magick++ init */
     Magick::InitializeMagick(*argv);
    /* Magick++ *** */
    
    /* NCURSES */
    init_curses();
    if (has_colors()) {
        use_default_colors();
        start_color();
        init_pair(1, COLOR_CYAN, COLOR_BLUE);
    }
    
    /* Print Message window */
    std::string msg = "Press <ESCAPE> to end program or <ENTER> to view image.";
    WINDOW *win_msg = newwin(1, 90, 1, 1);
    mvwprintw(win_msg, 0, 0, msg.c_str());
   /* End Print Message window */
        
    WINDOW *win = newwin(size + 2, imgs[0].length() + 1, 2, 1);
    refresh();
    wrefresh(win);
    wrefresh(win_msg);
    keypad(win, TRUE);
    
    int selected = 0;
    int choice = 0;
    int result = 0;
    /* NCURSES */
    
    /* List Image names in terminal window */
    while (1) {
        for (int i = 0; i < size; ++i) {
            if (i == selected) {
                wattron(win, COLOR_PAIR(1));
            }
            mvwprintw(win, i + 1, 1, imgs[i].c_str());
            wattroff(win, COLOR_PAIR(1));
        }
        choice = wgetch(win);
        result = highlight(choice, selected, size);
        if (result == -1) {
            break;
        } else if (result == 1) {
            /* Display images whit Magick++ in terminal window - does not open in the console*/
            Magick::Image image;
            image.read(imgs[selected]);
            image.display();
        }
    }
    
    /* End NCURSES window */
    endwin();
    return 0;
}
thank you
Post Reply