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

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