SGI/Sun raster image background program (in 3D)

Joe Ilacqua spike at adt.UUCP
Sat Apr 22 07:15:12 AEST 1989


	Now you can change your background in NeWS (Max (whatever you
call it)) with this handy program.  It takes a SGI or Sun rasterfile
(they are the samething) and loads it on to the root window.  There
are lots O' SGI rasterfiles in '/usr/NeWS/smi'. It has been tested on a
4D/20 and a 4D/70g (where it was very slow).


Have fun 

->Spike

# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by spike on Fri Apr 21 16:55:05 EDT 1989
# Contents:  README Makefile bgraster.c bgraster.cps rasterfile.h
 
echo x - README
sed 's/^@//' > "README" <<'@//E*O*F README//'
	bgraster is a program to load Sun and SGI rasterfiles on to
the background of the window manager's screen.  Simply type:

	bgraster filename

	There will be a slight pause between the time the program
exits and the time the screen redraws (if the image is deeper than the
number of planes you have, the pause can become quite long).  There is
a -v option which will cause it to print the height, width, and depth
of the image.

*Warning*

	This program tries very hard to make sure the file is a
Sun/SGI rasterfile.  It also makes sure that the path given to NeWS
is relative to root ("/").  It is pretty fool-proof, but if you find a
way to get something by it (like a damaged rasterfile) it will crash
your server.

The SGI rasterfiles (found in /usr/NeWS/smi and used by the NeWS image
demos) are the same format as Sun rasterfiles.  The Portable Bitmap
Package (pbm), which has been distributed on the net and comes on the
X tape, can convert a number of bitmap formats to a SGI/SUN raster
file.  With pbm, X bitmaps -- which can be found in great numbers by
anonymous ftp on expo.lcs.mit.edu -- can be used.  If you have a Mac,
pbm can convert MacPaint files too.

	Some bits of code are from jim (lower case) frost's cool
xbgsun.  If you run The X Windows System, check it out.
 
Have Fun
->Spike    4/21/89

Joe Ilacqua



@//E*O*F README//
chmod u=rw,g=r,o=r README
 
echo x - Makefile
sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//'
# Makefile for bgraster
#
# Joe Ilacqua 4.21.89
#

NEWSHOME = /usr/NeWS
  CFLAGS = -O -I$(NEWSHOME)/include
 LDFLAGS = -L$(NEWSHOME)/lib
    LIBS = -lcps -lbsd
     CPS = cps
      RM = rm -f

all: bgraster.h bgraster 

bgraster: bgraster.o
	$(CC) $(LDFLAGS) -o bgraster $(CFLAGS) $@.o $(LIBS)

bgraster.h: bgraster.cps
	$(CPS) bgraster.cps

@.c.o:
	$(CC) $(CFLAGS) -c $*.c

bgraster.o: rasterfile.h

clean:
	$(RM) bgraster.o bgraster bgraster.h
@//E*O*F Makefile//
chmod u=rw,g=r,o=r Makefile
 
echo x - bgraster.c
sed 's/^@//' > "bgraster.c" <<'@//E*O*F bgraster.c//'
#include "psio.h"
#include "bgraster.h"
#include "rasterfile.h"

  /* 
   * This is a program to load Sun and SGI rasterfiles on to
   * the background of the window manager's screen.
   * 
   * Writen by Joe Ilacqua 4/21/89
   * spike at bu-it.bu.edu
   * 
   * some bits of code are from jim frost's cool xbgsun.  If you run 
   * The X Windows System, check it out.
   * 
   * This might just work with NeWS on this Sun..  Then again..
   * 
   */
  
