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