source: libcaca/trunk/cucul/export_ansi.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.1 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_ansi.c
13 *  \version \$Id: export_ansi.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 ANSI
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 ANSI representation of current image.
33 *
34 *  This function generates and returns an ANSI representation of
35 *  the current image.
36 *  \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
37 *                  able to cut/paste the result to a function like printf
38 *  \return buffer containing generated ANSI codes as a big string
39 */
40void _cucul_get_ansi(cucul_t *qq, struct cucul_buffer *ex)
41{
42    static int const palette[] =
43    {
44        0,  4,  2,  6, 1,  5,  3,  7,
45        8, 12, 10, 14, 9, 13, 11, 15
46    };
47
48    char *cur;
49    unsigned int x, y;
50
51    /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
52     * 4 max bytes for a UTF-8 character).
53     * Add height*9 to that (zeroes color at the end and jump to next line) */
54    ex->size = (qq->height * 9) + (qq->width * qq->height * 23);
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(fg != prevfg || bg != prevbg)
74            {
75                cur += sprintf(cur, "\033[0;");
76
77                if(fg < 8)
78                    if(bg < 8)
79                        cur += sprintf(cur, "3%d;4%dm", fg, bg);
80                    else
81                        cur += sprintf(cur, "5;3%d;4%d;10%dm",
82                                            fg, bg - 8, bg - 8);
83                else
84                    if(bg < 8)
85                        cur += sprintf(cur, "1;3%d;4%d;9%dm",
86                                            fg - 8, bg, fg - 8);
87                    else
88                        cur += sprintf(cur, "5;1;3%d;4%d;9%d;10%dm",
89                                            fg - 8, bg - 8, fg - 8, bg - 8);
90            }
91
92            *cur++ = c & 0x7f;
93
94            prevfg = fg;
95            prevbg = bg;
96        }
97
98        cur += sprintf(cur, "\033[0m\r\n");
99    }
100
101    /* Crop to really used size */
102    ex->size = (uintptr_t)(cur - ex->data);
103    ex->data = realloc(ex->data, ex->size);
104}
105
Note: See TracBrowser for help on using the repository browser.