1 /*
2   Teem: Tools to process and visualize scientific data and images             .
3   Copyright (C) 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 "teem/nrrd.h"
25 #include <testDataPath.h>
26 
27 /*
28 ** Tests:
29 ** airSrandMT
30 ** airNormalRand
31 ** nrrdNew
32 ** nrrdAlloc_va
33 ** nrrdHisto
34 ** nrrdHistoDraw
35 ** nrrdSave (to .pgm file)
36 ** nrrdNuke
37 */
38 
39 #define BINS 1000
40 #define HGHT 1000
41 
42 /* have to use PGM format for image because Teem might
43    not have been built with PNG */
44 #define THISNAME "histo.pgm"
45 #define CORRNAME "test/trandhisto.pgm"
46 
47 int
main(int argc,const char * argv[])48 main(int argc, const char *argv[]) {
49   const char *me;
50   size_t vi, ii, qvalLen;
51   Nrrd *nval, *nhist, *nimg, *nread, *ncorr;
52   double aa, bb, *val;
53   airArray *mop;
54   char *corrname, explain[AIR_STRLEN_LARGE];
55   int differ;
56 
57   AIR_UNUSED(argc);
58   me = argv[0];
59   mop = airMopNew();
60 
61   qvalLen = 10*BINS;
62   nrrdAlloc_va(nval=nrrdNew(), nrrdTypeDouble, 1, 4*qvalLen);
63   airMopAdd(mop, nval, (airMopper)nrrdNuke, airMopAlways);
64   val = AIR_CAST(double*, nval->data);
65 
66   airSrandMT(999);
67   vi = 0;
68   for (ii=0; ii<qvalLen; ii++) {
69     airNormalRand(&aa, NULL);
70     val[vi++] = aa;
71   }
72   for (ii=0; ii<qvalLen; ii++) {
73     airNormalRand(NULL, &bb);
74     val[vi++] = bb;
75   }
76   for (ii=0; ii<qvalLen; ii++) {
77     airNormalRand(&aa, &bb);
78     val[vi++] = aa;
79     val[vi++] = bb;
80   }
81 
82   nhist=nrrdNew();
83   airMopAdd(mop, nhist, (airMopper)nrrdNuke, airMopAlways);
84   nimg=nrrdNew();
85   airMopAdd(mop, nimg, (airMopper)nrrdNuke, airMopAlways);
86   if (nrrdHisto(nhist, nval, NULL, NULL, BINS, nrrdTypeInt)
87       || nrrdHistoDraw(nimg, nhist, HGHT, AIR_TRUE, 0.0)
88       || nrrdSave(THISNAME, nimg, NULL)) {
89     char *err;
90     airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
91     fprintf(stderr, "%s: trouble producing histo:\n%s", me, err);
92     airMopError(mop); return 1;
93   }
94 
95   nread = nrrdNew();
96   airMopAdd(mop, nread, (airMopper)nrrdNuke, airMopAlways);
97   ncorr = nrrdNew();
98   airMopAdd(mop, ncorr, (airMopper)nrrdNuke, airMopAlways);
99 
100   corrname = testDataPathPrefix(CORRNAME);
101   airMopAdd(mop, corrname, airFree, airMopAlways);
102   if (nrrdLoad(ncorr, corrname, NULL)
103       || nrrdLoad(nread, THISNAME, NULL)) {
104     char *err;
105     airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
106     fprintf(stderr, "%s: trouble reading:\n%s", me, err);
107     airMopError(mop); return 1;
108   }
109 
110   if (nrrdCompare(ncorr, nread, AIR_FALSE /* onlyData */,
111                   0.0 /* epsilon */, &differ, explain)) {
112     char *err;
113     airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
114     fprintf(stderr, "%s: trouble comparing:\n%s", me, err);
115     airMopError(mop); return 1;
116   }
117   if (differ) {
118     fprintf(stderr, "%s: new and correct (%s) images differ: %s\n",
119             me, corrname, explain);
120     airMopError(mop); return 1;
121   } else {
122     printf("%s: all good\n", me);
123   }
124 
125   airMopOkay(mop);
126   return 0;
127 }
128