Image with float negative numbers

Hi,

I would like to know if there is a straightforward way to represent a 2 dimensional dataset
of float values (that can be negative) as an image (8bit or else).
Until now, I have to create a 8bit image (with function H5IMmake_image_8bit) by applying a prior
"255 * (data[i] - datamin ) ) / (datamax - datamin )" conversion on each value of the data array.
With this method I am also loosing the float precision on the result (float to int cast)...
Thanks
Daniel

Hello Daniel,

I'll take shot at this, knowing others may jump in with alternate
perspectives. (e.g. there may be simpler ways to accomplish this). In
general, HDF5 is extremely well suited for representing 2D datasets
of the type you describe. These would be typically defined using the
native datatype you mention (FLOAT32 in this case), as a numeric, 2D
dataset. You mentioned using the H5IMmake_image_8bit() interface in
your note, but I couldn't tell if you needed the final dataset to be
interpreted as a true graphic image or if you were using that
interface purely as a means to store the original data. I'd suggest
storing the dataset in its native dataset, to preserve precision etc,
and then if you need to express that data as an image, level-slice it
into a 16 bit or 8 bit representation for visualization later...

Here is a brief ANSI C code snippet that illustrates some of this,
using the little-endian float32 datatype as an example:

/* quick and dirty range-wise random number generator */
#define RANDOM_RANGE(lo,hi) (((lo)-1.0)+(rand()%(abs((hi)-(lo))+1.0)))

herr_t create_2D_dataset(const char *currHdf5Path,const char
*datasetName, const int32 nRows,const int 32 nColumns)
{
float *dataArray_p=NULL;
float loVal = 0.0;
float hiVal = 4567.89 ;
hid_t dataspaceID;
hid_t fileID = 0 ;
hid_t rank = 2;
hid_t dtasetID = 0 ;
hid_t dcpl = 0 ;
herr_t h5_status =0;
int32 dims[2] = {0};
int dataType = H5T_IEEE_F32LE ;

int i=0;
      dims[0] = nRows;
      dims[1] = nColumns;
      nElem = nRows * nColumns ;
      dataArray_p=(float *)calloc(sizeof(float),nElem);

       /* create HDF5 file level container */
       fileID = H5Fcreate(currHdf5Path, H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT);
       /* create a simple 2D dataspace -- of rank 2 -- using
dimensions in dims[] */
       dataspaceID = H5Screate_simple(rank, dims, NULL);
       /* create a dataset property list object */
       dcpl = H5Pcreate(H5P_DATASET_CREATE);
       /* create the 2D numeric dataset itself */
       dtasetID = H5Dcreate(fileID,
                              datasetName, /* e.g.
/ThisGroup/ThisDataset etc etc. */
                              inithdf5DatatypeToH5T("float32"),/*
                              dataspaceID,
                              H5P_DEFAULT,
                              dcpl, /* here we specify an explicit
shared property list */
                              H5P_DEFAULT);

            /* for illustration only, generate a synthetic float32 dataset */
            for(i=0; i < nElem; i++)
             dataArray_p[i] = (float)RANDOM_RANGE(loVal,hiVal);
            /* write out dataset in one call */
            h5_status = H5Dwrite(dtasetID,
                         H5T_IEEE_F32LE,H5S_ALL,H5S_ALL,H5P_DEFAULT,
dataArray_p) ;

            free(dataArray_p);

/* I've left out all the proper object close out logic here... */
/* return some status... */
return h5_status;
} /* end::create_2D_dataset() */

···

from: H5Tpublic.h header */

On Wed, Sep 7, 2011 at 3:27 AM, Sentenac Daniel <dsentenac@interfree.it> wrote:

Hi,

I would like to know if there is a straightforward way to represent a 2
dimensional dataset
of float values (that can be negative) as an image (8bit or else).
Until now, I have to create a 8bit image (with function
H5IMmake_image_8bit) by applying a prior
"255 * (data[i] - datamin ) ) / (datamax - datamin )" conversion on each
value of the data array.
With this method I am also loosing the float precision on the result (float
to int cast)...
Thanks
Daniel

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

--
------------------------------------------------------
Joseph Glassy
Lead Software Engineer (contractor)
NASA Measures (Freeze/Thaw),Rm CFC 429
College of Forestry and Conservation
Univ. Montana, Missoula, MT 59812