# HDF User’s Guide

Version 4.2r4

[Top] [Prev][Next]

> create_and_write_image.c

>     #include "hdf.h"
>     #define  FILE_NAME     "General_RImages.hdf"
>     #define  IMAGE_NAME    "Image Array 1"
>     #define  X_LENGTH      10    /* number of columns in the image */
>     #define  Y_LENGTH      5     /* number of rows in the image */
>     #define  N_COMPS       2     /* number of components in the image */
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn  status;         /* status for functions returning an intn */
>        int32 file_id,        /* HDF file identifier */
>              gr_id,          /* GR interface identifier */
>              ri_id,          /* raster image identifier */
>              start[2],       /* start position to write for each dimension */
>              edges[2],       /* number of elements to be written
>                                along each dimension */
>              dim_sizes[2],   /* dimension sizes of the image array */
>              interlace_mode, /* interlace mode of the image */
>              data_type,      /* data type of the image data */
>              i, j;
>        int16 image_buf[Y_LENGTH][X_LENGTH][N_COMPS];
>        /********************** End of variable declaration **********************/
>        /*
>        * Create and open the file.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_CREATE, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Set the data type, interlace mode, and dimensions of the image.
>        */
>        data_type = DFNT_INT16;
>        interlace_mode = MFGR_INTERLACE_PIXEL;
>        dim_sizes[0] = X_LENGTH;
>        dim_sizes[1] = Y_LENGTH;
>        /*
>        * Create the raster image array.
>        */
>        ri_id = GRcreate (gr_id, IMAGE_NAME, N_COMPS, data_type,
>                          interlace_mode, dim_sizes);
>        /*
>        * Fill the image data buffer with values.
>        */
>        for (i = 0; i < Y_LENGTH; i++)
>        {
>           for (j = 0; j < X_LENGTH; j++)
>           {
>              image_buf[i][j][0] = (i + j) + 1;     /* first component */
>              image_buf[i][j][1] = (i + j) + 1;     /* second component */
>           }
>         }
>        /*
>        * Define the size of the data to be written, i.e., start from the origin
>        * and go as long as the length of each dimension.
>        */
>        start[0] = start[1] = 0;
>        edges[0] = X_LENGTH;
>        edges[1] = Y_LENGTH;
>        /*
>        * Write the data in the buffer into the image array.
>        */
>        status = GRwriteimage(ri_id, start, NULL, edges, (VOIDP)image_buf);
>        /*
>        * Terminate access to the raster image and to the GR interface and,
>        * close the HDF file.
>        */
>        status = GRendaccess (ri_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> create_and_write_image.f

>           program create_raster_image
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*19 FILE_NAME
>           character*13 IMAGE_NAME
>           integer      X_LENGTH
>           integer      Y_LENGTH
>           integer      N_COMPS
>     C
>           parameter (FILE_NAME  = `General_RImages.hdf',
>          +           IMAGE_NAME = `Image Array 1',
>          +           X_LENGTH   = 10,
>          +           Y_LENGTH   = 5,
>          +           N_COMPS    = 2)
>           integer DFACC_CREATE, DFNT_INT16, MFGR_INTERLACE_PIXEL
>           parameter (DFACC_CREATE = 4,
>          +           DFNT_INT16   = 22,
>          +           MFGR_INTERLACE_PIXEL = 0)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgcreat, mgwrimg, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer status
>           integer file_id
>           integer gr_id, ri_id, num_type, interlace_mode
>           integer start(2), stride(2), edges(2), dimsizes(2)
>           integer i, j, k
>           integer*2  image_buf(N_COMPS, X_LENGTH, Y_LENGTH)
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Create and open the file.
>     C
>           file_id = hopen(FILE_NAME, DFACC_CREATE, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Set the number type, interlace mode, and dimensions of the image.
>     C
>           num_type = DFNT_INT16
>           interlace_mode = MFGR_INTERLACE_PIXEL
>           dimsizes(1) = X_LENGTH
>           dimsizes(2) = Y_lENGTH
>     C
>     C     Create the raster image array.
>     C
>           ri_id = mgcreat(gr_id, IMAGE_NAME, N_COMPS, num_type,
>          +                interlace_mode, dimsizes)
>     C
>     C     Fill the image data buffer with values.
>     C
>           do 30 i = 1, Y_LENGTH
>              do 20 j = 1, X_LENGTH
>                 do 10 k = 1, N_COMPS
>                    image_buf(k,j,i) = (i+j) - 1
>     10          continue
>     20       continue
>     30    continue
>     C
>     C     Define the size of the data to be written, i.e., start from the origin
>     C     and go as long as the length of each dimension.
>     C
>           start(1) = 0
>           start(2) = 0
>           edges(1) = X_LENGTH
>           edges(2) = Y_LENGTH
>           stride(1) = 1
>           stride(2) = 1
>     C
>     C     Write the data in the buffer into the image array.
>     C
>           status = mgwrimg(ri_id, start, stride, edges, image_buf)
>     C
>     C     Terminate access to the raster image and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

> modify_image.c

>     #include "hdf.h"
>     #define  FILE_NAME    "General_RImages.hdf"
>     #define  X1_LENGTH    5     /* number of columns in the first image
>                                   being modified */
>     #define  Y1_LENGTH    2     /* number of rows in the first image
>                                   being modified */
>     #define  N1_COMPS     2     /* number of components in the first image */
>     #define  IMAGE1_NAME  "Image Array 1"
>     #define  IMAGE2_NAME  "Image Array 2"
>     #define  X2_LENGTH    6     /* number of columns in the second image */
>     #define  Y2_LENGTH    4     /* number of rows in the second image */
>     #define  N2_COMPS     3     /* number of components in the second image */
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn  status;         /* status for functions returning an intn */
>        int32 file_id,        /* HDF file identifier */
>              gr_id,          /* GR interface identifier */
>              ri1_id,         /* raster image identifier */
>              start1[2],      /* start position to write for each dimension */
>              edges1[2],      /* number of elements to be written along
>                                each dimension */
>              ri2_id,         /* raster image identifier */
>              start2[2],      /* start position to write for each dimension */
>              edges2[2],      /* number of elements to be written along
>                                each dimension */
>              dims_sizes[2],  /* sizes of the two dimensions of the image array */
>              data_type,      /* data type of the image data */
>              interlace_mode; /* interlace mode of the image */
>        int16 i, j;           /* indices for the dimensions */
>        int16 image1_buf[Y1_LENGTH][X1_LENGTH][N1_COMPS]; /* data of first image */
>        char  image2_buf[Y2_LENGTH][X2_LENGTH][N2_COMPS]; /* data of second image*/
>        /********************** End of variable declaration **********************/
>        /*
>        * Open the HDF file for writing.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_WRITE, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Select the first raster image in the file.
>        */
>        ri1_id = GRselect (gr_id, 0);
>        /*
>        * Fill the first image data buffer with values.
>        */
>        for (i = 0; i < Y1_LENGTH; i++)
>        {
>           for (j = 0; j < X1_LENGTH; j++)
>           {
>              image1_buf[i][j][0] = 0;  /* first component */
>              image1_buf[i][j][1] = 0;  /* second component */
>           }
>         }
>        /*
>        * Define the size of the data to be written, i.e., start from the origin
>        * and go as long as the length of each dimension.
>        */
>        start1[0] = start1[1] = 0;
>        edges1[0] = X1_LENGTH;
>        edges1[1] = Y1_LENGTH;
>        /*
>        * Write the data in the buffer into the image array.
>        */
>        status = GRwriteimage (ri1_id, start1, NULL, edges1, (VOIDP)image1_buf);
>        /*
>        * Set the interlace mode and dimensions of the second image.
>        */
>        data_type = DFNT_CHAR8;
>        interlace_mode = MFGR_INTERLACE_PIXEL;
>        dims_sizes[0] = X2_LENGTH;
>        dims_sizes[1] = Y2_LENGTH;
>        /*
>        * Create the second image in the file.
>        */
>        ri2_id = GRcreate (gr_id, IMAGE2_NAME, N2_COMPS, data_type,
>                                     interlace_mode, dims_sizes);
>        /*
>        * Fill the second image data buffer with values.
>        */
>        for (i = 0; i < Y2_LENGTH; i++)
>        {
>           for (j = 0; j < X2_LENGTH; j++)
>           {
>              image2_buf[i][j][0] = `A';     /* first component */
>              image2_buf[i][j][1] = `B';     /* second component */
>              image2_buf[i][j][2] = `C';     /* third component */
>           }
>         }
>        /*
>        * Define the size of the data to be written, i.e., start from the origin
>        * and go as long as the length of each dimension.
>        */
>        for (i = 0; i < 2; i++) {
>           start2[i] = 0;
>           edges2[i] = dims_sizes[i];
>        }
>        /*
>        * Write the data in the buffer into the second image array.
>        */
>        status = GRwriteimage (ri2_id, start2, NULL, edges2, (VOIDP)image2_buf);
>        /*
>        * Terminate access to the raster images and to the GR interface, and
>        * close the HDF file.
>        */
>        status = GRendaccess (ri1_id);
>        status = GRendaccess (ri2_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> modify_image.f

>           program modify_image
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*19 FILE_NAME
>           character*13 IMAGE1_NAME
>           integer      X1_LENGTH
>           integer      Y1_LENGTH
>           integer      N1_COMPS
>           character*13 IMAGE2_NAME
>           integer      X2_LENGTH
>           integer      Y2_LENGTH
>           integer      N2_COMPS
>     C
>           parameter (FILE_NAME   = `General_RImages.hdf',
>          +           IMAGE1_NAME = `Image Array 1',
>          +           IMAGE2_NAME = `Image Array 2',
>          +           X1_LENGTH   = 5,
>          +           Y1_LENGTH   = 2,
>          +           N1_COMPS    = 2,
>          +           X2_LENGTH   = 6,
>          +           Y2_LENGTH   = 4,
>          +           N2_COMPS    = 3)
>           integer DFACC_WRITE, DFNT_INT16, DFNT_CHAR8,
>          +        MFGR_INTERLACE_PIXEL
>           parameter (DFACC_WRITE  = 2,
>          +           DFNT_CHAR8   = 4,
>          +           DFNT_INT16   = 22,
>          +           MFGR_INTERLACE_PIXEL = 0)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgselct, mgcreat, mgwrimg, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer status
>           integer file_id
>           integer gr_id, ri1_id, ri2_id, data_type, interlace_mode
>           integer start1(2), stride1(2), edges1(2)
>           integer start2(2), stride2(2), edges2(2), dim_sizes(2)
>           integer i, j, k
>           integer*2  image1_buf(N1_COMPS, X1_LENGTH, Y1_LENGTH)
>           character  image2_buf(N2_COMPS, X2_LENGTH, Y2_LENGTH)
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Open the HDF file for writing.
>     C
>           file_id = hopen(FILE_NAME, DFACC_WRITE, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Select the first raster image in the file.
>     C
>           ri1_id = mgselct(gr_id, 0)
>     C
>     C     Fill the buffer with values.
>     C
>           do 20 i = 1, Y1_LENGTH
>              do 10 j = 1, X1_LENGTH
>                    image1_buf(1,j,i) = 0
>                    image1_buf(2,j,i) = 0
>     10       continue
>     20    continue
>     C
>     C     Define the part of the data in the first image that will be overwritten
>     C     with the new values from image1_buf.
>     C
>           start1(1) = 0
>           start1(2) = 0
>           edges1(1) = X1_LENGTH
>           edges1(2) = Y1_LENGTH
>           stride1(1) = 1
>           stride1(2) = 1
>     C
>     C     Write the data in the buffer into the image array.
>     C
>           status = mgwrimg(ri1_id, start1, stride1, edges1, image1_buf)
>     C
>     C     Set the number type, interlace mode, and dimensions of the second image.
>     C
>           data_type = DFNT_CHAR8
>           interlace_mode = MFGR_INTERLACE_PIXEL
>           dim_sizes(1) = X2_LENGTH
>           dim_sizes(2) = Y2_LENGTH
>     C
>     C     Create the second image in the file.
>     C
>           ri2_id = mgcreat(gr_id, IMAGE2_NAME, N2_COMPS, data_type,
>          +                interlace_mode, dim_sizes)
>     C
>     C     Fill the image data buffer with values.
>     C
>           do 60 i = 1, Y2_LENGTH
>              do 50 j = 1, X2_LENGTH
>                 do 40 k = 1, N2_COMPS
>                    image2_buf(k,j,i) = char(65 + k - 1)
>     40          continue
>     50       continue
>     60    continue
>     C
>     C     Define the size of the data to be written, i.e., start from the origin
>     C     and go as long as the length of each dimension.
>     C
>           start2(1) = 0
>           start2(2) = 0
>           edges2(1) =  dim_sizes(1)
>           edges2(2) =  dim_sizes(2)
>           stride2(1) = 1
>           stride2(2) = 1
>     C
>     C     Write the data in the buffer into the image array.
>     C
>           status = mgwrimg(ri2_id, start2, stride2, edges2, image2_buf)
>     C
>     C     Terminate access to the raster images and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri1_id)
>           status = mgendac(ri2_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

> read_image.c

>     #include "hdf.h"
>     #define  FILE_NAME       "General_RImages.hdf"
>     #define  N_COMPS         2
>     #define  X_LENGTH        10   /* number of columns of the entire image */
>     #define  Y_LENGTH        5    /* number of rows of the entire image */
>     #define  PART_COLS       2    /* number of columns read for partial image */
>     #define  PART_ROWS       3    /* number of rows read for partial image */
>     #define  SKIP_COLS       5    /* number of columns read for skipped image */
>     #define  SKIP_ROWS       3    /* number of rows read for skipped image */
>     #define  COLS_PART_START 3    /* starting column to read partial image */
>     #define  ROWS_PART_START 1    /* starting row to read partial image */
>     #define  COLS_SKIP_START 1    /* starting column to read skipped image */
>     #define  ROWS_SKIP_START 0    /* starting row to read skipped image */
>     #define  N_STRIDES       2    /* number of elements to skip on each dim. */
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn  status;        /* status for functions returning an intn */
>        int32 index;
>        int32 file_id, gr_id, ri_id,
>              start[2],      /* start position to write for each dimension */
>              edges[2],      /* number of elements to bewritten along
>                                each dimension */
>              stride[2],     /* number of elements to skip on each dimension */
>              dim_sizes[2];  /* dimension sizes of the image array */
>        int16 entire_image[Y_LENGTH][X_LENGTH][N_COMPS],
>              partial_image[PART_ROWS][PART_COLS][N_COMPS],
>              skipped_image[SKIP_ROWS][SKIP_COLS][N_COMPS];
>        int32 i, j;
>        /********************** End of variable declaration **********************/
>        /*
>        * Open the HDF file for reading.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_READ, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Select the first raster image in the file.
>        */
>        ri_id = GRselect (gr_id, 0);
>        /*
>        * Define the size of the data to be read, i.e., start from the origin
>        * and go as long as the length of each dimension.
>        */
>        start[0] = start[1] = 0;
>        edges[0] = X_LENGTH;
>        edges[1] = Y_LENGTH;
>        /*
>        * Read the data from the raster image array.
>        */
>        status = GRreadimage (ri_id, start, NULL, edges, (VOIDP)entire_image);
>        /*
>        * Display only the first component of the image since the two components
>        * have the same data in this example.
>        */
>        printf ("First component of the entire image:\n");
>        for (i = 0; i < Y_LENGTH; i++)
>        {
>           for (j = 0; j < X_LENGTH; j++)
>              printf ("%d ", entire_image[i][j][0]);
>           printf ("\n");
>        }
>        /*
>        * Define the size of the data to be read.
>        */
>        start[0] = COLS_PART_START;
>        start[1] = ROWS_PART_START;
>        edges[0] = PART_COLS;
>        edges[1] = PART_ROWS;
>        /*
>        * Read a subset of the raster image array.
>        */
>        status = GRreadimage (ri_id, start, NULL, edges, (VOIDP)partial_image);
>        /*
>        * Display the first component of the read sample.
>        */
>        printf ("\nThree rows & two cols at 2nd row and 4th column");
>        printf (" of the first component:\n");
>        for (i = 0; i < PART_ROWS; i++)
>        {
>           for (j = 0; j < PART_COLS; j++)
>              printf ("%d ", partial_image[i][j][0]);
>           printf ("\n");
>        }
>        /*
>        * Define the size and the pattern to read the data.
>        */
>        start[0] = COLS_SKIP_START;
>        start[1] = ROWS_SKIP_START;
>        edges[0] = SKIP_COLS;
>        edges[1] = SKIP_ROWS;
>        stride[0] = stride[1] = N_STRIDES;
>        /*
>        * Read all the odd rows and even columns of the image.
>        */
>        status = GRreadimage (ri_id, start, stride, edges, (VOIDP)skipped_image);
>        /*
>        * Display the first component of the read sample.
>        */
>        printf ("\nAll odd rows and even columns of the first component:\n");
>        for (i = 0; i < SKIP_ROWS; i++)
>        {
>           for (j = 0; j < SKIP_COLS; j++)
>              printf ("%d ", skipped_image[i][j][0]);
>           printf ("\n");
>        }
>        /*
>        * Terminate access to the raster image and to the GR interface, and
>        * close the HDF file.
>        */
>        status = GRendaccess (ri_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> read_image.f

>           program read_raster_image
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*19 FILE_NAME
>           integer      X_LENGTH
>           integer      Y_LENGTH
>           integer      N_COMPS
>     C
>           parameter (FILE_NAME  = `General_RImages.hdf',
>          +           X_LENGTH   = 10,
>          +           Y_LENGTH   = 5,
>          +           N_COMPS    = 2)
>           integer PART_COLS, PART_ROWS, SKIP_COLS, SKIP_ROWS
>           integer COLS_PART_START, ROWS_PART_START
>           integer COLS_SKIP_START, ROWS_SKIP_START
>           integer N_STRIDES
>           parameter (PART_COLS = 3, PART_ROWS = 2,
>          +           SKIP_COLS = 3, SKIP_ROWS = 5,
>          +           COLS_PART_START = 1, ROWS_PART_START = 3,
>          +           COLS_SKIP_START = 0, ROWS_SKIP_START = 1,
>          +           N_STRIDES = 2)
>           integer DFACC_READ
>           parameter (DFACC_READ = 1)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgselct, mgrdimg, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer status
>           integer file_id
>           integer gr_id, ri_id
>           integer start(2), stride(2), edges(2)
>           integer i, j
>           integer*2  entire_image(N_COMPS, X_LENGTH, Y_LENGTH)
>           integer*2  partial_image(N_COMPS, PART_ROWS, PART_COLS)
>           integer*2  skipped_image(N_COMPS, SKIP_ROWS, SKIP_COLS)
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Open the HDF file for reading.
>     C
>           file_id = hopen(FILE_NAME, DFACC_READ, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Select the first raster image in the file.
>     C
>           ri_id = mgselct(gr_id, 0)
>     C
>     C     Define the size of the data to be read, i.e., start from the origin
>     C     and go as long as the length of each dimension.
>     C
>           start(1) = 0
>           start(2) = 0
>           edges(1) = X_LENGTH
>           edges(2) = Y_LENGTH
>           stride(1) = 1
>           stride(2) = 1
>     C
>     C     Read the data from the raster image array.
>     C
>           status = mgrdimg(ri_id, start, stride, edges, entire_image)
>     C
>     C     Display only the first component of the image since the two components
>     C     have the same data in this example.
>     C
>           write(*,*) `First component of the entire image'
>           write(*,*)
>           do 10 i = 1, X_LENGTH
>              write(*,1000) (entire_image(1,i,j), j = 1, Y_LENGTH)
>     10    continue
>           write(*,*)
>     C
>     C     Define the size of the data to be read.
>     C
>           start(1) = ROWS_PART_START
>           start(2) = COLS_PART_START
>           edges(1) = PART_ROWS
>           edges(2) = PART_COLS
>           stride(1) = 1
>           stride(2) = 1
>     C
>     C     Read a subset of the raster image array.
>     C
>           status = mgrdimg(ri_id, start, stride, edges, partial_image)
>     C
>     C     Display only the first component of the read sample.
>     C
>           write(*,*)
>          +  `Two rows and three columns at 4th row and 2nd column',
>          +  ` of the first component'
>           write(*,*)
>           do 20 i = 1, PART_ROWS
>              write(*,1000) (partial_image(1,i,j), j = 1, PART_COLS)
>     20    continue
>           write(*,*)
>     C
>     C     Define the size and the pattern to read the data.
>     C
>           start(1) = ROWS_SKIP_START
>           start(2) = COLS_SKIP_START
>           edges(1) = SKIP_ROWS
>           edges(2) = SKIP_COLS
>           stride(1) = N_STRIDES
>           stride(2) = N_STRIDES
>     C
>     C     Read all the odd rows and even columns of the image.
>     C
>           status = mgrdimg(ri_id, start, stride, edges, skipped_image)
>     C
>     C     Display only the first component of the read sample.
>     C
>           write(*,*) `All even rows and odd columns of the first component'
>           write(*,*)
>           do 30 i = 1, SKIP_ROWS
>              write(*,1000) (skipped_image(1,i,j), j = 1, SKIP_COLS)
>     30    continue
>           write(*,*)
>     C
>     C     Terminate access to the raster image and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>     1000  format(1x, 5(I4))
>           end

> image_info.c

>     #include "hdf.h"
>     #define  FILE_NAME    "General_RImages.hdf"
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn  status;            /* status for functions returning an intn */
>        int32 file_id, gr_id, ri_id,
>              n_rimages,         /* number of raster images in the file */
>              n_file_attrs,      /* number of file attributes */
>              ri_index,          /* index of a image */
>              dim_sizes[2],      /* dimensions of an image */
>              n_comps,           /* number of components an image contains */
>              interlace_mode,    /* interlace mode of an image */
>              data_type,         /* number type of an image */
>              n_attrs;           /* number of attributes belong to an image */
>        char  name[MAX_GR_NAME], /* name of an image */
>             *type_string,       /* mapped text of a number type */
>             *interlace_string;  /* mapped text of an interlace mode */
>        /********************** End of variable declaration **********************/
>        /*
>        * Open the file for reading.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_READ, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Determine the contents of the file.
>        */
>        status = GRfileinfo (gr_id, &n_rimages, &n_file_attrs);
>        /*
>        * For each image in the file, get and display the image information.
>        */
>        printf ("RI#    Name       Components  Type         Interlace     \
>        Dimensions   Attributes\n\n");
>        for (ri_index = 0; ri_index < n_rimages; ri_index++)
>        {
>           ri_id = GRselect (gr_id, ri_index);
>           status = GRgetiminfo (ri_id, name, &n_comps, &data_type,
>                               &interlace_mode, dim_sizes, &n_attrs);
>           /*
>           * Map the number type and interlace mode into text strings for output
>           * readability.  Note that, in this example, only two possible types
>           * are considered because of the simplicity of the example.  For real
>           * problems, all possible types should be checked and, if reading the
>           * data is desired, the size of the type must be determined based on the
>           * machine where the program resides.
>           */
>           if (data_type == DFNT_CHAR8)
>              type_string = "Char8";
>           else if (data_type == DFNT_INT16)
>              type_string = "Int16";
>           else
>              type_string = "Unknown";
>           switch (interlace_mode)
>           {
>              case MFGR_INTERLACE_PIXEL:
>                 interlace_string = "MFGR_INTERLACE_PIXEL";
>                 break;
>              case MFGR_INTERLACE_LINE:
>                 interlace_string = "MFGR_INTERLACE_LINE";
>                 break;
>              case MFGR_INTERLACE_COMPONENT:
>                 interlace_string = "MFGR_INTERLACE_COMPONENT";
>                 break;
>              default:
>                 interlace_string = "Unknown";
>                 break;
>           } /* switch */

>           /*
>           * Display the image information for the current raster image.
>           */
>               printf ("%d  %s       %d      %s   %s     %2d,%2d         %d\n",
>                      ri_index, name, n_comps, type_string, interlace_string,
>                      dim_sizes[0], dim_sizes[1], n_attrs);
>           /*
>           * Terminate access to the current raster image.
>           */
>           status = GRendaccess (ri_id);
>        }
>        /*
>        * Terminate access to the GR interface and close the HDF file.
>        */
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> image_info.f

>           program image_info
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*19 FILE_NAME
>     C
>           parameter (FILE_NAME = `General_RImages.hdf')
>           integer DFACC_READ
>           parameter (DFACC_READ = 1)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgselct, mgfinfo, mggiinf, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer status
>           integer file_id, gr_id, ri_id
>           integer n_rimages, n_file_attrs, ri_index
>           integer n_comps, interlace_mode, n_attrs, data_type
>           integer dim_sizes(2)
>           character*10 type_string
>           character*24 interlace_string
>           character*64 name
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Open the HDF file for reading.
>     C
>           file_id = hopen(FILE_NAME, DFACC_READ, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Determine the contents of the file.
>     C
>           status = mgfinfo(gr_id, n_rimages, n_file_attrs)
>     C
>     C     For each image in the file, get and display image information.
>     C
>           do 100 ri_index = 0, n_rimages-1
>              ri_id = mgselct(gr_id, ri_index)
>              status = mggiinf(ri_id, name, n_comps, data_type,
>          +                    interlace_mode, dim_sizes, n_attrs)
>     C
>     C     Map the number type and interlace mode into text strings for
>     C     output readability.
>     C
>           if(data_type .eq. 4) then
>              type_string = `DFNT_CHAR8'
>           else if(data_type .eq. 22) then
>              type_string = `DFNT_INT16'
>           else
>              type_string = `Unknown'
>           endif
>           if (interlace_mode .eq. 0) then
>               interlace_string = `MFGR_INTERLACE_PIXEL'
>           else if(interlace_mode .eq. 1) then
>               interlace_string = `MFGR_INTERLACE_LINE'
>           else if(interlace_mode .eq. 2) then
>               interlace_string = `MFGR_INTERLACE_COMPONENT'
>           else
>              interlace_string = `Unknown'
>           endif
>     C
>     C     Display the image information for the current image.
>     C
>           write(*,*) `Image index: `, ri_index
>           write(*,*) `Image name: `, name
>           write(*,*) `Number of components: `, n_comps
>           write(*,*) `Number type: `, type_string
>           write(*,*) `Interlace mode: `, interlace_string
>           write(*,*) `Dimnesions: `, dim_sizes(1), dim_sizes(2)
>           write(*,*) `Number of image attributes: `, n_attrs
>           write(*,*)
>     C
>     C     Terminate access to the current raster image.
>     C
>           status = mgendac(ri_id)
>     100   continue
>     C
>     C     Terminate access to the GR interface and close the HDF file.
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

> set_attribute.c

>     #include "hdf.h"
>     #define  FILE_NAME          "General_RImages.hdf"
>     #define  IMAGE_NAME         "Image Array 2"
>     #define  F_ATT1_NAME        "File Attribute 1"
>     #define  F_ATT2_NAME        "File Attribute 2"
>     #define  RI_ATT1_NAME       "Image Attribute 1"
>     #define  RI_ATT2_NAME       "Image Attribute 2"
>     #define  F_ATT1_VAL         "Contents of First FILE Attribute"
>     #define  F_ATT2_VAL         "Contents of Second FILE Attribute"
>     #define  F_ATT1_N_VALUES    32
>     #define  F_ATT2_N_VALUES    33
>     #define  RI_ATT1_VAL        "Contents of IMAGE's First Attribute"
>     #define  RI_ATT1_N_VALUES   35
>     #define  RI_ATT2_N_VALUES   6
>     main( )
>     {
>        /************************* Variable declaration **************************/

>        intn  status;         /* status for functions returning an intn */
>        int32 gr_id, ri_id, file_id,
>              ri_index;
>        int16 ri_attr_2[RI_ATT2_N_VALUES] = {1, 2, 3, 4, 5, 6};
>        /********************** End of variable declaration **********************/
>        /*
>        * Open the HDF file.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_WRITE, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Set two file attributes to the file with names, data types, numbers of
>        * values, and values of the attributes specified.
>        */
>        status = GRsetattr (gr_id, F_ATT1_NAME, DFNT_CHAR8, F_ATT1_N_VALUES,
>                            (VOIDP)F_ATT1_VAL);
>        status = GRsetattr (gr_id, F_ATT2_NAME, DFNT_CHAR8, F_ATT2_N_VALUES,
>                            (VOIDP)F_ATT2_VAL);
>        /*
>        * Obtain the index of the image named IMAGE_NAME.
>        */
>        ri_index = GRnametoindex (gr_id, IMAGE_NAME);
>        /*
>        * Obtain the identifier of this image.
>        */
>        ri_id = GRselect (gr_id, ri_index);
>        /*
>        * Set two attributes to the image with names, data types, numbers of
>        * values, and values of the attributes specified.
>        */
>        status = GRsetattr (ri_id, RI_ATT1_NAME, DFNT_CHAR8, RI_ATT1_N_VALUES,
>                            (VOIDP)RI_ATT1_VAL);
>        status = GRsetattr (ri_id, RI_ATT2_NAME, DFNT_INT16, RI_ATT2_N_VALUES,
>                            (VOIDP)ri_attr_2);
>        /*
>        * Terminate access to the image and to the GR interface, and close the
>        * HDF file.
>        */
>        status = GRendaccess (ri_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> set_attribute.f

>           program  set_attribute
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*19 FILE_NAME
>           character*13 IMAGE_NAME
>           character*16 F_ATT1_NAME
>           character*16 F_ATT2_NAME
>           character*17 RI_ATT1_NAME
>           character*17 RI_ATT2_NAME
>           character*32 F_ATT1_VAL
>           character*33 F_ATT2_VAL
>           integer      F_ATT1_N_VALUES
>           integer      F_ATT2_N_VALUES
>           character*35 RI_ATT1_VAL
>           integer      RI_ATT1_N_VALUES
>           integer      RI_ATT2_N_VALUES
>     C
>           parameter (FILE_NAME    = `General_RImages.hdf',
>          +           IMAGE_NAME   = `Image Array 2',
>          +           F_ATT1_NAME  = `File Attribute 1',
>          +           F_ATT2_NAME  = `File Attribute 2',
>          +           RI_ATT1_NAME = `Image Attribute 1',
>          +           RI_ATT2_NAME = `Image Attribute 2',
>          +           F_ATT1_VAL   = `Contents of First FILE Attribute',
>          +           F_ATT2_VAL   = `Contents of Second FILE Attribute',
>          +           F_ATT1_N_VALUES = 32,
>          +           F_ATT2_N_VALUES = 33,
>          +           RI_ATT1_VAL = `Contents of IMAGE''s First Attribute',
>          +           RI_ATT1_N_VALUES = 35,
>          +           RI_ATT2_N_VALUES = 6)
>           integer DFACC_WRITE, DFNT_INT16, DFNT_CHAR8
>           parameter (DFACC_WRITE  = 2,
>          +           DFNT_CHAR8   = 4,
>          +           DFNT_INT16   = 22)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgscatt, mgsnatt , mgn2ndx,
>          +        mgselct, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer   status
>           integer   file_id, gr_id, ri_id, ri_index
>           integer*2 ri_attr_2(RI_ATT2_N_VALUES)
>           integer   i
>           do 10 i = 1, RI_ATT2_N_VALUES
>              ri_attr_2(i) = i
>     10    continue
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Open the HDF file.
>     C
>           file_id = hopen(FILE_NAME, DFACC_WRITE, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Set two file attributes to the file with names, data type, numbers of
>     C     values, and values of attributes specified.
>     C
>           status = mgscatt(gr_id, F_ATT1_NAME, DFNT_CHAR8,
>          +                 F_ATT1_N_VALUES, F_ATT1_VAL)
>           status = mgscatt(gr_id, F_ATT2_NAME, DFNT_CHAR8,
>          +                 F_ATT2_N_VALUES, F_ATT2_VAL)
>     C
>     C     Obtain the index of the image named IMAGE_NAMR.
>     C
>           ri_index = mgn2ndx(gr_id, IMAGE_NAME)
>     C
>     C     Obtain the identifier of this image.
>     C
>           ri_id = mgselct(gr_id, ri_index)
>     C
>     C     Set two attributes of the image with names, data types, number of
>     C     values, and values of the attributes specified.
>     C
>           status = mgscatt(ri_id, RI_ATT1_NAME, DFNT_CHAR8,
>          +                 RI_ATT1_N_VALUES, RI_ATT1_VAL)
>           status = mgsnatt(ri_id, RI_ATT2_NAME, DFNT_INT16,
>          +                 RI_ATT2_N_VALUES, ri_attr_2)
>     C
>     C     Terminate access to the image and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

> get_attribute.c

>     #include "hdf.h"
>     #define  FILE_NAME       "General_RImages.hdf"
>     #define  RI_ATTR_NAME    "Image Attribute 2"
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn   status;          /* status for functions returning an intn */
>        int32  gr_id, ri_id, file_id,
>               f_att_index,     /* index of file attributes */
>               ri_att_index,    /* index of raster image attributes */
>               data_type,       /* image data type */
>               n_values,        /* number of values in an attribute */
>               value_index,     /* index of values in an attribute */
>               n_rimages,       /* number of raster images in the file */
>               n_file_attrs;    /* number of file attributes */
>        char   attr_name[MAX_GR_NAME];  /* buffer to hold the attribute name     */
>        VOIDP  data_buf;                /* buffer to hold the attribute values   */
>        int16 *int_ptr;      /* int16 pointer to point to a void data buffer     */
>        char8 *char_ptr;     /* char8 pointer to point to a void data buffer     */
>        /********************** End of variable declaration **********************/
>        /*
>        * Open the HDF file.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_READ, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Determine the number of attributes in the file.
>        */
>        status = GRfileinfo (gr_id, &n_rimages, &n_file_attrs);
>        if (status != FAIL && n_file_attrs > 0)
>        {
>           for (f_att_index = 0; f_att_index < n_file_attrs; f_att_index++)
>           {
>              /*
>              * Get information about the current file attribute.
>              */
>              status = GRattrinfo (gr_id, f_att_index, attr_name, &data_type,
>                                   &n_values);
>              /*
>              * Allocate a buffer to hold the file attribute data.  In this example,
>              * knowledge about the data type is assumed to be available from
>              * the previous example for simplicity.  In reality, the size
>              * of the type must be determined based on the machine where the
>              * program resides.
>              */
>              if (data_type == DFNT_CHAR8)
>              {
>                 data_buf = malloc (n_values * sizeof (char8));
>                 if (data_buf == NULL)
>                 {
>                    printf ("Unable to allocate space for attribute data.\n");
>                    exit (1);
>                 }
>              }
>              else
>              {
>                 printf ("Unable to determine data type to allocate data buffer.\n");
>                 exit (1);
>              }
>              /*
>              * Read and display the attribute values.
>              */
>              status = GRgetattr (gr_id, f_att_index, (VOIDP)data_buf);
>              char_ptr = (char8 *) data_buf;
>              printf ("Attribute %s: ", attr_name);
>              for (value_index = 0; value_index < n_values; value_index++)
>                 printf ("%c", char_ptr[value_index]);
>              printf ("\n");
>              /*
>              * Free the space allocated for the data buffer.
>              */
>              free (data_buf);
>           } /* for */
>        } /* if */
>        /*
>        * Select the second image in the file.
>        */
>        ri_id = GRselect (gr_id, 1);
>        /*
>        * Find the image attribute named RI_ATTR_NAME.
>        */
>        ri_att_index = GRfindattr (ri_id, RI_ATTR_NAME);
>        /*
>        * Get information about the attribute.
>        */
>        status = GRattrinfo (ri_id, ri_att_index, attr_name, &data_type, &n_values);
>        /*
>        * Allocate a buffer to hold the file attribute data.  As mentioned above,
>        * knowledge about the data type is assumed to be available from
>        * the previous example for simplicity.  In reality, the size of the
>        * type must be determined based on the machine where the program resides.
>        */
>        if (data_type == DFNT_INT16)
>           data_buf = malloc (n_values * sizeof (int16));
>        /*
>        * Read and display the attribute values.
>        */
>        status = GRgetattr (ri_id, ri_att_index, (VOIDP)data_buf);
>        printf ("\nAttribute %s: ", RI_ATTR_NAME);
>        int_ptr = (int16 *)data_buf;
>        for (value_index = 0; value_index < n_values; value_index++)
>           printf ("%d ", int_ptr[value_index]);
>        printf ("\n");
>        /*
>        * Free the space allocated for the data buffer.
>        */
>        free (data_buf);
>        /*
>        * Terminate access to the raster image and to the GR interface, and
>        * close the file.
>        */
>        status = GRendaccess (ri_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> get_attribute.f

>           program  get_attribute
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*19 FILE_NAME
>           character*17 RI_ATTR_NAME
>     C
>           parameter (FILE_NAME    = `General_RImages.hdf',
>          +           RI_ATTR_NAME  = `Image Attribute 2')
>           integer DFACC_READ, DFNT_INT16, DFNT_CHAR8
>           parameter (DFACC_READ   = 1,
>          +           DFNT_CHAR8   = 4,
>          +           DFNT_INT16   = 22)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgfinfo, mgatinf, mggcatt, mggnatt , mgfndat,
>          +        mgselct, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer      status
>           integer      file_id, gr_id, ri_id
>           integer      f_att_index, ri_att_index, data_type, n_values
>           integer      n_rimages, n_file_attrs
>           integer*2    int_buf(10)
>           character*17 attr_name
>           character*80 char_buf
>           integer      i
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Open the HDF file.
>     C
>           file_id = hopen(FILE_NAME, DFACC_READ, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Determine the number of attributes in the file.
>     C
>           status = mgfinfo(gr_id, n_rimages, n_file_attrs)
>           if ((status .NE. -1) .AND. (n_file_attrs .GT. 0)) then
>              do 10 f_att_index = 0, n_file_attrs-1
>     C
>     C        Get information about the current file attribute.
>     C
>              status = mgatinf(gr_id, f_att_index, attr_name, data_type,
>          +                    n_values)
>     C
>     C        Check whether data type is DFNT_CHAR8 in order to use allocated buffer.
>     C
>              if(data_type .NE. DFNT_CHAR8) then
>                 write(*,*)
>          +      `Unable to determine data type to use allocated buffer'
>              else
>     C
>     C           Read and display the attribute values.
>     C
>                 status = mggcatt(gr_id, f_att_index, char_buf)
>                 write(*,*) `Attribute `, attr_name, ` : `,
>          +                 char_buf(1:n_values)
>              endif
>     10       continue
>           endif
>     C
>     C     Select the second image in the file.
>     C
>           ri_id = mgselct(gr_id, 1)
>     C
>     C     Find the image attribute named RI_ATTR_NAME.
>     C
>           ri_att_index = mgfndat(ri_id, RI_ATTR_NAME)
>     C
>     C     Get information about the attribute.
>     C
>           status = mgatinf(ri_id, ri_att_index, attr_name, data_type,
>          +                 n_values)
>     C
>     C     Read and display attribute values.
>     C
>           status = mggnatt(ri_id, ri_att_index, int_buf)
>           write(*,*) `Attributes :', (int_buf(i), i = 1, n_values)
>     C
>     C     Terminate access to the image and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

> write_palette.c

>     #include "hdf.h"
>     #define  FILE_NAME         "Image_with_Palette.hdf"
>     #define  NEW_IMAGE_NAME    "Image with Palette"
>     #define  N_COMPS_IMG       2       /* number of image components */
>     #define  X_LENGTH          5
>     #define  Y_LENGTH          5
>     #define  N_ENTRIES         256     /* number of entries in the palette */
>     #define  N_COMPS_PAL       3       /* number of palette's components */
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn  status,         /* status for functions returning an intn */
>              i, j;
>        int32 file_id, gr_id, ri_id, pal_id,
>              interlace_mode,
>              start[2],     /* holds where to start to write for each dimension  */
>              edges[2],     /* holds how long to write for each dimension */
>              dim_sizes[2];  /* sizes of the two dimensions of the image array   */
>        uint8 image_buf[Y_LENGTH][X_LENGTH][N_COMPS_IMG]; /* data of first image */
>        uint8 palette_buf[N_ENTRIES][N_COMPS_PAL];
>        /********************** End of variable declaration **********************/
>        /*
>        * Open the HDF file.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_CREATE, 0);
>        /*
>        * Initialize the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Define the dimensions and interlace mode of the image.
>        */
>        dim_sizes[0] = X_LENGTH;
>        dim_sizes[1] = Y_LENGTH;
>        interlace_mode = MFGR_INTERLACE_PIXEL;
>        /*
>        * Create the image named NEW_IMAGE_NAME.
>        */
>        ri_id = GRcreate (gr_id, NEW_IMAGE_NAME, N_COMPS_IMG, DFNT_UINT8,
>                          interlace_mode, dim_sizes);
>        /*
>        * Fill the image data buffer with values.
>        */
>        for (i = 0; i < Y_LENGTH; i++)
>        {
>           for (j = 0; j < X_LENGTH; j++)
>           {
>              image_buf[i][j][0] = (i + j) + 1;
>              image_buf[i][j][1] = (i + j) + 2;
>           }
>         }
>        /*
>        * Define the size of the data to be written, i.e., start from the origin
>        * and go as long as the length of each dimension.
>        */
>        start[0] = start[1] = 0;
>        edges[0] = X_LENGTH;
>        edges[1] = Y_LENGTH;
>        /*
>        * Write the data in the buffer into the image array.
>        */
>        status = GRwriteimage (ri_id, start, NULL, edges, (VOIDP)image_buf);
>        /*
>        * Initialize the palette to grayscale.
>        */
>        for (i = 0; i < N_ENTRIES; i++) {
>           palette_buf[i][0] = i;
>           palette_buf[i][1] = i;
>           palette_buf[i][2] = i;
>        }
>        /*
>        * Define palette interlace mode.
>        */
>        interlace_mode = MFGR_INTERLACE_PIXEL;
>        /*
>        * Get the identifier of the palette attached to the image NEW_IMAGE_NAME.
>        */
>        pal_id = GRgetlutid (ri_id, 0);
>        /*
>        * Write data to the palette.
>        */
>        status = GRwritelut (pal_id, N_COMPS_PAL, DFNT_UINT8, interlace_mode,
>                             N_ENTRIES, (VOIDP)palette_buf);
>        /*
>        * Terminate access to the image and to the GR interface, and
>        * close the HDF file.
>        */
>        status = GRendaccess (ri_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> write_palette.f

>           program  write_palette
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*22 FILE_NAME
>           character*18 NEW_IMAGE_NAME
>           integer      X_LENGTH
>           integer      Y_LENGTH
>           integer      N_ENTRIES
>           integer      N_COMPS_IMG
>           integer      N_COMPS_PAL
>     C
>           parameter (FILE_NAME       = `Image_with_Palette.hdf',
>          +           NEW_IMAGE_NAME  = `Image with Palette',
>          +           X_LENGTH        = 5,
>          +           Y_LENGTH        = 5,
>          +           N_ENTRIES       = 256,
>          +           N_COMPS_IMG     = 2,
>          +           N_COMPS_PAL     = 3)
>           integer DFACC_CREATE, DFNT_CHAR8, DFNT_UINT8, MFGR_INTERLACE_PIXEL
>           parameter (DFACC_CREATE = 4,
>          +           DFNT_CHAR8   = 4,
>          +           DFNT_UINT8   = 21,
>          +           MFGR_INTERLACE_PIXEL = 0)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgcreat, mgwcimg, mggltid, mgwclut,
>          +        mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer    file_id, gr_id, ri_id, pal_id
>           integer    interlace_mode
>           integer    start(2), stride(2), edges(2), dim_sizes(2)
>           integer    status
>           integer    i, j
>           character  image_buf(N_COMPS_IMG, X_LENGTH, Y_LENGTH)
>           character  palette_buf(N_COMPS_PAL, N_ENTRIES)
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Create and open the file.
>     C
>           file_id = hopen(FILE_NAME, DFACC_CREATE, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Define interlace mode and dimensions of the image.
>     C
>           interlace_mode = MFGR_INTERLACE_PIXEL
>           dim_sizes(1) = X_LENGTH
>           dim_sizes(2) = Y_lENGTH
>     C
>     C     Create the raster image array.
>     C
>           ri_id = mgcreat(gr_id, NEW_IMAGE_NAME, N_COMPS_IMG, DFNT_CHAR8,
>          +                interlace_mode, dim_sizes)
>     C
>     C     Fill the image data buffer with values.
>     C
>           do 20 i = 1, Y_LENGTH
>              do 10 j = 1, X_LENGTH
>                    image_buf(1,j,i) = char(i + j - 1 )
>                    image_buf(2,j,i) = char(i + j)
>     10       continue
>     20    continue
>     C
>     C     Define the size of the data to be written, i.e., start from the origin
>     C     and go as long as the length of each dimension.
>     C
>           start(1) = 0
>           start(2) = 0
>           edges(1) = X_LENGTH
>           edges(2) = Y_LENGTH
>           stride(1) = 1
>           stride(2) = 1
>     C
>     C     Write the data in the buffer into the image array.
>     C
>           status = mgwcimg(ri_id, start, stride, edges, image_buf)
>     C
>     C     Initilaize the palette buffer to grayscale.
>     C
>           do 40 i = 1, N_ENTRIES
>               do 30 j = 1, N_COMPS_PAL
>                  palette_buf(j,i) = char(i)
>     30        continue
>     40    continue
>     C
>     C     Get the identifier of the palette attached to the image NEW_IMAGE_NAME.
>     C
>           pal_id = mggltid(ri_id, 0)
>     C
>     C     Set palette interlace mode.
>     C
>           interlace_mode = MFGR_INTERLACE_PIXEL
>     C
>     C     Write data to the palette.
>     C
>           status = mgwclut(pal_id, N_COMPS_PAL, DFNT_UINT8, interlace_mode,
>          +                 N_ENTRIES, palette_buf)
>     C
>     C     Terminate access to the raster image and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

> read_palette.c

>     #include "hdf.h"
>     #define  FILE_NAME      "Image_with_Palette.hdf"
>     #define  IMAGE_NAME     "Image with Palette"
>     #define  N_ENTRIES      256     /* number of elements of each color */
>     main( )
>     {
>        /************************* Variable declaration **************************/
>        intn  status,         /* status for functions returning an intn */
>              i, j;
>        int32 file_id, gr_id, ri_id, pal_id, ri_index;
>        int32 data_type, n_comps, n_entries, interlace_mode;
>        uint8 palette_data[N_ENTRIES][3];        /* static because of fixed size */
>        /************************* Variable declaration **************************/
>        /*
>        * Open the file.
>        */
>        file_id = Hopen (FILE_NAME, DFACC_READ, 0);
>        /*
>        * Initiate the GR interface.
>        */
>        gr_id = GRstart (file_id);
>        /*
>        * Get the index of the image IMAGR_NAME.
>        */
>        ri_index = GRnametoindex (gr_id, IMAGE_NAME);
>        /*
>        * Get image identifier.
>        */
>        ri_id = GRselect (gr_id, ri_index);

>        /*
>        * Get the identifier of the palette attached to the image.
>        */
>        pal_id = GRgetlutid (ri_id, ri_index);
>        /*
>        * Obtain and display information about the palette.
>        */
>        status = GRgetlutinfo (pal_id, &n_comps, &data_type, &interlace_mode,
>                               &n_entries);
>        printf ("Palette: %d components; %d entries\n", n_comps, n_entries);
>        /*
>        * Read the palette data.
>        */
>        status = GRreadlut (pal_id, (VOIDP)palette_data);
>        /*
>        * Display the palette data.  Recall that HDF supports only 256 colors.
>        * Each color is defined by its 3 components. Therefore,
>        * verifying the value of n_entries and n_comps is not necessary and
>        * the buffer to hold the palette data can be static.  However,
>        * if more values or colors are added to the model, these parameters
>        * must be checked to allocate sufficient space when reading a palette.
>        */
>        printf ("  Palette Data: \n");
>        for (i=0; i< n_entries; i++)
>        {
>           for (j = 0; j < n_comps; j++)
>              printf ("%i ", palette_data[i][j]);
>           printf ("\n");
>        }
>        printf ("\n");
>        /*
>        * Terminate access to the image and to the GR interface, and
>        * close the HDF file.
>        */
>        status = GRendaccess (ri_id);
>        status = GRend (gr_id);
>        status = Hclose (file_id);
>     }

> read_palette.f

>           program  read_palette
>           implicit none
>     C
>     C     Parameter declaration
>     C
>           character*22 FILE_NAME
>           character*18 IMAGE_NAME
>           integer      N_ENTRIES
>           integer      N_COMPS_PAL
>     C
>           parameter (FILE_NAME   = `Image_with_Palette.hdf',
>          +           IMAGE_NAME  = `Image with Palette',
>          +           N_COMPS_PAL = 3,
>          +           N_ENTRIES   = 256)
>           integer DFACC_READ, DFNT_CHAR8, DFNT_UINT8, MFGR_INTERLACE_PIXEL
>           parameter (DFACC_READ  = 1,
>          +           DFNT_CHAR8  = 4,
>          +           DFNT_UINT8  = 21,
>          +           MFGR_INTERLACE_PIXEL = 0)
>     C
>     C     Function declaration
>     C
>           integer hopen, hclose
>           integer mgstart, mgn2ndx, mgselct, mggltid, mgglinf,
>          +        mgrclut, mgendac, mgend
>     C
>     C**** Variable declaration *******************************************
>     C
>           integer    file_id, gr_id, ri_id, ri_index, pal_id, pal_index
>           integer    interlace_mode
>           integer    data_type, n_comps, n_entries_out
>           integer    status
>           integer    i, j
>           character  palette_data(N_COMPS_PAL, N_ENTRIES)
>     C
>     C**** End of variable declaration ************************************
>     C
>     C
>     C     Open the file.
>     C
>           file_id = hopen(FILE_NAME, DFACC_READ, 0)
>     C
>     C     Initialize the GR interface.
>     C
>           gr_id = mgstart(file_id)
>     C
>     C     Get the index of the image IMAGE_NAME.
>     C
>           ri_index = mgn2ndx(gr_id, IMAGE_NAME)
>     C
>     C     Get the image identifier.
>     C
>           ri_id = mgselct(gr_id, 0)
>     C
>     C     Get the identifier of the palette attached to the image.
>     C
>           pal_index = 0
>           pal_id = mggltid(ri_id, pal_index)
>     C
>     C     Obtain information about the palette.
>     C
>           status = mgglinf(pal_id, n_comps, data_type, interlace_mode,
>          +                 n_entries_out)
>           write(*,*) ` Palette: `, n_comps, ` components;  `,
>          +           n_entries_out, ` entries'
>     C
>     C     Read the palette.
>     C
>           status = mgrclut(pal_id, palette_data)
>     C
>     C     Display the palette data.
>     C
>           write(*,*) "Palette data"
>           do 10 i = 1, n_entries_out
>              write(*,*) (ichar(palette_data(j,i)), j = 1, n_comps)
>     10    continue
>     C
>     C     Terminate access to the raster image and to the GR interface,
>     C     and close the HDF file.
>     C
>           status = mgendac(ri_id)
>           status = mgend(gr_id)
>           status = hclose(file_id)
>           end

HDF4.2r4 - February 2009 Copyright | The HDF Group www.hdfgroup.org