Skip to content

Instantly share code, notes, and snippets.

@krtx
Last active December 19, 2015 19:38
Show Gist options
  • Select an option

  • Save krtx/6007514 to your computer and use it in GitHub Desktop.

Select an option

Save krtx/6007514 to your computer and use it in GitHub Desktop.
animation gif player 256 colors version

imagemagick が入ってれば多分使える ubuntu だったら libmagick++ みたいなやつ?

コンパイル

g++ `Magick++-config --cxxflags --cppflags` -o agif agif.cpp -lncurses `Magick++-config --ldflags --libs`

使い方

./agif [-w] filename

環境変数 TERM を screen-256color か xterm-256color にセットする必要があるかも

#include <Magick++.h>
#include <ncurses.h>
#include <unistd.h>
#include <vector>
#include <algorithm>
int main(int argc, char *argv[])
{
bool full = false;
int result;
while ((result = getopt(argc, argv, "w")) != -1) {
switch (result) {
case 'w':
full = true;
break;
}
}
if (optind >= argc) {
fprintf(stderr, "usage: ./agif256 [-w] filename\n");
return 1;
}
std::vector<Magick::Image> images;
Magick::readImages(&images, argv[optind]);
// init ncurses
WINDOW *scr = initscr();
nodelay(scr, TRUE); // non-blocking getch
start_color();
for (short i = 1; i < COLORS; i++) init_pair(i, i, i);
int lines, columns;
getmaxyx(scr, lines, columns);
// scale images
Magick::Geometry scale(columns / 2, lines, 0, 0);
scale.aspect(full);
for (std::vector<Magick::Image>::iterator it = images.begin(); it != images.end(); it++)
it->resize(scale);
// fill terminal
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
attrset(COLOR_PAIR(232));
move(i, j);
printw(" ");
}
}
// play animation
int cnt = 0;
while (true) {
for (int i = 0; i < images[0].rows(); i++) {
for (int j = 0; j < images[0].columns(); j++) {
Magick::ColorRGB c = images[cnt].pixelColor(j, i);
short r = (short)std::min(c.red() * 6, 5.0)
, g = (short)std::min(c.green() * 6, 5.0)
, b = (short)std::min(c.blue() * 6, 5.0);
attrset(COLOR_PAIR(16 + r * 36 + g * 6 + b));
move(lines / 2 - images[0].rows() / 2 + i, columns / 2 - images[0].columns() + j * 2);
printw(" ");
}
}
if (getch() != ERR) break;
refresh();
usleep(images[cnt].animationDelay() * 10000);
cnt = (cnt + 1) % images.size();
}
endwin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment