File: slice.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 (439 lines) | stat: -rw-r--r-- 11,789 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
 * \file slice.c
 * \brief MINC 2.0 Slice/volume scale functions
 * \author Bert Vincent
 *
 * These functions get and set the real minimum and maximum values for
 * either a particular slice or an entire volume.
 *
 * Each of the slice scale functions take an array of long integer
 * coordinates to specify the slice to consider.  The order of these
 * coordinates is always raw file (voxel) order, rather than the
 * apparent order.
 *
 * If slice scaling is not enabled, the slice scaling functions will
 * use the appropriate global volume scale value.
 ************************************************************************/
#include <stdlib.h>
#include <hdf5.h>
#include "minc2.h"
#include "minc2_private.h"

#define MIRW_SCALE_SET 0x0001
#define MIRW_SCALE_GET 0x0000

#define MIRW_SCALE_MIN 0x0002
#define MIRW_SCALE_MAX 0x0000

/* Forward declaration
 */
static int mirw_volume_minmax(int opcode, mihandle_t volume, double *value);

/** Get the minimum or maximum value for the slice containing the given point.
 */
static int
mirw_slice_minmax(int opcode, mihandle_t volume, 
                  const unsigned long start_positions[],
                  int array_length, double *value)
{
    hid_t dset_id;
    hid_t fspc_id;
    hid_t mspc_id;
    hssize_t hdf_start[MI2_MAX_VAR_DIMS];
    hsize_t hdf_count[MI2_MAX_VAR_DIMS];
    unsigned long count[MI2_MAX_VAR_DIMS];
    int dir[MI2_MAX_VAR_DIMS];
    int ndims;
    int i;
    int result;

    if (volume == NULL || value == NULL) {
        return (MI_ERROR);      /* Bad parameters */
    }

    if (!volume->has_slice_scaling) {
        return mirw_volume_minmax(opcode, volume, value);
    }

    if (opcode & MIRW_SCALE_MIN) {
        dset_id = volume->imin_id;
    }
    else {
        dset_id = volume->imax_id;
    }

    fspc_id = H5Dget_space(dset_id);
    if (fspc_id < 0) {
	return (MI_ERROR);
    }

    ndims = H5Sget_simple_extent_ndims(fspc_id);
    if (ndims > array_length) {
	ndims = array_length;
    }

    for (i = 0; i < ndims; i++) {
        count[i] = 1;
    }

    mitranslate_hyperslab_origin(volume,
                                 start_positions,
                                 count,
                                 hdf_start,
                                 hdf_count,
                                 dir);
                                 
    result = H5Sselect_elements(fspc_id, H5S_SELECT_SET, 1, 
				(const hsize_t **) &hdf_start);
    if (result < 0) {
	return (MI_ERROR);
    }

    /* Create a trivial scalar space to read the single value.
     */
    mspc_id = H5Screate(H5S_SCALAR);

    if (opcode & MIRW_SCALE_SET) {
	result = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, mspc_id, fspc_id, 
			  H5P_DEFAULT, value);
    }
    else {
	result = H5Dread(dset_id, H5T_NATIVE_DOUBLE, mspc_id, fspc_id, 
			 H5P_DEFAULT, value);
    }

    if (result < 0) {
	return (MI_ERROR);
    }

    H5Sclose(fspc_id);
    H5Sclose(mspc_id);
    return (MI_NOERROR);
}

/**
This function sets \a slice_min to the minimum real value of
voxels in the slice containing the coordinates \a start_positions.
The \a array_length may be less than or equal to the number of dimensions
in the volume, extra coordinates will be ignored.  Specifying too few
coordinates will trigger an error.
Coordinates must always be specified in raw file order.
 */
int
miget_slice_min(mihandle_t volume, const unsigned long start_positions[],
		int array_length, double *slice_min)
{
    return (mirw_slice_minmax(MIRW_SCALE_MIN + MIRW_SCALE_GET, 
			      volume, start_positions, 
			      array_length, slice_min));
}

/**
This function sets \a slice_max to the maximum real value of
voxels in the slice containing the coordinates \a start_positions.
The \a array_length may be less than or equal to the number of dimensions
in the volume, extra coordinates will be ignored.  Specifying too few
coordinates will trigger an error.
Coordinates must always be specified in raw file order.
 */
