aboutsummaryrefslogtreecommitdiff
path: root/sys/imfort/imrdhdr.x
blob: 745978112872242b95a95a25c0ed3ce42d20ff19 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <mach.h>
include <imhdr.h>
include "imfort.h"
include "imhv1.h"
include "imhv2.h"
include "oif.h"


# IMRDHDR -- Read the image header.  Either the main image header or the
# pixel file header can be read.

int procedure imrdhdr (fp, im, uchars, htype)

pointer	fp			#I header file descriptor
pointer	im			#I image descriptor
int	uchars			#I maxchars of user area data to read
int	htype			#I TY_IMHDR or TY_PIXHDR

pointer	sp, v1
char	immagic[SZ_IMMAGIC]
int	sulen_userarea, hdrlen, nchars, status

bool	streq()
int	i_miirdc(), i_miirdi(), i_miirdl(), i_miirdr()
int	btoi(), bfrseq(), bfseek()

define	readerr_ 91

begin
	# Determine the file type.
	if (bfseek (fp, BOFL) == ERR)
	    return (ERR)
	if (bfrseq (fp, immagic, SZ_IMMAGIC) != SZ_IMMAGIC)
	    return (ERR)

	if (htype == TY_PIXHDR && streq (immagic, V1_PMAGIC)) {
	    # V1 Pixel file header.
	    return (OK)

	} else if (htype == TY_IMHDR && streq (immagic, V1_MAGIC)) {
	    # Old V1 image header.

	    call smark (sp)
	    call salloc (v1, LEN_V1IMHDR, TY_STRUCT)

	    if (bfseek (fp, BOFL) == ERR) {
		call sfree (sp)
		return (ERR)
	    }
	    nchars = LEN_V1IMHDR * SZ_MII_INT
	    if (bfrseq (fp, IM_V1MAGIC(v1), nchars) != nchars) {
		call sfree (sp)
		return (ERR)
	    }

	    # Initialize the output image header.
	    call strcpy (IMH_MAGICSTR, IM_MAGIC(im), SZ_IMMAGIC)
	    IM_HDRVER(im) = V1_VERSION

	    # The following is the length of the user area in SU.
	    sulen_userarea = IM_V1HDRLEN(v1) - LEN_V1IMHDR
	    IM_HDRLEN(im) = LEN_IMHDR + sulen_userarea

	    IM_SWAP(im) = NO
	    IM_SWAPPED(im) = -1
	    IM_PIXTYPE(im) = IM_V1PIXTYPE(v1)

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

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

	    call strcpy (IM_V1PIXFILE(v1), IM_PIXFILE(im), SZ_IMPIXFILE)
	    call strcpy (IM_V1HDRFILE(v1), IM_HDRFILE(im), SZ_IMHDRFILE)
	    call strcpy (IM_V1TITLE(v1), IM_TITLE(im), SZ_IMTITLE)
	    call strcpy (IM_V1HISTORY(v1), IM_HISTORY(im), SZ_IMHIST)

	    # Read and output the user area.
	    if (uchars > 0) {
		nchars = min (uchars, sulen_userarea * SZ_MII_INT)
		if (bfrseq (fp, Memc[IM_USERAREA(im)], nchars) <= 0)
		    return (ERR)
	    }

	    call sfree (sp)
	    return (OK)
	}

	# Check for a new format header.
	if (bfseek (fp, BOFL) == ERR)
	    return (ERR)
	if (i_miirdc (fp, immagic, SZ_IMMAGIC) < 0)
	    return (ERR)

	if (htype == TY_PIXHDR && streq (immagic, V2_PMAGIC)) {
	    # V2 Pixel file header.
	    return (OK)

	} else if (htype == TY_IMHDR && streq (immagic, V2_MAGIC)) {
	    # Newer V2 image header.
	    status = ERR

	    # Initialize the output image header.
	    call strcpy (IMH_MAGICSTR, IM_MAGIC(im), SZ_IMMAGIC)
	    IM_HDRVER(im) = V2_VERSION

	    # "sulen_userarea" is the length of the user area in SU.
	    if (i_miirdi (fp, hdrlen, 1) != 1)
		goto readerr_
	    sulen_userarea = hdrlen - LEN_V2IMHDR
	    IM_HDRLEN(im) = LEN_IMHDR + sulen_userarea

	    if (i_miirdi (fp, IM_PIXTYPE(im), 1) != 1)
		goto readerr_

	    # Determine whether to byte swap the pixels.
	    if (i_miirdi (fp, IM_SWAPPED(im), 1) != 1)
		goto readerr_

	    IM_SWAP(im) = NO
	    switch (IM_PIXTYPE(im)) {
	    case TY_SHORT, TY_USHORT:
		IM_SWAP(im) = btoi (IM_SWAPPED(im) != BYTE_SWAP2)
	    case TY_INT, TY_LONG:
		IM_SWAP(im) = btoi (IM_SWAPPED(im) != BYTE_SWAP4)
	    case TY_REAL:
		if (IEEE_USED == YES)
		    IM_SWAP(im) = btoi (IM_SWAPPED(im) != IEEE_SWAP4)
	    case TY_DOUBLE:
		if (IEEE_USED == YES)
		    IM_SWAP(im) = btoi (IM_SWAPPED(im) != IEEE_SWAP8)
	    }

	    # Read the fixed-format fields of the header.
	    if (i_miirdi (fp, IM_NDIM(im), 1) < 0)
		goto readerr_
	    if (i_miirdi (fp, IM_LEN(im,1), IM_MAXDIM) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_PHYSLEN(im,1), IM_MAXDIM) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_SSMTYPE(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_LUTOFF(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_PIXOFF(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_HGMOFF(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_BLIST(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_SZBLIST(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_NBPIX(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_CTIME(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_MTIME(im), 1) < 0)
		goto readerr_
	    if (i_miirdl (fp, IM_LIMTIME(im), 1) < 0)
		goto readerr_

	    if (i_miirdr (fp, IM_MAX(im), 1) < 0)
		goto readerr_
	    if (i_miirdr (fp, IM_MIN(im), 1) < 0)
		goto readerr_

	    if (i_miirdc (fp, IM_PIXFILE(im), SZ_V2IMPIXFILE) < 0)
		goto readerr_
	    if (i_miirdc (fp, IM_HDRFILE(im), SZ_V2IMHDRFILE) < 0)
		goto readerr_
	    if (i_miirdc (fp, IM_TITLE(im), SZ_V2IMTITLE) < 0)
		goto readerr_
	    if (i_miirdc (fp, IM_HISTORY(im), SZ_V2IMHIST) < 0)
		goto readerr_

	    # Read the variable-length user area.
	    if (uchars > 0) {
		nchars = min (uchars, sulen_userarea * SZ_MII_INT)
		if (i_miirdc (fp, Memc[IM_USERAREA(im)], nchars) < 0)
		    goto readerr_
	    }

	    status = OK
readerr_
	    return (status)
	}

	return (ERR)
end