TermIm

Imel

Descrizione

Tramite l'utilizzo della libreria Imel questo programma analizza un'immagine e cerca di riprodurla nel terminale.


Sorgente

/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include <imel.h>
#include <sys/ioctl.h>
#include <string.h>

static char *p_chars = NULL;

void image_print (ImelImage *);

int main (int argc, char *argv[])
{
 ImelImage *image, *resize;
 ImelError error;
 int height, width;
 struct winsize ws;

 if ( argc < 2 ) {
      fprintf (stderr, "Usage: %s <image> [chars]\n", *argv);
      return 1;
 }

 if ( argc > 2 )
      p_chars = argv[2];

 image = imel_image_new_from (argv[1], 0, &error);
 if ( !image ) {
      fprintf (stderr, "%s\n", error.description);
 }

 ioctl(1, TIOCGWINSZ, &ws);
 width = (ws.ws_col > image->width) ? image->width : ws.ws_col;
 height = (ws.ws_col > image->height) ? image->height :
            (image->height * ws.ws_col) / image->width;
 resize = imel_image_resize (image, width, height);
 imel_image_free (image);
 imel_image_apply_effect (resize, IMEL_EFFECT_WHITE_BLACK);
 image_print (resize);
 imel_image_free (resize);

 return 0;
}

char get_character (ImelColor Y)
{
 char *s = " -/\\|~;?JO5E9#";

 if ( p_chars )
      return p_chars[(Y * strlen (p_chars)) / 255];

 return s[(Y * 14) / 255];
}

void image_print (ImelImage *image)
{
 ImelPixel *p;
 int x, y;

 for ( y = 0; y < image->height; y++ ) {
       for ( x = 0; x < image->width; x++ ) {
             putc (get_character (image->pixel[y][x].red), stdout);
       }
       putc ('\n', stdout);
 }
}