]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
48b16035f6e37fc03ab52f1aff6c9413a54d0f1d
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkReadMHDPlane.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkReadMHDPlane.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include "stdio.h"
8
9
10 #define _LARGEFILE64_SOURCE
11
12 #include <stdio.h>
13 #include <sys/types.h>
14
15 #if defined(_WIN32)
16 #else
17 #include <unistd.h>
18 #endif // defined(_WIN32)
19
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 namespace bbcreaVtk
27 {
28
29 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
30 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
31 //===== 
32 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
33 //===== 
34
35
36 vtkImageData* ReadMHDPlane::CreateDefaultImage()
37 {
38         int i;
39         int sizeX, sizeY, sizeZ;
40         sizeX = 200;
41         sizeY = sizeX;
42         sizeZ = 1;
43         vtkImageData *newImage = vtkImageData::New();
44         newImage->Initialize();
45         newImage->SetScalarTypeToUnsignedChar();
46         newImage->SetSpacing( 1,1,1 );
47         newImage->SetDimensions(  sizeX,sizeY,sizeZ );
48         newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
49         newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
50         newImage->SetNumberOfScalarComponents(1);
51         newImage->AllocateScalars();
52         newImage->Update();
53         memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
54         for (i=0; i<sizeX; i++)
55         {
56                 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
57                 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
58         } // for i
59         return newImage;
60
61
62 /* EED Borrame
63 void ReadMHDPlane::ReadNormalMHD()
64 {
65 // THE MAIN PROCESSING METHOD BODY
66 //   Here we simply set the input 'In' value to the output 'Out'
67 //   And print out the output value
68 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
69 //    void bbSet{Input|Output}NAME(const TYPE&)
70 //    const TYPE& bbGet{Input|Output}NAME() const 
71 //    Where :
72 //    * NAME is the name of the input/output
73 //      (the one provided in the attribute 'name' of the tag 'input')
74 //    * TYPE is the C++ type of the input/output
75 //      (the one provided in the attribute 'type' of the tag 'input')
76
77 //    bbSetOutputOut( bbGetInputIn() );
78 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
79
80         std::string inputfilename;
81         int slice;
82         int width;
83         long long dataSize;
84
85         if (bbGetInputDirectionPlane()=="XY")
86         {
87                 inputfilename=bbGetInputFileName();
88         } // if XY
89
90         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
91         {
92                 //-- Split FileName
93                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
94                 std::string path                =       bbGetInputFileName().substr(0,found+1);
95                 std::string filename    =       bbGetInputFileName().substr(found+1);
96 #ifdef _WIN32
97                 path=path+"YZ_ZX\\";
98 #else
99                 path=path+"YZ_ZX/";
100 #endif
101                 if (bbGetInputDirectionPlane()=="YZ") 
102                 { 
103                         inputfilename = path+filename+"_YZ.mhd";  
104                 } // if YZ
105
106                 if (bbGetInputDirectionPlane()=="ZX")
107                 {
108                         inputfilename = path+filename+"_ZX.mhd";
109                 } // XZ
110         } // if YZ || XZ
111
112         width = bbGetInputWidth();
113         if (width<=0 ) { width=1; }
114
115         slice = bbGetInputSlice();
116         if (slice<0 ) { slice=0; }
117
118         vtkImageData *newImage;
119         long long newHeaderSize;
120     std::string newFileName=inputfilename+"-OneSlice";
121
122     int sx,sy,sz;
123     char mystring[250];
124     char strTmp[20];
125         bool ok=true;
126     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
127         if (ffIn!=NULL)
128         {
129             FILE *ffOut = fopen(newFileName.c_str(),"w");
130         while(!feof(ffIn))
131         {
132             strcpy(mystring,"\n");
133                         fgets(mystring,250,ffIn);
134                 if (strncmp("NDims",mystring,5)==0) 
135                         {
136                                 if (width==1) 
137                                 { 
138                                         strcpy(mystring,"NDims = 2\n"); 
139                                 } else {
140                                         strcpy(mystring,"NDims = 3\n"); 
141                                 }
142                         }
143                 if (strncmp("DimSize",mystring,6)==0) 
144                         {
145                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
146                                 if (width==1) 
147                                 { 
148                                         sprintf(mystring,"DimSize = %d %d\n",sx,sy);
149                                 } else {
150                                         sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
151                                 }
152                         newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
153                                 if (bbGetInputSlice()>=sz) {ok=false;}
154                         } // if
155                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)                   { dataSize=sizeof(char);                        }
156                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)                   { dataSize=sizeof(char);                        }
157                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)                  { dataSize=sizeof(unsigned char);       }
158                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { dataSize=sizeof(unsigned char);       }
159                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0)                 { dataSize=sizeof(unsigned short);      }
160                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { dataSize=sizeof(unsigned short);      }
161                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)                  { dataSize=sizeof(short);                       }
162                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)                  { dataSize=sizeof(short);                       }
163                 if (strncmp("ElementType = MET_UINT",mystring,22)==0)                   { dataSize=sizeof(unsigned int);        }
164                 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0)   { dataSize=sizeof(unsigned int);        }
165                 if (strncmp("ElementType = MET_INT",mystring,21)==0)                    { dataSize=sizeof(int);                         }
166                 if (strncmp("ElementType = VTK_INT",mystring,21)==0)                    { dataSize=sizeof(int);                         }
167                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)                  { dataSize=sizeof(float);                       }
168                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)                  { dataSize=sizeof(float);                       }
169                 if (strncmp("ElementType = MET_LONG",mystring,22)==0)                   { dataSize=sizeof(long);                        }
170                 if (strncmp("ElementType = VTK_LONG",mystring,22)==0)                   { dataSize=sizeof(long);                        }
171                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0)                 { dataSize=sizeof(double);                      }
172                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0)                 { dataSize=sizeof(double);                      }
173                         newHeaderSize=newHeaderSize*dataSize;
174                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
175                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
176                 if (strncmp("ElementDataFile",mystring,15)==0) 
177                         {
178                                 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
179                         } // if
180                 fprintf(ffOut,mystring); 
181         } // while
182                 fclose(ffIn);
183                 fclose(ffOut);
184                 if (ok==true)
185                 {
186                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
187                         reader->SetFileName( newFileName.c_str() );
188                         reader->Update();
189                         newImage = reader->GetOutput();
190                 } // if ok
191         } else {
192                 ok=false;
193                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
194         }// if ffIn
195         if (ok==false)
196         {
197                 newImage=CreateDefaultImage();
198         } // if ok
199
200         bbSetOutputOut( newImage );
201         bbSetOutputOut2( ChangeOrientation(newImage) );
202 }
203 */
204
205 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* img)
206 {
207         int ext[6];
208         int sizeX, sizeY,sizeZ;
209         img->GetWholeExtent(ext);
210         int sizeLine;
211         if (bbGetInputDirectionPlane()=="XY")
212         {
213                 sizeX           = ext[1]-ext[0]+1;
214                 sizeY           = ext[3]-ext[2]+1;
215                 sizeZ           = 1;
216         } // XY 
217         if (bbGetInputDirectionPlane()=="YZ") 
218         {
219                 sizeX           = 1;
220                 sizeY           = ext[1]-ext[0]+1;
221                 sizeZ           = ext[3]-ext[2]+1;
222                 sizeLine        = sizeY;
223         } // YZ
224         if (bbGetInputDirectionPlane()=="ZX")
225         {
226                 sizeX           = ext[1]-ext[0]+1;
227                 sizeY           = 1;
228                 sizeZ           = ext[3]-ext[2]+1;
229                 sizeLine        = sizeX;
230         } // ZX
231         vtkImageData *newImageOrinted  = vtkImageData::New();
232         newImageOrinted->Initialize();
233         newImageOrinted->SetScalarType( img->GetScalarType() );
234         newImageOrinted->SetSpacing( img->GetSpacing() );
235         newImageOrinted->SetDimensions( sizeX,sizeY,sizeZ );
236         newImageOrinted->SetWholeExtent(0,sizeX-1,0,sizeY-1,0,sizeZ-1 );
237         newImageOrinted->SetExtent(0,sizeX-1,0,sizeY-1,0,sizeZ-1 );
238         newImageOrinted->SetNumberOfScalarComponents(1);
239         newImageOrinted->AllocateScalars();
240         newImageOrinted->Update();
241         if (bbGetInputDirectionPlane()=="XY")
242         {
243                 memcpy ( newImageOrinted->GetScalarPointer(), img->GetScalarPointer(), sizeX*sizeY*sizeZ*img->GetScalarSize() );
244         } else  {
245                 int yy;
246                 int dimY=ext[3]-ext[2]+1;
247                 int sizeByte=sizeLine*img->GetScalarSize();             
248                 char *ptrDst=(char*)(newImageOrinted->GetScalarPointer()) + sizeByte*dimY;
249                 char *ptrOrg=(char*)(img->GetScalarPointer());
250                 for (yy=0 ; yy<dimY; yy++)
251                 {
252                         ptrDst=ptrDst-sizeByte;
253                         memcpy ( ptrDst, ptrOrg , sizeByte );
254                         ptrOrg=ptrOrg+sizeByte;
255                 }// for
256         }
257         return newImageOrinted;
258 }
259
260 /* EED Borrame
261 void ReadMHDPlane::Read64lseek()
262 {
263         int             slice;
264         int             width;
265         width = bbGetInputWidth();
266         if (width<=0 ) { width=1; }
267         slice = bbGetInputSlice();
268         if (slice<0 ) { slice=0; }
269         int             dimX=-1,dimY=-1,dimZ=-1;
270         int             dim=-1;
271         std::string formattype("VOID");
272         std::string elementdatafile("VOID");
273         float           spcX=-1,spcY=-1,spcZ=-1;
274         float           ox=-1,oy=-1,oz=-1;
275         long int        headersize=0;
276         vtkImageData *newImage=NULL;
277         char mystring[250];
278         char strTmp[30];
279         char strTmp2[30];
280         FILE *ffIn      = fopen(bbGetInputFileName().c_str(),"r");
281         long long dataSize;
282         if (ffIn!=NULL)
283         {
284                 newImage = vtkImageData::New();
285                 while(!feof(ffIn))
286                 {
287             strcpy(mystring,"\n");
288                         fgets(mystring,250,ffIn);
289                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                                                 }
290                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);                                   } 
291                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
292                         if (strncmp("ElementSpacing",mystring,14)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);                                    }
293                         if (strncmp("ElementSize",mystring,11)==0)              { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);                                    }
294                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                                                }
295                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                                                  }
296                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
297                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
298                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
299                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)                  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
300                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
301                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0)                 { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
302                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
303                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
304                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
305                 if (strncmp("ElementType = MET_UINT",mystring,22)==0)                   { newImage->SetScalarTypeToUnsignedInt();       dataSize=sizeof(unsigned int);  }
306                 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0)   { newImage->SetScalarTypeToUnsignedInt();       dataSize=sizeof(unsigned int);  }
307                 if (strncmp("ElementType = MET_INT",mystring,21)==0)                    { newImage->SetScalarTypeToInt();                       dataSize=sizeof(int);                   }
308                 if (strncmp("ElementType = VTK_INT",mystring,21)==0)                    { newImage->SetScalarTypeToInt();                       dataSize=sizeof(int);                   }
309                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
310                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
311                 if (strncmp("ElementType = MET_LONG",mystring,22)==0)                   { newImage->SetScalarTypeToLong();                      dataSize=sizeof(long);                  }
312                 if (strncmp("ElementType = VTK_LONG",mystring,22)==0)                   { newImage->SetScalarTypeToLong();                      dataSize=sizeof(long);                  }
313                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
314                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
315         } // while
316                 fclose(ffIn);
317                 newImage->Initialize();
318                 int             fd;
319                 long long       ret;
320                 std::size_t found;
321                 std::string filename;
322                 found                                   = bbGetInputFileName().find_last_of("/\\");
323                 filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
324                 long long       pos;
325                 long long lsize                 = dimX*dimY*width *dataSize;
326 #if defined(_WIN32)
327                    _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
328 #else
329                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
330 #endif // defined(_WIN32)
331                 if (fd < 0)
332                 {
333                         printf("EED ReadMHDPlane::Read64lseek   WARNNING! raw file not exist\n");
334                         fprintf(stderr, "%s\n", strerror(errno));
335                         newImage=CreateDefaultImage();
336 //                      exit(1);
337                 }       
338                 if ((bbGetInputDirectionPlane()=="XY") && (fd>=0)) 
339                 { 
340                         newImage->SetSpacing( spcX,spcY,spcZ );
341                         newImage->SetDimensions(  dimX,dimY,width );
342                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,width-1 );
343                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,width-1 );
344                         newImage->SetNumberOfScalarComponents(1);
345                         newImage->AllocateScalars();
346                         newImage->Update();
347                         pos = dimX*dimY*(long long)slice*dataSize;
348 #if defined(_WIN32)
349                         if (_lseeki64( fd, pos, SEEK_SET ) < 0)
350 #else
351                         if (lseek64(fd, pos, SEEK_SET) < 0) 
352 #endif // defined(_WIN32)
353                         {
354                                 printf("EED ReadMHDPlane::Read64lseek \n");
355                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
356                                 exit(1);
357                         }
358                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
359                         {
360                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
361                                 exit(1);
362                         }
363                 }  // if PLANE XY
364                 if ((bbGetInputDirectionPlane()=="ZX") && (fd>=0))
365                 { 
366                         newImage->SetSpacing( spcX,spcZ,spcY );
367                         newImage->SetDimensions(  dimX,dimZ,width );
368                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
369                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
370                         newImage->SetNumberOfScalarComponents(1);
371                         newImage->AllocateScalars();
372                         newImage->Update();
373                         int iWidth;
374                         for (iWidth=0;iWidth<width;iWidth++)
375                         {
376                                 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
377                         }
378                 }  // if PLANE XZ
379                 if ((bbGetInputDirectionPlane()=="YZ") && (fd>=0))
380                 { 
381                         newImage->SetSpacing( spcY,spcZ,spcX );
382                         newImage->SetDimensions(  dimY,dimZ,width );
383                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
384                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
385                         newImage->SetNumberOfScalarComponents(1);
386                         newImage->AllocateScalars();
387                         newImage->Update();
388                         int iWidth;
389                         for (iWidth=0;iWidth<width;iWidth++)
390                         {
391                                 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
392                         }
393                 }  // if PLANE YZ       
394 #if defined(_WIN32)
395                 _close (fd);
396 #else
397                 close (fd);
398 #endif // defined(_WIN32)
399         } else {
400                 newImage=CreateDefaultImage();
401         } // if  ffIn
402         bbSetOutputOut( newImage );
403         bbSetOutputOut2( ChangeOrientation(newImage) );
404 }
405 */
406
407 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
408 {
409         int             slice;
410         int             width;
411         width = bbGetInputWidth();
412         if (width<=0 ) { width=1; }
413         slice = bbGetInputSlice();
414         if (slice<0 ) { slice=0; }
415         int             dimX=-1,dimY=-1,dimZ=-1;
416         int             dim=-1;
417         std::string formattype("VOID");
418         std::string elementdatafile("VOID");
419         float           spcX=-1,spcY=-1,spcZ=-1;
420         float           ox=-1,oy=-1,oz=-1;
421         long int        headersize=0;
422         vtkImageData *newImage=NULL;
423         char mystring[250];
424         char strTmp[30];
425         char strTmp2[30];
426         FILE *ffIn      = fopen( fileNameIn.c_str() , "r");
427         long long dataSize;
428         if (ffIn!=NULL)
429         {
430                 newImage = vtkImageData::New();
431                 while(!feof(ffIn))
432                 {
433             strcpy(mystring,"\n");
434                         fgets(mystring,250,ffIn);
435                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                                                 }
436                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);                                   } 
437                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
438                         if (strncmp("ElementSpacing",mystring,14)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);                                    }
439                         if (strncmp("ElementSize",mystring,11)==0)              { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);                                    }
440                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                                                }
441                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                                                  }
442                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
443                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
444                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
445                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)                  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
446                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
447                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0)                 { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
448                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
449                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
450                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
451                 if (strncmp("ElementType = MET_UINT",mystring,22)==0)                   { newImage->SetScalarTypeToUnsignedInt();       dataSize=sizeof(unsigned int);  }
452                 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0)   { newImage->SetScalarTypeToUnsignedInt();       dataSize=sizeof(unsigned int);  }
453                 if (strncmp("ElementType = MET_INT",mystring,21)==0)                    { newImage->SetScalarTypeToInt();                       dataSize=sizeof(int);                   }
454                 if (strncmp("ElementType = VTK_INT",mystring,21)==0)                    { newImage->SetScalarTypeToInt();                       dataSize=sizeof(int);                   }
455                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
456                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
457                 if (strncmp("ElementType = MET_LONG",mystring,22)==0)                   { newImage->SetScalarTypeToLong();                      dataSize=sizeof(long);                  }
458                 if (strncmp("ElementType = VTK_LONG",mystring,22)==0)                   { newImage->SetScalarTypeToLong();                      dataSize=sizeof(long);                  }
459                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
460                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
461         } // while
462                 fclose(ffIn);
463                 newImage->Initialize();
464                 int             fd;
465                 long long       ret;
466                 std::size_t found;
467                 std::string filename;
468                 found                                   = fileNameIn.find_last_of("/\\");
469                 filename                                = fileNameIn.substr(0,found+1) + elementdatafile ;
470                 long long       pos;
471                 long long lsize                 = dimX*dimY*width *dataSize;
472 #if defined(_WIN32)
473                    _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
474 #else
475                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
476 #endif // defined(_WIN32)
477                 if (fd < 0)
478                 {
479                         printf("EED ReadMHDPlane::Read64lseek   WARNNING! raw file not exist\n");
480                         fprintf(stderr, "%s\n", strerror(errno));
481                         newImage=CreateDefaultImage();
482 //                      exit(1);
483                 }       
484                 if ((plane=="XY") && (fd>=0)) 
485                 { 
486                         newImage->SetSpacing( spcX,spcY,spcZ );
487                         newImage->SetDimensions(  dimX,dimY,width );
488                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,width-1 );
489                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,width-1 );
490                         newImage->SetNumberOfScalarComponents(1);
491                         newImage->AllocateScalars();
492                         newImage->Update();
493                         pos = dimX*dimY*(long long)slice*dataSize;
494 #if defined(_WIN32)
495                         if (_lseeki64( fd, pos, SEEK_SET ) < 0)
496 #else
497                         if (lseek64(fd, pos, SEEK_SET) < 0) 
498 #endif // defined(_WIN32)
499                         {
500                                 printf("EED ReadMHDPlane::Read64lseek \n");
501                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
502                                 exit(1);
503                         }
504                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
505                         {
506                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
507                                 exit(1);
508                         }
509                 }  // if PLANE XY
510                 if ((plane=="ZX") && (fd>=0))
511                 { 
512                         newImage->SetSpacing( spcX,spcZ,spcY );
513                         newImage->SetDimensions(  dimX,dimZ,width );
514                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
515                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
516                         newImage->SetNumberOfScalarComponents(1);
517                         newImage->AllocateScalars();
518                         newImage->Update();
519                         int iWidth;
520                         for (iWidth=0;iWidth<width;iWidth++)
521                         {
522                                 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
523                         }
524                 }  // if PLANE XZ
525                 if ((plane=="YZ") && (fd>=0))
526                 { 
527                         newImage->SetSpacing( spcY,spcZ,spcX );
528                         newImage->SetDimensions(  dimY,dimZ,width );
529                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
530                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
531                         newImage->SetNumberOfScalarComponents(1);
532                         newImage->AllocateScalars();
533                         newImage->Update();
534                         int iWidth;
535                         for (iWidth=0;iWidth<width;iWidth++)
536                         {
537                                 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
538                         }
539                 }  // if PLANE YZ       
540 #if defined(_WIN32)
541                 _close (fd);
542 #else
543                 close (fd);
544 #endif // defined(_WIN32)
545         } else {
546                 newImage=CreateDefaultImage();
547         } // if  ffIn
548         bbSetOutputOut( newImage );
549         bbSetOutputOut2( ChangeOrientation(newImage) );
550 }
551
552
553 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
554 {
555         long long int j;
556         long long int i;
557         long long       ret;
558         long long       pos;
559         long long sizeBytesPlane = dimX*dimY*dataSize;
560         char *pImage;
561         for (j=0;j<dimZ;j++)
562         {
563                 for (i=0;i<dimY;i++)
564                 {
565                         pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
566 #if defined(_WIN32)
567                         if (_lseeki64( fd, pos , SEEK_SET ) < 0)
568 #else
569                         if (lseek64(fd, pos , SEEK_SET) < 0) 
570 #endif // defined(_WIN32)
571                         {
572                                 printf("EED ReadMHDPlane::Read64lseek \n");
573                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
574                                 exit(1);
575                         }
576
577                         pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
578                         if ((ret = read(fd, pImage , dataSize)) < 0) 
579                         {
580                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
581                                 exit(1);
582                         }
583                         } // for i
584                 } // for j
585 }
586
587
588 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
589 {
590         long long int j;
591         long long       ret;
592         long long       pos;
593         pos = dimX*(long long int)slice*dataSize;
594         long long sizeBytesPlane = dimX*dimY*dataSize;
595         char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
596         for (j=dimZ-1;j>=0;j--)
597         {
598 #if defined(_WIN32)
599                 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
600 #else
601                 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
602 #endif // defined(_WIN32)
603                 {
604                         printf("EED ReadMHDPlane::Read64lseek \n");
605                         fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
606                         exit(1);
607                 }
608
609                 if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
610                 {
611                         fprintf(stderr, "Failed reading: %s\n", strerror(errno));
612                         exit(1);
613                 }
614                 pImage = pImage+dimX*dataSize;
615         } // for j
616 }
617
618
619
620 void ReadMHDPlane::Process()
621 {
622         if (bbGetInputType()==0)
623         {
624                 //-- Split FileName
625                 std::string inputfilename;
626                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
627                 std::string path                =       bbGetInputFileName().substr(0,found+1);
628                 std::string filename    =       bbGetInputFileName().substr(found+1);
629 #ifdef _WIN32
630                 path=path+"YZ_ZX\\";
631 #else
632                 path=path+"YZ_ZX/";
633 #endif
634                 if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName();           } // if XY
635                 if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd";        } // if YZ
636                 if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd";        } // if XZ
637                 Read64lseek( inputfilename ,"XY");
638         }       
639
640         if (bbGetInputType()==1)
641         {
642                 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
643         }       
644
645 }
646 //===== 
647 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
648 //===== 
649 void ReadMHDPlane::bbUserSetDefaultValues()
650 {
651
652 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
653 //    Here we initialize the input 'In' to 0
654    bbSetInputFileName("");
655    bbSetInputSlice(0);
656    bbSetInputType(1);
657    bbSetInputWidth(1);
658    bbSetInputDirectionPlane("XY");
659   
660 }
661 //===== 
662 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
663 //===== 
664 void ReadMHDPlane::bbUserInitializeProcessing()
665 {
666
667 //  THE INITIALIZATION METHOD BODY :
668 //    Here does nothing 
669 //    but this is where you should allocate the internal/output pointers 
670 //    if any 
671
672   
673 }
674 //===== 
675 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
676 //===== 
677 void ReadMHDPlane::bbUserFinalizeProcessing()
678 {
679
680 //  THE FINALIZATION METHOD BODY :
681 //    Here does nothing 
682 //    but this is where you should desallocate the internal/output pointers 
683 //    if any
684   
685 }
686 }
687 // EO namespace bbcreaVtk
688
689