File: hrepack_utils.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (502 lines) | stat: -rw-r--r-- 18,422 bytes parent folder | download | duplicates (3)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <string.h>
#include <stdlib.h>

#include "hdf.h"
#include "mfhdf.h"
#include "hrepack_utils.h"
#include "hrepack_opttable.h"

/*-------------------------------------------------------------------------
 * Function: is_reserved
 *
 * Purpose: check for reserved Vgroup/Vdata class/names
 *
 * Return: 1 if reserved, 0 if not
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: August 22, 2003
 *
 *-------------------------------------------------------------------------
 */

int
is_reserved(char *vgroup_class)
{
    int ret = 0;

    /* ignore reserved HDF groups/vdatas */
    if (vgroup_class != NULL) {
        if ((strcmp(vgroup_class, _HDF_ATTRIBUTE) == 0) || (strcmp(vgroup_class, _HDF_VARIABLE) == 0) ||
            (strcmp(vgroup_class, _HDF_DIMENSION) == 0) || (strcmp(vgroup_class, _HDF_UDIMENSION) == 0) ||
            (strcmp(vgroup_class, DIM_VALS) == 0) || (strcmp(vgroup_class, DIM_VALS01) == 0) ||
            (strcmp(vgroup_class, _HDF_CDF) == 0) || (strcmp(vgroup_class, GR_NAME) == 0) ||
            (strcmp(vgroup_class, RI_NAME) == 0) || (strcmp(vgroup_class, RIGATTRNAME) == 0) ||
            (strcmp(vgroup_class, RIGATTRCLASS) == 0)) {
            ret = 1;
        }

        /* class and name(partial) for chunk table i.e. Vdata */
        if ((strncmp(vgroup_class, "_HDF_CHK_TBL_", 13) == 0)) {
            ret = 1;
        }
    }

    return ret;
}

/*-------------------------------------------------------------------------
 * Function: get_path
 *
 * Purpose: return absolute path for an object
 *
 * Return: path
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: July 11, 2003
 *
 *-------------------------------------------------------------------------
 */

char *
get_path(char *path_name, char *obj_name)
{
    char *path = NULL;
    /* initialize path */
    if (path_name != NULL) {
        path = (char *)malloc(strlen(path_name) + strlen(obj_name) + 2);
        strcpy(path, path_name);
        strcat(path, "/");
        strcat(path, obj_name);
    }
    else {
        path = (char *)malloc(strlen(obj_name) + 1);
        strcpy(path, obj_name);
    }
    return path;
}

/*-------------------------------------------------------------------------
 * Function: options_get_info
 *
 * Purpose: get COMP and CHUNK information from options
 *
 * Return: 0 if no information for this PATH, 1 otherwise
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: July 28, 2003
 *
 *-------------------------------------------------------------------------
 */

