File: an.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 (301 lines) | stat: -rw-r--r-- 10,414 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/***********************************************************
 *
 * Test program: Stores annotations in a file
 * Writes several SDSs and corresponding RISs to a file.
 * Writes labels and descriptions for all but the first three SDSs.
 * Writes labels and descriptions for all RISs.
 *
 *************************************************************/

#define TESTFILE "tdfan.hdf"

#include "tproto.h"

#define ISFIRST     (int)1
#define NOTFIRST    (int)0
#define MAXLEN_LAB  50
#define MAXLEN_DESC 1000
#define ROWS        10
#define COLS        10
#define REPS        32 /*2*/ /* number of data sets to write to file */

static void gen2Dfloat(int height, int width, float *data);

static void genimage(int height, int width, float *data, uint8 *image);

static void check_lab_desc(uint16 tag, uint16 ref, char *label, char *desc);

void
test_an(void)
{
    char   labsds[MAXLEN_LAB], labris[MAXLEN_LAB], descsds[MAXLEN_DESC], descris[MAXLEN_DESC];
    uint8  pal[768];
    uint8 *image, *newimage;
    uint16 refnum;
    int32  ret;
    intn   rank;
    int    j;
    int32  dimsizes[2];
    float *data;

    /* set up object labels and descriptions */

    strcpy(labsds, "Object label #1: sds");
    strcpy(labris, "Object label #2: image");
    strcpy(descsds, "Object Descr #1: 1  2  3  4  5  6  7  8  9 10 11 12 \n");
    strcat(descsds, "             13 14 15 16 17 18 19 20 **END SDS DESCR**\n");
    strcpy(descris, "Object Descr #2: A B C D E F G H I J K L \n");
    strcat(descris, "                M N O **END IMAGE DESCR **\n");

    /***** generate float array and image *****/

    data     = (float *)malloc(ROWS * COLS * sizeof(float));
    image    = (uint8 *)malloc(ROWS * COLS * sizeof(char));
    newimage = (uint8 *)malloc(ROWS * COLS * sizeof(char));

    dimsizes[0] = ROWS;
    dimsizes[1] = COLS;

    gen2Dfloat(ROWS, COLS, data);
    genimage(ROWS, COLS, data, image);

    ret = DFSDsetdims(2, dimsizes);
    RESULT("DFSDsetdims");

    /********  Write labels and descriptions *********/
    MESSAGE(5, printf("***  Writing labels and descriptions with SDS and RIS ***\n"););
    for (j = 0; j < REPS; j++) {

        /* write out scientific data set */
        ret = DFSDadddata(TESTFILE, 2, dimsizes, (void *)data);
        RESULT("DFSDadddata");

        if ((j % 3) != 0) { /* write out annotations for 2 out of every 3 */
            refnum = DFSDlastref();
            ret    = DFANputlabel(TESTFILE, DFTAG_SDG, refnum, labsds);
            RESULT("DFANputlabel");
            ret = DFANputdesc(TESTFILE, DFTAG_SDG, refnum, descsds, (int32)strlen(descsds));
            RESULT("DFANputdesc");
        }

        ret = DFR8addimage(TESTFILE, (void *)image, COLS, ROWS, 0);
        RESULT("DFR8addimage");
        refnum = DFR8lastref();
        ret    = DFANputlabel(TESTFILE, DFTAG_RIG, refnum, labris);
        RESULT("DFANputlabel");
        ret = DFANputdesc(TESTFILE, DFTAG_RIG, refnum, descris, (int32)strlen(descris));
        RESULT("DFANputdesc");
    }

    /********  Read labels and descriptions *********/

    MESSAGE(5, printf("*** Reading labels and descriptions for SDS and RIS ***\n"););

    for (j = 0; j < REPS; j++) {

        ret = DFSDgetdims(TESTFILE, &rank, dimsizes, 3);
        RESULT("DFSDgetdims");
        refnum = DFSDlastref();

        if ((j % 3) != 0) /* read in annotations for 2 out of every 3 */
            check_lab_desc(DFTAG_SDG, refnum, labsds, descsds);

        ret = DFR8getimage(TESTFILE, newimage, (int32)COLS, (int32)ROWS, pal);
        RESULT("DFR8getimage");
        refnum = DFR8lastref();
        check_lab_desc(DFTAG_RIG, refnum, labris, descris);
    }

    free(data);
    free(image);
    free(newimage);
}

/****************************************************************
**
**  gen2Dfloat:  generate 2-D data array
**
****************************************************************/
static void
gen2Dfloat(int height, int width, float *data)
{
    int    i, j;
    float *pdata;

    /* store one value per row, increasing by one for each row */
    pdata = data;
    for (i = 0; i < height; i++)
        for (j = 0; j < width; j++)
            *pdata++ = (float)(i + 1);
}

/****************************************************************
**
**  genimage:  generate image from 2-D float array
**
****************************************************************/
static void
genimage(int height, int width, float *data, uint8 *image)
{
    int    i, limit;
    float *pdata, max, min, multiplier;

    limit = height * width;
    pdata = data;
    max = min = *pdata;
    for (i = 0; i < limit; i++, pdata++) {
        max = (max > *pdata) ? max : *pdata;
        min = (min < *pdata) ? min : *pdata;
    }
    /* store one value per row, increasing by one for each row */
    pdata      = data;
    multiplier = (float32)255.0 / (max - min);
    for (i = 0; i < limit; i++)
        *image++ = (uint8)(((*pdata++) - min) * multiplier);
}

