diff options
author | Pedro Vicente Nunes <pvn@hdfgroup.org> | 2005-03-28 15:24:10 (GMT) |
---|---|---|
committer | Pedro Vicente Nunes <pvn@hdfgroup.org> | 2005-03-28 15:24:10 (GMT) |
commit | af6d77bfc4effe7e206745d2ef8da06fd0a7e58a (patch) | |
tree | 076f0722ce118d40560a0613edf26baf836f6ed8 /hl/test/test_image.c | |
parent | 293f9f9afb307c7e30151548651ae89ff3f0936f (diff) | |
download | hdf5-af6d77bfc4effe7e206745d2ef8da06fd0a7e58a.zip hdf5-af6d77bfc4effe7e206745d2ef8da06fd0a7e58a.tar.gz hdf5-af6d77bfc4effe7e206745d2ef8da06fd0a7e58a.tar.bz2 |
[svn-r10458] Purpose:
added new tests for the image API
Description:
Solution:
Platforms tested:
linux
solaris
AIX
Misc. update:
Diffstat (limited to 'hl/test/test_image.c')
-rw-r--r-- | hl/test/test_image.c | 605 |
1 files changed, 551 insertions, 54 deletions
diff --git a/hl/test/test_image.c b/hl/test/test_image.c index c01dd0d..8b446b8 100644 --- a/hl/test/test_image.c +++ b/hl/test/test_image.c @@ -14,30 +14,79 @@ #include "H5IM.h" - -#define FILE_NAME "test_image.h5" -#define WIDTH (hsize_t)500 -#define HEIGHT (hsize_t)200 -unsigned char image_in1 [ WIDTH*HEIGHT ]; -unsigned char image_out1[ WIDTH*HEIGHT ]; -unsigned char image_in2 [ WIDTH*HEIGHT*3 ]; -unsigned char image_out2[ WIDTH*HEIGHT*3 ]; +#include "pal_rgb.h" +#include <stdlib.h> +#include <string.h> + +#define FILE1 "test_image1.h5" +#define FILE2 "test_image2.h5" +#define FILE3 "test_image3.h5" +#define DATA_FILE1 "image8.txt" +#define DATA_FILE2 "image24pixel.txt" +#define DATA_FILE3 "image24plane.txt" +#define DATA_FILE4 "usa.wri" +#define IMAGE1_NAME "image8bit" +#define IMAGE2_NAME "image24bitpixel" +#define IMAGE3_NAME "image24bitplane" +#define PAL_NAME "rainbow pallete" + +#define WIDTH (hsize_t)50 +#define HEIGHT (hsize_t)20 + +/* prototypes */ +static int test_simple(void); +static int test_data(void); +static int test_generate(void); +static int read_data( const char* file_name, hsize_t *width, hsize_t *height ); + +/* globals */ +unsigned char *image_data = NULL; /*------------------------------------------------------------------------- * the main program *------------------------------------------------------------------------- */ -int main( void ) + +int main(void) { - hid_t file_id; - hsize_t width, height, planes; + int nerrors=0; + + nerrors += test_simple()<0 ?1:0; + nerrors += test_data()<0 ?1:0; + nerrors += test_generate()<0 ?1:0; + + if (nerrors) goto error; + printf("All image tests passed.\n"); + return 0; + +error: + printf("***** %d IMAGE TEST%s FAILED! *****\n",nerrors, 1 == nerrors ? "" : "S"); + return 1; +} + +/*------------------------------------------------------------------------- + * a simple test that generates images and palettes + *------------------------------------------------------------------------- + */ + +static int test_simple(void) +{ + hid_t fid; + hsize_t width; + hsize_t height; + hsize_t planes; hsize_t pal_dims[] = {9,3}; hsize_t pal_dims_out[2]; - hsize_t i; char interlace[20]; hssize_t npals; - + hsize_t i; + herr_t is_image; + herr_t is_pal; + unsigned char image_in1 [ WIDTH*HEIGHT ]; + unsigned char image_out1[ WIDTH*HEIGHT ]; + unsigned char image_in2 [ WIDTH*HEIGHT*3 ]; + unsigned char image_out2[ WIDTH*HEIGHT*3 ]; unsigned char pal_data_out[9*3]; /* create a 9 entry grey palette */ unsigned char pal_data_in[9*3] = {0,0,0, @@ -55,64 +104,91 @@ int main( void ) for (i = 0; i < WIDTH*HEIGHT*3; i++) image_in2[i] = (unsigned char)i; - /* Create a new HDF5 file using default properties. */ - file_id = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); + /* create a file using default properties */ + if ((fid=H5Fcreate(FILE1,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0) + goto out; + + + printf("Testing API functions\n"); /*------------------------------------------------------------------------- - * Indexed image test + * indexed image test *------------------------------------------------------------------------- */ - TESTING("indexed image"); + TESTING2("indexed image"); - /* Write image */ - if ( H5IMmake_image_8bit( file_id, "Image1", WIDTH, HEIGHT, image_in1 ) < 0 ) + /* write image */ + if (H5IMmake_image_8bit(fid,"image1",WIDTH,HEIGHT,image_in1)<0) goto out; - /* Make a palette */ - if ( H5IMmake_palette( file_id, "Pallete", pal_dims, pal_data_in ) < 0 ) + /* make a palette */ + if (H5IMmake_palette(fid,"palette",pal_dims,pal_data_in)<0) goto out; - /* Attach a palette to the image dataset */ - if ( H5IMlink_palette( file_id, "Image1", "Pallete" ) < 0 ) + /* attach the palette to the image dataset */ + if (H5IMlink_palette(fid,"image1","palette")<0) goto out; - /* Read image */ - if ( H5IMget_image_info( file_id, "Image1", &width, &height, &planes, interlace, &npals ) < 0 ) + /* get info */ + if (H5IMget_image_info(fid,"image1",&width,&height,&planes,interlace,&npals)<0) goto out; - if ( H5IMread_image( file_id, "Image1", image_out1 ) < 0 ) + if (width!=WIDTH) + goto out; + if (height!=HEIGHT) + goto out; + if (planes!=1) + goto out; + if (npals!=1) goto out; - for (i = 0; i < height*width*planes; i++) { - if ( image_in1[i] != image_out1[i] ) { - goto out; - + /* read image */ + if (H5IMread_image(fid,"image1",image_out1)<0) + goto out; + + /* check */ + for (i = 0; i < height*width*planes; i++) + { + if ( image_in1[i] != image_out1[i] ) + { + goto out; } } PASSED(); /*------------------------------------------------------------------------- - * True color image test + * true color image test *------------------------------------------------------------------------- */ - TESTING("true color image"); + TESTING2("true color image"); - /* Write image */ - if ( H5IMmake_image_24bit( file_id, "Image2", WIDTH, HEIGHT, "INTERLACE_PIXEL", image_in2 ) ) + /* write image */ + if (H5IMmake_image_24bit(fid,"image2",WIDTH,HEIGHT,"INTERLACE_PIXEL",image_in2)) goto out; - /* Read image */ - if ( H5IMget_image_info( file_id, "Image2", &width, &height, &planes, interlace, &npals ) < 0 ) + /* get info */ + if (H5IMget_image_info(fid,"image2",&width,&height,&planes,interlace,&npals)<0) + goto out; + + if (width!=WIDTH) + goto out; + if (height!=HEIGHT) + goto out; + if (planes!=3) goto out; - if ( H5IMread_image( file_id, "Image2", image_out2 ) < 0 ) + /* read image */ + if (H5IMread_image(fid,"image2",image_out2)<0) goto out; - for (i = 0; i < height*width*planes; i++) { - if ( image_in2[i] != image_out2[i] ) { + /* check */ + for (i = 0; i < height*width*planes; i++) + { + if ( image_in2[i] != image_out2[i] ) + { goto out; } } @@ -124,9 +200,12 @@ int main( void ) *------------------------------------------------------------------------- */ - TESTING("pallete functions"); + TESTING2("palette functions"); + + if (H5IMget_npalettes(fid,"image1",&npals)<0) + goto out; - if ( H5IMget_npalettes( file_id, "Image1", &npals ) < 0 ) + if (npals!=1) goto out; /*------------------------------------------------------------------------- @@ -134,12 +213,15 @@ int main( void ) *------------------------------------------------------------------------- */ - if ( H5IMget_palette_info( file_id, "Image1", 0, pal_dims_out ) < 0 ) + if (H5IMget_palette_info(fid,"image1",0,pal_dims_out)<0) goto out; - for (i = 0; i < 2; i++) { - if ( pal_dims[i] != pal_dims_out[i] ) { - goto out; + /* check */ + for (i = 0; i < 2; i++) + { + if ( pal_dims[i] != pal_dims_out[i] ) + { + goto out; } } @@ -148,11 +230,14 @@ int main( void ) *------------------------------------------------------------------------- */ - if ( H5IMget_palette( file_id, "Image1", 0, pal_data_out ) < 0 ) + if (H5IMget_palette(fid,"image1",0,pal_data_out)<0) goto out; - for (i = 0; i < 9*3; i++) { - if ( pal_data_in[i] != pal_data_out[i] ) { + /* check */ + for (i = 0; i < 9*3; i++) + { + if ( pal_data_in[i] != pal_data_out[i] ) + { goto out; } } @@ -162,10 +247,16 @@ int main( void ) *------------------------------------------------------------------------- */ - if ( H5IMis_image( file_id, "Image1" ) < 0 ) + if ((is_image=H5IMis_image(fid,"image1"))<0) + goto out; + + if (is_image!=1) + goto out; + + if ((is_image=H5IMis_image(fid,"image2"))<0) goto out; - if ( H5IMis_image( file_id, "Image2" ) < 0 ) + if (is_image!=1) goto out; /*------------------------------------------------------------------------- @@ -173,22 +264,428 @@ int main( void ) *------------------------------------------------------------------------- */ - if ( H5IMis_palette( file_id, "Pallete" ) < 0 ) + if ((is_pal=H5IMis_palette(fid,"palette"))<0) goto out; + if (is_pal!=1) + goto out; + + PASSED(); + /*------------------------------------------------------------------------- - * end tests + * close + *------------------------------------------------------------------------- + */ + if (H5Fclose(fid)<0) + goto out; + + return 0; + + /* error zone, gracefully close */ +out: + H5E_BEGIN_TRY { + H5Fclose(fid); + } H5E_END_TRY; + H5_FAILED(); + return FAIL; +} + + +/*------------------------------------------------------------------------- + * read sample realistic image data from ASCII files + *------------------------------------------------------------------------- + */ + +static int test_data(void) +{ + hid_t fid; + hsize_t pal_dims[2]; + char *srcdir = getenv("srcdir"); /* the source directory */ + char data_file[512]=""; /* buffer to hold name of existing data file */ + hsize_t width; + hsize_t height; + + /* create a file using default properties */ + if ((fid=H5Fcreate(FILE2,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0) + goto out; + + printf("Testing read ascii image data and generate images\n"); + +/*------------------------------------------------------------------------- + * read 8bit image data + *------------------------------------------------------------------------- + */ + + TESTING2("make indexed image"); + + strcpy(data_file, ""); + /* compose the name of the file to open, using the srcdir, if appropriate */ + if (srcdir) + { + strcpy(data_file, srcdir); + strcat(data_file, "/"); + } + /* read first data file */ + strcat(data_file,DATA_FILE1); + if ((read_data(data_file,&width,&height))<0) + goto out; + + /* make an image */ + if ((H5IMmake_image_8bit(fid,IMAGE1_NAME,width,height,image_data))<0) + goto out; + + /* initialize the palette data; pal_rgb data is contained in "pal_rgb.h" */ + pal_dims[0] = 256; + pal_dims[1] = 3; + + /* make a palette */ + if ((H5IMmake_palette(fid,PAL_NAME,pal_dims,pal_rgb))<0) + goto out; + + /* attach a palette to the image dataset */ + if ((H5IMlink_palette(fid,IMAGE1_NAME,PAL_NAME))<0) + goto out; + + PASSED(); + + +/*------------------------------------------------------------------------- + * true color image example with pixel interlace + *------------------------------------------------------------------------- + */ + + TESTING2("make true color image with pixel interlace"); + + /* read second data file */ + strcpy(data_file, ""); + /* compose the name of the file to open, using the srcdir, if appropriate */ + if ( srcdir ) + { + strcpy(data_file, srcdir); + strcat(data_file, "/"); + } + strcat( data_file, DATA_FILE2); + if ((read_data(data_file,&width,&height))<0) + goto out; + + /* make image */ + if ((H5IMmake_image_24bit(fid,IMAGE2_NAME,width,height,"INTERLACE_PIXEL",image_data))<0) + goto out; + + PASSED(); + +/*------------------------------------------------------------------------- + * True color image example with plane interlace *------------------------------------------------------------------------- */ - /* Close the file. */ - if(H5Fclose( file_id ) < 0) goto out; + TESTING2("make true color image with plane interlace"); + + /* read third data file */ + strcpy(data_file, ""); + /* compose the name of the file to open, using the srcdir, if appropriate */ + if ( srcdir ) + { + strcpy(data_file, srcdir); + strcat(data_file, "/"); + } + strcat( data_file, DATA_FILE3); + if ((read_data(data_file,&width,&height))<0) + goto out; + + /* make image */ + if ((H5IMmake_image_24bit(fid,IMAGE3_NAME,width,height,"INTERLACE_PLANE",image_data))<0) + goto out; PASSED(); + + +/*------------------------------------------------------------------------- + * close + *------------------------------------------------------------------------- + */ + if (H5Fclose(fid)<0) + goto out; + return 0; + + /* error zone, gracefully close */ +out: + H5E_BEGIN_TRY { + H5Fclose(fid); + } H5E_END_TRY; + H5_FAILED(); + return FAIL; +} + + + /* + The following test provides an examples of how to generate HDF5 image data from + floating point data. In the example we use real life topographic data + (from the North American hemisphere). In the dataset sea values are represented + as negative numbers and land values are represented as positive numbers. + The example generates 3 HDF5 images, one that generates an image from all the values, + another that generates an image from the land values and another that generates an + image from the sea values. + For the example we used data from MODB, the Mediterranean Oceanic Data Base + http://modb.oce.ulg.ac.be/ + +*/ + +static int test_generate(void) +{ + hid_t fid; + hsize_t pal_dims[2] = { 256, 3 }; + float *data; + int imax, jmax, kmax; + float valex, xmin, xmax, value; + FILE *f; + char *srcdir = getenv("srcdir"); /* the source directory */ + char data_file[512]=""; /* buffer to hold name of existing data file */ + int i; + + /* create a file using default properties */ + if ((fid=H5Fcreate(FILE3,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0) + goto out; + printf("Testing read and process data and make indexed images\n"); + +/*------------------------------------------------------------------------- + * read data; the file data format is described below + *------------------------------------------------------------------------- + */ + + /* compose the name of the file to open, using the srcdir, if appropriate */ + if ( srcdir ) + { + strcpy(data_file, srcdir); + strcat(data_file, "/"); + } + strcat(data_file,DATA_FILE4); + + /* Read data file */ + f = fopen( data_file, "r" ) ; + + if ( f == NULL ) + { + printf( "Could not find file %s. Try set $srcdir \n", DATA_FILE4 ); + H5Fclose(fid); + return -1; + } + +/* +!The first line of the ASCII file contains the dimension of the array : +! IMAX, JMAX, KMAX. The integers are stored with the FORTRAN format I5. +!The second line contains the exclusion value, the minimum and the maximum value of this +! file. These numbers are stored with the FORTRAN format E12.5. +! The remaining lines contains the data of the array, with 5 numbers per line +! (except the last line for each I-line). +! The array is stored in horizontal slices from sea surface to sea bottom and from +! north to south. So the indexes go from : +! +! DO K = KMAX to 1 +! DO J = JMAX to 1 +! DO I = 1 to IMAX +! read +! OD +! OD +! OD +! +! ____________________________ +! / /| (imax,jmax,kmax) +! / sea surface / | +! / / | +! /__________________________ / | +! | | | +! | | | (imax,jmax,1) n +! | | / / +! | | / / +! ^ j | | / w <-----o-----> e +! k | / |__________________________|/ / +! | / (imax,1,1) / +! |----------> s +! i +! +*/ + + + fscanf( f, "%d %d %d", &imax, &jmax, &kmax ); + fscanf( f, "%f %f %f", &valex, &xmin, &xmax ); + + data = (float*) malloc ( imax * jmax * kmax * sizeof( float )); + image_data = (unsigned char*) malloc ( imax * jmax * kmax * sizeof( unsigned char )); + + for ( i = 0; i < imax * jmax * kmax; i++ ) + { + fscanf( f, "%f ", &value ); + data[i] = value; + } + fclose( f ); + +/*------------------------------------------------------------------------- + * transform the data from floating point to unsigend char + * we are processing all the data here + *------------------------------------------------------------------------- + */ + + TESTING2("make indexed image from all the data"); + + for ( i = 0; i < imax * jmax * kmax; i++ ) + { + image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / (xmax - xmin )); + } + + /* Make the image */ + if ((H5IMmake_image_8bit(fid,"All data",(hsize_t)imax,(hsize_t)jmax,image_data))<0) + goto out; + + PASSED(); + +/*------------------------------------------------------------------------- + * transform the data from floating point to unsigend char + * here we just process the land data + *------------------------------------------------------------------------- + */ + + TESTING2("make indexed image from land data"); + + for ( i = 0; i < imax * jmax * kmax; i++ ) + { + if ( data[i] < 0 ) + image_data[i] = 0; + else + image_data[i] = (unsigned char)(( 255 * (data[i] ) ) / xmax ); + } + + /* make the image */ + if ((H5IMmake_image_8bit(fid,"Land data",(hsize_t)imax,(hsize_t)jmax,image_data))<0) + goto out; + + PASSED(); + +/*------------------------------------------------------------------------- + * transform the data from floating point to unsigend char + * here we just process the sea data + *------------------------------------------------------------------------- + */ + + TESTING2("make indexed image from sea data"); + + for ( i = 0; i < imax * jmax * kmax; i++ ) + { + if ( data[i] > 0 ) + image_data[i] = 0; + else + image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / xmin ); + } + + /* make the image */ + if ((H5IMmake_image_8bit(fid,"Sea data",(hsize_t)imax,(hsize_t)jmax,image_data))<0) + goto out; + + PASSED(); + +/*------------------------------------------------------------------------- + * make a palette and attach it to the datasets + *------------------------------------------------------------------------- + */ + + TESTING2("attaching palettes"); + + /* make a palette */ + if ((H5IMmake_palette(fid,PAL_NAME,pal_dims,pal_rgb))<0) + goto out; + + /* Attach the palette to the image datasets */ + if ((H5IMlink_palette(fid,"All data",PAL_NAME))<0) + goto out; + if ((H5IMlink_palette(fid,"Land data",PAL_NAME))<0) + goto out; + if ((H5IMlink_palette(fid,"Sea data",PAL_NAME))<0) + goto out; + + PASSED(); + + +/*------------------------------------------------------------------------- + * close + *------------------------------------------------------------------------- + */ + if (H5Fclose(fid)<0) + goto out; + + return 0; + + /* error zone, gracefully close */ out: + H5E_BEGIN_TRY { + H5Fclose(fid); + } H5E_END_TRY; H5_FAILED(); + return FAIL; +} + + +/*------------------------------------------------------------------------- + * 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* file_name, /*IN*/ + hsize_t *width, /*OUT*/ + hsize_t *height /*OUT*/ ) +{ + int i, n; + int color_planes; + char str[20]; + FILE *f; + int w, h; + + f = fopen( file_name, "r"); + + if ( f == NULL ) + { + printf( "Could not open file %s. Try set $srcdir \n", file_name ); + 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 ( image_data ) + { + free( image_data ); + image_data=NULL; + } + + image_data = (unsigned char*) malloc (w * h * color_planes * sizeof( unsigned char )); + + for (i = 0; i < h * w * color_planes ; i++) + { + fscanf( f, "%d",&n ); + image_data[i] = (unsigned char)n; + } + fclose(f); + return 1; } |