2 #include "marDicomBase.h"
4 #include <vtkVolume16Reader.h>
5 #include <vtkExtractVOI.h>
6 #include <vtkImageChangeInformation.h>
7 #include "vtkMetaImageWriter.h"
11 marFilesBase::marFilesBase( marParameters* p ) : marObject( p ) {
14 // -------------------------------------------------------------------------
15 marFilesBase::~marFilesBase(){
18 // -------------------------------------------------------------------------
19 kVolume* marFilesBase::getVolume( ){
22 // -------------------------------------------------------------------------
23 void marFilesBase::SetVolume( kVolume* volume ){
26 // -------------------------------------------------------------------------
27 bool marFilesBase::volumeLoaded( ){
28 return( _volume != NULL );
30 // -------------------------------------------------------------------------
31 void marFilesBase::freeVolume( ){
32 if( _volume ) delete _volume;
37 // -------------------------------------------------------------------------
39 std::vector < std::string*> *marFilesBase::GetListImages()
44 // -------------------------------------------------------------------------
45 void marFilesBase::CleanListImages()
48 size=this->_lstString.size();
57 // -------------------------------------------------------------------------
58 void marFilesBase::ResetLstFileNotReaded()
60 _lstFileNotReaded.clear();
64 // -------------------------------------------------------------------------
65 std::string marFilesBase::GetMsgLstFile()
69 std::string msgLstFile="";
70 sizeLst=_lstFileNotReaded.size();
71 for (i=0;i<sizeLst;i++)
73 msgLstFile = msgLstFile + *(_lstFileNotReaded[i]) ;
74 msgLstFile = msgLstFile + "\n";
85 // -------------------------------------------------------------------------
86 // -------------------------------------------------------------------------
87 // -------------------------------------------------------------------------
88 marRAWFiles::marRAWFiles(marParameters* p)
91 _littreEndianBigEndian=0;
93 // -------------------------------------------------------------------------
94 marRAWFiles::~marRAWFiles()
97 // -------------------------------------------------------------------------
99 bool marRAWFiles::loadImage(int i) // virtual
104 vtkImageData *vol = getVolume( )->castVtk();
105 void *p_vol=(void*)getVolume( )->castVtk()->GetScalarPointer(0,0,i);
106 vol->GetDimensions(dim);
108 int dataSize = sizeof(unsigned short)*dim[0]*dim[1];
112 std::string *ss = _lstString[i];
113 FILE *ff=fopen( (char*)(ss->c_str()) , "r+" );
114 fread( (char*)p_vol , dataSize,1, ff);
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 ( );
124 reader->SetDataByteOrderToBigEndian();
126 reader->SetFilePrefix ( (char*)(ss->c_str()) );
127 reader->SetFilePattern("%s");
128 reader->SetImageRange ( 0, 0 );
129 reader->SetDataSpacing ( 1, 1, 1 );
131 void *pp_vol=(void*)reader->GetOutput()->GetScalarPointer(0,0,0);
132 memcpy(p_vol,pp_vol,dataSize);
138 std::string *ss = _lstString[i];
139 vtkGsmisReader *reader = vtkGsmisReader::New();
140 reader->SetFileName( (char*)(ss->c_str()) );
141 vtkImageData *ima=reader->GetOutput();
143 void *pp_vol=(void*)ima->GetScalarPointer(0,0,0);
144 memcpy(p_vol,pp_vol,dataSize);
153 char *pp = (char*)p_vol;
154 unsigned short *valueA;
155 dataSize = dim[0]*dim[1];
156 for (ii=0;ii<dataSize;ii++)
162 valueA=(unsigned short*)pp;
163 *valueA=*valueA-32000;
174 _lstFileNotReaded.push_back( (char*)(ss->c_str()) );
179 // -------------------------------------------------------------------------
181 void marRAWFiles::loadActualSerie(wxGauge* gauge )
186 ff = fopen( _lstString[0] -> c_str() , "r");
187 std::string directory(*(_lstString[1]) );
192 //int type = atoi(tmp); // 0 // JPRx
203 double spx = atof(tmp);
206 double spy = atof(tmp);
209 double spz = atof(tmp);
215 this->SetLittreEndianBigEndian(LB);
219 this->CleanListImages();
223 std::string *tmpStr = new std::string(tmp);
224 (*tmpStr) = "/"+(*tmpStr);
225 (*tmpStr) = (directory)+(*tmpStr);
226 _lstString.push_back( tmpStr);
233 // Create Free Memory block image
241 kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
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 );
250 // Define Intercept Slope
251 getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
252 getParameters( )->setDoubleParam( marParameters::e_RescaleSlope , 1 );
254 // -------------------------------------------------------------------------
255 void marRAWFiles::SetLittreEndianBigEndian(int value)
257 _littreEndianBigEndian = value;
259 // -------------------------------------------------------------------------
260 int marRAWFiles::GetLittreEndianBigEndian()
262 return _littreEndianBigEndian;
264 // -------------------------------------------------------------------------
265 void marRAWFiles::reset( )
268 // -------------------------------------------------------------------------
269 void marRAWFiles::copyFrom( const marObject& from )
272 // -------------------------------------------------------------------------
273 bool marRAWFiles::save( std::ofstream& os )
277 // -------------------------------------------------------------------------
278 bool marRAWFiles::load( std::ifstream& is )
284 //-------------------------------------------------------------------------
285 // -------------------------------------------------------------------------
286 // -------------------------------------------------------------------------
287 marRAW2AsciiFiles::marRAW2AsciiFiles(marParameters* p)
291 // -------------------------------------------------------------------------
292 marRAW2AsciiFiles::~marRAW2AsciiFiles()
295 // -------------------------------------------------------------------------
296 bool marRAW2AsciiFiles::loadImage(int i) // virtual
302 // -------------------------------------------------------------------------
304 void marRAW2AsciiFiles::loadActualSerie(wxGauge* gauge )
309 ff = fopen( _lstString[0] -> c_str() , "r");
315 //int type = atoi(tmp); // 300 // JPRx
331 double spx = atof(tmp);
334 double spy = atof(tmp);
337 double spz = atof(tmp);
342 double intercept = atof(tmp);
345 double slope = atof(tmp);
349 std::string file(tmp);
350 std::string namefile( *_lstString[1] ); // directory
351 namefile=namefile+"/"; // +
352 namefile=namefile+file; // file
357 this->CleanListImages();
359 // Create Free Memory block image
367 kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
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 );
376 // Define Intercept Slope
377 getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
378 getParameters( )->setDoubleParam( marParameters::e_RescaleSlope , 1 );
381 void *p_vol = (void*)getVolume( )->castVtk()->GetScalarPointer(0,0,0);
382 short *pp = (short*)p_vol;
384 FILE *fff=fopen( (char*)namefile.c_str() , "r+" );
385 long int ii, dataSize = wx*wy*wz;
387 for (ii=0;ii<dataSize;ii++)
389 fscanf(fff,"%f ",&value);
391 *pp = (short) ( value*slope + intercept );
402 // FlipY( getVolume( )->castVtk() );
407 // -------------------------------------------------------------------------
408 // -------------------------------------------------------------------------
409 // -------------------------------------------------------------------------
410 marRAW2Files::marRAW2Files(marParameters* p)
414 // -------------------------------------------------------------------------
415 marRAW2Files::~marRAW2Files()
418 // -------------------------------------------------------------------------
419 bool marRAW2Files::loadImage(int i) // virtual
425 // -------------------------------------------------------------------------
427 void marRAW2Files::loadActualSerie(wxGauge* gauge )
432 ff = fopen( _lstString[0] -> c_str() , "r");
438 //int type = atoi(tmp); // 200 // JPRx
447 // fscanf(ff,"%s",tmp);
448 // fscanf(ff,"%s",tmp);
449 // int wz = atoi(tmp);
463 double spx = atof(tmp);
466 double spy = atof(tmp);
469 double spz = atof(tmp);
474 double intercept = atof(tmp);
477 double slope = atof(tmp);
482 int littleBig = atoi(tmp);
487 int bits = atoi(tmp);
489 if ((bits!=8) && (bits!=16)){
494 std::string file(tmp);
495 std::string namefile( *_lstString[1] ); // directory
496 namefile=namefile+"/"; // +
497 namefile=namefile+file; // file
502 this->CleanListImages();
504 // Create Free Memory block image
513 kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
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 );
522 // Define Intercept Slope
523 getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
524 getParameters( )->setDoubleParam( marParameters::e_RescaleSlope , 1 );
526 void *p_vol=(void*)getVolume( )->castVtk()->GetScalarPointer(0,0,0);
527 FILE *fff=fopen( (char*)namefile.c_str() , "r+" );
530 fseek( fff , wx*wy*wz1*2 , SEEK_CUR );
531 fread( (char*)p_vol , wx*wy*wz*2,1, fff);
539 char *pp = (char*)p_vol;
540 unsigned short *valueA;
541 long int dataSize = wx*wy*wz;
542 for (ii=0;ii<dataSize;ii++)
548 valueA= ((unsigned short*)pp);
558 unsigned short *pp = (unsigned short*)p_vol;
560 int ii, dataSize = wx*wy*wz;
561 for (ii=0;ii<dataSize;ii++)
563 tmpInt = (int) (*pp);
564 tmpInt = (int) ( tmpInt*slope+intercept );
573 (*pp) = (unsigned short)tmpInt;
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);
585 for ( i8bitImage=0 ; i8bitImage < size8bitImage ; i8bitImage++ )
588 tmpDouble=(float)pChar[ i8bitImage ];
589 tmpDouble=tmpDouble*slope+intercept;
594 pShort[ i8bitImage ] = (unsigned short)tmpDouble;
600 FlipY( getVolume( )->castVtk() );
607 // -------------------------------------------------------------------------
608 void marRAW2Files::FlipY(vtkImageData *imagedata)
610 vtkImageData *vtkimagedata = imagedata;
611 vtkimagedata->Update();
613 vtkimagedata->GetDimensions(dim);
615 unsigned short *pp=(unsigned short*)vtkimagedata->GetScalarPointer(0,0,0);
619 int sizeY2 = sizeY/2;
625 for ( zz=0 ; zz<sizeZ ; zz++ )
627 deltaZ=zz*sizeX*sizeY;
628 for ( xx=0 ; xx<sizeX ; xx++ )
630 for ( yy=0 ; yy<sizeY2 ; yy++ )
632 deltaA = xx + yy*sizeX + deltaZ;
633 deltaB = xx + (sizeY-yy-1)*sizeX + deltaZ;
635 pp[deltaA] = pp[deltaB ];
640 vtkimagedata->Update();
643 // -------------------------------------------------------------------------
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
649 std::string filename = directory+"/"+name;
652 vtkExtractVOI *crop = vtkExtractVOI::New();
653 crop->SetInput( imagedata );
660 crop->GetOutput()->GetDimensions(dim);
661 crop->GetOutput()->GetSpacing(spc);
662 crop->GetOutput()->GetExtent(ext);
665 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
666 change->SetInput( crop->GetOutput() );
669 // change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
672 change->SetOutputSpacing ( spc[0] , spc[1] , spc[2] );
673 change->Update(); //important
675 // change->SetExtentTranslation( ext[0], ext[2], ext[4] );
680 std::string nameMW = filename+".mhd";
682 vtkMetaImageWriter *writer = vtkMetaImageWriter::New( );
683 writer->SetInput( change->GetOutput() );
684 writer->SetFileName( nameMW.c_str() );
685 writer->SetFileDimensionality( 3 );
688 // write maracas file
689 std::string nameM=filename+".maracas";
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() );