Selaa lähdekoodia

Added HTML and IRC output support

tags/v0.99.beta14
Jean-Yves Lamoureux jylam 19 vuotta sitten
vanhempi
commit
6db4ae0857
2 muutettua tiedostoa jossa 217 lisäystä ja 0 poistoa
  1. +9
    -0
      src/caca.h
  2. +208
    -0
      src/graphics.c

+ 9
- 0
src/caca.h Näytä tiedosto

@@ -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


+ 208
- 0
src/graphics.c Näytä tiedosto

@@ -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, "<html>\n<head>\n<title>Generated by libcaca "VERSION"</title>\n");
/* CSS */
sprintf(buffer, "%s<style>\n", buffer);
sprintf(buffer, "%s.caca { font-family: monospace, fixed; font-weight: bold; }\n", buffer);

for(f=0;f<0x0f+1;f++)
{
for(b=0;b<0x0f+1;b++)
{
sprintf(buffer,
"%s.b%x%x { color:#%06x; background-color:#%06x;}\n",buffer,
b, f, html_palette[f], html_palette[b]);

}
}
sprintf(buffer, "%s</style>\n</head>\n<body>\n",buffer);

/* Table */
sprintf(buffer, "%s<table class='caca' cols='%d' cellpadding='0' cellspacing='0'>\n", buffer,caca_get_height());

for(y=0;y<_caca_height;y++)
{
sprintf(buffer,
"%s<tr>",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<td class=b%x%x>%c</td>",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<td class=b%x%x colspan=%d>",buffer,
cache_attr[x+y*caca_get_width()]>> 4,
cache_attr[x+y*caca_get_width()]&0x0f,
len+1);
for(i=0;i<len;i++)
{
if(cache_char[x+y*caca_get_width()]!=' ')
sprintf(buffer, "%s%c", buffer,cache_char[x+y*caca_get_width()]);
else
sprintf(buffer, "%s&nbsp;",buffer);
x++;
}
sprintf(buffer, "%s</td>",buffer);
}

}
sprintf(buffer, "%s</tr>\n",buffer);
}

/* Footer */
sprintf(buffer, "%s</table>\n</body>\n</html>\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;
}

Ladataan…
Peruuta
Tallenna