]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkReadMHDPlane.cxx
#3223 creaVtk Feature New Normal - vtk8itk4wx3-mingw64
[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         #include <share.h> 
17 #else
18         #include <unistd.h>
19 #endif // defined(_WIN32)
20
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26
27 namespace bbcreaVtk
28 {
29
30 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
31 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
32 //===== 
33 // 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)
34 //===== 
35
36
37 vtkImageData* ReadMHDPlane::CreateDefaultImage()
38 {
39         int i;
40         int sizeX, sizeY, sizeZ;
41         sizeX = 200;
42         sizeY = sizeX;
43         sizeZ = 1;
44         vtkImageData *newImage = vtkImageData::New();
45         newImage->Initialize();
46         newImage->SetSpacing( 1,1,1 );
47         newImage->SetDimensions(  sizeX,sizeY,sizeZ );
48         newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
49
50 //EED 2017-01-01 Migration VTK7
51 #if VTK_MAJOR_VERSION <= 5
52         newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
53         newImage->SetScalarTypeToUnsignedChar();
54         newImage->SetNumberOfScalarComponents(1);
55         newImage->AllocateScalars();
56         newImage->Update();
57 #else
58         newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
59 #endif
60
61         memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
62         for (i=0; i<sizeX; i++)
63         {
64                 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
65                 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
66         } // for i
67         return newImage;
68
69
70 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* imgOrg)
71 {
72         int     width = bbGetInputWidth();
73         int     ext[6];
74         int     sizeXOrg, sizeYOrg,sizeZOrg;
75         int     sizeXDst, sizeYDst,sizeZDst;
76         imgOrg->GetWholeExtent(ext);
77         int     sizeLine;
78         
79         sizeXOrg        = ext[1]-ext[0]+1;
80         sizeYOrg        = ext[3]-ext[2]+1;
81         sizeZOrg        = ext[5]-ext[4]+1;
82
83         if (bbGetInputDirectionPlane()=="XY")
84         {
85                 sizeXDst        = ext[1]-ext[0]+1;
86                 sizeYDst        = ext[3]-ext[2]+1;
87                 sizeZDst        = width;
88         } // XY 
89         if (bbGetInputDirectionPlane()=="YZ") 
90         {
91                 sizeXDst        = width;
92                 sizeYDst        = ext[1]-ext[0]+1;
93                 sizeZDst        = ext[3]-ext[2]+1;
94                 sizeLine        = sizeYDst;
95         } // YZ
96         if (bbGetInputDirectionPlane()=="ZX")
97         {
98                 sizeXDst        = ext[1]-ext[0]+1;
99                 sizeYDst        = width;
100                 sizeZDst        = ext[3]-ext[2]+1;
101                 sizeLine        = sizeXDst;
102         } // ZX
103         vtkImageData *imgDst  = vtkImageData::New();
104         imgDst->Initialize();
105         imgDst->SetScalarType( imgOrg->GetScalarType() );
106         imgDst->SetSpacing( imgOrg->GetSpacing() );
107         imgDst->SetDimensions( sizeXDst,sizeYDst,sizeZDst );
108         imgDst->SetWholeExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
109         imgDst->SetExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
110         imgDst->SetNumberOfScalarComponents(1);
111         imgDst->AllocateScalars();
112         imgDst->Update();
113
114         char *ptrDst,*ptrOrg;
115         int sizeBytes = imgOrg->GetScalarSize();                
116         int sizeLineBytes = sizeLine*imgOrg->GetScalarSize();           
117         int xx,yy,zz;
118
119         long int sizeXDstBytes          = sizeXDst*sizeBytes;
120         long int sizeXYDstBytes         = sizeXDst*sizeYDst*sizeBytes;
121         long int sizeXYDstBytes2        = sizeXDst*sizeYDst*sizeBytes*2;
122         long int sizeXYZDstBytes        = sizeXDst*sizeYDst*sizeZDst*sizeBytes;
123         long int sizeXYZDstBytes1       = sizeXDst*sizeYDst*(sizeZDst-1)*sizeBytes;
124
125         ptrOrg = (char*)( imgOrg->GetScalarPointer() );
126
127         if (bbGetInputDirectionPlane()=="XY")
128         {
129                 memcpy ( imgDst->GetScalarPointer(), ptrOrg , sizeXDst*sizeYDst*sizeZDst*(imgOrg->GetScalarSize()) );
130         } // if XY
131
132         if (bbGetInputDirectionPlane()=="ZX")
133         {
134                 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-00-1) );
135                 for( zz=0 ; zz<sizeZOrg ; zz++)
136                 {
137 //                      ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-00-1) );
138                         for( yy=0 ; yy<sizeYOrg ; yy++)
139                         {
140 //                              ptrOrg=(char*)( imgOrg->GetScalarPointer(0,yy,zz) );
141 //                              ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-yy-1) );
142                                 memcpy ( ptrDst, ptrOrg , sizeLineBytes );
143                                 ptrOrg = ptrOrg + sizeLineBytes;
144                                 ptrDst = ptrDst - sizeXYDstBytes;
145                         } // for yy 
146                         ptrDst = ptrDst + sizeXDstBytes;
147                         ptrDst = ptrDst + sizeXYZDstBytes;
148                 } // for zz 
149         } // ZX
150
151         if (bbGetInputDirectionPlane()=="YZ")
152         {
153                 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-0-1) );
154                 for( zz=0 ; zz<sizeZOrg ; zz++)
155                 {
156                         ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-0-1) );
157                         for( yy=0 ; yy<sizeYOrg ; yy++)
158                         {
159 //                              ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-yy-1) );
160                                 for( xx=0 ; xx<sizeXOrg ; xx++)
161                                 {
162 //                                      ptrOrg=(char*)( imgOrg->GetScalarPointer(xx,yy,zz) );
163 //                                      ptrDst=(char*)( imgDst->GetScalarPointer(zz,xx,sizeYOrg-yy-1) );
164                                         memcpy ( ptrDst, ptrOrg , sizeBytes );
165                                         ptrOrg+= sizeBytes;
166                                         ptrDst+= sizeXDstBytes;
167                                 } /// for xx
168                                 ptrDst = ptrDst - sizeXYDstBytes2;
169                         } // for yy 
170                         ptrDst = ptrDst + sizeXYZDstBytes;
171 //                      ptrDst++;
172                 } // for zz 
173         } // ZX
174
175         return imgDst;
176 }
177
178 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
179 {
180         int             slice;
181         int             width;
182         width = bbGetInputWidth();
183         if (width<=0 ) { width=1; }
184         slice = bbGetInputSlice();
185         if (slice<0 ) { slice=0; }
186         int             dimX=-1,dimY=-1,dimZ=-1;
187         int             dim=-1;
188         std::string formattype("VOID");
189         std::string elementdatafile("VOID");
190         float           spcX=-1,spcY=-1,spcZ=-1;
191         float           ox=-1,oy=-1,oz=-1;
192         long int        headersize=0;
193         vtkImageData *newImage=NULL;
194
195         int imageType;
196
197         char mystring[250];
198         char strTmp[30];
199         char strTmp2[30];
200         FILE *ffIn      = fopen( fileNameIn.c_str() , "r");
201         long long dataSize;
202         if (ffIn!=NULL)
203         {
204                 newImage = vtkImageData::New();
205                 while(!feof(ffIn))
206                 {
207             strcpy(mystring,"\n");
208                         fgets(mystring,250,ffIn);
209                 if (strncmp("NDims",mystring,5)==0)                     { sscanf(mystring,"%s %s %d"            ,strTmp, strTmp, &dim);                                                                 }
210                 if (strncmp("DimSize",mystring,6)==0)                   { sscanf(mystring,"%s %s %d %d %d"      ,strTmp, strTmp, &dimX, &dimY,&dimZ);                                   } 
211                         if (strncmp("ElementType",mystring,11)==0)              { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); formattype=strTmp2;                  }
212                         if (strncmp("ElementSpacing",mystring,14)==0)   { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);                                    }
213                         if (strncmp("ElementSize",mystring,11)==0)              { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &spcX,&spcY,&spcZ);                                    }
214                 if (strncmp("Offset",mystring,6)==0)                    { sscanf(mystring,"%s %s %f %f %f"      ,strTmp, strTmp, &ox, &oy, &oz);                                                }
215                 if (strncmp("HeaderSize",mystring,10)==0)               { sscanf(mystring,"%s %s %ld"           ,strTmp, strTmp, &headersize);                                                  }
216                         if (strncmp("ElementDataFile",mystring,15)==0)  { sscanf(mystring,"%s %s %s"            ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2;             }
217
218                         
219
220                         if (strncmp("ElementType = MET_CHAR",mystring,22)==0)                   { imageType =VTK_CHAR;                   dataSize=sizeof(char);                  }
221                         if (strncmp("ElementType = VTK_CHAR",mystring,22)==0)                   { imageType =VTK_CHAR;                   dataSize=sizeof(char);                  }
222                         if (strncmp("ElementType = MET_UCHAR",mystring,23)==0)                  { imageType =VTK_UNSIGNED_CHAR;  dataSize=sizeof(unsigned char); }
223                         if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0)  { imageType =VTK_UNSIGNED_CHAR;  dataSize=sizeof(unsigned char); }
224                         if (strncmp("ElementType = MET_USHORT",mystring,24)==0)                 { imageType =VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
225                         if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { imageType =VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
226                         if (strncmp("ElementType = MET_SHORT",mystring,23)==0)                  { imageType =VTK_SHORT;                  dataSize=sizeof(short);                 }
227                         if (strncmp("ElementType = VTK_SHORT",mystring,23)==0)                  { imageType =VTK_SHORT;                  dataSize=sizeof(short);                 }
228                         if (strncmp("ElementType = MET_UINT",mystring,22)==0)                   { imageType =VTK_UNSIGNED_INT;   dataSize=sizeof(unsigned int);  }
229                         if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0)   { imageType =VTK_UNSIGNED_INT;   dataSize=sizeof(unsigned int);  }
230                         if (strncmp("ElementType = MET_INT",mystring,21)==0)                    { imageType =VTK_INT;                    dataSize=sizeof(int);                   }
231                         if (strncmp("ElementType = VTK_INT",mystring,21)==0)                    { imageType =VTK_INT;                    dataSize=sizeof(int);                   }
232                         if (strncmp("ElementType = MET_FLOAT",mystring,23)==0)                  { imageType =VTK_FLOAT;                  dataSize=sizeof(float);                 }
233                         if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0)                  { imageType =VTK_FLOAT;                  dataSize=sizeof(float);                 }
234                         if (strncmp("ElementType = MET_LONG",mystring,22)==0)                   { imageType =VTK_LONG;                   dataSize=sizeof(long);                  }
235                         if (strncmp("ElementType = VTK_LONG",mystring,22)==0)                   { imageType =VTK_LONG;                   dataSize=sizeof(long);                  }
236                         if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0)                 { imageType =VTK_DOUBLE;                 dataSize=sizeof(double);                }
237                         if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0)                 { imageType =VTK_DOUBLE;                 dataSize=sizeof(double);                }
238
239         } // while
240                 fclose(ffIn);
241                 newImage->Initialize();
242                 int             fd;
243                 long long       ret;
244                 std::size_t found;
245                 std::string filename;
246                 found                                   = fileNameIn.find_last_of("/\\");
247                 filename                                = fileNameIn.substr(0,found+1) + elementdatafile ;
248                 long long       pos;
249                 long long lsize                 = dimX*dimY*width *dataSize;
250 #if defined(_WIN32)
251                    _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
252 #else
253                 fd = open ( filename.c_str() ,  O_RDONLY|O_LARGEFILE );
254 #endif // defined(_WIN32)
255                 if (fd < 0)
256                 {
257                         printf("EED ReadMHDPlane::Read64lseek   WARNNING! raw file not exist\n");
258                         fprintf(stderr, "%s\n", strerror(errno));
259                         newImage=CreateDefaultImage();
260 //                      exit(1);
261                 }       
262                 if ((plane=="XY") && (fd>=0)) 
263                 { 
264                         newImage->SetSpacing( spcX,spcY,spcZ );
265                         newImage->SetDimensions(  dimX,dimY,width );
266                         newImage->SetExtent(0,  dimX-1,0,dimY-1,0,width-1 );
267
268 //EED 2017-01-01 Migration VTK7
269 #if VTK_MAJOR_VERSION <= 5
270                         newImage->SetWholeExtent(0,  dimX-1,0,dimY-1,0,width-1 );
271                         newImage->SetNumberOfScalarComponents(1);
272                         newImage->SetScalarType(imageType);
273                         newImage->AllocateScalars();
274                         newImage->Update();
275 #else
276                         newImage->AllocateScalars( imageType,1 );
277 #endif
278
279
280                         pos = dimX*dimY*(long long)slice*dataSize;
281 #if defined(_WIN32)
282                         if (_lseeki64( fd, pos, SEEK_SET ) < 0)
283 #else
284                         if (lseek64(fd, pos, SEEK_SET) < 0) 
285 #endif // defined(_WIN32)
286                         {
287                                 printf("EED ReadMHDPlane::Read64lseek \n");
288                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
289                                 exit(1);
290                         }
291                         if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0) 
292                         {
293                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
294                                 exit(1);
295                         }
296                 }  // if PLANE XY
297                 if ((plane=="ZX") && (fd>=0))
298                 { 
299                         newImage->SetSpacing( spcX,spcZ,spcY );
300                         newImage->SetDimensions(  dimX,dimZ,width );
301                         newImage->SetExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
302 //EED 2017-01-01 Migration VTK7
303 #if VTK_MAJOR_VERSION <= 5
304                         newImage->SetWholeExtent(0,  dimX-1,0,dimZ-1,0,width-1 );
305                         newImage->SetNumberOfScalarComponents(1);
306                         newImage->SetScalarType(imageType);
307                         newImage->AllocateScalars();
308                         newImage->Update();
309 #else
310                         newImage->AllocateScalars( imageType, 1);
311 #endif
312
313                         int iWidth;
314                         for (iWidth=0;iWidth<width;iWidth++)
315                         {
316                                 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
317                         }
318                 }  // if PLANE XZ
319                 if ((plane=="YZ") && (fd>=0))
320                 { 
321                         newImage->SetSpacing( spcY,spcZ,spcX );
322                         newImage->SetDimensions(  dimY,dimZ,width );
323                         newImage->SetExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
324 //EED 2017-01-01 Migration VTK7
325 #if VTK_MAJOR_VERSION <= 5
326                         newImage->SetWholeExtent(0,  dimY-1,0,dimZ-1,0,width-1 );
327                         newImage->SetNumberOfScalarComponents(1);
328                         newImage->SetScalarType(imageType);
329                         newImage->AllocateScalars();
330                         newImage->Update();
331 #else
332                         newImage->AllocateScalars( imageType ,1 );
333 #endif
334
335                         int iWidth;
336                         for (iWidth=0;iWidth<width;iWidth++)
337                         {
338                                 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
339                         }
340                 }  // if PLANE YZ       
341 #if defined(_WIN32)
342                 _close (fd);
343 #else
344                 close (fd);
345 #endif // defined(_WIN32)
346         } else {
347                 newImage=CreateDefaultImage();
348         } // if  ffIn
349         bbSetOutputOut( newImage );
350         bbSetOutputOut2( ChangeOrientation(newImage) );
351 }
352
353
354 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
355 {
356         long long int j;
357         long long int i;
358         long long       ret;
359         long long       pos;
360         long long sizeBytesPlane = dimX*dimY*dataSize;
361         char *pImage;
362         for (j=0;j<dimZ;j++)
363         {
364                 for (i=0;i<dimY;i++)
365                 {
366                         pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
367 #if defined(_WIN32)
368                         if (_lseeki64( fd, pos , SEEK_SET ) < 0)
369 #else
370                         if (lseek64(fd, pos , SEEK_SET) < 0) 
371 #endif // defined(_WIN32)
372                         {
373                                 printf("EED ReadMHDPlane::Read64lseek \n");
374                                 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
375                                 exit(1);
376                         }
377
378                         pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
379                         if ((ret = read(fd, pImage , dataSize)) < 0) 
380                         {
381                                 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
382                                 exit(1);
383                         }
384                         } // for i
385                 } // for j
386 }
387
388
389 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
390 {
391         long long int j;
392         long long       ret;
393         long long       pos;
394         pos = dimX*(long long int)slice*dataSize;
395         long long sizeBytesPlane = dimX*dimY*dataSize;
396         char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
397         for (j=dimZ-1;j>=0;j--)
398         {
399 #if defined(_WIN32)
400                 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
401 #else
402                 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0) 
403 #endif // defined(_WIN32)
404                 {
405                         printf("EED ReadMHDPlane::Read64lseek \n");
406                         fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
407                         exit(1);
408                 }
409                 if ((ret = read(fd, pImage , dimX*dataSize)) < 0) 
410                 {
411                         fprintf(stderr, "Failed reading: %s\n", strerror(errno));
412                         exit(1);
413                 }
414                 pImage = pImage+dimX*dataSize;
415         } // for j
416 }
417
418 void ReadMHDPlane::Process()
419 {
420         if (bbGetInputActive()==true)
421         {
422                 if (bbGetInputType()==0)
423                 {
424                         //-- Split FileName
425                         std::string inputfilename;
426                         std::size_t found               =       bbGetInputFileName().find_last_of("/\\");
427                         std::string path                =       bbGetInputFileName().substr(0,found+1);
428                         std::string filename    =       bbGetInputFileName().substr(found+1);
429         #ifdef _WIN32
430                         path=path+"YZ_ZX\\";
431         #else
432                         path=path+"YZ_ZX/";
433         #endif
434                         if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName();           } // if XY
435                         if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd";        } // if YZ
436                         if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd";        } // if XZ
437                         Read64lseek( inputfilename ,"XY");
438                 }       
439                 if (bbGetInputType()==1)
440                 {
441                         Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
442                 }       
443         } // if active
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 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
452 //    Here we initialize the input 'In' to 0
453    bbSetInputActive(true);
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 //  THE INITIALIZATION METHOD BODY :
467 //    Here does nothing 
468 //    but this is where you should allocate the internal/output pointers 
469 //    if any 
470 }
471
472 //===== 
473 // 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)
474 //===== 
475 void ReadMHDPlane::bbUserFinalizeProcessing()
476 {
477 //  THE FINALIZATION METHOD BODY :
478 //    Here does nothing 
479 //    but this is where you should desallocate the internal/output pointers 
480 //    if any 
481 }
482
483 } // EO namespace bbcreaVtk
484
485