Changeset 485


Ignore:
Timestamp:
06/21/05 16:50:43 (8 years ago)
Author:
jylam
Message:

Added HTML and IRC output support

Location:
libcaca/trunk/src
Files:
2 edited

Legend:

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

    r464 r485  
    353353/*  @} */ 
    354354 
     355/** \defgroup exporter Exporters to various formats 
     356 * 
     357 *  These functions exports current image to various text formats 
     358 * 
     359 *  @{ */ 
     360char* caca_get_html(void); 
     361char* caca_get_irc(void); 
     362/*  @} */ 
     363 
    355364#ifdef __cplusplus 
    356365} 
  • libcaca/trunk/src/graphics.c

    r483 r485  
    17161716#endif 
    17171717 
     1718/* Exporters */ 
     1719 
     1720 
     1721/* HTML */ 
     1722static int const html_palette[] = 
     1723{ 
     1724    0, 
     1725    0x00007F, 
     1726    0x007F00, 
     1727    0x007F7F, 
     1728    0x7F0000, 
     1729    0x7F007F, 
     1730    0x7F7F00, 
     1731    0x7F7F7F, 
     1732    0, 
     1733    0x0000FF, 
     1734    0x00FF00, 
     1735    0x00FFFF, 
     1736    0xFF0000, 
     1737    0xFF00FF, 
     1738    0xFFFF00, 
     1739    0xFFFFFF, 
     1740 
     1741}; 
     1742 
     1743/** \brief Generate HTML representation of current image. 
     1744 * 
     1745 *  This function generates and returns the HTML representation of  
     1746 *  the current image. 
     1747 * 
     1748 */ 
     1749char* caca_get_html(void) 
     1750{ 
     1751  char *buffer; 
     1752  unsigned int x,y; 
     1753  int f,b; 
     1754 
     1755  /* 13000 -> css palette 
     1756     40 -> max size used for a pixel (plus 10, never know)*/ 
     1757 
     1758  buffer = malloc((13000 + ((_caca_width*_caca_height)*40))*sizeof(char)); 
     1759   
     1760  /* HTML header */ 
     1761  sprintf(buffer, "<html>\n<head>\n<title>Generated by libcaca "VERSION"</title>\n"); 
     1762  /* CSS */ 
     1763  sprintf(buffer, "%s<style>\n", buffer); 
     1764  sprintf(buffer, "%s.caca { font-family: monospace, fixed; font-weight: bold; }\n", buffer); 
     1765 
     1766  for(f=0;f<0x0f+1;f++) 
     1767    { 
     1768      for(b=0;b<0x0f+1;b++) 
     1769        { 
     1770          sprintf(buffer,  
     1771                  "%s.b%x%x { color:#%06x; background-color:#%06x;}\n",buffer, 
     1772                  b, f, html_palette[f], html_palette[b]); 
     1773 
     1774        } 
     1775    } 
     1776  sprintf(buffer, "%s</style>\n</head>\n<body>\n",buffer); 
     1777 
     1778  /* Table */ 
     1779  sprintf(buffer, "%s<table class='caca' cols='%d' cellpadding='0' cellspacing='0'>\n", buffer,caca_get_height()); 
     1780 
     1781  for(y=0;y<_caca_height;y++) 
     1782    { 
     1783      sprintf(buffer,  
     1784              "%s<tr>",buffer); 
     1785 
     1786      for(x=0;x<_caca_width;x++) 
     1787        { 
     1788          int len; 
     1789          int i; 
     1790          uint8_t *attr = cache_attr + x + y * _caca_width; 
     1791 
     1792          /* Use colspan option to factorize cells with same attributes  
     1793             (see below) */ 
     1794          len=1; 
     1795          while(x + len < _caca_width 
     1796                && (attr[len]>>4) == (attr[0]>>4) && 
     1797                (attr[len]&0x0f) == (attr[0]&0x0f)) 
     1798            len++; 
     1799           
     1800          if(len==1) 
     1801            { 
     1802              sprintf(buffer,  
     1803                      "%s<td class=b%x%x>%c</td>",buffer,  
     1804                      cache_attr[x+y*caca_get_width()]>>4,  
     1805                      cache_attr[x+y*caca_get_width()]&0x0f, 
     1806                      cache_char[x+y*caca_get_width()]); 
     1807            } 
     1808          else 
     1809            { 
     1810              sprintf(buffer,  
     1811                      "%s<td class=b%x%x colspan=%d>",buffer, 
     1812                      cache_attr[x+y*caca_get_width()]>> 4,  
     1813                      cache_attr[x+y*caca_get_width()]&0x0f, 
     1814                      len+1); 
     1815               
     1816              for(i=0;i<len;i++) 
     1817                { 
     1818                  if(cache_char[x+y*caca_get_width()]!=' ') 
     1819                    sprintf(buffer, "%s%c", buffer,cache_char[x+y*caca_get_width()]); 
     1820                  else 
     1821                    sprintf(buffer, "%s&nbsp;",buffer); 
     1822                  x++; 
     1823                } 
     1824              sprintf(buffer, "%s</td>",buffer); 
     1825               
     1826            } 
     1827 
     1828        } 
     1829      sprintf(buffer, "%s</tr>\n",buffer); 
     1830    } 
     1831 
     1832  /* Footer */ 
     1833  sprintf(buffer, "%s</table>\n</body>\n</html>\n",buffer); 
     1834 
     1835  /* Crop to really used size */ 
     1836  buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); 
     1837 
     1838  return buffer; 
     1839} 
     1840 
     1841 
     1842static int const irc_palette[] = 
     1843{ 
     1844  /* Dark */ 
     1845  1, 
     1846  2, 
     1847  3, 
     1848  10, 
     1849  5, 
     1850  6, 
     1851  7, 
     1852  14, 
     1853  /* Light */ 
     1854  1, 
     1855  12, 
     1856  9, 
     1857  11, 
     1858  4, 
     1859  13, 
     1860  8, 
     1861  16, 
     1862}; 
     1863 
     1864 
     1865/** \brief Generate IRC representation of current image. 
     1866 * 
     1867 *  This function generates and returns an IRC representation of  
     1868 *  the current image. 
     1869 *  This is XChat-like format, %Cf,b  with f the foreground color, 
     1870 *  and b the background color. 
     1871 * 
     1872 */ 
     1873char* caca_get_irc(void) 
     1874{ 
     1875  char *buffer; 
     1876  unsigned int x, y; 
     1877  /* 15 bytes assumed for max length per pixel */ 
     1878  buffer = malloc(((_caca_width*_caca_height*15)+1)*sizeof(char)); 
     1879  sprintf(buffer,"%%O"); 
     1880 
     1881  for(y=0;y<_caca_height;y++) 
     1882    { 
     1883      for(x=0;x<_caca_width;x++) 
     1884        { 
     1885          if(cache_char[x+y*caca_get_width()] == ' ') 
     1886            { 
     1887              sprintf(buffer,  
     1888                      "%s%%C%d,%d%c", buffer, 
     1889                      irc_palette[cache_attr[x+y*caca_get_width()]>>4], 
     1890                      irc_palette[cache_attr[x+y*caca_get_width()]>>4],  
     1891                      '#'); 
     1892            } 
     1893          else if(cache_char[x+y*caca_get_width()] == '%') 
     1894            { 
     1895             sprintf(buffer, 
     1896                    "%s%%C%d,%d%%%%",  buffer, 
     1897                    irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], 
     1898                    irc_palette[cache_attr[x+y*caca_get_width()]>> 4]); 
     1899            } 
     1900          else if(cache_char[x+y*caca_get_width()]>='0' && cache_char[x+y*caca_get_width()]<='9') 
     1901            { 
     1902              sprintf(buffer,  
     1903                      "%s%%C%d,%d%%B%%B%c", buffer, 
     1904                      irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], 
     1905                      irc_palette[cache_attr[x+y*caca_get_width()]>> 4], 
     1906                      cache_char[x+y*caca_get_width()]); 
     1907            } 
     1908      else 
     1909        { 
     1910          sprintf(buffer,  
     1911                  "%s%%C%d,%d%c", buffer, 
     1912                  irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], 
     1913                  irc_palette[cache_attr[x+y*caca_get_width()]>> 4], 
     1914                  cache_char[x+y*caca_get_width()]); 
     1915            } 
     1916 
     1917        } 
     1918      sprintf(buffer, "%s\n", buffer); 
     1919    } 
     1920  
     1921  /* Crop to really used size */ 
     1922  buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); 
     1923 
     1924  return buffer; 
     1925} 
Note: See TracChangeset for help on using the changeset viewer.