From: khera@cs.duke.edu (Vivek Khera)
Newsgroups: fj.mail-lists.x-window
Subject: Re: convert Sun suntool/xview icon image
Date: 12 Jan 93 17:53:49 GMT
Distribution: fj
Organization: Duke University CS Dept., Durham, NC

Date: 12 Jan 93 15:48:53 GMT
Message-Id: <KHERA.93Jan12104853@thneed.cs.duke.edu>
Newsgroups: comp.windows.x

In article <1ithqiINN57p@guinan.cs.purdue.edu> tzheng@cs.purdue.edu (Tom Zheng) writes:

   How can I convert icon image file in SunView/XVIEW to bitmap
   or pixmap?

i wrote a program to convert all my old suntools (now known as
SunView) icons into X bitmap format quite some time ago.  here it is
for your enjoyment (run flex then gcc the result).  the conversion
works on any sized sunview icon, but the output claims them all to be
the stock 64x64.  just edit the output file to change the sizes
defined and it will be fine.

--cut here--
 /* flex portion of converter. V. Khera  07-JUL-1988
  * assumes run through flex and gcc.
  */
%%
"/*"	{	/* skip comments */
		loop:
		while (input() != '*');
		switch (input()) {
		    case '/': break;
		    case '*': unput('*');
		    default: goto loop;
		}
	}

0x[0-9A-F]+ {	/* convert each hex number into two with bits reversed */
	static unsigned short num;
	unsigned char hi,lo;	/* parts of bitmap */
	extern int icount;
	void flip_bits(unsigned char *);

	sscanf(yytext,"0x%hx",&num);	/* get into manipulable forms */
	hi = (unsigned char)((num & 0xff00) >> 8);
	lo = (unsigned char)(num & 0x00ff);
	flip_bits(&hi);
	flip_bits(&lo);
	if (icount == 0) {
		printf("   0x%02x, 0x%02x",hi,lo);
	} else {
		printf(",");
		printf((icount % 12) ? " " : "\n   ");
		printf("0x%02x, 0x%02x",hi,lo);
	}
	icount += 2;
    }

.	/* ignore the rest */;
\n	;

%%

/*
 * program to convert sun icon format files into X bitmap format files
 *
 * V. Khera	08-JUL-1988
 */

static char rcs_id[] = "$Id: suntox.lex,v 1.5 90/10/22 13:00:58 khera Exp $";

#define BITMAP_EXT	".xbm"

char outfile[1024];	/* long enough, i hope */
char *infile;		/* points to argv[i] */
int icount = 0;		/* count number of items output per file */

#include <string.h>
char *rindex(const char *, char);
void convert(const char*, char*);

main(int argc, char **argv)
{
	char *i;

	if (argc < 2) {
		fprintf(stderr,"usage:%s <icon file> ...\n",argv[0]);
		fprintf(stderr,
			"Convert Sun icon format files into X bitmap files\n");
		exit (1);
	}

	for (++argv ;infile = *argv; argv++) {
		strcpy(outfile,infile);
		if (i=rindex(outfile,'.')) *i = '\0';
		strcat(outfile,BITMAP_EXT);

		convert(infile,outfile);
	}
	return 0;
}

/*
 * converts each word read in into 2 chars with bit order reversed.
 */

void convert(const char *infname, char *outfname)
{
	int Xwidth = 64;	/* default width */
	int Xheight = 64;	/* default height */
	int Swidth = -1;	/* Sun icon width */
	int Sheight = -1;	/* Sun icon height */
	register int i;
	char *p;

	fprintf(stderr,"converting %s to %s\n",infname,outfname);
	if ((freopen(infname,"r",stdin)) == NULL) {
		fprintf(stderr,"cannot open %s\n",infname);
		return;
	}
	if ((freopen(outfname,"w",stdout)) == NULL) {
		fprintf(stderr,"cannot open %s\n",outfname);
		return;
	}

	/*
	 * get size information from Sun icon header
	 */
	do {
		i = fscanf(stdin, "%*[^HW*]");
		if (i == EOF)
			break;
		switch(i = getc(stdin)) {
		case 'H':
			fscanf(stdin, "eight=%d", &Sheight);
			break;
		case 'W':
			fscanf(stdin, "idth=%d", &Swidth);
			break;
		case '*':
			if ((i = getc(stdin)) == '/')
				i = 0;
			else
				ungetc(i, stdin);
			break;
		}
	} while (i != 0 && i != EOF);

	if (Sheight > 0) Xheight = Sheight;
	if (Swidth > 0) Xwidth = Swidth;

	rewind(stdin);	/* confuses lex otherwise */

	/* we know that the name has a ".xbm" extension */
	*(rindex(outfname,'.')) = '\0';	/* use for naming parts of structure */
	if (p = rindex(outfname,'/')) outfname = p+1;	/* remove path name */

	icount = 0;
	printf("#define %s_width %d\n",outfname, Xwidth);
	printf("#define %s_height %d\n",outfname, Xheight);
	printf("static char %s_bits[] = {\n",outfname);

	yylex();	/* do the grunt work */

	printf("};\n");	/* close up */

}

#define BIT (unsigned char)0x01
#define BITSPERBYTE	8

void flip_bits(unsigned char *c)
{
	register unsigned char t = *c;	/* to play with */
	register int count;

	*c = '\0';
	for (count = 1; count <= BITSPERBYTE; count++) {
		*c |= (t & BIT)<<(BITSPERBYTE-count);
		t >>= 1;	/* shift bits over one */
	}
}
--cut here--
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Vivek Khera, Gradual Student/Systems Guy  Department of Computer Science
Internet:   khera@cs.duke.edu             Box 90129
            (MIME mail accepted)          Durham, NC 27708-0129 (919)660-6528