GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/acrop.c Lines: 20 54 37.0 %
Date: 2017-05-26 Branches: 1 24 4.2 %

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 " Automatically crop axes based on given measure"
28
static const char *_unrrdu_acropInfoL =
29
  (INFO ". For the axes that are to be cropped, the slices perpendicular "
30
   "to that axis are projected down to a scalar with the specified measure. "
31
   "The resulting 1D array is analyzed by determining what portions at the "
32
   "beginning and end constitute less than some portion of the cumulative "
33
   "array sum; these ends are cropped off.  The cropping bounds determined "
34
   "here can be saved and applied to other arrays via the \"-b\" option.\n "
35
   "* Uses nrrdCropAuto");
36
37
int
38
unrrdu_acropMain(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
  int pret;
44
  airArray *mop;
45
46
1
  size_t min[NRRD_DIM_MAX], max[NRRD_DIM_MAX];
47
1
  unsigned int *axes, axesLen;
48
1
  double frac;
49
1
  int measr, offset;
50
  Nrrd *nbounds;
51
1
  char *boundsSave;
52
53
1
  hestOptAdd(&opt, "a,axes", "ax0", airTypeUInt, 0, -1, &axes, "",
54
             "the axes (if any) that should NOT be cropped", &axesLen);
55
1
  hestOptAdd(&opt, "m,measure", "measr", airTypeEnum, 1, 1, &measr, NULL,
56
             "How to measure slices (along axes to crop) as scalars, "
57
             "to form 1-D array analyzed to determine cropping extent. "
58
             "All the measures from \"unu project\" can be used, but "
59
             "those that make more sense here include:\n "
60
             "\b\bo \"max\", \"mean\", \"median\", "
61
             "\"variance\": (self-explanatory)\n "
62
             "\b\bo \"stdv\": standard deviation\n "
63
             "\b\bo \"cov\": coefficient of variation\n "
64
             "\b\bo \"product\", \"sum\": product or sum of all values\n "
65
             "\b\bo \"L1\", \"L2\", \"NL2\", \"RMS\", \"Linf\": "
66
1
             "different norms.", NULL, nrrdMeasure);
67
1
  hestOptAdd(&opt, "f,frac", "frac", airTypeDouble, 1, 1, &frac, "0.1",
68
             "threshold of cumulative sum of 1-D array at which to crop. "
69
             "Needs to be in interval [0.0,0.5).");
70
1
  hestOptAdd(&opt, "off,offset", "offset", airTypeInt, 1, 1, &offset, "1",
71
             "how much to offset the numerically determined cropping; "
72
             "positive offsets means expanding the interval of kept "
73
             "indices (less cropping)");
74
1
  hestOptAdd(&opt, "b,bounds", "filename", airTypeString, 1, 1,
75
             &boundsSave, "",
76
             "if a filename is given here, the automatically determined "
77
             "min and max bounds for cropping are saved to this file "
78
             "as a 2-D array; first scanline is for -min, second is for -max. "
79
             "Unfortunately nothing using the \"m\" and \"M\" semantics "
80
             "(above) can currently be saved in the bounds file.");
81
1
  OPT_ADD_NIN(nin, "input nrrd");
82
1
  OPT_ADD_NOUT(out, "output nrrd");
83
84
1
  mop = airMopNew();
85
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
86
87
2
  USAGE(_unrrdu_acropInfoL);
88
  PARSE();
89
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
90
91
  nout = nrrdNew();
92
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
93
94
  if (nrrdCropAuto(nout, nin, min, max,
95
                   axes, axesLen,
96
                   measr, frac, offset)) {
97
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
98
    fprintf(stderr, "%s: error cropping nrrd:\n%s", me, err);
99
    airMopError(mop);
100
    return 1;
101
  }
102
  if (airStrlen(boundsSave)) {
103
    unsigned int axi;
104
    airULLong *bounds;
105
    nbounds = nrrdNew();
106
    airMopAdd(mop, nbounds, (airMopper)nrrdNuke, airMopAlways);
107
    if (nrrdMaybeAlloc_va(nbounds, nrrdTypeULLong, 2,
108
                          AIR_CAST(airULLong, nin->dim),
109
                          AIR_CAST(airULLong, 2))) {
110
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
111
      fprintf(stderr, "%s: error allocating cropping bounds array:\n%s",
112
              me, err);
113
      airMopError(mop);
114
      return 1;
115
    }
116
    bounds = AIR_CAST(airULLong*, nbounds->data);
117
    for (axi=0; axi<nin->dim; axi++) {
118
      bounds[axi + 0*(nin->dim)] = AIR_CAST(airULLong, min[axi]);
119
      bounds[axi + 1*(nin->dim)] = AIR_CAST(airULLong, max[axi]);
120
    }
121
    if (nrrdSave(boundsSave, nbounds, NULL)) {
122
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
123
      fprintf(stderr, "%s: error saving cropping bounds array:\n%s",
124
              me, err);
125
      airMopError(mop);
126
      return 1;
127
    }
128
  }
129
130
  SAVE(out, nout, NULL);
131
132
  airMopOkay(mop);
133
  return 0;
134
1
}
135
136
UNRRDU_CMD(acrop, INFO);