Skip to content

Commit

Permalink
new type MRI_USHRT, env variables FS_MGZIO_USEVOXELBUF & FS_MGZIO_TIM…
Browse files Browse the repository at this point in the history
…ING (freesurfer#899)

* 1. check in package dcm2niix
2. add DICOMRead3/niiRead3 to convert dicom using dcm2niix
3. fix dcmGetDWIParams() to use stricmp() to compare the Manufacturer tag

* set TI to -1 when there is no TI in dicom file for MGH_FREESURFER

* remove unused codes

* This is the correct version.

* redo the check in

* For some unknown reasons, dcm2niix and dcm2niixfsexe executables failed linking in nightly builds.
They are excluded from the build until we figure out why.
libdcm2niixfs.a is renamed to libdcm2niix.a

* 1. rename dcm2fsWrapper.h => dcm2niix_fswrapper.h, dcm2fsWrapper.cpp => dcm2niix_fswrapper.cpp
2. add comments to dcm2niix changes

* fix bvecs

* 1. introduced new type MRI_USHRT (10) - unsigned short,
   convert DT_UINT16 to MRI_USHRT, MRI_USHRT to DT_UINT16
2. added macro MRIUSseq_vox & MRIUSvox to access voxel in pre-allocated memory
3. added handling of type MRI_USHRT in various functions
4. introduced environment variable FS_MGZIO_USEVOXELBUF to read/write (mgzRead/mgzWrite)
   voxels in one buffer access. FS_MGZIO_USEVOXELBUF is ignored if mri voxel buffer is
   not allocated as a big chunk.
5. introduced environment variable FS_MGZIO_TIMING to time voxel read/write in mgzRead/mgzWrite

* ignore FS_MGZIO_USEVOXELBUF if mri voxel buffer is not allocated as a big chunk

Co-authored-by: Yujing Huang <[email protected]>
  • Loading branch information
yhuang43 and Yujing Huang authored Dec 6, 2021
1 parent a0c821d commit b82c3a8
Show file tree
Hide file tree
Showing 29 changed files with 940 additions and 128 deletions.
42 changes: 42 additions & 0 deletions freeview/FSVolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ bool FSVolume::Create( FSVolume* src_vol, bool bCopyVoxelData, int data_type )
case MRI_SHORT:
m_imageData->AllocateScalars(VTK_SHORT, 1);
break;
case MRI_USHRT:
m_imageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
break;
default:
break;
}
Expand All @@ -605,6 +608,9 @@ bool FSVolume::Create( FSVolume* src_vol, bool bCopyVoxelData, int data_type )
case MRI_SHORT:
m_imageData->SetScalarTypeToShort();
break;
case MRI_USHRT:
m_imageData->SetScalarTypeToUnsignedShort();
break;
default:
break;
}
Expand Down Expand Up @@ -1195,6 +1201,9 @@ bool FSVolume::UpdateMRIFromImage( vtkImageData* rasImage, bool resampleToOrigin
case MRI_SHORT:
MRISseq_vox( mri, i, j, k, nFrame ) = (short)val;
break;
case MRI_USHRT:
MRIUSseq_vox( mri, i, j, k, nFrame ) = (unsigned short)val;
break;
default:
break;
}
Expand Down Expand Up @@ -2042,6 +2051,9 @@ bool FSVolume::CreateImage( MRI* rasMRI )
case MRI_SHORT:
imageData->AllocateScalars(VTK_SHORT, zFrames);
break;
case MRI_USHRT:
imageData->AllocateScalars(VTK_UNSIGNED_SHORT, zFrames);
break;
default:
return false;
}
Expand All @@ -2065,6 +2077,9 @@ bool FSVolume::CreateImage( MRI* rasMRI )
case MRI_SHORT:
imageData->SetScalarTypeToShort();
break;
case MRI_USHRT:
imageData->SetScalarTypeToUnsignedShort();
break;
default:
return false;
}
Expand Down Expand Up @@ -2150,6 +2165,9 @@ bool FSVolume::ResizeRotatedImage( MRI* rasMRI, MRI* refTarget, vtkImageData* re
case MRI_SHORT:
imageData->AllocateScalars(VTK_SHORT, zFrames);
break;
case MRI_USHRT:
imageData->AllocateScalars(VTK_UNSIGNED_SHORT, zFrames);
break;
default:
return false;
}
Expand All @@ -2173,6 +2191,9 @@ bool FSVolume::ResizeRotatedImage( MRI* rasMRI, MRI* refTarget, vtkImageData* re
case MRI_SHORT:
imageData->SetScalarTypeToShort();
break;
case MRI_USHRT:
imageData->SetScalarTypeToUnsignedShort();
break;
default:
break ;
}
Expand Down Expand Up @@ -2406,6 +2427,9 @@ void FSVolume::CopyMRIDataToImage( MRI* mri,
case MRI_SHORT:
((short*)ptr)[nTuple*zFrames+nFrame] = MRISseq_vox( mri, nX, nY, nZ, nFrame );
break;
case MRI_USHRT:
((unsigned short*)ptr)[nTuple*zFrames+nFrame] = MRIUSseq_vox( mri, nX, nY, nZ, nFrame );
break;
default:
break;
}
Expand Down Expand Up @@ -3163,6 +3187,24 @@ void FSVolume::GetFrameValueRange(int frame, double *range)
}
}
break ;
case MRI_USHRT:
{
for (z = 0 ; z < depth ; z++)
{
for (y = 0 ; y < height ; y++)
{
for (x = 0 ; x < width ; x++)
{
val = (float)MRIUSseq_vox(mri, x, y, z, frame) ;
if (val < fmin)
fmin = val ;
if (val > fmax)
fmax = val ;
}
}
}
}
break ;
case MRI_UCHAR:
{
for (z = 0 ; z < depth ; z++)
Expand Down
3 changes: 3 additions & 0 deletions freeview/LayerODF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ bool LayerODF::Load(const QString &fn, const QString& vertex_fn, const QString&
case MRI_SHORT:
MRISseq_vox( mri2, j, k, n, i ) = MRISseq_vox( mri, i, j, k, n );
break;
case MRI_USHRT:
MRIUSseq_vox( mri2, j, k, n, i ) = MRIUSseq_vox( mri, i, j, k, n );
break;
default:
break;
}
Expand Down
7 changes: 7 additions & 0 deletions freeview/LayerSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,13 @@ bool LayerSurface::LoadRGBFromFile(const QString &filename)
for (int j = 0; j < 3; j++)
map.data << MRISseq_vox( mri, i, j, 0, 0 );
break;

case MRI_USHRT:
for (int i = 0; i < GetNumberOfVertices(); i++)
for (int j = 0; j < 3; j++)
map.data << MRIUSseq_vox( mri, i, j, 0, 0 );
break;

default:
MRIfree(&mri);
return false;
Expand Down
3 changes: 3 additions & 0 deletions freeview/SurfaceAnnotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ bool SurfaceAnnotation::LoadFromSegmentation(const QString &fn)
case MRI_SHORT:
n = MRIseq_vox( mri, i, 0, 0, 0);
break;
case MRI_USHRT:
n = (int)MRIUSseq_vox( mri, i, 0, 0, 0);
break;
default:
break;
}
Expand Down
12 changes: 11 additions & 1 deletion freeview/VolumeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ MRI* VolumeFilter::CreateMRIFromVolume( LayerMRI* layer )
mri_type = MRI_LONG;
break;
case VTK_SHORT:
case VTK_UNSIGNED_SHORT:
mri_type = MRI_SHORT;
break;
case VTK_UNSIGNED_SHORT:
mri_type = MRI_USHRT;
break;
default:
break;
}
Expand Down Expand Up @@ -180,6 +182,10 @@ MRI* VolumeFilter::CreateMRIFromVolume( LayerMRI* layer )
MRISseq_vox( mri, i, j, k, nFrame ) =
(int)MyVTKUtils::GetImageDataComponent(ptr, dim, n_frames, i, j, k, nFrame, scalar_type);
break;
case MRI_USHRT:
MRIUSseq_vox( mri, i, j, k, nFrame ) =
(unsigned short)MyVTKUtils::GetImageDataComponent(ptr, dim, n_frames, i, j, k, nFrame, scalar_type);
break;
default:
break;
}
Expand Down Expand Up @@ -234,6 +240,10 @@ void VolumeFilter::MapMRIToVolume( MRI* mri, LayerMRI* layer )
MyVTKUtils::SetImageDataComponent(ptr, dim, n_frames, nX, nY, nZ, nFrame, scalar_type,
MRISseq_vox( mri, nX, nY, nZ, nFrame ) );
break;
case MRI_USHRT:
MyVTKUtils::SetImageDataComponent(ptr, dim, n_frames, nX, nY, nZ, nFrame, scalar_type,
MRIUSseq_vox( mri, nX, nY, nZ, nFrame ) );
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions include/fio.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int znzreadShortEx (short *ps, znzFile fp) ;
int znzwriteDouble (double d, znzFile fp) ;
int znzwriteFloat (float f, znzFile fp) ;
int znzwriteShort (short s, znzFile fp) ;
int znzwriteUShort (unsigned short s, znzFile fp) ;
int znzwriteInt (int v, znzFile fp) ;
int znzwriteLong (long long v, znzFile fp) ;
int znzwrite1 (int v, znzFile fp) ;
Expand Down
2 changes: 2 additions & 0 deletions include/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
typedef int long32;
typedef long long long64;

