summaryrefslogtreecommitdiffstats
path: root/tools/h52jpeg/h52jpeg.c
blob: 56a7498be1a031e5eac93268212cc8681fa2281b (plain)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdlib.h>
#include <memory.h>



#if 0
#include "jpeglib.h"
#include "jerror.h"
#endif

#include "H5private.h"
#include "h5tools.h"
#include "h5tools_utils.h"
#include "h5trav.h"
#include "H5IMpublic.h"



const char *progname = "h52jpeg";
int d_status = EXIT_SUCCESS;

/* command-line options: The user can specify short or long-named parameters */
static const char *s_opts = "hVvi:t:";
static struct long_options l_opts[] = {
    { "help", no_arg, 'h' },
    { "version", no_arg, 'V' },
    { "verbose", no_arg, 'v' },
    { "image", require_arg, 'i' },
    { "type", require_arg, 't' },
    { NULL, 0, '\0' }
};


/* a structure that contains h52jpeg options */
typedef struct 
{
 const char  *file_name;
 const char  *template_name;
 const char  *image_name;
 int         image_type;
 int         verbose;


} h52jpeg_opt_t;


/* prototypes */
static void usage(const char *prog);
static int h52jpeg(h52jpeg_opt_t options);


/*-------------------------------------------------------------------------
 * Function: main
 *
 * Purpose: h52jpeg main program
 *
 * Programmer: Pedro Vicente, pvn@hdfgroup.org
 *
 * Date: May 30, 2008
 *
 *-------------------------------------------------------------------------
 */
int main(int argc, char **argv)
{
    h52jpeg_opt_t options;
    const char    *image_type = NULL;
    int           opt;

    /* initialze options to 0 */
    memset(&options,0,sizeof(h52jpeg_opt_t));
    
    /* parse command line options */
    while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF) 
    {
        switch ((char)opt) 
        {
        case 'h':
            usage(progname);
            exit(EXIT_SUCCESS);
        case 'V':
            print_version(progname);
            exit(EXIT_SUCCESS);
        case 'v':
            options.verbose = 1;
            break;        
        case 'i':
            options.image_name = opt_arg;
            break;
        case 't':
            image_type = opt_arg;
            break;
            
        } /* switch */
        
        
    } /* while */
    
    /* check for file names to be processed */
    if ( argv[ opt_ind ] != NULL && argv[ opt_ind + 1 ] != NULL ) 
    {
        options.file_name = argv[ opt_ind ];
        options.template_name = argv[ opt_ind + 1 ];    
    }
    
    else
    {
        usage(progname);
        exit(EXIT_FAILURE);
    }

    if ( h52jpeg(options) < 0 )
        return 1;
    
    
    return 0;
}



/*-------------------------------------------------------------------------
 * Function: usage
 *
 * Purpose: print usage
 *
 * Return: void
 *
 *-------------------------------------------------------------------------
 */
static void usage(const char *prog)
{
    printf("usage: %s [OPTIONS] file template\n", prog);
    printf("  file                     HDF5 file name\n");
    printf("  template                 Name template for jpeg images\n");
    printf("  OPTIONS\n");
    printf("   -h, --help              Print a usage message and exit\n");
    printf("   -v, --verbose           Verbose mode, print object information\n");
    printf("   -V, --version           Print version number and exit\n");
    printf("   -i, --image             Image name (full path in HDF5 file)\n");
    printf("   -t T, --type=T          Type of image (8bit or 24bit)\n");
    
    printf("\n");
    
    printf("  T - is a string, either <8bit> or <24bit>\n");
    
}

/*-------------------------------------------------------------------------
 * Function: h52jpeg
 *
 * Parameters: options at command line
 *
 * Purpose: traverse the HDF5 file, save HDF5 images to jpeg files, translate
 *  2D datasets of classes H5T_INTEGER and H5T_FLOAT to image data and save them
 *  to jpeg files
 *
 * Return: 0, all is fine, -1 not all is fine
 *
 *-------------------------------------------------------------------------
 */
