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)
4 #include "bbcreaVtkReadMHDPlane.h"
5 #include "bbcreaVtkPackage.h"
10 #define _LARGEFILE64_SOURCE
13 #include <sys/types.h>
19 #endif // defined(_WIN32)
30 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
31 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
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)
37 vtkImageData* ReadMHDPlane::CreateDefaultImage()
39 vtkImageData *newImage = NULL;
41 newImage = vtkImageData::New();
43 int sizeX, sizeY, sizeZ;
47 vtkImageData *newImage = vtkImageData::New();
48 newImage->Initialize();
49 newImage->SetSpacing( 1,1,1 );
50 newImage->SetDimensions( sizeX,sizeY,sizeZ );
51 newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
52 //EED 2017-01-01 Migration VTK7
53 #if VTK_MAJOR_VERSION <= 5
54 newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
55 newImage->SetScalarTypeToUnsignedChar();
56 newImage->SetNumberOfScalarComponents(1);
57 newImage->AllocateScalars();
60 // newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
61 newImage->AllocateScalars(VTK_SHORT,1);
63 // memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*sizeZ*sizeof(unsigned char) );
64 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*sizeof(short) );
65 // for (i=0; i<sizeX; i++)
67 // newImage->SetScalarComponentFromDouble(i,i ,0, 0, 255 );
68 // newImage->SetScalarComponentFromDouble(i,sizeY-1-i ,0, 0, 255 );
75 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* imgOrg)
77 vtkImageData *imgDst = NULL;
83 int sizeXOrg, sizeYOrg,sizeZOrg;
84 int sizeXDst, sizeYDst,sizeZDst;
85 //EED 2017-01-01 Migration VTK7
86 #if VTK_MAJOR_VERSION <= 5
87 imgOrg->GetWholeExtent(ext);
89 imgOrg->GetExtent(ext);
93 sizeXOrg = ext[1]-ext[0]+1;
94 sizeYOrg = ext[3]-ext[2]+1;
95 sizeZOrg = ext[5]-ext[4]+1;
98 if (bbGetInputDirectionPlane()=="XY")
100 sizeXDst = ext[1]-ext[0]+1;
101 sizeYDst = ext[3]-ext[2]+1;
104 if (bbGetInputDirectionPlane()=="YZ")
107 sizeYDst = ext[1]-ext[0]+1;
108 sizeZDst = ext[3]-ext[2]+1;
111 if (bbGetInputDirectionPlane()=="ZX")
113 sizeXDst = ext[1]-ext[0]+1;
115 sizeZDst = ext[3]-ext[2]+1;
118 imgDst = vtkImageData::New();
119 imgDst->Initialize();
120 imgDst->SetSpacing( imgOrg->GetSpacing() );
121 imgDst->SetDimensions( sizeXDst,sizeYDst,sizeZDst );
122 imgDst->SetExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
124 //EED 2017-01-01 Migration VTK7
125 #if VTK_MAJOR_VERSION <= 5
126 imgDst->SetWholeExtent(0,sizeXDst-1,0,sizeYDst-1,0,sizeZDst-1 );
127 imgDst->SetScalarType( imgOrg->GetScalarType() );
128 imgDst->SetNumberOfScalarComponents(1);
129 imgDst->AllocateScalars();
132 imgDst->AllocateScalars(imgOrg->GetScalarType(),1);
135 char *ptrDst,*ptrOrg;
136 int sizeBytes = imgOrg->GetScalarSize();
137 int sizeLineBytes = sizeLine*imgOrg->GetScalarSize();
140 long int sizeXDstBytes = sizeXDst*sizeBytes;
141 long int sizeXYDstBytes = sizeXDst*sizeYDst*sizeBytes;
142 long int sizeXYDstBytes2 = sizeXDst*sizeYDst*sizeBytes*2;
143 long int sizeXYZDstBytes = sizeXDst*sizeYDst*sizeZDst*sizeBytes;
144 long int sizeXYZDstBytes1 = sizeXDst*sizeYDst*(sizeZDst-1)*sizeBytes;
146 ptrOrg = (char*)( imgOrg->GetScalarPointer() );
148 if (bbGetInputDirectionPlane()=="XY")
150 memcpy ( imgDst->GetScalarPointer(), ptrOrg , sizeXDst*sizeYDst*sizeZDst*(imgOrg->GetScalarSize()) );
153 if (bbGetInputDirectionPlane()=="ZX")
155 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-00-1) );
156 for( zz=0 ; zz<sizeZOrg ; zz++)
158 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-00-1) );
159 for( yy=0 ; yy<sizeYOrg ; yy++)
161 // ptrOrg=(char*)( imgOrg->GetScalarPointer(0,yy,zz) );
162 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-yy-1) );
163 memcpy ( ptrDst, ptrOrg , sizeLineBytes );
164 ptrOrg = ptrOrg + sizeLineBytes;
165 ptrDst = ptrDst - sizeXYDstBytes;
167 ptrDst = ptrDst + sizeXDstBytes;
168 ptrDst = ptrDst + sizeXYZDstBytes;
172 if (bbGetInputDirectionPlane()=="YZ")
174 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-0-1) );
175 for( zz=0 ; zz<sizeZOrg ; zz++)
177 ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-0-1) );
178 for( yy=0 ; yy<sizeYOrg ; yy++)
180 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-yy-1) );
181 for( xx=0 ; xx<sizeXOrg ; xx++)
183 // ptrOrg=(char*)( imgOrg->GetScalarPointer(xx,yy,zz) );
184 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,xx,sizeYOrg-yy-1) );
185 memcpy ( ptrDst, ptrOrg , sizeBytes );
187 ptrDst+= sizeXDstBytes;
189 ptrDst = ptrDst - sizeXYDstBytes2;
191 ptrDst = ptrDst + sizeXYZDstBytes;
199 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
204 width = bbGetInputWidth();
205 if (width<=0 ) { width=1; }
206 slice = bbGetInputSlice();
207 if (slice<0 ) { slice=0; }
208 int dimX=-1,dimY=-1,dimZ=-1;
210 std::string formattype("VOID");
211 std::string elementdatafile("VOID");
212 float spcX=-1,spcY=-1,spcZ=-1;
213 float ox=-1,oy=-1,oz=-1;
214 long int headersize=0;
215 vtkImageData *newImage=NULL;
219 FILE *ffIn = fopen( fileNameIn.c_str() , "r");
223 newImage = vtkImageData::New();
226 strcpy(mystring,"\n");
227 fgets(mystring,250,ffIn);
228 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
229 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
230 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
231 if (strncmp("ElementSpacing",mystring,14)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
232 if (strncmp("ElementSize",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
233 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
234 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
235 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
236 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { imageType=VTK_CHAR; dataSize=sizeof(char); }
237 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { imageType=VTK_CHAR; dataSize=sizeof(char); }
238 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { imageType=VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
239 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { imageType=VTK_UNSIGNED_CHAR; dataSize=sizeof(unsigned char); }
240 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { imageType=VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
241 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { imageType=VTK_UNSIGNED_SHORT; dataSize=sizeof(unsigned short);}
242 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { imageType=VTK_SHORT; dataSize=sizeof(short); }
243 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { imageType=VTK_SHORT; dataSize=sizeof(short); }
244 if (strncmp("ElementType = MET_UINT",mystring,22)==0) { imageType=VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
245 if (strncmp("ElementType = VTK_UNSIGNED_INT",mystring,30)==0) { imageType=VTK_UNSIGNED_INT; dataSize=sizeof(unsigned int); }
246 if (strncmp("ElementType = MET_INT",mystring,21)==0) { imageType=VTK_INT; dataSize=sizeof(int); }
247 if (strncmp("ElementType = VTK_INT",mystring,21)==0) { imageType=VTK_INT; dataSize=sizeof(int); }
248 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { imageType=VTK_FLOAT; dataSize=sizeof(float); }
249 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { imageType=VTK_FLOAT; dataSize=sizeof(float); }
250 if (strncmp("ElementType = MET_LONG",mystring,22)==0) { imageType=VTK_LONG; dataSize=sizeof(long); }
251 if (strncmp("ElementType = VTK_LONG",mystring,22)==0) { imageType=VTK_LONG; dataSize=sizeof(long); }
252 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { imageType=VTK_DOUBLE; dataSize=sizeof(double); }
253 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { imageType=VTK_DOUBLE; dataSize=sizeof(double); }
256 newImage->Initialize();
260 std::string filename;
261 found = fileNameIn.find_last_of("/\\");
262 filename = fileNameIn.substr(0,found+1) + elementdatafile ;
264 long long lsize = dimX*dimY*width *dataSize;
266 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
270 fd = open ( filename.c_str() , O_RDONLY );
272 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
274 #endif // defined(_WIN32)
277 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
278 fprintf(stderr, "%s\n", strerror(errno));
279 newImage=CreateDefaultImage();
282 if ((plane=="XY") && (fd>=0))
284 newImage->SetSpacing( spcX,spcY,spcZ );
285 newImage->SetDimensions( dimX,dimY,width );
286 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
290 //EED 2017-01-01 Migration VTK7
291 #if VTK_MAJOR_VERSION <= 5
292 newImage->SetScalarType( imageType );
293 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,width-1 );
294 newImage->SetNumberOfScalarComponents(1);
295 newImage->AllocateScalars();
298 newImage->AllocateScalars(imageType,1);
301 pos = dimX*dimY*(long long)slice*dataSize;
303 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
307 if (lseek(fd, pos, SEEK_SET) < 0)
309 if (lseek64(fd, pos, SEEK_SET) < 0)
311 #endif // defined(_WIN32)
313 printf("EED ReadMHDPlane::Read64lseek \n");
314 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
317 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
319 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
323 if ((plane=="ZX") && (fd>=0))
325 newImage->SetSpacing( spcX,spcZ,spcY );
326 newImage->SetDimensions( dimX,dimZ,width );
327 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,width-1 );
330 //EED 2017-01-01 Migration VTK7
331 #if VTK_MAJOR_VERSION <= 5
332 newImage->SetScalarType( imageType );
333 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,width-1 );
334 newImage->SetNumberOfScalarComponents(1);
335 newImage->AllocateScalars();
338 newImage->AllocateScalars(imageType,1);
342 for (iWidth=0;iWidth<width;iWidth++)
344 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
347 if ((plane=="YZ") && (fd>=0))
349 newImage->SetSpacing( spcY,spcZ,spcX );
350 newImage->SetDimensions( dimY,dimZ,width );
351 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,width-1 );
353 //EED 2017-01-01 Migration VTK7
354 #if VTK_MAJOR_VERSION <= 5
355 newImage->SetScalarType( imageType );
356 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,width-1 );
357 newImage->SetNumberOfScalarComponents(1);
358 newImage->AllocateScalars();
361 newImage->AllocateScalars(imageType,1);
365 for (iWidth=0;iWidth<width;iWidth++)
367 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
374 #endif // defined(_WIN32)
376 newImage=CreateDefaultImage();
378 bbSetOutputOut( newImage );
379 bbSetOutputOut2( ChangeOrientation(newImage) );
383 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
389 long long sizeBytesPlane = dimX*dimY*dataSize;
395 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
397 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
401 if (lseek(fd, pos , SEEK_SET) < 0)
403 if (lseek64(fd, pos , SEEK_SET) < 0)
405 #endif // defined(_WIN32)
407 printf("EED ReadMHDPlane::Read64lseek \n");
408 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
412 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
413 if ((ret = read(fd, pImage , dataSize)) < 0)
415 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
423 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
428 pos = dimX*(long long int)slice*dataSize;
429 long long sizeBytesPlane = dimX*dimY*dataSize;
430 char *pImage = (char*)( newImage->GetScalarPointer(0,0,iWidth) );
431 for (j=dimZ-1;j>=0;j--)
434 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
438 if (lseek(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
440 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
442 #endif // defined(_WIN32)
444 printf("EED ReadMHDPlane::Read64lseek \n");
445 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
448 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
450 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
453 pImage = pImage+dimX*dataSize;
457 void ReadMHDPlane::Process()
459 if (bbGetInputActive()==true)
461 if (bbGetInputType()==0)
464 std::string inputfilename;
465 std::size_t found = bbGetInputFileName().find_last_of("/\\");
466 std::string path = bbGetInputFileName().substr(0,found+1);
467 std::string filename = bbGetInputFileName().substr(found+1);
473 if (bbGetInputDirectionPlane()=="XY") { inputfilename = bbGetInputFileName(); } // if XY
474 if (bbGetInputDirectionPlane()=="YZ") { inputfilename = path+filename+"_YZ.mhd"; } // if YZ
475 if (bbGetInputDirectionPlane()=="ZX") { inputfilename = path+filename+"_ZX.mhd"; } // if XZ
476 Read64lseek( inputfilename ,"XY");
478 if (bbGetInputType()==1)
480 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
486 // 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)
488 void ReadMHDPlane::bbUserSetDefaultValues()
490 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
491 // Here we initialize the input 'In' to 0
492 bbSetInputActive(true);
493 bbSetInputFileName("");
497 bbSetInputDirectionPlane("XY");
501 // 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)
503 void ReadMHDPlane::bbUserInitializeProcessing()
505 // THE INITIALIZATION METHOD BODY :
507 // but this is where you should allocate the internal/output pointers
512 // 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)
514 void ReadMHDPlane::bbUserFinalizeProcessing()
516 // THE FINALIZATION METHOD BODY :
518 // but this is where you should desallocate the internal/output pointers
522 } // EO namespace bbcreaVtk