unsigned short swapUShort(unsigned short us);
short swapShort(short s) ;
long32 swapLong32(long32 l);
long64 swapLong64(long64 l);
Expand All @@ -47,6 +48,7 @@ int ByteSwap8(void *buf8, long int nitems);

#define orderIntBytes(i) swapInt(i)
#define orderShortBytes(i) swapShort(i)
#define orderUShortBytes(i) swapUShort(i)
#define orderFloatBytes(i) swapFloat(i)
#define orderDoubleBytes(i) swapDouble(i)
#define orderLong32Bytes(i) swapLong32(i)
Expand Down
4 changes: 4 additions & 0 deletions include/mri.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#define MRI_FLOAT_COMPLEX 7
#define MRI_DOUBLE_COMPLEX 8
#define MRI_RGB 9
#define MRI_USHRT 10

#define NEAREST_NEIGHBOR_FACE 1
#define NEAREST_NEIGHBOR_EDGE 2
Expand Down Expand Up @@ -927,6 +928,7 @@ int MRIsurfaceRASToRAS(MRI *mri, double xsr, double ysr, double zsr,
#define MRIclear_bit(mri,x,y,z) MRIvox(mri,(x)/8,y,z) &= ~(0x001 << ((x)%8))

#define MRISvox(mri,x,y,z) (((short *)mri->slices[z][y])[x])
#define MRIUSvox(mri,x,y,z) (((unsigned short *)mri->slices[z][y])[x])
#define MRIFvox(mri,x,y,z) (((float *)(mri->slices[z][y]))[x])
#define MRIvox(mri,x,y,z) (((BUFTYPE *)mri->slices[z][y])[x])
#define MRISCvox(mri,x,y,z) (((signed char *)mri->slices[z][y])[x])
Expand All @@ -935,6 +937,8 @@ int MRIsurfaceRASToRAS(MRI *mri, double xsr, double ysr, double zsr,

#define MRISseq_vox(mri,x,y,z,n) (((short*)\
mri->slices[z+(n)*mri->depth][y])[x])
#define MRIUSseq_vox(mri,x,y,z,n) (((unsigned short*)\
mri->slices[z+(n)*mri->depth][y])[x])
#define MRISCseq_vox(mri,x,y,z,n) (((signed char*)\
mri->slices[z+(n)*mri->depth][y])[x])
#define MRIFseq_vox(mri,x,y,z,n) (((float*)\
Expand Down
7 changes: 7 additions & 0 deletions matlab/HippoSF/myMRIread.m
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@
MRI_FLOAT = 3 ;
MRI_SHORT = 4 ;
MRI_BITMAP = 5 ;
MRI_USHRT = 10 ;

