File: mgfyctrl.c

package info (click to toggle)
saoimage 1.35.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,188 kB
  • ctags: 4,403
  • sloc: ansic: 52,848; makefile: 243; sh: 31
file content (423 lines) | stat: -rw-r--r-- 13,497 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	mgfyctrl.c (Magnify Control)
 * Purpose:	Initialize params and organize drawing the magnifier window
 * Subroutine:	magnify_disp()			returns: void
 * Subroutine:	magnify_pan()			returns: void
 * Subroutine:	clear_coord_area()		returns: void
 * Subroutine:	redraw_magnifier()		returns: void
 * Xlib calls:	XCheckWindowEvent(), XSync()
 * Copyright:	1999 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		 6 June 1989
 *		{1} MVH added support for second set of coords	  7 Nov 1989
 *		{2} Doug Mink added support for WCS		 18 Oct 1994
 *		{3} DJM changed WCS arguments           	  6 Jul 1995
 *		{4} DJM overwrite excess chars in WCS area     	 17 Aug 1995
 *		{5} DJM overwrite excess chars in pixel area   	  2 Feb 1996
 *		{6} DJM write image number if 3D                 13 Jul 1998
 *		{6} DJM write face number if quad cube           23 Feb 1999
 *		{6} Doug Mink bump WCS strin from 32 to 48 char   6 May 1999
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>
#include <string.h>		/*  strlen */
#include <X11/Xlib.h>		/* X window stuff */
#include <X11/Xutil.h>		/* X window manager stuff */
#include "hfiles/struct.h"	/* declare structure types */
#include "hfiles/extern.h"	/* extern main SAOimage parameter structures */
#include "hfiles/constant.h"	/* codes */
#include "hfiles/magnify.h"	/* magnifier quick access structure */

extern struct windowRec desktop;
extern struct magRec magset;

#define VAL_SZ 10
/*
 * Subroutine:	magnify_disp
 * Purpose:	Magnify location of a dispbox event
 * Xlib calls:	XCheckWindowEvent(), XSync()
 */
void magnify_disp ( event, view, text )
     XEvent *event;		/* i: XEvent for location of mouse */
     int view, text;
{
  void draw_magnifier();
  static void label_file_coords(), label_file_coords_proportional();

  /* get only the most recent mouse moved event */
  XSync(dispbox.display, 0);
  while( XCheckWindowEvent(dispbox.display, dispbox.ID,
			   PointerMotionMask, event) );
  /* draw image fragment in scope magnibox if desired */
  if( view ) {
    /* get buffer coordinates */
    magset.buf.X = ((float)event->xmotion.x * coord.disptobuf.inx_outx) +
      coord.disptobuf.iadd_outx;
    magset.buf.Y = ((float)event->xmotion.y * coord.disptobuf.iny_outy) +
      coord.disptobuf.iadd_outy;
    draw_magnifier((double)magset.buf.X, (double)magset.buf.Y);
    if( text ) {
      if( magset.text.proportional )
	label_file_coords_proportional((double)magset.buf.X,
				       (double)magset.buf.Y);
      else
	label_file_coords((double)magset.buf.X, (double)magset.buf.Y);
    }
  } else if( text ) {
    float bufx, bufy;

    /* get buffer coordinates */
    bufx = ((float)event->xmotion.x * coord.disptobuf.inx_outx) +
      coord.disptobuf.iadd_outx;
    bufy = ((float)event->xmotion.y * coord.disptobuf.iny_outy) +
      coord.disptobuf.iadd_outy;
    if( magset.text.proportional )
	label_file_coords_proportional((double)bufx, (double)bufy);
    else
      label_file_coords((double)bufx, (double)bufy);
  }
}

/*
 * Subroutine:	magnify_pan
 * Purpose:	Magnify location of a panbox event
 * Xlib calls:	XCheckWindowEvent(), XSync()
 */
