]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/kernel/marDicomBase.cpp
3db0a806c3cb3f696e57d3b0aa5ffa467b089d29
[creaMaracasVisu.git] / lib / maracasVisuLib / src / kernel / marDicomBase.cpp
1
2 #include "marDicomBase.h"
3
4 #include <vtkVolume16Reader.h> 
5 #include <vtkExtractVOI.h>
6 #include <vtkImageChangeInformation.h>
7 #include "vtkMetaImageWriter.h"
8
9
10
11 marFilesBase::marFilesBase( marParameters* p  ) :  marObject( p ) {
12         _volume=NULL;
13 }
14 // -------------------------------------------------------------------------
15 marFilesBase::~marFilesBase(){
16         freeVolume( );
17 }
18 // -------------------------------------------------------------------------
19 kVolume* marFilesBase::getVolume( ){
20         return( _volume );
21 }
22 // -------------------------------------------------------------------------
23 void marFilesBase::SetVolume( kVolume* volume  ){
24         _volume=volume;
25 }
26 // -------------------------------------------------------------------------
27 bool marFilesBase::volumeLoaded( ){
28         return( _volume != NULL );
29 }
30 // -------------------------------------------------------------------------
31 void marFilesBase::freeVolume( ){
32         if( _volume ) delete _volume;
33         _volume = NULL;
34 }
35
36
37 // -------------------------------------------------------------------------
38
39 std::vector < std::string*>  *marFilesBase::GetListImages()
40 {
41         return &_lstString;
42 }
43
44 // -------------------------------------------------------------------------
45 void marFilesBase::CleanListImages()
46 {
47         int i,size;
48         size=this->_lstString.size();
49         for(i=0;i<size;i++)
50         {
51                 delete _lstString[i];
52         }
53         _lstString.clear();
54 }
55
56
57 // -------------------------------------------------------------------------
58 void marFilesBase::ResetLstFileNotReaded()
59 {
60         _lstFileNotReaded.clear();
61 }
62
63
64 // -------------------------------------------------------------------------
65 std::string marFilesBase::GetMsgLstFile()
66 {
67         int i,sizeLst;
68 //EEDx44
69         std::string msgLstFile="";
70         sizeLst=_lstFileNotReaded.size();
71         for (i=0;i<sizeLst;i++)
72         {
73                 msgLstFile = msgLstFile + *(_lstFileNotReaded[i]) ;
74                 msgLstFile = msgLstFile + "\n";
75         }
76
77         return msgLstFile;
78 }
79
80
81
82
83
84
85 // -------------------------------------------------------------------------
86 // -------------------------------------------------------------------------
87 // -------------------------------------------------------------------------
88 marRAWFiles::marRAWFiles(marParameters* p)
89 :  marFilesBase(p)
90 {
91         _littreEndianBigEndian=0;
92 }
93 // -------------------------------------------------------------------------
94 marRAWFiles::~marRAWFiles()
95 {
96 }
97 // -------------------------------------------------------------------------
98 //EED 9 oct 2006
99 bool marRAWFiles::loadImage(int i) // virtual
100 {
101
102         int dim[3];
103
104         vtkImageData *vol = getVolume( )->castVtk();
105         void *p_vol=(void*)getVolume( )->castVtk()->GetScalarPointer(0,0,i);
106         vol->GetDimensions(dim);
107
108         int dataSize = sizeof(unsigned short)*dim[0]*dim[1];
109         bool ok=true;
110
111 /*
112         std::string *ss = _lstString[i];
113         FILE *ff=fopen( (char*)(ss->c_str())  , "r+" );
114         fread( (char*)p_vol , dataSize,1, ff);
115         fclose(ff);
116 */
117
118         std::string *ss = _lstString[i];
119         vtkVolume16Reader *reader = vtkVolume16Reader::New ();
120         reader->SetDataDimensions ( dim[0] , dim[1] );
121         if (_littreEndianBigEndian == 0) {
122             reader->SetDataByteOrderToLittleEndian ( );
123         } else {
124                 reader->SetDataByteOrderToBigEndian();
125         }
126         reader->SetFilePrefix ( (char*)(ss->c_str())  );
127         reader->SetFilePattern("%s");
128     reader->SetImageRange ( 0, 0 );
129     reader->SetDataSpacing ( 1, 1, 1 );
130     reader->Update ();
131         void *pp_vol=(void*)reader->GetOutput()->GetScalarPointer(0,0,0);
132         memcpy(p_vol,pp_vol,dataSize);
133         reader->Delete();
134
135
136 /*
137
138         std::string *ss = _lstString[i];
139         vtkGsmisReader *reader = vtkGsmisReader::New();
140         reader->SetFileName( (char*)(ss->c_str())  );
141         vtkImageData *ima=reader->GetOutput();
142         ima->Update();
143         void *pp_vol=(void*)ima->GetScalarPointer(0,0,0);
144         memcpy(p_vol,pp_vol,dataSize);
145 //      reader->Delete();
146 */
147
148
149
150 /*
151         int             ii;
152         char    tmp;
153         char    *pp = (char*)p_vol;
154         unsigned short   *valueA;
155         dataSize        = dim[0]*dim[1];
156         for (ii=0;ii<dataSize;ii++)
157         {
158                 tmp             = *pp;
159                 *pp             = *(pp+1);
160                 *(pp+1) = tmp;
161
162                 valueA=(unsigned short*)pp;
163                 *valueA=*valueA-32000;
164                 if (*valueA>=32000)
165                 {
166                         *valueA=0;
167                 } 
168
169                 pp              = pp+2;
170         }
171 */
172
173         if (ok==false) {
174                 _lstFileNotReaded.push_back( (char*)(ss->c_str()) );
175         }
176
177         return ok;
178 }
179 // -------------------------------------------------------------------------
180 //EED 9 oct 2006
181 void marRAWFiles::loadActualSerie(wxGauge* gauge )
182 {
183
184         // Read File List
185         FILE *ff;
186         ff = fopen( _lstString[0] -> c_str() , "r"); 
187         std::string directory(*(_lstString[1]) );
188         char tmp[255];
189
190         fscanf(ff,"%s",tmp);
191         fscanf(ff,"%s",tmp);
192         //int type =    atoi(tmp);  // 0 // JPRx
193
194         fscanf(ff,"%s",tmp);
195         fscanf(ff,"%s",tmp);
196         int wx  =       atoi(tmp);
197         fscanf(ff,"%s",tmp);
198         fscanf(ff,"%s",tmp);
199         int wy  =       atoi(tmp);
200
201         fscanf(ff,"%s",tmp);
202         fscanf(ff,"%s",tmp);
203         double spx      =       atof(tmp);
204         fscanf(ff,"%s",tmp);
205         fscanf(ff,"%s",tmp);
206         double spy      =       atof(tmp);
207         fscanf(ff,"%s",tmp);
208         fscanf(ff,"%s",tmp);
209         double spz      =       atof(tmp);
210
211         fscanf(ff,"%s",tmp);
212         fscanf(ff,"%s",tmp);
213         int LB  =       atoi(tmp);
214
215         this->SetLittreEndianBigEndian(LB);
216
217
218         int wz=0;
219         this->CleanListImages();
220         while ( !feof(ff) )
221         {
222                 fscanf(ff,"%s",tmp);
223                 std::string *tmpStr =  new std::string(tmp);
224                 (*tmpStr) = "/"+(*tmpStr);
225                 (*tmpStr) = (directory)+(*tmpStr);
226                 _lstString.push_back( tmpStr);
227                 wz++;
228         }
229
230         fclose(ff);
231
232
233         // Create Free Memory block image
234         freeVolume();
235
236         if (wz==0)
237         {
238                 wz=1;
239         }
240
241         kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
242         SetVolume( vol );
243
244
245         // Define Spacing
246         getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, spx );
247         getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, spy );
248         getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, spz );
249
250         // Define Intercept Slope
251         getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
252         getParameters( )->setDoubleParam( marParameters::e_RescaleSlope     , 1 );
253 }
254 // -------------------------------------------------------------------------
255 void marRAWFiles::SetLittreEndianBigEndian(int value)
256 {
257         _littreEndianBigEndian = value;
258 }
259 // -------------------------------------------------------------------------
260 int     marRAWFiles::GetLittreEndianBigEndian()
261 {
262         return _littreEndianBigEndian;
263 }
264 // -------------------------------------------------------------------------
265 void marRAWFiles::reset( )
266 {
267 }
268 // -------------------------------------------------------------------------
269 void marRAWFiles::copyFrom( const marObject& from )
270 {
271 }
272 // -------------------------------------------------------------------------
273 bool marRAWFiles::save( std::ofstream& os )
274 {
275         return true;
276 }
277 // -------------------------------------------------------------------------
278 bool marRAWFiles::load( std::ifstream& is )
279 {
280         return true;
281 }
282
283
284 //-------------------------------------------------------------------------
285 // -------------------------------------------------------------------------
286 // -------------------------------------------------------------------------
287 marRAW2AsciiFiles::marRAW2AsciiFiles(marParameters* p)
288 :  marRAWFiles(p)
289 {
290 }
291 // -------------------------------------------------------------------------
292 marRAW2AsciiFiles::~marRAW2AsciiFiles()
293 {
294 }
295 // -------------------------------------------------------------------------
296 bool marRAW2AsciiFiles::loadImage(int i) // virtual
297 {
298         return true;
299 }
300
301
302 // -------------------------------------------------------------------------
303 //EED 9 oct 2006
304 void marRAW2AsciiFiles::loadActualSerie(wxGauge* gauge )
305 {
306
307         // Read File List
308         FILE *ff;
309         ff = fopen( _lstString[0] -> c_str() , "r"); 
310         char tmp[255];
311
312         // type
313         fscanf(ff,"%s",tmp);
314         fscanf(ff,"%s",tmp);
315         //int type =    atoi(tmp);  // 300  // JPRx
316
317         //size x,y,z
318         fscanf(ff,"%s",tmp);
319         fscanf(ff,"%s",tmp);
320         int wx  =       atoi(tmp);
321         fscanf(ff,"%s",tmp);
322         fscanf(ff,"%s",tmp);
323         int wy  =       atoi(tmp);
324         fscanf(ff,"%s",tmp);
325         fscanf(ff,"%s",tmp);
326         int wz  =       atoi(tmp);
327
328         // spacing x,y,z
329         fscanf(ff,"%s",tmp);
330         fscanf(ff,"%s",tmp);
331         double spx      =       atof(tmp);
332         fscanf(ff,"%s",tmp);
333         fscanf(ff,"%s",tmp);
334         double spy      =       atof(tmp);
335         fscanf(ff,"%s",tmp);
336         fscanf(ff,"%s",tmp);
337         double spz      =       atof(tmp);
338
339         // Intercept Slope
340         fscanf(ff,"%s",tmp);
341         fscanf(ff,"%s",tmp);
342         double intercept        =       atof(tmp);
343         fscanf(ff,"%s",tmp);
344         fscanf(ff,"%s",tmp);
345         double slope            =       atof(tmp);
346
347
348         fscanf(ff,"%s",tmp);
349         std::string file(tmp);
350         std::string namefile( *_lstString[1] );  // directory
351         namefile=namefile+"/";                   // +  
352         namefile=namefile+file;                  // file
353
354         fclose(ff);
355
356
357         this->CleanListImages();
358
359         // Create Free Memory block image
360         freeVolume();
361
362         if (wz==0)
363         {
364                 wz=1;
365         }
366
367         kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
368         SetVolume( vol );
369
370
371         // Define Spacing
372         getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, spx );
373         getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, spy );
374         getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, spz );
375
376         // Define Intercept Slope
377         getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
378         getParameters( )->setDoubleParam( marParameters::e_RescaleSlope     , 1 );
379
380
381         void    *p_vol  = (void*)getVolume( )->castVtk()->GetScalarPointer(0,0,0);
382         short   *pp             = (short*)p_vol;
383
384         FILE *fff=fopen( (char*)namefile.c_str() , "r+" );
385         long int ii, dataSize   = wx*wy*wz;
386         float value;
387         for (ii=0;ii<dataSize;ii++)
388         {
389                 fscanf(fff,"%f ",&value);
390                 // Intercept Slope
391                 *pp     = (short)        ( value*slope + intercept );
392                 if (*pp<0)
393                 {
394                         *pp=0;
395                 } 
396                 pp++;
397         }
398         fclose(fff);
399
400
401 // Y- Flip
402 //      FlipY( getVolume( )->castVtk() );
403         
404 }
405
406
407 // -------------------------------------------------------------------------
408 // -------------------------------------------------------------------------
409 // -------------------------------------------------------------------------
410 marRAW2Files::marRAW2Files(marParameters* p)
411 :  marRAWFiles(p)
412 {
413 }
414 // -------------------------------------------------------------------------
415 marRAW2Files::~marRAW2Files()
416 {
417 }
418 // -------------------------------------------------------------------------
419 bool marRAW2Files::loadImage(int i) // virtual
420 {
421         return true;
422 }
423
424
425 // -------------------------------------------------------------------------
426 //EED 9 oct 2006
427 void marRAW2Files::loadActualSerie(wxGauge* gauge )
428 {
429
430         // Read File List
431         FILE *ff;
432         ff = fopen( _lstString[0] -> c_str() , "r"); 
433         char tmp[255];
434
435         // type
436         fscanf(ff,"%s",tmp);
437         fscanf(ff,"%s",tmp);
438         //int type =    atoi(tmp);  // 200  // JPRx
439
440         //size x,y,z
441         fscanf(ff,"%s",tmp);
442         fscanf(ff,"%s",tmp);
443         int wx  =       atoi(tmp);
444         fscanf(ff,"%s",tmp);
445         fscanf(ff,"%s",tmp);
446         int wy  =       atoi(tmp);
447 //      fscanf(ff,"%s",tmp);
448 //      fscanf(ff,"%s",tmp);
449 //      int wz  =       atoi(tmp);
450
451         fscanf(ff,"%s",tmp);
452         fscanf(ff,"%s",tmp);
453         int wz1 =       atoi(tmp);
454         fscanf(ff,"%s",tmp);
455         fscanf(ff,"%s",tmp);
456         int wz2 =       atoi(tmp);
457
458         int wz = wz2-wz1;
459         
460         // spacing x,y,z
461         fscanf(ff,"%s",tmp);
462         fscanf(ff,"%s",tmp);
463         double spx      =       atof(tmp);
464         fscanf(ff,"%s",tmp);
465         fscanf(ff,"%s",tmp);
466         double spy      =       atof(tmp);
467         fscanf(ff,"%s",tmp);
468         fscanf(ff,"%s",tmp);
469         double spz      =       atof(tmp);
470
471         // Intercept Slope
472         fscanf(ff,"%s",tmp);
473         fscanf(ff,"%s",tmp);
474         double intercept        =       atof(tmp);
475         fscanf(ff,"%s",tmp);
476         fscanf(ff,"%s",tmp);
477         double slope            =       atof(tmp);
478
479         // L/B
480         fscanf(ff,"%s",tmp);
481         fscanf(ff,"%s",tmp);
482         int littleBig   =       atoi(tmp);
483
484         // bits
485         fscanf(ff,"%s",tmp);
486         fscanf(ff,"%s",tmp);
487         int bits        =       atoi(tmp);
488
489         if ((bits!=8) && (bits!=16)){
490                 bits=16;
491         }
492         
493         fscanf(ff,"%s",tmp);
494         std::string file(tmp);
495         std::string namefile( *_lstString[1] );  // directory
496         namefile=namefile+"/";                   // +  
497         namefile=namefile+file;                  // file
498
499         fclose(ff);
500
501
502         this->CleanListImages();
503
504         // Create Free Memory block image
505         freeVolume();
506
507         if (wz==0)
508         {
509                 wz=1;
510         }
511
512
513         kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
514         SetVolume( vol );
515
516
517         // Define Spacing
518         getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, spx );
519         getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, spy );
520         getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, spz );
521
522         // Define Intercept Slope
523         getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
524         getParameters( )->setDoubleParam( marParameters::e_RescaleSlope     , 1 );
525
526         void *p_vol=(void*)getVolume( )->castVtk()->GetScalarPointer(0,0,0);
527         FILE *fff=fopen( (char*)namefile.c_str() , "r+" );
528     if (bits==16)
529     {
530                 fseek( fff , wx*wy*wz1*2 , SEEK_CUR );
531         fread( (char*)p_vol , wx*wy*wz*2,1, fff);
532             fclose(fff);
533
534
535         // littleBigEndien
536                 if (littleBig==1){
537                         long int                ii;
538                         char                    tmpB;
539                         char                    *pp = (char*)p_vol;
540                         unsigned short   *valueA;
541                         long int dataSize       = wx*wy*wz;
542                         for (ii=0;ii<dataSize;ii++)
543                         {
544                                 tmpB            = *pp;
545                                 *pp                     = *(pp+1);
546                                 *(pp+1)         = tmpB;
547                                 
548                                 valueA= ((unsigned short*)pp);
549                                 if (*valueA>=32000)
550                                 {
551                                         *valueA=0;
552                                 } 
553                                 pp              = pp+2;
554
555                         }
556                 }
557
558                 unsigned short                  *pp = (unsigned short*)p_vol;
559                 int tmpInt;
560                 int ii, dataSize        = wx*wy*wz;
561                 for (ii=0;ii<dataSize;ii++)
562                 {
563                         tmpInt = (int) (*pp);
564                         tmpInt  = (int)  ( tmpInt*slope+intercept );
565                         if (tmpInt<0)
566                         {
567                                 tmpInt=0;
568                         } 
569                         if (tmpInt>65535) 
570                         {
571                                 tmpInt=65535;
572                         }
573                         (*pp) = (unsigned short)tmpInt;
574                         pp              = pp+1;
575                 }
576
577     } else {
578         unsigned short *pShort=(unsigned short*)p_vol;
579         int i8bitImage,size8bitImage = wx*wy*wz;
580         unsigned char *pChar=(unsigned char*)malloc( size8bitImage );
581                 fseek( fff , wx*wy*wz1 , SEEK_CUR );
582         fread( (unsigned char*)pChar , size8bitImage,1, fff);
583             fclose(fff);
584                 double tmpDouble;
585         for ( i8bitImage=0 ; i8bitImage < size8bitImage ; i8bitImage++ )
586         {
587                         
588                         tmpDouble=(float)pChar[ i8bitImage ];
589                         tmpDouble=tmpDouble*slope+intercept;
590                         if (tmpDouble<0)
591                         {
592                                 tmpDouble=0;
593                         } 
594                 pShort[ i8bitImage ] = (unsigned short)tmpDouble;
595         }
596         free (pChar);
597     }
598
599 // Y- Flip
600         FlipY( getVolume( )->castVtk() );
601         
602 }
603
604
605
606
607 // -------------------------------------------------------------------------
608 void marRAW2Files::FlipY(vtkImageData *imagedata)
609 {
610         vtkImageData *vtkimagedata = imagedata;
611         vtkimagedata->Update();
612         int dim[3];
613         vtkimagedata->GetDimensions(dim);
614         unsigned short tmp;
615         unsigned short *pp=(unsigned short*)vtkimagedata->GetScalarPointer(0,0,0);
616         int xx,yy,zz;
617         int sizeX  = dim[0];
618         int sizeY  = dim[1];
619         int sizeY2 = sizeY/2;
620         int sizeZ  = dim[2];
621         long int deltaZ;
622         long int deltaA;
623         long int deltaB;
624
625         for ( zz=0 ; zz<sizeZ ; zz++ )
626         {
627                 deltaZ=zz*sizeX*sizeY;
628                 for ( xx=0 ; xx<sizeX ; xx++ )
629                 {
630                 for ( yy=0 ; yy<sizeY2 ; yy++ )
631                         {
632                                 deltaA          = xx + yy*sizeX + deltaZ;
633                                 deltaB          = xx + (sizeY-yy-1)*sizeX + deltaZ;
634                                 tmp                     = pp[deltaA ];
635                                 pp[deltaA]      = pp[deltaB ];
636                                 pp[deltaB]      = tmp;
637                                 }
638                         }
639                 }
640    vtkimagedata->Update();
641
642 }
643 // -------------------------------------------------------------------------
644
645 // EED 23 Janvier 2007
646 void marRAW2Files::saveVolume( std::string directory, std::string name, vtkImageData *imagedata,int voi[6], double slope, double intercept) // virtual
647 {
648
649         std::string filename             = directory+"/"+name;
650
651                 // Crop
652         vtkExtractVOI *crop = vtkExtractVOI::New();
653         crop->SetInput( imagedata );
654         crop->SetVOI( voi );
655         crop->Update();
656
657         int dim[3];
658         int ext[6];
659         double spc[3];
660         crop->GetOutput()->GetDimensions(dim);
661         crop->GetOutput()->GetSpacing(spc);
662         crop->GetOutput()->GetExtent(ext);
663
664
665         vtkImageChangeInformation* change = vtkImageChangeInformation::New();
666         change->SetInput( crop->GetOutput() );
667
668
669 //      change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
670    
671
672         change->SetOutputSpacing ( spc[0] , spc[1] , spc[2] );
673         change->Update();    //important
674
675 //   change->SetExtentTranslation( ext[0], ext[2], ext[4] );
676 //      change->Update();
677
678
679         // write mdh file
680    std::string nameMW            = filename+".mhd";
681
682         vtkMetaImageWriter *writer = vtkMetaImageWriter::New( );
683         writer->SetInput( change->GetOutput() );
684         writer->SetFileName( nameMW.c_str() );
685         writer->SetFileDimensionality( 3 );
686         writer->Write( );
687
688                 // write maracas file
689         std::string nameM=filename+".maracas";
690
691
692         FILE *ff;
693         ff=fopen(nameM.c_str(),"w");
694                 fprintf(ff,"MaracasType 200\n" );
695                 fprintf(ff,"sizeX %d\n",dim[0] );
696                 fprintf(ff,"sizeY %d\n",dim[1] );
697                 fprintf(ff,"sizeZ1 %d\n",0 );
698                 fprintf(ff,"sizeZ2 %d\n",dim[2] );
699                 fprintf(ff,"spcX %f\n",spc[0]  );
700                 fprintf(ff,"spcY %f\n",spc[1]  );
701                 fprintf(ff,"spcZ %f\n",spc[2]  );
702                 fprintf(ff,"intercept %f\n",intercept );
703                 fprintf(ff,"slope %f\n",slope );
704                 fprintf(ff,"LB %d\n",0 );
705                 fprintf(ff,"bits %d\n",16 );
706                 fprintf(ff,"%s.raw\n", name.c_str() );
707         fclose(ff);
708
709         writer  -> Delete();
710         change  -> Delete();
711         crop    -> Delete();
712 }
713