Changeset 491
- Timestamp:
- Jun 24, 2005, 1:48:31 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/src/graphics.c
r490 r491 266 266 int id[94]; 267 267 unsigned char gl_resized=0, gl_bit=0; 268 #endif 268 #endif 269 269 270 270 … … 320 320 gl_new_width = w; 321 321 gl_new_height = h; 322 322 323 323 gl_resized = 1; 324 324 } 325 325 else 326 326 gl_bit=1; 327 327 328 328 } 329 329 #endif … … 984 984 sscanf(getenv("CACA_GEOMETRY"), 985 985 "%ux%u", &_caca_width, &_caca_height); 986 986 987 987 if(!_caca_width) 988 988 _caca_width = 80; … … 994 994 995 995 gl_width = _caca_width*gl_font_width; 996 gl_height = _caca_height*gl_font_height; 997 998 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 999 glutInitWindowSize(gl_width, gl_height); 996 gl_height = _caca_height*gl_font_height; 997 998 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 999 glutInitWindowSize(gl_width, gl_height); 1000 1000 gl_window = glutCreateWindow("caca for GL"); 1001 1001 … … 1003 1003 1004 1004 glDisable(GL_CULL_FACE); 1005 glDisable(GL_DEPTH_TEST); 1005 glDisable(GL_DEPTH_TEST); 1006 1006 1007 1007 glutKeyboardFunc(gl_handle_keyboard); … … 1029 1029 memset(empty, 255, 16*16*4); 1030 1030 glEnable(GL_TEXTURE_2D); 1031 1031 1032 1032 for(i=0;i<94;i++) 1033 1033 { 1034 glGenTextures(1,&id[i]); 1034 glGenTextures(1,&id[i]); 1035 1035 glBindTexture(GL_TEXTURE_2D, id[i]); 1036 1036 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); … … 1042 1042 for(i=0;i<94;i++) 1043 1043 { 1044 1044 1045 1045 glDisable(GL_TEXTURE_2D); 1046 1046 glClear(GL_COLOR_BUFFER_BIT); … … 1048 1048 glColor3f(1,1,1); 1049 1049 glRasterPos2f(0,15); 1050 glutBitmapCharacter(GLUT_BITMAP_9_BY_15,i+32); 1051 1050 glutBitmapCharacter(GLUT_BITMAP_9_BY_15,i+32); 1051 1052 1052 1053 1053 glEnable(GL_TEXTURE_2D); 1054 glBindTexture(GL_TEXTURE_2D,id[i]); 1054 glBindTexture(GL_TEXTURE_2D,id[i]); 1055 1055 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, gl_height-16, 16,16, 0); 1056 1056 1057 1057 glutMainLoopEvent(); 1058 1058 glutPostRedisplay(); … … 1066 1066 if(getenv("CACA_GEOMETRY") && *(getenv("CACA_GEOMETRY"))) 1067 1067 sscanf(getenv("CACA_GEOMETRY"), 1068 "%ux%u", &_caca_width, &_caca_height); 1068 "%ux%u", &_caca_width, &_caca_height); 1069 1069 if(!_caca_width) 1070 1070 _caca_width = 80; … … 1456 1456 { 1457 1457 unsigned int x, y, offsetx, offsety; 1458 1458 1459 1459 1460 1460 glClear(GL_COLOR_BUFFER_BIT); 1461 1461 1462 1462 offsety=0; 1463 1463 for(y=0;y<gl_height;y+=gl_font_height) … … 1474 1474 bg = ((gl_bg_palette[offset]&0x0000FF00)>>8)/255.0f; 1475 1475 bb = ((gl_bg_palette[offset]&0x000000FF))/255.0f; 1476 1476 1477 1477 glDisable(GL_TEXTURE_2D); 1478 1478 glColor3f(br, bg, bb); … … 1483 1483 glVertex2f(x,y+gl_font_height); 1484 1484 glEnd(); 1485 1485 1486 1486 1487 1487 offsetx++; 1488 } 1488 } 1489 1489 1490 1490 offsety++; … … 1497 1497 glEnable(GL_TEXTURE_2D); 1498 1498 glBlendFunc(GL_ONE, GL_ONE); 1499 1499 1500 1500 offsety=0; 1501 1501 for(y=0;y<gl_height;y+=gl_font_height) … … 1507 1507 unsigned char *chr = cache_char + offsetx + offsety * _caca_width; 1508 1508 float fr, fg, fb; 1509 1510 1509 1510 1511 1511 fr = ((gl_bg_palette[attr[0] & 0xf]&0x00FF0000)>>16)/255.0f; 1512 1512 fg = ((gl_bg_palette[attr[0] & 0xf]&0x0000FF00)>>8)/255.0f; … … 1516 1516 { 1517 1517 glBindTexture(GL_TEXTURE_2D, id[chr[0]-32]); 1518 1518 1519 1519 glColor3f(fr, fg, fb); 1520 1520 glBegin(GL_QUADS); … … 1527 1527 glTexCoord2f(0,0); 1528 1528 glVertex2f(x,y+gl_font_height); 1529 glEnd(); 1529 glEnd(); 1530 1530 } 1531 1531 offsetx++; … … 1656 1656 gl_width = gl_new_width; 1657 1657 gl_height = gl_new_height; 1658 1658 1659 1659 _caca_width = gl_width/gl_font_width; 1660 1660 _caca_height = (gl_height/gl_font_height)+1; 1661 1661 1662 1662 glMatrixMode(GL_PROJECTION); 1663 1663 glPushMatrix(); 1664 1664 glLoadIdentity(); 1665 1665 1666 1666 glViewport(0,0,gl_width, gl_height); 1667 gluOrtho2D(0, gl_width, gl_height, 0); 1667 gluOrtho2D(0, gl_width, gl_height, 0); 1668 1668 glMatrixMode(GL_MODELVIEW); 1669 1669 … … 1771 1771 /* Exporters */ 1772 1772 1773 1774 1773 /* HTML */ 1775 static int const html_palette[] =1776 {1777 0,1778 0x007,1779 0x070,1780 0x077,1781 0x700,1782 0x707,1783 0x770,1784 0x777,1785 0,1786 0x00F,1787 0x0F0,1788 0x0FF,1789 0xF00,1790 0xF0F,1791 0xFF0,1792 0xFFF,1793 1794 };1795 1774 1796 1775 /** \brief Generate HTML representation of current image. 1797 1776 * 1798 * This function generates and returns the HTML representation of 1777 * This function generates and returns the HTML representation of 1799 1778 * the current image. 1800 *1801 1779 */ 1802 1780 char* caca_get_html(void) 1803 1781 { 1782 static int const palette[] = 1783 { 1784 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888, 1785 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff, 1786 }; 1804 1787 char *buffer, *cur; 1805 1788 unsigned int x, y, len; … … 1820 1803 { 1821 1804 cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n", 1822 x, html_palette[x & 0xf ], html_palette[x >> 4]);1805 x, palette[x & 0xf ], palette[x >> 4]); 1823 1806 } 1824 1807 cur += sprintf(cur, "</style>\n</head>\n<body>\n"); … … 1829 1812 for(y = 0; y < _caca_height; y++) 1830 1813 { 1814 uint8_t *lineattr = cache_attr + y * _caca_width; 1815 uint8_t *linechar = cache_char + y * _caca_width; 1816 1831 1817 for(x = 0; x < _caca_width; x += len) 1832 1818 { 1833 uint8_t *cur_attr = cache_attr + y * _caca_width; 1834 uint8_t *cur_char = cache_char + y * _caca_width; 1835 1836 cur += sprintf(cur, "<span class='b%02x'>", cur_attr[x]); 1837 1838 for(len = 0; 1839 x + len < _caca_width && cur_attr[x + len] == cur_attr[x]; 1819 cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]); 1820 1821 for(len = 0; 1822 x + len < _caca_width && lineattr[x + len] == lineattr[x]; 1840 1823 len++) 1841 1824 { 1842 if( cur_char[x + len] == ' ')1825 if(linechar[x + len] == ' ') 1843 1826 cur += sprintf(cur, " "); 1844 1827 else 1845 cur += sprintf(cur, "%c", cur_char[x + len]);1828 cur += sprintf(cur, "%c", linechar[x + len]); 1846 1829 } 1847 1830 cur += sprintf(cur, "</span>"); … … 1861 1844 /** \brief Generate HTML3 representation of current image. 1862 1845 * 1863 * This function generates and returns the HTML3 representation of 1846 * This function generates and returns the HTML3 representation of 1864 1847 * the current image. It is way bigger than caca_get_html(), but 1865 1848 * permits viewing in old browsers (or limited ones such as links) 1866 * Won't work under gecko (mozilla rendering engine) unless you set 1849 * Won't work under gecko (mozilla rendering engine) unless you set 1867 1850 * a correct header. 1868 1851 */ 1869 1852 char* caca_get_html3(void) 1870 1853 { 1871 char *buffer, *cur; 1872 unsigned int x,y; 1873 1874 /* 13000 -> css palette 1875 40 -> max size used for a pixel (plus 10, never know)*/ 1876 1877 buffer = malloc((13000 + ((_caca_width*_caca_height)*40))*sizeof(char)); 1878 cur = buffer; 1879 1880 /* Table */ 1881 cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n", caca_get_height()); 1882 1883 for(y=0;y<_caca_height;y++) 1884 { 1885 cur += sprintf(cur, "<tr>"); 1886 1887 for(x=0;x<_caca_width;x++) 1888 { 1889 int len; 1890 int i; 1891 uint8_t *attr = cache_attr + x + y * _caca_width; 1892 1893 /* Use colspan option to factorize cells with same attributes 1894 (see below) */ 1895 len=1; 1896 while(x + len < _caca_width 1897 && (attr[len]>>4) == (attr[0]>>4) && 1898 (attr[len]&0x0f) == (attr[0]&0x0f)) 1899 len++; 1900 1901 if(len==1) 1902 { 1903 cur += sprintf(cur, 1904 "<td bgcolor=#%03X ><font color=#%03X>%c</font></td>", 1905 html_palette[cache_attr[x+y*caca_get_width()]>>4], 1906 html_palette[cache_attr[x+y*caca_get_width()]&0x0f], 1907 cache_char[x+y*caca_get_width()]); 1908 } 1909 else 1910 { 1911 cur += sprintf(cur, 1912 "<td bgcolor=#%03X colspan=%d><font color=#%03X>", 1913 html_palette[cache_attr[x+y*caca_get_width()]>> 4], 1914 len+1, 1915 html_palette[cache_attr[x+y*caca_get_width()]&0x0f]); 1916 1917 for(i=0;i<len;i++) 1918 { 1919 if(cache_char[x+y*caca_get_width()]!=' ') 1920 cur += sprintf(cur, "%c", cache_char[x+y*caca_get_width()]); 1921 else 1922 cur += sprintf(cur, " "); 1923 x++; 1924 } 1925 cur += sprintf(cur, "</font></td>"); 1926 1927 } 1928 1929 } 1930 cur += sprintf(cur, "</tr>\n"); 1931 } 1932 1933 /* Footer */ 1934 cur += sprintf(cur, "</table>\n"); 1935 1936 /* Crop to really used size */ 1937 buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); 1938 1939 return buffer; 1940 } 1941 1942 1943 static int const irc_palette[] = 1944 { 1945 /* Dark */ 1946 1, 2, 3, 10, 5, 6, 7, 14, 1947 /* Light */ 1948 1, 12, 9, 11, 4, 13, 8, 16, 1949 }; 1950 1854 static int const palette[] = 1855 { 1856 0x000000, 0x000088, 0x008800, 0x008888, 1857 0x880000, 0x880088, 0x888800, 0x888888, 1858 0x444444, 0x4444ff, 0x44ff44, 0x44ffff, 1859 0xff4444, 0xff44ff, 0xffff44, 0xffffff, 1860 }; 1861 char *buffer, *cur; 1862 unsigned int x, y, len; 1863 1864 /* 13000 -> css palette 1865 * 40 -> max size used for a pixel (plus 10, never know) */ 1866 buffer = malloc((13000 + ((_caca_width*_caca_height)*40))*sizeof(char)); 1867 cur = buffer; 1868 1869 /* Table */ 1870 cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n", 1871 _caca_height); 1872 1873 for(y = 0; y < _caca_height; y++) 1874 { 1875 uint8_t *lineattr = cache_attr + y * _caca_width; 1876 uint8_t *linechar = cache_char + y * _caca_width; 1877 1878 cur += sprintf(cur, "<tr>"); 1879 1880 for(x = 0; x < _caca_width; x += len) 1881 { 1882 int i; 1883 1884 /* Use colspan option to factorize cells with same attributes 1885 * (see below) */ 1886 len = 1; 1887 while(x + len < _caca_width && lineattr[x + len] == lineattr[x]) 1888 len++; 1889 1890 cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]); 1891 1892 if(len > 1) 1893 cur += sprintf(cur, " colspan=%d", len); 1894 1895 cur += sprintf(cur, "><font color=#%06x>", 1896 palette[lineattr[x] & 0x0f]); 1897 1898 for(i = 0; i < len; i++) 1899 { 1900 if(linechar[x + i] == ' ') 1901 cur += sprintf(cur, " "); 1902 else 1903 cur += sprintf(cur, "%c", linechar[x + i]); 1904 } 1905 1906 cur += sprintf(cur, "</font></td>"); 1907 } 1908 cur += sprintf(cur, "</tr>\n"); 1909 } 1910 1911 /* Footer */ 1912 cur += sprintf(cur, "</table>\n"); 1913 1914 /* Crop to really used size */ 1915 buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char)); 1916 1917 return buffer; 1918 } 1951 1919 1952 1920 /** \brief Generate IRC representation of current image. 1953 1921 * 1954 * This function generates and returns an IRC representation of 1922 * This function generates and returns an IRC representation of 1955 1923 * the current image. 1956 * This is XChat-like format, %Cf,b with f the foreground color,1957 * and b the background color.1958 *1959 1924 */ 1960 1925 char* caca_get_irc(void) 1961 1926 { 1962 char *buffer; 1963 unsigned int x, y; 1964 /* 15 bytes assumed for max length per pixel */ 1965 buffer = malloc(((_caca_width*_caca_height*15)+1)*sizeof(char)); 1966 1967 sprintf(buffer, "%%O"); 1968 1969 for(y = 0; y < _caca_height; y++) 1970 { 1971 for(x = 0; x < _caca_width; x++) 1972 { 1973 if(cache_char[x+y*caca_get_width()] == ' ') 1927 static int const palette[] = 1928 { 1929 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */ 1930 14, 12, 9, 11, 4, 13, 8, 0, /* Light */ 1931 }; 1932 1933 char *buffer, *cur; 1934 unsigned int x, y; 1935 1936 /* 11 bytes assumed for max length per pixel. Worst case scenario: 1937 * ^Cxx,yy 6 bytes 1938 * ^B^B 2 bytes 1939 * c 1 byte 1940 * \r\n 2 bytes 1941 * In real life, the average bytes per pixel value will be around 5. 1942 */ 1943 buffer = malloc((2 + (_caca_width * _caca_height * 11)) * sizeof(char)); 1944 cur = buffer; 1945 1946 *cur++ = '\x0f'; 1947 1948 for(y = 0; y < _caca_height; y++) 1949 { 1950 uint8_t *lineattr = cache_attr + y * _caca_width; 1951 uint8_t *linechar = cache_char + y * _caca_width; 1952 1953 uint8_t prevfg = -1; 1954 uint8_t prevbg = -1; 1955 1956 for(x = 0; x < _caca_width; x++) 1957 { 1958 uint8_t fg = palette[lineattr[x] & 0x0f]; 1959 uint8_t bg = palette[lineattr[x] >> 4]; 1960 uint8_t c = linechar[x]; 1961 1962 if(bg == prevbg) 1974 1963 { 1975 sprintf(buffer, 1976 "%s%%C%d,%d%c", buffer, 1977 irc_palette[cache_attr[x+y*caca_get_width()]>>4], 1978 irc_palette[cache_attr[x+y*caca_get_width()]>>4], 1979 '#'); 1964 if(fg == prevfg) 1965 ; /* Same fg/bg, do nothing */ 1966 else if(c == ' ') 1967 fg = prevfg; /* Hackety hack */ 1968 else 1969 { 1970 cur += sprintf(cur, "\x03%d", fg); 1971 if(c >= '0' && c <= '9') 1972 cur += sprintf(cur, "\x02\x02"); 1973 } 1980 1974 } 1981 else if(cache_char[x+y*caca_get_width()] == '%')1975 else 1982 1976 { 1983 sprintf(buffer, 1984 "%s%%C%d,%d%%%%", buffer, 1985 irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], 1986 irc_palette[cache_attr[x+y*caca_get_width()]>> 4]); 1977 if(fg == prevfg) 1978 cur += sprintf(cur, "\x03,%d", bg); 1979 else 1980 cur += sprintf(cur, "\x03%d,%d", fg, bg); 1981 1982 if(c >= '0' && c <= '9') 1983 cur += sprintf(cur, "\x02\x02"); 1987 1984 } 1988 else if(cache_char[x+y*caca_get_width()]>='0' && cache_char[x+y*caca_get_width()]<='9') 1989 { 1990 sprintf(buffer, 1991 "%s%%C%d,%d%%B%%B%c", buffer, 1992 irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], 1993 irc_palette[cache_attr[x+y*caca_get_width()]>> 4], 1994 cache_char[x+y*caca_get_width()]); 1995 } 1996 else 1997 { 1998 sprintf(buffer, 1999 "%s%%C%d,%d%c", buffer, 2000 irc_palette[cache_attr[x+y*caca_get_width()]&0x0f], 2001 irc_palette[cache_attr[x+y*caca_get_width()]>> 4], 2002 cache_char[x+y*caca_get_width()]); 2003 } 2004 2005 } 2006 sprintf(buffer, "%s\n", buffer); 2007 } 2008 2009 /* Crop to really used size */ 2010 buffer = realloc(buffer, (strlen(buffer)+1) * sizeof(char)); 2011 2012 return buffer; 2013 } 1985 *cur++ = c; 1986 prevfg = fg; 1987 prevbg = bg; 1988 } 1989 *cur++ = '\r'; 1990 *cur++ = '\n'; 1991 } 1992 1993 *cur++ = '\x0f'; 1994 1995 /* Crop to really used size */ 1996 buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char)); 1997 1998 return buffer; 1999 } 2000
Note: See TracChangeset
for help on using the changeset viewer.