void magnify_pan ( event )
     XEvent *event;		/* i: XEvent for location of mouse */
{
  void draw_magnifier();

  /* get only the most recent mouse moved event */
  XSync(panbox.display, 0);
  while( XCheckWindowEvent(panbox.display, panbox.ID,
			   PointerMotionMask, event) );
  /* draw image fragment in scope magnibox */
  /* get buffer coordinates */
  magset.buf.X = (coord.imgtobuf.inx_outx *
		 (((float)event->xmotion.x * coord.pantoimg.inx_outx) +
		  coord.pantoimg.iadd_outx)) + coord.imgtobuf.add_outx;
  magset.buf.Y = (coord.imgtobuf.iny_outy *
		 (((float)event->xmotion.y * coord.pantoimg.iny_outy) +
		  coord.pantoimg.iadd_outy)) + coord.imgtobuf.add_outy;
  draw_magnifier((double)magset.buf.X, (double)magset.buf.Y);
}

/*
 * Subroutine:	redraw_magnifier
 * Purpose:	Draw image piece in zoombox window, using last coords or
 *		dispbox center
 */
void redraw_magnifier ( )
{
  void draw_magnifier(), d_transform();

  /* if called by a window redraw event, redraw as it last was */
  if( magset.buf.X < 0 )
    d_transform(&coord.disptobuf, (double)coord.disp.cenX,
		 (double)coord.disp.cenY, &magset.buf.X, &magset.buf.Y);
  draw_magnifier((double)magset.buf.X, (double)magset.buf.Y);
}

/*
 * Subroutine:	label_file_coords
 * Purpose:	Show pointer coordinates and image value in display window
 * Xlib calls:	XDrawImageString()
 */
