File: label.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (223 lines) | stat: -rw-r--r-- 4,746 bytes parent folder | download | duplicates (4)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

/**
 * \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

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);
}

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_ERROR);
    }

    if (strlen(name) > MI_LABEL_MAX) {
        return (MI_ERROR);
    }

    if (volume->volume_class != MI_CLASS_LABEL) {
	return (MI_ERROR);
    }

    if (volume->ftype_id <= 0 || volume->mtype_id <= 0) {
	return (MI_ERROR);
    }

    result = H5Tenum_insert(volume->mtype_id, name, &value);
    if (result < 0) {
	return (MI_ERROR);
    }

    /* We might have to swap these values before adding them to
     * the file type.
     */
    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;
        }
    }
    result = H5Tenum_insert(volume->ftype_id, name, &value);
    if (result < 0) {
	return (MI_ERROR);
    }

    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_ERROR);
    }

    if (volume->volume_class != MI_CLASS_LABEL) {
        return (MI_ERROR);
    }
    if (volume->mtype_id <= 0) {
        return (MI_ERROR);
    }
    *name = malloc(MI_LABEL_MAX);
    if (*name == NULL) {
        return (MI_ERROR);
    }

    H5E_BEGIN_TRY {
        result = H5Tenum_nameof(volume->mtype_id, &value, *name, MI_LABEL_MAX);
    } H5E_END_TRY;

    if (result < 0) {
	return (MI_ERROR);
    }
    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_ERROR);
    }

    if (volume->volume_class != MI_CLASS_LABEL) {
        return (MI_ERROR);
    }

    if (volume->mtype_id <= 0) {
        return (MI_ERROR);
    }

    H5E_BEGIN_TRY {
        result = H5Tenum_valueof(volume->mtype_id, name, value_ptr);
    } H5E_END_TRY;

    if (result < 0) {
	return (MI_ERROR);
    }
    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_ERROR);
  }
  if (volume->volume_class != MI_CLASS_LABEL) {
    return (MI_ERROR);
  }

  if (volume->mtype_id <= 0) {
    return (MI_ERROR);
  }

  H5E_BEGIN_TRY {
    result = H5Tget_nmembers(volume->mtype_id);
  } H5E_END_TRY;

  if (result < 0) {
    return (MI_ERROR);
  }
  else {
    *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_ERROR);
  }
  if (volume->volume_class != MI_CLASS_LABEL) {
    return (MI_ERROR);
  }
  
  if (volume->mtype_id <= 0) {
    return (MI_ERROR);
  }

  H5E_BEGIN_TRY {
    result = H5Tget_member_value(volume->mtype_id,idx,value);
  } H5E_END_TRY;
   
  if (result < 0) {
    return (MI_ERROR);
  }

  return (MI_NOERROR);
}