% ss_write_Space_16bitIndexedTree( vol, LUT, filestem ) % ------------------------------------------------------------------------ % % Writes a 3-D matlab matrix into a Space type 1 volume file. % % Input: % % im - Volume matrix () is a volume of type double with % a range >= 0. % % global VOX % - If this variable is globally defined and is a 3 element % vector, the values are used as the x/y/z voxel dimensions. % % See http://lcni.uoregon.edu/~mark for a description of Space % software for viewing and using the output volume. % % Mark Dow, last modified April 2004 % University of Oregon % Brain Development Lab / Lewis Center for Neuroimaging % http://lcni.uoregon.edu/~mark % dow@uoregon.edu % function ss_write_Space_16bitIndexedTree( vol, LUT, filestem ) global VOX; global SCALE; global OFFSET; global ORIGIN; global DESCRIP; outfilename = [ filestem '.vol']; % To Do: Check for empty volume and warn. % Shift histogram of intensities to between 0 and 255. fid = fopen( outfilename, 'w', 'l'); % Write file identifier (mdvol, 5 char's - 3 x 8 bits) file_id = 'mdvol'; count = fwrite( fid, file_id, 'char' ); % Write file version (1 char - 8 bits) file_version = '1'; count = fwrite( fid, file_version, 'char' ); % Write header length (1 int - 1 x 32 bits) h_length = 10000; count = fwrite( fid, h_length, 'int' ); % Write pixel dimensions (3 int's - 3 x 32 bits) dim = size(vol); %%%%% dim(3) = 1; %%%%% %[ 'volume dimensions (voxels): ' num2str(dim(1)) ' x ' num2str(dim(2)) ' x ' num2str(dim(3))] [ 'Space volume dimensions (voxels): ' num2str(dim(1)) ' x ' num2str(dim(2)) ' x ' num2str(dim(3))] count = fwrite( fid, dim, 'int' ); % Write pixel sizes (3 float's - 3 x 32 bits) psize = [0,0,0]; if length( VOX ) == 3 & VOX(1) > 0 & VOX(2) > 0 & VOX(3) > 0 psize = VOX; end %[ 'voxel size: ' num2str(size(1)) ' x ' num2str(size(2)) ' x ' num2str(size(3))] count = fwrite( fid, psize, 'float32' ); % Write data format code(3 char's - 3 x 8 bits) data_format = '16t'; count = fwrite( fid, data_format, 'char' ); % Write initial black and white point for display (2 float - 2 x 32 bits) bw_point = [0.0,1.0]; count = fwrite( fid, bw_point, 'float32' ); % Write initial gamma for display (1 float - 1 x 32 bits) gamma = 1.0; count = fwrite( fid, gamma, 'float32' ); % Write file format description text (4900 char's - 1500 x 8 bits) for i = 1:4900 file_description(i) = ' '; end text = ' This is a simple file, consisting of a header and a set of value bytes, for storing volume image data. Specified by Mark Dow, January 2001. The header is a total of 10000 bytes: The first five characters (mdvol, 5 x 8 bits) identify the file as volume with this file type. One character, 8 bits, serves as a version indicator -- this is version 1. One integer, 32 bits, is the total length of the header. Three integers are dimensions, xyz in pixels, of the volume. Three floats, 3x32 bits, are physical dimensions, xyz in mm, of each voxel. Two floats indicate a reasonable black point and white point for the initial display, with a range [0,1]. One float indicates a reasonable gamma for the initial display where the gamma is applied after the black/white point. Three characters specifies gray 8 bit, g08, gray 16 bit, g16, or color 24 bit, c24, byte information. 4900 characters, 4900 x 8 bits, of text is this description of the file format. 151 characters are a text title. 4900 characters are a text description of the volume contents. The rest are bytes, in x/y/z or r/g/b/x/y/z order, describing the image data. If the color character is g08, there are x*y*z bytes of color data. If the color character is g16, there are 2*x*y*z bytes of color data. If the color character is c24, there are 3*x*y*z bytes of color data. '; file_description(1:length(text) ) = text; count = fwrite( fid, file_description, 'char' ); % Write volume title text (151 char's - 151 x 8 bits) for i = 1:151 volume_description(i) = ' '; end text = ''; volume_description(1:length(text) ) = text; count = fwrite( fid, volume_description, 'char' ); % Write volume description text (4900 char's - 3 x 8 bits) for i = 1:4900 volume_description(i) = ' '; end volume_description(1:length(DESCRIP) ) = DESCRIP; %[ 'volume description: ' DESCRIP ] count = fwrite( fid, volume_description, 'char' ); % size(LUT) % Write LUT (16,777,216 char's) count = fwrite( fid, LUT, 'char' ); %L(1:16777216) = LUT; %hist(L,256) %size(vol) % Write labels (unsigned 16 bit, uxvxw ushort's - uxvxw x 16 bits) count = fwrite( fid, uint16(vol), 'uint16' ); fclose(fid); clear im;