int
miget_slice_max(mihandle_t volume, const unsigned long start_positions[],
		int array_length, double *slice_max)
{
    return (mirw_slice_minmax(MIRW_SCALE_MAX + MIRW_SCALE_GET, 
			      volume, start_positions, 
			      array_length, slice_max));
}

/**
This function sets minimum real value of
values in the slice containing the coordinates \a start_positions.
The \a array_length may be less than or equal to the number of dimensions
in the volume, extra coordinates will be ignored.  Specifying too few
coordinates will trigger an error.
Coordinates must always be specified in raw file order.
 */
int
miset_slice_min(mihandle_t volume, const unsigned long start_positions[],
		int array_length, double slice_min)
{
    return (mirw_slice_minmax(MIRW_SCALE_MIN + MIRW_SCALE_SET, 
			      volume, start_positions, 
			      array_length, &slice_min));
}

/**
This function sets maximum real value of
values in the slice containing the coordinates \a start_positions.
The \a array_length may be less than or equal to the number of dimensions
in the volume, extra coordinates will be ignored.  Specifying too few
coordinates will trigger an error.
Coordinates must always be specified in raw file order.
 */
int
miset_slice_max(mihandle_t volume, const unsigned long start_positions[],
		int array_length, double slice_max)
{
    return (mirw_slice_minmax(MIRW_SCALE_MAX + MIRW_SCALE_SET, 
			      volume, start_positions, 
			      array_length, &slice_max));
}

/**
This function gets both the minimum and
maximum real value of voxels in the slice containing the coordinates
\a start_positions.  The \a array_length may be less than or equal to
the number of dimensions in the volume, extra coordinates will be
ignored.  Specifying too few coordinates will trigger an error.
Coordinates must always be specified in raw file order.
 */
int
miget_slice_range(mihandle_t volume, const unsigned long start_positions[],
		  int array_length, double *slice_max, double *slice_min)
{
    int r;

    r = mirw_slice_minmax(MIRW_SCALE_MAX + MIRW_SCALE_GET, 
			  volume, start_positions, 
			  array_length, slice_max);
    if (r < 0) {
        *slice_max = 1.0;
    }

    r = mirw_slice_minmax(MIRW_SCALE_MIN + MIRW_SCALE_GET,
			  volume, start_positions,
			  array_length, slice_min);

    if (r < 0) {
        *slice_min = 0.0;
    }

    return (MI_NOERROR);
}

/**
This function the minimum and maximum real value of voxels in the
slice containing the coordinates \a start_positions.  The \a
array_length may be less than or equal to the number of dimensions in
the volume, extra coordinates will be ignored.  Specifying too few
coordinates will trigger an error.  Coordinates must always be
specified in raw file order.
 */
int
miset_slice_range(mihandle_t volume, const unsigned long start_positions[],
		  int array_length, double slice_max, double slice_min)
{
    int r;

    r = mirw_slice_minmax(MIRW_SCALE_MAX + MIRW_SCALE_SET, 
			  volume, start_positions, 
			  array_length, &slice_max);
    if (r < 0) {
	return (MI_ERROR);
    }

    r = mirw_slice_minmax(MIRW_SCALE_MIN + MIRW_SCALE_SET,
			  volume, start_positions,
			  array_length, &slice_min);

    if (r < 0) {
	return (MI_ERROR);
    }

    return (MI_NOERROR);
}

/*! Internal function to read/write the volume global minimum or
 * maximum real range.
 */
static int
mirw_volume_minmax(int opcode, mihandle_t volume, double *value)
{
    hid_t dset_id;
    hid_t fspc_id;
    hid_t mspc_id;
    int result;

    if (volume == NULL || value == NULL) {
        return (MI_ERROR);
    }
    if (volume->has_slice_scaling) {
	return (MI_ERROR);
    }
    if ((opcode & MIRW_SCALE_SET) == 0) {
        if (opcode & MIRW_SCALE_MIN) {
            *value = volume->scale_min;
            return (MI_NOERROR);
        }
        else {
            *value = volume->scale_max;
            return (MI_NOERROR);
        }
    }
    if (opcode & MIRW_SCALE_MIN) {
        dset_id = volume->imin_id;
    }
    else {
        dset_id = volume->imax_id;
    }

    fspc_id = H5Dget_space(dset_id);
    if (fspc_id < 0) {
	return (MI_ERROR);
    }

    /* Make certain the value is a scalar.
     */
    if (H5Sget_simple_extent_ndims(fspc_id) != 0) {
	return (MI_ERROR);
    }

    /* Create a trivial scalar space to read the single value.
     */
    mspc_id = H5Screate(H5S_SCALAR);

    if (opcode & MIRW_SCALE_SET) {
	result = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, mspc_id, fspc_id, 
			  H5P_DEFAULT, value);
    }
    else {
	result = H5Dread(dset_id, H5T_NATIVE_DOUBLE, mspc_id, fspc_id, 
			 H5P_DEFAULT, value);
    }

    if (result < 0) {
	return (MI_ERROR);
    }

    /* Update the "cached" values.
     */
    if (opcode & MIRW_SCALE_MIN) {
        volume->scale_min = *value;
    }
    else {
        volume->scale_max = *value;
    }

    H5Sclose(fspc_id);
    H5Sclose(mspc_id);
    return (MI_NOERROR);
}

