GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/mrmap.c Lines: 20 56 35.7 %
Date: 2017-05-26 Branches: 1 28 3.6 %

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 "Map nrrd through a whole nrrd of regular univariate maps"
28
static const char *_unrrdu_mrmapInfoL =
29
(INFO
30
 ", one map per sample in input. The \"mmap\" nrrd has the same dimensional "
31
 "constraints as the \"mlut\" nrrd for \"unu mlut\".  This functionality "
32
 "is a generalization of \"unu 3op lerp\": it allows you to lerp through "
33
 "multiple nrrds, instead of just two.\n "
34
 "* Uses nrrdApplyMulti1DRegMap");
35
36
int
37
unrrdu_mrmapMain(int argc, const char **argv, const char *me,
38
                 hestParm *hparm) {
39
2
  hestOpt *opt = NULL;
40
1
  char *out, *err;
41
1
  Nrrd *nin, **_nmmap, *nmmap, *nout;
42
  airArray *mop;
43
  NrrdRange *range=NULL;
44
  unsigned int mapAxis;
45
1
  int typeOut, rescale, pret, blind8BitRange;
46
1
  unsigned int _nmmapLen;
47
1
  double min, max;
48
49
1
  hestOptAdd(&opt, "m,map", "mmap", airTypeOther, 1, -1, &_nmmap, NULL,
50
             "one nrrd of regular maps to map input nrrd through, or, "
51
             "list of nrrds which contain the individual entries of the map "
52
             "at each voxel, which will be joined together.",
53
1
             &_nmmapLen, NULL, nrrdHestNrrd);
54
1
  hestOptAdd(&opt, "r,rescale", NULL, airTypeInt, 0, 0, &rescale, NULL,
55
             "rescale the input values from the input range to the "
56
             "map domain.  The map domain is either explicitly "
57
             "defined by the axis min,max along axis 0 or 1, or, it "
58
             "is implicitly defined as zero to one minus the length of "
59
             "that axis.");
60
1
  hestOptAdd(&opt, "min,minimum", "value", airTypeDouble, 1, 1, &min, "nan",
61
             "Low end of input range. Defaults to lowest value "
62
             "found in input nrrd.  Explicitly setting this is useful "
63
             "only with rescaling (\"-r\") or if the map domain is only "
64
             "implicitly defined");
65
1
  hestOptAdd(&opt, "max,maximum", "value", airTypeDouble, 1, 1, &max, "nan",
66
             "High end of input range. Defaults to highest value "
67
             "found in input nrrd.  Explicitly setting this is useful "
68
             "only with rescaling (\"-r\") or if the map domain is only "
69
             "implicitly defined");
70
1
  hestOptAdd(&opt, "blind8", "bool", airTypeBool, 1, 1, &blind8BitRange,
71
1
             nrrdStateBlind8BitRange ? "true" : "false",
72
             "Whether to know the range of 8-bit data blindly "
73
             "(uchar is always [0,255], signed char is [-128,127]). "
74
             "Explicitly setting this is useful "
75
             "only with rescaling (\"-r\") or if the map domain is only "
76
             "implicitly defined");
77
1
  hestOptAdd(&opt, "t,type", "type", airTypeOther, 1, 1, &typeOut, "default",
78
             "specify the type (\"int\", \"float\", etc.) of the "
79
             "output nrrd. "
80
             "By default (not using this option), the output type "
81
             "is the map's type.",
82
             NULL, NULL, &unrrduHestMaybeTypeCB);
83
1
  OPT_ADD_NIN(nin, "input nrrd");
84
1
  OPT_ADD_NOUT(out, "output nrrd");
85
86
1
  mop = airMopNew();
87
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
88
89
2
  USAGE(_unrrdu_mrmapInfoL);
90
  PARSE();
91
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
92
93
  nout = nrrdNew();
94
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
95
96
  /* see comment rmap.c */
97
98
  /* by the end of this block we need to have nmmap and mapAxis */
99
  if (1 == _nmmapLen) {
100
    /* we got the mmap as a single nrrd */
101
    nmmap = _nmmap[0];
102
    mapAxis = nmmap->dim - nin->dim - 1;
103
    /* its not our job to do real error checking ... */
104
    mapAxis = AIR_MIN(mapAxis, nmmap->dim - 1);
105
  } else {
106
    /* we have to join together multiple nrrds to get the mmap */
107
    nmmap = nrrdNew();
108
    airMopAdd(mop, nmmap, (airMopper)nrrdNuke, airMopAlways);
109
    /* assume that mmap component nrrds are all compatible sizes,
110
       nrrdJoin will fail if they aren't */
111
    mapAxis = _nmmap[0]->dim - nin->dim;
112
    if (nrrdJoin(nmmap, (const Nrrd*const*)_nmmap, _nmmapLen, mapAxis, AIR_TRUE)) {
113
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
114
      fprintf(stderr, "%s: trouble joining mmap:\n%s", me, err);
115
      airMopError(mop);
116
      return 1;
117
    }
118
    /* set these if they were given, they'll be NaN otherwise */
119
    nmmap->axis[mapAxis].min = min;
120
    nmmap->axis[mapAxis].max = max;
121
  }
122
123
  if (!( AIR_EXISTS(nmmap->axis[mapAxis].min) &&
124
         AIR_EXISTS(nmmap->axis[mapAxis].max) )) {
125
    rescale = AIR_TRUE;
126
  }
127
  if (rescale) {
128
    range = nrrdRangeNew(min, max);
129
    airMopAdd(mop, range, (airMopper)nrrdRangeNix, airMopAlways);
130
    nrrdRangeSafeSet(range, nin, blind8BitRange);
131
  }
132
133
  if (nrrdTypeDefault == typeOut) {
134
    typeOut = nmmap->type;
135
  }
136
  if (nrrdApplyMulti1DRegMap(nout, nin, range, nmmap, typeOut, rescale)) {
137
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
138
    fprintf(stderr, "%s: trouble applying map:\n%s", me, err);
139
    airMopError(mop);
140
    return 1;
141
  }
142
143
  SAVE(out, nout, NULL);
144
145
  airMopOkay(mop);
146
  return 0;
147
1
}
148
149
UNRRDU_CMD(mrmap, INFO);