/****************************************************************
**
**  check_lab_desc:  read and compare label and description
**                   with expected ones
**
****************************************************************/
static void
check_lab_desc(uint16 tag, uint16 ref, char *label, char *desc)
{
    int32 inlablen, indesclen, ret;
    char  inlabel[MAXLEN_LAB], *indesc;

    inlablen = ret = DFANgetlablen(TESTFILE, tag, ref);
    RESULT("DFANgetlablen");
    if (inlablen != (int32)strlen(label)) {
        printf("\t>>>BAD LABEL LENGTH.\n\t       IS: %d\n\tSHOULD BE: %d<<<\n", (int)inlablen,
               (int)strlen(label));
        num_errs++;
    }
    ret = DFANgetlabel(TESTFILE, tag, ref, inlabel, MAXLEN_LAB);
    RESULT("DFANgetlabel");
    if (strcmp(inlabel, label) != 0) {
        printf("\t>>>BAD LABEL. \n\t       IS: %s; \n\tSHOULD BE: %s<<<\n", inlabel, label);
        num_errs++;
    }

    indesclen = ret = DFANgetdesclen(TESTFILE, tag, ref);
    RESULT("DFANgetdesclen");
    if (indesclen != (int32)strlen(desc)) {
        printf("\t>>>BAD DESCRIPTION LENGTH. \n\t       IS: %d", (int)indesclen);
        printf("\n\tSHOULD BE: %d<<<\n", (int)strlen(desc));
        num_errs++;
    }
    else {
        indesc = (char *)malloc(indesclen + 1);
        ret    = DFANgetdesc(TESTFILE, tag, ref, indesc, MAXLEN_DESC);
        RESULT("DFANgetdesc");
        indesc[indesclen] = '\0';
        if (strcmp(indesc, desc) != 0) {
            printf("\t>>>BAD DESCRIPTION.\n\t      IS: %s", indesc);
            printf("\n\tSHOULD BE: %s<<<\n", desc);
            num_errs++;
        }
        free(indesc);
    }
}

void
test_an_2(void)
{
    char   labsds[MAXLEN_LAB], labris[MAXLEN_LAB], descsds[MAXLEN_DESC], descris[MAXLEN_DESC];
    uint8  pal[768];
    uint8 *image, *newimage;
    uint16 refnum;
    int32  ret;
    intn   rank;
    int    j;
    int32  dimsizes[2];
    float *data;

    /* set up object labels and descriptions */

    strcpy(labsds, "Object label #1: sds");
    strcpy(labris, "Object label #2: image");
    strcpy(descsds, "Object Descr #1: 1  2  3  4  5  6  7  8  9 10 11 12 \n");
    strcat(descsds, "             13 14 15 16 17 18 19 20 **END SDS DESCR**\n");
    strcpy(descris, "Object Descr #2: A B C D E F G H I J K L \n");
    strcat(descris, "                M N O **END IMAGE DESCR **\n");

    /***** generate float array and image *****/

    data     = (float *)malloc(ROWS * COLS * sizeof(float));
    image    = (uint8 *)malloc(ROWS * COLS * sizeof(char));
    newimage = (uint8 *)malloc(ROWS * COLS * sizeof(char));

    dimsizes[0] = ROWS;
    dimsizes[1] = COLS;

    gen2Dfloat(ROWS, COLS, data);
    genimage(ROWS, COLS, data, image);

    ret = DFSDsetdims(2, dimsizes);
    RESULT("DFSDsetdims");

    /********  Write labels and descriptions *********/
    MESSAGE(5, printf("***  Writing labels and descriptions with SDS and RIS ***\n"););
    for (j = 0; j < REPS; j++) {

        /* write out scientific data set */
        ret = DFSDadddata(TESTFILE, 2, dimsizes, (void *)data);
        RESULT("DFSDadddata");

        if ((j % 3) != 0) { /* write out annotations for 2 out of every 3 */
            refnum = DFSDlastref();
            ret    = DFANputlabel(TESTFILE, DFTAG_SDG, refnum, labsds);
            RESULT("DFANputlabel");
            ret = DFANputdesc(TESTFILE, DFTAG_SDG, refnum, descsds, (int32)strlen(descsds));
            RESULT("DFANputdesc");
        }

        ret = DFR8addimage(TESTFILE, (void *)image, COLS, ROWS, 0);
        RESULT("DFR8addimage");
        refnum = DFR8lastref();
        ret    = DFANputlabel(TESTFILE, DFTAG_RIG, refnum, labris);
        RESULT("DFANputlabel");
        ret = DFANputdesc(TESTFILE, DFTAG_RIG, refnum, descris, (int32)strlen(descris));
        RESULT("DFANputdesc");
    }

    /********  Read labels and descriptions *********/

    MESSAGE(5, printf("*** Reading labels and descriptions for SDS and RIS ***\n"););

    for (j = 0; j < REPS; j++) {

        ret = DFSDgetdims(TESTFILE, &rank, dimsizes, 3);
        RESULT("DFSDgetdims");
        refnum = DFSDlastref();

        if ((j % 3) != 0) /* read in annotations for 2 out of every 3 */
            check_lab_desc(DFTAG_SDG, refnum, labsds, descsds);

        ret = DFR8getimage(TESTFILE, newimage, (int32)COLS, (int32)ROWS, pal);
        RESULT("DFR8getimage");
        refnum = DFR8lastref();
        check_lab_desc(DFTAG_RIG, refnum, labris, descris);
    }

    free(data);
    free(image);
    free(newimage);
}