% Determine number of bytes per voxel
switch type
Expand All @@ -546,6 +547,8 @@
nbytespervox = 1;
case MRI_SHORT,
nbytespervox = 2;
case MRI_USHRT,
nbytespervox = 2;
case MRI_INT,
nbytespervox = 4;
end
Expand All @@ -572,6 +575,8 @@
vol = fread(fid, nv, 'uchar') ;
case MRI_SHORT,
vol = fread(fid, nv, 'short') ;
case MRI_USHRT,
vol = fread(fid, nv, 'uint16') ;
case MRI_INT,
vol = fread(fid, nv, 'int') ;
end
Expand Down Expand Up @@ -620,6 +625,8 @@
[tmpslice nread] = fread(fid, nvslice, 'uchar') ;
case MRI_SHORT,
[tmpslice nread] = fread(fid, nvslice, 'short') ;
case MRI_USHRT,
[tmpslice nread] = fread(fid, nvslice, 'uint16') ;
case MRI_INT,
[tmpslice nread] = fread(fid, nvslice, 'int') ;
end
Expand Down
5 changes: 5 additions & 0 deletions matlab/load_mgh.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
MRI_FLOAT = 3 ;
MRI_SHORT = 4 ;
MRI_BITMAP = 5 ;
MRI_USHRT = 10 ;

