Changeset 496


Ignore:
Timestamp:
Jul 13, 2005, 7:58:58 PM (16 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • src/graphics.c src/caca.h : Added ANSI exporter
Location:
libcaca/trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/caca.h

    r495 r496  
    365365char* caca_get_html3(void);
    366366char* caca_get_irc(void);
     367char* caca_get_ANSI(int trailing);
    367368/*  @} */
    368369
  • libcaca/trunk/src/graphics.c

    r493 r496  
    20222022}
    20232023
     2024/** \brief Generate ANSI representation of current image.
     2025 *
     2026 *  This function generates and returns an ANSI representation of
     2027 *  the current image.
     2028 *  \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be able to cut/paste the result to a function like printf
     2029 *  \return buffer containing generated ANSI codes as a big string
     2030 */
     2031char* caca_get_ANSI(int trailing)
     2032{
     2033   static int const palette[] =
     2034    {
     2035      30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
     2036      30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
     2037    };
     2038 
     2039    char *buffer, *cur;
     2040    unsigned int x, y;
     2041 
     2042    /* 20 bytes assumed for max length per pixel.
     2043       Add height*9 to that (zeroes color at the end and jump to next line)
     2044     */
     2045    buffer = malloc(((_caca_height*9) + (_caca_width * _caca_height * 20)) * sizeof(char));
     2046    cur = buffer;
     2047
     2048    // *cur++ = '';
     2049
     2050    for(y = 0; y < _caca_height; y++)
     2051    {
     2052        uint8_t *lineattr = cache_attr + y * _caca_width;
     2053        uint8_t *linechar = cache_char + y * _caca_width;
     2054
     2055        uint8_t prevfg = -1;
     2056        uint8_t prevbg = -1;
     2057
     2058        for(x = 0; x < _caca_width; x++)
     2059        {
     2060            uint8_t fg = palette[lineattr[x] & 0x0f];
     2061            uint8_t bg = (palette[lineattr[x] >> 4])+10;
     2062            uint8_t c = linechar[x];
     2063
     2064            if(!trailing)
     2065              cur += sprintf(cur, "\033[");
     2066            else
     2067              cur += sprintf(cur, "\\033[");
     2068 
     2069            if(fg>7)
     2070              cur += sprintf(cur, "1;%d;%dm",fg,bg);
     2071            else
     2072              cur += sprintf(cur, "0;%d;%dm",fg,bg);
     2073            *cur++ = c;
     2074            if((c=='%') && trailing)
     2075              *cur++=c;
     2076            prevfg = fg;
     2077            prevbg = bg;
     2078        }
     2079        if(!trailing)
     2080          cur += sprintf(cur, "\033[0m\n\r");
     2081        else
     2082          cur += sprintf(cur, "\\033[0m\\n\n");
     2083    }
     2084    /* Crop to really used size */
     2085    buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
     2086
     2087    return buffer;
     2088
     2089}
Note: See TracChangeset for help on using the changeset viewer.