int
options_get_info(options_t     *options,     /* global options */
                 int32         *chunk_flags, /* chunk flags OUT */
                 HDF_CHUNK_DEF *chunk_def,   /* chunk definition OUT */
                 int           *info,        /* compression information OUT */
                 int           *szip_mode,   /* compression information OUT */
                 comp_coder_t  *comp_type,   /* compression type OUT  */
                 int            rank,        /* rank of object IN */
                 char          *path,        /* path of object IN */
                 int            ncomps,      /* number of GR image planes (for SZIP), IN */
                 int32         *dimsizes,    /* dimensions (for SZIP), IN */
                 int32          dtype        /* numeric type (for SZIP), IN */
)
{
    (void)dtype;
    (void)dimsizes;
    (void)ncomps;

    pack_info_t *obj = NULL; /* check if we have information for this object */
    int          i;
    comp_info    c_info; /* for SZIP default values */

    /*-------------------------------------------------------------------------
     * CASE 1: chunk==ALL comp==SELECTED
     *-------------------------------------------------------------------------
     */

    if (options->all_chunk == 1 && options->all_comp == 0) {
        /* NONE option */
        if (options->chunk_g.rank == -2) {
            chunk_flags = HDF_NONE;
        }

        /*check if the input rank is correct (warn this one cannot be chunked) */
        else if (options->chunk_g.rank != rank) {
            if (options->verbose)
                printf("Warning: chunk rank does not apply to <%s>\n", path);
        }
        else {
            *chunk_flags = HDF_CHUNK;
            for (i = 0; i < rank; i++)
                chunk_def->chunk_lengths[i] = options->chunk_g.chunk_lengths[i];
        }

        obj = options_get_object(path, options->op_tbl);

        if (obj != NULL) {

            /* 0 is the NONE option */
            *comp_type = obj->comp.type;
            *info      = obj->comp.info;
            *szip_mode = obj->comp.szip_mode;

            /* chunk and compress */
            if (*chunk_flags == HDF_CHUNK && *comp_type > 0) {
                /* assign the object CHUNK information   */
                *chunk_flags              = HDF_CHUNK | HDF_COMP;
                chunk_def->comp.comp_type = obj->comp.type;
                switch (obj->comp.type) {
                    case COMP_CODE_NONE:
                        break;

                    case COMP_CODE_SZIP:
                        if (set_szip(obj->comp.info, obj->comp.szip_mode, &c_info) == FAIL) {
                            return FAIL;
                        }
                        chunk_def->comp.cinfo = c_info;

                        break;
                    case COMP_CODE_RLE:
                        break;
                    case COMP_CODE_SKPHUFF:
                        chunk_def->comp.cinfo.skphuff.skp_size = obj->comp.info;
                        break;
                    case COMP_CODE_DEFLATE:
                        chunk_def->comp.cinfo.deflate.level = obj->comp.info;
                        break;
                    case COMP_CODE_JPEG:
                        chunk_def->comp.cinfo.jpeg.quality        = obj->comp.info;
                        chunk_def->comp.cinfo.jpeg.force_baseline = 1;
                        break;
                    default:
                        printf("Error: Unrecognized compression code in %d <%s>\n", obj->comp.type, path);
                        break;
                }; /*switch */
                for (i = 0; i < rank; i++) {
                    /* To use chunking with RLE, Skipping Huffman, and GZIP compression */
                    chunk_def->comp.chunk_lengths[i] = options->chunk_g.chunk_lengths[i];
                }
            } /* chunk_flags */
        }     /* obj */
    }

    /*-------------------------------------------------------------------------
     * CASE 2: chunk==SELECTED comp==SELECTED
     *-------------------------------------------------------------------------
     */
    else if (options->all_chunk == 0 && options->all_comp == 0) {
        obj = options_get_object(path, options->op_tbl);

        if (obj != NULL) {
            /* NONE option */
            if (obj->chunk.rank == -2) {
                *chunk_flags = HDF_NONE;
            }
            /* check if we have CHUNK information inserted for this one  */
            else if (obj->chunk.rank > 0) {
                /*check if the input rank is correct (just here, better later than never) */
                if (obj->chunk.rank != rank) {
                    printf("Error: chunk rank does not match for <%s>\n", path);
                    return FAIL;
                }
                *chunk_flags = HDF_CHUNK;
                for (i = 0; i < rank; i++)
                    chunk_def->chunk_lengths[i] = obj->chunk.chunk_lengths[i];
            }
            /* check if we have COMP information; 0 is the NONE option */
            if (obj->comp.type >= 0) {
                *comp_type = obj->comp.type;
                *info      = obj->comp.info;
                *szip_mode = obj->comp.szip_mode;
                /* check if we have also CHUNK info  */
                if (obj->chunk.rank > 0) {
                    *chunk_flags              = HDF_CHUNK | HDF_COMP;
                    chunk_def->comp.comp_type = *comp_type;
                    switch (*comp_type) {
                        case COMP_CODE_NONE:
                            break;

                        case COMP_CODE_SZIP:
                            if (set_szip(obj->comp.info, obj->comp.szip_mode, &c_info) == FAIL) {
                                return FAIL;
                            }
                            chunk_def->comp.cinfo = c_info;

                            break;
                        case COMP_CODE_RLE:
                            break;
                        case COMP_CODE_SKPHUFF:
                            chunk_def->comp.cinfo.skphuff.skp_size = obj->comp.info;
                            break;
                        case COMP_CODE_DEFLATE:
                            chunk_def->comp.cinfo.deflate.level = obj->comp.info;
                            break;
                        case COMP_CODE_JPEG:
                            chunk_def->comp.cinfo.jpeg.quality        = obj->comp.info;
                            chunk_def->comp.cinfo.jpeg.force_baseline = 1;
                            break;
                        default:
                            printf("Error: Unrecognized compression code in %d <%s>\n", *comp_type, path);
                            return FAIL;
                            break;
                    };
                }
            } /* comp.type */
        }     /* obj */
    }         /* else if */

    /*-------------------------------------------------------------------------
     * CASE 3: chunk==SELECTED comp==ALL
     *-------------------------------------------------------------------------
     */
    else if (options->all_chunk == 0 && options->all_comp == 1) {
        obj = options_get_object(path, options->op_tbl);

        if (obj != NULL) {

            /* NONE option */
            if (obj->chunk.rank == -2) {
                *chunk_flags = HDF_NONE;
            }

            /* check if we have CHUNK information inserted for this one  */
            else if (obj->chunk.rank > 0) {
                /*check if the input rank is correct (just here, better later than never) */
                if (obj->chunk.rank != rank) {
                    printf("Error: chunk rank does not match for <%s>\n", path);
                    return FAIL;
                }
                *chunk_flags = HDF_CHUNK;
                for (i = 0; i < rank; i++)
                    chunk_def->chunk_lengths[i] = obj->chunk.chunk_lengths[i];
            }
        } /* obj */

        /* we must have COMP information */

        *comp_type = options->comp_g.type;
        *info      = options->comp_g.info;
        *szip_mode = options->comp_g.szip_mode;
        /* check if we have also CHUNK information  */
        if ((*chunk_flags == HDF_CHUNK) || (*chunk_flags == (HDF_CHUNK | HDF_COMP))) {
            *chunk_flags              = HDF_CHUNK | HDF_COMP;
            chunk_def->comp.comp_type = *comp_type;
            switch (*comp_type) {
                case COMP_CODE_NONE:
                    break;

                case COMP_CODE_SZIP:
                    if (set_szip(options->comp_g.info, options->comp_g.szip_mode, &c_info) == FAIL) {
                        return FAIL;
                    }
                    chunk_def->comp.cinfo = c_info;

                case COMP_CODE_RLE:
                    break;
                case COMP_CODE_SKPHUFF:
                    chunk_def->comp.cinfo.skphuff.skp_size = *info;
                    break;
                case COMP_CODE_DEFLATE:
                    chunk_def->comp.cinfo.deflate.level = *info;
                    break;
                case COMP_CODE_JPEG:
                    chunk_def->comp.cinfo.jpeg.quality = *info;
                    ;
                    chunk_def->comp.cinfo.jpeg.force_baseline = 1;
                    break;
                default:
                    printf("Error: Unrecognized compression code in %d <%s>\n", *comp_type, path);
                    return FAIL;
                    break;
            };
        }
    } /* else if */

    /*-------------------------------------------------------------------------
     * CASE 4: chunk==ALL comp==ALL
     *-------------------------------------------------------------------------
     */
    else if (options->all_chunk == 1 && options->all_comp == 1) {
        /* NONE option */
        if (options->chunk_g.rank == -2) {
            *chunk_flags = HDF_NONE;
        }

        /*check if this object rank is the same as input (warn this one cannot be chunked) */
        else if (options->chunk_g.rank != rank) {
            if (options->verbose)
                printf("Warning: chunk rank does not apply to <%s>\n", path);
        }
        else {
            *chunk_flags = HDF_CHUNK;
            for (i = 0; i < rank; i++)
                chunk_def->chunk_lengths[i] = options->chunk_g.chunk_lengths[i];
        }

        /* we must have COMP information */
        *comp_type = options->comp_g.type;
        *info      = options->comp_g.info;
        *szip_mode = options->comp_g.szip_mode;
        /* check if we can apply CHUNK */
        if (options->chunk_g.rank == rank) {
            *chunk_flags              = HDF_CHUNK | HDF_COMP;
            chunk_def->comp.comp_type = *comp_type;
            switch (*comp_type) {
                case COMP_CODE_NONE:
                    break;

                case COMP_CODE_SZIP:
                    if (set_szip(options->comp_g.info, options->comp_g.szip_mode, &c_info) == FAIL) {
                        return FAIL;
                    }
                    chunk_def->comp.cinfo = c_info;

                case COMP_CODE_RLE:
                    break;
                case COMP_CODE_SKPHUFF:
                    chunk_def->comp.cinfo.skphuff.skp_size = *info;
                    break;
                case COMP_CODE_DEFLATE:
                    chunk_def->comp.cinfo.deflate.level = *info;
                    break;
                case COMP_CODE_JPEG:
                    chunk_def->comp.cinfo.jpeg.quality = *info;
                    ;
                    chunk_def->comp.cinfo.jpeg.force_baseline = 1;
                    break;
                default:
                    printf("Error: Unrecognized compression code in %d <%s>\n", *comp_type, path);
                    return FAIL;
                    break;
            };
        }
    } /* else if */

    return (obj == NULL) ? 0 : 1;
}

