X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=utilities_LW%2Fmha_read_volume.m;fp=utilities_LW%2Fmha_read_volume.m;h=e6d4a443d5f80013951577517b7fc0cf2dc701bb;hb=d0401c49b6b7511cfdaa0534b78bd3c5b2b0637a;hp=0000000000000000000000000000000000000000;hpb=f8358f5ec65f099f3080043580ef861c3fd3ba2e;p=CreaPhase.git diff --git a/utilities_LW/mha_read_volume.m b/utilities_LW/mha_read_volume.m new file mode 100644 index 0000000..e6d4a44 --- /dev/null +++ b/utilities_LW/mha_read_volume.m @@ -0,0 +1,88 @@ +function V = mha_read_volume(info) +% Function for reading the volume of a Insight Meta-Image (.mha, .mhd) file +% +% volume = tk_read_volume(file-header) +% +% examples: +% 1: info = mha_read_header() +% V = mha_read_volume(info); +% imshow(squeeze(V(:,:,round(end/2))),[]); +% +% 2: V = mha_read_volume('test.mha'); + +if(~isstruct(info)), info=mha_read_header(info); end + + +switch(lower(info.DataFile)) + case 'local' + otherwise + % Seperate file + info.Filename=fullfile(fileparts(info.Filename),info.DataFile); +end + +% Open file +switch(info.ByteOrder(1)) + case ('true') + fid=fopen(info.Filename,'rb','ieee-be'); + otherwise + fid=fopen(info.Filename,'rb','ieee-le'); +end + +switch(lower(info.DataFile)) + case 'local' + % Skip header + fseek(fid,info.HeaderSize,'bof'); + otherwise + fseek(fid,0,'bof'); +end + +datasize=prod(info.Dimensions)*info.BitDepth/8; % in bytes + +switch(info.CompressedData(1)) + case 'f' + % Read the Data + switch(info.DataType) + case 'char' + V = int8(fread(fid,datasize,'char')); + case 'uchar' + V = uint8(fread(fid,datasize,'uchar')); + case 'short' + V = int16(fread(fid,datasize,'short')); + case 'ushort' + V = uint16(fread(fid,datasize,'ushort')); + case 'int' + V = int32(fread(fid,datasize,'int')); + case 'uint' + V = uint32(fread(fid,datasize,'uint')); + case 'float' + V = single(fread(fid,datasize,'float')); + case 'double' + V = double(fread(fid,datasize,'double')); + end + case 't' + switch(info.DataType) + case 'char', DataType='int8'; + case 'uchar', DataType='uint8'; + case 'short', DataType='int16'; + case 'ushort', DataType='uint16'; + case 'int', DataType='int32'; + case 'uint', DataType='uint32'; + case 'float', DataType='single'; + case 'double', DataType='double'; + end + Z = fread(fid,inf,'uchar=>uint8'); + V = zlib_decompress(Z,DataType); + +end + +fclose(fid); +V = reshape(V,info.Dimensions); + +function M = zlib_decompress(Z,DataType) +import com.mathworks.mlwidgets.io.InterruptibleStreamCopier +a=java.io.ByteArrayInputStream(Z); +b=java.util.zip.InflaterInputStream(a); +isc = InterruptibleStreamCopier.getInterruptibleStreamCopier; +c = java.io.ByteArrayOutputStream; +isc.copyStream(b,c); +M=typecast(c.toByteArray,DataType);