Changeset 4235
- Timestamp:
- Jan 6, 2010, 9:56:29 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca/codec/export.c
r4228 r4235 982 982 } 983 983 984 /* Get troff color index from RGB 985 * 986 * We want for each component to know if it is >= 80 987 * Then we use the 3 bits to index colors list: 988 * 000 -> black 989 * 001 -> blue 990 * ... 991 * 111 -> white 992 */ 993 static uint8_t _rgb_to_troff_index(uint32_t color) 994 { 995 int i; 996 uint8_t r = 0; 997 for(i = 0; i < 3; i++) 998 { 999 r += ((color >> (8*i+7))&1) << i; 1000 } 1001 return r; 1002 } 1003 984 1004 /* Generate troff representation of current canvas. */ 985 1005 static void *export_troff(caca_canvas_t const *cv, size_t *bytes) … … 987 1007 char *data, *cur; 988 1008 int x, y; 1009 1010 const char * colors[8] = { "black", "blue", "green", "cyan", 1011 "red", "magenta", "yellow", "white" }; 989 1012 990 1013 uint32_t prevfg = 0; … … 993 1016 994 1017 /* Each char is at most 995 * 2x.defcolor (2x29) 996 * + 2x\mM (2x10) 1018 * 2x\mM (2x10) 997 1019 * + \fB + \fI + \fR (9) 998 * + 4 bytes = 911020 * + 4 bytes = 33 999 1021 * Each line has a \n (1) 1000 * Header has . color 1\n.nf\n (12)1022 * Header has .nf\n (3) 1001 1023 */ 1002 *bytes = 12 + cv->height + (cv->width * cv->height * 91);1024 *bytes = 3 + cv->height + (cv->width * cv->height * 33); 1003 1025 cur = data = malloc(*bytes); 1004 1005 cur += sprintf(cur, ".color 1\n");1006 1007 for(y = 0; y < cv->height; y++)1008 {1009 uint32_t *lineattr = cv->attrs + y * cv->width;1010 1011 for(x = 0; x < cv->width; x++)1012 {1013 1014 uint32_t fg = _caca_attr_to_rgb24bg(lineattr[x]);1015 uint32_t bg = _caca_attr_to_rgb24fg(lineattr[x]);1016 if(fg != prevfg || !started)1017 cur += sprintf(cur, ".defcolor %.06x rgb #%.06x\n", fg, fg);1018 if(bg != prevbg || !started)1019 cur += sprintf(cur, ".defcolor %.06x rgb #%.06x\n", bg, bg);1020 1021 prevfg = fg;1022 prevbg = bg;1023 started = 1;1024 }1025 }1026 1026 1027 1027 cur += sprintf(cur, ".nf\n"); … … 1038 1038 for(x = 0; x < cv->width; x++) 1039 1039 { 1040 1041 uint32_t fg = _caca_attr_to_rgb24bg(lineattr[x]); 1042 uint32_t bg = _caca_attr_to_rgb24fg(lineattr[x]); 1040 uint32_t fg = _rgb_to_troff_index(_caca_attr_to_rgb24fg(lineattr[x])); 1041 uint32_t bg = _rgb_to_troff_index(_caca_attr_to_rgb24bg(lineattr[x])); 1043 1042 uint32_t ch = linechar[x]; 1044 1043 if(fg != prevfg || !started) 1045 cur += sprintf(cur, "\\m[% .06x]", fg);1044 cur += sprintf(cur, "\\m[%s]", colors[fg]); 1046 1045 if(bg != prevbg || !started) 1047 cur += sprintf(cur, "\\M[% .06x]", bg);1046 cur += sprintf(cur, "\\M[%s]", colors[bg]); 1048 1047 if(lineattr[x] & CACA_BOLD) 1049 1048 cur += sprintf(cur, "\\fB");
Note: See TracChangeset
for help on using the changeset viewer.