comparison tools/x11gram.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /****************************************************************************
2 *
3 * compilation: cc -o xgram xgram.c -lX11
4 *
5 * "profile" is an array with an element for each pixel across the window.
6 *
7 ****************************************************************************/
8
9 #include <stdio.h>
10 #include <math.h>
11 #include "x11coord.h"
12
13 char DEFNAME[]= "stdin"; /* window name */
14 char *name = DEFNAME;
15
16 /****************************************************************************
17 * Defaults for plot
18 ****************************************************************************/
19 #define DEFn 128 /* number of data points on a plot line */
20 #define DEFln FULLWIDTH>>1 /* length of plot line in pixels */
21 #define DEFl 1000 /* max number of lines to plot */
22 #define DEFs 0 /* number of plot lines offset from start of file */
23 #define DEFf 0.2 /* scale factor for plot y-values */
24
25 #define DEFdx 2 /* origin increments for successive lines, in pixels */
26 #define DEFdy 2
27
28 /****************************************************************************
29 * Arguments
30 ****************************************************************************/
31 int s=DEFs; /* start position in file */
32 short n=DEFn; /* number of data points on a plot line */
33 short l=DEFl; /* max number of lines to plot */
34 short ln=DEFln; /* length of plot line in pixels */
35 short dx=DEFdx,dy=DEFdy; /* increments for successive lines, in pixels */
36 double f=DEFf; /* scale factor for each y-value */
37
38 /****************************************************************************
39 * main
40 ****************************************************************************/
41 main(argc, argv)
42 int argc ;
43 char *argv[] ;
44 {
45 FILE *fp, *fopen();
46 Window w;
47 short p;
48 int i;
49
50
51 while (--argc > 0 && **++argv == '-')
52 switch (*++*argv) {
53 case 'n': n = atoi(++*argv); break;
54 case 'l': l = atoi(++*argv); break;
55 case 's': s = atoi(++*argv); break;
56 case 'f': f *= atof(++*argv); break;
57 case 'x': dx = atoi(++*argv); break;
58 case 'y': dy = atoi(++*argv); break;
59 case 'h':
60 case 'H':
61 default: help();
62 }
63 /* Open data file */
64 if (argc) {
65 name = *argv;
66 if ((fp = fopen(name, "r")) == NULL) {
67 fprintf(stderr,"can't open %s\n", *argv);
68 exit(1);
69 }
70 }
71 else fp = stdin;
72 /* Seek start of data in file */
73 if (s > 0) {
74 s *= n; /* convert number of lines to number of points */
75 for (i=0 ; i<s && fread(&p, sizeof(short), 1, fp) ; i++)
76 ;
77 if (i<s) error("seek overshot end-of-file\n");
78 }
79
80 set_window_parameters(FULLXORG,FULLYORG, FULLWIDTH,FULLHEIGHT);
81 set_bordered_box(1);
82
83 /** set data so that points fill window ?? **/
84
85 xopen();
86 w = xcreate(name, ButtonPressMask | ExposureMask);
87 xevent_monitor(w,fp);
88 fclose(fp);
89 XDestroyWindow(theDisplay,w);
90 }
91
92
93 /****************************************************************************
94 * X11 Event Monitor.
95 ****************************************************************************/
96 xevent_monitor(w,fp)
97 Window w;
98 FILE *fp;
99 {
100 XEvent event;
101
102 for ( ; ; ) {
103 XNextEvent(theDisplay,&event);
104 switch (event.type) {
105 case ButtonPress:
106 switch(event.xbutton.button) {
107 case Button1: return; /* Left */
108 case Button2: return; /* Middle */
109 case Button3: return; /* Right */
110 default:
111 fprintf(stderr,"button %d not used\n", event.xbutton.button);
112 }
113 break;
114 case Expose:
115 if (event.xexpose.count == 0) {
116 draw_box(w);
117 draw_gram(w,FULLWIDTH,FULLHEIGHT,fp);
118 }
119 break;
120 default:
121 fprintf(stderr,"event type %d not found\n", event.type);
122 }
123 }
124 }
125
126
127
128 /****************************************************************************
129 * Draw line (spectro)-gram with hidden-line removal.
130 ****************************************************************************/
131 #define VISIBLE 0
132 #define HIDDEN 1
133 #define X0 10 /* origin of plot within window */
134 #define Y0 10
135 #define move(x,y) X=x;Y=y
136 #define cont(x,y) XDrawLine(theDisplay, w, theGC, X+X0,height-Y-Y0, x+X0,height-y-Y0);move(x,y)
137
138 draw_gram(w,width,height,fp)
139 Window w;
140 int width,height; /* window size, in pixels */
141 FILE *fp; /* i/p data file */
142 {
143 int i;
144 short p, *profile;
145 short X, Y; /* current active position */
146 short x, y, x0, y0, y1;
147 short xorg=0, yorg=0;
148 float index, res;
149 short rres;
150 char stat;
151
152 profile = (short*) malloc(width * sizeof(short));
153 for (i=0 ; i<width ; i++)
154 profile[i] = 0;
155
156 res = (float)ln/n;
157 rres = (int)(res+0.5); /* rounded resolution */
158 /* plot l lines, or until eof */
159 while (l-->0 && fread(&p, sizeof(short), 1, fp)) {
160 /* draw vertical to start of line */
161 x = xorg;
162 y = yorg + p*f;
163 if (y >= (y0=profile[x])) {
164 profile[x] = y;
165 move(x,y0);
166 cont(x,y);
167 stat = VISIBLE;
168 }
169 else stat = HIDDEN;
170 y0 = y;
171
172 /* draw rest of line */
173 index = xorg;
174 for (i=1 ; i<n && fread(&p, sizeof(short), 1, fp) ; i++) {
175 /* interpolate y-values for each pixel along line */
176 for (x0=1, y1=yorg+p*f ; x0<=rres && x0+x<width ; x0++) {
177 y = (short)( ((float)x0/res) * (y1-y0) + y0 );
178 /* a Hidden line becomes Visible */
179 if (y >= profile[x+x0]) {
180 profile[x+x0] = y;
181 if (stat == HIDDEN) {
182 move(x+x0,y);
183 stat = VISIBLE;
184 }
185 }
186 /* a Visible line becomes Hidden */
187 else {
188 if (stat == VISIBLE) {
189 cont(x+x0,y);
190 stat = HIDDEN;
191 }
192 }
193 }
194 y0 = y1;
195 index += res;
196 x = (int)(index+0.5); /* rounded index */
197 if (stat == VISIBLE)
198 cont(x,y);
199 }
200 cont(x,yorg);
201 xorg += dx;
202 yorg += dy;
203 }
204 }
205
206
207 /****************************************************************************
208 * Misc.
209 ****************************************************************************/
210 error(s)
211 {
212 fprintf(stderr,"%s",s);
213 exit(1);
214 }
215
216 help ()
217 {
218 printf ("\nUsage: a) xgram [options] filename\n");
219 printf (" b) cat filename | xgram [options]\n");
220 printf (" where filename is an input stream of 16-bit binary numbers\n");
221 printf ("Options:\n");
222 printf ("-n [int] = Number of points per plotted line (default=%d).\n", DEFn);
223 printf ("-l [int] = Max number of lines plotted (default=%d).\n", DEFl);
224 printf ("-s [int] = Offset lines (of n points) from start of file (default=%d).\n", DEFs);
225 printf ("-f [float] = Scale factor (default=1).\n");
226 printf ("-x-y[ints] = dx/dy changes for successive lines (defaults %d,%d)\n", DEFdx,DEFdy);
227 exit (0);
228 }