changeset 1629:0af2363760c2

Revised substantially
author bert <bert>
date Sat, 17 Jan 2004 00:22:18 +0000
parents c0dfe9219dc9
children defcdf5993a8
files libsrc2/label.c
diffstat 1 files changed, 84 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/libsrc2/label.c
+++ b/libsrc2/label.c
@@ -1,5 +1,14 @@
-/************************************************************************
- * MINC 2 DATA TYPE/SPACE FUNCTIONS
+/**
+ * \file label.c
+ * \brief MINC 2.0 Label functions
+ * \author Bert Vincent
+ *
+ * This small set of three functions are intended to allow for the
+ * definition of labeled, or enumerated, volumes.
+ * 
+ * Labeled volumes must have been created with the class MI_CLASS_LABEL,
+ * and with any integer subtype.
+ *
  ************************************************************************/
 
 #include <stdlib.h>
@@ -9,119 +18,105 @@
 
 #define MI_LABEL_MAX 128
 
+/**
+This function associates a label name with an integer value for the given
+volume. Functions which read and write voxel values will read/write 
+in integer values, and must call miget_label_name() to discover the 
+descriptive text string which corresponds to the integer value.
+*/
 int 
 midefine_label(mihandle_t volume, int value, const char *name)
 {
-    hid_t file_id;
-    hid_t grp_id;
-    hid_t type_id;
     int result;
 
-    file_id = volume->hdf_id;
-    if (file_id < 0) {
-	return (MI_ERROR);
+    if (volume == NULL || name == NULL) {
+        return (MI_ERROR);
     }
 
-    grp_id = midescend_path(file_id, "/minc-2.0");
-    if (grp_id < 0) {
+    if (strlen(name) > MI_LABEL_MAX) {
+        return (MI_ERROR);
+    }
+
+    if (volume->volume_class != MI_CLASS_LABEL) {
 	return (MI_ERROR);
     }
 
-    type_id = H5Topen(grp_id, "Labels");
-    if (type_id < 0) {
-	type_id = H5Tenum_create(H5T_NATIVE_INT);
-    }
-    else {
-	hid_t new_type_id = H5Tcopy(type_id);
-	H5Tclose(type_id);
-	H5Gunlink(grp_id, "Labels");
-	type_id = new_type_id;
-    }
-
-    if (type_id < 0) {
+    if (volume->type_id <= 0) {
 	return (MI_ERROR);
     }
 
-    result = H5Tenum_insert(type_id, name, &value);
+    result = H5Tenum_insert(volume->type_id, name, &value);
     if (result < 0) {
 	return (MI_ERROR);
     }
 
-    result = H5Tcommit(grp_id, "Labels", type_id);
-    if (result < 0) {
-	return (MI_ERROR);
-    }
-
-    H5Tclose(type_id);
-    H5Gclose(grp_id);
-
     return (MI_NOERROR);
 }
 
+/**
+For a labelled volume, this function retrieves the text name
+associated with a given integer value.
+
+The name pointer returned must be freed by calling mifree_name().
+*/
 int
 miget_label_name(mihandle_t volume, int value, char **name)
 {
-    hid_t file_id;
-    hid_t grp_id;
-    hid_t type_id;
     int result;
 
-    file_id = volume->hdf_id;
-    if (file_id < 0) {
-	return (MI_ERROR);
+    if (volume == NULL || name == NULL) {
+        return (MI_ERROR);
     }
 
-    grp_id = midescend_path(file_id, "/minc-2.0");
-    if (grp_id < 0) {
-	return (MI_ERROR);
+    if (volume->volume_class != MI_CLASS_LABEL) {
+        return (MI_ERROR);
+    }
+    if (volume->type_id <= 0) {
+        return (MI_ERROR);
+    }
+    *name = malloc(MI_LABEL_MAX);
+    if (*name == NULL) {
+        return (MI_ERROR);
     }
 
-    type_id = H5Topen(grp_id, "Labels");
-    if (type_id < 0) {
-	return (MI_ERROR);
-    }
+    H5E_BEGIN_TRY {
+        result = H5Tenum_nameof(volume->type_id, &value, *name, MI_LABEL_MAX);
+    } H5E_END_TRY;
 
-    *name = malloc(MI_LABEL_MAX);
-    result = H5Tenum_nameof(type_id, &value, *name, MI_LABEL_MAX);
     if (result < 0) {
 	return (MI_ERROR);
     }
-
-    H5Tclose(type_id);
-    H5Gclose(grp_id);
     return (MI_NOERROR);
 }
 
+/**
+This function is the inverse of miget_label_name(). It is called to determine
+what integer value, if any, corresponds to the given text string.
+*/
 int
 miget_label_value(mihandle_t volume, const char *name, int *value_ptr)
 {
-    hid_t file_id;
-    hid_t grp_id;
-    hid_t type_id;
     int result;
 
-    file_id = volume->hdf_id;
-    if (file_id < 0) {
-	return (MI_ERROR);
+    if (volume == NULL || name == NULL || value_ptr == NULL) {
+        return (MI_ERROR);
     }
 
-    grp_id = midescend_path(file_id, "/minc-2.0");
-    if (grp_id < 0) {
-	return (MI_ERROR);
+    if (volume->volume_class != MI_CLASS_LABEL) {
+        return (MI_ERROR);
     }
 
-    type_id = H5Topen(grp_id, "Labels");
-    if (type_id < 0) {
-	return (MI_ERROR);
+    if (volume->type_id <= 0) {
+        return (MI_ERROR);
     }
 
-    result = H5Tenum_valueof(type_id, name, value_ptr);
+    H5E_BEGIN_TRY {
+        result = H5Tenum_valueof(volume->type_id, name, value_ptr);
+    } H5E_END_TRY;
+
     if (result < 0) {
 	return (MI_ERROR);
     }
-
-    H5Tclose(type_id);
-    H5Gclose(grp_id);
     return (MI_NOERROR);
 }
 
@@ -139,15 +134,22 @@
     char *name;
     int result;
     int value;
-    
-    /* Turn off automatic error reporting - we'll take care of this
-     * ourselves, thanks!
-     */
-    H5Eset_auto(NULL, NULL);
-    
-    result = micreate_volume("test.h5", 0, NULL, 0, 0, NULL, &hvol);
+    midimhandle_t hdim[3];
+    unsigned long coords[3];
+
+    result = micreate_dimension("xspace", MI_DIMCLASS_SPATIAL, 
+                           MI_DIMATTR_REGULARLY_SAMPLED, 10, &hdim[0]);
+
+    result = micreate_dimension("yspace", MI_DIMCLASS_SPATIAL, 
+                           MI_DIMATTR_REGULARLY_SAMPLED, 10, &hdim[1]);
+
+    result = micreate_dimension("zspace", MI_DIMCLASS_SPATIAL, 
+                           MI_DIMATTR_REGULARLY_SAMPLED, 6, &hdim[2]);
+
+    result = micreate_volume("tst-label.mnc", 3, hdim, MI_TYPE_UINT, 
+                             MI_CLASS_LABEL, NULL, &hvol);
     if (result < 0) {
-	fprintf(stderr, "Unable to create test file %x", result);
+	fprintf(stderr, "Unable to create test file %x\n", result);
 	return (-1);
     }
 
@@ -200,6 +202,15 @@
 	TESTRPT("Invalid return from miget_label_value", result);
     }
 
+    micreate_volume_image(hvol);
+
+    coords[0] = 0;
+    coords[1] = 0;
+    coords[2] = 0;
+    miset_voxel_value(hvol, coords, 3, 0xffffff);
+    coords[2] = 2;
+    miset_voxel_value(hvol, coords, 3, 0x00ff00);
+
     miclose_volume(hvol);
 
     if (error_cnt != 0) {