Repository navigation
Expand file tree
/
Copy pathimd.c
More file actions
1953 lines (1632 loc) · 54.1 KB
/
Copy pathimd.c
File metadata and controls
1953 lines (1632 loc) · 54.1 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <netdb.h>
#include <fcntl.h>
#include <stdio.h>
#define CDL_LIBRARY_SOURCE
#include "cdl.h"
/*
* IMAGE DISPLAY -- The image display interface is responsible for actually
* displaying an image to the server, for reading back a raster from the
* server, and cursor positioning. This is a mid-level interface for
* handling the steps necessary for image display operations without dealing
* directly with the details of communicating with the server.
*
* imd = imd_open (imtdev)
* imd_displayImage (imd, pix, nx, ny, frame, fbconfig, comp_wcs)
* imd_readCursor (imd, sample, &x, &y, &wcs, &key)
* imd_[set|get]WCS (imd, name, title, a, b, c, d, tx, ty, z1, z2, zt)
* imd_[set|get]Mapping (imd, region, sx,sy,snx,sny, dx,dy,dnx,dny, ref)
* imd_close (imd)
*
* Low Level Procedures
* --------------------
* imd_writeImage (imd, pix, nx, ny, lx, ly)
* imd_readImage (imd, &pix, &nx, &ny)
* imd_readFrameBuffer (imd, &pix, &nx, &ny)
* imd_setFrame (imd, frame)
* imd_setFBConfig (imd, configno)
* imd_getFBConfig (imd, &configno, &width, &height, &nframes))
* imd_setName (imd, name)
* imd_setTitle (imd, title)
* imd_setCursor (imd, x, y, wcs)
* imd_clearFrame (imd)
* imd_writeSubRaster (imd, lx, ly, nx, ny, pix)
* imd_readSubRaster (imd, lx, ly, nx, ny, &pix)
*
* We leave it to the higher level procedures to handle Z-scale trans-
* formations, spatial scaling, and high level image I/O. All display pixels
* are assumed to be scaled to 8-bits already.
*/
/* Function prototypes */
#ifdef __STDC__
#include <stddef.h>
#include <stdlib.h>
#endif
#define SZ_BLOCK 16384
/* Types of coordinate and greyscale transformations. */
#define W_UNITARY 0 /* values map without change */
#define W_LINEAR 1 /* linear mapping */
#define W_LOG 2 /* logarithmic mapping */
#define W_USER 3 /* user transformation */
/* Connection types. */
#define UNIX 10
#define INET 11
#define FIFO 12
/* Default Values. */
#define DEF_FBCONFIG 1 /* default frame buffer config */
#define DEF_OSDEV_1 "unix:/tmp/.IMT%d"
#define DEF_OSDEV_2 "fifo:/dev/imt1i:/dev/imt1o"
/* Frame buffer configuration file definitions. */
#define FBCONFIG_1 ".imtoolrc"
#define FBCONFIG_2 "/usr/local/lib/imtoolrc"
#define FBCONFIG_ENV1 "imtoolrc"
#define FBCONFIG_ENV2 "IMTOOLRC"
#define DEF_FRAME_WIDTH 512
#define DEF_FRAME_HEIGHT 512
int imd_debug = 0; /* deug flag */
char buf[SZ_LINE]; /* temp buffer */
#ifdef ANSI_FUNC
static int imd_writeLine(IMDPtr imd, uchar *pix, int nbytes, int x, int y);
static int imd_readLine(IMDPtr imd, uchar *pix, int nbytes, int x, int y);
static IMDPtr imd_initialize(int fdin, int fdout, int domain);
static int imd_parseImtdev(char *imtdev, char *unixaddr, unsigned short *host_port, unsigned long *host_addr, char *ififo, char *ofifo);
static int imd_loadImtoolrc(IMDPtr imd);
static int imd_getstr(char **ipp, char *obuf, int maxch);
static void imd_minmax(uchar *pix, int nbytes, int *pmin, int *pmax);
static int imd_wcsVersion (IMDPtr imd);
#else
static IMDPtr imd_initialize();
static int imd_parseImtdev(), imd_loadImtoolrc(), imd_getstr();
static int imd_writeLine(), imd_readLine(), imd_wcsVersion();
static void imd_minmax();
#endif
#ifndef __STDC__
char *getenv();
#endif
/* IMD_OPEN -- Open a connection to the display server. The caller may
* either specify a connection at device open time, or the procedure will
* attempt to first connect on a unix socket or fifo pipe if that fails.
* The syntax for the imtdev argument is as follows:
*
* <domain> : <address>
*
* where <domain> is one of "inet" (internet tcp/ip socket), "unix" (unix
* domain socket) or "fifo" (named pipe). The form of the address depends
* upon the domain, as illustrated in the examples below.
*
* inet:5137 Server connection to port 5137 on the local
* host. For a client, a connection to the
* given port on the local host.
*
* inet:5137:foo.bar.edu Client connection to port 5137 on internet
* host foo.bar.edu. The dotted form of address
* may also be used.
*
* unix:/tmp/.IMT212 Unix domain socket with the given pathname
* IPC method, local host only.
*
* fifo:/dev/imt1i:/dev/imt1o FIFO or named pipe with the given pathname.
* IPC method, local host only. Two pathnames
* are required, one for input and one for
* output, since FIFOs are not bidirectional.
* For a client the first fifo listed will be
* the client's input fifo; for a server the
* first fifo will be the server's output fifo.
* This allows the same address to be used for
* both the client and the server, as for the
* other domains.
*
* The address field may contain up to two "%d" fields. If present, the
* user's UID will be substituted (e.g. "unix:/tmp/.IMT%d"). The default
* connection if no imtdev is specified is "unix:/tmp/.IMT%d", failing that,
* a connection is attempted on the /dev/imt1[io] named fifo pipes.
*/
#ifdef ANSI_FUNC
IMDPtr
imd_open (
char *imtdev /* connection type */
)
#else
IMDPtr
imd_open (imtdev)
char *imtdev; /* connection type */
#endif
{
IMDPtr imd;
int domain, fd, fdin, fdout, free_imtdev=0;
unsigned short host_port;
unsigned long host_addr;
char unixaddr[SZ_NAME];
char input_fifo[SZ_NAME], output_fifo[SZ_NAME];
if (imtdev == NULL) {
struct sockaddr_un sockaddr;
/* Try first to connect on a unix socket. */
if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
imtdev = DEF_OSDEV_2;
if (imd_debug)
printf ("Can't get unix socket...\n");
goto retry;
}
/* Compose network address. */
bzero ((char *)&sockaddr, sizeof(sockaddr));
sockaddr.sun_family = AF_UNIX;
sprintf (sockaddr.sun_path, "/tmp/.IMT%d", (int)getuid());
if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))<0){
close (fd);
imtdev = DEF_OSDEV_2;
if (imd_debug)
printf ("Can't connect to socket '%s'\n",sockaddr.sun_path);
goto retry; /* no connection */
} else {
imtdev = (char *) calloc (SZ_IMTDEV, sizeof(char));
free_imtdev++;
strcpy (imtdev, sockaddr.sun_path);
fdin = fdout = fd;
}
} else {
retry: domain = imd_parseImtdev (imtdev, unixaddr, &host_port, &host_addr,
input_fifo, output_fifo);
switch (domain) {
case FIFO:
/* Open the fifos. */
if ((fdin = open (input_fifo, O_RDONLY|O_NDELAY)) != ERR)
fcntl (fdin, F_SETFL, O_RDONLY);
if ((fdout = open (output_fifo, O_WRONLY|O_NDELAY)) != ERR)
fcntl (fdout, F_SETFL, O_WRONLY);
/* Clean up if there is an error. */
if (fdin < 0 || fdout < 0) {
(void) close (fdin);
(void) close (fdout);
if (imd_debug)
printf ("Can't connect to fifo '%s'\n",input_fifo);
goto err;
}
break;
case UNIX:
{ struct sockaddr_un sockaddr;
/* Try first to connect on a unix socket. */
if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
if (imd_debug)
printf ("Can't get unix socket...\n");
goto err;
}
/* Compose network address. */
bzero ((char *)&sockaddr, sizeof(sockaddr));
sockaddr.sun_family = AF_UNIX;
strcpy (sockaddr.sun_path, unixaddr);
if (connect(fd, (struct sockaddr *)&sockaddr,
sizeof(sockaddr))<0){
close (fd);
if (imd_debug)
printf ("Can't connect to socket '%s'\n",
sockaddr.sun_path);
goto err; /* no connection */
} else
fdin = fdout = fd;
}
break;
case INET:
{ struct sockaddr_in sockaddr;
/* Get socket. */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
if (imd_debug)
printf ("Can't get inet socket...\n");
goto err;
}
/* Compose network address. */
bzero ((char *)&sockaddr, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = host_port;
bcopy ((char *)&host_addr, (char *)&sockaddr.sin_addr,
sizeof(host_addr));
/* Connect to server. */
if (connect (fd, (struct sockaddr *)&sockaddr,
sizeof(sockaddr)) < 0) {
close (fd);
if (imd_debug)
printf ("Can't connect to socket %d\n",
(int) host_port);
goto err;
} else
fdin = fdout = fd;
}
break;
default:
goto err;
}
}
/* Allocate and initialize imd structure. */
imd = imd_initialize (fdin, fdout, domain);
if (imd_debug)
fprintf (stderr, "Connection established on '%s'\n", imtdev);
if (free_imtdev)
free ((char *)imtdev);
return (imd);
err:
fprintf (stderr, "Cannot open server connection on '%s'.\n", imtdev);
if (free_imtdev)
free ((char *)imtdev);
return NULL;
}
/* IMD_DISPLAYIMAGE -- Display an image to the server, setting the WCS and
* frame as needed. This is a high-level procedure used to make image
* display easy. It is assumed that the pixel array has already been scaled
* to 8-bits.
*/
int
imd_displayImage (imd, pix, nx, ny, frame, fbconfig, comp_wcs)
IMDPtr imd; /* package pointer */
uchar *pix; /* pixels to display */
int nx, ny; /* dimensions */
int frame; /* frame to display */
int fbconfig; /* fb config number */
int comp_wcs; /* compute a WCS */
{
register uchar *ip = pix;
register uchar *bp, *pp;
register int i, nnx = nx, nny = ny;
int status, use_subras = 0;
int pmin, pmax, x_off = 0, y_off = 0, lx, ly;
int fbwidth = imd->fbtab[fbconfig-1]->width;
int fbheight = imd->fbtab[fbconfig-1]->height;
int imd_writeImage();
if (imd_debug)
printf ("[imd_displayImage] frame=%d fb=%d->[%d,%d] %dx%d bytes\n",
frame, fbconfig, fbwidth, fbheight, nx, ny);
imd_setFrame (imd, frame); /* select frame */
imd_clearFrame (imd); /* erase frame */
imd_setFBConfig (imd, fbconfig); /* set fbconfig */
/* Check to see if the image to be displayed is larger than the
* current frame buffer. If so we'll need to pull out a subraster
* the size of the FB for display.
*/
if (nx > fbwidth || ny > fbheight) {
nnx = min(nx, fbwidth); /* get new dimensions */
nny = min(ny, fbheight);
bp = ip = (uchar *) calloc (nnx * nny, sizeof (uchar));
use_subras++;
/* Pull out the subraster for display */
x_off = (nx>fbwidth ? ((nx - fbwidth) / 2 - 1) : 0);
y_off = (ny>fbheight ? ((ny - fbheight) / 2 - 1) : 0);
pp = pix + (y_off * nx) + x_off;
for (i=0; i < nny; i++) {
bcopy (pp, bp, nnx);
pp += nx;
bp += nnx;
}
}
if (imd_debug)
printf("[imd_displayImage] nnx=%d nny=%d xo=%d yo=%d\n",
nnx, nny, x_off, y_off);
/* Center the placement in user coords.
*/
lx = (fbwidth / 2) - (nnx / 2);
ly = fbheight - ((fbheight / 2) + (nny / 2));
/* Compute a WCS for this image if it's not already defined. */
if (comp_wcs) {
imd->a = 1.0;
imd->b = 0.0;
imd->c = 0.0;
imd->d = -1.0;
imd->tx = (float) (nnx / 2) - (fbwidth / 2) + 1 + (float) x_off;
imd->ty = (float) (fbheight / 2) + (nny / 2) + (float) y_off;
if (imd->z1 == INDEF || imd->z2 == INDEF) {
imd_minmax (pix, nnx*nny, &pmin, &pmax);
imd->z1 = (imd->z1 == INDEF) ? (float) pmin : imd->z1;
imd->z2 = (imd->z2 == INDEF) ? (float) pmax : imd->z2;
}
imd->ztrans = W_LINEAR;
/* Initialize the mapping. */
imd->sx = 1.0;
imd->sy = 1.0;
imd->snx = nnx;
imd->sny = nny;
imd->dx = lx;
imd->dy = ly;
imd->dnx = nnx;
imd->dny = nny;
imd->iis_valid = 1;
if (imd_setWCS (imd, imd->name, imd->title, imd->a, imd->b, imd->c,
imd->d, imd->tx, imd->ty, imd->z1, imd->z2, imd->ztrans))
return (ERR);
}
/* Finally, display the image.
*/
status = imd_writeImage (imd, ip, nnx, nny, lx, ly);
if (use_subras)
free ((char *)ip);
return (status);
}
/* IMD_READCURSOR -- Read the current cursor position. If sample is defined
* logical cursor position will be sampled and returned immediately, otherwise
* the server will block until a key is hit and we return that value as well.
*/
#ifdef ANSI_FUNC
int
imd_readCursor (
IMDPtr imd, /* package pointer */
int sample, /* wait for keystroke? */
float *x,
float *y, /* position */
int *wcs, /* WCS */
char *key /* keystroke */
)
#else
int
imd_readCursor (imd, sample, x, y, wcs, key)
IMDPtr imd; /* package pointer */
int sample; /* wait for keystroke? */
float *x, *y; /* position */
int *wcs; /* WCS */
char *key; /* keystroke */
#endif
{
if (imd_debug)
printf ("[imd_readCursor]\n");
return (com_readCursor(imd->datain, imd->dataout,
sample, x, y, wcs, key));
}
/* IMD_SETWCS -- Set the WCS of the screen. The WCS is passed in a string
* defined as:
* Image_Name_String\n a b c d tx ty z1 z2 zt
* where:
* X' = a*X + c*Y + tx
* Y' = b*X + d*Y + ty
*
* z1 is the minimum pixel value, z2 is the maximum pixel value, zt
* defines the type of transformation to use.
*/
#ifdef ANSI_FUNC
int
imd_setWCS (
IMDPtr imd, /* package pointer */
char *name, /* name string */
char *title, /* title string */
float a,
float b,
float c,
float d, /* WCS values */
float tx,
float ty, /* translation */
float z1,
float z2, /* zscale values */
int zt /* transformation type */
)
#else
int
imd_setWCS (imd, name, title, a, b, c, d, tx, ty, z1, z2, zt)
IMDPtr imd; /* package pointer */
char *name; /* name string */
char *title; /* title string */
float a, b, c, d; /* WCS values */
float tx, ty; /* translation */
float z1, z2; /* zscale values */
int zt; /* transformation type */
#endif
{
int nbytes = 0, status = 0, version = imd->iis_version;
char old_wcs[SZ_OLD_WCSBUF], mapping[SZ_OLD_WCSBUF];
char wcstext[SZ_WCSBUF];
/* Format the string to set world coordinate parameters. */
(void) sprintf (old_wcs, "%s%s%s\n%g %g %g %g %g %g %g %g %d\n",
(*name ? name : " "), (title ? " - " : " "), (*title ? title : " "),
a, b, c, d, tx, ty, z1, z2, zt);
if (imd->iis_version > 0 && imd->iis_valid) {
char *path_prefix, *node, *path;
#ifdef SOLARIS
char *getcwd();
#else
char *getwd();
#endif
path_prefix = (char *) calloc (1025, sizeof(char));
path = (char *) calloc (512, sizeof(char));
node = (char *) calloc (512, sizeof(char));
/* Get a node!path prefix for the data file. */
gethostname (node, SZ_FNAME);
#ifdef SOLARIS
(void) getcwd (path, 511);
#else
(void) getwd (path);
#endif
if (*name == '/')
(void) sprintf (path_prefix, "%s!%s", node, name);
else
(void) sprintf (path_prefix, "%s!%s/%s", node, path, name);
(void) sprintf (mapping, "%s %g %g %d %d %d %d %d %d\n%s\n",
(*imd->region ? imd->region : "image"),
imd->sx, imd->sy, imd->snx, imd->sny,
imd->dx, imd->dy, imd->dnx, imd->dny,
(*imd->ref ? imd->ref : path_prefix));
(void) sprintf (wcstext, "%s%s", old_wcs, mapping);
free ((char *)path_prefix);
free ((char *)path);
free ((char *)node);
} else
(void) strcpy (wcstext, old_wcs);
if (imd_debug) { printf ("[imd_setWCS] wcs='%s'\n", wcstext); }
/* Send the WCS buffer. */
nbytes = strlen (wcstext) + 1;
status = com_writeWCS (imd->dataout, wcstext, nbytes, version);
/* Invalidate the mapping once it's been sent. */
imd->iis_valid = 0;
return (status);
}
#ifdef USE_OLD_GETWCS
/* IMD_OLDGETWCS -- Get the current display frame WCS information.
*/
#ifdef ANSI_FUNC
int
imd_getWCS (
IMDPtr imd, /* package pointer */
int wcs, /* WCS number to get */
char *name, /* name string */
char *title, /* title string */
float *a,
float *b,
float *c,
float *d, /* WCS values */
float *tx,
float *ty, /* translation */
float *z1,
float *z2, /* zscale values */
int *zt /* transformation type */
)
#else
int
imd_getWCS (imd, wcs, name, title, a, b, c, d, tx, ty, z1, z2, zt)
IMDPtr imd; /* package pointer */
int wcs; /* WCS number to get */
char *name; /* name string */
char *title; /* title string */
float *a, *b, *c, *d; /* WCS values */
float *tx, *ty; /* translation */
float *z1, *z2; /* zscale values */
int *zt; /* transformation type */
#endif
{
int datain = imd->datain;
int dataout = imd->dataout;
int nbytes = 0, version = imd->iis_version;
int tokens, status = 0;
char wcs_info[SZ_WCSBUF];
/* Read the WCS from the server. */
if (com_readWCS (datain, dataout, wcs_info, &nbytes, wcs, version))
return (ERR);
/* Parse the return buffer. */
name[0] = title[0] = '\0';
if (version == 0) {
tokens = sscanf (wcs_info, "%[^\n]\n%g%g%g%g%g%g%g%g%d",
buf, a, b, c, d, tx, ty, z1, z2, zt);
if (tokens == EOF) {
/* Had no WCS info, can't even get name, but no error. */
*a = *b = *c = *d = *tx = *ty = *z1 = *z2 = *zt = 0;
} else if (tokens < 10) {
/* partial read, something must be wrong. */
return (ERR);
}
sscanf (buf, "%s - %s", name, title);
}
if (imd_debug) {
printf ("[imd_getWCS] wcs=%d wcsbuf='%s'\n", wcs, wcsbuf);
}
return (OK);
}
#endif
/* IMD_GETWCS -- Get the current display frame WCS information.
*/
#ifdef ANSI_FUNC
int
imd_getWCS (
IMDPtr imd, /* package pointer */
int wcs, /* WCS number to get */
char *name, /* name string */
char *title, /* title string */
float *a,
float *b,
float *c,
float *d, /* WCS values */
float *tx,
float *ty, /* translation */
float *z1,
float *z2, /* zscale values */
int *zt /* transformation type */
)
#else
int
imd_getWCS (imd, wcs, name, title, a, b, c, d, tx, ty, z1, z2, zt)
IMDPtr imd; /* package pointer */
int wcs; /* WCS number to get */
char *name; /* name string */
char *title; /* title string */
float *a, *b, *c, *d; /* WCS values */
float *tx, *ty; /* translation */
float *z1, *z2; /* zscale values */
int *zt; /* transformation type */
#endif
{
int fbwidth = imd->fbtab[imd->fbconfig-1]->width;
int fbheight = imd->fbtab[imd->fbconfig-1]->height;
int datain = imd->datain;
int dataout = imd->dataout;
int nbytes = 0, version = imd->iis_version;
int tokens, status = 0;
char wcsbuf[SZ_WCSBUF];
/* Read the WCS from the server. */
if (com_readWCS (datain, dataout, wcsbuf, &nbytes, wcs, version))
return (ERR);
if (imd_debug) {
printf ("[imd_getWCS] wcs=%d wcsbuf='%s'\n", wcs, wcsbuf);
}
/* Attempt to read the WCS and set up a unitary transformation
* if the information cannot be read.
*/
if (sscanf (wcsbuf, "%[^\n]\n%f%f%f%f%f%f%f%f%d",
buf, a, b, c, d, tx, ty, z1, z2, zt) < 7) {
strcpy (title, "[NO WCS]\n");
*a = *d = 1;
*b = *c = 0;
*tx = *ty = 0;
*zt = 0;
} else {
if (name == NULL)
name = imd->name;
if (title == NULL)
title = imd->title;
sscanf (buf, "%s - %s", name, title);
}
/* Now try to read the mapping if we're expecting one, otherwise
* set up a default mapping of the entire frame buffer.
*/
if (imd->iis_version > 0) {
register int i, j;
/* Skip over the first two lines of WCS data. */
strcpy (buf, wcsbuf);
for (i=0, j=0; j < 2 && buf[i]; i++)
if (buf[i] == '\n') j++;
/* Attempt to read the mapping. */
imd->ref[0] = '\0';
imd->region[0] = '\0';
if (sscanf (&buf[i], "%s%f%f%d%d%d%d%d%d\n%s\n",
imd->region, &imd->sx, &imd->sy, &imd->snx, &imd->sny,
&imd->dx, &imd->dy, &imd->dnx, &imd->dny, imd->ref) < 10) {
/* Error in the read, set a default. */
strcpy (imd->region, "none");
strcpy (imd->ref, "none");
imd->sx = 1.0;
imd->sy = 1.0;
imd->snx = fbwidth;
imd->sny = fbheight;
imd->dx = 1;
imd->dy = 1;
imd->dnx = fbwidth;
imd->dny = fbheight;
imd->iis_valid = 0;
return (ERR);
} else
imd->iis_valid = 1;
}
return (OK);
}
/* IMD_SETMAPPING -- Set the mapping information to be sent with the next
* imd_setWcs() call.
*/
#ifdef ANSI_FUNC
int
imd_setMapping (
IMDPtr imd, /* package ptr */
char *region, /* region name */
float sx, /* source rect */
float sy,
int snx, /* source extent */
int sny,
int dx, /* dest rect */
int dy,
int dnx, /* dest extent */
int dny,
char *ref /* reference name */
)
#else
int
imd_setMapping (imd, region, sx,sy,snx,sny, dx,dy,dnx,dny, ref)
IMDPtr imd; /* package ptr */
char *region; /* region name */
float sx, sy; /* source rect */
int snx, sny; /* source extent */
int dx, dy; /* dest rect */
int dnx, dny; /* dest extent */
char *ref; /* reference name */
#endif
{
if (imd_debug) {
printf ("[imd_setMapping] region='%s' ref='%s'\n",
( region ? region : ""), (ref ? ref : ""));
printf ("\tsrc = %g,%g%d,%d dest = %d,%d,%d,%d\n",
sx, sy, snx, sny, dx, dy, dnx, dny);
}
strcpy (imd->region, (region ? region : ""));
strcpy (imd->ref, (ref ? ref : ""));
imd->sx = sx;
imd->sy = sy;
imd->snx = snx;
imd->sny = sny;
imd->dx = dx;
imd->dy = dy;
imd->dnx = dnx;
imd->dny = dny;
return ((imd->iis_valid = 1));
}
/* IMD_GETMAPPING -- Get the mapping information returned with the last
* imd_getWcs() call.
*/
#ifdef ANSI_FUNC
int
imd_getMapping (
IMDPtr imd, /* package ptr */
char *region, /* region name */
float *sx, /* source rect */
float *sy,
int *snx, /* source extent */
int *sny,
int *dx, /* dest rect */
int *dy,
int *dnx, /* dest extent */
int *dny,
char *ref /* reference name */
)
#else
int
imd_getMapping (imd, region, sx,sy,snx,sny, dx,dy,dnx,dny, ref)
IMDPtr imd; /* package ptr */
char *region; /* region name */
float *sx, *sy; /* source rect */
int *snx, *sny; /* source extent */
int *dx, *dy; /* dest rect */
int *dnx, *dny; /* dest extent */
char *ref; /* reference name */
#endif
{
if (imd->iis_valid) {
strcpy (region, imd->region);
*sx = imd->sx;
*sy = imd->sy;
*snx = imd->snx;
*sny = imd->sny;
*dx = imd->dx;
*dy = imd->dy;
*dnx = imd->dnx;
*dny = imd->dny;
strcpy (ref, imd->ref);
}
if (imd_debug) {
printf ("[imd_getMapping] valid=%d region='%s' ref='%s'\n",
imd->iis_valid, (region ? region : ""), (ref ? ref : ""));
printf ("\tsrc = %g,%g,%d,%d dest = %d,%d,%d,%d\n",
*sx, *sy, *snx, *sny, *dx, *dy, *dnx, *dny);
}
return (imd->iis_valid);
}
/* IMD_CLOSE -- Close the connection to the display server.
*/
#ifdef ANSI_FUNC
int
imd_close (
IMDPtr imd /* package pointer */
)
#else
int
imd_close (imd)
IMDPtr imd; /* package pointer */
#endif
{
if (imd_debug)
printf ("[imd_close]\n");
if (imd) {
register int i;
/* Close the connection. */
if (imd->datain)
close (imd->datain);
if (imd->dataout)
close (imd->dataout);
/* Free the frame buffer configuration table. */
for (i=0; i < MAX_FBCONFIG; i++)
free ((char *)imd->fbtab[i]);
/* Free the pointers in the imd structure. */
free (imd->title);
free (imd->name);
free (imd->region);
free (imd->ref);
free (imd);
}
return (OK);
}
/* --------------------
* Low-Level Procedures
* --------------------*/
/* IMD_WRITEIMAGE -- Display a raw pixel array to the server given the array
* dimensions, and a location. We use this instead of imd_writeSubRaster()
* since we can assume the frame is blank and we wish to avoid the overhead
* having to read back the frame buffer. The image corner position is given
* in user coords where the [0,0] origin is in the LL of the display window,
* we convert this to frame buffer coords where the origin is in the UL.
*/
int
imd_writeImage (imd, pix, nx, ny, llx, lly)
IMDPtr imd; /* package pointer */
uchar *pix; /* pixels to display */
int nx, ny; /* dimensions */
int llx, lly; /* LL corner of image */
{
register int i, j, k, y, nbytes, nl = ny, imline;
register int nnx = nx, nny = ny, block_has_data = 0;
register uchar *ip = pix;
register uchar *bp, *block, *ep;
register int lx, ly, nblocks, lines_per_block, fbline;
int x_off = 0, y_off = 0;
int fbwidth = imd->fbtab[imd->fbconfig-1]->width;
int fbheight = imd->fbtab[imd->fbconfig-1]->height;
/* Check to see if the image to be displayed is larger than the
* current frame buffer.
*/
if (nx > fbwidth || ny > fbheight) {
nnx = min(nx, fbwidth); /* get new dimensions */
nny = min(ny, fbheight);
x_off = (nx>fbwidth ? ((nx - fbwidth) / 2 - 1) : 0);
y_off = (ny>fbheight ? ((ny - fbheight) / 2 - 1) : 0);
}
/* Now see whether the image extends over the boundaries of the
* frame buffer in any direction. If so we'll set up to write
* only those pixels on the frame buffer.
*/
if (llx < 0) { /* image is left of frame */
x_off = -llx;
nnx = nx + llx;
}
if (lly < 0) { /* image is below frame */
y_off = -lly;
nny = ny + lly;
}
if ((llx + nx) > fbwidth) /* image overflows frame to right */
nnx = fbwidth - llx;
if ((lly + ny) > fbheight) /* image overflows frame at top */
nny = fbheight - lly;
ip = pix + (y_off * nx) + x_off;
/* Convert corner position to frame buffer coords. */
lx = max (0, llx);
ly = min (fbheight - 1, fbheight - lly - 1);
/* Compute the corner points in user units. */
imd->xs = llx;
imd->xe = llx + nnx - 1;
imd->ys = lly;
imd->ye = lly + nny - 1;
if (imd_debug) {
printf("[imd_writeImage] %dx%d bytes at [%d,%d] of [%d,%d]\n",
nx, ny, lx, ly, fbwidth, fbheight);
printf("[imd_writeImage] Xends = [%d,%d] Yends = [%d,%d]\n",
imd->xs, imd->xe, imd->ys, imd->ye);
printf("[imd_writeImage] nnx=%d nny=%d llx=%d lly=%d xo=%d yo=%d\n",
nnx, nny, llx, lly, x_off, y_off);
imd_debug = 1;
}
/* Display image. */
lines_per_block = (int) (SZ_BLOCK / fbwidth);
nblocks = (int) (fbheight / lines_per_block);
nbytes = fbwidth * lines_per_block;
block = (uchar *) calloc (nbytes, sizeof (uchar));
if (imd_debug) {
printf ("height=%d nblocks=%d lines/block=%d\n",
fbheight, nblocks, lines_per_block);
}
/* For each of the blocks we're sending.... */
fbline = fbheight - 1;
imline = ly;
ep = ip + nx * ny;
for (i=0, y = fbheight - lines_per_block; i < nblocks; i++) {
bp = block + (lines_per_block - 1) * fbwidth;
block_has_data = 0;
if (nnx != fbwidth || nny != fbheight)
/* Clear the block array */
for (k=0; k < nbytes; k++)
block[k] = 0;
/* Map image pixels to the data block. */
for (j=0; j < lines_per_block && nl >= 0; j++) {
if (fbline == imline && ip < ep) {
block_has_data = 1;
bcopy (ip, bp+lx, nnx);
ip += nx;
imline--;
nl--;
}
bp -= fbwidth;
fbline--;
}
if (block_has_data)
if (imd_writeLine(imd, block, nbytes, 0, y))
return (ERR);