GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/nrrd/ccmethods.c Lines: 0 54 0.0 %
Date: 2017-05-26 Branches: 0 32 0.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
int
28
nrrdCCValid(const Nrrd *nin) {
29
  static const char me[]="nrrdCCValid";
30
31
  if (nrrdCheck(nin)) {
32
    biffAddf(NRRD, "%s: basic validity check failed", me);
33
    return 0;
34
  }
35
  if (!( nrrdTypeIsIntegral[nin->type] )) {
36
    biffAddf(NRRD, "%s: need an integral type (not %s)", me,
37
             airEnumStr(nrrdType, nin->type));
38
    return 0;
39
  }
40
  if (!( nrrdTypeSize[nin->type] <= 2 ||
41
         nrrdTypeInt == nin->type ||
42
         nrrdTypeUInt == nin->type )) {
43
    biffAddf(NRRD, "%s: valid connected component types are 1- and 2-byte "
44
             "integers, and %s and %s", me,
45
             airEnumStr(nrrdType, nrrdTypeInt),
46
             airEnumStr(nrrdType, nrrdTypeUInt));
47
    return 0;
48
  }
49
  return 1;
50
}
51
52
/*
53
** things we could sensibly measure on CCs:
54
** - size
55
** - # neighbors (needs conny argument)
56
** - what else?
57
*/
58
59
unsigned int
60
nrrdCCSize(Nrrd *nout, const Nrrd *nin) {
61
  static const char me[]="nrrdCCSize", func[]="ccsize";
62
  unsigned int *out, maxid, (*lup)(const void *, size_t);
63
  size_t I, NN;
64
65
  if (!( nout && nrrdCCValid(nin) )) {
66
    biffAddf(NRRD, "%s: invalid args", me);
67
    return 1;
68
  }
69
  maxid = nrrdCCMax(nin);
70
  if (nrrdMaybeAlloc_va(nout, nrrdTypeUInt, 1,
71
                        AIR_CAST(size_t, maxid+1))) {
72
    biffAddf(NRRD, "%s: can't allocate output", me);
73
    return 1;
74
  }
75
  out = (unsigned int *)(nout->data);
76
  lup = nrrdUILookup[nin->type];
77
  NN = nrrdElementNumber(nin);
78
  for (I=0; I<NN; I++) {
79
    out[lup(nin->data, I)] += 1;
80
  }
81
  if (nrrdContentSet_va(nout, func, nin, "")) {
82
    biffAddf(NRRD, "%s:", me);
83
    return 1;
84
  }
85
86
  return 0;
87
}
88
89
/*
90
******** nrrdCCMax
91
**
92
** returns the highest CC ID, or 0 if there were problems
93
**
94
** does NOT use biff
95
*/
96
unsigned int
97
nrrdCCMax(const Nrrd *nin) {
98
  unsigned int (*lup)(const void *, size_t), id, max;
99
  size_t I, NN;
100
101
  if (!nrrdCCValid(nin)) {
102
    return 0;
103
  }
104
  lup = nrrdUILookup[nin->type];
105
  NN = nrrdElementNumber(nin);
106
  max = 0;
107
  for (I=0; I<NN; I++) {
108
    id = lup(nin->data, I);
109
    max = AIR_MAX(max, id);
110
  }
111
  return max;
112
}
113
114
/*
115
******** nrrdCCNum
116
**
117
** returns the number of connected components (the # of CC IDs assigned)
118
** a return of 0 indicates an error
119
*/
120
unsigned int
121
nrrdCCNum(const Nrrd *nin) {
122
  unsigned int (*lup)(const void *, size_t), num;
123
  size_t I, max, NN;
124
  unsigned char *hist;
125
126
  if (!nrrdCCValid(nin)) {
127
    return 0;
128
  }
129
  lup = nrrdUILookup[nin->type];
130
  NN = nrrdElementNumber(nin);
131
  max = nrrdCCMax(nin);
132
  hist = (unsigned char *)calloc(max+1, sizeof(unsigned char));
133
  if (!hist) {
134
    return 0;
135
  }
136
  for (I=0; I<NN; I++) {
137
    hist[lup(nin->data, I)] = 1;
138
  }
139
  num = 0;
140
  for (I=0; I<=max; I++) {
141
    num += hist[I];
142
  }
143
  free(hist);
144
  return num;
145
}