aboutsummaryrefslogtreecommitdiff
path: root/pkg/dataio/import/iplistpix.x
blob: 3f4a001deceb9815bccf9c07e08d4913c8f00980 (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
include	<imhdr.h>
include <error.h>
include <mwset.h>

# IP_LISTPIXELS -- Convert image pixels into a text stream, i.e., into a list.
# Each pixel is printed on a separate line, preceded by its coordinates.

procedure ip_listpix (im)

char	wcs[SZ_FNAME]
double	incoords[IM_MAXDIM], outcoords[IM_MAXDIM]
int	i, j, npix, ndim, wcsndim, laxis1, fmtstat
int	paxno[IM_MAXDIM], laxno[IM_MAXDIM]
long	v[IM_MAXDIM], vcoords[IM_MAXDIM]
pointer	im, line, mw, ct, fmtptrs[IM_MAXDIM]

int	imgnlr(), mw_stati()
pointer	mw_openim(), mw_sctran()

begin
	# Get info from the input image.
	ndim = IM_NDIM(im)
	npix = IM_LEN(im,1)

	# Get the wcs.
	call strcpy ("world", wcs, SZ_FNAME)
	ifnoerr (mw = mw_openim (im)) {
	    # Set up the transformation.
	    call mw_seti (mw, MW_USEAXMAP, NO)
	    ct = mw_sctran (mw, "logical", wcs, 0)
	    wcsndim = mw_stati (mw, MW_NPHYSDIM)

	    # Get the physical to logical axis map.
	    call mw_gaxmap (mw, paxno, laxno, wcsndim)

	    # Set the default wcs.
	    call mw_ssytem (mw, wcs)

	} else {
	    # Print the error message from the above loop.
	    call erract (EA_WARN)

	    # Set the transform to the identity transform.
	    mw = NULL
	    ct = NULL
	    wcsndim = ndim

	    # Set the default physical to logical axis map.
	    do i = 1, wcsndim
	        paxno[i] = i
	}

	# Initialize the v vectors.
	call amovkl (long (1), v, IM_MAXDIM)
	call amovkl (long (1), vcoords, IM_MAXDIM)

	# Initialize the coordinates.
	laxis1 = 0
	do i = 1, wcsndim {
	    if (paxno[i] == 0) {
	        incoords[i] = 1
	    } else if (paxno[i] == 1) {
	        laxis1 = i
	        incoords[i] = v[1]
	    } else {
	        incoords[i] = v[paxno[i]]
	    }
	}

	# Check and correct for the no axis mapping case.
	if (laxis1 == 0) {
	    laxis1 = 1
	    do i = 1, wcsndim
	        paxno[i] = i
	}

	# Get the logical to physical axis map for the format strings.
	do i = 1, ndim {
	    laxno[i] = 0
	    do j = 1, wcsndim {
	        if (paxno[j] != i)
	    	    next
	        laxno[i] = j
	            break
	    }
	}

	# Set the format strings for the logical axes.
	fmtstat = EOS
	do i = 1, ndim {
	    call malloc (fmtptrs[i], SZ_FNAME, TY_CHAR)
	    if (fmtstat != EOF)
	        call gargwrd (Memc[fmtptrs[i]], SZ_FNAME)
	    else
		Memc[fmtptrs[i]] = EOS
	    if (laxno[i] == 0)
	        call strcpy ("%0.15g ", Memc[fmtptrs[i]], SZ_FNAME)
	    else if (mw == NULL || ct == NULL)
	        call strcpy ("%0.15g ", Memc[fmtptrs[i]], SZ_FNAME)
	    else iferr (call mw_gwattrs (mw, laxno[i], "format",
	        Memc[fmtptrs[i]], SZ_FNAME))
	        call strcpy ("%0.15g ", Memc[fmtptrs[i]], SZ_FNAME)
	    else
	        call strcat (" ", Memc[fmtptrs[i]], SZ_FNAME)
	}

	# Print the pixels.
	while (imgnlr (im, line, v) != EOF) {
	    do i = 1, npix {
		incoords[laxis1] = i
	    	if (ct == NULL)
	    	    call amovd (incoords, outcoords, wcsndim)
	    	else
	    	    call mw_ctrand (ct, incoords, outcoords, wcsndim)
	    	do j = 1, ndim {	        # X, Y, Z, etc.
	    	    call printf (Memc[fmtptrs[j]])
	    	    if (laxno[j] == 0)
	    	        call pargd (double(vcoords[j]))
	    	    else
	    	        call pargd (outcoords[laxno[j]])
	    	}
	    	call printf (" %g\n")	        # pixel value
	    	    call pargr (Memr[line+i-1])
	    }
	    call amovl (v, vcoords, IM_MAXDIM)
	    do i = 1, wcsndim {
	    	if (paxno[i] == 0)
	    	    next
	    	incoords[i] = v[paxno[i]]
	    }
	}

	do i = 1, ndim
	    call mfree (fmtptrs[i], TY_CHAR)
	if (mw != NULL)
	    call mw_close (mw)
end