File: tmgrattr.c

package info (click to toggle)
libhdf4 4.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,384 kB
  • sloc: ansic: 128,700; sh: 15,015; fortran: 12,444; java: 5,863; xml: 1,205; makefile: 794; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (472 lines) | stat: -rw-r--r-- 19,442 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/****************************************************************************
 * tmgrattr.c - tests the attribute functions for the multi-file GR interface
 * Structure of the file:
 *    test_mgr_attr - test driver
 *        test_mgr_fillvalues - tests with fill value attributes
 *        test_mgr_userattr - tests with user-defined attributes
 *
 ****************************************************************************/

#include "hdf.h"
#include <stdio.h>
#include "tutils.h"

#include "tproto.h"
#define TESTFILE "tmgratt.hdf"

#define F_ATT1_VAL       "Contents of First FILE Attribute"
#define IMAGE1_NAME      "Image 1"
#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_N_VALUES  32
#define F_ATT2_N_VALUES  5
#define N_COMPS          4
#define RI_ATT1_VAL      "Contents of IMAGE1's First Attribute"
#define RI_ATT_N_VALUES  4
#define RI_ATT1_N_VALUES 36
#define RI_ATT2_N_VALUES 6

/* Pixel with fill values */
static float32 fill_pixel[RI_ATT_N_VALUES]  = {1.3, -2.4, 1000.3, .25};
static uint8   file_attr_2[F_ATT2_N_VALUES] = {1, 2, 3, 4, 5};

/********************************************************************
   Name: test_mgr_fillvalues()

   Description:
        This test routine sets fill values to an RI with GRsetattr
        then reads the values back with GRgetattr and verifies them.

   Return value:
        The number of errors occurred in this routine.

*********************************************************************/
static int
test_mgr_fillvalues()
{
    int32        fid;                   /* HDF file ID */
    int32        grid;                  /* ID for the GR interface */
    int32        riid;                  /* ID for the RI image */
    int32        attr_index;            /* attribute index */
    int32        dims[2] = {5, 7};      /* dimensions used on all images */
    uint16       ref;                   /* RI reference number */
    int32        ri_index;              /* RI index */
    float32      image[7][5][N_COMPS];  /* space for the image data */
    void        *read_fill_vals = NULL; /* space for fill values read from attr */
    float32      image0[7][5][N_COMPS]; /* space for the image data */
    int32        start[2];              /* start of image data to grab */
    int32        stride[2];             /* stride of image data to grab */
    char         attr_name[H4_MAX_GR_NAME];
    int32        ntype, n_values;
    hdf_ntinfo_t nt_info; /* struct containing name and byte order of a num type */
    int32        ret;     /* generic return value */

    MESSAGE(8, printf("Reading fill-value attribute\n"););

    fid = Hopen(TESTFILE, DFACC_CREATE, 0);
    CHECK(fid, FAIL, "Hopen");

    /* Initialize the GR interface */
    grid = GRstart(fid);
    CHECK(grid, FAIL, "GRstart");

    {
        /* Create empty image with default fill-values */
        riid = GRcreate(grid, IMAGE1_NAME, N_COMPS, DFNT_FLOAT32, MFGR_INTERLACE_PIXEL, dims);
        CHECK(riid, FAIL, "GRcreate");

        /* Set the fill-value */
        ret = GRsetattr(riid, FILL_ATTR, DFNT_FLOAT32, RI_ATT_N_VALUES, fill_pixel);
        CHECK(ret, FAIL, "GRsetattr");

        /* Save the ref. # for later access */
        ref = GRidtoref(riid);
        CHECK(ref, (uint16)FAIL, "GRidtoref");

        /* Close the empty image */
        ret = GRendaccess(riid);
        CHECK(ret, FAIL, "GRendaccess");

        /* Get the index of the newly created image */
        ri_index = GRreftoindex(grid, ref);
        CHECK(ri_index, FAIL, "GRreftoindex");

        /* Select the newly created image */
        riid = GRselect(grid, ri_index);
        CHECK(riid, FAIL, "GRselect");

        /* Buffer to read image's data in */
        memset(image, 0, (size_t)(dims[0] * dims[1] * N_COMPS) * sizeof(float32));

        /* Fill the memory-only with the default pixel fill-value */
        HDmemfill(image0, fill_pixel, sizeof(fill_pixel), sizeof(image0) / sizeof(fill_pixel));

        /* Read and verify image's data, should only be fill-values */
        start[0] = start[1] = 0;
        stride[0] = stride[1] = 1;
        ret                   = GRreadimage(riid, start, stride, dims, image);
        CHECK(ret, FAIL, "GRreadimage");

        if (memcmp(image, image0, sizeof(image0)) != 0) {
            MESSAGE(3, printf("Error reading data for image with user defined fill-values\n"););
            num_errs++;
        }

        /* Find the image's attribute named FILL_ATTR */
        attr_index = GRfindattr(riid, FILL_ATTR);
        VERIFY(attr_index, 0, "GRfindattr");

        /* Get information about the current attribute. */
        ret = GRattrinfo(riid, attr_index, attr_name, &ntype, &n_values);
        CHECK(ret, FAIL, "GRattrinfo");
        VERIFY(attr_index, 0, "GRattrinfo");
        VERIFY(ntype, DFNT_FLOAT32, "GRattrinfo");
        VERIFY(n_values, RI_ATT_N_VALUES, "GRattrinfo");
        VERIFY_CHAR(attr_name, FILL_ATTR, "GRattrinfo");

        /* Allocate a buffer to hold the attribute data. */
        read_fill_vals = malloc(n_values * sizeof(float32));
        if (read_fill_vals == NULL) {
            fprintf(stderr, "Unable to allocate space for attribute data.\n");
            exit(1);
        }

        /* Piggy-back a test for Hgetntinfo */
        ret = Hgetntinfo(ntype, &nt_info);
        CHECK(ret, FAIL, "Hgetntinfo");
        VERIFY_CHAR(nt_info.type_name, "float32", "Hgetntinfo");

        /* Read and verify the attribute's data */
        ret = GRgetattr(riid, attr_index, (void *)read_fill_vals);
        CHECK(ret, FAIL, "GRgetattr");

        if (memcmp(fill_pixel, read_fill_vals, RI_ATT_N_VALUES) != 0) {
            MESSAGE(3, printf("Error reading values of attribute FILL_ATTR\n"););
            num_errs++;
        } /* end if */

        free(read_fill_vals);

        /* Close the empty image */
        ret = GRendaccess(riid);
        CHECK(ret, FAIL, "GRendaccess");
    }

    /* Shut down the GR interface */
    ret = GRend(grid);
    CHECK(ret, FAIL, "GRend");

    /* Close the file */
    ret = Hclose(fid);
    CHECK(ret, FAIL, "Hclose");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
} /* end test_mgr_fillvalues() */

