C functions, that are equivalent to the Fortran subroutines, are available for those who prefer this
language (indeed using IMG from C can seem more natural as IMG returns pointers to data).
The major changes in the interface (compared with the Fortran specifications) are that the
names of functions change from img_something
or hdr_something
to imgSomething
and
hdrSomething
and that “strings” which are returned or can be passed as arrays also have a length
argument.
void imgCancl (char *param, int *status)
void imgDelet (char *param, int *status)
void imgFree (char *param, int *status)
void imgIn[N][X] (char *param, int *nx, [int *ny,] [int *nz,] TYPE **ip, int
*status)
void imgIndf (char *param, int *indf, int *status)
void imgMod[N][X] (char *param, int *nx, [int *ny,] [int nz,] TYPE **ip, int
*status)
void imgName[N][X] (char *param, char *value, int value_length, int *status)
void imgNew[N][X] (char *param, int nx, [int ny,] [int nz,] TYPE **ip, int
*status)
void imgOut[X] (char *param1, char *param2, TYPE **ip, int *status)
void imgTmp[N][X] (char *param, int nx, [int ny,] [int nz,] TYPE **ip, int
*status)
Where the parts in "[]"
are optional or not always available. [N]
indicates the number of dimensions
of the data you want to access and the necessary nx
, ny
and nz
arguments should be passed.
Number of dimensions [N] | nx , ny & nz required |
1 | nx |
2 | nx & ny |
3 | nx , ny & nz |
[N]
by anything).
The ordering of array dimensions is different in C to Fortran, so for instance the Fortran array
ARRAY(NX,NY,NZ)
is equivalent to the C array ARRAY[NZ][NY][NX]
, but usually this is of little
importance since images are not of fixed sizes (so you’re unlikely to hard code any array dimensions)
and you’re far more likely to use the data via a pointer to the first element. Since C arrays also always
start at 0 (rather than 1 as is the default in Fortran) the index of an element say [y][x]
is
nx
y
x
.
In 3-D this becomes [z][y][x]
—
nx
ny
z
nx
y
x
.
The data type of the image is specified by replacing the [X]
part of the name by one of the following character
codes:
Character code [X] | C data type (TYPE ) | Description |
F | float | |
D | double | |
I | int | |
S | short | |
US | unsigned short | |
B | signed char | Signed byte |
UB | unsigned char | Unsigned byte |
TYPE
by the related data type. The default data type is float
(in which
case do not replace [X]
by anything and set TYPE
to float
).
void hdrCopy ( char *param1, char *xname1, char *param2, char *xname2, int
*status )
void hdrDelet (char *param, char *xname, char *item, int comp, int *status)
void hdrIn (char *param, char *xname, char *item, int comp, char *value,
int value_length, int *status)
void hdrInC (char *param, char *xname, char *item, int comp, char *value,
int value_length, int *status)
void hdrIn[X] (char *param, char *xname, char *item, int comp, TYPE *value,
int *status)
void hdrMod (char *param, int *status)
void hdrName (char *param, char *xname, int n, char *item, int item_length,
int *status)
void hdrNumb (char *param, char *xname, char *item, int *n, int *status)
void hdrOut (char *param, char *xname, char *item, char *commen, char *value,
int value_length, int *status)
void hdrOutC (char *param, char *xname, char *item, char *commen, char *value,
int value_length, int *status)
void hdrOut[X] (char *param, char *xname, char *item, char *commen, TYPE
*value,
int *status)
"[X]"
indicate the data type of the header items. The possible values of [X]
are: Character code[X] | C data type (TYPE) | Description |
C | char | Character string |
D | double | |
I | int | |
L | int | Boolean |
F | float | |
This section shows a C version of the mean
program from §2.1.
The following notes refer to the numbered statements:
img.h
contains prototypes of all the IMG and HDR functions.
int *
argument. This is a replacement for the main
function. Note that the function is named
the same as the eventual program except for the trailing underscore. The underscore is
required as this function will be called from a Fortran subroutine (you should also note
that this “trick” may not be the same from machine to machine, to do this portably you
should use the CNF (SUN/209) macros as in the full example of mean.c
). As before an interface file - mean.ifl
- is also required to complete the program.
This example (flat.c
) shows how to create a new image. It also sets all the elements of the image to
1.0.
This example shows how to access an existing image and modify its values.
This example (add.c
) copies the input image and then modifies the copy.
This example (shadow.c
) shows how to create temporary images. The HDS (SUN/92) manual
describes some reasons why you might consider using temporary images rather than “malloc’d”
memory in your programs.
This section just shows some example calls that access spectra and data-cubes.
So far all the example shown access the image data assuming a type of float
. IMG can also access
data in other useful types. A list of these is shown in §C.1. Among the possible calls are:
Requirements for data types and dimensionalities can be mixed as in:
This example (proc.c
) shows how IMG can access more than one image per function call:
ip
is declared as an array of pointers to float
.
ip
is set to point to a different image. The advantage
of such a call is that all the images are guaranteed to be the same shape and data type. The way that you handle “bad” values in your data is similar to that used in Fortran, see §4.1.6, however,
the bad data values themselves are now defined in the img.h
file and have names to reflect the C data
types.
Bad value macro | C data type | |
VAL__BADF | float | |
VAL__BADD | double |
|
VAL__BADI | int | |
VAL__BADS | short |
|
VAL__BADUS | unsigned short |
|
VAL__BADB | signed char |
|
VAL__BADUB | unsigned char |
|
This example (hdrread.c
) shows how to read the value of a header item.
hdrIn
(this
argument is not present in the Fortran version). This example (hdrwrite.c
) shows how to write a header item to an image.
"Fred
Bloggs"
) has been passed. This could also have been set to the value 12, the string length.
This argument is of more significance when dealing with multiple images. All the possible types of header items that can be accessed are described in §C.2. Among the possible calls are:
This example (hdrlist.c
) shows how to access header items by index.
30
& 80
) that pass the maximum length of the output strings.
It is possible to read and write header items from/to many images in one call and some examples
follow. Note that arrays of strings can only be passed using fixed string length. It is not possible to use
arrays of pointers to char
forming a ragged array. When using a character array you should pass a
pointer to the first element not the actual array (as the HDR routines expect to see char
*
).
This example shows how to read the same header item from two images at the same time.
RA
and DEC
arrays is passed. This could have been
written as &RA[0][0]
and &DEC[0][0]
. This example shows how to write values to the same header item of two images.
Compiling and linking a C program is very similar to the methods described (in §6), except that you must compile the source code before the linking stage.
If your program source file is named prog.c
, and it only depends on IMG, then the commands: