1 /*=========================================================================
4 Module: $RCSfile: marDicom.cpp,v $
6 Date: $Date: 2008/10/31 16:32:54 $
7 Version: $Revision: 1.1 $
9 Copyright: (c) 2002, 2003
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notice for more information.
16 =========================================================================*/
21 #include <idacr-restricted.h>
26 static char* DicomTagsNames[] = {
29 "ID_SOP_Instance_UID",
32 "ID_Acquisition_Date",
36 "ID_Acquisition_Time",
40 "ID_Institution_Name",
41 "ID_Study_Description",
42 "ID_Series_Description",
43 "ID_Admitting_Diagnoses_Description",
46 "ID_Body_Part_Examined",
47 "ID_Scanning_Sequence",
48 "ID_Sequence_Variant",
50 "ID_MR_Acquisition_Type",
56 "ID_Number_of_Averages",
57 "ID_Imaging_Frequency",
60 "ID_Magnetic_Field_Strength",
61 "ID_Spacing_Between_Slices",
62 "ID_Echo_Train_Length",
63 "ID_Percent_Sampling",
64 "ID_Percent_Phase_Field_of_View",
66 "ID_Patient_Position",
67 "ID_Study_Instance_UID",
68 "ID_Series_Instance_UID",
71 "ID_Acquisition_Number",
73 "ID_Patient_Orientation",
75 "ID_Image_Position_Patient",
76 "ID_Image_Orientation",
77 "ID_Image_Orientation_Patient",
79 "ID_Frame_of_Reference_UID",
87 // -------------------------------------------------------------------------
88 marDicom::marDicom( marParameters* p )
90 _actualStudy( "" ), _actualSerie( "" ),
95 // ----------------------------------------------------------------------------
96 wxArrayString marDicom_SelectDirs( wxString& root )
103 it = wxFindFirstFile( tmp, wxDIR );
104 while( !it.IsEmpty( ) )
106 if( wxFileNameFromPath( it ).Cmp( "." ) != 0 && wxFileNameFromPath( it ).Cmp( ".." ) != 0 )
107 ret.Add( wxFileNameFromPath( it ) );
109 it = wxFindNextFile( );
116 // -------------------------------------------------------------------------
117 wxArrayString marDicom::getStudies( )
119 return( marDicom_SelectDirs( getParameters( )->getStringParam( marParameters::e_dicom_images_directory ) ) );
122 // -------------------------------------------------------------------------
123 wxArrayString marDicom::getSeries( )
127 tmp = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
130 return( marDicom_SelectDirs( tmp ) );
133 // -------------------------------------------------------------------------
134 wxArrayString marDicom::getStudyData( )
141 if( _actualStudy != "" ) {
142 cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
146 // WARNING : Use of LibIDO to get the information.
147 res = IdStrGetExamInfo( ( char* )cad.c_str( ), "*" );
148 for( i = 0; i < ID_dicom_tags_count; i++ )
150 ret.Add( copystring( res[ i ] ) );
154 ret[ID_File_Name] = _actualStudy;
161 // -------------------------------------------------------------------------
162 wxArrayString marDicom::getSerieData( )
169 if( _actualSerie != "" )
171 // WARNING : Use of LibIDO to get the information.
172 cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
178 res = IdStrGetSerieInfo( ( char* )cad.c_str( ), "*" );
179 for( i = 0; i < ID_dicom_tags_count; i++ )
181 ret.Add( copystring( res[ i ] ) );
185 ret[ ID_File_Name ] = _actualSerie;
192 // -------------------------------------------------------------------------
193 wxArrayString marDicom_SelectFiles( wxString& root )
200 it = wxFindFirstFile( tmp, wxFILE );
201 while( !it.IsEmpty( ) )
203 if( IdAcrIsAcrReadable( ( char* )it.c_str( ) ) ) // Check if DICOM
206 it = wxFindNextFile( );
213 // -------------------------------------------------------------------------
214 void marDicom::loadActualSerie( )
220 tmp = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
225 _imageFileNames = marDicom_SelectFiles( tmp );
229 // -------------------------------------------------------------------------
230 bool marDicom_ReadAllImageData( wxString& fn,
234 double& vX, double& vY, double& vZ )
240 ima = ( PPIMAGE_USHORT )IdAcrReadFile( ( char* )fn.c_str( ), IMA_USHORT );
241 dX = IdImaDimX( ima );
242 dY = IdImaDimY( ima );
244 tmp = IdAcrInquireIRMInfo( ( char* )fn.c_str( ) );
246 // printf("%s\n", tmp[20]);
248 //stmp = strtok( tmp[ 20 ], "\\" );
249 //vX = strtod( stmp, &end );
250 //stmp = strtok( NULL, "\\" );
251 //vY = strtod( stmp, &end );
253 sscanf( tmp[ 20 ], "%lf\\%lf", &vX, &vY);
254 //vZ = strtod( tmp[ 6 ], &end );
255 vZ = strtod( tmp[ 6 ], NULL );
257 // printf("Vx = %lf Vy = %lf Vz = %lf\n", vX, vY, vZ);
259 //for( int i = 0; i < marDicom::ID_dicom_tags_count; i++ )
266 // -------------------------------------------------------------------------
267 void marDicom::loadVolume( bool force )
270 TODO: this function calls 11 534 336 to kVolume::setPixel !!!!
272 wxString number, tmp;
273 int size, i, j, k, dx, dy, dz, loc;
275 PPIMAGE_USHORT ima_t;
277 if( force || !_volume )
280 _imageNumbers.Clear( );
281 dz = _imageFileNames.GetCount( );
285 tmp = _imageFileNames[0 ];
286 marDicom_ReadAllImageData( tmp, ima_t, number, dx, dy, vX, vY, vZ );
287 getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, vX );
288 getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, vY );
289 getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, vZ );
291 printf("dx = %i dy = %i dz = %i vX = %lf vY = %lf vZ = %lf\n", dx, dy, dz, vX, vY, vZ );
292 _volume = new kVolume( kVolume::USHORT, dx, dy, dz, vX, vY, vZ );
296 for( k = 0; k < dz; k++ )
298 tmp = _imageFileNames[ k ];
299 marDicom_ReadAllImageData( tmp, ima_t, number, dx, dy, vX, vY, vZ );
300 _imageNumbers.Add( number.c_str( ) );
302 for( j = 0; j < dy; j++ )
303 for( i = 0; i < dx; i++ )
304 _volume->setPixel( ima_t[ j ][ i ], i, j, k );
313 // -------------------------------------------------------------------------
314 void marDicom::freeVolume( )
316 if( _volume ) delete _volume;
320 // -------------------------------------------------------------------------
321 bool marDicom::setActualStudy( wxString& s )
325 if( _actualStudy != s )
327 cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
330 if( wxDirExists( cad.c_str( ) ) )
343 // -------------------------------------------------------------------------
344 bool marDicom::setActualSerie( wxString& s )
348 if( _actualSerie != s )
350 cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
355 if( wxDirExists( cad.c_str( ) ) )
367 // -------------------------------------------------------------------------
368 void marDicom::copyFrom( const marObject& from )
372 // -------------------------------------------------------------------------
373 bool marDicom::save( std::ofstream& os )
377 s = _actualStudy.length( );
378 os.write( ( const char* )&s, sizeof( int ) );
379 os.write( ( char* )_actualStudy.c_str( ), s * sizeof( char ) );
380 s = _actualSerie.length( );
381 os.write( ( const char* )&s, sizeof( int ) );
382 os.write( ( char* )_actualSerie.c_str( ), s * sizeof( char ) );
386 // -------------------------------------------------------------------------
387 bool marDicom::load( std::ifstream& is )
393 is.read( ( char* )&s, sizeof( int ) );
394 _actualStudy.resize( s );
395 is.read( ( char* )_actualStudy.c_str( ), s * sizeof( char ) );
396 is.read( ( char* )&s, sizeof( int ) );
397 _actualSerie.resize( s );
398 is.read( ( char* )_actualSerie.c_str( ), s * sizeof( char ) );
402 // ----------------------------------------------------------------------------
403 wxArrayString marDicom::getRelationalArrayStudyData( )
406 wxArrayString inf = getStudyData( );
409 for( i = 0; i < inf.GetCount( ); i++ )
411 ret.Add( wxString( DicomTagsNames[ i ] ) );
412 ret.Add( wxString( inf[ i ] ) );
419 // ----------------------------------------------------------------------------
420 wxArrayString marDicom::getRelationalArraySerieData( )
423 wxArrayString inf = getSerieData( );
426 for( i = 0; i < inf.GetCount( ); i++ ) {
428 ret.Add( wxString( DicomTagsNames[ i ] ) );
429 ret.Add( wxString( inf[ i ] ) );