File: minc_conversion.c

package info (click to toggle)
libminc 2.4.07-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,288 kB
  • sloc: ansic: 57,268; cpp: 3,654; sh: 100; makefile: 23; ruby: 18
file content (426 lines) | stat: -rw-r--r-- 9,577 bytes parent folder | download | duplicates (2)
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
#define _GNU_SOURCE 1
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <minc.h>
#include <minc2.h>
#include <limits.h>
#include <float.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>

#define FUNC_ERROR(x) (fprintf(stderr, "On line %d, function %s failed unexpectedly\n", __LINE__, x), ++errors)

#define TST_X 0
#define TST_Y 1
#define TST_Z 2

static long errors = 0;

extern void icv_tests(void);

#define XSIZE 20
#define YSIZE 30
#define ZSIZE 40

#define YBOOST 10
#define ZBOOST 20

static struct dimdef {
  char * name;
  int length;
} dimtab1[3] = { 
  { MIxspace, XSIZE }, 
  { MIyspace, YSIZE },
  { MIzspace, ZSIZE }
};

struct testinfo {
  char *name;
  int fd;
  int maxid;
  int minid;
  int imgid;
  int dim[3];
  int test_group;
  int test_attribute;
};

/* Test case 1 - file creation & definition. 
 */
static int test1(struct testinfo *ip, struct dimdef *dims, int ndims)
{
  int varid;
  int stat;
  int i;

  printf("test1\n");
  /* Test case #1 - file creation 
   */
  ip->name = micreate_tempfile();
  if (ip->name == NULL) {
    FUNC_ERROR("micreate_tempfile\n");
  }

  printf("%s:%d\n",__FILE__,__LINE__);
  ip->fd = micreate(ip->name, NC_CLOBBER|MI2_CREATE_V1); /*Create MINC1 format*/
  if (ip->fd < 0) {
    FUNC_ERROR("micreate");
  }
  printf("%s:%d\n",__FILE__,__LINE__);
  /* Have to use ncdimdef() here since there is no MINC equivalent.  Sigh. 
   */
  printf("%s:%d\n",__FILE__,__LINE__);
  for (i = 0; i < ndims; i++) {

    /* Define the dimension 
     */
    ip->dim[i] = ncdimdef(ip->fd, dims[i].name, dims[i].length);
    if (ip->dim[i] < 0) {
      FUNC_ERROR("ncdimdef");
    }

    /* Create the dimension variable.
     */
    varid = micreate_std_variable(ip->fd, dims[i].name, NC_DOUBLE, 0, &ip->dim[i]);
    if (varid < 0) {
      FUNC_ERROR("micreate_std_variable");
    }
    stat = miattputdbl(ip->fd, varid, MIstep, 0.8);
    if (stat < 0) {
      FUNC_ERROR("miattputdbl");
    }
    stat = miattputdbl(ip->fd, varid, MIstart, 22.0);
    if (stat < 0) {
      FUNC_ERROR("miattputdbl");
    }
  }

  /* Create the image-max variable.
   */
  printf("%s:%d\n",__FILE__,__LINE__);
  ip->maxid = micreate_std_variable(ip->fd, (char*)MIimagemax, NC_DOUBLE, 0, NULL);
  if (ip->maxid < 0) {
    FUNC_ERROR("micreate_std_variable");
  }

  /* Create the image-min variable.
   */
  printf("%s:%d\n",__FILE__,__LINE__);
  ip->minid = micreate_std_variable(ip->fd, (char*)MIimagemin, NC_DOUBLE, 0, NULL);
  if (ip->minid < 0) {
    FUNC_ERROR("micreate_std_variable");
  }

  printf("%s:%d\n",__FILE__,__LINE__);
  ip->imgid = micreate_std_variable(ip->fd, (char*)MIimage, NC_FLOAT, ndims, ip->dim);
  if (ip->imgid < 0) {
    FUNC_ERROR("micreate_std_variable");
  }
  
  return (0);
}

