[comp.windows.x] SUN to X bitmap converter

clyde@ut-ngp.UUCP (03/03/87)

Here is a handy little tool I put together so that I could use all the
icons I had already done for SunTools.  Plus, the X bitmap editor is a real
pain to use, so I didn't care to try to use it to draw new ones.

----- cut here ------
#include <stdio.h>

/*
 *	suntox - Convert SUN bitmap (icon) format to X bitmap format
 *
 *	Usage: suntox [-h X bitmap height] [-w X bitmap width]
 *		[-n X bitmap name] [filename] (stdin default)
 *
 *	Author:	Clyde Hoover
 *		Computation Center, The University of Texas at Austin
 *		clyde@ngp.utexas.edu
 */
unsigned short	flip();		/* Bit flipper */
char	*bitmapname();

FILE	*ifile = stdin;		/* Input file pointer */
char	*Xname = 0;		/* X bitmap name */
int	Xwidth = 0,		/* Width of X bitmap */
	Xheight = 0,		/* Height of X bitmap */
	Xresize = 0;		/* Force actual bitmap resize (NYI) */
char	*options = "h:w:n:r";	/* Option string for getopt() */

main(argc, argv)
int	argc;
char	*argv[];
{
	int	Iwidth = -1,	/* SUN bitmap width */
		Iheight = -1;	/* SUN bitmap height */
	register int i;		/* Temp */
	int	argx;		/* Option traverser */
	char	pb[8];		/* Temp */
	extern char	*optarg;/* From getopt() */
	extern int	optind;	/* From getopt() */

	/*
 	 * Process command line options
	 */
	while ((argx = getopt(argc, argv, options)) != EOF) {
		switch(argx) {
		case 'w':	Xwidth = atoi(optarg); break;
		case 'h':	Xheight = atoi(optarg); break;
		case 'n':	Xname = optarg; break;
/* 		case 'r':	Xresize = 1; break; */
		}
	}

	if (argv[optind]) {
		if ((ifile = fopen(argv[optind], "r")) == NULL) {
			fprintf(stderr, "Can't open %s\n", argv[optind]);
			exit(1);
		}
		if (Xname == (char *)0) Xname = bitmapname(argv[optind]);
	}
	/*
	 * Dig out size information from SUN header
	 */
	do {
		i = fscanf(ifile, "%*[^HW*]");
		if (i == EOF)
			break;
		switch(i = getc(ifile)) {
		case 'H':	fscanf(ifile, "eight=%d", &Iheight); break;
		case 'W':	fscanf(ifile, "idth=%d", &Iwidth); break;
		case '*':	if ((i = getc(ifile)) == '/') i = 0;
				else ungetc(i, ifile);
				break;
		}
	} while (i != 0 && i != EOF);
	if (Iheight < 0 || Iwidth < 0) {
		fprintf(stderr, "Missing format info\n");
		exit(1);
	}

	/*
	 * Verify reasonableness of bitmap dimensions
	 */
	if (Xwidth == 0) Xwidth = Iwidth;
	if (Xheight == 0) Xheight = Iheight;
	if (Xname == (char *)0) Xname = "X";

	if (Xwidth > Iwidth || Xheight > Iheight) {
		fprintf(stderr, "Bad sizing option(s) ignored\n");
		if (Xwidth > Iwidth) Xwidth = Iwidth;
		if (Xheight > Iheight) Xheight = Iheight;
	}
	/*
	 * Output X bitmap header
	 */
	printf("#define	%s_width %d\n#define %s_height %d\n",
		Xname, Xwidth, Xname, Xheight);
	printf("static short %s_bits[] = {\n", Xname);

	/*
	 * Convert image bytes
	 */
	i = 0;
	while (!feof(ifile)) {
		int	x;
		unsigned int	word;

		x = fscanf(ifile, " 0x%X,", &word);
		if (x == EOF)
			break;
		if (x == 0) continue;
		if ((i % 4) == 0) printf("\n");
		sprintf(pb, "%04x", flip((unsigned short)word));
		printf("0x%s,", pb);
		i++;
	}
	printf("};\n");
}

/*
 *	flip - Invert bit order in a short (16 bit) word
 */
unsigned short
flip(this)
register unsigned short this;
{
	register unsigned short that = 0;	/* Return value */
	register int i = 0;		/* Counter */

	for (i = 1; i <= (sizeof(short) * 2); i++) {
		that <<= 4;
		if (this & 0x8)	that |= 01;
		if (this & 0x4)	that |= 02;
		if (this & 0x2)	that |= 04;
		if (this & 0x1)	that |= 08;
		this >>= 4;
	}
	return(that);
}

/*
 *	bitmapname - make X bitmap name from input file name
 */
#include <ctype.h>
char *
bitmapname(filename)
char	*filename;		/* Name of input file */
{
	register char	*p;		/* Temp */
	static char	xs[256];	/* Return value */
	extern char	*rindex();

	if (p = rindex(filename, '/'))
		p++;
	else
		p = filename;
	(void) strcpy(xs, p);
	for (p = xs; *p; p++) {
		if (!isalnum(*p)) {
			*p = 0;
			break;
		}
	}
	return(xs);
}
/* --------- End ----------------- */
-- 
Shouter-To-Dead-Parrots @ Univ. of Texas Computation Center; Austin, Texas  
	clyde@ngp.cc.utexas.edu; ...!ut-sally!ut-ngp!clyde
"It's a sort of a threat, you see.  I've never been very good at them
myself, but I've told they can be very effective."