[comp.windows.x] xpbm.c

jonnyg@ROVER.UMD.EDU (Jon Greenblatt) (04/09/88)

	I had a hack attack tonight and thought this program would be usefull.
This program, xpbm.c, takes a file in pbm (Portable bitmap format) and displays
it on a X11 console. This is an example of a minimum X window application
but it works well.

To compile: cc -O xpbm.c -o xpbm -lX

	I felt there was now good way to directly display pbm files on X11
when the file is too big for the X Window bitmap program.
Well here it is:

--------------------------------Snip here-------------------------------------
/*
 * xpbm.c: Reads a Portable bitmap file  and displays it on an X11 console.
 * This program is free free free.
 * Written by Jonathan Greenblatt (jonnyg@rover.umd.edu) All rights resevered.
 * This is a very simple program and uses a minimum of resources.
 * Someone may like to add some resource processing if they find this useful.
 */

#include	<stdio.h>
#include	<Xlib.h>
#include	<Xutil.h>

Display *BsDpy;
Window BsWnd;

#define	MemorySize  250

Pixmap BsPix;
int xs, ys, xh, yh;


init() {
	BsDpy = XOpenDisplay("");	/* Open the default display */
	BsWnd = XCreateSimpleWindow(BsDpy,DefaultRootWindow(BsDpy),16,16,16,16,1,WhitePixel(BsDpy,DefaultScreen(BsDpy)),BlackPixel(BsDpy,DefaultScreen(BsDpy)));
	XStoreName(BsDpy,BsWnd,"Bitmap Show");
	XPending(BsDpy);
	XSelectInput(BsDpy,BsWnd,ExposureMask | SubstructureRedirectMask);
	}

show() {
        XEvent BsEvent;
        XSizeHints BsHints;

	/* Put the picture in the background! */
        XSetWindowBackgroundPixmap(BsDpy,BsWnd,BsPix);

        BsHints.flags = PPosition | PSize | USSize;
        BsHints.x = 0;
        BsHints.y = 0;
        BsHints.width = xs;
        BsHints.height = ys;
        XSetNormalHints(BsDpy,BsWnd,&BsHints);
        XPending(BsDpy);
        XSelectInput(BsDpy,BsWnd,StructureNotifyMask);
	XMapRaised(BsDpy,BsWnd);
        XPending(BsDpy);
        do {
                XNextEvent(BsDpy,&BsEvent);
                } while (BsEvent.type != MapNotify);
	}

/* Bits ! */
char xtab[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

readbits(fname)
char *fname;
	{
	FILE *f;
	char *buff, *malloc();
	register unsigned int i, j, c;
	int bsize, lsize;

	if (fname == NULL)
		f = stdin;
	else	f = fopen(fname,"r");
	if (f == NULL) exit(1);

	fscanf(f,"%d %d\n",&xs,&ys);
	lsize = (xs + 7) >> 3;
	buff = malloc(bsize = lsize * ys);
	if (buff == NULL) exit(1);
	for (i = 0; i < bsize; i++) buff[i] = 0;
	for (j = 0; (j < ys) && !feof(f); j++) {
		register char *buf = &buff[lsize*j];
		for (i = 0; (i < xs) && !feof(f); i++) {
			do {
				c = fgetc(f);
				} while (c != EOF && c != '1' && c != '0');
			if (c != '1') {
				buf[i >> 3] |= xtab[i & 7];
				}
			}
		}
	if (fname != NULL) fclose(f);
	BsPix = XCreateBitmapFromData(BsDpy, BsWnd, buff, xs, ys);
	free(buff);
	}

waitbutton() {
	XEvent BsEvent;
        long eventmask;

        eventmask = ButtonPressMask;
        XPending(BsDpy);
        XSelectInput(BsDpy,BsWnd,eventmask);
        while (1) {
                XNextEvent(BsDpy,&BsEvent);
                switch (BsEvent.type) {
                        case ButtonPress:
                                return;
			default: break;
			}
		}
	}

main(argc,argv)
char *argv[];
	{
	char *fname;

	if (argc < 2) fname = NULL;
	else fname = argv[1];
	init();
	readbits(fname);
	show();
	waitbutton();
	XCloseDisplay(BsDpy);
	}