GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/nrrd/defaultsNrrd.c Lines: 37 101 36.6 %
Date: 2017-05-26 Branches: 12 50 24.0 %

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 "nrrd.h"
25
#include "privateNrrd.h"
26
27
/*
28
** these aren't "const"s because the user should be able to change
29
** default behavior- until a more sophisticated mechanism for this
30
** kind of control is developed, it seems simple and usable enough to
31
** have this be global state which we agree to treat nicely, as in,
32
** threads shouldn't be changing these willy-nilly.
33
**
34
** What IS a "default"?  A default is the assertion of a certain
35
** choice in situations where the user hasn't set it explicitly, but
36
** COULD.  The pad value in resampling is a good example: it is set by
37
** a constructor to nrrdDefaultResamplePadValue, but the user can also set it
38
** explicitly.
39
*/
40
41
int nrrdDefaultWriteEncodingType = nrrdEncodingTypeRaw;
42
int nrrdDefaultWriteBareText = AIR_TRUE;
43
unsigned int nrrdDefaultWriteCharsPerLine = 75;
44
unsigned int nrrdDefaultWriteValsPerLine = 8;
45
/* ---- BEGIN non-NrrdIO */
46
int nrrdDefaultResampleBoundary = nrrdBoundaryBleed;
47
int nrrdDefaultResampleType = nrrdTypeDefault;
48
int nrrdDefaultResampleRenormalize = AIR_TRUE;
49
int nrrdDefaultResampleRound = AIR_TRUE;
50
int nrrdDefaultResampleClamp = AIR_TRUE;
51
int nrrdDefaultResampleCheap = AIR_FALSE;
52
double nrrdDefaultResamplePadValue = 0.0;
53
int nrrdDefaultResampleNonExistent = nrrdResampleNonExistentNoop;
54
double nrrdDefaultKernelParm0 = 1.0;
55
/* ---- END non-NrrdIO */
56
int nrrdDefaultCenter = nrrdCenterCell;
57
double nrrdDefaultSpacing = 1.0;
58
59
/* these aren't really "defaults" because there's no other channel for
60
   specifying this information.  It is just global state.  Obviously,
61
   like defaults, they are not thread-safe if different threads ever
62
   set them differently. */
63
int nrrdStateVerboseIO = 1; /* NrrdIO-hack-003 */
64
int nrrdStateKeyValuePairsPropagate = AIR_FALSE;
65
/* ---- BEGIN non-NrrdIO */
66
int nrrdStateBlind8BitRange = AIR_TRUE;
67
int nrrdStateMeasureType = nrrdTypeFloat;
68
int nrrdStateMeasureModeBins = 1024;
69
int nrrdStateMeasureHistoType = nrrdTypeFloat;
70
int nrrdStateDisallowIntegerNonExist = AIR_TRUE;
71
/* ---- END non-NrrdIO */
72
int nrrdStateAlwaysSetContent = AIR_TRUE;
73
int nrrdStateDisableContent = AIR_FALSE;
74
const char *nrrdStateUnknownContent = NRRD_UNKNOWN;
75
int nrrdStateGrayscaleImage3D = AIR_FALSE;
76
/* there is no sane reason to change this initialization */
77
int nrrdStateKeyValueReturnInternalPointers = AIR_FALSE;
78
/* Making the default for this be AIR_TRUE means that nrrd is not only
79
   completely conservative about updating kind, but purposely stupid.
80
   Nrrd is only going to implement the most converative kind of logic
81
   anyway, based on existing sementics nailed down by the format spec. */
82
int nrrdStateKindNoop = AIR_FALSE;
83
84
/* these are helper functions for min/max testing */
85
airLLong
86
_nrrdLLongMaxHelp(airLLong val) {
87
80
  return val*2 + 1;
88
}
89
airLLong
90
_nrrdLLongMinHelp(airLLong val) {
91
80
  return val*2;
92
}
93
airULLong
94
_nrrdULLongMaxHelp(airULLong val) {
95
40
  return val + 1;
96
}
97
98
/* should the acceptance (or not) of malformed NRRD header fields
99
   embedded in PNM or text comments be controlled here? */
100
101
/* Are there other assumptions currently built into nrrd which could
102
   stand to be user-controllable? */
103
104
/* ---- BEGIN non-NrrdIO */
105
106
const char *const nrrdEnvVarDefaultWriteEncodingType
107
  = "NRRD_DEFAULT_WRITE_ENCODING_TYPE";
108
const char *const nrrdEnvVarDefaultWriteBareText
109
  = "NRRD_DEFAULT_WRITE_BARE_TEXT";
110
const char *const nrrdEnvVarDefaultWriteBareTextOld
111
  = "NRRD_DEF_WRITE_BARE_TEXT";
112
const char *const nrrdEnvVarDefaultCenter
113
  = "NRRD_DEFAULT_CENTER";
114
const char *const nrrdEnvVarDefaultCenterOld
115
  = "NRRD_DEF_CENTER";
