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

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