GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/project.c Lines: 15 53 28.3 %
Date: 2017-05-26 Branches: 1 32 3.1 %

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 "Collapse scanlines to scalars along some axis"
28
static const char *_unrrdu_projectInfoL =
29
(INFO
30
 ". The scanline is reduced to a single scalar by "
31
 "\"measuring\" all the values in the scanline "
32
 "with some measure.  The output nrrd has dimension "
33
 "one less than input (except when the input is itself 1-D); "
34
 "the output type depends on "
35
 "the measure in a non-trivial way, or it can be set explicitly "
36
 "with the \"-t\" option.  To save the overhead of multiple input data "
37
 "reads if projections along different axes are needed, you can give "
38
 "multiple axes to \"-a\" (and a matching number of output filenames "
39
 "to \"-o\"), as well as multiple measures to \"-m\" (and possibly a "
40
 "specific type to \"-t\" to permit their joining on fastest axis).\n "
41
 "* Uses nrrdProject, and nrrdJoin if multiple measures");
42
43
int
44
unrrdu_projectMain(int argc, const char **argv, const char *me,
45
                   hestParm *hparm) {
46
2
  hestOpt *opt = NULL;
47
1
  char **out, *err;
48
1
  Nrrd *nin, *nout;
49
  Nrrd **nslice;
50
1
  unsigned int *axis, axisLen, outLen, measrLen, outIdx, measrIdx;
51
1
  int *measr, pret, type;
52
  airArray *mop;
53
54
1
  hestOptAdd(&opt, "a,axis", "axis", airTypeUInt, 1, -1, &axis, NULL,
55
             "axis or axes to project along", &axisLen);
56
1
  hestOptAdd(&opt, "m,measure", "measr", airTypeEnum, 1, -1, &measr, NULL,
57
             "How to \"measure\" a scanline, by summarizing all its values "
58
             "with a single scalar. Multiple measures will be joined along "
59
             "fastest axis if output, but you may need to set output type "
60
             "explicitly via \"-t\" so that the join works. "
61
1
             NRRD_MEASURE_DESC, &measrLen, nrrdMeasure);
62
1
  hestOptAdd(&opt, "t,type", "type", airTypeOther, 1, 1, &type, "default",
63
             "type to use for output. By default (not using this option), "
64
             "the output type is determined auto-magically",
65
             NULL, NULL, &unrrduHestMaybeTypeCB);
66
1
  OPT_ADD_NIN(nin, "input nrrd");
67
1
  hestOptAdd(&opt, "o,output", "nout", airTypeString, 1, -1, &out, "-",
68
             "one or more output nrrd filenames. Number of names here "
69
             "has to match number of axes specified.", &outLen);
70
1
  mop = airMopNew();
71
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
72
73
2
  USAGE(_unrrdu_projectInfoL);
74
  PARSE();
75
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
76
77
  if (axisLen != outLen) {
78
    fprintf(stderr, "%s: got %u \"-a\" axes but %u \"-o\" outputs\n", me,
79
            axisLen, outLen);
80
    airMopError(mop);
81
    return 1;
82
  }
83
84
  if (measrLen > 1) {
85
    nslice = AIR_CALLOC(measrLen, Nrrd *);
86
    airMopAdd(mop, nslice, airFree, airMopAlways);
87
    for (measrIdx=0; measrIdx<measrLen; measrIdx++) {
88
      nslice[measrIdx] = nrrdNew();
89
      airMopAdd(mop, nslice[measrIdx], (airMopper)nrrdNuke, airMopAlways);
90
    }
91
  }
92
  nout = nrrdNew();
93
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
94
95
  for (outIdx=0; outIdx<outLen; outIdx++) {
96
    if (measrLen > 1) {
97
      /* first project into slices */
98
      for (measrIdx=0; measrIdx<measrLen; measrIdx++) {
99
        if (nrrdProject(nslice[measrIdx], nin, axis[outIdx],
100
                        measr[measrIdx], type)) {
101
          airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
102
          fprintf(stderr, "%s: error projecting nrrd %u/%u:\n%s",
103
                  me, outIdx, measrIdx, err);
104
          airMopError(mop);
105
          return 1;
106
        }
107
      }
108
      /* then join slices into output */
109
      if (nrrdJoin(nout, (const Nrrd *const*)nslice, measrLen, 0, AIR_TRUE)) {
110
        airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
111
        fprintf(stderr, "%s: error joining nrrd %u; will have to use \"-t\" "
112
                "option to make sure all projections have same type:\n%s",
113
                me, outIdx, err);
114
        airMopError(mop);
115
        return 1;
116
      }
117
    } else {
118
      if (nrrdProject(nout, nin, axis[outIdx], measr[0], type)) {
119
        airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
120
        fprintf(stderr, "%s: error projecting nrrd %u:\n%s", me, outIdx, err);
121
        airMopError(mop);
122
        return 1;
123
      }
124
    }
125
    SAVE(out[outIdx], nout, NULL);
126
  }
127
128
  airMopOkay(mop);
129
  return 0;
130
1
}
131
132
UNRRDU_CMD(project, INFO);