source: libcaca/trunk/cucul/export_irc.c @ 769

Last change on this file since 769 was 769, checked in by Sam Hocevar, 14 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: export_irc.c 769 2006-04-14 07:30:53Z sam $
8 *
9 *  This library is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains export functions for IRC
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#   include <stdio.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30/** \brief Generate IRC representation of current image.
31 *
32 *  This function generates and returns an IRC representation of
33 *  the current image.
34 */
35void _cucul_get_irc(cucul_t *qq, struct cucul_buffer *ex)
36{
37    static int const palette[] =
38    {
39        1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
40        14, 12, 9, 11, 4, 13, 8, 0, /* Light */
41    };
42
43    char *cur;
44    unsigned int x, y;
45
46    /* 11 bytes assumed for max length per pixel. Worst case scenario:
47     * ^Cxx,yy   6 bytes
48     * ^B^B      2 bytes
49     * c         1 byte
50     * \r\n      2 bytes
51     * In real life, the average bytes per pixel value will be around 5.
52     */
53
54    ex->size = 2 + (qq->width * qq->height * 11);
55    ex->data = malloc(ex->size);
56
57    cur = ex->data;
58
59    for(y = 0; y < qq->height; y++)
60    {
61        uint32_t *lineattr = qq->attr + y * qq->width;
62        uint32_t *linechar = qq->chars + y * qq->width;
63
64        uint8_t prevfg = -1;
65        uint8_t prevbg = -1;
66
67        for(x = 0; x < qq->width; x++)
68        {
69            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
70            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
71            uint32_t c = linechar[x];
72
73            if(bg == prevbg)
74            {
75                if(fg == prevfg)
76                    ; /* Same fg/bg, do nothing */
77                else if(c == (uint32_t)' ')
78                    fg = prevfg; /* Hackety hack */
79                else
80                {
81                    cur += sprintf(cur, "\x03%d", fg);
82                    if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
83                        cur += sprintf(cur, "\x02\x02");
84                }
85            }
86            else
87            {
88                if(fg == prevfg)
89                    cur += sprintf(cur, "\x03,%d", bg);
90                else
91                    cur += sprintf(cur, "\x03%d,%d", fg, bg);
92
93                if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
94                    cur += sprintf(cur, "\x02\x02");
95            }
96            *cur++ = c & 0x7f;
97            prevfg = fg;
98            prevbg = bg;
99        }
100        *cur++ = '\r';
101        *cur++ = '\n';
102    }
103
104    /* Crop to really used size */
105    ex->size = (uintptr_t)(cur - ex->data);
106    ex->data = realloc(ex->data, ex->size);
107}
Note: See TracBrowser for help on using the repository browser.