]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/marDicom.cpp
creaMaracasVisu Library
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / marDicom.cpp
1 /*=========================================================================
2
3   Program:   wxMaracas
4   Module:    $RCSfile: marDicom.cpp,v $
5   Language:  C++
6   Date:      $Date: 2008/10/31 16:32:54 $
7   Version:   $Revision: 1.1 $
8
9   Copyright: (c) 2002, 2003
10   License:
11   
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.
15
16 =========================================================================*/
17
18 extern "C"
19 {
20 #include <idacr.h>
21 #include <idacr-restricted.h>
22 }
23 #include "marDicom.h"
24 #include <wx/file.h>
25
26 static char* DicomTagsNames[] = {
27     "ID_File_Name",
28     "ID_SOP_Class_UID",
29     "ID_SOP_Instance_UID",
30     "ID_Study_Date",
31     "ID_Series_Date",
32     "ID_Acquisition_Date",
33     "ID_Image_Date",
34     "ID_Study_Time",
35     "ID_Series_Time",
36     "ID_Acquisition_Time",
37     "ID_Image_Time",
38     "ID_Modality",
39     "ID_Manufacturer",
40     "ID_Institution_Name",
41     "ID_Study_Description",
42     "ID_Series_Description",
43     "ID_Admitting_Diagnoses_Description",
44     "ID_Patient_Name",
45     "ID_Patient_ID",
46     "ID_Body_Part_Examined",
47     "ID_Scanning_Sequence",
48     "ID_Sequence_Variant",
49     "ID_Scan_Options",
50     "ID_MR_Acquisition_Type",
51     "ID_Sequence_Name",
52     "ID_Slice_Thickness",
53     "ID_Repetition_Time",
54     "ID_Echo_Time",
55     "ID_Inversion_Time",
56     "ID_Number_of_Averages",
57     "ID_Imaging_Frequency",
58     "ID_Imaged_Nucleus",
59     "ID_Echo_Number",
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",
65     "ID_Receiving_Coil",
66     "ID_Patient_Position",
67     "ID_Study_Instance_UID",
68     "ID_Series_Instance_UID",
69     "ID_Study_ID",
70     "ID_Series_Number",
71     "ID_Acquisition_Number",
72     "ID_Image_Number",
73     "ID_Patient_Orientation",
74     "ID_Image_Position",
75     "ID_Image_Position_Patient",
76     "ID_Image_Orientation",
77     "ID_Image_Orientation_Patient",
78     "ID_Location",
79     "ID_Frame_of_Reference_UID",
80     "ID_Slice_Location",
81     "ID_Image_Comments",
82     "ID_Pixel_Spacing",
83     "ID_Window_Center",
84     "ID_Window_Width",
85 };
86
87 // -------------------------------------------------------------------------
88 marDicom::marDicom( marParameters* p )
89     : marObject( p ),
90       _actualStudy( "" ), _actualSerie( "" ),
91       _volume( NULL )
92 {
93 }
94
95 // ----------------------------------------------------------------------------
96 wxArrayString marDicom_SelectDirs( wxString& root )
97 {
98     wxArrayString ret;
99     wxString it, tmp;
100
101     tmp = root.c_str( );
102     tmp += "/*";
103     it = wxFindFirstFile( tmp, wxDIR );
104     while( !it.IsEmpty( ) )
105     {
106       if( wxFileNameFromPath( it ).Cmp( "." ) != 0 && wxFileNameFromPath( it ).Cmp( ".." ) != 0 )
107         ret.Add( wxFileNameFromPath( it ) );
108
109       it = wxFindNextFile( );
110     } // fwhile
111
112     ret.Sort( );
113     return( ret );
114 }
115
116 // -------------------------------------------------------------------------
117 wxArrayString marDicom::getStudies( )
118 {
119     return( marDicom_SelectDirs( getParameters( )->getStringParam( marParameters::e_dicom_images_directory ) ) );
120 }
121
122 // -------------------------------------------------------------------------
123 wxArrayString marDicom::getSeries( )
124 {
125     wxString tmp;
126     
127     tmp = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
128     tmp += '/';
129     tmp += _actualStudy;
130     return( marDicom_SelectDirs( tmp ) );
131 }
132
133 // -------------------------------------------------------------------------
134 wxArrayString marDicom::getStudyData( )
135 {
136   int i;
137   char** res;
138   wxArrayString ret;
139   wxString cad;
140
141   if( _actualStudy != "" ) {
142     cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
143     cad += '/';
144     cad += _actualStudy;
145     
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++ )
149     {
150       ret.Add( copystring( res[ i ] ) );
151       delete res[ i ];
152     } // rof
153
154     ret[ID_File_Name] = _actualStudy;
155     delete res;
156
157   } // fi
158   return( ret );
159 }
160
161 // -------------------------------------------------------------------------
162 wxArrayString marDicom::getSerieData( )
163 {
164     int i;
165     char** res;
166     wxArrayString ret;
167     wxString cad;
168
169     if( _actualSerie != "" )
170     {
171       // WARNING : Use of LibIDO to get the information.
172       cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
173       cad += '/';
174       cad += _actualStudy;
175       cad += '/';
176       cad += _actualSerie;
177
178       res = IdStrGetSerieInfo( ( char* )cad.c_str( ), "*" );
179       for( i = 0; i < ID_dicom_tags_count; i++ )
180       {
181         ret.Add( copystring( res[ i ] ) );
182         delete res[ i ];  
183       } // rof
184
185       ret[ ID_File_Name ] = _actualSerie;
186       delete res;
187
188     } // fi
189     return( ret );
190 }
191
192 // -------------------------------------------------------------------------
193 wxArrayString marDicom_SelectFiles( wxString& root )
194 {
195     wxString it, tmp;
196     wxArrayString ret;
197
198     tmp = root.c_str( );
199     tmp += "/*";
200     it = wxFindFirstFile( tmp, wxFILE );
201     while( !it.IsEmpty( ) )
202     {
203       if( IdAcrIsAcrReadable( ( char* )it.c_str( ) ) ) // Check if DICOM
204         ret.Add( it );
205
206       it = wxFindNextFile( );
207     } // fwhile
208
209     ret.Sort( );
210     return( ret );
211 }
212
213 // -------------------------------------------------------------------------
214 void marDicom::loadActualSerie( )
215 {
216     wxString tmp;
217
218     reset( );
219
220     tmp = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
221     tmp += '/';
222     tmp += _actualStudy;
223     tmp += '/';
224     tmp += _actualSerie;
225     _imageFileNames = marDicom_SelectFiles( tmp );
226     loadVolume( true );
227 }
228
229 // -------------------------------------------------------------------------
230 bool marDicom_ReadAllImageData( wxString& fn,
231                                 PPIMAGE_USHORT& ima,
232                                 wxString& number,
233                                 int& dX, int& dY,
234                                 double& vX, double& vY, double& vZ )
235 {
236     char** tmp;
237 //    char*  stmp;
238 //    char*  end;
239
240     ima = ( PPIMAGE_USHORT )IdAcrReadFile( ( char* )fn.c_str( ), IMA_USHORT );
241     dX = IdImaDimX( ima );
242     dY = IdImaDimY( ima );
243
244     tmp = IdAcrInquireIRMInfo( ( char* )fn.c_str( ) );
245     number = tmp[ 16 ];
246 //    printf("%s\n", tmp[20]);
247     
248     //stmp = strtok( tmp[ 20 ], "\\" );
249     //vX = strtod( stmp, &end );
250     //stmp = strtok( NULL, "\\" );
251     //vY = strtod( stmp, &end );
252     
253     sscanf( tmp[ 20 ], "%lf\\%lf", &vX, &vY);
254     //vZ = strtod( tmp[ 6 ], &end );
255     vZ = strtod( tmp[ 6 ], NULL );
256     
257 //    printf("Vx = %lf  Vy = %lf  Vz = %lf\n", vX, vY, vZ);
258
259     //for( int i = 0; i < marDicom::ID_dicom_tags_count; i++ )
260     //delete tmp[ i ];
261     //delete tmp;
262     return( true );
263
264 }
265
266 // -------------------------------------------------------------------------
267 void marDicom::loadVolume( bool force )
268 {
269 /*
270 TODO: this function calls 11 534 336 to kVolume::setPixel !!!!
271 */
272     wxString number, tmp;
273     int size, i, j, k, dx, dy, dz, loc;
274     double vX, vY, vZ;
275     PPIMAGE_USHORT ima_t;
276
277     if( force || !_volume )
278     {
279       freeVolume( );
280       _imageNumbers.Clear( );
281       dz = _imageFileNames.GetCount( );
282
283       if( dz > 0 )
284       {
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 );
290
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 );
293         IdImaFree( ima_t );
294
295         // Loads real data
296         for( k = 0; k < dz; k++ )
297         {
298           tmp = _imageFileNames[ k ];
299           marDicom_ReadAllImageData( tmp, ima_t, number, dx, dy, vX, vY, vZ );
300           _imageNumbers.Add( number.c_str( ) );
301           
302           for( j = 0; j < dy; j++ )
303             for( i = 0; i < dx; i++ )
304               _volume->setPixel( ima_t[ j ][ i ], i, j, k );
305           
306           IdImaFree( ima_t );
307
308         } // rof
309       } // fi
310     } // fi
311 }
312
313 // -------------------------------------------------------------------------
314 void marDicom::freeVolume( )
315 {
316     if( _volume ) delete _volume;
317     _volume = NULL;
318 }
319
320 // -------------------------------------------------------------------------
321 bool marDicom::setActualStudy( wxString& s )
322 {
323     wxString cad;
324
325     if( _actualStudy != s )
326     {
327       cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
328       cad += '/';
329       cad += s;
330       if( wxDirExists( cad.c_str( ) ) )
331       {
332         _actualStudy = s;
333         _actualSerie = "";
334         return( true );
335       }
336       else
337         return( false );
338     }
339     else
340       return( true );
341 }
342
343 // -------------------------------------------------------------------------
344 bool marDicom::setActualSerie( wxString& s )
345 {
346     wxString cad;
347
348     if( _actualSerie != s  )
349     {
350       cad = getParameters( )->getStringParam( marParameters::e_dicom_images_directory );
351       cad += '/';
352       cad += _actualStudy;
353       cad += '/';
354       cad += s;
355       if( wxDirExists( cad.c_str( ) ) )
356       {
357         _actualSerie = s;
358         return( true );
359       }
360       else
361         return( false );
362     }
363     else
364       return( true );
365 }
366
367 // -------------------------------------------------------------------------
368 void marDicom::copyFrom( const marObject& from )
369 { // TODO
370 }
371
372 // -------------------------------------------------------------------------
373 bool marDicom::save( std::ofstream& os )
374 {
375     int s;
376
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 ) );
383     return( true );
384 }
385
386 // -------------------------------------------------------------------------
387 bool marDicom::load( std::ifstream& is )
388 {
389     int s;
390
391     reset( );
392
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 ) );
399     return( true );
400 }
401
402 // ----------------------------------------------------------------------------
403 wxArrayString marDicom::getRelationalArrayStudyData( )
404 {
405     wxArrayString ret;
406     wxArrayString inf = getStudyData( );
407     unsigned int i;
408
409     for( i = 0; i < inf.GetCount( ); i++ )
410     {
411         ret.Add( wxString( DicomTagsNames[ i ] ) );
412         ret.Add( wxString( inf[ i ] ) );
413
414     } // rof
415     return( ret );
416
417 }
418
419 // ----------------------------------------------------------------------------
420 wxArrayString marDicom::getRelationalArraySerieData( )
421 {
422     wxArrayString ret;
423     wxArrayString inf = getSerieData( );
424     unsigned int i;
425
426     for( i = 0; i < inf.GetCount( ); i++ ) {
427
428         ret.Add( wxString( DicomTagsNames[ i ] ) );
429         ret.Add( wxString( inf[ i ] ) );
430
431     } // rof
432     return( ret );
433
434 }
435
436 // eof - dicom.cxx