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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* From jpeg documentation
*
* Include file for users of JPEG library.
* You will need to have included system headers that define at least
* the typedefs FILE and size_t before you can include jpeglib.h.
* (stdio.h is sufficient on ANSI-conforming systems.)
* You may also wish to include "jerror.h".
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "jpeglib.h"
#include "jerror.h"
#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:cp:";
static struct long_options l_opts[] = {
{ "help", no_arg, 'h' },
{ "version", no_arg, 'V' },
{ "verbose", no_arg, 'v' },
{ "image", require_arg, 'i' },
{ "convert", no_arg, 'c' },
{ "palette", require_arg, 'p' },
{ 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 convert_true;
int idx_palette;
int verbose;
} h52jpeg_opt_t;
/* prototypes */
static void usage(const char *prog);
static int h52jpeg(h52jpeg_opt_t opt);
static void make_jpeg_name( const char* template_name, const char* image_name, char* jpeg_name);
static int do_object(hid_t fid, h52jpeg_opt_t opt, const char* image_name);
static int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name, char* jpeg_name);
static void write_JPEG_file(char *filename, JSAMPLE *image_buffer, int image_height, int image_width, int planes);
static void convert_to_true( hsize_t width, hsize_t height, unsigned char* ibuf, unsigned char* pbuf, unsigned char* tbuf);
/*-------------------------------------------------------------------------
* Function: main
*
* Purpose: h52jpeg main program
*
* Programmer: Pedro Vicente, pvn@hdfgroup.org
*
* Date: May 30, 2008
*
*-------------------------------------------------------------------------
*/
int main(int argc, const char *argv[])
{
h52jpeg_opt_t opt;
int op;
/* initialze options to 0 */
memset(&opt,0,sizeof(h52jpeg_opt_t));
/* parse command line options */
while ((op = get_option(argc, argv, s_opts, l_opts)) != EOF)
{
switch ((char)op)
{
case 'h':
usage(progname);
exit(EXIT_SUCCESS);
case 'V':
print_version(progname);
exit(EXIT_SUCCESS);
case 'v':
opt.verbose = 1;
break;
case 'i':
opt.image_name = opt_arg;
break;
case 'c':
opt.convert_true = 1;
break;
case 'p':
opt.idx_palette = atoi(opt_arg);
break;
} /* switch */
} /* while */
/* check for file names to be processed */
if ( argv[ opt_ind ] != NULL && argv[ opt_ind + 1 ] != NULL )
{
opt.file_name = argv[ opt_ind ];
opt.template_name = argv[ opt_ind + 1 ];
}
else
{
usage(progname);
exit(EXIT_FAILURE);
}
if ( h52jpeg(opt) < 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 HDF5 version number and exit\n");
printf(" -i, --image Image name (full path in HDF5 file)\n");
printf(" -c, --convert Convert image from graycolor to truecolor\n");
printf(" -p P, --palette=P Use HDF5 palette index P in conversion -c\n");
printf("\n");
printf(" P - is an integer, the palette index in the HDF5 image. Default is 0\n");
}
/*-------------------------------------------------------------------------
* Function: h52jpeg
*
* Parameters: OPT, 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;
size_t i;
/* 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;
}
/*-------------------------------------------------------------------------
* object name was specified at command line
*-------------------------------------------------------------------------
*/
if ( opt.image_name )
{
/* read object, save jpeg image */
do_object(fid, opt, opt.image_name);
}
/*-------------------------------------------------------------------------
* object name was not specified; traverse the file
*-------------------------------------------------------------------------
*/
else
{
/* 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:
/* read object, save jpeg image */
do_object(fid, opt, travt->objs[i].name);
break; /* H5TRAV_TYPE_DATASET */
} /* switch */
} /* i */
/* free table */
trav_table_free(travt);
} /* opt.image_name */
/* close */
if ( H5Fclose(fid) < 0 )
return -1;
return 0;
out:
H5E_BEGIN_TRY
{
H5Fclose(fid);
} H5E_END_TRY;
return -1;
}
/*-------------------------------------------------------------------------
* Function: do_object
*
* Parameters: HDF5 file id, command line options, an object name
*
* Purpose: read HDF5 object, save jpeg image
*
* Return: 0, all is fine, -1 not all is fine
*
*-------------------------------------------------------------------------
*/
static
int do_object(hid_t fid, h52jpeg_opt_t opt, const char* object_name)
{
int done=0; /* return value from do_image */
char jpeg_name[1024];
/* build the jpeg file name */
make_jpeg_name( opt.template_name, object_name, jpeg_name);
if ( opt.verbose)
{
printf("%s ...", object_name );
}
/*-------------------------------------------------------------------------
* HDF5 Image
*-------------------------------------------------------------------------
*/
if ( H5IMis_image( fid, object_name ) )
{
/* read image, save jpeg image */
done = do_image(fid, opt, object_name, jpeg_name);
}
/*-------------------------------------------------------------------------
* HDF5 Image palette, ignore
*-------------------------------------------------------------------------
*/
else if ( H5IMis_palette( fid, object_name ) )
{
}
/*-------------------------------------------------------------------------
* regular dataset
*-------------------------------------------------------------------------
*/
else
{
} /* else */
if ( opt.verbose)
{
if ( done )
{
printf("saved to %s\n", jpeg_name );
}
else
{
printf("\n");
}
}
return 0;
}
/*-------------------------------------------------------------------------
* Function: do_image
*
* Parameters: HDF5 file id, command line options, an image name
*
* Purpose: read HDF5 image, save jpeg image
*
* Return: 0, all is fine, -1 not all is fine
*
*-------------------------------------------------------------------------
*/
static
int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name, char* jpeg_name)
{
hsize_t width;
hsize_t height;
hsize_t planes;
char interlace[20];
hssize_t npals;
const char* name;
int done;
unsigned char* ibuf=NULL;
name = image_name;
done = 0;
if ( H5IMget_image_info( fid, name, &width, &height, &planes, interlace, &npals ) < 0 )
goto out;
if (NULL == (ibuf = HDmalloc( (size_t)width * (size_t)height * (size_t)planes )))
goto out;
if ( H5IMread_image( fid, name, ibuf ) < 0 )
{
goto out;
}
/*-------------------------------------------------------------------------
* no conversion to true color requested or true color image, just save what we found
* this will result either in
*
* 24bit HDF5 ---> true color jpeg
* 8bit HDF5 ---> grey color jpeg
*
*-------------------------------------------------------------------------
*/
if ( planes == 3 || !opt.convert_true )
{
/* write the jpeg file */
write_JPEG_file (jpeg_name,
ibuf,
(int) height,
(int) width,
(int) planes);
done = 1;
}
/*-------------------------------------------------------------------------
* conversion to truecolor
* this will result in
*
* 8bit HDF5 ---> true color jpeg
*
*-------------------------------------------------------------------------
*/
else if (opt.convert_true && planes == 1 )
{
hsize_t pdims[2]; /* palette dimensions */
unsigned char *pbuf=NULL;/* palette array */
unsigned char *tbuf=NULL;/* true color array */
int ipal; /* palette to use */
if ( H5IMget_npalettes( fid, name, &npals ) < 0 )
{
goto out;
}
/*-------------------------------------------------------------------------
* there are palettes
*-------------------------------------------------------------------------
*/
if ( npals > 0 )
{
/* use either the default (0) palette or a requested one */
ipal = ( opt.idx_palette > 0 ) ? opt.idx_palette : 0;
/* the requested palette may not exist . use the default */
if ( opt.idx_palette >= npals )
{
ipal = 0;
if ( opt.verbose )
{
printf("palette index <%d> does not exist. Using default...",
ipal );
}
}
if ( H5IMget_palette_info( fid, name, ipal, pdims ) < 0 )
goto out;
if (NULL == (pbuf = HDmalloc( (size_t)pdims[0] * (size_t)pdims[1] )))
goto out;
if (NULL == (tbuf = HDmalloc( (size_t)width * (size_t)height * 3 )))
goto out;
if ( H5IMget_palette( fid, name, ipal, pbuf ) < 0 )
goto out;
/* convert indexed image to true color image */
convert_to_true(width, height, ibuf, pbuf, tbuf);
/* write the jpeg file */
write_JPEG_file (jpeg_name,
tbuf,
(int) height,
(int) width,
3);
done = 1;
free( pbuf );
free( tbuf );
pbuf = NULL;
tbuf = NULL;
}
/*-------------------------------------------------------------------------
* there are no palettes
*-------------------------------------------------------------------------
*/
else
{
done = 0;
if ( opt.verbose )
{
printf("image <%s> has no palette...", name );
}
} /* no palettes */
} /* conversion to truecolor */
free( ibuf );
ibuf = NULL;
return done;
out:
return -1;
}
/*-------------------------------------------------------------------------
* Function: make_jpeg_name
*
* Parameters: template name (IN), image name (IN), jpeg name (IN/OUT)
*
* Purpose: build a name for the jpeg image file upon a template name
* and the HDF5 image name. Replace the special characters
* "%", "@", "$", "/", ":", "&", and "*" with "_"
*
* Return: void
*
*-------------------------------------------------------------------------
*/
static
void make_jpeg_name( const char* template_name, const char* image_name, char* jpeg_name)
{
int j;
int len;
HDstrcpy( jpeg_name, template_name );
HDstrcat( jpeg_name, image_name );
HDstrcat( jpeg_name, ".jpeg" );
len = HDstrlen( jpeg_name);
/* HDF5 path names might contain '/', replace with '_' */
for (j = 0; j < len; j++)
{
if ( (jpeg_name[j] == '/') ||
(jpeg_name[j] == '%') ||
(jpeg_name[j] == '@') ||
(jpeg_name[j] == '$') ||
(jpeg_name[j] == '/') ||
(jpeg_name[j] == ':') ||
(jpeg_name[j] == '&') ||
(jpeg_name[j] == '*') )
{
jpeg_name[j] = '_';
}
}
}
/*-------------------------------------------------------------------------
* Function: convert_to_true
*
* Parameters:
*
* Purpose: convert a greycolor buffer to a true color using a palette buffer
*
* Return:
*
*-------------------------------------------------------------------------
*/
static
void convert_to_true( hsize_t width, hsize_t height, unsigned char* ibuf, unsigned char* pbuf, unsigned char* tbuf)
{
hsize_t i, j;
for ( i = 0, j = 0; i < width * height; i++, j += 3)
{
unsigned char idx;
unsigned char r;
unsigned char g;
unsigned char b;
/* get the index from the grey image */
idx = ibuf[i];
/* get the RGB color */
r = pbuf[3*idx];
g = pbuf[3*idx+1];
b = pbuf[3*idx+2];
/* define the color buffer */
tbuf[j] = r;
tbuf[j+1] = g;
tbuf[j+2] = b;
}
}
/*
* Sample routine for JPEG compression.
*
* IMAGE DATA FORMATS:
*
* The standard input image format is a rectangular array of pixels, with
* each pixel having the same number of "component" values (color channels).
* Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
* If you are working with color data, then the color values for each pixel
* must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
* RGB color.
*
* For this example, we'll assume that this data structure matches the way
* our application has stored the image in memory, so we can just pass a
* pointer to our image buffer.
*/
static
void write_JPEG_file(char *filename,
JSAMPLE *image_buffer, /* Points to large array of R,G,B-order data */
int image_height, /* Number of rows in image */
int image_width, /* Number of columns in image */
int planes) /* # of color components per pixel */
{
/* This struct contains the JPEG compression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
* It is possible to have several such structures, representing multiple
* compression/decompression processes, in existence at once. We refer
* to any one struct (and its associated working data) as a "JPEG object".
*/
struct jpeg_compress_struct cinfo;
/* This struct represents a JPEG error handler. It is declared separately
* because applications often want to supply a specialized error handler
* (see the second half of this file for an example). But here we just
* take the easy way out and use the standard error handler, which will
* print a message on stderr and call exit() if compression fails.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
struct jpeg_error_mgr jerr;
/* More stuff */
FILE * outfile; /* target file */
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */
/* Step 1: allocate and initialize JPEG compression object */
/* We have to set up the error handler first, in case the initialization
* step fails. (Unlikely, but it could happen if you are out of memory.)
* This routine fills in the contents of struct jerr, and returns jerr's
* address which we place into the link field in cinfo.
*/
cinfo.err = jpeg_std_error(&jerr);
/* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo);
/* Step 2: specify data destination (eg, a file) */
/* Note: steps 2 and 3 can be done in either order. */
/* Here we use the library-supplied code to send compressed data to a
* stdio stream. You can also write your own code to do something else.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to write binary files.
*/
if ((outfile = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
/* Step 3: set parameters for compression */
/* First we supply a description of the input image.
* Four fields of the cinfo struct must be filled in:
*/
cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components = planes; /* # of color components per pixel */
/* colorspace of input image */
if (planes == 3)
cinfo.in_color_space = JCS_RGB;
else if (planes == 1)
cinfo.in_color_space = JCS_GRAYSCALE;
/* Now use the library's routine to set default compression parameters.
* (You must set at least cinfo.in_color_space before calling this,
* since the defaults depend on the source color space.)
*/
jpeg_set_defaults(&cinfo);
/* Now you can set any non-default parameters you wish to.
* Here we just illustrate the use of quality (quantization table) scaling:
*/
jpeg_set_quality(&cinfo, 100, TRUE /* limit to baseline-JPEG values */);
/* Step 4: Start compressor */
/* TRUE ensures that we will write a complete interchange-JPEG file.
* Pass TRUE unless you are very sure of what you're doing.
*/
jpeg_start_compress(&cinfo, TRUE);
/* Step 5: while (scan lines remain to be written) */
/* jpeg_write_scanlines(...); */
/* Here we use the library's state variable cinfo.next_scanline as the
* loop counter, so that we don't have to keep track ourselves.
* To keep things simple, we pass one scanline per call; you can pass
* more if you wish, though.
*/
row_stride = image_width * planes; /* JSAMPLEs per row in image_buffer */
while (cinfo.next_scanline < cinfo.image_height) {
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
/* After finish_compress, we can close the output file. */
fclose(outfile);
/* Step 7: release JPEG compression object */
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_compress(&cinfo);
/* And we're done! */
}
|