ftp.nice.ch/pub/next/unix/text/texttotiff.c

This is texttotiff.c in view mode; [Download] [Up]

/* Produce "no alpha" .tiff files from text file input */
/* Eric P. Scott, San Francisco State University, July 1989 */
/* 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 */
	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 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);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.