[net.sources] sunview - image viewer for SUN

majka@ubc-cs.UUCP (02/17/87)

Here is a program (+ makefile and manual page) called "sunview" which
allows you to view a halftone rendition of a raster "image" file on
a SUN running suntools.  It is meant to run in a "gfx-window".  The
input image file should be 1 byte per pixel.  Number of rows and
columns are set in the command line.

Happy viewing!

---
Marc Majka  - UBC Laboratory for Computational Vision


- - - CUT - - - SH - - - CUT - - - SH - - - CUT - - - SH - - - CUT - - -
#/bin/sh
#
# shell archive - extract with /bin/sh
#
echo extracting file sunview.c
sed 's/^X//' > sunview.c <<'!FUNKY!STUFF!'
X/*************************************************************/
X/*                                                           */
X/*  Copyright (c) 1987                                       */
X/*  Marc S. Majka - UBC Laboratory for Computational Vision  */
X/*                                                           */
X/*  Permission is hereby granted to copy all or any part of  */
X/*  this program for free distribution.   The author's name  */
X/*  and this copyright notice must be included in any copy.  */
X/*                                                           */
X/*************************************************************/
X
X/* sunview - read a raster image file, halftone and scale it */
X/* to fit into a suntools graphics window.  Does not handle  */
X/* damage to the window.  Crufty and slow, but it works.     */
X
X#include <sys/types.h>
X#include <pixrect/pixrect.h>
X#include <sunwindow/rect.h>
X#include <sunwindow/rectlist.h>
X#include <sunwindow/pixwin.h>
X#include <suntool/gfxsw.h>
X#include <stdio.h>
X
X/* screen output area */
Xint c1, r1, c2, r2, dw, dh;
X
X/* output window */
Xstruct pixwin *pw;
Xstruct gfxsubwindow *gfx;
X
X/* memory image pixrect */
Xstruct pixrect *impr;
X
X/* The whole thing goes in core */
X#define MAX 512
Xunsigned char image[MAX][MAX];
X
X/* Modulating signal */
X#define MAXP 16
Xint period, modsignal[MAXP][MAXP];
X
Xmain (argc, argv)
Xint argc;
Xchar *argv[];
X{
X	FILE *fopen(), *ifp;
X	int row, col, nrows, ncols;
X	int done, pcnt, next;
X	int gotimage, argn;
X	float sin(), alpha, beta, delta, sinalpha, ampl, atof();
X
X	gotimage = 0;
X	
X	/* Frobs to play with if you are so inclined */
X	/* Amplitude of modulating signal */
X	ampl = 75.0;
X
X	/* Period of modulating signal */
X	period = 5;
X
X	/* assume a 256 * 256 input image */
X	nrows = 256;
X	ncols = 256;
X
X	for (argn = 1; argn < argc; argn++) {
X		if (argv[argn][0] == '-') {
X			switch (argv[argn][1]) {
X				case 'a': /* Amplitude */
X					ampl = atof(argv[++argn]);
X					break;
X				case 'p': /* period */
X					period = atoi(argv[++argn]);
X					if (period > MAXP) {
X						fprintf(stderr,"Maximum period is %d\n",MAXP);
X						exit(1);
X					}
X					break;
X				case 'r': /* nrows */
X					nrows = atoi(argv[++argn]);
X					if (nrows > MAX) {
X						fprintf(stderr,"Maximum nrows is %d\n",MAX);
X						exit(1);
X					}
X					break;
X				case 'c': /* ncols */
X					ncols = atoi(argv[++argn]);
X					if (ncols > MAX) {
X						fprintf(stderr,"Maximum ncols is %d\n",MAX);
X						exit(1);
X					}
X					break;
X				default:
X					usage();
X					exit(0);
X			}
X		}
X		else {
X			if (gotimage) {
X					usage();
X					exit(0);
X			}
X
X			gotimage = 1;
X			ifp = fopen(argv[argn],"r");
X			if (ifp == NULL) {
X				fprintf(stderr,"can't open image file %s\n",argv[argn]);
X				exit(1);
X			}
X		}
X	}
X
X	if (!gotimage) ifp = stdin;
X
X	done = 0;
X	pcnt = 0;
X	next = 0;
X	fprintf(stderr,"reading image ...\n");
X	for (row = 0; row < nrows; row++) {
X		for (col = 0; col < ncols; col++) image[row][col] = getc(ifp);
X
X		done += 100;
X		pcnt = done / nrows;
X		if (pcnt >= next) {
X			fprintf(stderr,"%d%% ",pcnt);
X			fflush(stderr);
X			next += 10;
X		}
X	}
X	fprintf(stderr,"\n");
X	
X	fclose(ifp);
X
X	/* Open gfx subwindow */
X	gfx = gfxsw_init(0,NULL);
X	pw = gfx->gfx_pixwin;
X
X	/* Initialize a table (modsignal) containing the modulating signal */
X	delta = 3.141592657 * 2.0 / (float)period;
X	for (row = 0, alpha = 0.0; row < period; row++, alpha += delta) {
X		sinalpha = sin(alpha);
X		for (col = 0, beta = 0.0; col < period; col++, beta += delta)
X			modsignal[row][col] = sin(beta) * ampl * sinalpha;
X	}
X
X	start();
X	resize(nrows,ncols);
X	pw_rop(pw,c1,r1,dw,dh,PIX_SRC,impr,0,0);
X	fprintf(stderr,"Type <return> to erase screen and quit." );
X	getchar();
X	exit(0);
X}
X
X/* get output window size and clear it */
Xstart()
X{
X	struct rect rect;
X	
X	win_getsize(gfx->gfx_windowfd,&rect);
X	c1 = rect.r_left;
X	r1 = rect.r_top;
X	dw = gfx->gfx_rect.r_width;
X	dh = gfx->gfx_rect.r_height;
X	c2 = rect_right(&rect);
X	r2 = rect_bottom(&rect);
X	pw_writebackground(gfx->gfx_pixwin,0,0,rect.r_width,rect.r_height,PIX_CLR);
X}
X
X/* resize nrows * ncols to fit in output window */
X/* modulation-style halftoning is done on the fly */
Xresize(nr,nc)
Xint nr,nc;
X{
X	struct pixrect *mem_create();
X	float x, y, dx, dy;
X	int r0, c0, a, q, r, c, rows, cols, s, t, done, pcnt, next;
X	
X	pcnt = 0;
X	done = 0;
X	next = 0;
X	fprintf(stderr,"halftoning image ...\n");
X	
X	impr = mem_create(dw,dh,1);
X	
X	rows = dh;
X	cols = dw;
X	t = 0;
X	
X	dx = (nr * 1.0) / dh;
X	dy = (nc * 1.0) / dw;
X
X	for (x = 0.0, r = 0; r < rows; x += dx, r++) {
X		t = (t + 1) % period;
X		s = 0;
X		r0 = x;
X		
X		for (y = 0.0, c = 0; c < cols; y += dy, c++) {
X			c0 = y;
X			s = (s + 1) % period;
X			a = image[r0][c0];
X			q = (int)(a + modsignal[t][s]) <= 127; 
X			pr_put(impr,c,r,q);
X
X		}
X
X		done += 100;
X		pcnt = done / rows;
X		if (pcnt >= next) {
X			fprintf(stderr,"%d%% ",pcnt);
X			fflush(stderr);
X			next += 10;
X		}
X	}
X	fprintf(stderr,"\n");
X}
X
Xusage()
X{
X	fprintf(stderr,"sunview [image] [-r %%d] [-c %%d] [-a %%f] [-p %%d]\n");
X	fprintf(stderr,"    image - input raster image file\n");
X	fprintf(stderr,"    -r    - number of image rows [default is 256]\n");
X	fprintf(stderr,"    -c    - number of image columns [default is 256]\n");
X	fprintf(stderr,"    -a    - modulation amplitude [default is 75.0]\n");
X	fprintf(stderr,"    -p    - modulation period [default is 5]\n");
X}
X
!FUNKY!STUFF!
echo extracting file sunview.1
sed 's/^X//' > sunview.1 <<'!FUNKY!STUFF!'
X.TH SUNVIEW 1
X.SH NAME
Xsunview - view an image file on a Sunwindows GFX-Window
X.SH SYNOPSIS
X.B sunview
X[image] [-r nrows] [-c ncols] [-a amplitude] [-p period]
X.SH DESCRIPTION
X.I sunview
Xreads a raster image file and displays a halftone rendition in a
Xsun gfx-window.  The input image size is specified by the
X.B -r
Xand
X.B -c
Xoptions. The default image size is 256 rows and 256 columns.  The
Xinput image is expected to be 1 byte per pixel.  The image is resized
Xto fit into the available window space. A modulation technique is used
Xfor halftoning. The amplitude and period of the modulation may be changed
Xwith the 
X.B -a
Xand
X.B -p
Xoptions.  The default amplitude is 75.0, the default period is 5.
X.SH AUTHOR
XMarc Majka  -  UBC Laboratory for Computational Vision
!FUNKY!STUFF!
echo extracting file Makefile
sed 's/^X//' > Makefile <<'!FUNKY!STUFF!'
X# Makefile for sunview
X
XSUNTOOL=-lsuntool -lsunwindow -lpixrect
X
Xsunview: sunview.c
X	cc -o sunview sunview.c ${SUNTOOL} -lm
!FUNKY!STUFF!
echo Done\!
exit 0