-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmincreshape.c
1813 lines (1630 loc) · 65.2 KB
/
mincreshape.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 : mincreshape
@INPUT : argc, argv - command line arguments
@OUTPUT : (none)
@RETURNS : error status
@DESCRIPTION: Program to allow reshaping of minc lattices: selecting a
a subrange (or superrange) of dimension indices, eliminating
dimensions, or re-ordering axes. As well, all icv conversions
are made accessible on the command line.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 10, 1994 (Peter Neelin)
@MODIFIED :
* $Log: mincreshape.c,v $
* Revision 6.14 2008-01-17 02:33:02 rotor
* * removed all rcsids
* * removed a bunch of ^L's that somehow crept in
* * removed old (and outdated) BUGS file
*
* Revision 6.13 2008/01/13 09:38:54 stever
* Avoid compiler warnings about functions and variables that are defined
* but not used. Remove some such functions and variables,
* conditionalize some, and move static declarations out of header files
* into C files.
*
* Revision 6.12 2008/01/12 19:08:15 stever
* Add __attribute__ ((unused)) to all rcsid variables.
*
* Revision 6.11 2005/08/26 21:07:18 bert
* Use #if rather than #ifdef with MINC2 symbol, and be sure to include config.h whereever MINC2 is used
*
* Revision 6.10 2004/11/01 22:38:39 bert
* Eliminate all references to minc_def.h
*
* Revision 6.9 2004/04/27 15:30:02 bert
* Add -2 flag
*
* Revision 6.8 2001/11/13 14:16:50 neelin
* Get correct valid range for conversion from int to float types
*
* Revision 6.7 2001/09/18 15:32:53 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:36 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:45 neelin
* Replaced NC_NAT with MI_ORIGINAL_TYPE.
*
* Revision 6.4 2001/04/17 18:40:24 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:29 neelin
* Fixed Log subsitutions for CVS
*
* Revision 6.2 1998/08/19 13:57:23 neelin
* Fixed argument parsing to detect errors such as octal numbers
* containing illegal digits (08) and extraneous characters on the end of
* arguments.
*
* Revision 6.0 1997/09/12 13:24:12 neelin
* Release of minc version 0.6
*
* Revision 5.0 1997/08/21 13:25:10 neelin
* Release of minc version 0.5
*
* Revision 4.0 1997/05/07 20:01:44 neelin
* Release of minc version 0.4
*
* Revision 3.0 1995/05/15 19:32:36 neelin
* Release of minc version 0.3
*
* Revision 1.6 1995/02/09 14:08:24 neelin
* Mods to make irix 5 lint happy.
*
* Revision 1.5 1995/02/08 19:31:47 neelin
* Moved ARGSUSED statements for irix 5 lint.
*
* Revision 1.4 1994/11/23 11:47:05 neelin
* Handle image-min/max properly when using icv for normalization.
*
* Revision 1.3 94/11/22 08:46:09 neelin
* Fixed handling of normalization for number of image dimensions > 2.
* Added appropriate default values of image-max and image-min.
*
* Revision 1.2 94/11/03 08:48:20 neelin
* Allow chunk_count to have sizes less than full block size.
*
* Revision 1.1 94/11/02 16:21:24 neelin
* Initial revision
*
@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.
---------------------------------------------------------------------------- */
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <float.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <minc.h>
#include <ParseArgv.h>
#include <time_stamp.h>
#include "mincreshape.h"
static void get_arginfo(int argc, char *argv[],
Reshape_info *reshape_info);
static int get_fillvalue(char *dst, char *key, char *nextArg);
static int get_dimsize(char *dst, char *key, char *nextArg);
static int get_axis_order(char *dst, char *key, char *nextArg);
static int get_axis_range(char *dst, char *key, char *nextArg);
static int get_arg_vector(char *dst, char *key, char *nextArg);
static void get_default_datatype(int mincid, nc_type *datatype, int *is_signed,
double valid_range[2]);
static void setup_dim_sizes(int icvid, int mincid, Dimsize_list *dimsize_list);
static void setup_reshaping_info(int icvid, int mincid,
int do_norm, double fillvalue, int do_scalar,
char *axis_order[], Axis_ranges *axis_ranges,
long hs_start[], long hs_count[],
int max_chunk_size_in_kb,
Reshape_info *reshape_info);
static void setup_output_file(int mincid, char *history,
Reshape_info *reshape_info);
static void create_dim_var(int outmincid, int outdimid,
int inicvid, int cur_image_dim, int inmincid,
long input_start, long input_count);
static void copy_dimension_values(int outmincid, int outdimid, int inmincid,
long input_start, long input_count);
static void copy_dim_var_values(int outmincid, char *dimname, char *varname,
int inmincid,
long input_start, long input_count);
/* Main program */
int main(int argc, char *argv[])
{
Reshape_info reshape_info;
int result_code;
/* Get argument information and create the output file */
get_arginfo(argc, argv, &reshape_info);
/* Copy the data */
result_code = copy_data(&reshape_info);
/* Close the output file */
(void) miattputstr(reshape_info.outmincid, reshape_info.outimgid,
MIcomplete, MI_TRUE);
(void) miclose(reshape_info.outmincid);
(void) miclose(reshape_info.inmincid);
exit(result_code);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_arginfo
@INPUT : argc - number of command-line arguments
argv - command-line arguments
@OUTPUT : reshape_info - information for reshaping file
@RETURNS : (nothing)
@DESCRIPTION: Routine to get information from arguments about input and
output files and reshaping.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 11, 1994 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static void get_arginfo(int argc, char *argv[],
Reshape_info *reshape_info)
{
/* Argument variables */
static int clobber = FALSE;
static int verbose = TRUE;
static nc_type datatype = MI_ORIGINAL_TYPE;
static int is_signed = INT_MIN;
static double valid_range[2] = {DBL_MAX,DBL_MAX};
static double image_range[2] = {DBL_MAX,DBL_MAX};
static int do_norm = FALSE;
static double pixfillvalue = FILL;
static int do_scalar= FALSE;
static int direction = MI_ICV_ANYDIR;
static int xdirection = INT_MIN;
static int ydirection = INT_MIN;
static int zdirection = INT_MIN;
static int keep_aspect = FALSE;
static int image_size = MI_ICV_ANYSIZE;
static int row_size = MI_ICV_ANYSIZE;
static int col_size = MI_ICV_ANYSIZE;
static Dimsize_list dimsize_list = {0, {NULL}, {0}};
static char *axis_order[MAX_VAR_DIMS+1];
static Axis_ranges axis_ranges = {0, {NULL}, {0}, {0}};
static long hs_start[MAX_VAR_DIMS] = {LONG_MIN};
static long hs_count[MAX_VAR_DIMS] = {LONG_MIN};
static double fillvalue = NOFILL;
static int max_chunk_size_in_kb = DEFAULT_MAX_CHUNK_SIZE_IN_KB;
#if MINC2
static int minc2_format = 0;
#endif /* MINC2 */
int cflags; /* File creation flags */
/* Argument table */
static ArgvInfo argTable[] = {
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
"General options:"},
{"-clobber", ARGV_CONSTANT, (char *) TRUE, (char *) &clobber,
"Overwrite existing file."},
{"-noclobber", ARGV_CONSTANT, (char *) FALSE, (char *) &clobber,
"Do not overwrite existing file (default)."},
{"-verbose", ARGV_CONSTANT, (char *) TRUE, (char *) &verbose,
"Print out log messages as processing is being done (default).\n"},
{"-quiet", ARGV_CONSTANT, (char *) FALSE, (char *) &verbose,
"Do not print out any log messages."},
{"-max_chunk_size_in_kb", ARGV_INT, (char *) 0,
(char *) &max_chunk_size_in_kb,
"Specify the maximum size of the copy buffer (in kbytes)."},
#if MINC2
{"-2", ARGV_CONSTANT, (char *) TRUE, (char *)&minc2_format,
"Produce a MINC 2.0 format output file."},
#endif /* MINC2 */
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
"Image conversion options (pixel type and range):"},
{"-filetype", ARGV_CONSTANT, (char *) MI_ORIGINAL_TYPE, (char *) &datatype,
"Don't do any type conversion (default)."},
{"-byte", ARGV_CONSTANT, (char *) NC_BYTE, (char *) &datatype,
"Convert to byte data"},
{"-short", ARGV_CONSTANT, (char *) NC_SHORT, (char *) &datatype,
"Convert to short integer data"},
{"-int", ARGV_CONSTANT, (char *) NC_INT, (char *) &datatype,
"Convert to 32-bit integer data"},
{"-long", ARGV_CONSTANT, (char *) NC_INT, (char *) &datatype,
"Superseded by -int"},
{"-float", ARGV_CONSTANT, (char *) NC_FLOAT, (char *) &datatype,
"Convert to single-precision floating-point data"},
{"-double", ARGV_CONSTANT, (char *) NC_DOUBLE, (char *) &datatype,
"Convert to double-precision floating-point data"},
{"-signed", ARGV_CONSTANT, (char *) TRUE, (char *) &is_signed,
"Convert to signed integer data"},
{"-unsigned", ARGV_CONSTANT, (char *) FALSE, (char *) &is_signed,
"Convert to unsigned integer data"},
{"-valid_range", ARGV_FLOAT, (char *) 2, (char *) valid_range,
"Valid range for output data (pixel values)"},
{"-image_range", ARGV_FLOAT, (char *) 2, (char *) image_range,
"Normalize images to a given minimum and maximum"},
{"-normalize", ARGV_CONSTANT, (char *) TRUE, (char *) &do_norm,
"Normalize images to file minimum and maximum."},
{"-nonormalize", ARGV_CONSTANT, (char *) FALSE, (char *) &do_norm,
"Do not normalize images (default)."},
{"-nopixfill", ARGV_FUNC, (char *) get_fillvalue,
(char *) &pixfillvalue,
"Do not convert out-of-range values in input file."},
{"-pixfill", ARGV_FUNC, (char *) get_fillvalue,
(char *) &pixfillvalue,
"Replace out-of-range values in input file by smallest value (default)."},
{"-pixfillvalue", ARGV_FLOAT, (char *) 0,
(char *) &pixfillvalue,
"Specify new value to replace out-of-range values in input file."},
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
"Image conversion options (dimension direction and size):"},
{"-scalar", ARGV_CONSTANT, (char *) TRUE, (char *) &do_scalar,
"Convert vector images to scalar images."},
{"-noscalar", ARGV_CONSTANT, (char *) FALSE, (char *) &do_scalar,
"Do not convert vector images to scalar images (default)."},
{"+direction", ARGV_CONSTANT, (char *) MI_ICV_POSITIVE,
(char *) &direction,
"Flip images to give positive step value for spatial axes."},
{"-direction", ARGV_CONSTANT, (char *) MI_ICV_NEGATIVE,
(char *) &direction,
"Flip images to give negative step value for spatial axes."},
{"-anydirection", ARGV_CONSTANT, (char *) MI_ICV_ANYDIR,
(char *) &direction,
"Don't flip images along spatial axes (default)."},
{"+xdirection", ARGV_CONSTANT, (char *) MI_ICV_POSITIVE,
(char *) &xdirection,
"Flip images to give positive xspace:step value (left-to-right)."},
{"-xdirection", ARGV_CONSTANT, (char *) MI_ICV_NEGATIVE,
(char *) &xdirection,
"Flip images to give negative xspace:step value (right-to-left)."},
{"-xanydirection", ARGV_CONSTANT, (char *) MI_ICV_ANYDIR,
(char *) &xdirection,
"Don't flip images along x-axis."},
{"+ydirection", ARGV_CONSTANT, (char *) MI_ICV_POSITIVE,
(char *) &ydirection,
"Flip images to give positive yspace:step value (post-to-ant)."},
{"-ydirection", ARGV_CONSTANT, (char *) MI_ICV_NEGATIVE,
(char *) &ydirection,
"Flip images to give negative yspace:step value (ant-to-post)."},
{"-yanydirection", ARGV_CONSTANT, (char *) MI_ICV_ANYDIR,
(char *) &ydirection,
"Don't flip images along y-axis."},
{"+zdirection", ARGV_CONSTANT, (char *) MI_ICV_POSITIVE,
(char *) &zdirection,
"Flip images to give positive zspace:step value (inf-to-sup)."},
{"-zdirection", ARGV_CONSTANT, (char *) MI_ICV_NEGATIVE,
(char *) &zdirection,
"Flip images to give negative zspace:step value (sup-to-inf)."},
{"-zanydirection", ARGV_CONSTANT, (char *) MI_ICV_ANYDIR,
(char *) &zdirection,
"Don't flip images along z-axis."},
{"-keepaspect", ARGV_CONSTANT, (char *) TRUE, (char *) &keep_aspect,
"Preserve aspect ratio when resizing images."},
{"-nokeepaspect", ARGV_CONSTANT, (char *) FALSE, (char *) &keep_aspect,
"Do not preserve aspect ratio when resizing images (default)."},
{"-imgsize", ARGV_INT, (char *) 0, (char *) &image_size,
"Specify the desired image size."},
{"-rowsize", ARGV_INT, (char *) 0, (char *) &row_size,
"Specify the desired number of rows in the image."},
{"-colsize", ARGV_INT, (char *) 0, (char *) &col_size,
"Specify the desired number of columns in the image."},
{"-dimsize", ARGV_FUNC, (char *) get_dimsize,
(char *) &dimsize_list,
"Specify the size of a named dimension (<dimension>=<size>)."},
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
"Reshaping options:"},
{"-transverse", ARGV_FUNC, (char *) get_axis_order,
(char *) axis_order,
"Write out transverse slices"},
{"-sagittal", ARGV_FUNC, (char *) get_axis_order,
(char *) axis_order,
"Write out sagittal slices"},
{"-coronal", ARGV_FUNC, (char *) get_axis_order,
(char *) axis_order,
"Write out coronal slices"},
{"-dimorder", ARGV_FUNC, (char *) get_axis_order,
(char *) axis_order,
"Specify dimension order (<dim1>,<dim2>,<dim3>,...)."},
{"-dimrange", ARGV_FUNC, (char *) get_axis_range,
(char *) &axis_ranges,
"Specify range of dimension subscripts (<dim>=<start>[,<count>])."},
{"-start", ARGV_FUNC, (char *) get_arg_vector, (char *) hs_start,
"Specifies corner of hyperslab (C conventions for indices)"},
{"-count", ARGV_FUNC, (char *) get_arg_vector, (char *) hs_count,
"Specifies edge lengths of hyperslab to read"},
{NULL, ARGV_HELP, (char *) NULL, (char *) NULL,
"Missing data options:"},
{"-nofill", ARGV_FUNC, (char *) get_fillvalue,
(char *) &fillvalue,
"Use value zero for points outside of input volume (default)"},
{"-fill", ARGV_FUNC, (char *) get_fillvalue,
(char *) &fillvalue,
"Use a fill value for points outside of input volume"},
{"-fillvalue", ARGV_FLOAT, (char *) 0,
(char *) &fillvalue,
"Specify a fill value for points outside of input volume"},
{NULL, ARGV_END, NULL, NULL, NULL}
};
/* Other variables */
char *infile, *outfile;
char *history, *pname;
int icvid;
/* Get the history information and program name */
history = time_stamp(argc, argv);
pname = argv[0];
/* Call ParseArgv */
if (ParseArgv(&argc, argv, argTable, 0) || (argc!=3)) {
(void) fprintf(stderr,
"\nUsage: %s [<options>] <infile> <outfile>\n", pname);
(void) fprintf(stderr,
" %s [-help]\n\n", pname);
exit(EXIT_FAILURE);
}
infile = argv[1];
outfile = argv[2];
/* check for the infile */
if(access(infile, F_OK) != 0){
fprintf(stderr, "%s: Couldn't find %s\n\n", argv[0], infile);
exit(EXIT_FAILURE);
}
/* check for output file */
if(access(outfile, F_OK) == 0 && !clobber) {
fprintf(stderr, "%s: %s exists, use -clobber to overwrite\n\n",
argv[0], outfile);
exit(EXIT_FAILURE);
}
/* Save verbose setting */
reshape_info->verbose = verbose;
/* Check max chunk size value */
if (max_chunk_size_in_kb <= 0) {
(void) fprintf(stderr, "Illegal value for max_chunk_size (%d)\n",
max_chunk_size_in_kb);
exit(EXIT_FAILURE);
}
/* Check the x, y and z directions */
if (xdirection == INT_MIN) xdirection = direction;
if (ydirection == INT_MIN) ydirection = direction;
if (zdirection == INT_MIN) zdirection = direction;
/* Check the row and column size */
if (row_size == MI_ICV_ANYSIZE) row_size = image_size;
if (col_size == MI_ICV_ANYSIZE) col_size = image_size;
/* Check for normalization to specified range */
if (image_range[0] != DBL_MAX) do_norm = TRUE;
/* Open the input file */
reshape_info->inmincid = miopen(infile, NC_NOWRITE);
/* Get the default datatype */
get_default_datatype(reshape_info->inmincid, &datatype, &is_signed,
valid_range);
reshape_info->output_datatype = datatype;
reshape_info->output_is_signed = is_signed;
/* Create the icv */
reshape_info->icvid = miicv_create();
icvid = reshape_info->icvid;
/* Set the icv properties */
/* Set datatype properties (get min and max for type from icv) */
(void) miicv_setint(icvid, MI_ICV_TYPE, datatype);
if (is_signed != INT_MIN) {
if (is_signed)
(void) miicv_setstr(icvid, MI_ICV_SIGN, MI_SIGNED);
else
(void) miicv_setstr(icvid, MI_ICV_SIGN, MI_UNSIGNED);
}
if (valid_range[0] != DBL_MAX) {
(void) miicv_setdbl(icvid, MI_ICV_VALID_MIN, valid_range[0]);
(void) miicv_setdbl(icvid, MI_ICV_VALID_MAX, valid_range[1]);
}
/* Check for normalization */
(void) miicv_setint(icvid, MI_ICV_DO_NORM, do_norm);
(void) miicv_setint(icvid, MI_ICV_USER_NORM, (image_range[0] != DBL_MAX));
(void) miicv_setdbl(icvid, MI_ICV_IMAGE_MIN, image_range[0]);
(void) miicv_setdbl(icvid, MI_ICV_IMAGE_MAX, image_range[1]);
/* Check for pixel fill value conversion */
(void) miicv_setint(icvid, MI_ICV_DO_FILLVALUE,
(pixfillvalue != NOFILL));
(void) miicv_setdbl(icvid, MI_ICV_FILLVALUE, pixfillvalue);
/* Set up for dimension conversion */
(void) miicv_setint(icvid, MI_ICV_DO_DIM_CONV, TRUE);
(void) miicv_setint(icvid, MI_ICV_DO_SCALAR, do_scalar);
(void) miicv_setint(icvid, MI_ICV_XDIM_DIR, xdirection);
(void) miicv_setint(icvid, MI_ICV_YDIM_DIR, ydirection);
(void) miicv_setint(icvid, MI_ICV_ZDIM_DIR, zdirection);
/* Set up for image resizing */
(void) miicv_setint(icvid, MI_ICV_KEEP_ASPECT, keep_aspect);
(void) miicv_setint(icvid, MI_ICV_ADIM_SIZE, col_size);
(void) miicv_setint(icvid, MI_ICV_BDIM_SIZE, row_size);
setup_dim_sizes(icvid, reshape_info->inmincid, &dimsize_list);
/* Save reshaping information */
setup_reshaping_info(icvid, reshape_info->inmincid,
do_norm, fillvalue, do_scalar,
axis_order, &axis_ranges, hs_start, hs_count,
max_chunk_size_in_kb,
reshape_info);
/* Attach the icv */
(void) miicv_attach(icvid, reshape_info->inmincid,
ncvarid(reshape_info->inmincid, MIimage));
/* Create the output file */
if (clobber) {
cflags = NC_CLOBBER;
}
else {
cflags = NC_NOCLOBBER;
}
#if MINC2
if (minc2_format) {
cflags |= MI2_CREATE_V2;
}
#endif /* MINC2 */
reshape_info->outmincid = micreate(outfile, cflags);
setup_output_file(reshape_info->outmincid, history, reshape_info);
free(history);
return;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_fillvalue
@INPUT : dst - Pointer to client data from argument table
key - argument key
nextArg - argument following key
@OUTPUT : (nothing)
@RETURNS : FALSE so that ParseArgv will not discard nextArg
@DESCRIPTION: Routine called by ParseArgv to set the fill value
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 16, 1994 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_fillvalue(char *dst, char *key, char *nextArg)
/* ARGSUSED */
{
double *dptr;
/* Get pointer to client data */
dptr = (double *) dst;
/* Check key for fill value to set */
if ((strcmp(key, "-fill") == 0) ||
(strcmp(key, "-pixfill") == 0)) {
*dptr = FILL;
}
else if ((strcmp(key, "-nofill") == 0) ||
(strcmp(key, "-nopixfill") == 0)) {
*dptr = NOFILL;
}
return FALSE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_dimsize
@INPUT : dst - Pointer to client data from argument table
key - argument key
nextArg - argument following key
@OUTPUT : (nothing)
@RETURNS : TRUE so that ParseArgv will discard nextArg
@DESCRIPTION: Routine called by ParseArgv to get a dimension size
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 16, 1994 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_dimsize(char *dst, char *key, char *nextArg)
/* ARGSUSED */
{
Dimsize_list *dimsize_list;
char *size_string;
int ientry;
char *cur;
/* Get pointer to client data */
dimsize_list = (Dimsize_list *) dst;
/* Check for next argument */
if (nextArg == NULL) {
(void) fprintf(stderr,
"\"%s\" option requires an additional argument\n",
key);
exit(EXIT_FAILURE);
}
/* Check that we have enough space in the list */
if (dimsize_list->nentries >= MAX_VAR_DIMS) {
(void) fprintf(stderr, "Too many \"%s\" options.\n", key);
exit(EXIT_FAILURE);
}
ientry = dimsize_list->nentries;
/* Parse the argument (<dim name>=<size>) */
/* Remove leading space */
while (ISSPACE(*nextArg)) nextArg++;
dimsize_list->name[ientry] = nextArg;
/* Find the '=' */
size_string = strchr(nextArg, '=');
if ((size_string == NULL) || (size_string == nextArg)) {
(void) fprintf(stderr,
"\"%s\" option requires the argument <dim>=<size>\n",
key);
exit(EXIT_FAILURE);
}
/* Remove trailing blanks on name */
cur = size_string - 1;
while ((cur>=nextArg) && ISSPACE(*cur)) cur--;
cur++;
*cur = '\0';
/* Get the size */
size_string++;
dimsize_list->size[ientry] = ParseLong(size_string, &cur);
if (cur == size_string) {
(void) fprintf(stderr,
"\"%s\" option requires the argument <dim>=<size>\n",
key);
exit(EXIT_FAILURE);
}
/* Check for extra stuff (spaces are allowed) */
while (ISSPACE(*cur)) cur++;
if (*cur != '\0') {
(void) fprintf(stderr,
"\"%s\" option requires the argument <dim>=<size>\n",
key);
exit(EXIT_FAILURE);
}
dimsize_list->nentries++;
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_axis_order
@INPUT : dst - Pointer to client data from argument table
key - argument key
nextArg - argument following key
@OUTPUT : (nothing)
@RETURNS : TRUE or FALSE (so that ParseArgv will discard nextArg only
when needed)
@DESCRIPTION: Routine called by ParseArgv to set the axis order
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 16, 1994 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_axis_order(char *dst, char *key, char *nextArg)
/* ARGSUSED */
{
char **axis_order;
char *cur;
int ndims;
/* Get pointer to client data */
axis_order = (char **) dst;
/* Check key */
if (strcmp(key, "-transverse") == 0) {
axis_order[0] = MIzspace;
axis_order[1] = MIyspace;
axis_order[2] = MIxspace;
return FALSE;
}
if (strcmp(key, "-sagittal") == 0) {
axis_order[0] = MIxspace;
axis_order[1] = MIzspace;
axis_order[2] = MIyspace;
return FALSE;
}
if (strcmp(key, "-coronal") == 0) {
axis_order[0] = MIyspace;
axis_order[1] = MIzspace;
axis_order[2] = MIxspace;
return FALSE;
}
/* Make sure that we have a "-dimorder" argument */
if (strcmp(key, "-dimorder") != 0) {
(void) fprintf(stderr,
"Unrecognized option \"%s\": internal program error.\n",
key);
exit(EXIT_FAILURE);
}
/* Check for next argument */
if (nextArg == NULL) {
(void) fprintf(stderr,
"\"%s\" option requires an additional argument\n",
key);
exit(EXIT_FAILURE);
}
/* Set up pointers to end of string and first non-space character */
cur = nextArg;
while (ISSPACE(*cur)) cur++;
ndims = 0;
/* Loop through string looking for space or comma-separated names */
while ((ndims < MAX_VAR_DIMS) && (*cur!='\0')) {
/* Get string */
axis_order[ndims] = cur;
/* Search for end of dimension name */
while (!ISSPACE(*cur) && (*cur != ARG_SEPARATOR) &&
(*cur != '\0')) cur++;
if (*cur != '\0') {
*cur = '\0';
cur++;
}
ndims++;
/* Skip any spaces */
while (ISSPACE(*cur)) cur++;
}
/* Terminate list with NULL */
axis_order[ndims] = NULL;
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_axis_range
@INPUT : dst - Pointer to client data from argument table
key - argument key
nextArg - argument following key
@OUTPUT : (nothing)
@RETURNS : TRUE so that ParseArgv will discard nextArg
@DESCRIPTION: Routine called by ParseArgv to set the axis range
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 16, 1994 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_axis_range(char *dst, char *key, char *nextArg)
/* ARGSUSED */
{
Axis_ranges *axis_ranges;
int ientry;
char *num_string, *cur;
/* Get pointer to client data */
axis_ranges = (Axis_ranges *) dst;
/* Check for next argument */
if (nextArg == NULL) {
(void) fprintf(stderr,
"\"%s\" option requires an additional argument\n",
key);
exit(EXIT_FAILURE);
}
/* Check that we have enough space in the list */
if (axis_ranges->nentries >= MAX_VAR_DIMS) {
(void) fprintf(stderr, "Too many \"%s\" options.\n", key);
exit(EXIT_FAILURE);
}
ientry = axis_ranges->nentries;
/* Parse the argument (<dim name>=<start>,[<count>]) */
/* Remove leading space */
while (ISSPACE(*nextArg)) nextArg++;
axis_ranges->name[ientry] = nextArg;
/* Find the '=' */
num_string = strchr(nextArg, '=');
if ((num_string == NULL) || (num_string == nextArg)) {
(void) fprintf(stderr,
"\"%s\" option requires the argument <dim>=<start>[,<count>]\n",
key);
exit(EXIT_FAILURE);
}
/* Remove trailing blanks on name */
cur = num_string - 1;
while ((cur>=nextArg) && ISSPACE(*cur)) cur--;
cur++;
*cur = '\0';
/* Get the start */
num_string++;
axis_ranges->start[ientry] = ParseLong(num_string, &cur);
if ((cur == num_string) ||
!(ISSPACE(*cur) || (*cur == ARG_SEPARATOR) || (*cur == '\0'))) {
(void) fprintf(stderr,
"\"%s\" option requires the argument <dim>=<start>[,<count>]\n",
key);
exit(EXIT_FAILURE);
}
/* Skip any spaces */
while (ISSPACE(*cur)) cur++;
/* Skip an optional comma */
if (*cur == ARG_SEPARATOR) cur++;
/* Look for a count string */
num_string = cur;
axis_ranges->count[ientry] = ParseLong(num_string, &cur);
if ((cur == num_string) || (axis_ranges->count[ientry] == 0)) {
axis_ranges->count[ientry] = 0;
}
axis_ranges->nentries++;
/* Check for extra stuff (spaces are allowed) */
while (ISSPACE(*cur)) cur++;
if (*cur != '\0') {
(void) fprintf(stderr,
"\"%s\" option requires the argument <dim>=<start>[,<count>]\n",
key);
exit(EXIT_FAILURE);
}
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_arg_vector
@INPUT : key - argv key string (-start, -count)
nextArg - string from which vector should be read
@OUTPUT : dst - pointer to vector of longs into which values should
be written (padded with LONG_MIN)
@RETURNS : TRUE, since nextArg is used (unless it is NULL)
@DESCRIPTION: Parses a command-line argument into a vector of longs. The
string should contain at most MAX_VAR_DIMS comma separated
integer values (spaces are skipped).
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : June 10, 1993 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static int get_arg_vector(char *dst, char *key, char *nextArg)
/* ARGSUSED */
{
long *vector;
int nvals, i;
char *cur, *end, *prev;
/* Check for following argument */
if (nextArg == NULL) {
(void) fprintf(stderr,
"\"%s\" option requires an additional argument\n",
key);
return FALSE;
}
/* Get pointer to vector of longs */
vector = (long *) dst;
/* Set up pointers to end of string and first non-space character */
end = nextArg + strlen(nextArg);
cur = nextArg;
while (ISSPACE(*cur)) cur++;
nvals = 0;
/* Loop through string looking for integers */
while ((nvals < MAX_VAR_DIMS) && (cur!=end)) {
/* Get integer */
prev = cur;
vector[nvals] = ParseLong(prev, &cur);
if ((cur == prev) ||
!(ISSPACE(*cur) || (*cur == VECTOR_SEPARATOR) || (*cur == '\0'))) {
(void) fprintf(stderr,
"expected vector of integers for \"%s\", but got \"%s\"\n",
key, nextArg);
exit(EXIT_FAILURE);
}
nvals++;
/* Skip any spaces */
while (ISSPACE(*cur)) cur++;
/* Skip an optional comma */
if (*cur == VECTOR_SEPARATOR) cur++;
}
/* Pad with LONG_MIN */
for (i=nvals; i < MAX_VAR_DIMS; i++) {
vector[i] = LONG_MIN;
}
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_default_datatype
@INPUT : mincid - id of input minc file
@OUTPUT : datatype - datatype of file
is_signed - TRUE if data is signed, FALSE if not. Defaults
to FALSE for byte, TRUE otherwise.
valid_range - DBL_MAX if not known
@RETURNS : (nothing)
@DESCRIPTION: Routine to get the datatype info from a file. If datatype
is not MI_ORIGINAL_TYPE, then is_signed only is set to its
default. Otherwise, is_signed is only modified if it is set
to INT_MIN and valid_range is only modified if it is set to
DBL_MAX.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 16, 1994 (Peter Neelin)
@MODIFIED :
---------------------------------------------------------------------------- */
static void get_default_datatype(int mincid, nc_type *datatype, int *is_signed,
double valid_range[2])
{
int imgid;
int file_is_signed;
nc_type file_datatype;
int found_range, file_integer_type, integer_type;
/* Get the image variable id */
imgid = ncvarid(mincid, MIimage);
/* Get the file data type */
(void) miget_datatype(mincid, imgid, &file_datatype, &file_is_signed);
/* Get the type and sign. If a type is specified, then the sign should
be the default for that type. If it is not specified, then use
the default from the file. */
if (*datatype != MI_ORIGINAL_TYPE) {
if (*is_signed == INT_MIN) {
*is_signed = ((*datatype == NC_BYTE) ? FALSE : TRUE);
}
}
else {
*datatype = file_datatype;
if (*is_signed == INT_MIN) {
*is_signed = file_is_signed;
}
}
/* Set the valid_range if needed. For integer types, just get the
default for the type. For float, get the valid range of the
input if it is also float and is specified, otherwise get the
image range. */
if (valid_range[0] == DBL_MAX) {
/* Test for integer types */
integer_type = (*datatype != NC_FLOAT && *datatype != NC_DOUBLE);
file_integer_type =
(file_datatype != NC_FLOAT && file_datatype != NC_DOUBLE);
/* Integer type */
if (integer_type) {
(void) miget_default_range(*datatype, *is_signed, valid_range);
}
/* Float type */
else {
found_range = FALSE;
/* Just get input valid_range for float input */
if (!file_integer_type) {
(void) miget_valid_range(mincid, imgid, valid_range);
if (file_datatype == NC_FLOAT)
found_range = (valid_range[1] != FLT_MAX);
else if (file_datatype == NC_DOUBLE)
found_range = (valid_range[1] != DBL_MAX);
}
/* If not float input, or did not find valid_range, then
get it from the image range (image-max/min) */
if (!found_range) {
/* Get the range if the variables exist, otherwise use
the default */
if (mivar_exists(mincid, MIimagemax) &&
mivar_exists(mincid, MIimagemin)) {
(void) miget_image_range(mincid, valid_range);
}
else if (!file_integer_type) {
(void) miget_default_range(*datatype, *is_signed,
valid_range);
}
else {
valid_range[0] = MI_DEFAULT_MIN;
valid_range[1] = MI_DEFAULT_MAX;
}
}
}