/*- * Copyright (c) 2001 Nik Clayton * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char rcsid[] = "$Id: scr2txt.c,v 1.2 2001/05/20 14:40:51 nik Exp $"; #endif /* not lint */ #include #include #include #include #include #include #include #include #define MAGIC "SCRSHOT_" #define VERSION 1 static int color2ansi[] = { 30, /* Black */ 34, /* Blue */ 32, /* Green */ 36, /* Cyan */ 31, /* Red */ 35, /* Magenta */ 33, /* Brown */ 37 /* White */ }; int main(int argc, char *argv[]) { int mx, my; /* Maxmimum X and Y images positions */ int cx; /* Current X and Y images positions */ int curChar; /* Current character to render */ int curColor; /* Index in to color2ansi[] */ char header[BUFSIZ]; /* Buffer for header components */ int ansi = 0; /* Use ANSI escape sequences? */ int fake_line = 0; /* Fake the line drawing characters? */ int ch; /* Argument option code */ /* * Option processing */ while ((ch = getopt(argc, argv, "al")) != -1) switch (ch) { case 'a': ansi = 1; break; case 'l': fake_line = 1; break; } argc -= optind; argv += optind; /* * Read the SCRSHOT_ header, confirm the version, and read the image * dimensions. */ bzero(header, BUFSIZ); if(fread(header, sizeof(unsigned char), 8, stdin) != 8) err(1, "fread() header bytes"); if(strncmp(MAGIC, header, strlen(MAGIC)) != 0) err(1, "Expecting '%s', read '%s'", MAGIC, header); header[0] = getchar(); if(header[0] != VERSION) fprintf(stderr, "Expecting version %d, read %d\n", VERSION, header[0]); header[0] = getchar(); if(header[0] == EOF) err(1, "Received EOF when reading header length"); if(header[0] < 2) err(1, "Header length is less than 2 ('%d')", header[0]); mx = getchar(); header[0]--; if(mx == EOF) err(1, "Received EOF when reading image width"); getchar(); header[0]--; if(my == EOF) err(1, "Received EOF when reading image height"); /* * Don't know what to do with any other data in the header, * so skip it. */ while(header[0]-- != 0) getchar(); /* * Read from STDIN, rendering on to the image */ cx = 0; while((curChar = getchar()) != EOF) { curColor = getchar(); if(ansi) { int fg = curColor & 0x0F; int bg = (curColor & 0xF0) >> 4; int bold = 0; bold = fg & 0x08; fg &= 0x07; bg &= 0x07; printf("\e[%s%d;%dm", bold ? "1;" : "", color2ansi[fg], color2ansi[bg] + 10); } if(fake_line) switch (curChar) { case 0xb3: /* Single vertical bar */ case 0xba: /* Double vertical bar */ curChar = '|'; break; case 0xb4: /* Horizontal single left T */ case 0xb9: /* Horizontal double left T */ case 0xc1: /* Single inverted T */ case 0xc2: /* Single T */ case 0xc3: /* Horizontal single right T */ case 0xc5: /* Single cross */ case 0xca: /* Double inverted T */ case 0xcb: /* Double T */ case 0xcc: /* Horizontal left T */ case 0xce: /* Double cross */ curChar = '+'; break; case 0xbb: /* Double top right corner */ case 0xbf: /* Single top right corner */ case 0xc9: /* Double top left */ case 0xda: /* Single top left corner */ curChar = '.'; break; case 0xc0: /* Single bottom left corner */ case 0xc8: /* Double bottom left corner */ curChar = '`'; break; case 0xc4: /* Single horizontal bar */ case 0xcd: /* Double horizontal bar */ curChar = '-'; break; case 0xbc: /* Double bottom right corner */ case 0xd9: /* Single bottom right corner */ curChar = '\''; break; } putchar(curChar); if(ansi) printf("\e[0m"); /* New line? */ if(++cx >= mx) { cx = 0; putchar('\n'); } } exit(0); }