10#if !defined(__newimageio_h)
18#include "NewNifti/NewNifti.h"
19#include "armawrap/newmatio.h"
20#include "miscmaths/miscmaths.h"
22#include "complexvolume.h"
26bool FslIsSingleFileType(
int filetype);
27int FslNiftiVersionFileType(
int filetype);
28int FslFiletypeFromHeader(
const NiftiIO::NiftiHeader& header);
29int fslFileType(std::string filename);
30int FslGetEnvOutputType(
void);
31std::string outputExtension(
const int filetype);
33bool FslFileExists(
const std::string& filename);
34bool FslImageExists(
const std::string& filename);
35inline bool fsl_imageexists(
const std::string& filename) {
return FslImageExists(filename); }
36bool FslIsCompressedFileType(
int filetype);
37std::string return_validimagefilename(
const std::string& filename,
const bool quiet=
false,
const bool strict=
false);
38std::string make_basename(std::string& filename);
39std::string make_basename(
const std::string& filename);
40std::string appendFSLfilename(
const std::string inputName,
const std::string addendum);
41int find_pathname(std::string& filename);
42int fslFileType(std::string filename);
44void ConvertAndScaleNewNiftiBuffer(
char* buffer, T*& tbuffer,
const NiftiIO::NiftiHeader& niihdr,
const size_t & imagesize);
47int read_volume(volume<T>& target,
const std::string& filename,
const bool& legacyRead=
true);
49int read_volumeROI(volume<T>& target,
const std::string& filename,
50 int64_t x0, int64_t y0, int64_t z0, int64_t x1, int64_t y1, int64_t z1);
52int read_volumeROI(volume<T>& target,
const std::string& filename,
53 int64_t x0, int64_t y0, int64_t z0, int64_t x1, int64_t y1, int64_t z1,
54 int64_t xskip, int64_t yskip, int64_t zskip);
56int read_volumeROI(volume<T>& target,
const std::string& filename,
57 int64_t x0, int64_t y0, int64_t z0, int64_t t0,
58 int64_t x1, int64_t y1, int64_t z1, int64_t t1);
61int read_volumeROI(volume<T>& target,
const std::string& filename,
short& dtype,
62 int64_t x0, int64_t y0, int64_t z0, int64_t t0,
63 int64_t x1, int64_t y1, int64_t z1, int64_t t1,
64 const bool swap2radiological,
const bool readAs4D=
false) {
65 return readGeneralVolume(target,filename,dtype,swap2radiological,x0,y0,z0,t0,-1L,-1L,-1L,x1,y1,z1,t1,-1L,-1L,-1L,readAs4D);
69int read_volumeROI(volume<T>& target,
const std::string& filename,
70 int64_t x0, int64_t y0, int64_t z0, int64_t t0,
71 int64_t x1, int64_t y1, int64_t z1, int64_t t1,
72 const int64_t xskip, int64_t yskip, int64_t zskip, int64_t tskip);
74int read_complexvolume(volume<float>& realvols, volume<float>& imagvols,
75 const std::string& filename,
bool read_img_data=
true);
76int read_complexvolume(complexvolume& vol,
const std::string& filename);
79void set_volume_properties(
const NiftiIO::NiftiHeader& niihdr, volume<T>& target);
83int read_volume_hdr_only(volume<T>& target,
const std::string& filename)
85 int64_t nthreads = target.nthreads();
87 NiftiIO::NiftiHeader niihdr;
89 niihdr = NiftiIO::loadHeader(return_validimagefilename(filename));
90 }
catch ( std::exception& e ) { imthrow(
"Failed to read volume "+filename+
"\nError : "+e.what(),22); }
91 for (
int n=1; n<=7; n++) {
92 if (niihdr.dim[n]<1) niihdr.dim[n]=1;
95 target.initialize(niihdr.dim[1],niihdr.dim[2],niihdr.dim[3],niihdr.dim[4],niihdr.dim[5],niihdr.dim[6],niihdr.dim[7],tbuffer,
true,nthreads);
96 set_volume_properties(niihdr,target);
97 if (!target.RadiologicalFile) target.makeradiological(
true);
102int read_volume4D_hdr_only(volume<T>& target,
const std::string& filename) {
103 return read_volume_hdr_only(target,filename);
110int save_volume(
const volume<T>& source,
const std::string& filename,
const int filetype=-1);
112int save_complexvolume(
const volume<float>& realvol,
113 const volume<float>& imagvol,
const std::string& filename);
114int save_complexvolume(
const complexvolume& vol,
const std::string& filename);
121short closestTemplatedType(
const short inputType);
123int read_volume_size(
const std::string& filename,
124 int64_t& sx, int64_t& sy, int64_t& sz, int64_t& st, int64_t& s5, int64_t& s6, int64_t& s7);
126short dtype(
const char* T);
127short dtype(
const short* T);
128short dtype(
const int* T);
129short dtype(
const float* T);
130short dtype(
const double* T);
132short dtype(
const volume<char>& vol);
133short dtype(
const volume<short>& vol);
134short dtype(
const volume<int>& vol);
135short dtype(
const volume<float>& vol);
136short dtype(
const volume<double>& vol);
138short dtype(
const std::string& filename);
146int load_volume(volume<T>& target,
const std::string& filename);
151int write_volume(
const volume<T>& source,
const std::string& filename);
165int read_volumeROI(volume<T>& target,
const std::string& filename,
166 int64_t x0, int64_t y0, int64_t z0, int64_t x1, int64_t y1, int64_t z1,
167 int64_t xskip, int64_t yskip, int64_t zskip)
169 int retval=read_volumeROI(target,filename,x0,y0,z0,x1,y1,z1);
171 if (xskip<1) xskip=1;
172 if (yskip<1) yskip=1;
173 if (zskip<1) zskip=1;
174 int64_t sx=(target.maxx()-target.minx())/xskip + 1;
175 int64_t sy=(target.maxy()-target.miny())/yskip + 1;
176 int64_t sz=(target.maxz()-target.minz())/zskip + 1;
177 volume<T> tmpvol(sx,sy,sz);
178 int64_t xx=0, yy=0, zz=0, x=0, y=0, z=0;
179 for (z=target.minz(), zz=0; z<=target.maxz(); z+=zskip, zz++) {
180 for (y=target.miny(), yy=0; y<=target.maxy(); y+=yskip, yy++) {
181 for (x=target.minx(), xx=0; x<=target.maxx(); x+=xskip, xx++) {
182 tmpvol(xx,yy,zz) = target(x,y,z);
186 tmpvol.copyproperties(target);
198int read_volumeROI(volume<T>& target,
const std::string& filename,
199 int64_t x0, int64_t y0, int64_t z0,
200 int64_t x1, int64_t y1, int64_t z1)
203 return read_volumeROI(target,filename,dtype,
204 x0,y0,z0,0,x1,y1,z1,0);
209int read_volumeROI(volume<T>& target,
const std::string& filename,
210 int64_t x0, int64_t y0, int64_t z0, int64_t t0,
211 int64_t x1, int64_t y1, int64_t z1, int64_t t1,
212 int64_t xskip, int64_t yskip, int64_t zskip, int64_t tskip)
214 read_volumeROI(target,filename,x0,y0,z0,t0,x1,y1,z1,t1);
215 if (xskip<1) xskip=1;
216 if (yskip<1) yskip=1;
217 if (zskip<1) zskip=1;
218 if (tskip<1) tskip=1;
219 int64_t sx=(target.maxx()-target.minx())/xskip + 1;
220 int64_t sy=(target.maxy()-target.miny())/yskip + 1;
221 int64_t sz=(target.maxz()-target.minz())/zskip + 1;
222 int64_t st=(target.maxt()-target.mint())/tskip + 1;
223 volume<T> tmpvol(sx,sy,sz,st);
224 int64_t xx=0, yy=0, zz=0, tt=0, x=0, y=0, z=0, t=0;
225 for (t=target.mint(), tt=0; t<=target.maxt(); t+=tskip, tt++) {
226 for (z=target.minz(), zz=0; z<=target.maxz(); z+=zskip, zz++) {
227 for (y=target.miny(), yy=0; y<=target.maxy(); y+=yskip, yy++) {
228 for (x=target.minx(), xx=0; x<=target.maxx(); x+=xskip, xx++) {
229 tmpvol(xx,yy,zz,tt) = target(x,y,z,t);
234 tmpvol.copyproperties(target[0]);
240int read_volumeROI(volume<T>& target,
const std::string& filename,
241 int64_t x0, int64_t y0, int64_t z0, int64_t t0,
242 int64_t x1, int64_t y1, int64_t z1, int64_t t1)
245 return (read_volumeROI(target,filename,dtype,
246 x0,y0,z0,t0,x1,y1,z1,t1,
true));
251int read_volume(volume<T>& target,
const std::string& filename,
const bool& legacyRead)
254 read_volumeROI(target,filename,dtype,0,0,0,0,-1,-1,-1,-1,
true);
255 if ( legacyRead && target.tsize() > 1 ) {
256 std::cerr <<
"Warning: An input intended to be a single 3D volume has " <<
257 "multiple timepoints. Input will be truncated to first volume, but " <<
258 "this functionality is deprecated and will be removed in a future release." << std::endl;
259 target=volume<T>(target[0]);
268NiftiIO::mat44 newmat2mat44(
const NEWMAT::Matrix& nmat);
271int set_fsl_hdr(
const volume<T>& source, NiftiIO::NiftiHeader& niihdr)
273 niihdr.dim[0]=source.dimensionality();
274 niihdr.dim[1]=source.size1();
275 niihdr.dim[2]=source.size2();
276 niihdr.dim[3]=source.size3();
277 niihdr.dim[4]=source.size4();
278 niihdr.dim[5]=source.size5();
279 niihdr.dim[6]=source.size6();
280 niihdr.dim[7]=source.size7();
282 niihdr.datatype=dtype(source);
284 niihdr.pixdim[1]=source.pixdim1();
285 niihdr.pixdim[2]=source.pixdim2();
286 niihdr.pixdim[3]=source.pixdim3();
287 niihdr.pixdim[4]=source.pixdim4();
288 niihdr.pixdim[5]=source.pixdim5();
289 niihdr.pixdim[6]=source.pixdim6();
290 niihdr.pixdim[7]=source.pixdim7();
293 niihdr.sformCode = source.sform_code();
294 niihdr.qformCode = source.qform_code();
295 niihdr.setSForm(newmat2mat44(source.sform_mat()));
296 niihdr.setQForm(newmat2mat44(source.qform_mat()));
298 niihdr.intentCode = source.intent_code();
299 niihdr.intent_p1 = source.intent_param(1);
300 niihdr.intent_p2 = source.intent_param(2);
301 niihdr.intent_p3 = source.intent_param(3);
303 niihdr.sclSlope = 1.0;
304 niihdr.sclInter = 0.0;
306 niihdr.cal_min = source.getDisplayMinimum();
307 niihdr.cal_max = source.getDisplayMaximum();
308 niihdr.auxillaryFile = source.getAuxFile();
310 niihdr.units= NiftiIO::NIFTI_UNITS_SEC | NiftiIO::NIFTI_UNITS_MM;
311 niihdr.sliceOrdering=0;
317int save_basic_volume(
const volume<T>& source,
const std::string& filename,
318 int filetype,
bool save_orig=
false);
321int save_volume(
const volume<T>& source,
const std::string& filename,
const int filetype) {
322 return save_basic_volume(source,filename,filetype,
false);
325template <
class T,
class dType>
327 int operator()(
const volume<T> source,
const std::string& filename,
const int filetype) {
329 copyconvert(source,output);
330 return save_volume(output,filename,filetype);
336 int operator()(
const volume<T> source,
const std::string& filename,
const int filetype) {
337 return save_volume(source,filename,filetype);
342int save_volume_dtype(
const volume<T>& source,
const std::string& filename,
short datatype,
const int filetype=-1)
344 switch(closestTemplatedType(datatype)) {
345 case NiftiIO::DT_UNSIGNED_CHAR:
return typedSave<T,char>()(source,filename,filetype);
347 case NiftiIO::DT_SIGNED_INT:
return typedSave<T,int>()(source,filename,filetype);
351 std::ostringstream errmsg;
352 errmsg <<
"NEWIMAGE::save_volume_dtype: DT " << datatype <<
" not supported";
353 perror(errmsg.str().c_str());
359int save_volume_and_splines(
const volume<T>& source,
const std::string& filename)
361 if (!source.hasSplines()) {
362 std::cerr <<
"NEWIMAGE::save_volume_and_splines: volume has no valid splines" << std::endl;
365 if (source.tsize() > 1) {
366 std::cerr <<
"NEWIMAGE::save_volume_and_splines: writing of 4D files not supported" << std::endl;
369 volume<T> ovol(source.xsize(),source.ysize(),source.zsize(),2);
370 copybasicproperties(source,ovol);
371 for (
int k=0; k<source.zsize(); k++) {
372 for (
int j=0; j<source.ysize(); j++) {
373 for (
int i=0; i<source.xsize(); i++) {
374 ovol(i,j,k,0) = source(i,j,k);
375 ovol(i,j,k,1) = source.splineCoef(i,j,k);
379 return(save_volume(ovol,filename));
385int save_orig_volume(
const volume<T>& source,
const std::string& filename,
const int filetype = -1)
387 return save_basic_volume(source,filename,filetype,
true);
397int load_volume(volume<T>& target,
const std::string& filename)
398{
return read_volume(target,filename); }
402int write_volume(
const volume<T>& source,
const std::string& filename)
403{
return save_volume(source,filename); }
408int read_orig_volume(volume<T>& target,
const std::string& filename)
411 read_volumeROI(target,filename,dtype,0,0,0,0,-1,-1,-1,-1,
false);
418int write_volume4D(
const volume<T>& source,
const std::string& filename) {
419 return save_volume(source,filename); }
421int save_volume4D(
const volume<T>& source,
const std::string& filename) {
422 return save_volume(source,filename); }
424int load_volume4D(volume<T>& source,
const std::string& filename) {
425 return read_volume(source,filename,
false); }
427int read_volume4D(volume<T>& source,
const std::string& filename) {
428 return read_volume(source,filename,
false); }
430int read_orig_volume4D(volume<T>& target,
const std::string& filename) {
431 return read_orig_volume(target,filename);}
Definition: newimage.h:100
Definition: newimageio.h:326