/*------------------------------------------------------------------------- * Function: H5IM_get_palette * * Purpose: private function that reads a palette to memory type TID * * Return: Success: 0, Failure: -1 * * Programmer: Pedro Vicente Nunes, [email protected] * * Date: May 10, 2005 * * Comments: * This function allows reading of an 8bit palette from disk disk * to memory type TID * The memory datatype can be H5T_NATIVE_INT or H5T_NATIVE_UCHAR currently. * the H5T_NATIVE_INT is supposed to be called from * the FORTRAN interface where the image buffer is defined as type "integer" * * Comments: * based on HDF5 Image and Palette Specification * http://hdf.ncsa.uiuc.edu/HDF5/H5Image/ImageSpec.html * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5IM_get_palette(hid_t loc_id, const char *image_name, int pal_number, hid_t tid, void *pal_data) { hid_t image_id; int has_pal; hid_t attr_type; hid_t attr_id; hid_t attr_space_id; hid_t attr_class; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; /* Open the dataset. */ if((image_id = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(image_id); if(has_pal == 1) { if((attr_id = H5Aopen(image_id, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((attr_type = H5Aget_type(attr_id)) < 0) goto out; if((attr_class = H5Tget_class(attr_type)) < 0) goto out; /* Check if it is really a reference */ if(attr_class == H5T_REFERENCE) { /* Get the reference(s) */ if((attr_space_id = H5Aget_space(attr_id)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(attr_space_id); dim_ref = n_refs; refbuf = malloc(sizeof(hobj_ref_t) * (int)dim_ref); if(H5Aread(attr_id, attr_type, refbuf) < 0) goto out; /* Get the palette id */ if((pal_id = H5Rdereference(image_id, H5R_OBJECT, &refbuf[pal_number])) < 0) goto out; /* Read the palette dataset using the memory type TID */ if(H5Dread(pal_id, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0) goto out; if(H5Sclose(attr_space_id) < 0) goto out; /* close the dereferenced dataset */ if(H5Dclose(pal_id) < 0) goto out; free(refbuf); } /* H5T_REFERENCE */ if(H5Tclose(attr_type) < 0) goto out; /* Close the attribute. */ if(H5Aclose(attr_id) < 0) goto out; } /* Close the image dataset. */ if(H5Dclose(image_id) < 0) return -1; return 0; out: H5Dclose(image_id); return -1; }
herr_t H5IMget_palette_info( hid_t loc_id, const char *image_name, int pal_number, hsize_t *pal_dims ) { hid_t did; int has_pal; hid_t atid=-1; hid_t aid; hid_t asid=-1; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; hid_t pal_space_id; hsize_t pal_maxdims[2]; /* check the arguments */ if (image_name == NULL) return -1; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* 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(H5Tget_class(atid) < 0) goto out; /* Get the reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = (hsize_t)n_refs; refbuf = (hobj_ref_t*)HDmalloc( sizeof(hobj_ref_t) * (size_t)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* Get the actual palette */ if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0) goto out; if ( (pal_space_id = H5Dget_space( pal_id )) < 0) goto out; if ( H5Sget_simple_extent_ndims( pal_space_id ) < 0) goto out; if ( H5Sget_simple_extent_dims( pal_space_id, pal_dims, pal_maxdims ) < 0) goto out; /* close */ if (H5Dclose(pal_id)<0) goto out; if ( H5Sclose( pal_space_id ) < 0) goto out; if ( H5Sclose( asid ) < 0) goto out; if ( H5Tclose( atid ) < 0) goto out; if ( H5Aclose( aid ) < 0) goto out; HDfree( refbuf ); } /* 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 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; }
herr_t H5IMget_npalettes( hid_t loc_id, const char *image_name, hssize_t *npals ) { hid_t did; hid_t atid; hid_t aid; hid_t asid; H5T_class_t aclass; int has_pal; /* check the arguments */ if(image_name == NULL) return -1; /*assume initially we have no palettes attached*/ *npals = 0; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* 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) { 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; } /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); return -1; }
herr_t H5IMget_palette( hid_t loc_id, const char *image_name, int pal_number, unsigned char *pal_data ) { hid_t did; int has_pal; hid_t atid=-1; hid_t aid; hid_t asid=-1; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* 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(H5Tget_class(atid) < 0) goto out; /* Get the reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = n_refs; refbuf = (hobj_ref_t*)malloc( sizeof(hobj_ref_t) * (int)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* Get the palette id */ if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0) goto out; /* Read the palette dataset */ if ( H5Dread( pal_id, H5Dget_type(pal_id), H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data ) < 0) goto out; /* close */ if (H5Dclose(pal_id)<0) goto out; if ( H5Sclose( asid ) < 0) goto out; if ( H5Tclose( atid ) < 0) goto out; if ( H5Aclose( aid ) < 0) goto out; free( refbuf ); } /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); H5Sclose( asid ); H5Tclose( atid ); H5Aclose( aid ); return -1; }