]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
#2960 creaVtk Bug New Normal - ElementSize = ElementSpacing in MHDFileInfo box and...
[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
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
84         if (bbGetInputDirectionPlane()=="XY")
85         {
86                 inputfilename=bbGetInputFileName();
87         } // if XY
88
89
90         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
91         {
92
93                 //-- Split FileName
94                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
95                 std::string path                =       bbGetInputFileName().substr(0,found+1);
96                 std::string filename    =       bbGetInputFileName().substr(found+1);
97 #ifdef _WIN32
98                 path=path+"YZ_ZX\\";
99 #else
100                 path=path+"YZ_ZX/";
101 #endif
102         
103         
104                 if (bbGetInputDirectionPlane()=="YZ") 
105                 { 
106                         inputfilename = path+filename+"_YZ.mhd";  
107                 } // if YZ
108
109                 if (bbGetInputDirectionPlane()=="ZX")
110                 {
111                         inputfilename = path+filename+"_ZX.mhd";
112                 } // XZ
113         } // if YZ || XZ
114
115         width = bbGetInputWidth();
116         if (width<=0 ) { width=1; }
117
118         slice = bbGetInputSlice();
119         if (slice<0 ) { slice=0; }
120
121
122         vtkImageData *newImage;
123         long long int newHeaderSize;
124     std::string newFileName=inputfilename+"-OneSlice";
125
126     int sx,sy,sz;
127     char mystring[250];
128     char strTmp[20];
129         bool ok=true;
130     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
131         if (ffIn!=NULL)
132         {
133             FILE *ffOut = fopen(newFileName.c_str(),"w");
134
135         while(!feof(ffIn))
136         {
137             strcpy(mystring,"\n");
138                         fgets(mystring,250,ffIn);
139                 if (strncmp("NDims",mystring,5)==0) 
140                         {
141                                 if (width==1) 
142                                 { 
143                                         strcpy(mystring,"NDims = 2\n"); 
144                                 } else {
145                                         strcpy(mystring,"NDims = 3\n"); 
146                                 }
147                         }
148                 if (strncmp("DimSize",mystring,6)==0) 
149                         {
150                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
151
152                                 if (width==1) 
153                                 { 
154                                         sprintf(mystring,"DimSize = %d %d\n",sx,sy);
155                                 } else {
156                                         sprintf(mystring,"DimSize = %d %d %d\n",sx,sy,width);
157                                 }
158
159                         newHeaderSize = (long long int)sx*(long long int)sy*(long long int)slice;
160                                 if (bbGetInputSlice()>=sz) {ok=false;}
161                         } // if
162                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
163                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newHeaderSize=newHeaderSize*1; }
164                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
165                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
166                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
167                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
168                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
169                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
170                 if (strncmp("ElementDataFile",mystring,15)==0) 
171                         {
172                                 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
173                         } // if
174                 fprintf(ffOut,mystring); 
175         } // while
176                 fclose(ffIn);
177                 fclose(ffOut);
178
179                 if (ok==true)
180                 {
181                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
182                         reader->SetFileName( newFileName.c_str() );
183                         reader->Update();
184                         newImage = reader->GetOutput();
185                 } // if ok
186         } else {
187                 ok=false;
188                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
189         }
190
191         if (ok==false)
192         {
193                 newImage=CreateDefaultImage();
194         } // if ok
195
196         bbSetOutputOut( newImage );
197 }
198
199
200
201
202
203 void ReadMHDPlane::Read64lseek()
204 {
205         int             slice;
206         int             width;
207
208         width = bbGetInputWidth();
209         if (width<=0 ) { width=1; }
210
211         slice = bbGetInputSlice();
212         if (slice<0 ) { slice=0; }
213
214         int             dimX=-1,dimY=-1,dimZ=-1;
215         int             dim=-1;
216         std::string formattype("VOID");
217         std::string elementdatafile("VOID");
218         float           spcX=-1,spcY=-1,spcZ=-1;
219         float           ox=-1,oy=-1,oz=-1;
220         long int        headersize=0;
221
222         vtkImageData *newImage=NULL;
223
224         char mystring[250];
225         char strTmp[30];
226         char strTmp2[30];
227         FILE *ffIn      = fopen(bbGetInputFileName().c_str(),"r");
228         long long dataSize;
229
230         if (ffIn!=NULL)
231         {
232                 newImage = vtkImageData::New();
233
234                 while(!feof(ffIn))
235                 {
236             strcpy(mystring,"\n");
237                         fgets(mystring,250,ffIn);
238                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                 }
239                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);   } 
240                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
241                         if (strncmp("ElementSpacing",mystring,14)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);    }
242                         if (strncmp("ElementSize",mystring,11)==0)              { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);    }
243                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                }
244                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                  }
245                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
246
247                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
248                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)                   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
249                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)                  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
250                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
251                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0)                 { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
252                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
253                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
254                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)                  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
255                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
256                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)                  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
257                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
258                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0)                 { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
259
260         } // while
261                 fclose(ffIn);
262
263                 newImage->Initialize();
264
265
266                 int             fd;
267                 long long       ret;
268                 std::size_t found;
269                 std::string filename;
270                 found                                   = bbGetInputFileName().find_last_of("/\\");
271                 filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
272                 long long       pos;
273
274                 long long lsize                 = dimX*dimY*width *dataSize;
275
276
277 #if defined(_WIN32)
278                    _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
279 #else
280                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
281 #endif // defined(_WIN32)
282
283
284
285                 if (fd < 0)
286                 {
287                         printf("EED ReadMHDPlane::Read64lseek \n");
288                         fprintf(stderr, "%s\n", strerror(errno));
289                         exit(1);
290                 }
291
292                 if (bbGetInputDirectionPlane()=="XY") 
293                 { 
294                         newImage->SetSpacing( spcX,spcY,spcZ );
295                         newImage->SetDimensions(  dimX,dimY,width );
296                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,width-1 );
297                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,width-1 );
298                         newImage->SetNumberOfScalarComponents(1);
299                         newImage->AllocateScalars();
300                         newImage->Update();
301                         pos = dimX*dimY*(long long)slice*dataSize;
302 #if defined(_WIN32)
303                         if (_lseeki64( fd, pos, SEEK_SET ) < 0)
304 #else
305                         if (lseek64(fd, pos, SEEK_SET) < 0) 
306 #endif // defined(_WIN32)
307                         {
308                                 printf("EED ReadMHDPlane::Read64lseek \n");
309                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
310                                 exit(1);
311                         }
312
313                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
314                         {
315                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
316                                 exit(1);
317                         }
318                 }  // if PLANE XY
319
320                 if (bbGetInputDirectionPlane()=="XZ") 
321                 { 
322                         newImage->SetSpacing( spcX,spcZ,spcY );
323                         newImage->SetDimensions(  dimX,dimZ,width );
324                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
325                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
326                         newImage->SetNumberOfScalarComponents(1);
327                         newImage->AllocateScalars();
328                         newImage->Update();
329                         int iWidth;
330                         for (iWidth=0;iWidth<width;iWidth++)
331                         {
332                                 copy_XZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
333                         }
334                 }  // if PLANE XZ
335
336
337                 if (bbGetInputDirectionPlane()=="YZ") 
338                 { 
339                         newImage->SetSpacing( spcY,spcZ,spcX );
340                         newImage->SetDimensions(  dimY,dimZ,width );
341                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
342                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
343                         newImage->SetNumberOfScalarComponents(1);
344                         newImage->AllocateScalars();
345                         newImage->Update();
346                         int iWidth;
347                         for (iWidth=0;iWidth<width;iWidth++)
348                         {
349                                 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
350                         }
351
352                 }  // if PLANE YZ       
353 #if defined(_WIN32)
354                 _close (fd);
355 #else
356                 close (fd);
357 #endif // defined(_WIN32)
358         } else {
359                 newImage=CreateDefaultImage();
360         } // if  ffIn
361
362         bbSetOutputOut( newImage );
363 }
364
365
366 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
367 {
368         long long int j;
369         long long int i;
370         long long       ret;
371         long long       pos;
372         long long sizeBytesPlane = dimX*dimY*dataSize;
373         char *pImage;
374         for (j=0;j<dimZ;j++)
375         {
376                 for (i=0;i<dimY;i++)
377                 {
378                         pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
379 #if defined(_WIN32)
380                         if (_lseeki64( fd, pos , SEEK_SET ) < 0)
381 #else
382                         if (lseek64(fd, pos , SEEK_SET) < 0) 
383 #endif // defined(_WIN32)
384                         {
385                                 printf("EED ReadMHDPlane::Read64lseek \n");
386                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
387                                 exit(1);
388                         }
389
390                         pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
391                         if ((ret = read(fd, pImage , dataSize)) < 0) 
392                         {
393                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
394                                 exit(1);
395                         }
396                         } // for i
397                 } // for j
398 }
399
400
401 void ReadMHDPlane::copy_XZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
402 {
403         long long int j;
404         long long       ret;
405         long long       pos;
406         pos = dimX*(long long int)slice*dataSize;
407         long long sizeBytesPlane = dimX*dimY*dataSize;
408         char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
409         for (j=dimZ-1;j>=0;j--)
410         {
411 #if defined(_WIN32)
412                 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
413 #else
414                 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
415 #endif // defined(_WIN32)
416                 {
417                         printf("EED ReadMHDPlane::Read64lseek \n");
418                         fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
419                         exit(1);
420                 }
421
422                 if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
423                 {
424                         fprintf(stderr, "Failed reading: %s\n", strerror(errno));
425                         exit(1);
426                 }
427                 pImage = pImage+dimX*dataSize;
428         } // for j
429 }
430
431
432
433 void ReadMHDPlane::Process()
434 {
435         if (bbGetInputType()==0)
436         {
437                 ReadNormalMHD();
438         }       
439
440         if (bbGetInputType()==1)
441         {
442                 Read64lseek();
443         }       
444
445 }
446 //===== 
447 // 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)
448 //===== 
449 void ReadMHDPlane::bbUserSetDefaultValues()
450 {
451
452 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
453 //    Here we initialize the input 'In' to 0
454    bbSetInputFileName("");
455    bbSetInputSlice(0);
456    bbSetInputType(1);
457    bbSetInputWidth(1);
458    bbSetInputDirectionPlane("XY");
459   
460 }
461 //===== 
462 // 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)
463 //===== 
464 void ReadMHDPlane::bbUserInitializeProcessing()
465 {
466
467 //  THE INITIALIZATION METHOD BODY :
468 //    Here does nothing 
469 //    but this is where you should allocate the internal/output pointers 
470 //    if any 
471
472   
473 }
474 //===== 
475 // 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)
476 //===== 
477 void ReadMHDPlane::bbUserFinalizeProcessing()
478 {
479
480 //  THE FINALIZATION METHOD BODY :
481 //    Here does nothing 
482 //    but this is where you should desallocate the internal/output pointers 
483 //    if any
484   
485 }
486 }
487 // EO namespace bbcreaVtk
488
489