% Determine number of bytes per voxel
switch type
Expand All @@ -166,6 +167,8 @@
nbytespervox = 1;
case MRI_SHORT,
nbytespervox = 2;
case MRI_USHRT,
nbytespervox = 2;
case MRI_INT,
nbytespervox = 4;
end
Expand Down Expand Up @@ -196,6 +199,8 @@
dtype = 'short' ;
case MRI_INT,
dtype = 'int' ;
case MRI_USHRT,
dtype = 'uint16' ;
end

% preserve volume datatype if env var is set to 1
Expand Down
5 changes: 4 additions & 1 deletion matlab/load_mgh2.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
MRI_FLOAT = 3 ;
MRI_SHORT = 4 ;
MRI_BITMAP = 5 ;
MRI_USHRT = 10 ;

fseek(fid, unused_space_size, 'cof') ;

Expand All @@ -95,7 +96,9 @@
case MRI_SHORT,
vol = fread(fid, nv, 'short') ;
case MRI_INT,
vol = fread(fid, nv, 'int') ;
vol = fread(fid, nv, 'int') ;
case MRI_USHRT,
vol = fread(fid, nv, 'uint16') ;
end
if(~feof(fid))
[mr_parms count] = fread(fid,4,'float32');
Expand Down
8 changes: 8 additions & 0 deletions mri_ca_normalize/mri_ca_normalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,10 @@ normalizeFromLabel(MRI *mri_in, MRI *mri_dst, MRI *mri_seg, double *fas)
MRISseq_vox(mri_dst, x, y, z, input) =
(short)nint(val) ;
break ;
case MRI_USHRT:
MRIUSseq_vox(mri_dst, x, y, z, input) =
(unsigned short)nint(val) ;
break ;
case MRI_FLOAT:
MRIFseq_vox(mri_dst, x, y, z, input) =
val ;
Expand Down Expand Up @@ -2146,6 +2150,10 @@ normalizeChannelFromLabel(MRI *mri_in, MRI *mri_dst, MRI *mri_seg,
MRISseq_vox(mri_dst, x, y, z, input_index) =
(short)nint(val) ;
break ;
case MRI_USHRT:
MRIUSseq_vox(mri_dst, x, y, z, input_index) =
(unsigned short)nint(val) ;
break ;
case MRI_FLOAT:
MRIFseq_vox(mri_dst, x, y, z, input_index) =
val ;
Expand Down
7 changes: 6 additions & 1 deletion mri_info/mri_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ static void do_file(char *fname)
case MRI_FLOAT: fprintf(fpout,"float\n") ; break ;
case MRI_LONG: fprintf(fpout,"long\n") ; break ;
case MRI_SHORT: fprintf(fpout,"short\n") ; break ;
case MRI_USHRT: fprintf(fpout,"ushrt\n") ; break ;
case MRI_INT: fprintf(fpout,"int\n") ; break ;
case MRI_TENSOR: fprintf(fpout,"tensor\n") ; break ;
}
Expand Down Expand Up @@ -761,7 +762,10 @@ static void do_file(char *fname)
case MRI_SHORT:
fprintf(fpout,"short\n") ;
break ;
case MRI_INT:
case MRI_USHRT:
fprintf(fpout,"ushrt\n") ;
break ;
case MRI_INT:
fprintf(fpout,"int\n") ;
break ;
case MRI_TENSOR:
Expand Down Expand Up @@ -1153,6 +1157,7 @@ static void do_file(char *fname)
printf(" type: %s (%d)\n",
mri->type == MRI_UCHAR ? "UCHAR" :
mri->type == MRI_SHORT ? "SHORT" :
mri->type == MRI_USHRT ? "USHRT" :
mri->type == MRI_INT ? "INT" :
mri->type == MRI_LONG ? "LONG" :
mri->type == MRI_BITMAP ? "BITMAP" :
Expand Down
19 changes: 19 additions & 0 deletions mri_ms_fitparms/mri_ms_fitparms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,7 @@ MRIssqrt(MRI *mri_src, MRI *mri_dst)
{
int width, height, depth, x, y, z, frame ;
short *psrc, *pdst ;
unsigned short *psrc2, *pdst2 ;

width = mri_src->width ;
height = mri_src->height ;
Expand All @@ -2662,6 +2663,15 @@ MRIssqrt(MRI *mri_src, MRI *mri_dst)
*pdst++ = sqrt(*psrc++) ;
}
break ;
case MRI_USHRT:
psrc2 = &MRIUSseq_vox(mri_src, 0, y, z, frame) ;
pdst2 = &MRIUSseq_vox(mri_dst, 0, y, z, frame) ;
for (x = 0 ; x < width ; x++)
{
check_finite(sqrt(*psrc2)) ;
*pdst2++ = sqrt(*psrc2++) ;
}
break ;
default:
ErrorReturn(NULL,
(ERROR_UNSUPPORTED,
Expand All @@ -2685,6 +2695,7 @@ MRIsscalarMul(MRI *mri_src, MRI *mri_dst, float scalar)
{
int width, height, depth, x, y, z, frame ;
short *psrc, *pdst ;
unsigned short *psrc2, *pdst2 ;

width = mri_src->width ;
height = mri_src->height ;
Expand All @@ -2710,6 +2721,14 @@ MRIsscalarMul(MRI *mri_src, MRI *mri_dst, float scalar)
*pdst++ = *psrc++ * scalar ;
}
break ;
case MRI_USHRT:
psrc2 = &MRIUSseq_vox(mri_src, 0, y, z, frame) ;
pdst2 = &MRIUSseq_vox(mri_dst, 0, y, z, frame) ;
for (x = 0 ; x < width ; x++)
{
*pdst2++ = *psrc2++ * scalar ;
}
break ;
default:
ErrorReturn(NULL,
(ERROR_UNSUPPORTED,
Expand Down
Loading

0 comments on commit b82c3a8

Please sign in to comment.