aboutsummaryrefslogtreecommitdiff
path: root/sys/imio/iki/oif
diff options
context:
space:
mode:
authorJoe Hunkeler <jhunkeler@gmail.com>2015-08-11 16:51:37 -0400
committerJoe Hunkeler <jhunkeler@gmail.com>2015-08-11 16:51:37 -0400
commit40e5a5811c6ffce9b0974e93cdd927cbcf60c157 (patch)
tree4464880c571602d54f6ae114729bf62a89518057 /sys/imio/iki/oif
downloadiraf-osx-40e5a5811c6ffce9b0974e93cdd927cbcf60c157.tar.gz
Repatch (from linux) of OSX IRAF
Diffstat (limited to 'sys/imio/iki/oif')
-rw-r--r--sys/imio/iki/oif/README1
-rw-r--r--sys/imio/iki/oif/imhv1.h75
-rw-r--r--sys/imio/iki/oif/imhv2.h43
-rw-r--r--sys/imio/iki/oif/mkpkg21
-rw-r--r--sys/imio/iki/oif/oif.h15
-rw-r--r--sys/imio/iki/oif/oifaccess.x51
-rw-r--r--sys/imio/iki/oif/oifclose.x36
-rw-r--r--sys/imio/iki/oif/oifcopy.x32
-rw-r--r--sys/imio/iki/oif/oifdelete.x53
-rw-r--r--sys/imio/iki/oif/oifgpfn.x60
-rw-r--r--sys/imio/iki/oif/oifmkpfn.x118
-rw-r--r--sys/imio/iki/oif/oifopen.x137
-rw-r--r--sys/imio/iki/oif/oifopix.x103
-rw-r--r--sys/imio/iki/oif/oifrdhdr.x196
-rw-r--r--sys/imio/iki/oif/oifrename.x102
-rw-r--r--sys/imio/iki/oif/oifupdhdr.x34
-rw-r--r--sys/imio/iki/oif/oifwrhdr.x233
17 files changed, 1310 insertions, 0 deletions
diff --git a/sys/imio/iki/oif/README b/sys/imio/iki/oif/README
new file mode 100644
index 00000000..01e30678
--- /dev/null
+++ b/sys/imio/iki/oif/README
@@ -0,0 +1 @@
+IKI/OIF -- IKI kernel for the old (original) IRAF image format.
diff --git a/sys/imio/iki/oif/imhv1.h b/sys/imio/iki/oif/imhv1.h
new file mode 100644
index 00000000..a9a37874
--- /dev/null
+++ b/sys/imio/iki/oif/imhv1.h
@@ -0,0 +1,75 @@
+# IMHV1.H -- Version 1 of the OIF binary file header (April 1988).
+
+define V1_MAGIC "imhdr" # file identification tag
+define V1_PMAGIC "impix" # file identification tag
+define V1_VERSION 1 # header version number
+
+define SZ_V1IMPIXFILE 79 # name of pixel storage file
+define SZ_V1IMHDRFILE 79 # name of header storage file
+define SZ_V1IMTITLE 79 # image title string
+define SZ_V1IMHIST 511 # image history record
+define SZ_V1BUNIT 9 # brightness units string
+define SZ_V1CTYPE 9 # coord axes units string
+
+# The IMIO image header structure.
+
+# Parameters.
+define LEN_V1IMHDR 513 # length of std header
+define LEN_V1PIXHDR 183 # length of pixel file header
+define V1U LEN_V1IMHDR # offset to user fields
+define IM_V1USERAREA (P2C($1+V1U)) # user area (database)
+
+# Disk resident header.
+define IM_V1MAGIC Memi[$1] # contains the string "imhdr"
+define IM_V1HDRLEN Memi[$1+3] # length of image header
+define IM_V1PIXTYPE Memi[$1+4] # datatype of the pixels
+define IM_V1NDIM Memi[$1+5] # number of dimensions
+define IM_V1LEN Meml[$1+$2+6-1] # length of the dimensions
+define IM_V1PHYSLEN Meml[$1+$2+13-1] # physical length (as stored)
+define IM_V1SSMTYPE Meml[$1+20] # type of subscript mapping
+define IM_V1LUTOFF Meml[$1+21] # offset to subscript map luts
+define IM_V1PIXOFF Meml[$1+22] # offset of the pixels
+define IM_V1HGMOFF Meml[$1+23] # offset of hgm pixels
+define IM_V1BLIST Meml[$1+24] # offset of bad pixel list
+define IM_V1SZBLIST Meml[$1+25] # size of bad pixel list
+define IM_V1NBPIX Meml[$1+26] # number of bad pixels
+define IM_V1CTIME Meml[$1+27] # time of image creation
+define IM_V1MTIME Meml[$1+28] # time of last modify
+define IM_V1LIMTIME Meml[$1+29] # time min,max computed
+define IM_V1MAX Memr[P2R($1+30)] # max pixel value
+define IM_V1MIN Memr[P2R($1+31)] # min pixel value
+define IM_V1HGM ($1+33) # histogram descriptor
+define IM_V1CTRAN ($1+52) # coordinate transformations
+define IM_V1PIXFILE Memc[P2C($1+103)] # name of pixel storage file
+define IM_V1HDRFILE Memc[P2C($1+143)] # name of header storage file
+define IM_V1TITLE Memc[P2C($1+183)] # image name string
+define IM_V1HISTORY Memc[P2C($1+223)] # history comment string
+
+# The Histogram structure (field IM_HGM)
+define LEN_HGMSTRUCT 20
+define HGM_TIME Meml[$1] # time when hgm was computed
+define HGM_LEN Meml[$1+1] # number of bins in hgm
+define HGM_NPIX Meml[$1+2] # npix used to compute hgm
+define HGM_MIN Memr[P2R($1+3)] # min hgm value
+define HGM_MAX Memr[P2R($1+4)] # max hgm value
+define HGM_INTEGRAL Memr[P2R($1+5)] # integral of hgm
+define HGM_MEAN Memr[P2R($1+6)] # mean value
+define HGM_VARIANCE Memr[P2R($1+7)] # variance about mean
+define HGM_SKEWNESS Memr[P2R($1+8)] # skewness of hgm
+define HGM_MODE Memr[P2R($1+9)] # modal value of hgm
+define HGM_LCUT Memr[P2R($1+10)] # low cutoff value
+define HGM_HCUT Memr[P2R($1+11)] # high cutoff value
+# next available field: ($1+12)
+
+# The Coordinate Transformation Structure (IM_CTRAN)
+define LEN_CTSTRUCT 50
+define CT_VALID Memi[$1] # (y/n) is structure valid?
+define CT_BSCALE Memr[P2R($1+1)] # pixval scale factor
+define CT_BZERO Memr[P2R($1+2)] # pixval offset
+define CT_CRVAL Memr[P2R($1+$2+3-1)] # value at pixel
+define CT_CRPIX Memr[P2R($1+$2+10-1)] # index of pixel
+define CT_CDELT Memr[P2R($1+$2+17-1)] # increment along axis
+define CT_CROTA Memr[P2R($1+$2+24-1)] # rotation angle
+define CT_BUNIT Memc[P2C($1+31)] # pixval ("brightness") units
+define CT_CTYPE Memc[P2C($1+36)] # coord units string
+# next available field: ($1+41)
diff --git a/sys/imio/iki/oif/imhv2.h b/sys/imio/iki/oif/imhv2.h
new file mode 100644
index 00000000..d7eaa1f7
--- /dev/null
+++ b/sys/imio/iki/oif/imhv2.h
@@ -0,0 +1,43 @@
+# IMHV2.H -- Version 2 of the OIF binary file header (March 1997).
+
+define V2_MAGIC "imhv2" # file identification tag
+define V2_PMAGIC "impv2" # file identification tag
+define V2_VERSION 2 # header version
+
+define SZ_V2IMPIXFILE 255 # name of pixel storage file
+define SZ_V2IMHDRFILE 255 # name of header storage file
+define SZ_V2IMTITLE 383 # image title string
+define SZ_V2IMHIST 1023 # image history record
+
+# The IMIO image header structure.
+
+# Parameters.
+define LEN_V2IMHDR 1024 # length of std header
+define LEN_V2PIXHDR 293 # length of pixel file header
+define V2U LEN_V2IMHDR # offset to user fields
+define IM_V2USERAREA (P2C($1+V2U)) # user area (database)
+
+# Disk resident header.
+define IM_V2MAGIC Memi[$1] # contains the string "imhdr"
+define IM_V2HDRLEN Memi[$1+3] # length of image header
+define IM_V2PIXTYPE Memi[$1+4] # datatype of the pixels
+define IM_V2SWAPPED Memi[$1+5] # pixels are byte swapped
+define IM_V2NDIM Memi[$1+6] # number of dimensions
+define IM_V2LEN Meml[$1+$2+7-1] # length of the dimensions
+define IM_V2PHYSLEN Meml[$1+$2+14-1] # physical length (as stored)
+define IM_V2SSMTYPE Meml[$1+21] # type of subscript mapping
+define IM_V2LUTOFF Meml[$1+22] # offset to subscript map luts
+define IM_V2PIXOFF Meml[$1+23] # offset of the pixels
+define IM_V2HGMOFF Meml[$1+24] # offset of hgm pixels
+define IM_V2BLIST Meml[$1+25] # offset of bad pixel list
+define IM_V2SZBLIST Meml[$1+26] # size of bad pixel list
+define IM_V2NBPIX Meml[$1+27] # number of bad pixels
+define IM_V2CTIME Meml[$1+28] # time of image creation
+define IM_V2MTIME Meml[$1+29] # time of last modify
+define IM_V2LIMTIME Meml[$1+30] # time min,max computed
+define IM_V2MAX Memr[P2R($1+31)] # max pixel value
+define IM_V2MIN Memr[P2R($1+32)] # min pixel value
+define IM_V2PIXFILE Memc[P2C($1+37)] # name of pixel storage file
+define IM_V2HDRFILE Memc[P2C($1+165)] # name of header storage file
+define IM_V2TITLE Memc[P2C($1+293)] # image name string
+define IM_V2HISTORY Memc[P2C($1+485)] # history comment string
diff --git a/sys/imio/iki/oif/mkpkg b/sys/imio/iki/oif/mkpkg
new file mode 100644
index 00000000..81a4d57e
--- /dev/null
+++ b/sys/imio/iki/oif/mkpkg
@@ -0,0 +1,21 @@
+# Make the IKI/OIF interface (Old IRAF Format images)
+
+$checkout libex.a lib$
+$update libex.a
+$checkin libex.a lib$
+$exit
+
+libex.a:
+ oifaccess.x oif.h
+ oifclose.x <error.h> <imhdr.h> <imio.h> <protect.h>
+ oifcopy.x oif.h
+ oifdelete.x <error.h> <imhdr.h> <protect.h>
+ oifgpfn.x oif.h <knet.h>
+ oifmkpfn.x oif.h <imhdr.h> <imio.h> <knet.h>
+ oifopen.x oif.h <imhdr.h> <imio.h> <fio.h> <error.h>
+ oifopix.x oif.h <config.h> <imhdr.h> <imio.h>
+ oifrdhdr.x imhv1.h imhv2.h oif.h <imhdr.h> <imio.h> <mach.h>
+ oifrename.x oif.h <error.h> <imhdr.h> <imio.h>
+ oifupdhdr.x oif.h <error.h> <imhdr.h> <imio.h>
+ oifwrhdr.x imhv1.h imhv2.h oif.h <imhdr.h> <imio.h> <mach.h>
+ ;
diff --git a/sys/imio/iki/oif/oif.h b/sys/imio/iki/oif/oif.h
new file mode 100644
index 00000000..d1161659
--- /dev/null
+++ b/sys/imio/iki/oif/oif.h
@@ -0,0 +1,15 @@
+# OIF.H -- IKI/OIF internal definitions.
+
+define MAX_LENEXTN 3 # max length imagefile extension
+define OIF_HDREXTN "imh" # image header filename extension
+define OIF_PIXEXTN "pix" # image pixfile extension
+define LEN_PIXHDR 512 # max length of PIXHDR structure
+define COMPRESS NO # disable alignment of image lines?
+define DEF_VERSION 2 # default file version
+
+define ENV_OIFVER "oifversion" # default format for new images
+define HDR "HDR$" # stands for header directory
+define STRLEN_HDR 4
+
+define TY_IMHDR 1 # main imagefile header
+define TY_PIXHDR 2 # pixel file header
diff --git a/sys/imio/iki/oif/oifaccess.x b/sys/imio/iki/oif/oifaccess.x
new file mode 100644
index 00000000..e5dfe28a
--- /dev/null
+++ b/sys/imio/iki/oif/oifaccess.x
@@ -0,0 +1,51 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include "oif.h"
+
+# OIF_ACCESS -- Test the accessibility or existence of an existing image, or
+# the legality of the name of a new image.
+
+procedure oif_access (kernel, root, extn, acmode, status)
+
+int kernel #I IKI kernel
+char root[ARB] #I root filename
+char extn[ARB] #U extension (SET on output if none specified)
+int acmode #I access mode (0 to test only existence)
+int status #O status
+
+pointer sp, fname
+int btoi(), access(), iki_validextn()
+string oif_extn OIF_HDREXTN
+bool strne()
+
+begin
+ call smark (sp)
+ call salloc (fname, SZ_PATHNAME, TY_CHAR)
+
+ # If new image, test only the legality of the given extension.
+ # This is used to select a kernel given the imagefile extension.
+
+ if (acmode == NEW_IMAGE || acmode == NEW_COPY) {
+ status = btoi (iki_validextn (kernel, extn) > 0)
+ call sfree (sp)
+ return
+ }
+
+ # Reject image if an invalid extension is given.
+ if (extn[1] != EOS && strne (extn, oif_extn)) {
+ status = NO
+ call sfree (sp)
+ return
+ }
+
+ # Check for the imagefile.
+ call iki_mkfname (root, oif_extn, Memc[fname], SZ_PATHNAME)
+ if (access (Memc[fname], acmode, 0) == YES) {
+ if (extn[1] == EOS)
+ call strcpy (oif_extn, extn, MAX_LENEXTN)
+ status = YES
+ } else
+ status = NO
+
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifclose.x b/sys/imio/iki/oif/oifclose.x
new file mode 100644
index 00000000..8eb58b4f
--- /dev/null
+++ b/sys/imio/iki/oif/oifclose.x
@@ -0,0 +1,36 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <protect.h>
+include <error.h>
+include <imhdr.h>
+include <imio.h>
+
+# OIF_CLOSE -- Close an OIF format image. There is little for us to do, since
+# IMIO will already have updated the header if necessary and flushed any pixel
+# output. Neither do we have to deallocate the IMIO descriptor, since it was
+# allocated by IMIO.
+
+procedure oif_close (im, status)
+
+pointer im # image descriptor
+int status
+
+int junk
+int protect()
+
+begin
+ # Close the pixel file and header file, if open.
+ if (IM_PFD(im) != NULL)
+ call close (IM_PFD(im))
+ if (IM_HFD(im) != NULL)
+ call close (IM_HFD(im))
+
+ # If we are closing a new image, set delete protection on the
+ # header file to prevent the user from using DELETE to delete
+ # the image header file, which would leave a headerless pixel
+ # storage file lying about somewhere.
+
+ if (IM_ACMODE(im) == NEW_IMAGE || IM_ACMODE(im) == NEW_COPY)
+ iferr (junk = protect (IM_HDRFILE(im), SET_PROTECTION))
+ call erract (EA_WARN)
+end
diff --git a/sys/imio/iki/oif/oifcopy.x b/sys/imio/iki/oif/oifcopy.x
new file mode 100644
index 00000000..8a7ea41d
--- /dev/null
+++ b/sys/imio/iki/oif/oifcopy.x
@@ -0,0 +1,32 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include "oif.h"
+
+# OIF_COPY -- Copy an image. A special operator is provided for fast, blind
+# copies of entire images.
+
+procedure oif_copy (kernel, old_root, old_extn, new_root, new_extn, status)
+
+int kernel #I IKI kernel
+char old_root[ARB] # old image root name
+char old_extn[ARB] # old image extn
+char new_root[ARB] # new image root name
+char new_extn[ARB] # new extn
+int status
+
+pointer sp
+pointer old_fname, new_fname
+
+begin
+ call smark (sp)
+ call salloc (old_fname, SZ_PATHNAME, TY_CHAR)
+ call salloc (new_fname, SZ_PATHNAME, TY_CHAR)
+
+ # Get filename of old and new images.
+ call iki_mkfname (old_root, old_extn, Memc[old_fname], SZ_PATHNAME)
+ call iki_mkfname (new_root, OIF_HDREXTN, Memc[new_fname], SZ_PATHNAME)
+
+ # For now, this is stubbed out.
+ status = ERR
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifdelete.x b/sys/imio/iki/oif/oifdelete.x
new file mode 100644
index 00000000..758309a7
--- /dev/null
+++ b/sys/imio/iki/oif/oifdelete.x
@@ -0,0 +1,53 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <protect.h>
+include <error.h>
+include <imhdr.h>
+
+# OIF_DELETE -- Delete an image. A special operator is required since the
+# image is stored as two files.
+
+procedure oif_delete (kernel, root, extn, status)
+
+int kernel #I IKI kernel
+char root[ARB] #I root filename
+char extn[ARB] #U extension
+int status
+
+int junk
+pointer sp, fname, pixfile
+int access(), protect()
+pointer im, immapz()
+
+begin
+ call smark (sp)
+ call salloc (fname, SZ_PATHNAME, TY_CHAR)
+ call salloc (pixfile, SZ_PATHNAME, TY_CHAR)
+
+ # Generate filename.
+ call iki_mkfname (root, extn, Memc[fname], SZ_PATHNAME)
+
+ iferr (im = immapz (Memc[fname], READ_ONLY, 0)) {
+ call erract (EA_WARN)
+
+ } else {
+ if (IM_PIXFILE(im) != EOS) {
+ call oif_gpixfname (IM_PIXFILE(im), IM_HDRFILE(im),
+ Memc[pixfile], SZ_PATHNAME)
+ if (access (Memc[pixfile],0,0) == YES)
+ iferr (call delete (Memc[pixfile]))
+ call erract (EA_WARN)
+ }
+
+ call imunmap (im)
+
+ # Do not complain if the file is not protected.
+ iferr (junk = protect (Memc[fname], REMOVE_PROTECTION))
+ ;
+ iferr (call delete (Memc[fname]))
+ call erract (EA_WARN)
+ }
+
+ call sfree (sp)
+ status = OK
+end
diff --git a/sys/imio/iki/oif/oifgpfn.x b/sys/imio/iki/oif/oifgpfn.x
new file mode 100644
index 00000000..cc9a7fef
--- /dev/null
+++ b/sys/imio/iki/oif/oifgpfn.x
@@ -0,0 +1,60 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <knet.h>
+include "oif.h"
+
+# OIF_GPIXFNAME -- Convert a logical pixfile name into a physical pathname.
+
+procedure oif_gpixfname (pixfile, hdrfile, path, maxch)
+
+char pixfile[ARB] # pixfile name
+char hdrfile[ARB] # header file name (gives hdr directory)
+char path[maxch] # receives pathname
+int maxch
+
+int ip, nchars
+pointer sp, fname, op
+int strncmp(), fnldir()
+
+begin
+ # Merely return pathname if not case "HDR$".
+ if (strncmp (pixfile, HDR, STRLEN_HDR) != 0) {
+ call fpathname (pixfile, path, maxch)
+ return
+ }
+
+ call smark (sp)
+ call salloc (fname, SZ_PATHNAME, TY_CHAR)
+
+ # Get host pathname of pixel file directory.
+ nchars = fnldir (hdrfile, Memc[fname], SZ_PATHNAME)
+ call fpathname (Memc[fname], path, maxch)
+
+ # Fold in any subdirectories from the pixfile name.
+ # (as in HDR$pixels/).
+
+ op = fname
+ nchars = 0
+
+ for (ip=STRLEN_HDR+1; pixfile[ip] != EOS; ip=ip+1) {
+ if (pixfile[ip] == '/') {
+ Memc[op] = EOS
+ call zfsubd (path, maxch, Memc[fname], nchars)
+ op = fname
+ } else {
+ Memc[op] = pixfile[ip]
+ op = op + 1
+ }
+ }
+
+ # Tack on the pixel file name, which was left in the fname buf.
+ if (op > fname) {
+ Memc[op] = EOS
+ if (nchars > 0)
+ call strcpy (Memc[fname], path[nchars+1], maxch-nchars)
+ else
+ call strcat (Memc[fname], path, maxch)
+ }
+
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifmkpfn.x b/sys/imio/iki/oif/oifmkpfn.x
new file mode 100644
index 00000000..234fa706
--- /dev/null
+++ b/sys/imio/iki/oif/oifmkpfn.x
@@ -0,0 +1,118 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <syserr.h>
+include <imhdr.h>
+include <imio.h>
+include <knet.h>
+include "oif.h"
+
+# OIF_MKPIXFNAME -- Generate the pixel file name. Leave the logical pixfile
+# name in the image header, and return the pathname to the pixel file in the
+# output argument.
+
+procedure oif_mkpixfname (im, pixfile, maxch)
+
+pointer im # image descriptor
+char pixfile[maxch] # receives pathname to pixfile
+int maxch
+
+char suffix[2]
+int len_osdir, len_root, len_extn, n
+pointer sp, imdir, osdir, root, extn, subdir, fname, ip, op
+
+bool fnullfile()
+int fnroot(), fnldir(), access(), envgets(), strncmp()
+string pixextn OIF_PIXEXTN
+errchk fmkdir, imerr
+
+begin
+ # Clear junk text at the end of the filename.
+ call aclrc (IM_PIXFILE(im), SZ_IMPIXFILE)
+
+ # Check for the null image.
+ if (fnullfile (IM_HDRFILE(im))) {
+ call strcpy ("dev$null", IM_PIXFILE(im), SZ_IMPIXFILE)
+ call strcpy (IM_PIXFILE(im), pixfile, maxch)
+ return
+ }
+
+ call smark (sp)
+ call salloc (imdir, SZ_PATHNAME, TY_CHAR)
+ call salloc (osdir, SZ_PATHNAME, TY_CHAR)
+ call salloc (root, SZ_PATHNAME, TY_CHAR)
+ call salloc (subdir, SZ_PATHNAME, TY_CHAR)
+ call salloc (fname, SZ_PATHNAME, TY_CHAR)
+ call salloc (extn, SZ_FNAME, TY_CHAR)
+
+ if (envgets ("imdir", Memc[imdir], SZ_PATHNAME) <= 0)
+ call strcpy (HDR, Memc[imdir], SZ_PATHNAME)
+
+ if (strncmp (Memc[imdir], HDR, STRLEN_HDR) == 0) {
+ # Put pixfile in same directory as the header or in a subdirectory.
+ # In the latter case, create the directory if it does not already
+ # exist.
+
+ ip = imdir + STRLEN_HDR
+ for (op=subdir; Memc[ip] != EOS && Memc[ip] != '/'; ip=ip+1) {
+ Memc[op] = Memc[ip]
+ op = op + 1
+ }
+ Memc[op] = EOS
+
+ if (Memc[subdir] != EOS) {
+ n = fnldir (IM_HDRFILE(im), Memc[fname], SZ_PATHNAME)
+ call fpathname (Memc[fname], Memc[fname], SZ_PATHNAME)
+ call zfsubd (Memc[fname], SZ_PATHNAME, Memc[subdir], n)
+ if (access (Memc[fname], 0, DIRECTORY_FILE) == NO)
+ call fmkdir (Memc[fname])
+ }
+ } else
+ call fpathname (Memc[imdir], Memc[imdir], SZ_PATHNAME)
+
+ # Make up the root name of the new pixel file. Take the root part of
+ # the header file and escape sequence encode it. We have to do this
+ # because it is to be concatenated to an OS directory name, which will
+ # prevent translation of the root file name during normal filename
+ # mapping.
+
+ if (fnroot (IM_HDRFILE(im), Memc[fname], SZ_PATHNAME) <= 0)
+ call strcpy (pixextn, Memc[fname], SZ_PATHNAME)
+ call iki_mkfname (Memc[fname], pixextn, Memc[fname], SZ_PATHNAME)
+ call vfn_translate (Memc[fname], Memc[osdir], len_osdir,
+ Memc[root], len_root, Memc[extn], len_extn)
+
+ suffix[1] = 'a'
+ suffix[2] = 'a'
+ suffix[3] = EOS
+
+ for (n=0; ; n=n+1) {
+ call sprintf (IM_PIXFILE(im), SZ_PATHNAME, "%s%s.%s")
+ call pargstr (Memc[imdir])
+ call pargstr (Memc[root])
+ call pargstr (pixextn)
+
+ call oif_gpixfname (IM_PIXFILE(im), IM_HDRFILE(im), pixfile, maxch)
+
+ # Ensure that the filename is unique.
+ if (access (pixfile, 0,0) == YES) {
+ if (n == 0) {
+ for (op=root; Memc[op] != EOS; op=op+1)
+ ;
+ } else {
+ if (suffix[2] == 'z') {
+ suffix[2] = 'a'
+ if (suffix[1] == 'z')
+ call imerr (IM_NAME(im), SYS_FMKTEMP)
+ else
+ suffix[1] = suffix[1] + 1
+ } else
+ suffix[2] = suffix[2] + 1
+ }
+
+ call strcpy (suffix, Memc[op], 2)
+ } else
+ break
+ }
+
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifopen.x b/sys/imio/iki/oif/oifopen.x
new file mode 100644
index 00000000..a280f163
--- /dev/null
+++ b/sys/imio/iki/oif/oifopen.x
@@ -0,0 +1,137 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <syserr.h>
+include <error.h>
+include <imhdr.h>
+include <imio.h>
+include <fio.h>
+include "oif.h"
+
+# OIF_OPEN -- Open/create an image.
+
+procedure oif_open (kernel, im, o_im, root, extn, ksection, cl_index, cl_size, acmode, status)
+
+int kernel #I IKI kernel
+pointer im #I image descriptor
+pointer o_im #I old image, if new_copy image
+char root[ARB] #I root image name
+char extn[ARB] #I extension, if any
+char ksection[ARB] #I NOT USED
+int cl_index #I NOT USED
+int cl_size #I NOT USED
+int acmode #I access mode
+int status #O return value
+
+pointer sp, fname, pixfile
+int hfd, nchars, mode, junk
+int open(), oif_rdhdr(), access(), protect(), envgeti()
+bool envgetb(), fnullfile()
+errchk syserrs
+define err_ 91
+
+begin
+ call smark (sp)
+ call salloc (fname, SZ_PATHNAME, TY_CHAR)
+ call salloc (pixfile, SZ_PATHNAME, TY_CHAR)
+
+ status = OK
+
+ # The only valid cl_index is -1 (none specified) or 1.
+ if (!(cl_index < 0 || cl_index == 1))
+ goto err_
+
+ # This kernel does not permit a kernel section to be used.
+ if (ksection[1] != EOS)
+ call syserrs (SYS_IKIKSECTNS, Memc[fname])
+
+ # Determine access mode for header file.
+ if (acmode == NEW_COPY || acmode == NEW_IMAGE)
+ mode = NEW_FILE
+ else
+ mode = acmode
+
+ # Generate full header file name; the extension may be either ".imh"
+ # or nothing, and was set earlier by oif_access().
+
+ if (mode == NEW_FILE && extn[1] == EOS)
+ call iki_mkfname (root, OIF_HDREXTN, Memc[fname], SZ_PATHNAME)
+ else
+ call iki_mkfname (root, extn, Memc[fname], SZ_PATHNAME)
+
+ # Delete any old image if one exists and imclobber is enabled.
+ if (mode == NEW_FILE && !fnullfile (Memc[fname]) &&
+ (access (Memc[fname], 0,0) == YES)) {
+
+ if (envgetb ("imclobber")) {
+ iferr (hfd = open (Memc[fname], READ_ONLY, BINARY_FILE)) {
+ status = ERR
+ goto err_
+ }
+ nchars = LEN_IMHDR * SZ_MII_INT
+ if (oif_rdhdr (hfd, im, nchars, TY_IMHDR) < 0) {
+ status = ERR
+ goto err_
+ }
+ if (IM_PIXFILE(im) != EOS) {
+ call oif_gpixfname (IM_PIXFILE(im), IM_HDRFILE(im),
+ Memc[pixfile], SZ_PATHNAME)
+ if (access (Memc[pixfile],0,0) == YES)
+ iferr (call delete (Memc[pixfile]))
+ call erract (EA_WARN)
+ }
+ call close (hfd)
+ iferr (junk = protect (Memc[fname], REMOVE_PROTECTION))
+ ;
+ iferr (call delete (Memc[fname]))
+ call erract (EA_WARN)
+ } else
+ call syserrs (SYS_IKICLOB, Memc[fname])
+ }
+
+ # Open the image header file.
+ iferr (hfd = open (Memc[fname], mode, BINARY_FILE))
+ goto err_
+
+ IM_HFD(im) = hfd
+
+ # If opening an existing image, read the OIF fixed format binary
+ # image header into the image descriptor. If opening a new image,
+ # write out a generic image header so that the image can be accessed
+ # and deleted with imdelete should the operation be aborted before
+ # a full image is written.
+
+ if (mode == NEW_FILE) {
+ iferr (IM_HDRVER(im) = envgeti (ENV_OIFVER))
+ IM_HDRVER(im) = DEF_VERSION
+ call aclrc (IM_HDRFILE(im), SZ_IMHDRFILE)
+ call strcpy (Memc[fname], IM_HDRFILE(im), SZ_IMHDRFILE)
+ iferr (call oif_updhdr (im, status))
+ ;
+ } else {
+ iferr {
+ nchars = (IM_LENHDRMEM(im) - LEN_IMHDR) * SZ_MII_INT
+ if (oif_rdhdr (hfd, im, nchars, TY_IMHDR) < 0)
+ status = ERR
+ else {
+ call aclrc (IM_HDRFILE(im), SZ_IMHDRFILE)
+ call strcpy (Memc[fname], IM_HDRFILE(im), SZ_IMHDRFILE)
+ }
+ } then
+ status = ERR
+ }
+
+ # It is best to close the header file at this point for two reasons:
+ # to improve error recovery (if an abort occurs with a new file still
+ # open FIO will delete it) and to free file descriptors (important for
+ # applications that open many images). If the header needs to be
+ # updated, oif_updhdr will reopen the header file.
+
+ call close (hfd)
+ IM_HFD(im) = NULL
+
+ call sfree (sp)
+ return
+err_
+ status = ERR
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifopix.x b/sys/imio/iki/oif/oifopix.x
new file mode 100644
index 00000000..c9652374
--- /dev/null
+++ b/sys/imio/iki/oif/oifopix.x
@@ -0,0 +1,103 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <syserr.h>
+include <config.h>
+include <imhdr.h>
+include <imio.h>
+include "oif.h"
+
+# OIF_OPIX -- Open (or create) the pixel storage file. If the image header file
+# is `image.imh' the associated pixel storage file will be `imdir$image.pix',
+# or some variation thereon should a collision occur. The environment variable
+# IMDIR controls where the pixfile will be placed. The following classes of
+# values are provided:
+#
+# path Put pixfile in named absolute directory regardless of
+# where the header file is.
+# ./ Put pixfile in the current directory at image creation
+# time (special case of previous case).
+# HDR$ Put pixfile in the same directory as the header file.
+# HDR$subdir/ Put pixfiles in the subdirectory `subdir' of the
+# directory containing the header file. IMIO will
+# create the subdirectory if necessary.
+
+procedure oif_opix (im, status)
+
+pointer im # image descriptor
+int status # return status
+
+long pixoff
+pointer sp, pixhdr, pixfile
+int pfd, blklen
+
+int open(), fdevblk(), oif_rdhdr()
+errchk open, falloc, fdevblk, imerr, oif_rdhdr, oif_updhdr
+errchk imioff, oif_wrhdr, oif_mkpixfname, oif_gpixfname, flush
+
+begin
+ status = OK
+ if (IM_PFD(im) != NULL)
+ return
+
+
+ call smark (sp)
+ call salloc (pixhdr, LEN_IMDES + LEN_PIXHDR, TY_STRUCT)
+ call salloc (pixfile, SZ_PATHNAME, TY_CHAR)
+
+ switch (IM_ACMODE(im)) {
+ case READ_ONLY, READ_WRITE, WRITE_ONLY, APPEND:
+ if (IM_PIXFILE(im) == EOS)
+ call imerr (IM_NAME(im), SYS_IMRDPIXFILE)
+
+ call oif_gpixfname (IM_PIXFILE(im), IM_HDRFILE(im), Memc[pixfile],
+ SZ_PATHNAME)
+ pfd = open (Memc[pixfile], IM_ACMODE(im), STATIC_FILE)
+
+ call seek (pfd, BOFL)
+ if (oif_rdhdr (pfd, pixhdr, 0, TY_PIXHDR) < 0)
+ call imerr (IM_NAME(im), SYS_IMRDPIXFILE)
+
+ case NEW_COPY, NEW_FILE, TEMP_FILE:
+ # Generate the pixel file name.
+ call oif_mkpixfname (im, Memc[pixfile], SZ_PATHNAME)
+
+ # Compute the offset to the pixels in the pixfile. Allow space
+ # for the pixhdr pixel storage file header and start the pixels
+ # on the next device block boundary.
+
+ blklen = fdevblk (Memc[pixfile])
+ pixoff = LEN_PIXHDR * SZ_MII_INT
+ call imalign (pixoff, blklen)
+
+ # Call IMIO to initialize the physical dimensions of the image
+ # and the absolute file offsets of the major components of the
+ # pixel storage file.
+
+ call imioff (im, pixoff, COMPRESS, blklen)
+
+ # Open the new pixel storage file (preallocate space if
+ # enabled on local system). Save the physical pathname of
+ # the pixfile in the image header, in case "imdir$" changes.
+
+ if (IM_FALLOC == YES) {
+ call falloc (Memc[pixfile], IM_HGMOFF(im) - 1)
+ pfd = open (Memc[pixfile], READ_WRITE, STATIC_FILE)
+ } else
+ pfd = open (Memc[pixfile], NEW_FILE, BINARY_FILE)
+
+ # Write small header into pixel storage file. Allows detection of
+ # headerless pixfiles, and reconstruction of header if it gets lost.
+
+ call oif_wrhdr (pfd, im, TY_PIXHDR)
+ call flush (pfd)
+
+ # Update the image header so that it knows about the pixel file.
+ call oif_updhdr (im, status)
+
+ default:
+ call imerr (IM_NAME(im), SYS_IMACMODE)
+ }
+
+ IM_PFD(im) = pfd
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifrdhdr.x b/sys/imio/iki/oif/oifrdhdr.x
new file mode 100644
index 00000000..b11601cb
--- /dev/null
+++ b/sys/imio/iki/oif/oifrdhdr.x
@@ -0,0 +1,196 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <mach.h>
+include <imhdr.h>
+include <imio.h>
+include "imhv1.h"
+include "imhv2.h"
+include "oif.h"
+
+
+# OIF_RDHDR -- Read the image header. Either the main image header or the
+# pixel file header can be read.
+
+int procedure oif_rdhdr (fd, im, uchars, htype)
+
+int fd #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 miireadc(), miireadi(), miireadl(), miireadr()
+int btoi(), read()
+
+errchk read, miireadc, miireadi, miireadl, miireadr
+define readerr_ 91
+
+begin
+ # Determine the file type.
+ call seek (fd, BOFL)
+ if (read (fd, 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)
+
+ call seek (fd, BOFL)
+ nchars = LEN_V1IMHDR * SZ_MII_INT
+ if (read (fd, 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 && sulen_userarea > 0) {
+ nchars = min (uchars, sulen_userarea * SZ_MII_INT)
+ if (read (fd, Memc[IM_USERAREA(im)], nchars) <= 0)
+ return (ERR)
+ }
+
+ call sfree (sp)
+ return (OK)
+ }
+
+ # Check for a new format header.
+ call seek (fd, BOFL)
+ if (miireadc (fd, 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 (miireadi (fd, hdrlen, 1) != 1)
+ goto readerr_
+ sulen_userarea = hdrlen - LEN_V2IMHDR
+ IM_HDRLEN(im) = LEN_IMHDR + sulen_userarea
+
+ if (miireadi (fd, IM_PIXTYPE(im), 1) != 1)
+ goto readerr_
+
+ # Determine whether to byte swap the pixels.
+ if (miireadi (fd, 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 (miireadi (fd, IM_NDIM(im), 1) < 0)
+ goto readerr_
+ if (miireadi (fd, IM_LEN(im,1), IM_MAXDIM) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_PHYSLEN(im,1), IM_MAXDIM) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_SSMTYPE(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_LUTOFF(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_PIXOFF(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_HGMOFF(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_BLIST(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_SZBLIST(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_NBPIX(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_CTIME(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_MTIME(im), 1) < 0)
+ goto readerr_
+ if (miireadl (fd, IM_LIMTIME(im), 1) < 0)
+ goto readerr_
+
+ if (miireadr (fd, IM_MAX(im), 1) < 0)
+ goto readerr_
+ if (miireadr (fd, IM_MIN(im), 1) < 0)
+ goto readerr_
+
+ if (miireadc (fd, IM_PIXFILE(im), SZ_V2IMPIXFILE) < 0)
+ goto readerr_
+ if (miireadc (fd, IM_HDRFILE(im), SZ_V2IMHDRFILE) < 0)
+ goto readerr_
+ if (miireadc (fd, IM_TITLE(im), SZ_V2IMTITLE) < 0)
+ goto readerr_
+ if (miireadc (fd, IM_HISTORY(im), SZ_V2IMHIST) < 0)
+ goto readerr_
+
+ # Read the variable-length user area.
+ if (uchars > 0 && sulen_userarea > 0) {
+ nchars = min (uchars, sulen_userarea * SZ_MII_INT)
+ if (miireadc (fd, Memc[IM_USERAREA(im)], nchars) < 0)
+ goto readerr_
+ }
+
+ status = OK
+readerr_
+ return (status)
+ }
+
+ return (ERR)
+end
diff --git a/sys/imio/iki/oif/oifrename.x b/sys/imio/iki/oif/oifrename.x
new file mode 100644
index 00000000..edba1bcd
--- /dev/null
+++ b/sys/imio/iki/oif/oifrename.x
@@ -0,0 +1,102 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <error.h>
+include <imhdr.h>
+include <imio.h>
+include "oif.h"
+
+# OIF_RENAME -- Rename an image. A special operator is required since the image
+# is stored as two files.
+
+procedure oif_rename (kernel, old_root, old_extn, new_root, new_extn, status)
+
+int kernel #I IKI kernel
+char old_root[ARB] # old image root name
+char old_extn[ARB] # old image extn
+char new_root[ARB] # new image root name
+char new_extn[ARB] # old image extn
+int status
+
+pointer sp, im
+bool heq, peq
+pointer old_hfn, new_hfn
+pointer old_pfn, new_pfn
+int nchars, old_rootoff, new_rootoff, junk
+
+bool streq()
+pointer immapz()
+int access(), strlen(), strncmp()
+errchk immapz, rename
+
+begin
+ call smark (sp)
+ call salloc (old_hfn, SZ_PATHNAME, TY_CHAR)
+ call salloc (new_hfn, SZ_PATHNAME, TY_CHAR)
+ call salloc (old_pfn, SZ_PATHNAME, TY_CHAR)
+ call salloc (new_pfn, SZ_PATHNAME, TY_CHAR)
+
+ # Get filenames of old and new images.
+ call iki_mkfname (old_root, old_extn, Memc[old_hfn], SZ_PATHNAME)
+ call iki_mkfname (new_root, OIF_HDREXTN, Memc[new_hfn], SZ_PATHNAME)
+ heq = streq (Memc[old_hfn], Memc[new_hfn])
+
+ # Our task here is nontrivial as the pixel file must be renamed as
+ # well as the header file, e.g., since renaming the header file may
+ # move it to a different directory, and the PIXFILE field in the
+ # image header may indicate that the pixel file is in the same dir
+ # as the header. Must open image, get pixfile name from the header,
+ # and generate the new pixfile name. The CURRENT value of IMDIR is
+ # used to generate the new pixfile name.
+
+ im = immapz (Memc[old_hfn], READ_WRITE, 0)
+
+ if (IM_PIXFILE(im) != EOS) {
+ # Get old pixel file filename.
+ call oif_gpixfname (IM_PIXFILE(im), Memc[old_hfn], Memc[old_pfn],
+ SZ_PATHNAME)
+
+ # Get new pixel file filename.
+ call strcpy (Memc[new_hfn], IM_HDRFILE(im), SZ_IMHDRFILE)
+ call oif_mkpixfname (im, Memc[new_pfn], SZ_PATHNAME)
+
+ # Do not change the pixel file name if the name does not change
+ # other than by the addition of the "aa" style suffix added by
+ # mkpixfname.
+
+ peq = false
+ call zfnbrk (old_root, old_rootoff, junk)
+ call zfnbrk (new_root, new_rootoff, junk)
+ peq = streq (old_root[old_rootoff], new_root[new_rootoff])
+
+ if (peq) {
+ nchars = strlen (Memc[new_pfn]) - strlen ("aa.imh")
+ peq = (strncmp (Memc[old_pfn], Memc[new_pfn], nchars) == 0)
+ }
+
+ if (peq)
+ IM_UPDATE(im) = NO
+ else {
+ # If the pixel file rename fails do not rename the header file
+ # and do not change the name of the pixel file in the header.
+
+ iferr (call rename (Memc[old_pfn], Memc[new_pfn])) {
+ if (access (Memc[old_pfn], 0, 0) == YES) {
+ IM_UPDATE(im) = NO
+ call imunmap (im)
+ call erract (EA_ERROR)
+ }
+ }
+ }
+
+ } else
+ call strcpy (Memc[new_hfn], IM_HDRFILE(im), SZ_IMHDRFILE)
+
+ call strcpy (Memc[old_hfn], IM_HDRFILE(im), SZ_IMHDRFILE)
+ call imunmap (im)
+
+ # Rename the header file.
+ if (!heq)
+ call rename (Memc[old_hfn], Memc[new_hfn])
+
+ call sfree (sp)
+end
diff --git a/sys/imio/iki/oif/oifupdhdr.x b/sys/imio/iki/oif/oifupdhdr.x
new file mode 100644
index 00000000..516d62c1
--- /dev/null
+++ b/sys/imio/iki/oif/oifupdhdr.x
@@ -0,0 +1,34 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <syserr.h>
+include <error.h>
+include <imhdr.h>
+include <imio.h>
+include "oif.h"
+
+# OIF_UPDHDR -- Update the image header.
+
+procedure oif_updhdr (im, status)
+
+pointer im #I image descriptor
+int status #O return status
+
+int hfd
+errchk imerr, open, oif_wrhdr, flush
+int open()
+
+begin
+ status = OK
+ hfd = IM_HFD(im)
+
+ if (IM_ACMODE(im) == READ_ONLY)
+ call imerr (IM_NAME(im), SYS_IMUPIMHDR)
+ if (hfd == NULL)
+ hfd = open (IM_HDRFILE(im), READ_WRITE, BINARY_FILE)
+
+ call oif_wrhdr (hfd, im, TY_IMHDR)
+ call flush (hfd)
+
+ if (IM_HFD(im) == NULL)
+ call close (hfd)
+end
diff --git a/sys/imio/iki/oif/oifwrhdr.x b/sys/imio/iki/oif/oifwrhdr.x
new file mode 100644
index 00000000..7b4e7349
--- /dev/null
+++ b/sys/imio/iki/oif/oifwrhdr.x
@@ -0,0 +1,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