2006-03-07 00:11:11 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
2007-02-07 22:56:24 +08:00
|
|
|
* Copyright by The HDF Group. *
|
2006-03-07 00:11:11 +08:00
|
|
|
* 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 *
|
2007-02-07 22:56:24 +08:00
|
|
|
* 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. *
|
2006-03-07 00:11:11 +08:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2006-04-13 05:21:38 +08:00
|
|
|
#include "hdf5.h"
|
2006-04-20 12:49:06 +08:00
|
|
|
#include "hdf5_hl.h"
|
2006-03-07 00:11:11 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DATA_FILE1 "image8.txt"
|
|
|
|
#define DATA_FILE2 "image24pixel.txt"
|
|
|
|
#define IMAGE1_NAME "image8bit"
|
|
|
|
#define IMAGE2_NAME "image24bitpixel"
|
|
|
|
#define PAL_NAME "palette"
|
|
|
|
#define PAL_ENTRIES 256
|
|
|
|
|
|
|
|
static int read_data(const char* file_name, hsize_t *width, hsize_t *height );
|
[svn-r24726] Description:
Revert some earlier usage of strncpy, which was incorrect.
Bring Coverity changes from branch back to trunk:
r20821:
Use HDstrncpy. --gh
(Fixed already, with strdup)
r20822:
(Not merged, incorrect use of HDstrncpy())
r20823:
(Not merged, incorrect use of HDstrncpy())
r20824:
Maintenance: Bug fix: addressed CID 666.
Value stored at *expression_len should be used in the call to
HD5packFstring to avoid overflow (and unnecessary arithmetic
calculation and casting)
r20825:
Issue 642: Added check for error and handler with print to stderr and exit.
r20826:
Undo revision 20818, as that issue has already been fixed in the 1.8 branch
and trunk (but not coverity branch)
r20827:
(Not merged, incorrect use of HDstrncpy())
r20828:
Use HDstrncpy. --gh
(Corrected use of strncpy())
r20829:
Check return of H5Lget_val(print_udata->fid, path, targbuf,
linfo->u.val_size + 1, H5P_DEFAULT) and if error set trgbuf[0] to 0.
Check if H5Lunpack_elink_val(targbuf, linfo->u.val_size, NULL,
&filename, &objname) was successful and allow print. Otherwise filename
and objname are not created. (init those to NULL)
r20830:
resolved coverity issues 939, 940, 941, 944, and 947.
all were complaints about use of sprintf, and in all cases, the
buffers used were large enough for all eventualities.
Resolved issue by replacing calls to sprintf with calls
to snprintf.
r20831:
Maintenance: Addressed CID 852
Replaced sprintf with snprintf
r20832:
Purpose: Fix valgrind issues with hl/examples/ex_image2
Description:
Modified hl/examples/ex_image2 to free global "gbuf" before exit.
Tested on:
Mac OSX/64 10.9.1 (amaon) w/C++, FORTRAN & Threadsafety
(too minor to require h5committest)
2014-02-21 05:29:26 +08:00
|
|
|
unsigned char *gbuf = NULL; /* global buffer for image data */
|
2006-03-07 00:11:11 +08:00
|
|
|
|
|
|
|
int main( void )
|
|
|
|
{
|
|
|
|
hid_t file_id; /* HDF5 file identifier */
|
|
|
|
hsize_t width; /* width of image */
|
|
|
|
hsize_t height; /* height of image */
|
|
|
|
unsigned char pal[ PAL_ENTRIES * 3 ]; /* palette array */
|
|
|
|
hsize_t pal_dims[2] = {PAL_ENTRIES,3}; /* palette dimensions */
|
2015-03-10 02:45:44 +08:00
|
|
|
herr_t i, n;
|
2006-06-27 22:45:06 +08:00
|
|
|
|
2006-03-07 00:11:11 +08:00
|
|
|
/* create a new HDF5 file using default properties. */
|
|
|
|
file_id = H5Fcreate( "ex_image2.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
|
|
|
|
|
|
|
|
/* read first data file */
|
|
|
|
if (read_data(DATA_FILE1,&width,&height)<0)
|
|
|
|
goto out;
|
2006-06-27 22:45:06 +08:00
|
|
|
|
2006-03-07 00:11:11 +08:00
|
|
|
/* make the image */
|
2015-03-10 02:45:44 +08:00
|
|
|
H5IMmake_image_8bit( file_id, IMAGE1_NAME, width, height, gbuf );
|
2013-07-03 22:34:12 +08:00
|
|
|
if (gbuf) {
|
|
|
|
free(gbuf);
|
|
|
|
gbuf = NULL;
|
|
|
|
}
|
2006-03-07 00:11:11 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2006-06-27 22:45:06 +08:00
|
|
|
* define a palette, blue to red tones
|
2006-03-07 00:11:11 +08:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
for ( i=0, n=0; i<PAL_ENTRIES*3; i+=3, n++)
|
|
|
|
{
|
|
|
|
pal[i] =n; /* red */
|
|
|
|
pal[i+1]=0; /* green */
|
|
|
|
pal[i+2]=255-n; /* blue */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make a palette */
|
2015-03-10 02:45:44 +08:00
|
|
|
H5IMmake_palette( file_id, PAL_NAME, pal_dims, pal );
|
2006-03-07 00:11:11 +08:00
|
|
|
|
|
|
|
/* attach the palette to the image */
|
2015-03-10 02:45:44 +08:00
|
|
|
H5IMlink_palette( file_id, IMAGE1_NAME, PAL_NAME );
|
2006-03-07 00:11:11 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* True color image example with pixel interlace
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* read second data file */
|
|
|
|
if (read_data(DATA_FILE2,&width,&height)<0)
|
|
|
|
goto out;
|
2006-06-27 22:45:06 +08:00
|
|
|
|
2006-03-07 00:11:11 +08:00
|
|
|
/* make dataset */
|
2015-03-10 02:45:44 +08:00
|
|
|
H5IMmake_image_24bit( file_id, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", gbuf );
|
2006-03-07 00:11:11 +08:00
|
|
|
|
|
|
|
/* close the file. */
|
|
|
|
H5Fclose( file_id );
|
|
|
|
|
[svn-r24726] Description:
Revert some earlier usage of strncpy, which was incorrect.
Bring Coverity changes from branch back to trunk:
r20821:
Use HDstrncpy. --gh
(Fixed already, with strdup)
r20822:
(Not merged, incorrect use of HDstrncpy())
r20823:
(Not merged, incorrect use of HDstrncpy())
r20824:
Maintenance: Bug fix: addressed CID 666.
Value stored at *expression_len should be used in the call to
HD5packFstring to avoid overflow (and unnecessary arithmetic
calculation and casting)
r20825:
Issue 642: Added check for error and handler with print to stderr and exit.
r20826:
Undo revision 20818, as that issue has already been fixed in the 1.8 branch
and trunk (but not coverity branch)
r20827:
(Not merged, incorrect use of HDstrncpy())
r20828:
Use HDstrncpy. --gh
(Corrected use of strncpy())
r20829:
Check return of H5Lget_val(print_udata->fid, path, targbuf,
linfo->u.val_size + 1, H5P_DEFAULT) and if error set trgbuf[0] to 0.
Check if H5Lunpack_elink_val(targbuf, linfo->u.val_size, NULL,
&filename, &objname) was successful and allow print. Otherwise filename
and objname are not created. (init those to NULL)
r20830:
resolved coverity issues 939, 940, 941, 944, and 947.
all were complaints about use of sprintf, and in all cases, the
buffers used were large enough for all eventualities.
Resolved issue by replacing calls to sprintf with calls
to snprintf.
r20831:
Maintenance: Addressed CID 852
Replaced sprintf with snprintf
r20832:
Purpose: Fix valgrind issues with hl/examples/ex_image2
Description:
Modified hl/examples/ex_image2 to free global "gbuf" before exit.
Tested on:
Mac OSX/64 10.9.1 (amaon) w/C++, FORTRAN & Threadsafety
(too minor to require h5committest)
2014-02-21 05:29:26 +08:00
|
|
|
if(gbuf) {
|
|
|
|
free(gbuf);
|
|
|
|
gbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-07 00:11:11 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
printf("Error on return function...Exiting\n");
|
[svn-r24726] Description:
Revert some earlier usage of strncpy, which was incorrect.
Bring Coverity changes from branch back to trunk:
r20821:
Use HDstrncpy. --gh
(Fixed already, with strdup)
r20822:
(Not merged, incorrect use of HDstrncpy())
r20823:
(Not merged, incorrect use of HDstrncpy())
r20824:
Maintenance: Bug fix: addressed CID 666.
Value stored at *expression_len should be used in the call to
HD5packFstring to avoid overflow (and unnecessary arithmetic
calculation and casting)
r20825:
Issue 642: Added check for error and handler with print to stderr and exit.
r20826:
Undo revision 20818, as that issue has already been fixed in the 1.8 branch
and trunk (but not coverity branch)
r20827:
(Not merged, incorrect use of HDstrncpy())
r20828:
Use HDstrncpy. --gh
(Corrected use of strncpy())
r20829:
Check return of H5Lget_val(print_udata->fid, path, targbuf,
linfo->u.val_size + 1, H5P_DEFAULT) and if error set trgbuf[0] to 0.
Check if H5Lunpack_elink_val(targbuf, linfo->u.val_size, NULL,
&filename, &objname) was successful and allow print. Otherwise filename
and objname are not created. (init those to NULL)
r20830:
resolved coverity issues 939, 940, 941, 944, and 947.
all were complaints about use of sprintf, and in all cases, the
buffers used were large enough for all eventualities.
Resolved issue by replacing calls to sprintf with calls
to snprintf.
r20831:
Maintenance: Addressed CID 852
Replaced sprintf with snprintf
r20832:
Purpose: Fix valgrind issues with hl/examples/ex_image2
Description:
Modified hl/examples/ex_image2 to free global "gbuf" before exit.
Tested on:
Mac OSX/64 10.9.1 (amaon) w/C++, FORTRAN & Threadsafety
(too minor to require h5committest)
2014-02-21 05:29:26 +08:00
|
|
|
|
|
|
|
if(gbuf) {
|
|
|
|
free(gbuf);
|
|
|
|
gbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-07 00:11:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read_data
|
|
|
|
* utility function to read ASCII image data
|
|
|
|
* the files have a header of the type
|
|
|
|
*
|
|
|
|
* components
|
|
|
|
* n
|
|
|
|
* height
|
|
|
|
* n
|
|
|
|
* width
|
|
|
|
* n
|
|
|
|
*
|
|
|
|
* followed by the image data
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int read_data( const char* fname, /*IN*/
|
|
|
|
hsize_t *width, /*OUT*/
|
|
|
|
hsize_t *height /*OUT*/ )
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
int color_planes;
|
|
|
|
char str[20];
|
|
|
|
FILE *f;
|
|
|
|
int w, h;
|
|
|
|
char *srcdir = getenv("srcdir"); /* the source directory */
|
|
|
|
char data_file[512]=""; /* buffer to hold name of existing data file */
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* compose the name of the file to open, using "srcdir", if appropriate
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
strcpy(data_file, "");
|
|
|
|
if (srcdir)
|
|
|
|
{
|
|
|
|
strcpy(data_file, srcdir);
|
|
|
|
strcat(data_file, "/");
|
|
|
|
}
|
|
|
|
strcat(data_file,fname);
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
f = fopen(data_file, "r");
|
|
|
|
if ( f == NULL )
|
|
|
|
{
|
|
|
|
printf( "Could not open file %s. Try set $srcdir \n", data_file );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fscanf( f, "%s", str );
|
|
|
|
fscanf( f, "%d", &color_planes );
|
|
|
|
fscanf( f, "%s", str );
|
|
|
|
fscanf( f, "%d", &h);
|
|
|
|
fscanf( f, "%s", str );
|
|
|
|
fscanf( f, "%d", &w);
|
|
|
|
|
|
|
|
*width = (hsize_t)w;
|
|
|
|
*height = (hsize_t)h;
|
|
|
|
|
|
|
|
if ( gbuf )
|
|
|
|
{
|
|
|
|
free( gbuf );
|
|
|
|
gbuf=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gbuf = (unsigned char*) malloc (w * h * color_planes * sizeof( unsigned char ));
|
|
|
|
|
|
|
|
for (i = 0; i < h * w * color_planes ; i++)
|
|
|
|
{
|
|
|
|
fscanf( f, "%d",&n );
|
|
|
|
gbuf[i] = (unsigned char)n;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|