[comp.sys.next] Poor Man's Icon--a text to tiff converter

epsilon@wet.UUCP (Eric P. Scott) (07/15/89)

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.  <usual disclaimers>
"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 <stdio.h>
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);
}

bruceh@zygot.UUCP (Bruce Henderson) (07/18/89)

I have been having a really nasty problem with my copy of "Icon"  It
seems that when editing small tiff files [the one in question is 30 x
20] the program will bring the file up in a very strange and trashed
out format.  Usually in a 48 x 48 size.  This is a real bitch because I
need some rather small icons to add to some buttons in my app.  If
there is a work arround or I'm brain dead, let me know.  btw, I already
trashed my copy on the HD and loaded a "fresh" one from the optical
Thanks in advance!

Bruce Henderson   apple!zygot!bruceh

-- 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Bruce Henderson                                       Software Engineer
zygot!bruceh@Apple.COM			    
"Sorry, Mathematica can't goon this much"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

epsilon@wet.UUCP (Eric P. Scott) (07/20/89)

In article <1744@zygot.UUCP> bruceh@zygot.UUCP (Bruce Henderson) writes:
>I have been having a really nasty problem with my copy of "Icon"  It
>seems that when editing small tiff files [the one in question is 30 x
>20] the program will bring the file up in a very strange and trashed
>out format.  Usually in a 48 x 48 size.  This is a real bitch because I
>need some rather small icons to add to some buttons in my app.  If
>there is a work arround or I'm brain dead, let me know.

Well, here's sort of a work-around: make a .tiff file that's big
enough to hold your icons.  When you "get it right," select a
bounding rectangle (it helps if you turn on the Coordinates
display), choose Save As and click the Save Document button into
Save Selection.  Icon will write out small images properly, it
just can't read them back in.  If you want to view a small .tiff
file, drag it into a Draw window.  Hold Alternate and grab a
corner knobbie to magnify the image.
					-=EPS=- / SFSU