aboutsummaryrefslogtreecommitdiff
path: root/sys/imio/iki/oif/oifwrhdr.x
blob: 7b4e7349340fdfa7a56015c5d4c70a38eba5696c (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <syserr.h>
include <imhdr.h>
include <mach.h>
include <imio.h>
include "imhv1.h"
include "imhv2.h"
include "oif.h"

# OIF_WRHDR -- Write an OIF image header.

procedure oif_wrhdr (fd, im, htype)

int	fd			#I header file descriptor
pointer	im			#I image descriptor
int	htype			#I TY_IMHDR or TY_PIXHDR

pointer	sp, v1, fname
int	status, hdrlen, len_userarea
errchk	write, miiwritec, miiwritei, miiwritel, miiwriter
int	strlen()

define	v1done_  91
define	v2start_ 92
define	v2done_  93

begin
	switch (IM_HDRVER(im)) {
	case V1_VERSION:
	    # Old V1 image header.
	    # ----------------------

	    status = ERR
	    call smark (sp)
	    call salloc (v1, LEN_V1IMHDR, TY_STRUCT)

	    # Initialize the output image header.
	    switch (htype) {
	    case TY_IMHDR:
		call strcpy (V1_MAGIC, IM_V1MAGIC(v1), SZ_IMMAGIC)
		hdrlen = LEN_V1IMHDR
	    case TY_PIXHDR:
		call strcpy (V1_PMAGIC, IM_V1MAGIC(v1), SZ_IMMAGIC)
		hdrlen = LEN_V1PIXHDR
	    default:
		goto v1done_
	    }

	    # The following is the length of the user area in chars.
	    len_userarea = strlen (Memc[IM_USERAREA(im)]) + 1
	    IM_V1HDRLEN(v1) = LEN_V1IMHDR +
		(len_userarea + SZ_MII_INT-1) / SZ_MII_INT

	    IM_V1PIXTYPE(v1) = IM_PIXTYPE(im)
	    IM_V1NDIM(v1) = IM_NDIM(im)
	    call amovl (IM_LEN(im,1), IM_V1LEN(v1,1), IM_MAXDIM)
	    call amovl (IM_PHYSLEN(im,1), IM_V1PHYSLEN(v1,1), IM_MAXDIM)

	    IM_V1SSMTYPE(v1) = IM_SSMTYPE(im)
	    IM_V1LUTOFF(v1) = IM_LUTOFF(im)
	    IM_V1PIXOFF(v1) = IM_PIXOFF(im)
	    IM_V1HGMOFF(v1) = IM_HGMOFF(im)
	    IM_V1CTIME(v1) = IM_CTIME(im)
	    IM_V1MTIME(v1) = IM_MTIME(im)
	    IM_V1LIMTIME(v1) = IM_LIMTIME(im)
	    IM_V1MAX(v1) = IM_MAX(im)
	    IM_V1MIN(v1) = IM_MIN(im)

	    if (strlen(IM_PIXFILE(im)) > SZ_V1IMPIXFILE)
		goto v1done_
	    if (strlen(IM_HDRFILE(im)) > SZ_V1IMHDRFILE)
		goto v1done_

	    call strcpy (IM_PIXFILE(im), IM_V1PIXFILE(v1), SZ_V1IMPIXFILE)
	    call strcpy (IM_HDRFILE(im), IM_V1HDRFILE(v1), SZ_V1IMHDRFILE)
	    call strcpy (IM_TITLE(im), IM_V1TITLE(v1), SZ_V1IMTITLE)
	    call strcpy (IM_HISTORY(im), IM_V1HISTORY(v1), SZ_V1IMHIST)

	    # For historical reasons the pixel file header stores the host
	    # pathname of the header file in the PIXFILE field of the pixel
	    # file header.

	    if (htype == TY_PIXHDR)
		call fpathname (IM_HDRFILE(im), IM_V1PIXFILE(v1),
		    SZ_V1IMPIXFILE)

	    # Write the file header.
	    call seek (fd, BOFL)
	    call write (fd, IM_V1MAGIC(v1), hdrlen * SZ_MII_INT)

	    # Write the user area.
	    if (htype == TY_IMHDR)
		call write (fd, Memc[IM_USERAREA(im)], len_userarea)

	    status = OK
v1done_
	    call sfree (sp)
	    if (status != OK)
		call syserrs (SYS_IKIUPDHDR, IM_NAME(im))

	case V2_VERSION:
	    # Newer V2 image header.
	    # ----------------------
v2start_
	    status = ERR
	    call smark (sp)
	    call salloc (fname, SZ_PATHNAME, TY_CHAR)

	    call seek (fd, BOFL)

	    # Initialize the output image header.
	    switch (htype) {
	    case TY_IMHDR:
		call miiwritec (fd, V2_MAGIC, SZ_IMMAGIC)
		hdrlen = LEN_V2IMHDR
	    case TY_PIXHDR:
		call miiwritec (fd, V2_PMAGIC, SZ_IMMAGIC)
		hdrlen = LEN_V2PIXHDR
	    default:
		goto v2done_
	    }

	    # The following is the length of the user area in SU.
	    len_userarea = strlen (Memc[IM_USERAREA(im)]) + 1
	    hdrlen = LEN_V2IMHDR + (len_userarea + SZ_MII_INT-1) / SZ_MII_INT

	    call miiwritei (fd, hdrlen, 1)
	    call miiwritei (fd, IM_PIXTYPE(im), 1)

	    # Record the byte swapping used for this image.  When writing a
	    # new image we use the native data type of the host and don't
	    # swap bytes, so IM_SWAPPED is YES if the host architecture is
	    # byte swapped.

	    switch (IM_ACMODE(im)) {
	    case NEW_IMAGE, NEW_COPY, TEMP_FILE:
		IM_SWAPPED(im) = -1
		switch (IM_PIXTYPE(im)) {
		case TY_SHORT, TY_USHORT:
		    IM_SWAPPED(im) = BYTE_SWAP2
		case TY_INT, TY_LONG:
		    IM_SWAPPED(im) = BYTE_SWAP4
		case TY_REAL:
		    if (IEEE_USED == YES)
			IM_SWAPPED(im) = IEEE_SWAP4
		case TY_DOUBLE:
		    if (IEEE_USED == YES)
			IM_SWAPPED(im) = IEEE_SWAP8
		}
	    default:
		# IM_SWAPPED should already be set in header.
	    }

	    call miiwritei (fd, IM_SWAPPED(im), 1)
	    call miiwritei (fd, IM_NDIM(im), 1)
	    call miiwritel (fd, IM_LEN(im,1), IM_MAXDIM)
	    call miiwritel (fd, IM_PHYSLEN(im,1), IM_MAXDIM)
	    call miiwritel (fd, IM_SSMTYPE(im), 1)
	    call miiwritel (fd, IM_LUTOFF(im), 1)
	    call miiwritel (fd, IM_PIXOFF(im), 1)
	    call miiwritel (fd, IM_HGMOFF(im), 1)
	    call miiwritel (fd, IM_BLIST(im), 1)
	    call miiwritel (fd, IM_SZBLIST(im), 1)
	    call miiwritel (fd, IM_NBPIX(im), 1)
	    call miiwritel (fd, IM_CTIME(im), 1)
	    call miiwritel (fd, IM_MTIME(im), 1)
	    call miiwritel (fd, IM_LIMTIME(im), 1)
	    call miiwriter (fd, IM_MAX(im), 1)
	    call miiwriter (fd, IM_MIN(im), 1)

	    if (strlen(IM_PIXFILE(im)) > SZ_V2IMPIXFILE)
		goto v2done_
	    if (strlen(IM_HDRFILE(im)) > SZ_V2IMHDRFILE)
		goto v2done_

	    # For historical reasons the pixel file header stores the host
	    # pathname of the header file in the PIXFILE field of the pixel
	    # file header.

	    if (htype == TY_PIXHDR) {
		call aclrc (Memc[fname], SZ_PATHNAME)
		call fpathname (IM_HDRFILE(im), Memc[fname], SZ_PATHNAME)
		call miiwritec (fd, Memc[fname], SZ_V2IMPIXFILE)
		status = OK
		goto v2done_
	    } else
		call miiwritec (fd, IM_PIXFILE(im), SZ_V2IMPIXFILE)

	    call oif_trim (IM_HDRFILE(im), SZ_V2IMHDRFILE)
	    call miiwritec (fd, IM_HDRFILE(im), SZ_V2IMHDRFILE)

	    call oif_trim (IM_TITLE(im), SZ_V2IMTITLE)
	    call miiwritec (fd, IM_TITLE(im), SZ_V2IMTITLE)

	    call oif_trim (IM_HISTORY(im), SZ_V2IMHIST)
	    call miiwritec (fd, IM_HISTORY(im), SZ_V2IMHIST)

	    # Write the variable-length user area.
	    call miiwritec (fd, Memc[IM_USERAREA(im)], len_userarea)

	    status = OK
v2done_	
	    call sfree (sp)
	    if (status != OK)
		call syserrs (SYS_IKIUPDHDR, IM_NAME(im))

	default:
	    IM_HDRVER(im) = V2_VERSION
	    goto v2start_
	}
end


# OIF_TRIM -- Trim trailing garbage at the end of a string.  This does not
# affect the value of the string, but makes the contents of the output file
# clearer when examined with file utilities.

procedure oif_trim (s, nchars)

char	s[ARB]
int	nchars

int	n, ntrim
int	strlen()

begin
	n = strlen(s) + 1
	ntrim = nchars - n

	if (ntrim > 0)
	    call aclrc (s[n], ntrim)
end