116
const char *const nrrdEnvVarDefaultWriteCharsPerLine
117
  = "NRRD_DEFAULT_WRITE_CHARS_PER_LINE";
118
const char *const nrrdEnvVarDefaultWriteValsPerLine
119
  = "NRRD_DEFAULT_WRITE_VALS_PER_LINE";
120
const char *const nrrdEnvVarDefaultKernelParm0
121
  = "NRRD_DEFAULT_KERNEL_PARM0";
122
const char *const nrrdEnvVarDefaultSpacing
123
  = "NRRD_DEFAULT_SPACING";
124
125
const char *const nrrdEnvVarStateKindNoop
126
  = "NRRD_STATE_KIND_NOOP";
127
const char *const nrrdEnvVarStateVerboseIO
128
  = "NRRD_STATE_VERBOSE_IO";
129
const char *const nrrdEnvVarStateKeyValuePairsPropagate
130
  = "NRRD_STATE_KEYVALUEPAIRS_PROPAGATE";
131
const char *const nrrdEnvVarStateBlind8BitRange
132
  = "NRRD_STATE_BLIND_8_BIT_RANGE";
133
const char *const nrrdEnvVarStateAlwaysSetContent
134
  = "NRRD_STATE_ALWAYS_SET_CONTENT";
135
const char *const nrrdEnvVarStateDisableContent
136
  = "NRRD_STATE_DISABLE_CONTENT";
137
const char *const nrrdEnvVarStateMeasureType
138
  = "NRRD_STATE_MEASURE_TYPE";
139
const char *const nrrdEnvVarStateMeasureModeBins
140
  = "NRRD_STATE_MEASURE_MODE_BINS";
141
const char *const nrrdEnvVarStateMeasureHistoType
142
  = "NRRD_STATE_MEASURE_HISTO_TYPE";
143
const char *const nrrdEnvVarStateGrayscaleImage3D
144
  = "NRRD_STATE_GRAYSCALE_IMAGE_3D";
