GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/lut2.c Lines: 20 61 32.8 %
Date: 2017-05-26 Branches: 1 34 2.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 "Map nrrd through a bivariate lookup table"
28
static const char *_unrrdu_lut2InfoL =
29
(INFO
30
 " (itself represented as a nrrd). The lookup table "
31
 "can be 2D, in which case the output "
32
 "has the same dimension as the input, or 3D, in which case "
33
 "the output has one more dimension than the input, and each "
34
 "pair of values is mapped to a scanline (along axis 0) from the "
35
 "lookup table.  In any case, axis 0 of the input must have length two.\n "
36
 "* Uses nrrdApply2DLut");
37
38
int
39
unrrdu_lut2Main(int argc, const char **argv, const char *me,
40
                hestParm *hparm) {
41
2
  hestOpt *opt = NULL;
42
1
  char *out, *err;
43
1
  Nrrd *nin, *nlut, *nout, *ntmp[2];
44
  airArray *mop;
45
1
  int typeOut, rescale[2], pret, blind8BitRange;
46
1
  double min[2], max[2];
47
1
  NrrdRange *range[2]={NULL,NULL};
48
  unsigned int mapAxis, rai;
49
50
1
  hestOptAdd(&opt, "m,map", "lut", airTypeOther, 1, 1, &nlut, NULL,
51
             "lookup table to map input nrrd through",
52
1
             NULL, NULL, nrrdHestNrrd);
53
1
  hestOptAdd(&opt, "r,rescale", "bool bool", airTypeBool, 2, 2, rescale,
54
             "false false",
55
             "rescale one or both of the input values from the "
56
             "input range to the lut domain.  The lut domain is either "
57
             "explicitly defined by the axis min,max along axis 0 or 1, "
58
             "or, it is implicitly defined as zero to the length of that axis "
59
             "minus one.");
60
1
  hestOptAdd(&opt, "min,minimum", "min0 min1", airTypeDouble, 2, 2, min,
61
             "nan nan",
62
             "Low ends of input range. Defaults to lowest values "
63
             "found in input nrrd.  Explicitly setting this is useful "
64
             "only with rescaling (\"-r\")");
65
1
  hestOptAdd(&opt, "max,maximum", "max0 max1", airTypeDouble, 2, 2, max,
66
             "nan nan",
67
             "High end of input range. Defaults to highest values "
68
             "found in input nrrd.  Explicitly setting this is useful "
69
             "only with rescaling (\"-r\")");
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 only with rescaling (\"-r\")");
75
1
  hestOptAdd(&opt, "t,type", "type", airTypeOther, 1, 1, &typeOut, "default",
76
             "specify the type (\"int\", \"float\", etc.) of the "
77
             "output nrrd. "
78
             "By default (not using this option), the output type "
79
             "is the lut's type.",
80
             NULL, NULL, &unrrduHestMaybeTypeCB);
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_lut2InfoL);
88
  PARSE();
89
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
90
91
  nout = nrrdNew();
92
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
93
94
  if (!( nin->dim > 1 && 2 == nin->axis[0].size )) {
95
    char stmp[AIR_STRLEN_SMALL];
96
    fprintf(stderr, "%s: input nrrd dim must be > 1, and axis[0].size "
97
            "must be 2 (not %s)\n", me,
98
            airSprintSize_t(stmp, nin->axis[0].size));
99
    airMopError(mop);
100
    return 1;
101
  }
102
  mapAxis = nlut->dim - 2;
103
  if (!(0 == mapAxis || 1 == mapAxis)) {
104
    fprintf(stderr, "%s: dimension of lut should be 2 or 3, not %d",
105
            me, nlut->dim);
106
    airMopError(mop);
107
    return 1;
108
  }
109
110
  /* see comment in rmap.c */
111
  for (rai=0; rai<=1; rai++) {
112
    if (!( AIR_EXISTS(nlut->axis[mapAxis + rai].min) &&
113
           AIR_EXISTS(nlut->axis[mapAxis + rai].max) )) {
114
      rescale[rai] = AIR_TRUE;
115
    }
116
    if (rescale[rai]) {
117
      ntmp[rai] = nrrdNew();
118
      airMopAdd(mop, ntmp[rai], AIR_CAST(airMopper, nrrdNuke), airMopAlways);
119
      if (nrrdSlice(ntmp[rai], nin, 0, rai)) {
120
        airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
121
        fprintf(stderr, "%s: trouble slicing input value %u:\n%s",
122
                me, rai, err);
123
        airMopError(mop);
124
        return 1;
125
      }
126
      range[rai] = nrrdRangeNew(min[rai], max[rai]);
127
      airMopAdd(mop, range[rai], (airMopper)nrrdRangeNix, airMopAlways);
128
      nrrdRangeSafeSet(range[rai], ntmp[rai], blind8BitRange);
129
    }
130
  }
131
  if (nrrdTypeDefault == typeOut) {
132
    typeOut = nlut->type;
133
  }
134
  if (nrrdApply2DLut(nout, nin, 0, range[0], range[1], nlut, typeOut,
135
                     rescale[0], rescale[1])) {
136
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
137
    fprintf(stderr, "%s: trouble applying 2-D LUT:\n%s", me, err);
138
    airMopError(mop);
139
    return 1;
140
  }
141
142
  SAVE(out, nout, NULL);
143
144
  airMopOkay(mop);
145
  return 0;
146
1
}
147
148
UNRRDU_CMD(lut2, INFO);