GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/crop.c Lines: 17 72 23.6 %
Date: 2017-05-26 Branches: 1 42 2.4 %

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 "Crop along each axis to make a smaller nrrd"
28
static const char *_unrrdu_cropInfoL =
29
  (INFO ".\n "
30
   "* Uses nrrdCrop");
31
32
int
33
unrrdu_cropMain(int argc, const char **argv, const char *me,
34
                hestParm *hparm) {
35
2
  hestOpt *opt = NULL;
36
1
  char *out, *err;
37
1
  Nrrd *nin, *nout;
38
  unsigned int ai;
39
1
  int minLen, maxLen, pret;
40
1
  long int *minOff, *maxOff;
41
1
  size_t min[NRRD_DIM_MAX], max[NRRD_DIM_MAX];
42
  airArray *mop;
43
1
  Nrrd *_nbounds;
44
45
1
  OPT_ADD_BOUND("min,minimum", 0, minOff, "0",
46
                "low corner of bounding box.\n "
47
                "\b\bo <int> gives 0-based index\n "
48
                "\b\bo M, M+<int>, M-<int> give index relative "
49
                "to the last sample on the axis (M == #samples-1).",
50
                minLen);
51
1
  OPT_ADD_BOUND("max,maximum", 0, maxOff, "0",
52
                "high corner of bounding box.  "
53
                "Besides the specification styles described above, "
54
                "there's also:\n "
55
                "\b\bo m+<int> give index relative to minimum.",
56
                maxLen);
57
1
  hestOptAdd(&opt, "b,bounds", "filename", airTypeOther, 1, 1,
58
             &_nbounds, "",
59
             "a filename given here overrides the -min and -max "
60
             "options (they don't need to be used) and provides the "
61
             "cropping bounds as a 2-D array; first scanline is for "
62
             "-min, second is for -max. Unfortunately the "
63
             "\"m\" and \"M\" semantics (above) are currently not "
64
             "supported in the bounds file.",
65
1
             NULL, NULL, nrrdHestNrrd);
66
1
  OPT_ADD_NIN(nin, "input nrrd");
67
1
  OPT_ADD_NOUT(out, "output nrrd");
68
69
1
  mop = airMopNew();
70
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
71
72
2
  USAGE(_unrrdu_cropInfoL);
73
  PARSE();
74
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
75
76
  if (!_nbounds) {
77
    if (!( minLen == (int)nin->dim && maxLen == (int)nin->dim )) {
78
      fprintf(stderr,
79
              "%s: # min coords (%d) or max coords (%d) != nrrd dim (%d)\n",
80
              me, minLen, maxLen, nin->dim);
81
      airMopError(mop);
82
      return 1;
83
    }
84
    for (ai=0; ai<nin->dim; ai++) {
85
      if (-1 == minOff[0 + 2*ai]) {
86
        fprintf(stderr, "%s: can't use m+<int> specification for axis %d min\n",
87
                me, ai);
88
        airMopError(mop);
89
        return 1;
90
      }
91
    }
92
    for (ai=0; ai<nin->dim; ai++) {
93
      min[ai] = minOff[0 + 2*ai]*(nin->axis[ai].size-1) + minOff[1 + 2*ai];
94
      if (-1 == maxOff[0 + 2*ai]) {
95
        max[ai] = min[ai] + maxOff[1 + 2*ai];
96
      } else {
97
        max[ai] = maxOff[0 + 2*ai]*(nin->axis[ai].size-1) + maxOff[1 + 2*ai];
98
      }
99
      /*
100
        fprintf(stderr, "%s: ai %2d: min = %4d, max = %4d\n",
101
        me, ai, min[ai], max[ai]);
102
      */
103
    }
104
  } else {
105
    Nrrd *nbounds;
106
    airULLong *bounds;
107
    unsigned int axi;
108
    if (!(2 == _nbounds->dim
109
          && nin->dim == AIR_CAST(unsigned int, _nbounds->axis[0].size)
110
          && 2 == _nbounds->axis[1].size)) {
111
      char stmp1[AIR_STRLEN_SMALL], stmp2[AIR_STRLEN_SMALL];
112
      if (_nbounds->dim >= 2) {
113
        airSprintSize_t(stmp1, _nbounds->axis[1].size);
114
      } else {
115
        strcpy(stmp1, "");
116
      }
117
      fprintf(stderr, "%s: expected 2-D %u-by-2 array of cropping bounds, "
118
              "not %u-D %s%s%s%s\n", me, nin->dim, _nbounds->dim,
119
              airSprintSize_t(stmp2, _nbounds->axis[0].size),
120
              _nbounds->dim >= 2 ? "-by-" : "-long",
121
              _nbounds->dim >= 2 ? stmp1 : "",
122
              _nbounds->dim > 2 ? "-by-X" : "");
123
      airMopError(mop);
124
      return 1;
125
    }
126
    nbounds = nrrdNew();
127
    airMopAdd(mop, nbounds, (airMopper)nrrdNuke, airMopAlways);
128
    if (nrrdConvert(nbounds, _nbounds, nrrdTypeULLong)) {
129
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
130
      fprintf(stderr, "%s: error converting bounds array:\n%s", me, err);
131
      airMopError(mop);
132
      return 1;
133
    }
134
    bounds = AIR_CAST(airULLong*, nbounds->data);
135
    for (axi=0; axi<nin->dim; axi++) {
136
      min[axi] = AIR_CAST(size_t, bounds[axi + 0*(nin->dim)]);
137
      max[axi] = AIR_CAST(size_t, bounds[axi + 1*(nin->dim)]);
138
    }
139
  }
140
141
  nout = nrrdNew();
142
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
143
144
  if (nrrdCrop(nout, nin, min, max)) {
145
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
146
    fprintf(stderr, "%s: error cropping nrrd:\n%s", me, err);
147
    airMopError(mop);
148
    return 1;
149
  }
150
151
  SAVE(out, nout, NULL);
152
153
  airMopOkay(mop);
154
  return 0;
155
1
}
156
157
UNRRDU_CMD(crop, INFO);