GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/slice.c Lines: 15 74 20.3 %
Date: 2017-05-26 Branches: 1 54 1.9 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 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 "Slice along one or more axes at given positions"
28
static const char *_unrrdu_sliceInfoL =
29
  (INFO
30
   ". Output nrrd dimension is less than input nrrd "
31
   "dimension by the number of slice axes (except when the "
32
   "input is or gets down to 1-D). Can slice on all axes "
33
   "in order to sample a single value from the array. "
34
   "Per-axis information is preserved.\n "
35
   "* Uses nrrdSlice (possibly called multiple times)");
36
37
int
38
unrrdu_sliceMain(int argc, const char **argv, const char *me,
39
                 hestParm *hparm) {
40
2
  hestOpt *opt = NULL;
41
1
  char *out, *err;
42
1
  Nrrd *nin, *nout;
43
1
  unsigned int *axis;
44
1
  int pret, axi, axisNum, posNum;
45
1
  size_t pos[NRRD_DIM_MAX];
46
1
  long int *_pos;
47
  airArray *mop;
48
49
1
  hestOptAdd(&opt, "a,axis", "axis", airTypeUInt, 1, -1, &axis, NULL,
50
             "single axis or multiple axes to slice along.  "
51
             "Giving multiple axes here leads to doing multiple slices "
52
             "(at the corresponding positions "
53
             "given with \"-p\"). Multiple axes should be identified "
54
             "in terms of the axis numbering of the original nrrd; as "
55
             "the slices are done (in the given ordering) the actual "
56
             "slice axis will be different if previous slices were on "
57
             "lower-numbered (faster) axes.", &axisNum);
58
1
  hestOptAdd(&opt, "p,position", "pos", airTypeOther, 1, -1, &_pos, NULL,
59
             "position(s) to slice at:\n "
60
             "\b\bo <int> gives 0-based index\n "
61
             "\b\bo M-<int> give index relative "
62
             "to the last sample on the axis (M == #samples-1).",
63
             &posNum, NULL, &unrrduHestPosCB);
64
1
  OPT_ADD_NIN(nin, "input nrrd");
65
1
  OPT_ADD_NOUT(out, "output nrrd");
66
67
1
  mop = airMopNew();
68
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
69
70
2
  USAGE(_unrrdu_sliceInfoL);
71
  PARSE();
72
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
73
  if (axisNum != posNum) {
74
    fprintf(stderr, "%s: # axes %u != # positions %u\n", me, axisNum, posNum);
75
    airMopError(mop); return 1;
76
  }
77
  if (axisNum > NRRD_DIM_MAX) {
78
    fprintf(stderr, "%s: got more slice axes %u than max nrrd dimension %u\n",
79
            me, axisNum, NRRD_DIM_MAX);
80
    airMopError(mop); return 1;
81
  }
82
  for (axi=0; axi<axisNum; axi++) {
83
    char stmp[AIR_STRLEN_SMALL];
84
    if (axisNum > 1) {
85
      sprintf(stmp, "[%d]", axi);
86
    } else {
87
      strcpy(stmp, "");
88
    }
89
    if (!( axis[axi] < nin->dim )) {
90
      fprintf(stderr, "%s: axis%s %d not in range [0,%d]\n",
91
              me, stmp, axis[axi], nin->dim-1);
92
      airMopError(mop); return 1;
93
    }
94
    if (_pos[0 + 2*axi] == -1) {
95
      fprintf(stderr, "%s: pos%s m+<int> spec not meaningful here\n",
96
              me, stmp);
97
      airMopError(mop); return 1;
98
    }
99
    pos[axi] = (_pos[0 + 2*axi]*(nin->axis[axis[axi]].size-1)
100
                + _pos[1 + 2*axi]);
101
    /*
102
    printf("%s: [%d] axis = %u, pos = %u\n", me, axi, axis[axi],
103
           AIR_CAST(unsigned int, pos[axi]));
104
    */
105
  }
106
  /* check on possibly adjust slice axes downward */
107
  if (axisNum > 1) {
108
    int axj;
109
    for (axi=0; axi<axisNum-1; axi++) {
110
      for (axj=axi+1; axj<axisNum; axj++) {
111
        if (axis[axi] == axis[axj]) {
112
          fprintf(stderr, "%s: can't repeat axis: axis[%d] = axis[%d] = %u\n",
113
                  me, axi, axj, axis[axj]);
114
          airMopError(mop); return 1;
115
        }
116
      }
117
    }
118
    for (axi=0; axi<axisNum-1; axi++) {
119
      for (axj=axi+1; axj<axisNum; axj++) {
120
        axis[axj] -= (axis[axj] > axis[axi]);
121
      }
122
    }
123
    /*
124
    for (axi=0; axi<axisNum; axi++) {
125
      printf("%s: axis[%d] = %u\n", me, axi, axis[axi]);
126
    }
127
    */
128
  }
129
130
  nout = nrrdNew();
131
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
132
133
  if (1 == axisNum) {
134
    /* old single axis code */
135
    if (nrrdSlice(nout, nin, axis[0], pos[0])) {
136
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
137
      fprintf(stderr, "%s: error slicing nrrd:\n%s", me, err);
138
      airMopError(mop); return 1;
139
    }
140
  } else {
141
    /* have to do multiple slices */
142
    Nrrd *ntmp[2];
143
    unsigned int tidx = 0;
144
    ntmp[0] = nrrdNew();
145
    airMopAdd(mop, ntmp[0], (airMopper)nrrdNuke, airMopAlways);
146
    ntmp[1] = nrrdNew();
147
    airMopAdd(mop, ntmp[1], (airMopper)nrrdNuke, airMopAlways);
148
    for (axi=0; axi<axisNum; axi++) {
149
      if (nrrdSlice((axi < axisNum-1
150
                     ? ntmp[1-tidx] /* use an ntmp for all but last output */
151
                     : nout),       /* and use nout for last output */
152
                    (0 == axi
153
                     ? nin          /* use nin for only first input */
154
                     : ntmp[tidx]), /* use an ntmp for all but first input */
155
                    axis[axi], pos[axi])) {
156
        airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
157
        fprintf(stderr, "%s: error with slice %d of %d:\n%s", me,
158
                axi+1, axisNum, err);
159
        airMopError(mop); return 1;
160
      }
161
      tidx = 1 - tidx;
162
    }
163
  }
164
165
  SAVE(out, nout, NULL);
166
167
  airMopOkay(mop);
168
  return 0;
169
1
}
170
171
UNRRDU_CMD(slice, INFO);