]> Creatis software - creaVtk.git/blobdiff - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
#2913 creaVtk Feature New Normal - new box MHDFileInfo
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkReadMHDPlane.cxx
index 5436547076db319eee239fe021624f225e3af6d2..6bb42f73c63da4b3eef6c1ea599c9d0cc0c0f968 100644 (file)
@@ -32,69 +32,120 @@ void ReadMHDPlane::Process()
 //    bbSetOutputOut( bbGetInputIn() );
 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
 
+       std::string inputfilename;
+
+       if (bbGetInputDirectionPlane()=="XY")
+       {
+               inputfilename=bbGetInputFileName();
+       } // if XY
 
 
+       if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
+       {
 
+               //-- Split FileName
+               std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
+               std::string path                =       bbGetInputFileName().substr(0,found+1);
+               std::string filename    =       bbGetInputFileName().substr(found+1);
+#ifdef _WIN32
+               path=path+"YZ_ZX\\";
+#else
+               path=path+"YZ_ZX/";
+#endif
+       
+       
+               if (bbGetInputDirectionPlane()=="YZ") 
+               { 
+                       inputfilename = path+filename+"_YZ.mhd";  
+               } // if YZ
+
+               if (bbGetInputDirectionPlane()=="ZX")
+               {
+                       inputfilename = path+filename+"_ZX.mhd";
+               } // XZ
+       } // if YZ || XZ
+
+       vtkImageData *newImage;
        long int newHeaderSize;
-       std::string newFileName=bbGetInputFileName()+"-OneSlice";
+    std::string newFileName=inputfilename+"-OneSlice";
 
-    int sx,sy;
+    int sx,sy,sz;
     char mystring[250];
     char strTmp[20];
-    FILE *ffOut        = fopen(newFileName.c_str(),"w+");
-    FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r+");
-
-    while(!feof(ffIn))
-    {
-        strcpy(mystring,"\n");
-        fgets(mystring,250,ffIn);
-        if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
-        if (strncmp("DimSize",mystring,6)==0) 
+       bool ok=true;
+    FILE *ffIn = fopen(inputfilename.c_str(),"r");
+       if (ffIn!=NULL)
        {
-               sscanf(mystring,"%s %s %d %d",strTmp, strTmp, &sx, &sy);
-               sprintf(mystring,"DimSize = %d %d\n",sx,sy);
-               newHeaderSize = sx*sy*bbGetInputSlice();
-       }
-        if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
-        if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newHeaderSize=newHeaderSize*2; }
-        if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*2; }
-        if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
-        if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
-        if (strncmp("ElementDataFile",mystring,15)==0) 
+           FILE *ffOut = fopen(newFileName.c_str(),"w");
+
+       while(!feof(ffIn))
+       {
+           strcpy(mystring,"\n");
+                       fgets(mystring,250,ffIn);
+               if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
+               if (strncmp("DimSize",mystring,6)==0) 
+                       {
+                               sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
+                               sprintf(mystring,"DimSize = %d %d\n",sx,sy);
+                       newHeaderSize = sx*sy*bbGetInputSlice();
+                               if (bbGetInputSlice()>=sz) {ok=false;}
+                       } // if
+               if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
+               if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newHeaderSize=newHeaderSize*1; }
+               if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
+               if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
+               if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
+               if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
+               if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
+               if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
+               if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
+               if (strncmp("ElementDataFile",mystring,15)==0) 
+                       {
+                               fprintf(ffOut,"HeaderSize = %ld\n\n", newHeaderSize );
+                       } // if
+               fprintf(ffOut,mystring); 
+       } // while
+               fclose(ffIn);
+               fclose(ffOut);
+
+               if (ok==true)
+               {
+                       vtkMetaImageReader * reader = vtkMetaImageReader::New();
+                       reader->SetFileName( newFileName.c_str() );
+                       reader->Update();
+                       newImage = reader->GetOutput();
+               } // if ok
+       } else {
+               ok=false;
+               printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
+       }
+
+       if (ok==false)
        {
-               fprintf(ffOut,"HeaderSize = %ld\n\n", newHeaderSize );
-       }
-        fprintf(ffOut,mystring); 
-    }
-
-/*
-   fprintf(ff,"ObjectType = Image\n");
-   fprintf(ff,"NDims = 2\n");
-   fprintf(ff,"BinaryData = True\n");
-   fprintf(ff,"BinaryDataByteOrderMSB = False\n");
-   fprintf(ff,"CompressedData = False\n");
-   fprintf(ff,"TransformMatrix = 1 0 0 0 1 0 0 0 1\n");
-   fprintf(ff,"CenterOfRotation = 0 0 0\n");
-   fprintf(ff,"ElementSpacing = 0.7 0.7 0.7\n");
-   fprintf(ff,"DimSize = 512 512\n");
-   fprintf(ff,"AnatomicalOrientation = ???\n");
-   fprintf(ff,"ElementType = MET_SHORT\n");
-   fprintf(ff,"ElementNumberOfChannels = 1\n");
-   fprintf(ff,"Offset = 0 0 0\n");
-   fprintf(ff,"HeaderSize = %ld\n", newHeaderSize );
-   fprintf(ff,"ElementDataFile = inspi1_ana.raw\n");
-*/
-
-   fclose(ffIn);
-   fclose(ffOut);
-
-
-   vtkMetaImageReader * reader = vtkMetaImageReader::New();
-   reader->SetFileName( newFileName.c_str() );
-   reader->Update();
-   
-   bbSetOutputOut( reader->GetOutput() );
-
+               int i;
+               int sizeX, sizeY, sizeZ;
+               sizeX = 200;
+               sizeY = sizeX;
+               sizeZ = 1;
+               newImage = vtkImageData::New();
+               newImage->Initialize();
+               newImage->SetScalarTypeToUnsignedChar();
+               newImage->SetSpacing( 1,1,1 );
+               newImage->SetDimensions(  sizeX,sizeY,sizeZ );
+               newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
+               newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
+               newImage->SetNumberOfScalarComponents(1);
+               newImage->AllocateScalars();
+               newImage->Update();
+               memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
+               for (i=0; i<sizeX; i++)
+               {
+                       newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
+                       newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
+               } // for i
+       } // if ok
+
+       bbSetOutputOut( newImage );
 
 }
 //===== 
@@ -107,6 +158,7 @@ void ReadMHDPlane::bbUserSetDefaultValues()
 //    Here we initialize the input 'In' to 0
    bbSetInputFileName("");
    bbSetInputSlice(0);
+   bbSetInputDirectionPlane("XY");
   
 }
 //=====