/**
This function returns the minimum real value of
voxels in the entire \a volume.  If per-slice scaling is enabled, this
function will return an error.
 */
int
miget_volume_min(mihandle_t volume, double *vol_min)
{
    return (mirw_volume_minmax(MIRW_SCALE_MIN + MIRW_SCALE_GET, 
                               volume, vol_min));
}

/**
This function returns the maximum real value of
voxels in the entire \a volume.  If per-slice scaling is enabled, this
function will return an error.
 */
int
miget_volume_max(mihandle_t volume, double *vol_max)
{
    return (mirw_volume_minmax(MIRW_SCALE_MAX + MIRW_SCALE_GET, 
                               volume, vol_max));
}

/**
This function sets the minimum real value of
voxels in the entire \a volume.  If per-slice scaling is enabled, this
function will return an error.
 */
int
miset_volume_min(mihandle_t volume, double vol_min)
{
    return (mirw_volume_minmax(MIRW_SCALE_MIN + MIRW_SCALE_SET, 
			       volume, &vol_min));
}

/**
This function sets the maximum real value of
voxels in the entire \a volume.  If per-slice scaling is enabled, this
function will return an error.
 */
int
miset_volume_max(mihandle_t volume, double vol_max)
{
    return (mirw_volume_minmax(MIRW_SCALE_MAX + MIRW_SCALE_SET, 
			       volume, &vol_max));
}

/**
This function retrieves the maximum and minimum real values of
voxels in the entire \a volume.  If per-slice scaling is enabled, this
function will return an error.
 */
int
miget_volume_range(mihandle_t volume, double *vol_max, double *vol_min)
{
    int r;

    r = mirw_volume_minmax(MIRW_SCALE_MAX + MIRW_SCALE_GET, volume, vol_max);
    if (r < 0) {
	return (MI_ERROR);
    }

    r = mirw_volume_minmax(MIRW_SCALE_MIN + MIRW_SCALE_GET, volume, vol_min);
    if (r < 0) {
	return (MI_ERROR);
    }

    return (MI_NOERROR);
}

/**
This function sets the maximum and minimum real values of
voxels in the entire \a volume.  If per-slice scaling is enabled, this
function will return an error.
 */
int
miset_volume_range(mihandle_t volume, double vol_max, double vol_min)
{
    int r;

    r = mirw_volume_minmax(MIRW_SCALE_MAX + MIRW_SCALE_SET, volume, &vol_max);
    if (r < 0) {
	return (MI_ERROR);
    }

    r = mirw_volume_minmax(MIRW_SCALE_MIN + MIRW_SCALE_SET, volume, &vol_min);
    if (r < 0) {
	return (MI_ERROR);
    }

    return (MI_NOERROR);
}

/*! Function to get the volume's slice-scaling flag.
 */
int
miget_slice_scaling_flag(mihandle_t volume, miboolean_t *slice_scaling_flag)
{
    if (volume == NULL || slice_scaling_flag == NULL) {
	return (MI_ERROR);
    }
    *slice_scaling_flag = volume->has_slice_scaling;
    return (MI_NOERROR);
}

/*! Function to set the volume's slice-scaling flag.
 */
int
miset_slice_scaling_flag(mihandle_t volume, miboolean_t slice_scaling_flag)
{
    if (volume == NULL) {
	return (MI_ERROR);
    }
    volume->has_slice_scaling = slice_scaling_flag;
    return (MI_NOERROR);
}