/*-------------------------------------------------------------------------
 * Function: set_szip
 *
 * Purpose: utility to set SZIP parameters
 *
 * SZIP compresses data block by block, with a user-tunable block size.
 * This block size is passed in the parameter pixels_per_block and must be even,
 * with typical values being 8, 10, 16, and 32. The more pixel values vary,
 * the smaller this number should be. For optimal performance, the number of
 * pixels per scan line (i.e., the size of the fastest-changing dimension in the chunk)
 * should be an even multiple of the number of pixels per block.
 *
 * Return: 0 for OK, -1 otherwise
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: August 11, 2003
 *
 *-------------------------------------------------------------------------
 */

int
set_szip(int        pixels_per_block, /*in */
         int        compression_mode, /* in */
         comp_info *c_info /*out*/)
{
#ifdef H4_HAVE_LIBSZ

    int ppb = pixels_per_block;

    if (SZ_encoder_enabled() == 0) {
        printf("Warning: SZIP encoder is not enabled\n");
        return -1;
    }

    if ((compression_mode != NN_MODE) && (compression_mode != EC_MODE)) {
        printf("SZIP compression mode must be NN_MODE or EC_MODE");
        return -1;
    }

    /*
    pixels_per_block must be an even number, and <= pixels_per_scanline
    and <= SZ_MAX_PIXELS_PER_BLOCK
    */

    if (pixels_per_block & 1) {
        printf("Pixels per block must be even.\n");
        return -1;
    }
    if (ppb < 2 || ppb > 32) {
        printf("Pixels per block must be 2-32.\n");
        return -1;
    }
    c_info->szip.pixels_per_block = ppb;

    /* set according to input value */
    c_info->szip.options_mask = SZ_EC_OPTION_MASK;
    if (compression_mode == EC_MODE) {
        c_info->szip.options_mask = SZ_EC_OPTION_MASK;
    }
    else if (compression_mode == NN_MODE) {
        c_info->szip.options_mask = SZ_NN_OPTION_MASK;
    }
    c_info->szip.options_mask |= SZ_RAW_OPTION_MASK;

    return 0;
#else
    printf("Warning: SZIP compression is not available\n");
    return -1;
#endif
}

/*-------------------------------------------------------------------------
 * Function: cache
 *
 * Purpose: Checks chunk size
 *
 *-------------------------------------------------------------------------
 */

int
cache(HDF_CHUNK_DEF chunk_def, int32 eltsz, int32 rank, int32 *dimsize)
{
    int32 targetbytes;
    int32 chunkrow;
    int32 chunkcnt;
    int32 chunksizes[32];
    int   i;
    int32 cntr;

    for (i = 0; i < rank; i++) {
        chunkcnt    = 1;
        targetbytes = dimsize[i] * eltsz;
        chunkrow    = eltsz * chunk_def.chunk_lengths[i];
        cntr        = chunkrow;
        while (cntr < targetbytes) {
            cntr += chunkrow;
            chunkcnt++;
        }
        chunksizes[i] = chunkcnt;
    }
    chunkcnt = 1;
    for (i = 0; i < rank; i++) {
        chunkcnt *= chunksizes[i];
    }
    printf("total chunks is %d\n", chunkcnt);
    return 0;
}