[svn-r15306] check in a working beta version that only processes HDF5 images

tested: linux, windows
This commit is contained in:
Pedro Vicente Nunes 2008-07-01 14:24:20 -05:00
parent 5c0aa56c83
commit cc7d722bba
3 changed files with 65 additions and 283 deletions

View File

@ -308,22 +308,10 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name)
hsize_t planes;
char interlace[20];
hssize_t npals;
unsigned char* buf=NULL;
H5T_class_t tclass;
hid_t sid;
hid_t did;
hid_t tid;
int rank;
hsize_t dims[H5S_MAX_RANK];
hsize_t maxdim[H5S_MAX_RANK];
size_t size;
hsize_t nelmts;
const char* name;
size_t i;
int j;
int done;
char jpeg_name[1024];
name = image_name;
/* build the jpeg file name */
@ -344,6 +332,8 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name)
if ( H5IMis_image( fid, name ) )
{
unsigned char* buf=NULL;
if ( H5IMget_image_info( fid, name, &width, &height, &planes, interlace, &npals ) < 0 )
goto out;
@ -369,7 +359,7 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name)
}
/*-------------------------------------------------------------------------
* HDF5 Image palette, ignore
*-------------------------------------------------------------------------
@ -386,103 +376,7 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name)
*/
else
{
unsigned char* image_buffer = NULL;
if (( did = H5Dopen2( fid, name, H5P_DEFAULT )) < 0)
goto out;
if (( sid = H5Dget_space( did )) < 0 )
goto out;
if (( rank = H5Sget_simple_extent_ndims(sid)) < 0 )
goto out;
if (( tid = H5Dget_type( did )) < 0 )
goto out;
if (( tclass = H5Tget_class(tid)) < 0)
goto out;
if ( ( H5T_FLOAT == tclass || H5T_INTEGER == tclass) &&
( rank == 2 ) )
{
if ( H5Sget_simple_extent_dims( sid, dims, maxdim ) < 0 )
goto out;
size = H5Tget_size( tid );
nelmts = 1;
for ( j = 0; j < rank; j++)
{
nelmts *= dims[j];
}
if ( NULL == (buf = HDmalloc( (size_t)nelmts * size )))
goto out;
if ( H5Dread(did,tid,H5S_ALL,H5S_ALL,H5P_DEFAULT,buf) < 0 )
goto out;
height = dims[0];
width = dims[1];
if ( opt.image_type == 0 )
planes = 1;
else if ( opt.image_type == 1 )
planes = 3;
if ( NULL == (image_buffer = HDmalloc( (size_t)nelmts * sizeof (unsigned char) )))
{
goto out;
}
/*-------------------------------------------------------------------------
* convert the data to unsigned char
*
*-------------------------------------------------------------------------
*/
{
double min = DBL_MAX;
double max = -DBL_MAX;
double ratio;
/* search for the minimum and maximum */
for ( i = 0; i < nelmts; i++)
{
if ( buf[i] < min) min = buf[i];
if ( buf[i] > max) max = buf[i];
}
/* converts the data based on the ratio to a 0-255 range */
ratio = (min == max) ? 1.0 : (double)(255.0/(max-min));
for ( i = 0; i < nelmts; i++)
{
image_buffer[i] = (unsigned char)ceil( (( buf[i] - min ) / ratio) );
}
}
/* write the jpeg file */
write_JPEG_file (jpeg_name,
image_buffer,
(int) height,
(int) width,
(int) planes);
free( image_buffer );
free( buf );
buf = NULL;
image_buffer = NULL;
done = 1;
}
H5Sclose(sid);
H5Tclose(tid);
H5Dclose(did);
{
@ -505,17 +399,6 @@ int do_image(hid_t fid, h52jpeg_opt_t opt, const char* image_name)
return 0;
out:
H5E_BEGIN_TRY
{
H5Sclose(sid);
H5Tclose(tid);
H5Dclose(did);
} H5E_END_TRY;
if ( buf != NULL )
free( buf );
return -1;

View File

@ -25,12 +25,8 @@
#define IMAGE2_NAME "image24bitpixel"
#define PAL_NAME "palette"
#define PAL_ENTRIES 256
#define RANK 2
#define HEIGHT 200
#define WIDTH 300
static int make_datasets( hid_t fid );
static int make_images( hid_t fid );
static int read_data(const char* file_name, hsize_t *width, hsize_t *height );
unsigned char *gbuf = 0; /* global buffer for image data */
@ -59,11 +55,7 @@ int main( void )
/* make images */
if ( make_images( fid ) < 0 )
goto out;
/* make images */
if ( make_datasets( fid ) < 0 )
goto out;
/* close the file. */
H5Fclose( fid );
@ -142,99 +134,6 @@ out:
/*-------------------------------------------------------------------------
* Function: make_datasets
*
* Purpose: generate datasets
*
*-------------------------------------------------------------------------
*/
static int make_datasets( hid_t fid )
{
hsize_t width; /* width of image */
hsize_t height; /* height of image */
hid_t sid;
hid_t did;
hsize_t dims[2];
unsigned char *buf;
hsize_t i;
/* read a data file with 8bit data */
if ( read_data( DATA_FILE1, &width, &height ) < 0 )
goto out;
dims[0] = height;
dims[1] = width;
if ((sid = H5Screate_simple(RANK, dims, NULL)) < 0)
{
goto out;
}
/*-------------------------------------------------------------------------
* H5T_NATIVE_SHORT
*-------------------------------------------------------------------------
*/
if (NULL == (buf = HDmalloc( (size_t)width * (size_t)height * sizeof(short) )))
goto out;
for ( i = 0; i < height * width; i++)
{
buf[i] = gbuf[i];
}
if ((did = H5Dcreate2(fid, "short", H5T_NATIVE_SHORT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
if (H5Dwrite(did, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
goto out;
if (H5Dclose(did)< 0)
goto out;
free( buf );
/*-------------------------------------------------------------------------
* H5T_NATIVE_INT
*-------------------------------------------------------------------------
*/
if (NULL == (buf = HDmalloc( (size_t)width * (size_t)height * sizeof(int) )))
goto out;
for ( i = 0; i < height * width; i++)
{
buf[i] = gbuf[i];
}
if ((did = H5Dcreate2(fid, "int", H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
if (H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
goto out;
if (H5Dclose(did)< 0)
goto out;
free( buf );
/* close */
H5Sclose(sid);
return 0;
out:
H5E_BEGIN_TRY
{
H5Sclose(sid);
H5Dclose(did);
} H5E_END_TRY;
return -1;
}
/*-------------------------------------------------------------------------
* read_data
* utility function to read ASCII image data
@ -256,65 +155,65 @@ 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;
}