1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/**
* \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>
#include <hdf5.h>
#include "minc2.h"
#include "minc2_private.h"
#define MI_LABEL_MAX 128
static int miswap2(unsigned short tmp)
{
unsigned char *x = (unsigned char *) &tmp;
unsigned char t = x[0];
x[0] = x[1];
x[1] = t;
return (tmp);
}
static int miswap4(unsigned int tmp)
{
unsigned char *x = (unsigned char *) &tmp;
unsigned char t = x[0];
x[0] = x[3];
x[3] = t;
t = x[1];
x[1] = x[2];
x[2] = t;
return (tmp);
}
/**
* 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)
{
int result;
if (volume == NULL || name == NULL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Trying to use null volume or variable");
}
if (strlen(name) > MI_LABEL_MAX) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Label name is too long");
}
if (volume->volume_class != MI_CLASS_LABEL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume class is not label");
}
if (volume->ftype_id <= 0 || volume->mtype_id <= 0) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume not initialized");
}
MI_CHECK_HDF_CALL_RET(result = H5Tenum_insert(volume->mtype_id, name, &value),"H5Tenum_insert");
/* We might have to swap these values before adding them to
* the file type.
*
* COOL! the whole purpose of HDF5 being machine independent is defeated here!
*/
if (H5Tget_order(volume->ftype_id) != H5Tget_order(volume->mtype_id)) {
switch (H5Tget_size(volume->ftype_id)) {
case 2:
value = miswap2((unsigned short) value);
break;
case 4:
value = miswap4((unsigned int) value);
break;
}
}
MI_CHECK_HDF_CALL_RET(result = H5Tenum_insert(volume->ftype_id, name, &value),"H5Tenum_insert");
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)
{
int result;
if (volume == NULL || name == NULL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Trying to use null volume or variable");
}
if (volume->volume_class != MI_CLASS_LABEL) {
MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume class is not label");
}
if (volume->mtype_id <= 0) {
MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume is not initialized");
}
*name = malloc(MI_LABEL_MAX);
if (*name == NULL) {
return MI_LOG_ERROR(MI2_MSG_OUTOFMEM,MI_LABEL_MAX);
}
H5E_BEGIN_TRY {
result = H5Tenum_nameof(volume->mtype_id, &value, *name, MI_LABEL_MAX);
} H5E_END_TRY;
MI_CHECK_HDF_CALL_RET(result,"H5Tenum_nameof");
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)
{
int result;
if (volume == NULL || name == NULL || value_ptr == NULL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Trying to use null volume or variable");
}
if (volume->volume_class != MI_CLASS_LABEL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume class is not label");
}
if (volume->mtype_id <= 0) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume is not initialized");
}
H5E_BEGIN_TRY {
result = H5Tenum_valueof(volume->mtype_id, name, value_ptr);
} H5E_END_TRY;
MI_CHECK_HDF_CALL_RET(result,"H5Tenum_valueof");
return (MI_NOERROR);
}
/**
* This function returns the number of defined labels, if any, or zero.
*/
int miget_number_of_defined_labels(mihandle_t volume, int *number_of_labels)
{
int result;
if (volume == NULL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Trying to use null volume");
}
if (volume->volume_class != MI_CLASS_LABEL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume class is not label");
}
if (volume->mtype_id <= 0) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume is not initialized");
}
H5E_BEGIN_TRY {
result = H5Tget_nmembers(volume->mtype_id);
} H5E_END_TRY;
MI_CHECK_HDF_CALL_RET(result,"H5Tget_nmembers");
*number_of_labels = result;
return (MI_NOERROR);
}
/**
* This function returns the label value associated with an index (0,1,...)
*/
int miget_label_value_by_index(mihandle_t volume, int idx, int *value)
{
int result;
if (volume == NULL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Trying to use null volume");
}
if (volume->volume_class != MI_CLASS_LABEL) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume class is not label");
}
if (volume->mtype_id <= 0) {
return MI_LOG_ERROR(MI2_MSG_GENERIC,"Volume is not initialized");
}
H5E_BEGIN_TRY {
result = H5Tget_member_value(volume->mtype_id,idx,value);
} H5E_END_TRY;
MI_CHECK_HDF_CALL_RET(result,"H5Tget_member_value");
return (MI_NOERROR);
}
/* kate: indent-mode cstyle; indent-width 2; replace-tabs on; */
|