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

Last change on this file since 609 was 609, checked in by Sam Hocevar, 14 years ago
  • Replaced all cucul_get_* exporters with a generic cucul_export() function.
  • Got rid of static buffers; we now use cucul_free() to free exported buffers.
  • Fixed light background in the ANSI exporter by adding escape sequences for most terminal emulators.
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
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.c
13 *  \version \$Id: export_irc.c 609 2006-03-14 22:05:34Z 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
33
34/** \brief Generate IRC representation of current image.
35 *
36 *  This function generates and returns an IRC representation of
37 *  the current image.
38 */
39void _cucul_get_irc(cucul_t *qq, struct cucul_buffer *ex)
40{
41    static int const palette[] =
42    {
43        1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
44        14, 12, 9, 11, 4, 13, 8, 0, /* Light */
45    };
46
47    char *cur;
48    unsigned int x, y;
49
50    /* 11 bytes assumed for max length per pixel. Worst case scenario:
51     * ^Cxx,yy   6 bytes
52     * ^B^B      2 bytes
53     * c         1 byte
54     * \r\n      2 bytes
55     * In real life, the average bytes per pixel value will be around 5.
56     */
57
58    ex->size = 2 + (qq->width * qq->height * 11);
59    ex->buffer = malloc(ex->size);
60
61    cur = ex->buffer;
62
63    *cur++ = '\x0f';
64
65    for(y = 0; y < qq->height; y++)
66    {
67        uint8_t *lineattr = qq->attr + y * qq->width;
68        uint32_t *linechar = qq->chars + y * qq->width;
69
70        uint8_t prevfg = -1;
71        uint8_t prevbg = -1;
72
73        for(x = 0; x < qq->width; x++)
74        {
75            uint8_t fg = palette[lineattr[x] & 0x0f];
76            uint8_t bg = palette[lineattr[x] >> 4];
77            uint32_t c = linechar[x];
78
79            if(bg == prevbg)
80            {
81                if(fg == prevfg)
82                    ; /* Same fg/bg, do nothing */
83                else if(c == (uint32_t)' ')
84                    fg = prevfg; /* Hackety hack */
85                else
86                {
87                    cur += sprintf(cur, "\x03%d", fg);
88                    if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
89                        cur += sprintf(cur, "\x02\x02");
90                }
91            }
92            else
93            {
94                if(fg == prevfg)
95                    cur += sprintf(cur, "\x03,%d", bg);
96                else
97                    cur += sprintf(cur, "\x03%d,%d", fg, bg);
98
99                if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
100                    cur += sprintf(cur, "\x02\x02");
101            }
102            *cur++ = c & 0x7f;
103            prevfg = fg;
104            prevbg = bg;
105        }
106        *cur++ = '\r';
107        *cur++ = '\n';
108    }
109
110    *cur++ = '\x0f';
111
112    /* Crop to really used size */
113    ex->size = strlen(ex->buffer) + 1;
114    ex->buffer = realloc(ex->buffer, ex->size);
115}
Note: See TracBrowser for help on using the repository browser.