/************************************************************************ File: im02vtk.c Author: George J. Grevera Date: 12-july-2006 Desc.: This program takes slices from a 3D grey image in the IM0 format and writes it out as a 3D vtk version 2.0 file. Byte order is reversed (for shorts). ************************************************************************/ #define BINARY_OUTPUT #include #include #include #include #include #include "Viewnix.h" #include "3dv.h" typedef unsigned char uchar; typedef unsigned long ulong; static FILE *infp; static ViewnixHeader vh_in; static ulong bytes_per_slice; static int size; static char *data, group[5], element[5]; static bool verbose=true; /************************************************************************/ /* This function performs any necessary initialization such as opening files and allocating memory. */ static int init ( int argc, char* argv[] ) { if (argc!=2) { printf( "\nusage: \n %s \n", argv[0] ); puts ( " Output written to stdout. \n" ); exit( 0 ); } //open the input file infp = fopen( argv[1], "rb" ); assert( infp != NULL ); //read the input file's header int err = VReadHeader( infp, &vh_in, group, element ); if (err && err < 106) { fprintf(stderr, "Can't read the input file.\n"); exit(-1); } assert(vh_in.scn.dimension == 3); switch (vh_in.scn.num_of_bits) { case 8 : size = 1; break; case 16 : size = 2; break; default : assert(0); break; } if (::verbose) fprintf( stderr, "size=%d byte(s). \n", size ); bytes_per_slice = size * vh_in.scn.xysize[0] * vh_in.scn.xysize[1]; data = (char*) malloc(bytes_per_slice); assert(data != NULL); } /************************************************************************/ static void initOutput ( void ) { puts( "# vtk DataFile Version 2.0" ); puts( "created from IM0 data" ); #ifdef BINARY_OUTPUT puts( "BINARY" ); #else puts( "ASCII" ); #endif puts( "DATASET STRUCTURED_POINTS" ); printf( "DIMENSIONS %d %d %d\n", vh_in.scn.xysize[0], vh_in.scn.xysize[1], vh_in.scn.num_of_subscenes[0] ); if ( vh_in.scn.num_of_subscenes[0]==1 ) printf( "SPACING %f %f %f\n", vh_in.scn.xypixsz[0], vh_in.scn.xypixsz[1], 1.0 ); else printf( "SPACING %f %f %f\n", vh_in.scn.xypixsz[0], vh_in.scn.xypixsz[1], (vh_in.scn.loc_of_subscenes[1]-vh_in.scn.loc_of_subscenes[0]) ); puts( "ORIGIN 0.0 0.0 0.0" ); printf( "POINT_DATA %d\n", (vh_in.scn.xysize[0] * vh_in.scn.xysize[1] * vh_in.scn.num_of_subscenes[0]) ); if (size==1) puts( "SCALARS scalars unsigned_char 1" ); else puts( "SCALARS scalars unsigned_short 1" ); puts( "LOOKUP_TABLE default" ); } /************************************************************************/ //This function converts the IM0 data to vtk. static void im02vtk( void ) { //move to the beginning of the data part int err = VSeekData( infp, 0 ); assert(err == 0); //read the input file's data (slice by slice) for (int z=0; z