/********************************************************************
   Name: test_mgr_userattr()

   Description:
        This test routine creates user-define attributes to the file
        and to an RI with GRsetattr then reads the values back with
        GRgetattr and verifies them.

   Return value:
        The number of errors occurred in this routine.

*********************************************************************/
static int
test_mgr_userattr()
{
    int32 grid, riid, fid, ri_index, f_att_index, /* index of file attributes */
        ri_att_index,                             /* index of raster image attributes */
        n_values,                                 /* number of values in an attribute */
        n_rimages,                                /* number of raster images in the file */
        n_file_attrs;                             /* number of file attributes */
    char         attr_name[H4_MAX_GR_NAME];       /* buffer to hold the attribute name */
    char         ri_name[H4_MAX_GR_NAME];         /* buffer to hold the image name */
    int32        ncomp;                           /* number of components */
    int32        ntype;                           /* number type of the components */
    int32        il;                              /* interlace of the image data */
    int32        dims[2];                         /* dimension sizes of the image */
    int32        n_attrs;                         /* number of attributes with each image */
    int16        ri_attr_2[RI_ATT2_N_VALUES] = {1, 2, 3, 4, 5, 6};
    void        *data_buf; /* buffer to hold the attribute values */
    hdf_ntinfo_t nt_info;  /* struct containing name and byte order of a num type */
    intn         status;   /* status for functions returning an intn */

    MESSAGE(8, printf("Reading user-defined attribute\n"););

    fid = Hopen(TESTFILE, DFACC_RDWR, 0);
    CHECK(fid, FAIL, "Hopen");

    /* Initialize the GR interface */
    grid = GRstart(fid);
    CHECK(grid, FAIL, "GRstart");

    /* Set two file attributes. */
    status = GRsetattr(grid, F_ATT1_NAME, DFNT_CHAR8, F_ATT1_N_VALUES, F_ATT1_VAL);
    CHECK(status, FAIL, "GRsetattr");

    status = GRsetattr(grid, F_ATT2_NAME, DFNT_UINT8, F_ATT2_N_VALUES, (void *)file_attr_2);
    CHECK(status, FAIL, "GRsetattr");

    /* Obtain the index of the image named IMAGE1_NAME. */
    ri_index = GRnametoindex(grid, IMAGE1_NAME);
    CHECK(ri_index, FAIL, "GRnametoindex");

    /* Obtain the identifier of this image. */
    riid = GRselect(grid, ri_index);
    CHECK(riid, FAIL, "GRselect");

    /* Set two attributes to the image. */
    status = GRsetattr(riid, RI_ATT1_NAME, DFNT_CHAR8, RI_ATT1_N_VALUES, RI_ATT1_VAL);
    CHECK(status, FAIL, "GRsetattr");

    status = GRsetattr(riid, RI_ATT2_NAME, DFNT_INT16, RI_ATT2_N_VALUES, (void *)ri_attr_2);
    CHECK(status, FAIL, "GRsetattr");

    /* Terminate accesses, and close the HDF file. */
    status = GRendaccess(riid);
    CHECK(status, FAIL, "GRendaccess");
    status = GRend(grid);
    CHECK(status, FAIL, "GRend");
    status = Hclose(fid);
    CHECK(status, FAIL, "Hclose");

    /* Reopen the file again to read the attributes and verify their values.*/
    fid = Hopen(TESTFILE, DFACC_RDWR, 0);
    CHECK(fid, FAIL, "Hopen");

    /* Initialize the GR interface */
    grid = GRstart(fid);
    CHECK(grid, FAIL, "GRstart");

    /* Verify the number of file attributes */
    status = GRfileinfo(grid, &n_rimages, &n_file_attrs);
    CHECK(status, FAIL, "GRfileinfo");

    /* Read each file attribute and verify its values */
    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(grid, f_att_index, attr_name, &ntype, &n_values);
            CHECK(status, FAIL, "GRattrinfo");

            /* Allocate a buffer to hold the file attribute data.  Knowledge
             * about the data type is assumed to be available from the previous
             * part of the test where the correspondent GRsetattr was called. */
            switch (ntype) {
                case DFNT_CHAR8:
                    data_buf = malloc(n_values * sizeof(char8));
                    if (data_buf == NULL) {
                        fprintf(stderr,
                                "test_mgr_userattr: Failed to allocate memory for data buffer for char8.\n");
                        exit(1);
                    }
                    break;
                case DFNT_UINT8:
                    data_buf = malloc(n_values * sizeof(uint8));
                    if (data_buf == NULL) {
                        fprintf(stderr,
                                "test_mgr_userattr: Failed to allocate memory for data buffer for uint8.\n");
                        exit(1);
                    }
                    break;
                default: {
                    fprintf(stderr,
                            "test_mgr_userattr: Unable to determine data type to allocate data buffer.\n");
                    exit(1);
                }
            } /* switch */

            /* Read and verify the attribute values. */
            status = GRgetattr(grid, f_att_index, (void *)data_buf);
            CHECK(status, FAIL, "GRgetattr");

            switch (ntype) {
                case DFNT_CHAR8:
                    if (memcmp(data_buf, F_ATT1_VAL, n_values) != 0) {
                        MESSAGE(3, printf("Error reading values of attribute %s\n", attr_name););
                        num_errs++;
                    } /* end if */

                    /* Piggy-back a test for Hgetntinfo */
                    status = Hgetntinfo(ntype, &nt_info);
                    CHECK(status, FAIL, "Hgetntinfo");
                    VERIFY_CHAR(nt_info.type_name, "char8", "Hgetntinfo");
                    break;
                case DFNT_UINT8:
                    if (memcmp(data_buf, file_attr_2, n_values) != 0) {
                        MESSAGE(3, printf("Error reading values of attribute %s\n", attr_name););
                        num_errs++;
                    } /* end if */

                    /* Piggy-back a test for Hgetntinfo */
                    status = Hgetntinfo(ntype, &nt_info);
                    CHECK(status, FAIL, "Hgetntinfo");
                    VERIFY_CHAR(nt_info.type_name, "uint8", "Hgetntinfo");
                    break;
                default: {
                    fprintf(stderr, "test_mgr_userattr: Unable to determine data type to verify data.\n");
                    exit(1);
                }
            } /* switch */

            /* Free the space allocated for the data buffer. */
            free(data_buf);

        } /* for */
    }     /* if */

    /* Obtain the index of the image named IMAGE1_NAME. */
    ri_index = GRnametoindex(grid, IMAGE1_NAME);
    CHECK(ri_index, FAIL, "GRnametoindex");

    /* Obtain the identifier of this image. */
    riid = GRselect(grid, ri_index);
    CHECK(riid, FAIL, "GRselect");

    /* Get the image information */
    status = GRgetiminfo(riid, ri_name, &ncomp, &ntype, &il, dims, &n_attrs);
    CHECK(status, FAIL, "GRgetiminfo");

    /* Verify each attribute's values */
    if (status != FAIL && n_attrs > 0) {
        for (ri_att_index = 0; ri_att_index < n_attrs; ri_att_index++) {
            /* Get information about the current attribute. */
            status = GRattrinfo(riid, ri_att_index, attr_name, &ntype, &n_values);
            CHECK(status, FAIL, "GRattrinfo");

            /* Allocate a buffer to hold the attribute data.  Knowledge about
             * the data type is assumed to be available from the previous
             * part of the test where the correspondent GRsetattr was called. */
            switch (ntype) {
                case DFNT_FLOAT32:
                    data_buf = malloc(n_values * sizeof(float32));
                    if (data_buf == NULL) {
                        fprintf(
                            stderr,
                            "test_mgr_userattr: Failed to allocate memory for data buffer for float32.\n");
                        exit(1);
                    }
                    break;
                case DFNT_CHAR8:
                    data_buf = malloc(n_values * sizeof(char8));
                    if (data_buf == NULL) {
                        fprintf(stderr,
                                "test_mgr_userattr: Failed to allocate memory for data buffer for char8.\n");
                        exit(1);
                    }
                    break;
                case DFNT_INT16:
                    data_buf = malloc(n_values * sizeof(int16));
                    if (data_buf == NULL) {
                        fprintf(stderr,
                                "test_mgr_userattr: Failed to allocate memory for data buffer for int16.\n");
                        exit(1);
                    }
                    break;
                default: {
                    fprintf(stderr,
                            "test_mgr_userattr: Unable to determine data type to allocate data buffer.\n");
                    exit(1);
                }
            } /* switch */

            /* Read and verify the attribute values. */
            status = GRgetattr(riid, ri_att_index, (void *)data_buf);
            CHECK(status, FAIL, "GRgetattr");

            /* Verify values from the attribute.
             * Knowledge about the data type is assumed to be available from
             * the previous part of the test. */
            switch (ntype) {
                case DFNT_FLOAT32:
                    if (memcmp(fill_pixel, data_buf, RI_ATT_N_VALUES) != 0) {
                        MESSAGE(3, printf("Error reading values of attribute FILL_ATTR\n"););
                        num_errs++;
                    } /* end if */
                    break;
                case DFNT_CHAR8:
                    if (memcmp(RI_ATT1_VAL, data_buf, RI_ATT1_N_VALUES) != 0) {
                        MESSAGE(3, printf("Error reading values of attribute FILL_ATTR\n"););
                        num_errs++;
                    } /* end if */
                    break;
                case DFNT_INT16:
                    if (memcmp(ri_attr_2, data_buf, RI_ATT2_N_VALUES) != 0) {
                        MESSAGE(3, printf("Error reading values of attribute FILL_ATTR\n"););
                        num_errs++;
                    } /* end if */
                    break;
                default: {
                    fprintf(stderr,
                            "test_mgr_userattr: Unable to determine data type to read attribute data.\n");
                    exit(1);
                }
            } /* switch */

            /* Free the space allocated for the data buffer. */
            free(data_buf);

        } /* for */
    }     /* if */
    /* Terminate accesses, and close the HDF file. */
    status = GRendaccess(riid);
    CHECK(status, FAIL, "GRendaccess");
    status = GRend(grid);
    CHECK(status, FAIL, "GRend");
    status = Hclose(fid);
    CHECK(status, FAIL, "Hclose");

    /* Return the number of errors that's been kept track of so far */
    return num_errs;
} /* test_mgr_userattr */

/****************************************************************
**
**  test_mgr_attr(): Main multi-file raster attribute test routine
**
**  VII. Attribute Functions
**      A. GRattrinfo
**      B. Read/Write Attributes
**          1. GRsetattr
**          2. GRgetattr
**      C. GRfindattr
**
**  test_mgr_attr is invoked by test_mgr in mgr.c.
**
****************************************************************/
/* Test driver for testing attributes in multi-file GR interface */
extern void
test_mgr_attr()
{
    /* Output message about test being performed */
    MESSAGE(5, printf("Testing Multi-file Raster Attribute routines\n"););

    /* Test attribute functions with fill-values attributes */
    num_errs = num_errs + test_mgr_fillvalues();

    /* Test attribute functions with user-defined attributes */
    num_errs = num_errs + test_mgr_userattr();

    if (num_errs != 0) {
        H4_FAILED();
    }
} /* test_mgr_attr() */