1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
27 #include "marDicomBase.h"
29 #include <vtkVolume16Reader.h>
30 #include <vtkExtractVOI.h>
31 #include <vtkImageChangeInformation.h>
32 #include "vtkMetaImageWriter.h"
36 marFilesBase::marFilesBase( marParameters* p ) : marObject( p ) {
39 // -------------------------------------------------------------------------
40 marFilesBase::~marFilesBase(){
43 // -------------------------------------------------------------------------
44 kVolume* marFilesBase::getVolume( ){
47 // -------------------------------------------------------------------------
48 void marFilesBase::SetVolume( kVolume* volume ){
51 // -------------------------------------------------------------------------
52 bool marFilesBase::volumeLoaded( ){
53 return( _volume != NULL );
55 // -------------------------------------------------------------------------
56 void marFilesBase::freeVolume( ){
57 if( _volume ) delete _volume;
62 // -------------------------------------------------------------------------
64 std::vector < std::string*> *marFilesBase::GetListImages()
69 // -------------------------------------------------------------------------
70 void marFilesBase::CleanListImages()
73 size=this->_lstString.size();
82 // -------------------------------------------------------------------------
83 void marFilesBase::ResetLstFileNotReaded()
85 _lstFileNotReaded.clear();
89 // -------------------------------------------------------------------------
90 std::string marFilesBase::GetMsgLstFile()
94 std::string msgLstFile="";
95 sizeLst=_lstFileNotReaded.size();
96 for (i=0;i<sizeLst;i++)
98 msgLstFile = msgLstFile + *(_lstFileNotReaded[i]) ;
99 msgLstFile = msgLstFile + "\n";
110 // -------------------------------------------------------------------------
111 // -------------------------------------------------------------------------
112 // -------------------------------------------------------------------------
113 marRAWFiles::marRAWFiles(marParameters* p)
116 _littreEndianBigEndian=0;
118 // -------------------------------------------------------------------------
119 marRAWFiles::~marRAWFiles()
122 // -------------------------------------------------------------------------
124 bool marRAWFiles::loadImage(int i) // virtual
129 vtkImageData *vol = getVolume( )->castVtk();
130 void *p_vol=(void*)getVolume( )->castVtk()->GetScalarPointer(0,0,i);
131 vol->GetDimensions(dim);
133 int dataSize = sizeof(unsigned short)*dim[0]*dim[1];
137 std::string *ss = _lstString[i];
138 FILE *ff=fopen( (char*)(ss->c_str()) , "r+" );
139 fread( (char*)p_vol , dataSize,1, ff);
143 std::string *ss = _lstString[i];
144 vtkVolume16Reader *reader = vtkVolume16Reader::New ();
145 reader->SetDataDimensions ( dim[0] , dim[1] );
146 if (_littreEndianBigEndian == 0) {
147 reader->SetDataByteOrderToLittleEndian ( );
149 reader->SetDataByteOrderToBigEndian();
151 reader->SetFilePrefix ( (char*)(ss->c_str()) );
152 reader->SetFilePattern("%s");
153 reader->SetImageRange ( 0, 0 );
154 reader->SetDataSpacing ( 1, 1, 1 );
156 void *pp_vol=(void*)reader->GetOutput()->GetScalarPointer(0,0,0);
157 memcpy(p_vol,pp_vol,dataSize);
163 std::string *ss = _lstString[i];
164 vtkGsmisReader *reader = vtkGsmisReader::New();
165 reader->SetFileName( (char*)(ss->c_str()) );
166 vtkImageData *ima=reader->GetOutput();
168 void *pp_vol=(void*)ima->GetScalarPointer(0,0,0);
169 memcpy(p_vol,pp_vol,dataSize);
178 char *pp = (char*)p_vol;
179 unsigned short *valueA;
180 dataSize = dim[0]*dim[1];
181 for (ii=0;ii<dataSize;ii++)
187 valueA=(unsigned short*)pp;
188 *valueA=*valueA-32000;
199 _lstFileNotReaded.push_back( (char*)(ss->c_str()) );
204 // -------------------------------------------------------------------------
206 void marRAWFiles::loadActualSerie(wxGauge* gauge )
211 ff = fopen( _lstString[0] -> c_str() , "r");
212 std::string directory(*(_lstString[1]) );
217 //int type = atoi(tmp); // 0 // JPRx
228 double spx = atof(tmp);
231 double spy = atof(tmp);
234 double spz = atof(tmp);
240 this->SetLittreEndianBigEndian(LB);
244 this->CleanListImages();
248 std::string *tmpStr = new std::string(tmp);
249 (*tmpStr) = "/"+(*tmpStr);
250 (*tmpStr) = (directory)+(*tmpStr);
251 _lstString.push_back( tmpStr);
258 // Create Free Memory block image
266 kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
271 getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, spx );
272 getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, spy );
273 getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, spz );
275 // Define Intercept Slope
276 getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
277 getParameters( )->setDoubleParam( marParameters::e_RescaleSlope , 1 );
279 // -------------------------------------------------------------------------
280 void marRAWFiles::SetLittreEndianBigEndian(int value)
282 _littreEndianBigEndian = value;
284 // -------------------------------------------------------------------------
285 int marRAWFiles::GetLittreEndianBigEndian()
287 return _littreEndianBigEndian;
289 // -------------------------------------------------------------------------
290 void marRAWFiles::reset( )
293 // -------------------------------------------------------------------------
294 void marRAWFiles::copyFrom( const marObject& from )
297 // -------------------------------------------------------------------------
298 bool marRAWFiles::save( std::ofstream& os )
302 // -------------------------------------------------------------------------
303 bool marRAWFiles::load( std::ifstream& is )
309 //-------------------------------------------------------------------------
310 // -------------------------------------------------------------------------
311 // -------------------------------------------------------------------------
312 marRAW2AsciiFiles::marRAW2AsciiFiles(marParameters* p)
316 // -------------------------------------------------------------------------
317 marRAW2AsciiFiles::~marRAW2AsciiFiles()
320 // -------------------------------------------------------------------------
321 bool marRAW2AsciiFiles::loadImage(int i) // virtual
327 // -------------------------------------------------------------------------
329 void marRAW2AsciiFiles::loadActualSerie(wxGauge* gauge )
334 ff = fopen( _lstString[0] -> c_str() , "r");
340 //int type = atoi(tmp); // 300 // JPRx
356 double spx = atof(tmp);
359 double spy = atof(tmp);
362 double spz = atof(tmp);
367 double intercept = atof(tmp);
370 double slope = atof(tmp);
374 std::string file(tmp);
375 std::string namefile( *_lstString[1] ); // directory
376 namefile=namefile+"/"; // +
377 namefile=namefile+file; // file
382 this->CleanListImages();
384 // Create Free Memory block image
392 kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
397 getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, spx );
398 getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, spy );
399 getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, spz );
401 // Define Intercept Slope
402 getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
403 getParameters( )->setDoubleParam( marParameters::e_RescaleSlope , 1 );
406 void *p_vol = (void*)getVolume( )->castVtk()->GetScalarPointer(0,0,0);
407 short *pp = (short*)p_vol;
409 FILE *fff=fopen( (char*)namefile.c_str() , "r+" );
410 long int ii, dataSize = wx*wy*wz;
412 for (ii=0;ii<dataSize;ii++)
414 fscanf(fff,"%f ",&value);
416 *pp = (short) ( value*slope + intercept );
427 // FlipY( getVolume( )->castVtk() );
432 // -------------------------------------------------------------------------
433 // -------------------------------------------------------------------------
434 // -------------------------------------------------------------------------
435 marRAW2Files::marRAW2Files(marParameters* p)
439 // -------------------------------------------------------------------------
440 marRAW2Files::~marRAW2Files()
443 // -------------------------------------------------------------------------
444 bool marRAW2Files::loadImage(int i) // virtual
450 // -------------------------------------------------------------------------
452 void marRAW2Files::loadActualSerie(wxGauge* gauge )
457 ff = fopen( _lstString[0] -> c_str() , "r");
463 //int type = atoi(tmp); // 200 // JPRx
472 // fscanf(ff,"%s",tmp);
473 // fscanf(ff,"%s",tmp);
474 // int wz = atoi(tmp);
488 double spx = atof(tmp);
491 double spy = atof(tmp);
494 double spz = atof(tmp);
499 double intercept = atof(tmp);
502 double slope = atof(tmp);
507 int littleBig = atoi(tmp);
512 int bits = atoi(tmp);
514 if ((bits!=8) && (bits!=16)){
519 std::string file(tmp);
520 std::string namefile( *_lstString[1] ); // directory
521 namefile=namefile+"/"; // +
522 namefile=namefile+file; // file
527 this->CleanListImages();
529 // Create Free Memory block image
538 kVolume *vol =new kVolume( kVolume::USHORT, wx, wy, wz,spx, spy, spz, malloc(sizeof(unsigned short)*wx*wy*wz ) );
543 getParameters( )->setDoubleParam( marParameters::e_voxel_x_dimension, spx );
544 getParameters( )->setDoubleParam( marParameters::e_voxel_y_dimension, spy );
545 getParameters( )->setDoubleParam( marParameters::e_voxel_z_dimension, spz );
547 // Define Intercept Slope
548 getParameters( )->setDoubleParam( marParameters::e_RescaleIntercept , 0 );
549 getParameters( )->setDoubleParam( marParameters::e_RescaleSlope , 1 );
551 void *p_vol=(void*)getVolume( )->castVtk()->GetScalarPointer(0,0,0);
552 FILE *fff=fopen( (char*)namefile.c_str() , "r+" );
555 fseek( fff , wx*wy*wz1*2 , SEEK_CUR );
556 fread( (char*)p_vol , wx*wy*wz*2,1, fff);
564 char *pp = (char*)p_vol;
565 unsigned short *valueA;
566 long int dataSize = wx*wy*wz;
567 for (ii=0;ii<dataSize;ii++)
573 valueA= ((unsigned short*)pp);
583 unsigned short *pp = (unsigned short*)p_vol;
585 int ii, dataSize = wx*wy*wz;
586 for (ii=0;ii<dataSize;ii++)
588 tmpInt = (int) (*pp);
589 tmpInt = (int) ( tmpInt*slope+intercept );
598 (*pp) = (unsigned short)tmpInt;
603 unsigned short *pShort=(unsigned short*)p_vol;
604 int i8bitImage,size8bitImage = wx*wy*wz;
605 unsigned char *pChar=(unsigned char*)malloc( size8bitImage );
606 fseek( fff , wx*wy*wz1 , SEEK_CUR );
607 fread( (unsigned char*)pChar , size8bitImage,1, fff);
610 for ( i8bitImage=0 ; i8bitImage < size8bitImage ; i8bitImage++ )
613 tmpDouble=(float)pChar[ i8bitImage ];
614 tmpDouble=tmpDouble*slope+intercept;
619 pShort[ i8bitImage ] = (unsigned short)tmpDouble;
625 FlipY( getVolume( )->castVtk() );
632 // -------------------------------------------------------------------------
633 void marRAW2Files::FlipY(vtkImageData *imagedata)
635 vtkImageData *vtkimagedata = imagedata;
636 vtkimagedata->Update();
638 vtkimagedata->GetDimensions(dim);
640 unsigned short *pp=(unsigned short*)vtkimagedata->GetScalarPointer(0,0,0);
644 int sizeY2 = sizeY/2;
650 for ( zz=0 ; zz<sizeZ ; zz++ )
652 deltaZ=zz*sizeX*sizeY;
653 for ( xx=0 ; xx<sizeX ; xx++ )
655 for ( yy=0 ; yy<sizeY2 ; yy++ )
657 deltaA = xx + yy*sizeX + deltaZ;
658 deltaB = xx + (sizeY-yy-1)*sizeX + deltaZ;
660 pp[deltaA] = pp[deltaB ];
665 vtkimagedata->Update();
668 // -------------------------------------------------------------------------
670 // EED 23 Janvier 2007
671 void marRAW2Files::saveVolume( std::string directory, std::string name, vtkImageData *imagedata,int voi[6], double slope, double intercept) // virtual
674 std::string filename = directory+"/"+name;
677 vtkExtractVOI *crop = vtkExtractVOI::New();
678 crop->SetInput( imagedata );
685 crop->GetOutput()->GetDimensions(dim);
686 crop->GetOutput()->GetSpacing(spc);
687 crop->GetOutput()->GetExtent(ext);
690 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
691 change->SetInput( crop->GetOutput() );
694 // change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
697 change->SetOutputSpacing ( spc[0] , spc[1] , spc[2] );
698 change->Update(); //important
700 // change->SetExtentTranslation( ext[0], ext[2], ext[4] );
705 std::string nameMW = filename+".mhd";
707 vtkMetaImageWriter *writer = vtkMetaImageWriter::New( );
708 writer->SetInput( change->GetOutput() );
709 writer->SetFileName( nameMW.c_str() );
710 writer->SetFileDimensionality( 3 );
713 // write maracas file
714 std::string nameM=filename+".maracas";
718 ff=fopen(nameM.c_str(),"w");
719 fprintf(ff,"MaracasType 200\n" );
720 fprintf(ff,"sizeX %d\n",dim[0] );
721 fprintf(ff,"sizeY %d\n",dim[1] );
722 fprintf(ff,"sizeZ1 %d\n",0 );
723 fprintf(ff,"sizeZ2 %d\n",dim[2] );
724 fprintf(ff,"spcX %f\n",spc[0] );
725 fprintf(ff,"spcY %f\n",spc[1] );
726 fprintf(ff,"spcZ %f\n",spc[2] );
727 fprintf(ff,"intercept %f\n",intercept );
728 fprintf(ff,"slope %f\n",slope );
729 fprintf(ff,"LB %d\n",0 );
730 fprintf(ff,"bits %d\n",16 );
731 fprintf(ff,"%s.raw\n", name.c_str() );