diff --git a/src/caca.h b/src/caca.h index 38b40ff..8a5c6e3 100644 --- a/src/caca.h +++ b/src/caca.h @@ -352,6 +352,15 @@ void caca_draw_bitmap(int, int, int, int, struct caca_bitmap const *, void *); void caca_free_bitmap(struct caca_bitmap *); /* @} */ +/** \defgroup exporter Exporters to various formats + * + * These functions exports current image to various text formats + * + * @{ */ +char* caca_get_html(void); +char* caca_get_irc(void); +/* @} */ + #ifdef __cplusplus } #endif diff --git a/src/graphics.c b/src/graphics.c index 0b25da6..8ed3b0c 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1715,3 +1715,211 @@ static RETSIGTYPE sigwinch_handler(int sig) } #endif +/* Exporters */ + + +/* HTML */ +static int const html_palette[] = +{ + 0, + 0x00007F, + 0x007F00, + 0x007F7F, + 0x7F0000, + 0x7F007F, + 0x7F7F00, + 0x7F7F7F, + 0, + 0x0000FF, + 0x00FF00, + 0x00FFFF, + 0xFF0000, + 0xFF00FF, + 0xFFFF00, + 0xFFFFFF, + +}; + +/** \brief Generate HTML representation of current image. + * + * This function generates and returns the HTML representation of + * the current image. + * + */ +char* caca_get_html(void) +{ + char *buffer; + unsigned int x,y; + int f,b; + + /* 13000 -> css palette + 40 -> max size used for a pixel (plus 10, never know)*/ + + buffer = malloc((13000 + ((_caca_width*_caca_height)*40))*sizeof(char)); + + /* HTML header */ + sprintf(buffer, "\n\nGenerated by libcaca "VERSION"\n"); + /* CSS */ + sprintf(buffer, "%s\n\n\n",buffer); + + /* Table */ + sprintf(buffer, "%s\n", buffer,caca_get_height()); + + for(y=0;y<_caca_height;y++) + { + sprintf(buffer, + "%s",buffer); + + for(x=0;x<_caca_width;x++) + { + int len; + int i; + uint8_t *attr = cache_attr + x + y * _caca_width; + + /* Use colspan option to factorize cells with same attributes + (see below) */ + len=1; + while(x + len < _caca_width + && (attr[len]>>4) == (attr[0]>>4) && + (attr[len]&0x0f) == (attr[0]&0x0f)) + len++; + + if(len==1) + { + sprintf(buffer, + "%s",buffer, + cache_attr[x+y*caca_get_width()]>>4, + cache_attr[x+y*caca_get_width()]&0x0f, + cache_char[x+y*caca_get_width()]); + } + else + { + sprintf(buffer, + "%s\n",buffer); + } + + /* Footer */ + sprintf(buffer, "%s
%c",buffer, + cache_attr[x+y*caca_get_width()]>> 4, + cache_attr[x+y*caca_get_width()]&0x0f, + len+1); + + for(i=0;i",buffer); + + } + + } + sprintf(buffer, "%s
\n\n\n",buffer); + + /* Crop to really used size */ + buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); + + return buffer; +} + + +static int const irc_palette[] = +{ + /* Dark */ + 1, + 2, + 3, + 10, + 5, + 6, + 7, + 14, + /* Light */ + 1, + 12, + 9, + 11, + 4, + 13, + 8, + 16, +}; + + +/** \brief Generate IRC representation of current image. + * + * This function generates and returns an IRC representation of + * the current image. + * This is XChat-like format, %Cf,b with f the foreground color, + * and b the background color. + * + */ +char* caca_get_irc(void) +{ + char *buffer; + unsigned int x, y; + /* 15 bytes assumed for max length per pixel */ + buffer = malloc(((_caca_width*_caca_height*15)+1)*sizeof(char)); + sprintf(buffer,"%%O"); + + for(y=0;y<_caca_height;y++) + { + for(x=0;x<_caca_width;x++) + { + if(cache_char[x+y*caca_get_width()] == ' ') + { + sprintf(buffer, + "%s%%C%d,%d%c", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]>>4], + irc_palette[cache_attr[x+y*caca_get_width()]>>4], + '#'); + } + else if(cache_char[x+y*caca_get_width()] == '%') + { + sprintf(buffer, + "%s%%C%d,%d%%%%", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], + irc_palette[cache_attr[x+y*caca_get_width()]>> 4]); + } + else if(cache_char[x+y*caca_get_width()]>='0' && cache_char[x+y*caca_get_width()]<='9') + { + sprintf(buffer, + "%s%%C%d,%d%%B%%B%c", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], + irc_palette[cache_attr[x+y*caca_get_width()]>> 4], + cache_char[x+y*caca_get_width()]); + } + else + { + sprintf(buffer, + "%s%%C%d,%d%c", buffer, + irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], + irc_palette[cache_attr[x+y*caca_get_width()]>> 4], + cache_char[x+y*caca_get_width()]); + } + + } + sprintf(buffer, "%s\n", buffer); + } + + /* Crop to really used size */ + buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); + + return buffer; +}