-
Notifications
You must be signed in to change notification settings - Fork 25
/
rawtominc.c
1907 lines (1747 loc) · 64.4 KB
/
rawtominc.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------- MNI Header -----------------------------------
@NAME : rawtominc
@INPUT : argc, argv - command line arguments
@OUTPUT : (none)
@RETURNS : error status
@DESCRIPTION: Converts a raw volume of data values (read from standard input)
to a minc format file.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : September 25, 1992 (Peter Neelin)
@MODIFIED :
* $Log: rawtominc.c,v $
* Revision 6.29 2008-08-13 06:26:29 rotor
* * added Claudes (many) 64 bit fixes to dicom code and updates
* * 64 bit fix for rawtominc's use of ParseArgv
*
* Revision 6.28 2008/01/17 02:33:06 rotor
* * removed all rcsids
* * removed a bunch of ^L's that somehow crept in
* * removed old (and outdated) BUGS file
*
* Revision 6.27 2008/01/13 04:30:28 stever
* Add braces around static initializers.
*
* Revision 6.26 2008/01/12 19:08:15 stever
* Add __attribute__ ((unused)) to all rcsid variables.
*
* Revision 6.25 2008/01/11 07:17:08 stever
* Remove unused variables.
*
* Revision 6.24 2007/02/20 18:24:45 baghdadi
* make sure the global scaling happens when input is float and output is float or double
*
* Revision 6.23 2007/02/02 18:53:46 baghdadi
* Create global scaling for float dat
*
* Revision 6.22 2007/01/09 21:56:59 baghdadi
* Added -2 option for minc2.0 support
*
* Revision 6.21 2006/07/28 18:17:15 baghdadi
* Added -like option to list of arguments similar behaviour like mincresample.
*
* Revision 6.20 2006/05/11 15:09:57 claude
* added float and double to -swap_bytes
*
* Revision 6.13.2.2 2005/03/16 19:02:52 bert
* Port changes from 2.0 branch
*
* Revision 6.13.2.1 2004/09/28 20:07:30 bert
* Minor portability fixes for Windows
*
* Revision 6.13 2004/02/02 18:24:58 bert
* Include a ARGV_VERINFO record in the argTable[]
*
* Revision 6.12 2003/11/14 16:52:24 stever
* More last-minute fixes.
*
* Revision 6.11 2003/11/03 19:43:18 bert
* Handle unspecified dimension order correctly
*
* Revision 6.10 2003/10/29 17:50:18 bert
* Added -dimorder option
*
* Revision 6.9 2003/10/21 22:22:09 bert
* Added -swap_bytes option for int or short input, per A. Janke.
*
* Revision 6.8 2002/08/05 00:53:50 neelin
* Added slightly modified code from Colin Holmes to support -skip option
*
* Revision 6.7 2001/09/18 15:33:00 neelin
* Create image variable last to allow big images and to fix compatibility
* problems with 2.3 and 3.x.
*
* Revision 6.6 2001/08/16 16:41:37 neelin
* Added library functions to handle reading of datatype, sign and valid range,
* plus writing of valid range and setting of default ranges. These functions
* properly handle differences between valid_range type and image type. Such
* difference can cause valid data to appear as invalid when double to float
* conversion causes rounding in the wrong direction (out of range).
* Modified voxel_loop, volume_io and programs to use these functions.
*
* Revision 6.5 2001/04/24 13:38:46 neelin
* Replaced NC_NAT with MI_ORIGINAL_TYPE.
*
* Revision 6.4 2001/04/17 18:40:25 neelin
* Modifications to work with NetCDF 3.x
* In particular, changed NC_LONG to NC_INT (and corresponding longs to ints).
* Changed NC_UNSPECIFIED to NC_NAT.
* A few fixes to the configure script.
*
* Revision 6.3 1999/10/19 14:45:31 neelin
* Fixed Log subsitutions for CVS
*
* Revision 6.2 1998/06/22 14:06:01 neelin
* Fixed bug in handling of default types and signs.
*
* Revision 6.1 1997/09/29 12:22:46 neelin
* Clarified argument error messages.
*
* Revision 6.0 1997/09/12 13:23:25 neelin
* Release of minc version 0.6
*
* Revision 5.0 1997/08/21 13:24:26 neelin
* Release of minc version 0.5
*
* Revision 4.0 1997/05/07 19:59:55 neelin
* Release of minc version 0.4
*
* Revision 3.3 1997/04/21 20:19:17 neelin
* Added -origin option.
*
* Revision 3.2 1996/06/19 18:24:16 neelin
* Check errors on fopen when -input is used.
* Try opening input before creating minc file.
*
* Revision 3.1 1995/11/16 13:11:16 neelin
* Added include of math.h to get declaration of strtod under SunOs
*
* Revision 3.0 1995/05/15 19:31:00 neelin
* Release of minc version 0.3
*
* Revision 2.7 1995/03/30 13:00:05 neelin
* Added -sattribute and -dattribute options.
*
* Revision 2.6 1995/02/15 18:10:35 neelin
* Added check for global attribute specified with -attribute.
*
* Revision 2.5 1995/02/08 19:31:47 neelin
* Moved ARGSUSED statements for irix 5 lint.
*
* Revision 2.4 1995/01/23 09:07:46 neelin
* changed ncclose to miclose.
*
* Revision 2.3 95/01/23 08:59:31 neelin
* Changed nccreate to micreate
*
* Revision 2.2 95/01/03 13:09:24 neelin
* Added direction cosine support.
*
* Revision 2.1 94/10/11 16:18:50 neelin
* Fixed scanning of integers for max and min (conversion to double had
* signed and unsigned reversed).
*
* Revision 2.0 94/09/28 10:33:01 neelin
* Release of minc version 0.2
*
* Revision 1.15 94/09/28 10:32:55 neelin
* Pre-release
*
* Revision 1.14 94/09/23 08:30:52 neelin
* Added -xyz, etc options for image/volume orientation.
*
* Revision 1.13 94/06/10 15:24:41 neelin
* Added option -real_range.
*
* Revision 1.12 94/05/05 10:31:53 neelin
* Added -scan_range, -frame_time, -frame_width, -input, -attribute and
* modality options.
*
* Revision 1.11 93/10/06 10:14:17 neelin
* Added include of string.h.
*
* Revision 1.10 93/08/11 15:25:05 neelin
* Added RCS logging to source.
*
December 2, 1992 (P.N.)
- changed to parse argv with ParseArgv
@COPYRIGHT :
Copyright 1993 Peter Neelin, McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <minc.h>
#include <float.h>
#include <math.h>
#if HAVE_UNISTD_H
#define __USE_XOPEN
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <ParseArgv.h>
#include <time_stamp.h>
#include <convert_origin_to_start.h>
/* Some constants */
#define VECTOR_SEPARATOR ','
#define NORMAL_STATUS 0
#define ERROR_STATUS 1
#define MIN_DIMS 2
#define MAX_DIMS 4
#define DEF_TYPE 0
#define BYTE_TYPE 1
#define SHORT_TYPE 2
#define INT_TYPE 3
#define FLOAT_TYPE 4
#define DOUBLE_TYPE 5
#define DEF_SIGN 0
#define SIGNED 1
#define UNSIGNED 2
#define TRUE 1
#define FALSE 0
#define X 0
#define Y 1
#define Z 2
#define WORLD_NDIMS 3
#define DEF_STEP DBL_MAX
#define DEF_START DBL_MAX
#define DEF_RANGE DBL_MAX
#define DEF_DIRCOS DBL_MAX
#define DEF_ORIGIN DBL_MAX
#define ARG_SEPARATOR ','
/* LB. needed for volume_def */
#define VOL_NDIMS 3 /* Number of volume dimensions */
#define WORLD_NDIMS 3 /* Number of world spatial dimensions */
int Specified_like = FALSE;
/* Macros */
#define STR_EQ(s1,s2) (strcmp(s1,s2)==0)
/* LB. For referring to world axes in arrays subscripted by WORLD_NDIMS */
#define NO_AXIS -1
#define XAXIS 0
#define YAXIS 1
#define ZAXIS 2
typedef struct {
char *name;
int mincid;
int imgid;
int maxid;
int minid;
int ndims;
nc_type datatype;
int is_signed;
double vrange[2]; /* [0]=min, [1]=max */
long nelements[MAX_VAR_DIMS]; /* Size of each dimension */
int world_axes[MAX_VAR_DIMS]; /* Relates variable index to X, Y, Z
or NO_AXIS */
int indices[VOL_NDIMS]; /* Indices of volume dimenions (subscripted
from slowest to fastest) */
int axes[WORLD_NDIMS]; /* Relates world X,Y,Z (index) to dimension
order (value=0,1,2; 0=slowest varying) */
int using_icv; /* True if we are using an icv to read data */
int icvid; /* Id of icv (if used) */
long slices_per_image; /* Number of volume slices (row, column) per
minc file image */
long images_per_file; /* Number of minc file images in the file */
int do_slice_renormalization; /* Flag indicating that we need to
loop through the data a second time,
recomputing the slices to normalize
images properly */
int keep_real_range; /* Flag indicating whether we should keep
the real range of the input data or not */
} File_Info;
typedef struct {
int axes[WORLD_NDIMS]; /* Relates world X,Y,Z (index) to dimension
order (value=0,1,2; 0=slowest varying) */
long nelements[WORLD_NDIMS]; /* These are subscripted by X, Y and Z */
double step[WORLD_NDIMS];
double start[WORLD_NDIMS];
double dircos[WORLD_NDIMS][WORLD_NDIMS];
double *coords[WORLD_NDIMS];
char units[WORLD_NDIMS][MI_MAX_ATTSTR_LEN];
char spacetype[WORLD_NDIMS][MI_MAX_ATTSTR_LEN];
} Volume_Definition;
/* Function declarations */
static void parse_args(int argc, char *argv[]);
static void usage_error(char *pname);
static int get_attribute(char *dst, char *key, char *nextarg);
static int get_times(char *dst, char *key, char *nextarg);
static int get_axis_order(char *dst, char *key, char *nextArg);
/* LB. function prototypes */
static void get_file_info(char *filename, int initialized_volume_def,
Volume_Definition *volume_def,
File_Info *file_info);
static int get_model_file(char *dst, char *key, char *nextArg);
/* Array containing information about signs. It is subscripted by
[signtype][type]. Note that the first row should never be used, since
default_signs should be used instead. */
char *sign_names[][6] = {
{MI_SIGNED, MI_UNSIGNED, MI_SIGNED, MI_SIGNED, MI_SIGNED, MI_SIGNED},
{MI_SIGNED, MI_SIGNED, MI_SIGNED, MI_SIGNED, MI_SIGNED, MI_SIGNED},
{MI_SIGNED, MI_UNSIGNED, MI_UNSIGNED, MI_UNSIGNED, MI_UNSIGNED, MI_UNSIGNED}
};
/* Array containing default signs subscripted by [type] (default, byte,
short, int, float, double) */
int default_signs[] = {
SIGNED, UNSIGNED, SIGNED, SIGNED, SIGNED, SIGNED
};
/* Information converting types for argument processing to NetCDF types */
nc_type convert_types[] = {
MI_ORIGINAL_TYPE, NC_BYTE, NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE
};
/* enum for modality types to avoid problems with passing pointers in ParseArgv */
typedef enum {
MODALITY_NONE, MODALITY_PET, MODALITY_MRI, MODALITY_SPECT, MODALITY_GAMMA,
MODALITY_MRS, MODALITY_MRA, MODALITY_CT, MODALITY_DSA, MODALITY_DR
} Modality_code;
/* Argument variables */
char *pname;
char *filename;
int clobber=FALSE;
#if MINC2
int v2format=FALSE; /* Version 2.0 file format? */
#endif /* MINC2 */
char *dimname[MAX_VAR_DIMS];
long dimlength[MAX_VAR_DIMS];
int ndims;
/* LB. */
int NDims=0;
int type = BYTE_TYPE;
int signtype = DEF_SIGN;
nc_type datatype;
char *sign;
double valid_range[2] = {0.0, -1.0};
int vrange_set;
int otype = DEF_TYPE;
int osigntype = DEF_SIGN;
nc_type odatatype;
char *osign;
double ovalid_range[2] = {0.0, -1.0};
int ovrange_set;
double dimstep[3] = {DEF_STEP, DEF_STEP, DEF_STEP};
double dimstart[3] = {DEF_START, DEF_START, DEF_START};
double dimdircos[3][3] = {
{ DEF_DIRCOS, DEF_DIRCOS, DEF_DIRCOS },
{ DEF_DIRCOS, DEF_DIRCOS, DEF_DIRCOS },
{ DEF_DIRCOS, DEF_DIRCOS, DEF_DIRCOS }
};
double origin[3] = {DEF_ORIGIN, DEF_ORIGIN, DEF_ORIGIN};
int vector_dimsize = -1;
Modality_code modality = MODALITY_NONE;
int attribute_list_size = 0;
int attribute_list_alloc = 0;
struct {
char *variable;
char *attribute;
char *value;
double double_value;
} *attribute_list = NULL;
int num_frame_times = 0;
double *frame_times = NULL;
int num_frame_widths = 0;
double *frame_widths = NULL;
char *inputfile = NULL;
int do_minmax = FALSE;
double real_range[2] = {DEF_RANGE, DEF_RANGE};
long skip_length;
int swap_bytes = FALSE;
char *axis_order[MAX_DIMS+1] = { MItime, MIzspace, MIyspace, MIxspace };
/* LB. */
static Volume_Definition volume_def;
/* Argument table */
ArgvInfo argTable[] = {
{NULL, ARGV_VERINFO, (char *) VERSION, (char *) NULL, NULL},
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
"Options to specify input dimension order (from slowest to fastest)."},
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
" Default = -transverse."},
{"-transverse", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Transverse images : [[time] z] y x"},
{"-sagittal", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Sagittal images : [[time] x] z y"},
{"-coronal", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Coronal images : [[time] y] z x"},
{"-time", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Time ordered images : [[z] time] y x"},
{"-xyz", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Dimension order : [[time] x] y z"},
{"-xzy", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Dimension order : [[time] x] z y"},
{"-yxz", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Dimension order : [[time] y] x z"},
{"-yzx", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Dimension order : [[time] y] z x"},
{"-zxy", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Dimension order : [[time] z] x y"},
{"-zyx", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Dimension order : [[time] z] y x"},
{"-dimorder", ARGV_FUNC, (char *) get_axis_order, (char *) axis_order,
"Arbitrary dimensions: <dim1>,<dim2>[,<dim3>[,<dim4]]"},
{"-vector", ARGV_INT, (char *) 1, (char *) &vector_dimsize,
"Specifies the size of a vector dimension"},
{NULL, ARGV_HELP, NULL, NULL,
"Options to specify input data type. Default = -byte."},
{"-byte", ARGV_CONSTANT, (char *) BYTE_TYPE, (char *) &type,
"Byte values"},
{"-short", ARGV_CONSTANT, (char *) SHORT_TYPE, (char *) &type,
"Short integer values"},
{"-int", ARGV_CONSTANT, (char *) INT_TYPE, (char *) &type,
"32-bit integer values"},
{"-long", ARGV_CONSTANT, (char *) INT_TYPE, (char *) &type,
"Superseded by -int"},
{"-float", ARGV_CONSTANT, (char *) FLOAT_TYPE, (char *) &type,
"Single-precision floating point values"},
{"-double", ARGV_CONSTANT, (char *) DOUBLE_TYPE, (char *) &type,
"Double-precision floating point values"},
{NULL, ARGV_HELP, NULL, NULL,
"Options for sign of input data. Default = -signed (-unsigned for byte)."},
{"-signed", ARGV_CONSTANT, (char *) SIGNED, (char *) &signtype,
"Signed values"},
{"-unsigned", ARGV_CONSTANT, (char *) UNSIGNED, (char *) &signtype,
"Unsigned values"},
{NULL, ARGV_HELP, NULL, NULL,
"Other input value options."},
{"-skip", ARGV_INT, (char *) 0, (char *) &skip_length,
"Specifies the number of bytes to skip over in the input stream"},
{"-range", ARGV_FLOAT, (char *) 2, (char *) valid_range,
"Valid range of input values (default = full range)."},
{"-real_range", ARGV_FLOAT, (char *) 2, (char *) real_range,
"VIO_Real range of input values (ignored for floating-point types)."},
{"-swap_bytes", ARGV_CONSTANT, (char *) TRUE, (char *)&swap_bytes,
"Swap bytes on short, long integer, float or double input." },
{NULL, ARGV_HELP, NULL, NULL,
"Options for type of output minc data. Default = input data type"},
{"-obyte", ARGV_CONSTANT, (char *) BYTE_TYPE, (char *) &otype,
"Byte values"},
{"-oshort", ARGV_CONSTANT, (char *) SHORT_TYPE, (char *) &otype,
"Short integer values"},
{"-oint", ARGV_CONSTANT, (char *) INT_TYPE, (char *) &otype,
"32-bit integer values"},
{"-olong", ARGV_CONSTANT, (char *) INT_TYPE, (char *) &otype,
"Superseded by -oint"},
{"-ofloat", ARGV_CONSTANT, (char *) FLOAT_TYPE, (char *) &otype,
"Single-precision floating point values"},
{"-odouble", ARGV_CONSTANT, (char *) DOUBLE_TYPE, (char *) &otype,
"Double precision floating point values"},
{NULL, ARGV_HELP, NULL, NULL,
"Options for sign of output minc data."},
{NULL, ARGV_HELP, NULL, NULL,
" Default = -signed (-unsigned for byte data)"},
{"-osigned", ARGV_CONSTANT, (char *) SIGNED, (char *) &osigntype,
"Signed values"},
{"-ounsigned", ARGV_CONSTANT, (char *) UNSIGNED, (char *) &osigntype,
"Unsigned values"},
{NULL, ARGV_HELP, NULL, NULL,
"Other output value options."},
{"-orange", ARGV_FLOAT, (char *) 2, (char *) ovalid_range,
"Valid range of output minc values."},
{NULL, ARGV_HELP, NULL, NULL,
"Options to specify whether integers are scanned for min and max."},
{"-scan_range", ARGV_CONSTANT, (char *) TRUE, (char *) &do_minmax,
"Scan input for min and max."},
{"-noscan_range", ARGV_CONSTANT, (char *) FALSE, (char *) &do_minmax,
"Do not scan input for min and max (default)."},
{NULL, ARGV_HELP, NULL, NULL,
"Options for writing output file. Default = -noclobber."},
#if MINC2
{"-2", ARGV_CONSTANT, (char *) TRUE, (char *) &v2format,
"Produce a MINC 2.0 format output file."},
#endif /* MINC2 */
{"-clobber", ARGV_CONSTANT, (char *) TRUE, (char *) &clobber,
"Overwrite existing file"},
{"-noclobber", ARGV_CONSTANT, (char *) FALSE, (char *) &clobber,
"Don't overwrite existing file"},
{NULL, ARGV_HELP, NULL, NULL,
"Option for specifying input raw data file."},
{"-input", ARGV_STRING, (char *) 1, (char *) &inputfile,
"Name of input file (default=stdin)."},
{NULL, ARGV_HELP, NULL, NULL,
"Options for specifying spatial dimension coordinates."},
{"-xstep", ARGV_FLOAT, (char *) 1, (char *) &dimstep[X],
"Step size for x dimension."},
{"-ystep", ARGV_FLOAT, (char *) 1, (char *) &dimstep[Y],
"Step size for y dimension."},
{"-zstep", ARGV_FLOAT, (char *) 1, (char *) &dimstep[Z],
"Step size for z dimension."},
{"-xstart", ARGV_FLOAT, (char *) 1, (char *) &dimstart[X],
"Starting coordinate for x dimension."},
{"-ystart", ARGV_FLOAT, (char *) 1, (char *) &dimstart[Y],
"Starting coordinate for y dimension."},
{"-zstart", ARGV_FLOAT, (char *) 1, (char *) &dimstart[Z],
"Starting coordinate for z dimension."},
{"-xdircos", ARGV_FLOAT, (char *) 3, (char *) dimdircos[X],
"Direction cosines for x dimension."},
{"-ydircos", ARGV_FLOAT, (char *) 3, (char *) dimdircos[Y],
"Direction cosines for y dimension."},
{"-zdircos", ARGV_FLOAT, (char *) 3, (char *) dimdircos[Z],
"Direction cosines for z dimension."},
{"-origin", ARGV_FLOAT, (char *) 3, (char *) origin,
"Coordinate of first pixel."},
{NULL, ARGV_HELP, NULL, NULL,
"Options for specifying imaging modality. Default = -nomodality."},
{"-nomodality", ARGV_CONSTANT, (char *)MODALITY_NONE, (char *) &modality,
"Do not store modality type in file."},
{"-pet", ARGV_CONSTANT, (char *)MODALITY_PET, (char *) &modality,
"PET data."},
{"-mri", ARGV_CONSTANT, (char *)MODALITY_MRI, (char *) &modality,
"MRI data."},
{"-spect", ARGV_CONSTANT, (char *)MODALITY_SPECT, (char *) &modality,
"SPECT data."},
{"-gamma", ARGV_CONSTANT,(char *) MODALITY_GAMMA, (char *) &modality,
"Data from a gamma camera."},
{"-mrs", ARGV_CONSTANT, (char *)MODALITY_MRS, (char *) &modality,
"MR spectroscopy data."},
{"-mra", ARGV_CONSTANT, (char *)MODALITY_MRA, (char *) &modality,
"MR angiography data."},
{"-ct", ARGV_CONSTANT, (char *)MODALITY_CT, (char *) &modality,
"CT data."},
{"-dsa", ARGV_CONSTANT, (char *)MODALITY_DSA, (char *) &modality,
"DSA data"},
{"-dr", ARGV_CONSTANT, (char *)MODALITY_DR, (char *) &modality,
"Digital radiography data."},
{NULL, ARGV_HELP, NULL, NULL,
"Options for specifying attribute values by name."},
{"-sattribute", ARGV_FUNC, (char *) get_attribute, NULL,
"Set a string attribute (<var>:<attr>=<value>)."},
{"-dattribute", ARGV_FUNC, (char *) get_attribute, NULL,
"Set a double precision attribute (<var>:<attr>=<value>)."},
{"-attribute", ARGV_FUNC, (char *) get_attribute, NULL,
"Set an attribute, guessing the type (<var>:<attr>=<value>)."},
{NULL, ARGV_HELP, NULL, NULL,
"Options for specifying time dimension coordinates."},
{"-frame_times", ARGV_FUNC, (char *) get_times, NULL,
"Specify the frame starting times (\"<t1>,<t2>,<t3>,...\")."},
{"-frame_widths", ARGV_FUNC, (char *) get_times, NULL,
"Specify the frame lengths (\"<w1>,<w2>,<w3>,...\")."},
{"-like", ARGV_FUNC, (char *) get_model_file, (char *) &volume_def,
"Specifies a model file."},
{NULL, ARGV_END, NULL, NULL, NULL}
};
/* Main program */
int main(int argc, char *argv[])
{
int do_vrange;
int cdfid, imgid, maxid, minid, varid;
int icv;
long start[MAX_VAR_DIMS];
long count[MAX_VAR_DIMS];
long end[MAX_VAR_DIMS];
int dim[MAX_VAR_DIMS];
void *image;
double imgmax, imgmin, value;
long image_size, image_pix, nread, fastdim;
int pix_size;
int image_dims;
int i, j;
int index;
FILE *instream;
char *tm_stamp;
int iatt;
long time_start, time_count;
int is_signed;
void *ptr;
int floating_type;
int do_real_range;
int status;
double scale, offset, denom, pixel_min, pixel_max;
double dircos[WORLD_NDIMS][WORLD_NDIMS];
int cflags;
/* Save time stamp and args */
tm_stamp = time_stamp(argc, argv);
/* Parse arguments */
parse_args(argc, argv);
/* Do normalization if input type is float */
floating_type = (( datatype==NC_FLOAT) || ( datatype==NC_DOUBLE));
do_minmax = do_minmax || floating_type;
/* Find max and min for vrange if output type is float */
do_vrange =
do_minmax && (( odatatype==NC_FLOAT) || ( odatatype==NC_DOUBLE));
/* Did the user provide a real range */
do_real_range = (real_range[0] != DEF_RANGE) && !floating_type;
/* Check that only start or origin are specified, not both */
for (i=0; i<WORLD_NDIMS; i++) {
if ((dimstart[i] != DEF_START) && (origin[0] != DEF_START)) {
(void) fprintf(stderr, "Do not specify both start and origin.\n");
exit(ERROR_STATUS);
}
}
/* If origin is given, convert it to a start using direction cosines.
Default direction cosines must be provided. */
if (origin[0] != DEF_ORIGIN) {
/* Set direction cosines */
for (i=0; i<WORLD_NDIMS; i++) {
if (dimdircos[i][0] != DEF_DIRCOS) {
for (j=0; j<WORLD_NDIMS; j++)
dircos[i][j] = dimdircos[i][j];
}
else { /* Use default */
for (j=0; j<WORLD_NDIMS; j++)
dircos[i][j] = 0.0;
dircos[i][i] = 1.0;
}
}
/* Convert the origin to a start value */
status = convert_origin_to_start(origin, dircos[X], dircos[Y], dircos[Z],
dimstart);
switch (status) {
case 1:
(void) fprintf(stderr,
"Cannot convert origin to start - some direction cosines have zero length.\n");
break;
case 2:
(void) fprintf(stderr,
"Cannot convert origin to start - some direction cosines are parallel.\n");
break;
}
}
/* Open the input file */
if (inputfile == NULL) {
instream=stdin;
}
else {
instream = fopen(inputfile, "r");
if (instream == NULL) {
(void) fprintf(stderr, "Error opening input file \"%s\"\n",
inputfile);
exit(ERROR_STATUS);
}
}
/* Create an icv */
icv=miicv_create();
(void) miicv_setint(icv, MI_ICV_TYPE, datatype);
(void) miicv_setstr(icv, MI_ICV_SIGN, sign);
if (vrange_set) {
(void) miicv_setdbl(icv, MI_ICV_VALID_MIN, valid_range[0]);
(void) miicv_setdbl(icv, MI_ICV_VALID_MAX, valid_range[1]);
}
if (floating_type) {
(void) miicv_setint(icv, MI_ICV_DO_NORM, TRUE);
(void) miicv_setint(icv, MI_ICV_USER_NORM, TRUE);
}
/* Calculate scale and offset to convert pixel to real value for
input data */
if (do_real_range) {
(void) miicv_inqdbl(icv, MI_ICV_VALID_MIN, &pixel_min);
(void) miicv_inqdbl(icv, MI_ICV_VALID_MAX, &pixel_max);
denom = pixel_max - pixel_min;
if (denom != 0.0)
scale = (real_range[1] - real_range[0]) / denom;
else
scale = 0.0;
offset = real_range[0] - pixel_min * scale;
}
else {
scale = 1.0;
offset = 0.0;
}
/* Create the file and save the time stamp */
if (clobber) {
cflags = NC_CLOBBER;
}
else {
cflags = NC_NOCLOBBER;
}
#if MINC2
if (v2format) {
cflags |= MI2_CREATE_V2;
}
#endif /* MINC2 */
cdfid=micreate(filename, cflags);
(void) miattputstr(cdfid, NC_GLOBAL, MIhistory, tm_stamp);
/* Set the number of image dimensions */
image_dims = 2;
/* Create the dimensions */
for (i=0; i<ndims; i++) {
/* Create dimension */
dim[i] = ncdimdef(cdfid, dimname[i], dimlength[i]);
/* Create the variable if needed */
if (STR_EQ(dimname[i], MItime)) {
if (num_frame_times > 0) {
varid = micreate_std_variable(cdfid, MItime,
NC_DOUBLE, 1, &dim[i]);
}
if (num_frame_widths > 0) {
varid = micreate_std_variable(cdfid, MItime_width,
NC_DOUBLE, 1, &dim[i]);
}
}
else {
varid = micreate_std_variable(cdfid, dimname[i],
NC_INT, 0, NULL);
/* Write out step and start and direction cosine */
if (STR_EQ(dimname[i], MIxspace)) index = X;
if (STR_EQ(dimname[i], MIyspace)) index = Y;
if (STR_EQ(dimname[i], MIzspace)) index = Z;
if (dimstep[index] != DEF_STEP) {
(void) miattputdbl(cdfid, varid, MIstep, dimstep[index]);
}
if (dimstart[index] != DEF_START) {
(void) miattputdbl(cdfid, varid, MIstart, dimstart[index]);
}
if (dimdircos[index][0] != DEF_DIRCOS) {
(void) ncattput(cdfid, varid, MIdirection_cosines, NC_DOUBLE,
3, dimdircos[index]);
}
}
/* Set variables for looping through images */
start[i]=0;
count[i]= ((i<ndims-image_dims) ? 1 : dimlength[i]);
end[i]=dimlength[i];
}
/* Check for vector dimension */
if (vector_dimsize > 0) {
ndims++;
image_dims++;
dim[ndims-1] = ncdimdef(cdfid, MIvector_dimension,
(long) vector_dimsize);
start[ndims-1] = 0;
count[ndims-1] = vector_dimsize;
end[ndims-1] = vector_dimsize;
}
/* Create the modality attribute */
if (modality != MODALITY_NONE) {
varid = micreate_group_variable(cdfid, MIstudy);
switch (modality){
case MODALITY_PET:
(void) miattputstr(cdfid, varid, MImodality, MI_PET);
break;
case MODALITY_MRI:
(void) miattputstr(cdfid, varid, MImodality, MI_MRI);
break;
case MODALITY_SPECT:
(void) miattputstr(cdfid, varid, MImodality, MI_SPECT);
break;
case MODALITY_GAMMA:
(void) miattputstr(cdfid, varid, MImodality, MI_GAMMA);
break;
case MODALITY_MRS:
(void) miattputstr(cdfid, varid, MImodality, MI_MRS);
break;
case MODALITY_MRA:
(void) miattputstr(cdfid, varid, MImodality, MI_MRA);
break;
case MODALITY_CT:
(void) miattputstr(cdfid, varid, MImodality, MI_CT);
break;
case MODALITY_DSA:
(void) miattputstr(cdfid, varid, MImodality, MI_DSA);
break;
case MODALITY_DR:
(void) miattputstr(cdfid, varid, MImodality, MI_DR);
break;
case MODALITY_NONE:
default:
(void) fprintf(stderr, "%s: Unknown Modality, this should not happen (call houston).\n", pname);
exit(ERROR_STATUS);
break;
}
}
/* Create any special attributes */
ncopts = 0;
for (iatt=0; iatt < attribute_list_size; iatt++) {
if (strlen(attribute_list[iatt].variable) == 0) {
varid = NC_GLOBAL;
}
else {
varid = ncvarid(cdfid, attribute_list[iatt].variable);
if (varid == MI_ERROR) {
varid = micreate_group_variable(cdfid,
attribute_list[iatt].variable);
}
if (varid == MI_ERROR) {
varid = ncvardef(cdfid, attribute_list[iatt].variable, NC_INT,
0, NULL);
}
if (varid == MI_ERROR) {
continue;
}
}
if (attribute_list[iatt].value != NULL) {
(void) miattputstr(cdfid, varid, attribute_list[iatt].attribute,
attribute_list[iatt].value);
}
else {
(void) miattputdbl(cdfid, varid, attribute_list[iatt].attribute,
attribute_list[iatt].double_value);
}
}
ncopts = NC_VERBOSE | NC_FATAL;
/* Create the image */
if (do_minmax || do_real_range) {
if (!floating_type || (otype != FLOAT_TYPE && otype != DOUBLE_TYPE)) {
maxid = micreate_std_variable(cdfid, MIimagemax,
NC_DOUBLE, ndims-image_dims, dim);
minid = micreate_std_variable(cdfid, MIimagemin,
NC_DOUBLE, ndims-image_dims, dim);
}
else
{
maxid = micreate_std_variable(cdfid, MIimagemax,
NC_DOUBLE, 0, NULL);
minid = micreate_std_variable(cdfid, MIimagemin,
NC_DOUBLE, 0, NULL);
}
}
imgid=micreate_std_variable(cdfid, MIimage, odatatype, ndims, dim);
(void) miattputstr(cdfid, imgid, MIcomplete, MI_FALSE);
(void) miattputstr(cdfid, imgid, MIsigntype, osign);
if (ovrange_set)
(void) miset_valid_range(cdfid, imgid, ovalid_range);
/* End definition mode */
(void) ncendef(cdfid);
/* Write out the frame times and widths */
time_start = 0;
time_count = num_frame_times;
if (num_frame_times > 0) {
(void) mivarput(cdfid, ncvarid(cdfid, MItime),
&time_start, &time_count,
NC_DOUBLE, NULL, frame_times);
}
if (num_frame_widths > 0) {
(void) mivarput(cdfid, ncvarid(cdfid, MItime_width),
&time_start, &time_count,
NC_DOUBLE, NULL, frame_widths);
}
/* Attach the icv */
(void) miicv_attach(icv, cdfid, imgid);
/* Get a buffer for reading images */
image_pix = 1;
for (i=1; i<=image_dims; i++)
image_pix *= end[ndims-i];
pix_size=nctypelen(datatype);
image_size=image_pix*pix_size;
image=malloc(image_size);
/* Loop through the images */
fastdim=ndims-image_dims-1;
if (fastdim<0) fastdim=0;
if (do_vrange) {
ovalid_range[0]=DBL_MAX;
ovalid_range[1]=(-DBL_MAX);
}
/* CJH - July 02 - Skip over any header bytes */
if (skip_length > 0) {
/* First try seeking over the header */
if (fseek(instream, skip_length, SEEK_SET) == 0) {
/* OK instream was a file and we skipped the requested bytes */
}
else {
char buffer[8192];
int bytes_copied = 0;
while ( bytes_copied < skip_length ) {
int bytes_to_read = sizeof(buffer);
if ( bytes_to_read + bytes_copied > skip_length )
bytes_to_read = skip_length - bytes_copied;
nread = fread(buffer, sizeof(char), bytes_to_read, instream);
bytes_copied += nread;
if (nread != bytes_to_read) {
(void) fprintf(stderr, "%s: Premature end of file.\n", pname);
perror("While skipping over small header");
exit(ERROR_STATUS);
}
}
}
}
while (start[0] < end[0]) {
/* Read in image */
nread=fread(image, pix_size, image_pix, instream);
if (nread!=image_pix) {
(void) fprintf(stderr, "%s: Premature end of file.\n", pname);
exit(ERROR_STATUS);
}
/* If the user wants to swap bytes, do it here before any further
* processing of the image.
*/
if (swap_bytes) {
switch (datatype) {
case NC_SHORT:
/* Easy case - call swab() */
if ((image_size & 1) != 0) {
fprintf(stderr,
"%s: image size must be even for -swap_bytes\n",
pname);
exit(ERROR_STATUS);
}
else {
swab(image, image, image_size);
}
break;
case NC_INT:
case NC_FLOAT:
/* Harder case - have to do a more complex 4-byte swap. */
if ((image_size & 3) != 0) {
fprintf(stderr,
"%s: image size must be divisible by 4 for -swap_bytes!\n",
pname);
exit(ERROR_STATUS);
}
else {
unsigned char *img_ptr = image;
unsigned char *img_end = (unsigned char*)image + image_size;
for ( ; img_ptr < img_end; img_ptr += 4) {
unsigned char tmp;
/* Swap the inner 2 bytes
*/
tmp = img_ptr[1];
img_ptr[1] = img_ptr[2];
img_ptr[2] = tmp;
/* Swap the outer 2 bytes
*/
tmp = img_ptr[0];
img_ptr[0] = img_ptr[3];
img_ptr[3] = tmp;
}
}
break;
case NC_DOUBLE :
/* Harder case - have to do a more complex 8-byte swap. */
if ((image_size & 7) != 0) {
fprintf(stderr,
"%s: image size must be divisible by 8 for -swap_bytes!\n",
pname);
exit(ERROR_STATUS);
} else {
unsigned char *img_ptr = image;
unsigned char *img_end = (unsigned char*)image + image_size;
for ( ; img_ptr < img_end; img_ptr += 8) {
unsigned char tmp;
/* Swap bytes [0,1,2,3,4,5,6,7] to [7,6,5,4,3,2,1,0] */
tmp = img_ptr[3];
img_ptr[3] = img_ptr[4];
img_ptr[4] = tmp;
tmp = img_ptr[2];
img_ptr[2] = img_ptr[5];
img_ptr[5] = tmp;
tmp = img_ptr[1];
img_ptr[1] = img_ptr[6];
img_ptr[6] = tmp;
tmp = img_ptr[0];
img_ptr[0] = img_ptr[7];
img_ptr[7] = tmp;
}
}
break;
default:
fprintf(stderr,
"Warning: you specified -swap_bytes, but I can't swap this type of input\n");
break;