static int test2(struct testinfo *ip, struct dimdef *dims, int ndims)
{
  int i, j, k;
  int stat;
  long coords[3];
  double flt;

  printf("test2\n");
  stat = miattputdbl(ip->fd, ip->imgid, MIvalid_max, (XSIZE * 10000.0));
  if (stat < 0) {
    FUNC_ERROR("miattputdbl");
  }

  stat = miattputdbl(ip->fd, ip->imgid, MIvalid_min, 0.0);
  if (stat < 0) {
    FUNC_ERROR("miattputdbl");
  }

  ncendef(ip->fd);		/* End definition mode. */

  coords[0] = 0;

  flt = 0.0;
  stat = mivarput1(ip->fd, ip->minid, coords, NC_DOUBLE, MI_SIGNED, &flt);
  if (stat < 0) {
    FUNC_ERROR("mivarput1");
  }
    
  flt = XSIZE * 10000.0;
  stat = mivarput1(ip->fd, ip->maxid, coords, NC_DOUBLE, MI_SIGNED, &flt);
  if (stat < 0) {
    FUNC_ERROR("mivarput1");
  }

  for (i = 0; i < dims[TST_X].length; i++) {
    for (j = 0; j < dims[TST_Y].length; j++) {
      for (k = 0; k < dims[TST_Z].length; k++) {
        float tmp = (i * 10000.0f) + (j * 100.0f) + k;

        coords[TST_X] = i;
        coords[TST_Y] = j;
        coords[TST_Z] = k;

        stat = mivarput1(ip->fd, ip->imgid, coords, NC_FLOAT, MI_SIGNED, &tmp);
        if (stat < 0) {
          fprintf(stderr, "At (%d,%d,%d), status %d: ", i,j,k,stat);
          FUNC_ERROR("mivarput1");
        }
      }
    }
  }
  
  return (0);
}

static int
test3(struct testinfo *ip, struct dimdef *dims, int ndims)
{
  /* Get the same variable again, but this time use an ICV to scale it.
   */
  size_t total;
  long coords[3];
  long lengths[3];
  double range[2];
  
  void *buf_ptr;
  float *flt_ptr;
  int i, j, k;
  int stat;
  int icv;

  printf("test3\n");

  total = 1;
  for (i = 0; i < ndims; i++) {
    total *= dims[i].length;
  }
  
  buf_ptr = malloc(total * sizeof (float));
  
  if (buf_ptr == NULL) {
    fprintf(stderr, "Oops, malloc failed\n");
    return (-1);
  }
  
  coords[TST_X] = 0;
  coords[TST_Y] = 0;
  coords[TST_Z] = 0;
  lengths[TST_X] = dims[TST_X].length;
  lengths[TST_Y] = dims[TST_Y].length;
  lengths[TST_Z] = dims[TST_Z].length;
  
  icv = miicv_create();
  if (icv < 0) {
    FUNC_ERROR("miicv_create");
  }
  
  stat = miicv_setint(icv, MI_ICV_TYPE, NC_FLOAT);
  if (stat < 0) {
    FUNC_ERROR("miicv_setint");
  }

  stat = miicv_setint(icv, MI_ICV_DO_NORM, 1);
  if (stat < 0) {
    FUNC_ERROR("miicv_setint");
  }
  
  stat = miicv_setint(icv, MI_ICV_USER_NORM, 1);
  if (stat < 0) {
    FUNC_ERROR("miicv_setint");
  }
  
  stat = miicv_attach(icv, ip->fd, ip->imgid);
  if (stat < 0) {
    FUNC_ERROR("miicv_attach");
  }
  
  stat = miicv_get(icv, coords, lengths, buf_ptr);
  if (stat < 0) {
    FUNC_ERROR("miicv_get");
  }
  
  stat = miget_image_range(ip->fd, range);
  if (stat < 0) {
    FUNC_ERROR("miget_image_range");
  }
  
  if (range[0] != 0 || range[1] != (XSIZE * 10000.0)) {
    fprintf(stderr, "miget_image_range: bad result\n");
    errors++;
  }
  
  stat = miget_valid_range(ip->fd, ip->imgid, range);
  if (stat < 0) {
    FUNC_ERROR("miget_valid_range");
  }
  
  if (range[0] != 0 || range[1] != (XSIZE * 10000.0)) {
    fprintf(stderr, "miget_valid_range: bad result\n");
    errors++;
  }
  
  flt_ptr = (float *) buf_ptr;
  for (i = 0; i < dims[TST_X].length; i++) {
    for (j = 0; j < dims[TST_Y].length; j++) {
      for (k = 0; k < dims[TST_Z].length; k++) {
        float tmp = (i * 10000.0f) + (j * 100.0f) + k;
        if (*flt_ptr != tmp ) {
          fprintf(stderr, "1. Data error at (%d,%d,%d) %f != %f\n", i,j,k, *flt_ptr, tmp);
          errors++;
        }
        flt_ptr++;
      }
    }
  }
  
  stat = miicv_detach(icv);
  if (stat < 0) {
    FUNC_ERROR("miicv_detach");
  }
  
  stat = miicv_free(icv);
  if (stat < 0) {
    FUNC_ERROR("miicv_free");
  }
  
  free(buf_ptr);
  
  return (0);
}


