Procházet zdrojové kódy

* Added getopt / mygetopt support to img2irc, and added options to control width, height, and format

tags/v0.99.beta14
Jean-Yves Lamoureux jylam před 17 roky
rodič
revize
2efdd10e16
2 změnil soubory, kde provedl 95 přidání a 17 odebrání
  1. +1
    -1
      src/Makefile.am
  2. +94
    -16
      src/img2irc.c

+ 1
- 1
src/Makefile.am Zobrazit soubor

@@ -29,7 +29,7 @@ cacaplay_LDADD = ../caca/libcaca.la ../cucul/libcucul.la
cacaserver_SOURCES = cacaserver.c
cacaserver_LDADD = ../cucul/libcucul.la

img2irc_SOURCES = img2irc.c common-image.c common-image.h
img2irc_SOURCES = img2irc.c mygetopt.c mygetopt.h common-image.c common-image.h
img2irc_LDADD = ../cucul/libcucul.la
img2irc_CFLAGS = $(IMLIB2_CFLAGS)
img2irc_LDFLAGS = $(IMLIB2_LIBS)


+ 94
- 16
src/img2irc.c Zobrazit soubor

@@ -1,6 +1,7 @@
/*
* pic2irc image to IRC converter
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* 2007 Jean-Yves Lamoureux <jylam@lnxscene.org>
* All Rights Reserved
*
* $Id$
@@ -21,14 +22,41 @@
# include <stdlib.h>
#endif

#if !defined HAVE_GETOPT_LONG
# include "mygetopt.h"
#elif defined HAVE_GETOPT_H
# include <getopt.h>
#endif
#if defined HAVE_GETOPT_LONG
# define mygetopt getopt_long
# define myoptind optind
# define myoptarg optarg
# define myoption option
#endif

#include "cucul.h"
#include "common-image.h"

static void usage(int argc, char **argv)
{
fprintf(stderr, "Usage: %s <image>\n", argv[0]);
fprintf(stderr, " %s <image> <columns>\n", argv[0]);
fprintf(stderr, " %s [-h|--help]\n", argv[0]);
fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
fprintf(stderr, "Convert IMAGE to any text based available format.\n");
fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -h, --help\tThis help\n");
fprintf(stderr, " -W, --width=WIDTH\tWidth of resulting image\n");
fprintf(stderr, " -H, --HEIGHT=HEIGHT\tHeight of resulting image\n");
fprintf(stderr, " -f, --format=FORMAT\tFormat of the resulting image :\n");
fprintf(stderr, "\t\t\tansi : coulored ANSI\n");
fprintf(stderr, "\t\t\tcaca : internal libcaca format\n");
fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n");
fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n");
fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n");
fprintf(stderr, "\t\t\thtml : Pure HTML3 with tables\n");
fprintf(stderr, "\t\t\tirc : IRC with ctrl-k codes\n");
fprintf(stderr, "\t\t\tps : Postscript\n");
fprintf(stderr, "\t\t\tsvg : Scalable Vector Graphics\n");
fprintf(stderr, "\t\t\ttga : Targa Image\n\n");
}

int main(int argc, char **argv)
@@ -38,22 +66,54 @@ int main(int argc, char **argv)
void *export;
unsigned long int len;
struct image *i;
int cols, lines;
unsigned int cols = 0, lines = 0;
char *format = NULL;

if(argc < 2 || argc > 3)

if(argc < 2)
{
fprintf(stderr, "%s: wrong argument count\n", argv[0]);
usage(argc, argv);
return 1;
}

if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
for(;;)
{
fprintf(stderr, "%s: convert images to IRC file data\n", argv[0]);
usage(argc, argv);
return 0;
int option_index = 0;
static struct myoption long_options[] =
{
{ "width", 1, NULL, 'W' },
{ "height", 1, NULL, 'H' },
{ "format", 1, NULL, 'f' },
{ "help", 0, NULL, 'h' },
};
int c = mygetopt(argc, argv, "W:H:f:h", long_options, &option_index);
if(c == -1)
break;

switch(c)
{
case 'W': /* --width */
cols = atoi(myoptarg);
break;
case 'H': /* --height */
lines = atoi(myoptarg);
break;
case 'f': /* --help */
format = myoptarg;
break;
case 'h': /* --help */
usage(argc, argv);
return 0;
break;
default:
return 1;
break;
}
}



cv = cucul_create_canvas(0, 0);
if(!cv)
{
@@ -61,7 +121,7 @@ int main(int argc, char **argv)
return 1;
}

i = load_image(argv[1]);
i = load_image(argv[argc-1]);
if(!i)
{
fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[1]);
@@ -70,9 +130,20 @@ int main(int argc, char **argv)
}

/* Assume a 6×10 font */
cols = argc == 3 ? atoi(argv[2]) : 0;
cols = cols ? cols : 60;
lines = cols * i->h * 6 / i->w / 10;
if(!cols && !lines)
{
cols = 60;
lines = cols * i->h * 6 / i->w / 10;
}
else if(cols && !lines)
{
lines = cols * i->h * 6 / i->w / 10;
}
else if(!cols && lines)
{
cols = lines * i->w * 10 / i->h / 6;
}


cucul_set_canvas_size(cv, cols, lines);
cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
@@ -81,9 +152,16 @@ int main(int argc, char **argv)

unload_image(i);

export = cucul_export_memory(cv, "irc", &len);
fwrite(export, len, 1, stdout);
free(export);
export = cucul_export_memory(cv, format?format:"ansi", &len);
if(!export)
{
fprintf(stderr, "Can't export to format '%s'\n", format);
}
else
{
fwrite(export, len, 1, stdout);
free(export);
}

cucul_free_canvas(cv);



Načítá se…
Zrušit
Uložit