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
|
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
include <imhdr.h>
include <imio.h>
include <ctype.h>
.help impmhdr
.nf --------------------------------------------------------------------------
IMPMHDR -- Routines to encode/decode an image header in a title string
such as is provided by pl_[save|load]f, so that general image headers can
be saved in .pl files.
nchars = im_pmsvhdr (im, bufp, sz_buf)
im_pmldhdr (im, bufp)
The information saved in the plio save file title string consist of a
series of keyword = value assignments, one per line.
.endhelp ---------------------------------------------------------------------
define DEF_SZBUF 32768
define INC_SZBUF 16384
define INC_HDRMEM 8100
define IDB_RECLEN 80
define KW_TITLE "$TITLE = "
define LEN_KWTITLE 9
define KW_CTIME "$CTIME = "
define LEN_KWCTIME 9
define KW_MTIME "$MTIME = "
define LEN_KWMTIME 9
define KW_LIMTIME "$LIMTIME = "
define LEN_KWLIMTIME 11
define KW_MINPIXVAL "$MINPIXVAL = "
define LEN_KWMINPIXVAL 13
define KW_MAXPIXVAL "$MAXPIXVAL = "
define LEN_KWMAXPIXVAL 13
# IM_PMSVHDR -- Save an image header in a text string as a sequence of
# keyword = value assignments, one per line. A pointer to a text buffer
# containing the encoded header is returned as the output parameter, and
# the string length in chars is returned as the function value.
# The caller should deallocate this buffer when it is no longer needed.
int procedure im_pmsvhdr (im, bp, sz_buf)
pointer im #I image descriptor
pointer bp #U buffer containing encoded header
int sz_buf #U allocated size of buffer, chars
int nchars, ualen, ch, i
pointer sp, tbuf, ip, op, idb, rp
errchk malloc, realloc, idb_open
int gstrcpy(), idb_nextcard
pointer idb_open()
begin
call smark (sp)
call salloc (tbuf, SZ_IMTITLE, TY_CHAR)
# Allocate text buffer if the user hasn't already done so.
if (bp == NULL || sz_buf <= 0) {
sz_buf = DEF_SZBUF
call malloc (bp, sz_buf, TY_CHAR)
}
# Store title string in buffer.
call strcpy (IM_TITLE(im), Memc[tbuf], SZ_IMTITLE)
op = bp + gstrcpy (KW_TITLE, Memc[bp], ARB)
Memc[op] = '"'; op = op + 1
for (ip=tbuf; Memc[ip] != EOS; ip=ip+1) {
if (Memc[ip] == '"') {
Memc[op] = '\\'; op = op + 1
}
Memc[op] = Memc[ip]; op = op + 1
}
Memc[op] = '"'; op = op + 1
Memc[op] = '\n'; op = op + 1
# Store the create time in buffer.
call sprintf (Memc[tbuf], SZ_IMTITLE, "%d")
call pargl (IM_CTIME(im))
op = op + gstrcpy (KW_CTIME, Memc[op], ARB)
op = op + gstrcpy (Memc[tbuf], Memc[op], ARB)
Memc[op] = '\n'; op = op + 1
# Store the modify time in buffer.
call sprintf (Memc[tbuf], SZ_IMTITLE, "%d")
call pargl (IM_MTIME(im))
op = op + gstrcpy (KW_MTIME, Memc[op], ARB)
op = op + gstrcpy (Memc[tbuf], Memc[op], ARB)
Memc[op] = '\n'; op = op + 1
# Store the limits time in buffer.
call sprintf (Memc[tbuf], SZ_IMTITLE, "%d")
call pargl (IM_LIMTIME(im))
op = op + gstrcpy (KW_LIMTIME, Memc[op], ARB)
op = op + gstrcpy (Memc[tbuf], Memc[op], ARB)
Memc[op] = '\n'; op = op + 1
# Store the minimum good pixel value in buffer.
call sprintf (Memc[tbuf], SZ_IMTITLE, "%g")
call pargr (IM_MIN(im))
op = op + gstrcpy (KW_MINPIXVAL, Memc[op], ARB)
op = op + gstrcpy (Memc[tbuf], Memc[op], ARB)
Memc[op] = '\n'; op = op + 1
# Store the maximum good pixel value in buffer.
call sprintf (Memc[tbuf], SZ_IMTITLE, "%g")
call pargr (IM_MAX(im))
op = op + gstrcpy (KW_MAXPIXVAL, Memc[op], ARB)
op = op + gstrcpy (Memc[tbuf], Memc[op], ARB)
Memc[op] = '\n'; op = op + 1
# Copy the header cards.
idb = idb_open (im, ualen)
while (idb_nextcard (idb, rp) != EOF) {
# Increase the size of the output buffer if it fills.
nchars = op - bp
if (sz_buf - nchars < IDB_RECLEN) {
sz_buf = sz_buf + INC_SZBUF
call realloc (bp, sz_buf, TY_CHAR)
op = bp + nchars
}
# Copy the card, stripping any trailing whitespace.
nchars = 0
do i = 1, IDB_RECLEN {
ch = Memc[rp+i-1]
Memc[op+i-1] = ch
if (!IS_WHITE(ch))
nchars = i
}
op = op + nchars
Memc[op] = '\n'; op = op + 1
}
# All done, terminate the string and return any extra space.
Memc[op] = EOS; op = op + 1
nchars = op - bp
call realloc (bp, nchars, TY_CHAR)
# Clean up.
call idb_close (idb)
call sfree (sp)
return (nchars)
end
# IM_PMLDHDR -- Load the image header from a save buffer, prepared in a
# previous call to im_pmsvhdr. The saved header will overwrite any
# existing cards in the output image header.
procedure im_pmldhdr (im, bp)
pointer im #I image descriptor
pointer bp #I pointer to text buffer (header save buf)
int hdrlen, sz_ua, nchars, ch, i
pointer sp, tbuf, ip, op, rp, ua
int strncmp(), ctol(), ctor()
errchk realloc
begin
call smark (sp)
call salloc (tbuf, SZ_IMTITLE, TY_CHAR)
# Get the image title string.
for (ip = bp; Memc[ip] != EOS;) {
if (Memc[ip] == '$') {
if (strncmp (Memc[ip], KW_TITLE, LEN_KWTITLE) == 0) {
# Advance to first character of quoted string.
ip = ip + LEN_KWTITLE
while (Memc[ip] != EOS && Memc[ip] != '"')
ip = ip + 1
if (Memc[ip] == '"')
ip = ip + 1
# Extract the string.
op = tbuf
while (Memc[ip] != EOS && Memc[ip] != '"') {
if (Memc[ip] == '\\' && Memc[ip+1] == '"')
ip = ip + 1
Memc[op] = Memc[ip]
op = min (tbuf + SZ_IMTITLE, op + 1)
ip = ip + 1
}
# Store in image descriptor.
Memc[op] = EOS
call strcpy (Memc[tbuf], IM_TITLE(im), SZ_IMTITLE)
# Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
} else if (strncmp (Memc[ip], KW_CTIME, LEN_KWCTIME) == 0) {
# Decode the create time.
ip = ip + LEN_KWCTIME
rp = 1
if (ctol (Memc[ip], rp, IM_CTIME(im)) <= 0)
IM_CTIME(im) = 0
ip = ip + rp - 1
# Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
} else if (strncmp (Memc[ip], KW_MTIME, LEN_KWMTIME) == 0) {
# Decode the modify time.
ip = ip + LEN_KWMTIME
rp = 1
if (ctol (Memc[ip], rp, IM_MTIME(im)) <= 0)
IM_MTIME(im) = 0
ip = ip + rp - 1
# Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
} else if (strncmp (Memc[ip], KW_LIMTIME, LEN_KWLIMTIME) == 0) {
# Decode the limits time.
ip = ip + LEN_KWLIMTIME
rp = 1
if (ctol (Memc[ip], rp, IM_LIMTIME(im)) <= 0)
IM_LIMTIME(im) = 0
ip = ip + rp - 1
# Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
} else if (strncmp(Memc[ip],KW_MINPIXVAL,LEN_KWMINPIXVAL)==0) {
# Decode the minimum pixel value.
ip = ip + LEN_KWMINPIXVAL
rp = 1
if (ctor (Memc[ip], rp, IM_MIN(im)) <= 0)
IM_MIN(im) = 0.0
ip = ip + rp - 1
# Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
} else if (strncmp(Memc[ip],KW_MAXPIXVAL,LEN_KWMAXPIXVAL)==0) {
# Decode the maximum pixel value.
ip = ip + LEN_KWMAXPIXVAL
rp = 1
if (ctor (Memc[ip], rp, IM_MAX(im)) <= 0)
IM_MAX(im) = 0.0
ip = ip + rp - 1
# Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
} else {
# No keyword matched. Advance to next line.
while (Memc[ip] != EOS && Memc[ip] != '\n')
ip = ip + 1
if (Memc[ip] == '\n')
ip = ip + 1
}
} else
break
}
# Get the header keywords.
hdrlen = LEN_IMDES + IM_LENHDRMEM(im)
sz_ua = (hdrlen - IMU) * SZ_STRUCT - 1
ua = IM_USERAREA(im)
op = ua
while (Memc[ip] != EOS) {
rp = op
# Reallocate descriptor if we need more space. Since we are
# called at image map time and the descriptor pointer has not
# yet been passed out, the image descriptor can be reallocated.
nchars = rp - ua
if (nchars + IDB_RECLEN + 2 > sz_ua) {
hdrlen = hdrlen + INC_HDRMEM
IM_LENHDRMEM(im) = IM_LENHDRMEM(im) + INC_HDRMEM
call realloc (im, hdrlen, TY_STRUCT)
sz_ua = (hdrlen - IMU) * SZ_STRUCT - 1
ua = IM_USERAREA(im)
op = ua + nchars
}
# Copy the saved card, leave IP positioned to past newline.
do i = 1, IDB_RECLEN + 1 {
ch = Memc[ip]
if (ch != EOS)
ip = ip + 1
if (ch == '\n')
break
Memc[op] = ch
op = op + 1
}
# Blank fill the card.
while (op - rp < IDB_RECLEN) {
Memc[op] = ' '
op = op + 1
}
# Add newline termination.
Memc[op] = '\n'; op = op + 1
}
Memc[op] = EOS
IM_UABLOCKED(im) = YES
call sfree (sp)
end
|