herr_t H5IMunlink_palette( hid_t loc_id,
                          const char *image_name,
                          const char *pal_name )
{
    hid_t       did;
    hid_t       atid;
    hid_t       aid;
    H5T_class_t aclass;
    int         ok_pal, has_pal;

    /* check the arguments */
    if(image_name == NULL) 
      return -1;
    if(pal_name == NULL) 
      return -1;

    /* Try to find the palette dataset */
    has_pal = H5LTfind_dataset( loc_id, pal_name );

    /* It does not exist. Return */
    if ( has_pal == 0 )
        return -1;

    /* The image dataset may or not have the attribute "PALETTE"
    * First we try to open to see if it is already there; if not, it is created.
    * If it exists, the array of references is extended to hold the reference
    * to the new palette
    */

    /* First we get the image id */
    if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "PALETTE" on the >>image<< dataset */
    ok_pal = H5LT_find_attribute(did, "PALETTE");

    /* It does not exist. Nothing to do */
    if(ok_pal == 0)
        return -1;

    /* The attribute exists, open it */
    else if(ok_pal ==  1)
    {
        if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if((aclass = H5Tget_class(atid)) < 0)
            goto out;

        /* Check if it is really a reference */
        if(aclass == H5T_REFERENCE)
        {
            /* Delete the attribute */
            if(H5Adelete(did, "PALETTE") < 0)
                goto out;

        }  /* H5T_REFERENCE */

        if(H5Tclose(atid) < 0)
            goto out;

        /* Close the attribute. */
        if(H5Aclose(aid) < 0)
            goto out;

    } /* ok_pal */

    /* Close the image dataset. */
    if(H5Dclose(did) < 0)
        return -1;

    return 0;

out:
    H5Dclose( did );
    return -1;
}
herr_t H5IMlink_palette( hid_t loc_id,
                        const char *image_name,
                        const char *pal_name )

