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
25 #include "mex.h"
26 #include <teem/nrrd.h>
27
mexFunction(int nlhs,mxArray * plhs[],int nrhs,const mxArray * prhs[])28 void mexFunction(int nlhs, mxArray *plhs[],
29 int nrhs, const mxArray *prhs[])
30 {
31 char me[]="nrrdLoadOrientation", *filename, *errPtr, errBuff[AIR_STRLEN_MED];
32 int filenameLen, sizeI[NRRD_DIM_MAX];
33 mxClassID mtype;
34 size_t sizeZ[NRRD_DIM_MAX];
35 unsigned int axIdx;
36 Nrrd *nrrd;
37 NrrdIoState *nio;
38 airArray *mop;
39 unsigned int domainAxisNum, domainAxisIdx[NRRD_DIM_MAX], axisIdx,
40 rowIdx, colIdx;
41 double spacing, spaceDir[NRRD_SPACE_DIM_MAX];
42 int spacingStatus;
43
44 if (!(1 == nrhs && mxIsChar(prhs[0]))) {
45 sprintf(errBuff, "%s: requires one string argument (the name of the file)",
46 me);
47 mexErrMsgTxt(errBuff);
48 }
49
50 mop = airMopNew();
51 filenameLen = mxGetM(prhs[0])*mxGetN(prhs[0])+1;
52 filename = mxCalloc(filenameLen, sizeof(mxChar)); /* managed by Matlab */
53 mxGetString(prhs[0], filename, filenameLen);
54
55 nrrd = nrrdNew();
56 airMopAdd(mop, nrrd, (airMopper)nrrdNix, airMopAlways);
57 nio = nrrdIoStateNew();
58 airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
59 nrrdIoStateSet(nio, nrrdIoStateSkipData, AIR_TRUE);
60
61 /* read header, but no data */
62 if (nrrdLoad(nrrd, filename, nio)) {
63 errPtr = biffGetDone(NRRD);
64 airMopAdd(mop, errPtr, airFree, airMopAlways);
65 sprintf(errBuff, "%s: trouble reading NRRD header:\n%s", me, errPtr);
66 airMopError(mop);
67 mexErrMsgTxt(errBuff);
68 }
69
70 domainAxisNum = nrrdDomainAxesGet(nrrd, domainAxisIdx);
71 plhs[0] = mxCreateDoubleMatrix(domainAxisNum /* # rows */,
72 nrrd->dim /* # cols */, mxREAL);
73 for (colIdx=0; colIdx<nrrd->dim; colIdx++) {
74 spacingStatus = nrrdSpacingCalculate(nrrd, domainAxisIdx[colIdx],
75 &spacing, spaceDir);
76 switch(spacingStatus) {
77 case nrrdSpacingStatusNone:
78 for (rowIdx=0; rowIdx<domainAxisNum; rowIdx++) {
79 mxGetPr(plhs[0])[rowIdx + domainAxisNum*colIdx] = AIR_NAN;
80 }
81 break;
82 case nrrdSpacingStatusScalarNoSpace:
83 for (rowIdx=0; rowIdx<domainAxisNum; rowIdx++) {
84 mxGetPr(plhs[0])[rowIdx + domainAxisNum*colIdx] = 0;
85 }
86 if (colIdx < domainAxisNum) {
87 mxGetPr(plhs[0])[colIdx + domainAxisNum*colIdx] = spacing;
88 }
89 break;
90 case nrrdSpacingStatusDirection:
91 for (rowIdx=0; rowIdx<domainAxisNum; rowIdx++) {
92 mxGetPr(plhs[0])[rowIdx + domainAxisNum*colIdx] =
93 nrrd->axis[colIdx].spaceDirection[rowIdx];
94 }
95 break;
96 case nrrdSpacingStatusUnknown:
97 sprintf(errBuff, "%s: error interpreting axis %u spacing "
98 "(nrrdSpacingStatusUnknown)", me, colIdx);
99 airMopError(mop);
100 mexErrMsgTxt(errBuff);
101 break;
102 case nrrdSpacingStatusScalarWithSpace:
103 sprintf(errBuff, "%s: error interpreting axis %u spacing "
104 "(nrrdSpacingScalarWithSpace)", me, colIdx);
105 airMopError(mop);
106 mexErrMsgTxt(errBuff);
107 break;
108 }
109 }
110
111
112 airMopOkay(mop);
113 return;
114 }
115