|
From: <kin...@us...> - 2004-02-19 23:02:12
|
Update of /cvsroot/teem/teemdoc/html/nrrd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21253/nrrd Modified Files: lib.html Log Message: various updates Index: lib.html =================================================================== RCS file: /cvsroot/teem/teemdoc/html/nrrd/lib.html,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** lib.html 9 Jan 2004 19:46:46 -0000 1.5 --- lib.html 19 Feb 2004 22:51:12 -0000 1.6 *************** *** 127,131 **** ** Comments. Read from, and written to, header. ** The comment array "cmt" is NOT NULL-terminated. ! ** The number of comments is cmtArr->len. */ char **cmt; --- 127,131 ---- ** Comments. Read from, and written to, header. ** The comment array "cmt" is NOT NULL-terminated. ! ** The number of comments is cmtArr->len. */ char **cmt; *************** *** 310,313 **** --- 310,368 ---- </ul> + <h2>Reading a NRRD with a different memory allocator</h2> + + In some cases you want to read a NRRD in, but you want to do the + memory allocation, instead of the nrrd library doing it for you. This + is supported in nrrd, but you have to read the nrrd in two steps: the + first time reads the nrrd header so that you can figure out how much + memory to allocate, and the second time reads in the data (as well + as re-reading the header). This function demonstrates this: + + <blockquote><pre> + Nrrd * + customLoad(char *filename) { + char me[]="demoIO", *err; + NrrdIoState *nios; + Nrrd *nin; + void *data; + + /* create a new nrrd */ + nin = nrrdNew(); + + /* tell nrrdLoad to only read the header, not the data */ + nios = nrrdIoStateNew(); + nrrdIoStateSet(nios, nrrdIoStateSkipData, AIR_TRUE); + + /* read in the nrrd header from file */ + if (nrrdLoad(nin, filename, nios)) { + err = biffGetDone(NRRD); + fprintf(stderr, "%s: trouble reading \"%s\" header:\n%s", + me, filename, err); + free(err); nios = nrrdIoStateNix(nios); + return NULL; + } + + /* we're done with the nrrdIoState, this sets it to NULL */ + nios = nrrdIoStateNix(nios); + + /* load the nrrd again, this time the data will be read, but + the existing nin->data memory will be used, because nrrdRead + (called by nrrdLoad) remembers the address and size of incoming + allocated data, and eventually it will get used, instead of + nrrd allocating new memory */ + if (nrrdLoad(nin, filename, NULL)) { + err = biffGetDone(NRRD); + fprintf(stderr, "%s: trouble reading \"%s\" data:\n%s", + me, filename, err); + free(err); + return NULL; + } + + /* return the nrrd. Remember that you probably want to nrrdNix this + nrrd, and not nrrdNuke, since nrrdNuke will free(nin->data) */ + return nin; + } + </pre></blockquote> + <h2>Getting data into and out of nrrds</h2> *************** *** 378,382 **** nval = nrrdNew(); nrrdAlloc(nval, nrrdTypeDouble, 3, sx, sy, sz); ! val = (double*)nval->data; val[10 + sx*(20 + sy*30)] = 42; /* val[x=10,y=20,z=30] = 42 */ ... --- 433,437 ---- nval = nrrdNew(); nrrdAlloc(nval, nrrdTypeDouble, 3, sx, sy, sz); ! val = (double*)nval->data; val[10 + sx*(20 + sy*30)] = 42; /* val[x=10,y=20,z=30] = 42 */ ... *************** *** 650,654 **** <td align=right><tt>nrrdContentSet</tt> <td><b>-</b> ! <td align=left>printf-style setting of the nrrd->content field <tr> <td align=right><tt>nrrdCheck</tt> --- 705,709 ---- <td align=right><tt>nrrdContentSet</tt> <td><b>-</b> ! <td align=left>printf-style setting of the nrrd->content field <tr> <td align=right><tt>nrrdCheck</tt> *************** *** 740,747 **** <td><b>-</b> <td align=left>reverses the byte-ordering of the data in memory - <tr> - <td align=right><tt></tt> - <td><b>-</b> - <td align=left> </table> </center> --- 795,798 ---- *************** *** 761,766 **** size_t I, N; ! lup = nrrdDLookup[nrrd->type]; ! ins = nrrdDInsert[nrrd->type]; N = nrrdElementNumber(nrrd); for (I=0; I<N; I++) { --- 812,817 ---- size_t I, N; ! lup = nrrdDLookup[nrrd->type]; ! ins = nrrdDInsert[nrrd->type]; N = nrrdElementNumber(nrrd); for (I=0; I<N; I++) { |