{
    hid_t       did;
    hid_t       atid=-1;
    hid_t       aid=-1;
    hid_t       asid=-1;
    hobj_ref_t  ref;         /* write a new reference */
    hobj_ref_t  *refbuf;     /* buffer to read references */
    hssize_t    n_refs;
    hsize_t     dim_ref;
    int         ok_pal;

    
    /* check the arguments */
    if (image_name == NULL) 
      return -1;
    if (pal_name == NULL) 
      return -1;

    /* The image dataset may or may not have the attribute "PALETTE"
    * First we try to open to see if it is already there; if not, it is created.
    * If it exists, the array of references is extended to hold the reference
    * to the new palette
    */

    /* First we get the image id */
    if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "PALETTE" on the >>image<< dataset */
    ok_pal = H5LT_find_attribute( did, "PALETTE" );

    /*-------------------------------------------------------------------------
    * It does not exist. We create the attribute and one reference
    *-------------------------------------------------------------------------
    */
    if(ok_pal == 0 )
    {
        if((asid = H5Screate(H5S_SCALAR)) < 0)
            goto out;

        /* Create the attribute type for the reference */
        if((atid = H5Tcopy(H5T_STD_REF_OBJ)) < 0)
            goto out;

        /* Create the attribute "PALETTE" to be attached to the image*/
        if((aid = H5Acreate2(did, "PALETTE", atid, asid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto out;

        /* Create a reference. The reference is created on the local id.  */
        if(H5Rcreate(&ref, loc_id, pal_name, H5R_OBJECT, (hid_t)-1) < 0)
            goto out;

        /* Write the attribute with the reference */
        if(H5Awrite(aid, atid, &ref) < 0)
            goto out;

        /* close */
        if(H5Sclose(asid) < 0)
            goto out;
        if(H5Tclose(atid) < 0)
            goto out;
        if(H5Aclose(aid) < 0)
            goto out;

    }

    /*-------------------------------------------------------------------------
    * The attribute already exists, open it
    *-------------------------------------------------------------------------
    */
    else if(ok_pal ==  1)
    {
        if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if(H5Tget_class(atid) < 0)
            goto out;

        /* Get and save the old reference(s) */
        if((asid = H5Aget_space(aid)) < 0)
            goto out;

        n_refs = H5Sget_simple_extent_npoints(asid);

        dim_ref = (hsize_t)n_refs + 1;

        refbuf = (hobj_ref_t*)HDmalloc( sizeof(hobj_ref_t) * (size_t)dim_ref );

        if ( H5Aread( aid, atid, refbuf ) < 0)
            goto out;

        /* The attribute must be deleted, in order to the new one can reflect the changes*/
        if(H5Adelete(did, "PALETTE") < 0)
            goto out;

        /* Create a new reference for this palette. */
        if ( H5Rcreate( &ref, loc_id, pal_name, H5R_OBJECT, (hid_t)-1 ) < 0)
            goto out;

        refbuf[n_refs] = ref;

        /* Create the data space for the new references */
        if(H5Sclose(asid) < 0)
            goto out;

        if((asid = H5Screate_simple(1, &dim_ref, NULL)) < 0)
            goto out;

        /* Create the attribute again with the changes of space */
        if(H5Aclose(aid) < 0)
            goto out;

        if((aid = H5Acreate2(did, "PALETTE", atid, asid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            goto out;

        /* Write the attribute with the new references */
        if(H5Awrite(aid, atid, refbuf) < 0)
            goto out;

        /* close */
        if(H5Sclose(asid) < 0)
            goto out;
        if(H5Tclose(atid) < 0)
            goto out;
        if(H5Aclose(aid) < 0)
            goto out;

        HDfree( refbuf );

    } /* ok_pal ==  1 */

    /* Close the image dataset. */
    if ( H5Dclose( did ) < 0)
        return -1;

    return 0;

out:
    H5Dclose( did );
    H5Sclose( asid );
    H5Tclose( atid );
    H5Aclose( aid );
    return -1;
}
herr_t H5IMis_palette( hid_t loc_id,
                      const char *dset_name )
{
    hid_t      did;
    int        has_class;
    hid_t      atid;
    hid_t      aid = -1;
    char*      attr_data;    /* Name of attribute */
    hsize_t    storage_size; /* Size of storage for attribute */
    herr_t     ret;

    /* check the arguments */
    if (dset_name == NULL) 
      return -1;

    /* Assume initially fail condition */
    ret = -1;

    /* Open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "CLASS" on the dataset */
    has_class = H5LT_find_attribute(did, "CLASS");

    if(has_class ==  0)
    {
        H5Dclose( did );
        return 0;
    }
    else if(has_class ==  1)
    {

        if((aid = H5Aopen(did, "CLASS", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

	/* check to make sure attribute is a string */
	if(H5T_STRING != H5Tget_class(atid))
	    goto out;

	/* check to make sure string is null-terminated */
	if(H5T_STR_NULLTERM != H5Tget_strpad(atid))
	    goto out;

	/* allocate buffer large enough to hold string */
	if((storage_size = H5Aget_storage_size(aid)) == 0)
	    goto out;

	attr_data = (char*)HDmalloc( (size_t)storage_size * sizeof(char) + 1);
	if(attr_data == NULL)
	    goto out;

        if(H5Aread(aid, atid, attr_data) < 0)
            goto out;

        if(HDstrncmp(attr_data, PALETTE_CLASS, MIN(HDstrlen(PALETTE_CLASS),HDstrlen(attr_data))) == 0)
            ret = 1;
        else
            ret = 0;

	HDfree(attr_data);

        if ( H5Tclose( atid ) < 0)
            goto out;

        if ( H5Aclose( aid ) < 0)
            goto out;

    }

    /* Close the dataset. */
    if ( H5Dclose( did ) < 0)
        return -1;

    return ret;

out:
    H5Dclose( did );
    return -1;

}
herr_t H5IMget_image_info( hid_t loc_id,
                          const char *dset_name,
                          hsize_t *width,
                          hsize_t *height,
                          hsize_t *planes,
                          char *interlace,
                          hssize_t *npals )
{
    hid_t       did;
    hid_t       sid;
    hsize_t     dims[IMAGE24_RANK];
    hid_t       aid;
    hid_t       asid;
    hid_t       atid;
    H5T_class_t aclass;
    int         has_pal;
    int         has_attr;

    /* check the arguments */
    if (dset_name == NULL) 
      return -1;
    if (interlace == NULL) 
      return -1;

    /*assume initially we have no palettes attached*/
    *npals = 0;

    /* Open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "INTERLACE_MODE" on the >>image<< dataset */
    has_attr = H5LT_find_attribute(did, "INTERLACE_MODE");

    /* It exists, get it */
    if(has_attr == 1)
    {

        if((aid = H5Aopen(did, "INTERLACE_MODE", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if(H5Aread(aid, atid, interlace) < 0)
            goto out;

        if(H5Tclose(atid) < 0)
            goto out;

        if(H5Aclose(aid) < 0)
            goto out;
    }

    /* Get the dataspace handle */
    if ( (sid = H5Dget_space( did )) < 0)
        goto out;

    /* Get dimensions */
    if ( H5Sget_simple_extent_dims( sid, dims, NULL) < 0)
        goto out;

    /* Initialize the image dimensions */

    if ( has_attr == 1 )
        /* This is a 24 bit image */
    {

      if ( HDstrncmp( interlace, "INTERLACE_PIXEL", 15 ) == 0 )
        {
            /* Number of color planes is defined as the third dimension */
            *height = dims[0];
            *width  = dims[1];
            *planes = dims[2];
        }
        else
	  if ( HDstrncmp( interlace, "INTERLACE_PLANE", 15 ) == 0 )
            {
                /* Number of color planes is defined as the first dimension */
                *planes = dims[0];
                *height = dims[1];
                *width  = dims[2];
            }
	  else return -1;
    }
    else
        /* This is a 8 bit image */
    {
        *height = dims[0];
        *width  = dims[1];
        *planes = 1;
    }

    /* Close */
    if ( H5Sclose( sid ) < 0)
        goto out;


    /* Get number of palettes */


    /* Try to find the attribute "PALETTE" on the >>image<< dataset */
    has_pal = H5IM_find_palette(did);

    if(has_pal ==  1)
    {

        if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if((aclass = H5Tget_class(atid)) < 0)
            goto out;

        /* Check if it is really a reference */

        if(aclass == H5T_REFERENCE)
        {

            /* Get the reference(s) */

            if ( (asid = H5Aget_space( aid )) < 0)
                goto out;

            *npals = H5Sget_simple_extent_npoints( asid );

            if ( H5Sclose( asid ) < 0)
                goto out;

        } /* H5T_REFERENCE */

        if ( H5Tclose( atid ) < 0)
            goto out;

        /* Close the attribute. */
        if ( H5Aclose( aid ) < 0)
            goto out;

    }

    /* End access to the dataset and release resources used by it. */
    if ( H5Dclose( did ) < 0)
        goto out;

    return 0;

out:
    H5Dclose( did );
    H5Aclose( aid );
    H5Sclose( asid );
    H5Tclose( atid );
    return -1;

}
Esempio n. 5
0
File: H5IM.c Progetto: Andy-Sun/VTK
herr_t H5IMis_palette( hid_t loc_id,
                      const char *dset_name )
{
    hid_t      did;
    int        has_class;
    hid_t      atid;
    hid_t      aid;
    char       attr_data[20];
    herr_t     ret;

    /* Assume initially fail condition */
    ret = -1;

    /* Open the dataset. */
    if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
        return -1;

    /* Try to find the attribute "CLASS" on the dataset */
    has_class = H5LT_find_attribute(did, "CLASS");

    if(has_class ==  0)
    {
        H5Dclose( did );
        return 0;
    }
    else if(has_class ==  1)
    {

        if((aid = H5Aopen(did, "CLASS", H5P_DEFAULT)) < 0)
            goto out;

        if((atid = H5Aget_type(aid)) < 0)
            goto out;

        if(H5Tget_class(atid) < 0)
            goto out;

        if(H5Aread(aid, atid, attr_data) < 0)
            goto out;

        if(strcmp(attr_data, PALETTE_CLASS) == 0)
            ret = 1;
        else
            ret = 0;

        if ( H5Tclose( atid ) < 0)
            goto out;

        if ( H5Aclose( aid ) < 0)
            goto out;

    }

    /* Close the dataset. */
    if ( H5Dclose( did ) < 0)
        return -1;

    return ret;

out:
    H5Dclose( did );
    return -1;

}