aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/tv/tvmark/mkmark.x
blob: 72583fcb7a78ee64a4d622fd451156e937abf2ad (plain) (blame)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
include <fset.h>
include <imhdr.h>
include "tvmark.h"

define	HELPFILE	"iraf$lib/scr/tvmark.key"

# MK_MARK -- Procedure to mark symbols in the frame buffer interactively.

int procedure mk_mark (mk, im, iw, cl, dl, log, fnt, autolog, interactive)

pointer	mk		# pointer to the mark structure
pointer	im		# frame image descriptor
pointer	iw		# pointer to the wcs structure
int	cl		# coordinate file descriptor
int	dl		# pointer to the deletions file
int	log		# output log file descriptor
int	fnt		# font file descriptor
int	autolog		# automatic logging enabled
int	interactive	# interactive mode

int	ncmd, ncols, nlines, nc, nr
int	wcs, bkey, skey, vkey, ekey, fkey, okey, key
int	id, ltid, ndelete, req_num, lreq_num, prev_num, newlist
pointer	sim, sp, scratchim, cmd, str, keepcmd, label
real	cwx, cwy, wx, wy, owx, owy, fx, fy, ofx, ofy
real	xlist, ylist, oxlist, oylist, rmax

int	imd_gcur(), mk_stati(), strdic(), mk_gscur(), nscan(), mk_new()
int	mk_find(), fstati()
real	mk_statr()