static int h52jpeg(h52jpeg_opt_t opt)
{
    hid_t          fid;
    trav_table_t   *travt = NULL;
    hsize_t        width;
    hsize_t        height;
    hsize_t        planes;
    char           interlace[20];
    hssize_t       npals;
    void           *buf=NULL;
    H5T_class_t    tclass;
    hid_t          sid;
    hid_t          did;
    hid_t          tid;
    int            rank;
    hsize_t        dims[H5S_MAX_RANK];
    hsize_t        maxdim[H5S_MAX_RANK];
    size_t         size;
    hsize_t        nelmts;
    char*          name;
    size_t         i;
    int            j;
    int            done;
    char           jpeg_name[1024];

    /* open the HDF5 file */
    if (( fid = h5tools_fopen(opt.file_name, H5F_ACC_RDONLY, H5P_DEFAULT, NULL, NULL, (size_t)0)) < 0) 
    {
        error_msg(progname, "cannot open file <%s>\n", opt.file_name );
        return -1;
    }
    
    /* initialize traversal table */
    trav_table_init(&travt);
    
    /* get the list of objects in the file */
    if ( h5trav_gettable(fid, travt) < 0 )
        goto out;

    /* search for images/datasets in file */
    for ( i = 0; i < travt->nobjs; i++) 
    {
        
        switch ( travt->objs[i].type ) 
        {
        default:
            goto out;
            
        case H5TRAV_TYPE_GROUP:
        case H5TRAV_TYPE_NAMED_DATATYPE:
        case H5TRAV_TYPE_LINK:
        case H5TRAV_TYPE_UDLINK:
            
            break;
            
        case H5TRAV_TYPE_DATASET:

            name = travt->objs[i].name;
            strcpy( jpeg_name, opt.template_name );
            strcat( jpeg_name, name );

            done = 0;

            if ( opt.verbose)
                printf("%s ...", name );
            
            /*-------------------------------------------------------------------------
            * HDF5 Image
            *-------------------------------------------------------------------------
            */

            if ( H5IMis_image( fid, name ) )
            {
                
                if ( H5IMget_image_info( fid, name, &width, &height, &planes, interlace, &npals ) < 0 )
                    goto out;
                
                if (NULL == (buf = HDmalloc( (size_t)width * (size_t)height * (size_t)planes ))) 
                    goto out;
                
                if ( H5IMread_image( fid, name, buf ) < 0 )
                    goto out;
                
                free( buf );
                buf = NULL;
                
                
                
            }

            /*-------------------------------------------------------------------------
            * regular dataset
            *-------------------------------------------------------------------------
            */

            else
            {

                if (( did = H5Dopen2( fid, name, H5P_DEFAULT )) < 0) 
                    goto out;
                if (( sid = H5Dget_space( did )) < 0 )
                    goto out;
                if (( rank = H5Sget_simple_extent_ndims(sid)) < 0 )
                    goto out;
                if (( tid = H5Dget_type( did )) < 0 )
                    goto out;
                if (( tclass = H5Tget_class(tid)) < 0)
                    goto out;

                if ( ( H5T_FLOAT == tclass || H5T_INTEGER == tclass) &&
                    ( rank == 2 ) )
                {
                    
                    if ( H5Sget_simple_extent_dims( sid, dims, maxdim ) < 0 )
                        goto out;
                    
                    size =  H5Tget_size( tid );
                    
                    nelmts = 1;
                    for ( j = 0; j < rank; j++)
                    {
                        nelmts *= dims[j];
                    }
                    
                    if ( NULL == (buf = HDmalloc( (size_t)nelmts * size ))) 
                        goto out;                  
                    if ( H5Dread(did,tid,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf) < 0 )
                        goto out;
                    
                    
                    free( buf );
                    buf = NULL;
                    
                }
                
                
                
                H5Sclose(sid);
                H5Tclose(tid);
                H5Dclose(did);

              
                
                
            } /* else */
            
            
            if ( opt.verbose)
            {                
                if ( done )
                {
                    printf("saved to %s\n", jpeg_name );
                }
                else
                {
                    printf("\n");
                }
                
            }
            
            
            
            break; /* H5TRAV_TYPE_DATASET */
            
            
            
        } /* switch */
        
        
        
    } /* i */

    
    
    /* free table */
    trav_table_free(travt);
    
    
    /* close */
    if ( H5Fclose(fid) < 0 )
        return -1;
    
    return 0;
    
out:
    H5E_BEGIN_TRY 
    {

        H5Sclose(sid);
        H5Tclose(tid);
        H5Dclose(did);
        H5Fclose(fid);
        
    } H5E_END_TRY;

    if ( buf != NULL )
        free( buf );
    
    
    return -1;
}