145
146
/*
147
**    return
148
**     value:
149
**        -1: unset, or bad args    ==> *val NOT set
150
**  AIR_TRUE: set in a valid way    ==> *val set (to something)
151
** AIR_FALSE: set in an invalid way ==> *val NOT set
152
*/
153
154
int
155
nrrdGetenvBool(int *val, char **envStr, const char *envVar) {
156
  char *env;
157
  int tmp;
158
159
14
  if (!(val && envVar)) {
160
    return -1;
161
  }
162
7
  env = getenv(envVar);
163
7
  if (envStr) {
164
7
    *envStr = env;
165
7
  }
166
7
  if (!env) {
167
7
    return -1;
168
  }
169
  if (!strlen(env)) {
170
    /* for bools, being merely set (but not to any string) means "true" */
171
    *val = AIR_TRUE;
172
    return AIR_TRUE;
173
  }
174
  tmp = airEnumVal(airBool, env);
175
  if (airEnumUnknown(airBool) == tmp) {
176
    return AIR_FALSE;
177
  } else {
178
    *val = tmp;
179
    return AIR_TRUE;
180
  }
181
7
}
182
183
int
184
nrrdGetenvEnum(int *val, char **envStr, const airEnum *enm,
185
               const char *envVar) {
186
  char *env;
187
  int tmp;
188
189
8
  if (!(val && envVar)) {
190
    return -1;
191
  }
192
4
  env = getenv(envVar);
193
4
  if (envStr) {
194
4
    *envStr = env;
195
4
  }
196
4
  if (!env) {
197
4
    return -1;
198
  }
199
  tmp = airEnumVal(enm, env);
200
  if (airEnumUnknown(enm) == tmp) {
201
    return AIR_FALSE;
202
  } else {
203
    *val = tmp;
204
    return AIR_TRUE;
205
  }
206
4
}
207
208
int
209
nrrdGetenvUInt(unsigned int *val, char **envStr, const char *envVar) {
210
  char *env;
211
4
  unsigned int tmp;
212
213
2
  if (!(val && envVar)) {
214
    return -1;
215
  }
216
2
  env = getenv(envVar);
217
2
  if (envStr) {
218
2
    *envStr = env;
219
2
  }
220
2
  if (!env) {
221
2
    return -1;
222
  }
223
  if (1 != sscanf(env, "%u", &tmp)) {
224
    return AIR_FALSE;
225
  } else {
226
    *val = tmp;
227
    return AIR_TRUE;
228
  }
229
2
}
230
231
int
232
nrrdGetenvInt(int *val, char **envStr, const char *envVar) {
233
  char *env;
234
4
  int tmp;
235
236
2
  if (!(val && envVar)) {
237
    return -1;
238
  }
239
2
  env = getenv(envVar);
240
2
  if (envStr) {
241
2
    *envStr = env;
242
2
  }
243
2
  if (!env) {
244
2
    return -1;
245
  }
246
  if (1 != sscanf(env, "%d", &tmp)) {
247
    return AIR_FALSE;
248
  } else {
249
    *val = tmp;
250
    return AIR_TRUE;
251
  }
252
2
}
253
254
int
255
nrrdGetenvDouble(double *val, char **envStr, const char *envVar) {
256
  char *env;
257
  double tmp;
258
259
  if (!(val && envVar)) {
260
    return -1;
261
  }
262
  env = getenv(envVar);
263
  if (envStr) {
264
    *envStr = env;
265
  }
266
  if (!env) {
267
    return -1;
268
  }
269
  if (1 != airSingleSscanf(env, "%lf", &tmp)) {
270
    return AIR_FALSE;
271
  } else {
272
    *val = tmp;
273
    return AIR_TRUE;
274
  }
275
}
276
277
/*
278
** This function is not used in the same way within nrrd the same way
279
** as the other nrrdGetenv functions; it was added just to have a more
280
** convenient wrapper around getenv for strings.
281
*/
282
int
283
nrrdGetenvString(char **envStr, const char *envVar) {
284
285
  if (!(envStr && envVar)) {
286
    return -1;
287
  }
288
  *envStr = getenv(envVar);
289
  if (!(*envStr)) {
290
    return AIR_FALSE;
291
  } else {
292
    return AIR_TRUE;
293
  }
294
}
295
296
void
297
nrrdDefaultGetenv(void) {
298
299
  /* these two pre-date Def --> Default rename */
300
  if (-1 == nrrdGetenvBool(/**/ &nrrdDefaultWriteBareText, NULL,
301
                           nrrdEnvVarDefaultWriteBareTextOld)) {
302
    nrrdGetenvBool(/**/ &nrrdDefaultWriteBareText, NULL,
303
                   nrrdEnvVarDefaultWriteBareText);
304
  }
305
  if (-1 == nrrdGetenvEnum(/**/ &nrrdDefaultCenter, NULL, nrrdCenter,
306
                           nrrdEnvVarDefaultCenterOld)) {
307
    nrrdGetenvEnum(/**/ &nrrdDefaultCenter, NULL, nrrdCenter,
308
                   nrrdEnvVarDefaultCenter);
309
  }
310
  /* these post-date the Def --> Default rename */
311
312
  nrrdGetenvEnum(/**/ &nrrdDefaultWriteEncodingType, NULL, nrrdEncodingType,
313
                 nrrdEnvVarDefaultWriteEncodingType);
314
  nrrdGetenvUInt(/**/ &nrrdDefaultWriteCharsPerLine, NULL,
315
                 nrrdEnvVarDefaultWriteCharsPerLine);
316
  nrrdGetenvUInt(/**/ &nrrdDefaultWriteValsPerLine, NULL,
317
                 nrrdEnvVarDefaultWriteValsPerLine);
318
  nrrdGetenvDouble(/**/ &nrrdDefaultKernelParm0, NULL,
319
                   nrrdEnvVarDefaultKernelParm0);
320
  nrrdGetenvDouble(/**/ &nrrdDefaultSpacing, NULL,
321
                   nrrdEnvVarDefaultSpacing);
322
323
  return;
324
}
325
326
void
327
nrrdStateGetenv(void) {
328
329
  nrrdGetenvBool(/**/ &nrrdStateKindNoop, NULL,
330
                 nrrdEnvVarStateKindNoop);
331
  nrrdGetenvInt(/**/ &nrrdStateVerboseIO, NULL,
332
                nrrdEnvVarStateVerboseIO);
333
  nrrdGetenvBool(/**/ &nrrdStateKeyValuePairsPropagate, NULL,
334
                 nrrdEnvVarStateKeyValuePairsPropagate);
335
  nrrdGetenvBool(/**/ &nrrdStateBlind8BitRange, NULL,
336
                 nrrdEnvVarStateBlind8BitRange);
337
  nrrdGetenvBool(/**/ &nrrdStateAlwaysSetContent, NULL,
338
                 nrrdEnvVarStateAlwaysSetContent);
339
  nrrdGetenvBool(/**/ &nrrdStateDisableContent, NULL,
340
                 nrrdEnvVarStateDisableContent);
341
  nrrdGetenvEnum(/**/ &nrrdStateMeasureType, NULL, nrrdType,
342
                 nrrdEnvVarStateMeasureType);
343
  nrrdGetenvInt(/**/ &nrrdStateMeasureModeBins, NULL,
344
                nrrdEnvVarStateMeasureModeBins);
345
  nrrdGetenvEnum(/**/ &nrrdStateMeasureHistoType, NULL, nrrdType,
346
                 nrrdEnvVarStateMeasureHistoType);
347
  nrrdGetenvBool(/**/ &nrrdStateGrayscaleImage3D, NULL,
348
                 nrrdEnvVarStateGrayscaleImage3D);
349
350
  return;
351
}
352
353
/* ---- END non-NrrdIO */