begin
	# Allocate working memory.
	call smark (sp)
	call salloc (scratchim, SZ_FNAME, TY_CHAR)
	call salloc (cmd, SZ_LINE, TY_CHAR)
	call salloc (keepcmd, SZ_LINE, TY_CHAR)
	call salloc (str, SZ_LINE, TY_CHAR)
	call salloc (label, SZ_LINE, TY_CHAR)

	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)

	sim = NULL

	# Reinitialize.
	ekey = ' '
	fkey = ' '
	okey = ' '
	skey = ' '
	vkey = ' '
	bkey = ' '
	ltid = 0
	ndelete = 0
	newlist = NO
	owx = INDEFR
	owy = INDEFR
	Memc[cmd] = EOS
	Memc[keepcmd] = EOS

	while (imd_gcur ("commands", wx,wy,wcs,key,Memc[cmd],SZ_LINE) != EOF) {

	    # Save current cursor coordinates.
	    cwx = wx
	    cwy = wy

	    # Check for new object.
	    if (mk_new (wx, wy, owx, owy, xlist, ylist, newlist) == YES)
		;

	    # Transform to frame buffer coordinates.
	    call iw_im2fb (iw, wx, wy, fx, fy)

	    switch (key) {

	    # Print the help page.
	    case '?':
		if (interactive == YES)
		    call pagefile (HELPFILE, "Type ? for help, q to quit")

	    # Quit the task.
	    case 'q':
		break

	    # Keep the previous cursor command.
	    case 'k':
		if (log != NULL)
		    if (autolog == YES)
			call printf ("Automatic logging is already enabled.\n")
		    else
		        call mk_logcmd (log, Memc[keepcmd])
		else {
		    if (interactive == YES)
		        call printf ("The log file is undefined.\n")
		}

	    # Rewind the coordinate list.
	    case 'o':
	        if (cl != NULL) {
		    call seek (cl, BOF)
		    oxlist = INDEFR
		    oylist = INDEFR
		    ltid = 0
	        } else if (interactive == YES)
		    call printf ("Coordinate list is undefined.\n")

	    # Move to the previous object.
	    case '-':
		if (cl != NULL) {
		    prev_num = ltid
		    req_num = ltid - 1
		    if (req_num < 1) {
			if (interactive == YES)
			    call printf ("Requested object is less than 1.\n")
		    } else if (mk_gscur (cl, NULL, xlist, ylist, Memc[label],
		        prev_num, req_num, ltid) != EOF) {
			if (interactive == YES)
			    call printf ("Moved to object: %d  %g  %g\n")
				call pargi (ltid)
				call pargr (xlist)
				call pargr (ylist)
			newlist = YES
		    } else if (interactive == YES)
			call printf (
			    "End of coordinate list, type o to rewind.\n")
		} else if (interactive == YES) 
		    call printf ("Coordinate file is undefined.\n")

	    # Mark the previous object.
	    case 'p':
		if (cl != NULL) {
		    prev_num = ltid
		    req_num = ltid - 1
		    if (req_num < 1) {
			if (interactive == YES)
			    call printf ("Requested object is less than 1.\n")
		    } else if (mk_gscur (cl, NULL, xlist, ylist, Memc[label],
		        prev_num, req_num, ltid) != EOF) {
			call mk_onemark (mk, im, iw, xlist, ylist, oxlist,
			    oylist, Memc[label], ltid)
			newlist = YES
		    } else if (interactive == YES) {
			call printf (
			    "End of coordinate list, type o to rewind.\n")
		    }
		} else if (interactive == YES) 
		    call printf ("Coordinate file is undefined.\n")

	    # Move to the next object.
	    case 'm':
		if (cl != NULL) {
		    prev_num = ltid
		    req_num = ltid + 1
		    if (mk_gscur (cl, NULL, xlist, ylist, Memc[label],
		        prev_num, req_num, ltid) != EOF) {
			if (interactive == YES)
			    call printf ("Moved to object: %d  %g  %g\n")
				call pargi (ltid)
				call pargr (xlist)
				call pargr (ylist)
			newlist = YES
		    } else if (interactive == YES)
			call printf (
			    "End of coordinate list, type o to rewind.\n")
		} else if (interactive == YES) 
		    call printf ("Coordinate file is undefined.\n")

	    # Mark the next object.
	    case 'n':
		if (cl != NULL) {
		    prev_num = ltid
		    req_num = ltid + 1
		    if (mk_gscur (cl, NULL, xlist, ylist, Memc[label],
		        prev_num, req_num, ltid) != EOF) {
			call mk_onemark (mk, im, iw, xlist, ylist, oxlist,
			    oylist, Memc[label], ltid)
			newlist = YES
		    } else if (interactive == YES)
			call printf (
			    "End of coordinate list, type o to rewind.\n")
		} else if (interactive == YES) 
		    call printf ("Coordinate file is undefined.\n")

	    # Mark the entire list.
	    case 'l':
	        if (cl != NULL) {
		    call seek (cl, BOF)
		    ltid = 0
		    call mk_bmark (mk, im, iw, cl, ltid, fnt)
	        } else if (interactive == YES)
		    call printf ("Coordinate list is undefined.\n")

	    # Append to the coordinate list.
	    case 'a':
		if (cl == NULL) {
		    if (interactive == YES)
		        call printf ("Coordinate file is undefined.\n")
		} else if (fstati (cl, F_MODE) != READ_WRITE) {
		    if (interactive == YES)
			call printf (
			"No write permission on coordinate file.\n")
		} else  {

		    # Move to the end of the list.
		    prev_num = ltid
		    req_num = ltid + 1
		    while (mk_gscur (cl, NULL, xlist, ylist, Memc[label],
		        prev_num, req_num, ltid) != EOF) {
			prev_num = ltid
			req_num = ltid + 1
		    }

		    # Add the object.
		    call fprintf (cl, "%g %g\n")
			call pargr (wx)
			call pargr (wy)
		    call flush (cl)
		    ltid = ltid + 1
		    #call seek (cl, EOF)

		    # Mark the object.
		    call mk_onemark (mk, im, iw, wx, wy, oxlist, oylist, "",
		        ltid)

		} 

	    # Delete an object.
	    case 'd':
		if (cl == NULL) {
		    if (interactive == YES)
		        call printf ("Coordinate file is undefined.\n")
		} else if (fstati (cl, F_MODE) != READ_WRITE) {
		    if (interactive == YES)
			call printf (
			"No write permission on coordinate file.\n")
		} else {

		    # Find the nearest object to the cursor and delete.
		    if (mk_find (cl, wx, wy, xlist, ylist, Memc[label], id,
		        ltid, mk_statr (mk, TOLERANCE)) > 0) {
		        call fprintf (dl, "%d\n")
			    call pargi (id)
		        ndelete = ndelete + 1
		        call mk_onemark (mk, im, iw, xlist, ylist, oxlist,
			    oylist, Memc[label], ltid)
		    } else if (interactive == YES)
			call printf ("Object not in coordinate list.\n")

		}

	    # Draw a dot.
	    case '.':
		call mk_dmark (mk, im, fx, fy) 

	    # Draw a plus sign.
	    case '+':
		call mk_tmark (mk, im, "+", fx, fy, YES)

	    # Draw a cross.
	    case 'x':
		call mk_tmark (mk, im, "x", fx, fy, YES)

	    # Mark and erase a region.
	    case 'e':
		if (sim != NULL) {
		    if ((key == ekey) && (okey == 'e' || okey == 'k')) {
			call mk_imsection (mk, sim, im, nint (ofx), nint (fx),
			    nint (ofy), nint (fy))
			ekey = ' '
		    } else {
		        if (interactive == YES)
		            call printf ("Type e again to define region.\n")
		        ekey = key
		        ofx = fx
		        ofy = fy
		    }
		} else if (interactive == YES)
		    call printf ("Define a scratch image with :save.\n")

	    # Fill region
	    case 'f':
		if ((key == fkey) && (okey == 'f' || okey == 'k')) {
		    call mk_imsection (mk, NULL, im, nint (ofx), nint (fx),
			nint (ofy), nint (fy))
		    fkey = ' '
		} else {
		    if (interactive == YES)
		        call printf ("Type f again to define region.\n")
		    fkey = key
		    ofx = fx
		    ofy = fy
		}

	    # Mark a single circle.
	    case 'v':
		if ((key == vkey) && (okey == 'v' || okey == 'k')) {
		    rmax = sqrt ((fx - ofx) ** 2 + (fy - ofy) ** 2)
		    call mk_ocmark (mk, im, iw, ofx, ofy, rmax)
		    vkey = ' '
		} else {
		    if (interactive == YES)
		        call printf ("Type v again to draw circle.\n")
		    vkey = key
		    ofx = fx
		    ofy = fy
		}

	    # Draw concentric circles.
	    case 'c':
		nc = mk_stati (mk, NCIRCLES)
		if (nc > 0) {
		    call mk_cmark (mk, im, iw, fx, fy)
		} else if (interactive == YES)
		    call printf ("Use :radii to specifiy radii.\n")

	    # Draw concentric rectangles.
	    case 'r':
		nr = mk_stati (mk, NRECTANGLES)
		if (nr > 0) {
		    call mk_rmark (mk, im, iw, fx, fy)
		} else if (interactive == YES)
		    call printf ("Use :lengths to specify box lengths.\n")

	    # Draw a vector segment.
	    case 's':
		if ((skey == key) && (okey == 's' || okey == 'k'))
		    call mk_lmark (mk, im, ofx, ofy, fx, fy)
		if (interactive == YES)
		    call printf ("Type s again to draw line segment.\n")
		ofx = fx
		ofy = fy
		skey = key

	    # Draw a box
	    case 'b':
		if ((key == bkey) && (okey == 'b' || okey == 'k')) {
		    call mk_xmark (mk, im, ofx, ofy, fx, fy)
		    bkey = ' '
		} else {
		    if (interactive == YES)
		        call printf ("Type b again to draw box.\n")
		    bkey = key
		    ofx = fx
		    ofy = fy
		}

	    # Execute the colon command.
	    case ':':
		call sscan (Memc[cmd])
		    call gargwrd (Memc[str], SZ_LINE)
		ncmd = strdic (Memc[str], Memc[str], SZ_LINE, MKCMDS2)

		if (ncmd <= 0)
		    call mk_colon (mk, Memc[cmd], im, iw, sim, log, cl, ltid,
		        dl)

		else if (ncmd == MKCMD2_WTEXT) {
		    call gargstr (Memc[str], SZ_LINE)
		    if (Memc[str] != EOS)
			call mk_tmark (mk, im, Memc[str], fx, fy, NO)

		} else if (ncmd == MKCMD2_MOVE) {
		    if (cl != NULL) {
		        call gargi (req_num)
			prev_num = ltid
			if (nscan () < 2)
			    req_num = ltid + 1
			if (req_num < 1) {
			    if (interactive == YES)
				call printf (
				"Requested object is less than 1.\n")
		        } else if (mk_gscur (cl, NULL, xlist, ylist,
			    Memc[label], prev_num, req_num, ltid) != EOF) {
			    if (interactive == YES)
			        call printf ("Moved to object: %d  %g  %g\n")
				    call pargi (ltid)
				    call pargr (xlist)
				    call pargr (ylist)
			    newlist = YES
		        } else if (interactive == YES) {
			    call printf (
			    	"End of coordinate list, type o to rewind.\n")
			}
		    } else if (interactive == YES)
			call printf ("Coordinate file is undefined.\n")

		} else if (ncmd == MKCMD2_NEXT) {
		    if (cl != NULL) {
			call gargi (req_num)
			call gargi (lreq_num)
			prev_num = ltid
			if (nscan () < 2) {
			    req_num = ltid + 1
			    lreq_num = req_num
			} else if (nscan () < 3)
			    lreq_num = req_num
		        while (mk_gscur (cl, NULL, xlist, ylist, Memc[label],
			    prev_num, req_num, ltid) != EOF) {
			    if (ltid > lreq_num)
				break
			    call mk_onemark (mk, im, iw, xlist, ylist, oxlist,
			        oylist, Memc[label], ltid)
			    newlist = YES
			    prev_num = ltid
			    req_num = ltid + 1
			}
		    } else if (interactive == YES)
			call printf ("Coordinate field is undefined.\n")
		}

	    default:
		call printf ("Unrecognized keystroke command.\7\n")
	    }

	    # Encode and log the last cursor command. Do not encode any
	    # keep commands if autologging is turned off.

	    if (autolog == YES) {
	        call mk_encodecmd (wx, wy, wcs, key, Memc[cmd], Memc[keepcmd])
		if (log == NULL) {
		    if (interactive == YES)
		        call printf ("The logfile is undefined.\n")
		} else
		    call mk_logcmd (log, Memc[keepcmd])
	    } else if (key != 'k')
	        call mk_encodecmd (wx, wy, wcs, key, Memc[cmd], Memc[keepcmd])

	    # Get set up for next cursor command.
	    owx = cwx
	    owy = cwy
	    okey = key
	    Memc[cmd] = EOS
	    if (newlist == YES) {
	        oxlist = xlist
	        oylist = ylist
	    }
	}

	# Delete scratch image.
	if (sim != NULL) {
	    call strcpy (IM_HDRFILE(sim), Memc[scratchim], SZ_FNAME)
	    call imunmap (sim)
	    call imdelete (Memc[scratchim])
	}

	call sfree (sp)

	return (ndelete)
end


# MK_ENCODECMD -- Encode the cursor command.

procedure mk_encodecmd (wx, wy, wcs, key, cmd, keepcmd)

real	wx, wy		# cursor position
int	wcs		# world coordinate system
int	key		# cursor keystroke command
char	cmd[ARB]	# command
char	keepcmd[ARB]	# encode cursor command

begin
	call sprintf (keepcmd, SZ_LINE,  "%g  %g  %d  %c  %s")
	    call pargr (wx)
	    call pargr (wy)
	    call pargi (wcs)
	    call pargi (key)
	    call pargstr (cmd)
end


# MK_LOGCMD -- Log the command.

procedure mk_logcmd (log, cmd)

int	log		# logfile descriptor
char	cmd[ARB]	# command

begin
	call fprintf (log, "%s\n")
	    call pargstr (cmd)
end