static void test4(struct testinfo *ip, struct dimdef *dims, int ndims)
{
  mihandle_t vol;  
  
  int           ndim;  
  
  int r;
  /*Now we are going to work with the volume using apparent dimension order*/
  midimhandle_t my_dim[3];
  static char *my_dimorder[] = {MIxspace,MIyspace,MIzspace};
  misize_t my_sizes[3];
  misize_t my_start[3];
  misize_t my_count[3];
  miclass_t      volume_class;
  mitype_t       volume_type;
  
  misize_t i,j,k;
  
  float *buffer;
  float *flt_ptr;
  /**/
  if( miopen_volume ( ip->name, MI2_OPEN_READ, &vol )<0)
    FUNC_ERROR("miopen_volume");
  
  if(miget_volume_dimension_count(vol,MI_DIMCLASS_ANY,MI_DIMATTR_ALL,&ndim)<0)
    FUNC_ERROR("miget_volume_dimension_count");
  
  
  if(ndim!=3)
    FUNC_ERROR("incorrect dimensions count");
  
  
  r=miget_data_class(vol, &volume_class);
  
  if ( r < 0 ) {
    FUNC_ERROR ( "failed to get volume class" );
  }
  
  r=miget_data_type(vol, &volume_type);
  
  if ( r < 0 ) {
    FUNC_ERROR ( "failed to get volume type" );
  }
  
 
  if(miset_apparent_dimension_order_by_name ( vol, 3, my_dimorder )<0)
    FUNC_ERROR("miset_apparent_dimension_order_by_name");
  
  /* get the apparent dimensions and their sizes */
  r  = miget_volume_dimensions ( vol, MI_DIMCLASS_ANY,
                                 MI_DIMATTR_ALL, MI_DIMORDER_APPARENT,
                                 3, my_dim );
  if ( r <0 ) {
    FUNC_ERROR("Error in miget_volume_dimensions\n" );
  }
  r = miget_dimension_sizes ( my_dim, 3, my_sizes );
  
  if ( r <0 ) {
    FUNC_ERROR("Error in miget_dimension_sizes\n" );
  }
  
  
  buffer=flt_ptr=malloc(sizeof(float)*my_sizes[0]*my_sizes[1]*my_sizes[2]);
  my_start[0]=my_start[1]=my_start[2]=0;
  
  my_count[0]=my_sizes[0];
  my_count[1]=my_sizes[1];
  my_count[2]=my_sizes[2];
  
  printf("Reading full volume %dx%dx%d float ... \n",(int)my_count[0],(int)my_count[1],(int)my_count[2]);
  
  if ( (r=miget_real_value_hyperslab ( vol, MI_TYPE_FLOAT, my_start, my_count, flt_ptr )) < 0 ) {
    FUNC_ERROR ( "Could not get float full volume." );
  }
  
  
  for (i = 0; i < my_count[0]; i++) {
    for (j = 0; j < my_count[1]; j++) {
      for (k = 0; k < my_count[2]; k++) {
        float tmp = (i * 10000) + (j * 100) + k;
        
        if (*flt_ptr != (float) tmp ) {
          fprintf(stderr, "2. Data error at (%llu,%llu,%llu) %f != %f\n", i,j,k, *flt_ptr, tmp);
          errors++;
        }
        flt_ptr++;
      }
    }
  }
  
  free(buffer);
  
  /* close volume*/
  miclose_volume ( vol );
  
}

/* Test MINC API's 
 */
int main(int argc, char **argv)
{
  struct testinfo info;

  test1(&info, dimtab1, 3);
  
  test2(&info, dimtab1, 3);

  test3(&info, dimtab1, 3);
  
  if (miclose(info.fd) != MI_NOERROR) {
    FUNC_ERROR("miclose");
  }
  
  /*now let's use MINC2 API*/
  test4(&info, dimtab1, 3);
  
  /*unlink(info.name);*/		/* Delete the temporary file. */

  free(info.name);		/* Free the temporary filename */
  
  if(!errors)
    printf("No errors detected!\n");
  else
    printf("Errors: %ld!\n",errors);
  
  return (errors);
}