1 /*
2   Teem: Tools to process and visualize scientific data and images             .
3   Copyright (C) 2012, 2011, 2010, 2009  University of Chicago
4   Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5   Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6 
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public License
9   (LGPL) as published by the Free Software Foundation; either
10   version 2.1 of the License, or (at your option) any later version.
11   The terms of redistributing and/or modifying this software also
12   include exceptions to the LGPL that facilitate static linking.
13 
14   This library is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   Lesser General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public License
20   along with this library; if not, write to Free Software Foundation, Inc.,
21   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23 
24 #include "unrrdu.h"
25 #include "privateUnrrdu.h"
26 
27 #define INFO "Permute slices along one axis"
28 static const char *_unrrdu_shuffleInfoL =
29 (INFO
30  ". Slices along one axis are re-arranged as units "
31  "according to the given permutation (or its inverse). "
32  "The permutation tells which old slice to put at each "
33  "new position.  For example, the shuffle "
34  "0->1,\t1->2,\t2->0 would be \"2 0 1\".  Obviously, "
35  "if you have to rearrange the many slices of a large "
36  "dataset, you should probably store the permutation "
37  "in a plain text file and use it as a "
38  "\"response file\".\n "
39  "* Uses nrrdShuffle");
40 
41 int
unrrdu_shuffleMain(int argc,const char ** argv,const char * me,hestParm * hparm)42 unrrdu_shuffleMain(int argc, const char **argv, const char *me,
43                    hestParm *hparm) {
44   hestOpt *opt = NULL;
45   char *out, *err;
46   Nrrd *nin, *nout;
47   unsigned int di, axis, permLen, *perm, *iperm, *whichperm;
48   size_t *realperm;
49   int inverse, pret;
50   airArray *mop;
51 
52   /* so that long permutations can be read from file */
53   hparm->respFileEnable = AIR_TRUE;
54 
55   hestOptAdd(&opt, "p,permute", "slc0 slc1", airTypeUInt, 1, -1, &perm, NULL,
56              "new slice ordering", &permLen);
57   hestOptAdd(&opt, "inv,inverse", NULL, airTypeInt, 0, 0, &inverse, NULL,
58              "use inverse of given permutation");
59   OPT_ADD_AXIS(axis, "axis to shuffle along");
60   OPT_ADD_NIN(nin, "input nrrd");
61   OPT_ADD_NOUT(out, "output nrrd");
62 
63   mop = airMopNew();
64   airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
65 
66   USAGE(_unrrdu_shuffleInfoL);
67   PARSE();
68   airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
69 
70   nout = nrrdNew();
71   airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
72 
73   /* we have to do error checking on axis in order to do error
74      checking on length of permutation */
75   if (!( axis < nin->dim )) {
76     fprintf(stderr, "%s: axis %d not in valid range [0,%d]\n",
77             me, axis, nin->dim-1);
78     airMopError(mop);
79     return 1;
80   }
81   if (!( permLen == nin->axis[axis].size )) {
82     char stmp[AIR_STRLEN_SMALL];
83     fprintf(stderr, "%s: permutation length (%u) != axis %d's size (%s)\n",
84             me, permLen, axis,
85             airSprintSize_t(stmp, nin->axis[axis].size));
86     airMopError(mop);
87     return 1;
88   }
89   if (inverse) {
90     iperm = AIR_CALLOC(permLen, unsigned int);
91     airMopAdd(mop, iperm, airFree, airMopAlways);
92     if (nrrdInvertPerm(iperm, perm, permLen)) {
93       fprintf(stderr,
94               "%s: couldn't compute inverse of given permutation\n", me);
95       airMopError(mop);
96       return 1;
97     }
98     whichperm = iperm;
99   } else {
100     whichperm = perm;
101   }
102 
103   realperm = AIR_CALLOC(permLen, size_t);
104   airMopAdd(mop, realperm, airFree, airMopAlways);
105   for (di=0; di<permLen; di++) {
106     realperm[di] = whichperm[di];
107   }
108   if (nrrdShuffle(nout, nin, axis, realperm)) {
109     airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
110     fprintf(stderr, "%s: error shuffling nrrd:\n%s", me, err);
111     airMopError(mop);
112     return 1;
113   }
114 
115   SAVE(out, nout, NULL);
116 
117   airMopOkay(mop);
118   return 0;
119 }
120 
121 UNRRDU_CMD(shuffle, INFO);
122