source: libcaca/trunk/src/img2irc.c @ 1328

Last change on this file since 1328 was 1328, checked in by Sam Hocevar, 13 years ago
  • Wrote manual pages for img2irc, cacaplay and cacaserver.
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/*
2 *  pic2irc       image to IRC converter
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: img2irc.c 1328 2006-11-10 15:38:09Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15#include "common.h"
16
17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <string.h>
20#   include <stdlib.h>
21#endif
22
23#include "cucul.h"
24#include "common-image.h"
25
26static void usage(int argc, char **argv)
27{
28    fprintf(stderr, "Usage: %s <image>\n", argv[0]);
29    fprintf(stderr, "       %s <image> <columns>\n", argv[0]);
30    fprintf(stderr, "       %s [-h|--help]\n", argv[0]);
31}
32
33int main(int argc, char **argv)
34{
35    /* libcucul context */
36    cucul_canvas_t *cv;
37    void *export;
38    unsigned long int len;
39    struct image *i;
40    int cols, lines;
41
42    if(argc < 2 || argc > 3)
43    {
44        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
45        usage(argc, argv);
46        return 1;
47    }
48
49    if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
50    {
51        fprintf(stderr, "%s: convert images to IRC file data\n", argv[0]);
52        usage(argc, argv);
53        return 0;
54    }
55
56    cv = cucul_create_canvas(0, 0);
57    if(!cv)
58    {
59        fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
60        return 1;
61    }
62
63    i = load_image(argv[1]);
64    if(!i)
65    {
66        fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[1]);
67        cucul_free_canvas(cv);
68        return 1;
69    }
70
71    /* Assume a 6×10 font */
72    cols = argc == 3 ? atoi(argv[2]) : 0;
73    cols = cols ? cols : 60;
74    lines = cols * i->h * 6 / i->w / 10;
75
76    cucul_set_canvas_size(cv, cols, lines);
77    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
78    cucul_clear_canvas(cv);
79    cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
80
81    unload_image(i);
82
83    export = cucul_export_memory(cv, "irc", &len);
84    fwrite(export, len, 1, stdout);
85    free(export);
86
87    cucul_free_canvas(cv);
88
89    return 0;
90}
91
Note: See TracBrowser for help on using the repository browser.