aboutsummaryrefslogtreecommitdiff
path: root/pkg/proto/t_binfil.x
blob: d2d025e72badfbae6299760b78578252232b1bec (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
include <imhdr.h>
include <error.h>
include	<mach.h>

# Binary file image transfer utilities --
# 	1. Convert from IRAF image to binary format
# 	1. Convert from binary formats to IRAF image

define	LEN_USER_AREA	720

# BINFIL -- Convert IRAF image file of shorts to a binary string
#           A short header of 90 bytes is prepended and has the
#           following elements;
#
#	       bytes   content
#		1-2    nrows
#		3-4    ncols
#		5-6    IRAF pixel type
#		7-26   space set to 0
#		27-90  header (ASCII)

procedure t_binfil()

char	ifile[SZ_FNAME], header[64], out_image[SZ_FNAME]
int	infile, nfiles, fd, i, file_nr, ncols, nrows, ptype
short	space[10], sncols, snrows, sptype
long	v1[IM_MAXDIM]
real	scale_fact, temp
bool	add_header
pointer	im, pix, sp, inpix

int	clpopni(), clplen(), clgfil(), open(), imgnlr(), strlen()
real	clgetr()
bool	clgetb()
pointer	immap()

begin
	# Get file names
	infile = clpopni ("input")
	nfiles = clplen (infile)

	# Get optional scaling factor
	scale_fact = clgetr ("scale_fact")
	if (scale_fact == 0.0)
	    scale_fact = 1.0

	# Should a header string be added?
	add_header = clgetb ("header")

	# Zero header spaces
	do i = 1, 10
	    space[i] = 0

	# Loop over all images
	while (clgfil (infile, ifile, SZ_FNAME) != EOF) {
	    iferr (im = immap (ifile, READ_ONLY, LEN_USER_AREA)) {
		call eprintf ("[%s] not found\n")
		call pargstr (ifile)
		go to 10
	    }

	    ncols = IM_LEN (im, 1)
	    nrows = IM_LEN (im, 2)
	    ptype = IM_PIXTYPE (im)

	    # Pack header characters
	    call strpak (IM_TITLE(im), header, strlen (IM_TITLE(im)))

	    # Create output file name and open it - append ".b"
	    call sprintf (out_image, SZ_FNAME, "%s.b")
		call pargstr (ifile)
	    call printf ("%s --> %s\n")
		call pargstr (ifile)
		call pargstr (out_image)
	    call flush (STDOUT)

	    file_nr = file_nr + 1

	    fd = open (out_image, NEW_FILE, BINARY_FILE)

	    # Write header parameters
	    if (add_header) {
		sncols = ncols
		snrows = nrows
		sptype = ptype
		call write (fd, sncols, SZ_SHORT/SZ_CHAR)
		call write (fd, snrows, SZ_SHORT/SZ_CHAR)
		call write (fd, sptype, SZ_SHORT/SZ_CHAR)
		call write (fd, space, 10 * SZ_SHORT/SZ_CHAR)
		call write (fd, header, 64 / SZB_CHAR)
	    }

	    call smark (sp)
	    call salloc (pix, ncols, TY_SHORT)

	    # Access pixels and write them out for each row
	    call amovkl (long(1), v1, IM_MAXDIM)
	    while (imgnlr (im, inpix, v1) != EOF) {
		do i = 1, ncols {
		    temp = Memr[inpix+i-1] * scale_fact

		    if (temp > MAX_SHORT)
			temp = MAX_SHORT
		    else if (temp < -(MAX_SHORT))
			temp = -MAX_SHORT

		    Mems[pix+i-1] = temp
		}

		call write (fd, Mems[pix], ncols * SZ_SHORT/SZ_CHAR)

	    }
	    call close (fd)
	    call sfree (sp)

	    call imunmap (im)
10	    ;
	}
end

# IRAFIL -- Convert 16 or 8-bit binary string to IRAF image file

procedure t_irafil()

char	ifile[SZ_FNAME], out_image[SZ_FNAME]
int	infile, nfiles, fd, i, j, file_nr, ncols, nrows, ptype, krow
int	nr_bits, nr_chars, nr_skip, nc_skip, ival
long	offset
bool	flip, sign16
pointer	im, pix, sp, temp, opix, sp1, hdr, src

int	clpopni(), clplen(), clgfil(), clgeti()
int	open(), read()
bool	clgetb()
pointer	immap(), impl2s(), impl2l()

begin
	# Get file names
	infile = clpopni ("input")
	nfiles = clplen (infile)

	# Get image dimensions
	nrows = clgeti ("nrows")
	ncols = clgeti ("ncols")

	# Is input string of data 8 or 16 bits?
	nr_bits = clgeti ("bits")
	if (nr_bits != 8 && nr_bits != 16)
	    call error (0, "Must be 8 or 16 bits")

	# Is bit 16 to be used as a sign bit?
	if (nr_bits == 16) {
	    sign16 = clgetb ("signed")
	    offset = 65536
	} else {
	    sign16 = true
	    offset = 256
	}

	# Should image be top-bottom flipped?
	# For some input images (e.g. Compaq 286 display) this is
	# needed to make SNAPS look correct.
	flip = clgetb ("tb_flip")

	# Header info can be skipped if number of bytes is given
	nr_skip = clgeti ("skip")

	# Loop over all images
	while (clgfil (infile, ifile, SZ_FNAME) != EOF) {
	    iferr (fd = open (ifile, READ_ONLY, BINARY_FILE)) {
		call eprintf ("cannot open %s\n")
		call pargstr (ifile)
		go to 10
	    }

	    if (sign16)
		ptype = TY_SHORT
	    else
		ptype = TY_LONG

	    # Create output file name and open it - append ".i"
	    call sprintf (out_image, SZ_FNAME, "%s.i")
		call pargstr (ifile)
	    file_nr = file_nr + 1
	    call printf ("%s --> %s\n")
		call pargstr (ifile)
		call pargstr (out_image)
	    call flush (STDOUT)

	    im = immap (out_image, NEW_IMAGE, 0)
	    IM_NDIM (im) = 2
	    IM_LEN (im, 1) = ncols
	    IM_LEN (im, 2) = nrows
	    IM_PIXTYPE (im) = ptype

	    call smark (sp)
	    call salloc (pix, ncols, TY_SHORT)
	    call salloc (temp, ncols, TY_SHORT)

	    # Skip over header pixels if any
	    nc_skip = nr_skip / 2
	    if (nr_skip > 0) {
		call smark (sp1)
		call salloc (hdr, nc_skip, TY_SHORT)
		if (read (fd, Mems[hdr], nc_skip) != EOF)
		    ;
		call sfree (sp1)
	    }

	    # Access pixels and write them out for each row
	    nr_chars = ncols * nr_bits / 8 / 2
	    do i = 1, nrows {
	        iferr (nc_skip = read (fd, Mems[pix], nr_chars))
		    call amovks (0, Mems[pix], nr_chars)
		else {
		    if (nr_bits == 8) {
			call chrupk (Mems[pix], 1, Mems[temp], 1, ncols)
			src = temp
		    } else
			src = pix
		}

		# Provide top-bottom flip for special image formats
		if (flip)
		    krow = nrows-i+1
		else
		    krow = i

		# Select proper pointer type
		if (sign16)
		    opix = impl2s (im, krow)
		else
		    opix = impl2l (im, krow)

		# Transfer all pixels, correcting for signed/unsigned data
		do j = 1, ncols {
		    ival = Mems[src+j-1]
		    if (sign16) {
			if (nr_bits == 8 && ival < 0)
			    Mems[opix+j-1] = ival + offset
		        else
			    Mems[opix+j-1] = ival
		    } else {
		        if (ival < 0)
			    Meml[opix+j-1] = ival + offset
		        else
			    Meml[opix+j-1] = ival
		    }
		}
	    }

	    call sfree (sp)
	    call close (fd)
	    call imunmap (im)
10	    ;
	}
end