]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
3209a952b3d42d8978f49d7bdbd0b7ee80a6e14e
[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 void ReadMHDPlane::ReadNormalMHD()
36 {
37 // THE MAIN PROCESSING METHOD BODY
38 //   Here we simply set the input 'In' value to the output 'Out'
39 //   And print out the output value
40 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
41 //    void bbSet{Input|Output}NAME(const TYPE&)
42 //    const TYPE& bbGet{Input|Output}NAME() const 
43 //    Where :
44 //    * NAME is the name of the input/output
45 //      (the one provided in the attribute 'name' of the tag 'input')
46 //    * TYPE is the C++ type of the input/output
47 //      (the one provided in the attribute 'type' of the tag 'input')
48
49 //    bbSetOutputOut( bbGetInputIn() );
50 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
51
52         std::string inputfilename;
53
54         if (bbGetInputDirectionPlane()=="XY")
55         {
56                 inputfilename=bbGetInputFileName();
57         } // if XY
58
59
60         if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
61         {
62
63                 //-- Split FileName
64                 std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
65                 std::string path                =       bbGetInputFileName().substr(0,found+1);
66                 std::string filename    =       bbGetInputFileName().substr(found+1);
67 #ifdef _WIN32
68                 path=path+"YZ_ZX\\";
69 #else
70                 path=path+"YZ_ZX/";
71 #endif
72         
73         
74                 if (bbGetInputDirectionPlane()=="YZ") 
75                 { 
76                         inputfilename = path+filename+"_YZ.mhd";  
77                 } // if YZ
78
79                 if (bbGetInputDirectionPlane()=="ZX")
80                 {
81                         inputfilename = path+filename+"_ZX.mhd";
82                 } // XZ
83         } // if YZ || XZ
84
85         vtkImageData *newImage;
86         long long int newHeaderSize;
87     std::string newFileName=inputfilename+"-OneSlice";
88
89     int sx,sy,sz;
90     char mystring[250];
91     char strTmp[20];
92         bool ok=true;
93     FILE *ffIn  = fopen(inputfilename.c_str(),"r");
94         if (ffIn!=NULL)
95         {
96             FILE *ffOut = fopen(newFileName.c_str(),"w");
97
98         while(!feof(ffIn))
99         {
100             strcpy(mystring,"\n");
101                         fgets(mystring,250,ffIn);
102                 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
103                 if (strncmp("DimSize",mystring,6)==0) 
104                         {
105                                 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
106                                 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
107                         newHeaderSize = (long long int)sx*(long long int)sy*(long long int)bbGetInputSlice();
108                                 if (bbGetInputSlice()>=sz) {ok=false;}
109                         } // if
110                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newHeaderSize=newHeaderSize*1; }
111                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newHeaderSize=newHeaderSize*1; }
112                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
113                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newHeaderSize=newHeaderSize*2; }
114                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
115                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newHeaderSize=newHeaderSize*4; }
116                 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
117                 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
118                 if (strncmp("ElementDataFile",mystring,15)==0) 
119                         {
120                                 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
121                         } // if
122                 fprintf(ffOut,mystring); 
123         } // while
124                 fclose(ffIn);
125                 fclose(ffOut);
126
127                 if (ok==true)
128                 {
129                         vtkMetaImageReader * reader = vtkMetaImageReader::New();
130                         reader->SetFileName( newFileName.c_str() );
131                         reader->Update();
132                         newImage = reader->GetOutput();
133                 } // if ok
134         } else {
135                 ok=false;
136                 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
137         }
138
139         if (ok==false)
140         {
141                 int i;
142                 int sizeX, sizeY, sizeZ;
143                 sizeX = 200;
144                 sizeY = sizeX;
145                 sizeZ = 1;
146                 newImage = vtkImageData::New();
147                 newImage->Initialize();
148                 newImage->SetScalarTypeToUnsignedChar();
149                 newImage->SetSpacing( 1,1,1 );
150                 newImage->SetDimensions(  sizeX,sizeY,sizeZ );
151                 newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
152                 newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
153                 newImage->SetNumberOfScalarComponents(1);
154                 newImage->AllocateScalars();
155                 newImage->Update();
156                 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
157                 for (i=0; i<sizeX; i++)
158                 {
159                         newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
160                         newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
161                 } // for i
162         } // if ok
163
164         bbSetOutputOut( newImage );
165
166 }
167
168 void ReadMHDPlane::Read64lseek()
169 {
170
171         int             dimX=-1,dimY=-1,dimZ=-1;
172         int             dim=-1;
173         std::string formattype("VOID");
174         std::string elementdatafile("VOID");
175         float           spcX=-1,spcY=-1,spcZ=-1;
176         float           ox=-1,oy=-1,oz=-1;
177         long int        headersize=0;
178
179         vtkImageData *newImage;
180
181         char mystring[250];
182         char strTmp[30];
183         char strTmp2[30];
184         FILE *ffIn      = fopen(bbGetInputFileName().c_str(),"r");
185         long long dataSize;
186
187
188
189         if (ffIn!=NULL)
190         {
191                 newImage = vtkImageData::New();
192
193                 while(!feof(ffIn))
194                 {
195             strcpy(mystring,"\n");
196                         fgets(mystring,250,ffIn);
197                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                 }
198                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);   } 
199                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
200                         if (strncmp("ElementSpacing",mystring,11)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);    }
201                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                }
202                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                  }
203                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
204
205                 if (strncmp("ElementType = MET_CHAR",mystring,22)==0)   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
206                 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)   { newImage->SetScalarTypeToChar();                      dataSize=sizeof(char);                  }
207                 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
208                 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { newImage->SetScalarTypeToUnsignedChar();      dataSize=sizeof(unsigned char); }
209                 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
210                 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort();     dataSize=sizeof(unsigned short);}
211                 if (strncmp("ElementType = MET_SHORT",mystring,23)==0)  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
212                 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)  { newImage->SetScalarTypeToShort();                     dataSize=sizeof(short);                 }
213                 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
214                 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)  { newImage->SetScalarTypeToFloat();                     dataSize=sizeof(float);                 }
215                 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
216                 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble();            dataSize=sizeof(double);                }
217
218         } // while
219                 fclose(ffIn);
220
221                 newImage->Initialize();
222
223
224                 int fd;
225                 long long ret;
226                 std::size_t found;
227                 std::string filename;
228                 found                                   =       bbGetInputFileName().find_last_of("/\\");
229                 filename                                = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
230                 long long pos;
231
232                 long long lsize                 = dimX*dimY*1 *dataSize;
233
234
235 #if defined(_WIN32)
236                    _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
237 #else
238                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
239 #endif // defined(_WIN32)
240
241
242
243                 if (fd < 0)
244                 {
245                         printf("EED ReadMHDPlane::Read64lseek \n");
246                         fprintf(stderr, "%s\n", strerror(errno));
247                         exit(1);
248                 }
249
250                 if (bbGetInputDirectionPlane()=="XY") 
251                 { 
252                         newImage->SetSpacing( spcX,spcY,spcZ );
253                         newImage->SetDimensions(  dimX,dimY,1 );
254                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,0 );
255                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,0 );
256                         newImage->SetNumberOfScalarComponents(1);
257                         newImage->AllocateScalars();
258                         newImage->Update();
259                         pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
260
261
262
263 #if defined(_WIN32)
264                         if (_lseeki64( fd, pos, SEEK_SET ) < 0)
265 #else
266                         if (lseek64(fd, pos, SEEK_SET) < 0) 
267 #endif // defined(_WIN32)
268
269                         {
270                                 printf("EED ReadMHDPlane::Read64lseek \n");
271                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
272                                 exit(1);
273                         }
274
275                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
276                         {
277                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
278                                 exit(1);
279                         }
280                 }  // if PLANE XY
281
282                 /*
283                 if (bbGetInputDirectionPlane()=="XZ") 
284                 { 
285                         long long int j;
286                         newImage->SetSpacing( spcX,spcZ,spcY );
287                         newImage->SetDimensions(  dimX,dimZ,1 );
288                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,0 );
289                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,0 );
290                         newImage->SetNumberOfScalarComponents(1);
291                         newImage->AllocateScalars();
292                         newImage->Update();
293                         long long sizeBytesPlane = dimX*dimY*dataSize;
294                         pos = dimX*(long long int)bbGetInputSlice()*dataSize;
295                         void *pImage = newImage->GetScalarPointer();
296                         for (j=0;j<dimZ;j++)
297                         {
298 printf("EED EED ReadMHDPlane::Read64lseek XZ j=%d \n", j);
299                                 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
300                                 {
301                                         printf("EED ReadMHDPlane::Read64lseek \n");
302                                         fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
303                                         exit(1);
304                                 }
305
306                                 if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
307                                 {
308                                         fprintf(stderr, "Failed reading: %s\n", strerror(errno));
309                                         exit(1);
310                                 }
311                                 pImage=pImage+dimX*dataSize;
312                         } // for j
313                 }  // if PLANE XZ
314
315
316                 if (bbGetInputDirectionPlane()=="YZ") 
317                 { 
318                         long long int j;
319                         long long int i;
320                         newImage->SetSpacing( spcY,spcZ,spcX );
321                         newImage->SetDimensions(  dimY,dimZ,1 );
322                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,0 );
323                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,0 );
324                         newImage->SetNumberOfScalarComponents(1);
325                         newImage->AllocateScalars();
326                         newImage->Update();
327                         long long sizeBytesPlane = dimX*dimY*dataSize;
328 //                      pos = dimX*(long long int)bbGetInputSlice()*dataSize;
329                         void *pImage;
330                         for (j=0;j<dimZ;j++)
331                         {
332 printf("EED EED ReadMHDPlane::Read64lseek YZ i=%d j=%d \n", i,j);
333                                 for (i=0;i<dimY;i++)
334                                 {
335                                         pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
336                                         if (lseek64(fd, pos  , SEEK_SET) < 0) 
337                                         {
338                                                 printf("EED ReadMHDPlane::Read64lseek \n");
339                                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
340                                                 exit(1);
341                                         }
342
343                                         pImage=newImage->GetScalarPointer(i, j,0 );
344                                         if ((ret = read(fd, pImage , dataSize)) < 0) 
345                                         {
346                                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
347                                                 exit(1);
348                                         }
349                                 }
350                         } // for j
351                 }  // if PLANE YZ
352                 */
353         
354 #if defined(_WIN32)
355                 _close (fd);
356 #else
357                 close (fd);
358 #endif // defined(_WIN32)
359
360                 bbSetOutputOut( newImage );
361         } // if
362
363
364 }
365
366
367 void ReadMHDPlane::Process()
368 {
369         if (bbGetInputType()==0)
370         {
371                 ReadNormalMHD();
372         }       
373
374         if (bbGetInputType()==1)
375         {
376                 Read64lseek();
377         }       
378
379 }
380 //===== 
381 // 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)
382 //===== 
383 void ReadMHDPlane::bbUserSetDefaultValues()
384 {
385
386 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
387 //    Here we initialize the input 'In' to 0
388    bbSetInputFileName("");
389    bbSetInputSlice(0);
390    bbSetInputType(1);
391    bbSetInputDirectionPlane("XY");
392   
393 }
394 //===== 
395 // 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)
396 //===== 
397 void ReadMHDPlane::bbUserInitializeProcessing()
398 {
399
400 //  THE INITIALIZATION METHOD BODY :
401 //    Here does nothing 
402 //    but this is where you should allocate the internal/output pointers 
403 //    if any 
404
405   
406 }
407 //===== 
408 // 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)
409 //===== 
410 void ReadMHDPlane::bbUserFinalizeProcessing()
411 {
412
413 //  THE FINALIZATION METHOD BODY :
414 //    Here does nothing 
415 //    but this is where you should desallocate the internal/output pointers 
416 //    if any
417   
418 }
419 }
420 // EO namespace bbcreaVtk
421
422