Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!ucsfcgl!cca.ucsf.edu!wet!epsilon From: epsilon@wet.UUCP (Eric P. Scott) Newsgroups: comp.sys.next Subject: Poor Man's Icon--a text to tiff converter Message-ID: <281@wet.UUCP> Date: 14 Jul 89 20:21:17 GMT Reply-To: epsilon@wet.UUCP (Eric P. Scott) Organization: Wetware Diversions Public Access UNIX, San Francisco Lines: 135 I needed a more convenient way to produce .tiff files than "mousing them out" in Icon. Here's a quick-and-dirty filter that takes text input like +++++**##**+++++ +++*###**###*+++ ++##*......*##++ +*#..........#*+ +#*..........*#+ *#...**..**...#* *#...##..##...#* #*...**..**...*# #*............*# *#............#* *#..+#....#+..#* +#*..*####*..*#+ +*#....++....#*+ ++##*......*##++ +++*###**###*+++ +++++**##**+++++ and produces a usable .tiff file. "The inverse is left as an exercise to the reader" -=EPS=- ------- texttotiff.c /* Produce "no alpha" .tiff files from text file input */ /* Eric P. Scott, San Francisco State University, July 1989 */ /* This code is in the public domain; as is, no warranty */ /* cc -bsd -O -o texttotiff -s texttotiff.c */ #include struct { long tiff_magic, tiff_firstifd; short tiff_ifdents; struct ifdent { short ifd_tag, ifd_type; long ifd_len, ifd_off; } tiff_ifd[8]; long tiff_ifdchain; } header={ /* warning: nonportable: byte/word order dependent */ 0x4d4d002aL, 8L, 8, { { 255, 3, 1L, 1L<<16 }, { 256, 3, 1L, 0L<<16 }, /* ImageWidth */ { 257, 3, 1L, 0L<<16 }, /* ImageLength */ { 262, 3, 1L, 1L<<16 }, { 277, 3, 1L, 1L<<16 }, { 284, 3, 1L, 2L<<16 }, { 258, 3, 1L, 2L<<16 }, { 273, 4, 1L, 110L } }, 0L }; /* "just like 0.9 Icon" */ char lex[]="#*+."; /* these characters represent gray levels */ main(argc, argv) int argc; char *argv[]; { char *index(), *malloc(); register char *q, *p; register unsigned char *o; register int n; register char *a, *w; char line[1122]; if (argc!=3) { fprintf(stderr, "Usage: %s text-file tiff-file\n", *argv); exit(1); } if (strcmp(argv[1], "-")&&!freopen(argv[1], "r", stdin)) { perror(argv[1]); exit(1); } if (strcmp(argv[2], "-")&&!freopen(argv[2], "w", stdout)) { perror(argv[2]); exit(1); } if (!fgets(line, sizeof line, stdin)) { fprintf(stderr, "%s: empty file?!?\n", *argv); exit(1); } if (p=index(line, '\n')) *p='\0'; else { fprintf(stderr, "%s: internal buffer overflow\n", *argv); exit(1); } if (p==line) { fprintf(stderr, "%s: blank line?!?\n", *argv); exit(1); } if (!(a=malloc((p-line)*208))) { fprintf(stderr, "%s: malloc failure\n", *argv); exit(1); } header.tiff_ifd[1].ifd_off=(p-line)<<16; w=p; o=(unsigned char *)a; for (n=1;;) { p=line; do { if (!(q=index(lex, *p++))) { bad: fprintf(stderr, "%s: illegal character on line %d col %d\n", *argv, n, p-line); exit(1); } *o=(q-lex)<<6; if (!*p) { o++; break; } if (!(q=index(lex, *p++))) goto bad; *o|=(q-lex)<<4; if (!*p) { o++; break; } if (!(q=index(lex, *p++))) goto bad; *o|=(q-lex)<<2; if (!*p) { o++; break; } if (!(q=index(lex, *p++))) goto bad; *o++|=(q-lex); } while (*p); if (!fgets(line, sizeof line, stdin)) break; if (++n>832||(p=index(line, '\n'))) *p='\0'; else { fprintf(stderr, "%s: internal buffer overflow on line %d\n", *argv, n); exit(1); } if (p!=w) { fprintf(stderr, "%s: image not rectangular on line %d\n", *argv, n); exit(1); } } header.tiff_ifd[2].ifd_off=n<<16; fwrite((char *)&header, sizeof header, 1, stdout); fwrite(a, (char *)o-a, 1, stdout); fflush(stdout); exit(0); }