static void label_file_coords ( bufX, bufY )
     double bufX, bufY;
{
  int val;
  static char string[48], cstring[64];
  int lstr = 48;
  int lwcs, lpix;
  float fileX, fileY;
  GC gc, set_edit_gc();
  void d_transform();
  static void draw_proportional_coord();
  static int lwcs0 = 0;
  static int lpix0 = 0;
  int i;
  int iswcs();

  gc = set_edit_gc(magset.text.font,
		   magset.text.foreground, magset.text.background);
  d_transform(&coord.buftofile, bufX, bufY, &fileX, &fileY);

  if (img.filenimg > 1) {
    if (iswcs(wcs) && (wcs->prjcode == WCS_CSC ||
	wcs->prjcode == WCS_QSC || wcs->prjcode == WCS_TSC))
      sprintf(cstring, " %6.1f %6.1f f%d", fileX, fileY, img.nimage-1);
    else
      sprintf(cstring, " %6.1f %6.1f %d", fileX, fileY, img.nimage);
    }
  else
    sprintf(cstring, " %6.1f %6.1f", fileX, fileY);

  /* Cursor is not on the image */
  if( (bufX < coord.buf.X1) || (bufX > coord.buf.X2) ||
      (bufY < coord.buf.Y1) || (bufY > coord.buf.Y2) ) {
    sprintf(string, "%s       x ", cstring);
    lpix = strlen (string);
    if (lpix < lpix0) {
      for (i=lpix; i<lpix0; i++)
	string[i] = ' ';
      lpix = lpix0;
      }
    else
      lpix0 = lpix;
    XDrawImageString(desktop.display, desktop.ID, gc, magset.text.x_x,
		     magset.text.y, string, lpix);

  /* Image values are scaled */
  } else if( img.fiscaled ) {
    double rval;
    if( (buffer.filebuf == NULL) ||
        (buffer.filebuf == (char *)buffer.shortbuf) ) {
      /* values scaled, originals not available */
      val = buffer.shortbuf[(int)bufX + ((int)bufY * coord.buf.width)];
      rval = ((double)val * img.fiscale) + img.fibias;
      /* print strings with spaces padding out the end */
      if( val <= buffer.clipmin )
	sprintf(string, "%s  <%.4g ", cstring, rval);
      else if( val >= buffer.clipmax )
	sprintf(string, "%s  >%.4g ", cstring, rval);
      else
	sprintf(string, "%s   %.4g ", cstring, rval);
    } else {
      /* values scaled, originals in filebuf */
      float fbX, fbY;
      d_transform(&coord.buftofbuf, bufX, bufY, &fbX, &fbY);
      if( img.storage_type == ARR_I4 ) {
	rval = (double)
	  *((int *)(buffer.filebuf +
		    (((int)fbX + ((int)fbY * coord.fbuf.width)) *
		     sizeof(int))));
      } else if( img.storage_type == ARR_R4 ) {
	rval = (double)
	  *((float *)(buffer.filebuf +
		      (((int)fbX + ((int)fbY * coord.fbuf.width)) *
		       sizeof(float))));
      } else if( img.storage_type == ARR_R8 ) {
	rval = *((double *)(buffer.filebuf +
			    (((int)fbX + ((int)fbY * coord.fbuf.width)) *
			     sizeof(double))));
      } else
	rval = 0.0;
      if( img.fscaled )
	rval = img.fbias + (rval * img.fscale);
      sprintf(string, "%s   %.4g ", cstring, rval);
    }
    lpix = strlen (string);
    if (lpix < lpix0) {
      for (i=lpix; i<lpix0; i++)
	string[i] = ' ';
      lpix = lpix0;
      }
    else
      lpix0 = lpix;
    XDrawImageString(desktop.display, desktop.ID, gc, magset.text.x_x,
		     magset.text.y, string, lpix);
    if (iswcs(wcs)) {
      pix2wcst (wcs,(double)fileX,(double)fileY,string,lstr);
      lwcs = strlen (string);
      if (lwcs < lwcs0) {
	for (i=lwcs; i<lwcs0; i++)
	  string[i] = ' ';
	lwcs = lwcs0;
	}
      else
	lwcs0 = lwcs;
      XDrawImageString(desktop.display, desktop.ID, gc, magset.text.x_x,
		       magset.text.y-20, string, lwcs);
      }

  /* Image values are straight from the file */
  } else {
    val = buffer.shortbuf[(int)bufX + ((int)bufY * coord.buf.width)];
    sprintf(string, "%s  %6d ", cstring, val);
    lpix = strlen (string);
    if (lpix < lpix0) {
      for (i=lpix; i<lpix0; i++)
	string[i] = ' ';
      lpix = lpix0;
      }
    else
      lpix0 = lpix;
    XDrawImageString(desktop.display, desktop.ID, gc, magset.text.x_x,
		     magset.text.y, string, 23);
    if (iswcs(wcs)) {
      pix2wcst (wcs,(double)fileX,(double)fileY,string,lstr);
      lwcs = strlen (string);
      if (lwcs < lwcs0) {
	for (i=lwcs; i<lwcs0; i++)
	  string[i] = ' ';
	lwcs = lwcs0;
	}
      else
        lwcs0 = lwcs;
      XDrawImageString(desktop.display, desktop.ID, gc, magset.text.x_x,
		       magset.text.y-20, string, lwcs);
      }
  }
}

/*
 * Subroutine:	clear_coord_area
 * Purpose:	Erase area of coords, esp. when imtool_aux is no longer used
 */
void clear_coord_area ()
{
  int height;
  height = magset.text.yoff * 2 + 1;
  XClearArea(desktop.display, desktop.ID, btnbox.y - (height + 1), 1,
	     panbox.x - 2, height, False);
}

/*
 * Subroutine:	label_file_coords_proportional
 * Purpose:	Show pointer coordinates and image value in display window
 *		Special handling for proporitonal fonts is good for the coords,
 *		but has not been refined for the val section.
 * Xlib call:	XDrawImageString()
 */
