diff options
author | Larry Knox <lrknox@hdfgroup.org> | 2021-10-29 22:10:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-29 22:10:31 (GMT) |
commit | 111c7ae9ac2efc29e8b399e6951253af9110b81a (patch) | |
tree | f69fa03253bdab97d7cf3615baff02cd936323f9 /hl/examples/ex_image2.c | |
parent | db30c2da68ece4a155e9e50c28ec16d6057509b2 (diff) | |
download | hdf5-hdf5-1_10_8.zip hdf5-hdf5-1_10_8.tar.gz hdf5-hdf5-1_10_8.tar.bz2 |
Merge hdf5 1 10 8 (#1154)hdf5-1_10_8
Merge HDF5 1.10.8 release files to 1.10/master
Diffstat (limited to 'hl/examples/ex_image2.c')
-rw-r--r-- | hl/examples/ex_image2.c | 272 |
1 files changed, 133 insertions, 139 deletions
diff --git a/hl/examples/ex_image2.c b/hl/examples/ex_image2.c index 5abf723..be9fc4b 100644 --- a/hl/examples/ex_image2.c +++ b/hl/examples/ex_image2.c @@ -6,7 +6,7 @@ * This file is part of HDF5. The full HDF5 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/HDF5/releases. * + * distribution tree, or in https://www.hdfgroup.org/licenses. * * If you do not have access to either file, you may request a copy from * * help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -16,90 +16,89 @@ #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 +#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 ); -unsigned char *gbuf = NULL; /* global buffer for image data */ +static int read_data(const char *file_name, hsize_t *width, hsize_t *height); +unsigned char *gbuf = NULL; /* global buffer for image data */ -int main( void ) +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 */ - herr_t i, n; - - /* 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; - - /* make the image */ - H5IMmake_image_8bit( file_id, IMAGE1_NAME, width, height, gbuf ); - if (gbuf) { - free(gbuf); - gbuf = NULL; - } - -/*------------------------------------------------------------------------- - * define a palette, blue to red tones - *------------------------------------------------------------------------- - */ - 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 */ - H5IMmake_palette( file_id, PAL_NAME, pal_dims, pal ); - - /* attach the palette to the image */ - H5IMlink_palette( file_id, IMAGE1_NAME, PAL_NAME ); - -/*------------------------------------------------------------------------- - * True color image example with pixel interlace - *------------------------------------------------------------------------- - */ - - /* read second data file */ - if (read_data(DATA_FILE2,&width,&height)<0) - goto out; - - /* make dataset */ - H5IMmake_image_24bit( file_id, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", gbuf ); - - /* close the file. */ - H5Fclose( file_id ); - - if(gbuf) { - free(gbuf); - gbuf = NULL; - } - - return 0; + 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 */ + herr_t i, n; + + /* 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; + + /* make the image */ + H5IMmake_image_8bit(file_id, IMAGE1_NAME, width, height, gbuf); + if (gbuf) { + free(gbuf); + gbuf = NULL; + } + + /*------------------------------------------------------------------------- + * define a palette, blue to red tones + *------------------------------------------------------------------------- + */ + 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 */ + H5IMmake_palette(file_id, PAL_NAME, pal_dims, pal); + + /* attach the palette to the image */ + H5IMlink_palette(file_id, IMAGE1_NAME, PAL_NAME); + + /*------------------------------------------------------------------------- + * True color image example with pixel interlace + *------------------------------------------------------------------------- + */ + + /* read second data file */ + if (read_data(DATA_FILE2, &width, &height) < 0) + goto out; + + /* make dataset */ + H5IMmake_image_24bit(file_id, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", gbuf); + + /* close the file. */ + H5Fclose(file_id); + + if (gbuf) { + free(gbuf); + gbuf = NULL; + } + + return 0; out: - printf("Error on return function...Exiting\n"); + printf("Error on return function...Exiting\n"); - if(gbuf) { - free(gbuf); - gbuf = NULL; - } + if (gbuf) { + free(gbuf); + gbuf = NULL; + } - return 1; + return 1; } - /*------------------------------------------------------------------------- * read_data * utility function to read ASCII image data @@ -117,68 +116,63 @@ out: *------------------------------------------------------------------------- */ -static int read_data( const char* fname, /*IN*/ - hsize_t *width, /*OUT*/ - hsize_t *height /*OUT*/ ) +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; - + 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; } - |