main(argc,argv)
  int argc;
  char *argv[];
{

  FILE  *f;
  int   magic;
  char fname[BUFSIZ], *cwd, *getcwd();
  struct rheader header;
  int verbose = 0;

  if ((argc < 2) || (argc > 3)) {
    fprintf(stderr,"Usage: %s [-v] rasterfile\n",argv[0]);
    exit(0);
  }
  
  if (argc == 3)
    if(! strncmp(argv[1],"-v",2))
      verbose++;
    else {
      fprintf(stderr,"Usage: %s [-v] rasterfile\n",argv[0]);
      exit(0);
    }

  /* 
   * WARNING NeWS gets very unhappy if you send if a file it can't use!
   * 
   * Thus we try very hard here to make sure it is a Sun/SGI raster file.
   * 
   */

  
  /* 
   * NeWS can not find the file if it's path does not start at '/'
   * 
   */

  if((*argv[argc - 1] != '/')) {
    if ((cwd = getcwd((char *)NULL, BUFSIZ)) == NULL) {
      perror("pwd");
      exit(0);
    }
    sprintf(fname,"%s/%s",cwd,argv[argc -1]);
  }
  else
    sprintf(fname,"%s",argv[argc -1]);

  f= fopen(fname, "r");
  
  if (f == NULL) {
    perror(fname);
    exit(0);
  }
  
  if (fread(&header, sizeof(struct rheader), 1, f) != 1) {
    printf("%s: error loading rasterfile header.\n", argv[0]);
    exit(0);
  }
  
  /* check magic number
   */
  
  if (header.magic != RMAGICNUMBER) {
    printf("I don't know what '%s' is, but it's not a Raster image.\n",
	   argv[argc - 1]);
    exit(0);
  }
  
  if (verbose) {
    printf("Hmm, %s is a %dx%d %s image", argv[argc -1],
	   header.width, header.height,
	   (header.depth == 1 ? "monochrome" : "color"));
    if (header.depth > 1)
      printf(" with %d planes", header.depth);
    printf(".\n");
  }
  
  if( ps_open_PostScript() == 0) {
    fprintf(stderr,"Can not connect to NeWS server\n");
    exit(0);
  }
  loadImage(fname);
  ps_flush_PostScript();
  ps_close_PostScript();
  exit(1);
}
@//E*O*F bgraster.c//
chmod u=rw,g=r,o=r bgraster.c
 
echo x - bgraster.cps
sed 's/^@//' > "bgraster.cps" <<'@//E*O*F bgraster.cps//'
% this is the cps file for bgraster
% we just send our new /PaintRoot off to the system
%

cdef loadImage(string file)

systemdict begin
/pic {
    file readcanvas pause
} def

/PaintRoot {
gsave

framebuffer setcanvas clippath pathbbox scale pop pop
pic imagecanvas pause

grestore
} def 
end
PaintRoot

@//E*O*F bgraster.cps//
chmod u=rw,g=r,o=r bgraster.cps
 
echo x - rasterfile.h
sed 's/^@//' > "rasterfile.h" <<'@//E*O*F rasterfile.h//'
/* 
 * rasterfile.h:
 *
 * this describes the header for Sun rasterfiles.  if you have SunOS, a
 * better description is in /usr/include/rasterfile.h.  this is used
 * instead to improve portability and to avoid distribution problems.
 *
 * This file (with a minor change) is taken from jim frost's xbgsun
 * 
 */

struct rheader {
  unsigned int magic;   /* magic number */
  unsigned int width;   /* width of image in pixels */
  unsigned int height;  /* height of image in pixels */
  unsigned int depth;   /* depth of each pixel */
  unsigned int length;  /* length of the image in bytes */
  unsigned int type;    /* format of file */
  unsigned int maptype; /* type of colormap */
  unsigned int maplen;  /* length of colormap in bytes */
};

/* following the header is the colormap (unless maplen is zero) then
 * the image.  each row of the image is rounded to 2 bytes.
 */

#define RMAGICNUMBER 0x59a66a95 /* magic number of this file type */

/* these are the possible file formats
 */

#define ROLD       0 /* old format, see /usr/include/rasterfile.h */
#define RSTANDARD  1 /* standard format */
#define RRLENCODED 2 /* run length encoding to compress the image */

/* these are the possible colormap types.  if it's in RGB format,
 * the map is made up of three byte arrays (red, green, then blue)
 * that are each 1/3 of the colormap length.
 */

#define RNOMAP  0 /* no colormap follows the header */
#define RRGBMAP 1 /* rgb colormap */
#define RRAWMAP 2 /* raw colormap; good luck */

#define RESC 128 /* run-length encoding escape character */

@//E*O*F rasterfile.h//
chmod u=rw,g=r,o=r rasterfile.h
 
exit 0



More information about the Comp.sys.sgi mailing list