[svn-r10458] Purpose:

added new tests for the image API

Description:

Solution:

Platforms tested:
linux
solaris
AIX

Misc. update:
This commit is contained in:
Pedro Vicente Nunes 2005-03-28 10:24:10 -05:00
parent 293f9f9afb
commit af6d77bfc4
7 changed files with 445631 additions and 54 deletions

View File

@ -1596,9 +1596,15 @@
./hl/test/test_table.c
./hl/test/test_packet.c
./hl/test/test_ds.c
./hl/test/pal_rgb.h
./hl/test/test_table_le.hdf5
./hl/test/test_table_be.hdf5
./hl/test/test_table_cray.hdf5
./hl/test/image8.txt
./hl/test/image24pixel.txt
./hl/test/image24plane.txt
./hl/test/usa.wri
# hl fortran
./hl/fortran/src/H5f90i.h

101475
hl/test/image24pixel.txt Normal file

File diff suppressed because it is too large Load Diff

101475
hl/test/image24plane.txt Normal file

File diff suppressed because it is too large Load Diff

120006
hl/test/image8.txt Normal file

File diff suppressed because it is too large Load Diff

267
hl/test/pal_rgb.h Normal file
View File

@ -0,0 +1,267 @@
const unsigned char pal_rgb[256*3] = {0,0,0,
0,0,131,
0,0,135,
0,0,139,
0,0,143,
0,0,147,
0,0,151,
0,0,155,
0,0,159,
0,0,163,
0,0,167,
0,0,171,
0,0,175,
0,0,179,
0,0,183,
0,0,187,
0,0,191,
0,0,195,
0,0,199,
0,0,203,
0,0,207,
0,0,211,
0,0,215,
0,0,219,
0,0,223,
0,0,227,
0,0,231,
0,0,235,
0,0,239,
0,0,243,
0,0,247,
0,0,251,
0,0,255,
0,0,255,
0,3,255,
0,7,255,
0,11,255,
0,15,255,
0,19,255,
0,23,255,
0,27,255,
0,31,255,
0,35,255,
0,39,255,
0,43,255,
0,47,255,
0,51,255,
0,55,255,
0,59,255,
0,63,255,
0,67,255,
0,71,255,
0,75,255,
0,79,255,
0,83,255,
0,87,255,
0,91,255,
0,95,255,
0,99,255,
0,103,255,
0,107,255,
0,111,255,
0,115,255,
0,119,255,
0,123,255,
0,127,255,
0,131,255,
0,135,255,
0,139,255,
0,143,255,
0,147,255,
0,151,255,
0,155,255,
0,159,255,
0,163,255,
0,167,255,
0,171,255,
0,175,255,
0,179,255,
0,183,255,
0,187,255,
0,191,255,
0,195,255,
0,199,255,
0,203,255,
0,207,255,
0,211,255,
0,215,255,
0,219,255,
0,223,255,
0,227,255,
0,231,255,
0,235,255,
0,239,255,
0,243,255,
0,247,255,
0,251,255,
0,255,255,
0,255,255,
3,255,251,
7,255,247,
11,255,243,
15,255,239,
19,255,235,
23,255,231,
27,255,227,
31,255,223,
35,255,219,
39,255,215,
43,255,211,
47,255,207,
51,255,203,
55,255,199,
59,255,195,
63,255,191,
67,255,187,
71,255,183,
75,255,179,
79,255,175,
83,255,171,
87,255,167,
91,255,163,
95,255,159,
99,255,155,
103,255,151,
107,255,147,
111,255,143,
115,255,139,
119,255,135,
123,255,131,
127,255,127,
131,255,123,
135,255,119,
139,255,115,
143,255,111,
147,255,107,
151,255,103,
155,255,99,
159,255,95,
163,255,91,
167,255,87,
171,255,83,
175,255,79,
179,255,75,
183,255,71,
187,255,67,
191,255,63,
195,255,59,
199,255,55,
203,255,51,
207,255,47,
211,255,43,
215,255,39,
219,255,35,
223,255,31,
227,255,27,
231,255,23,
235,255,19,
239,255,15,
243,255,11,
247,255,7,
251,255,3,
255,255,0,
255,251,0,
255,247,0,
255,243,0,
255,239,0,
255,235,0,
255,231,0,
255,227,0,
255,223,0,
255,219,0,
255,215,0,
255,211,0,
255,207,0,
255,203,0,
255,199,0,
255,195,0,
255,191,0,
255,187,0,
255,183,0,
255,179,0,
255,175,0,
255,171,0,
255,167,0,
255,163,0,
255,159,0,
255,155,0,
255,151,0,
255,147,0,
255,143,0,
255,139,0,
255,135,0,
255,131,0,
255,127,0,
255,123,0,
255,119,0,
255,115,0,
255,111,0,
255,107,0,
255,103,0,
255,99,0,
255,95,0,
255,91,0,
255,87,0,
255,83,0,
255,79,0,
255,75,0,
255,71,0,
255,67,0,
255,63,0,
255,59,0,
255,55,0,
255,51,0,
255,47,0,
255,43,0,
255,39,0,
255,35,0,
255,31,0,
255,27,0,
255,23,0,
255,19,0,
255,15,0,
255,11,0,
255,7,0,
255,3,0,
255,0,0,
250,0,0,
246,0,0,
241,0,0,
237,0,0,
233,0,0,
228,0,0,
224,0,0,
219,0,0,
215,0,0,
211,0,0,
206,0,0,
202,0,0,
197,0,0,
193,0,0,
189,0,0,
184,0,0,
180,0,0,
175,0,0,
171,0,0,
167,0,0,
162,0,0,
158,0,0,
153,0,0,
149,0,0,
145,0,0,
140,0,0,
136,0,0,
131,0,0,
127,0,0
};

View File

@ -14,30 +14,79 @@
#include "H5IM.h"
#include "pal_rgb.h"
#include <stdlib.h>
#include <string.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 ];
#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 ( H5IMread_image( file_id, "Image2", image_out2 ) < 0 )
if (width!=WIDTH)
goto out;
if (height!=HEIGHT)
goto out;
if (planes!=3)
goto out;
for (i = 0; i < height*width*planes; i++) {
if ( image_in2[i] != image_out2[i] ) {
/* read image */
if (H5IMread_image(fid,"image2",image_out2)<0)
goto out;
/* 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( file_id, "Image1", &npals ) < 0 )
if (H5IMget_npalettes(fid,"image1",&npals)<0)
goto out;
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 ( H5IMis_image( file_id, "Image2" ) < 0 )
if (is_image!=1)
goto out;
if ((is_image=H5IMis_image(fid,"image2"))<0)
goto out;
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
*-------------------------------------------------------------------------
*/
/* Close the file. */
if(H5Fclose( file_id ) < 0) goto out;
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();
return 0;
/*-------------------------------------------------------------------------
* 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
*-------------------------------------------------------------------------
*/
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;
}

121851
hl/test/usa.wri Normal file

File diff suppressed because it is too large Load Diff