static void label_file_coords_proportional ( bufX, bufY )
     double bufX, bufY;
{
  int val;
  static char string[48];
  float fileX, fileY;
  GC gc, set_edit_gc();
  void d_transform();
  static void draw_proportional_number();

  gc = set_edit_gc(magset.text.font,
		   magset.text.foreground, magset.text.background);
  d_transform(&coord.buftofile, bufX, bufY, &fileX, &fileY);
  sprintf(string, "        %6.1f    ", fileX);
  draw_proportional_number(string, 8, magset.text.x_x, magset.text.width, gc);
  sprintf(string, "        %6.1f    ", fileY);
  draw_proportional_number(string, 8, magset.text.y_x, magset.text.width, gc);
  if( (bufX < coord.buf.X1) || (bufX > coord.buf.X2) ||
      (bufY < coord.buf.Y1) || (bufY > coord.buf.Y2) ) {
    sprintf(string, "                        x        ");
    draw_proportional_number(string, 24, magset.text.val_x,
			     2 * magset.text.width, gc);
  } else {
    double dval;
    int ival, clip, i;
    if( get_pixel_val((int)bufX, (int)bufY, &ival, &dval, &clip) ) {
      /* value is an integer */
      integer_string(ival, clip, &(string[16]), VAL_SZ);
    } else {
      real_string(dval, &string[17], VAL_SZ - 1);
      if( clip ) {
	if( clip > 0 )
	  string[16] = '>';
	else
	  string[16] = '<';
      } else
	string[16] = ' ';
    }
    for( i = 0; i < 16; i++ )
      string[i] = ' ';
    for( i = 16 + VAL_SZ; i < (VAL_SZ + 20); i++ )
      string[i] = ' ';
    draw_proportional_number(string, 16, magset.text.val_x,
			     2 * magset.text.width, gc);
  }
}

/*
 * Subroutine:	draw_proportional_coord
 * Purpose:	Draw proportional text to cover area and place decimal point
 */
static void draw_proportional_number ( string, first_num, x, width, gc )
     char *string;
     int first_num;	/* i: where number begins (after leading spaces) */
     int x;
     int width;		/* i: pixel width to fill with label */
     GC gc;
{
  int size, count, offset;
  int letter, leading, not_done;

  leading = 1;
  not_done = 1;
  /* do a quick XTextWidth using a restricted table */
  for( count = 0, size = 0; not_done; count++ ) {
    letter = string[count + first_num];
    if( letter == ' ' ) {
      if( leading )
	size += magset.text.space;
      else
	/* label ends at first space after text */
	not_done = 0;
    } else {
      leading = 0;
      if( (letter >= '0') && (letter <= '9') )
	/* sizes of numbers */
	size += magset.text.numsz[letter - '0'];
      else if( letter == '.' )
	size += magset.text.dot;
      else if( (letter == '-') || (letter == '+') )
	size += magset.text.dash;
      else if( letter == '\0' ) {
	char *space;
	/* oops, no trailing spaces - put them in */
	space = &(string[count + first_num]);
	*space = ' ';
	*(space + 1) = ' ';
	*(space + 2) = '\0';
	not_done = 0;
      } else
	/* this covers whatever else ('e', 'E', '<'. '>') */
	size += magset.text.e;
    }
  }
  /* determine number of extra spaces to cover area and their x offset */
  offset = (width - size) / magset.text.space;
  x += (width - (size + (offset * magset.text.space)));
  if( (first_num -= offset) < 0 ) {
    /* move starting posisiont forward, shorten string */
    x -= (first_num * magset.text.space);
    count += first_num;
    first_num = 0;
  }
  count += (offset + 2);
  XDrawImageString(desktop.display, desktop.ID, gc, x,
		   magset.text.y, &(string[first_num]), count);
}

/*
 * Subroutine:	blank_scope
 * Purpose:	Fill scope with blank data
 */
void blank_scope()
{
  GC gc, set_gc();
  void mark_Zmagnifier();

  /* blank field */
  bzero(magset.data, magset.data_size);
  if( !magset.halftone ) {
    /* install the sighting mark */
    mark_Zmagnifier();
  } else {
#ifdef NOTYET /* %% */
    mark_XYmagnifier();
#endif
  }
  gc = set_gc(magset.gcset_disp);
  XPutImage(magset.win.display, magset.win.ID, gc, magset.image,
	    0, 0, magset.win.x, magset.win.y,
	    magset.win.width, magset.win.height);
}