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()
40 int sizeX, sizeY, sizeZ;
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 );
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();
58 newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
61 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
62 for (i=0; i<sizeX; i++)
64 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
65 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
70 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* imgOrg)
72 int width = bbGetInputWidth();
74 int sizeXOrg, sizeYOrg,sizeZOrg;
75 int sizeXDst, sizeYDst,sizeZDst;
76 imgOrg->GetWholeExtent(ext);
79 sizeXOrg = ext[1]-ext[0]+1;
80 sizeYOrg = ext[3]-ext[2]+1;
81 sizeZOrg = ext[5]-ext[4]+1;
83 if (bbGetInputDirectionPlane()=="XY")
85 sizeXDst = ext[1]-ext[0]+1;
86 sizeYDst = ext[3]-ext[2]+1;
89 if (bbGetInputDirectionPlane()=="YZ")
92 sizeYDst = ext[1]-ext[0]+1;
93 sizeZDst = ext[3]-ext[2]+1;
96 if (bbGetInputDirectionPlane()=="ZX")
98 sizeXDst = ext[1]-ext[0]+1;
100 sizeZDst = ext[3]-ext[2]+1;
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();
114 char *ptrDst,*ptrOrg;
115 int sizeBytes = imgOrg->GetScalarSize();
116 int sizeLineBytes = sizeLine*imgOrg->GetScalarSize();
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;
125 ptrOrg = (char*)( imgOrg->GetScalarPointer() );
127 if (bbGetInputDirectionPlane()=="XY")
129 memcpy ( imgDst->GetScalarPointer(), ptrOrg , sizeXDst*sizeYDst*sizeZDst*(imgOrg->GetScalarSize()) );
132 if (bbGetInputDirectionPlane()=="ZX")
134 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-00-1) );
135 for( zz=0 ; zz<sizeZOrg ; zz++)
137 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-00-1) );
138 for( yy=0 ; yy<sizeYOrg ; yy++)
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;
146 ptrDst = ptrDst + sizeXDstBytes;
147 ptrDst = ptrDst + sizeXYZDstBytes;
151 if (bbGetInputDirectionPlane()=="YZ")
153 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-0-1) );
154 for( zz=0 ; zz<sizeZOrg ; zz++)
156 ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-0-1) );
157 for( yy=0 ; yy<sizeYOrg ; yy++)
159 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-yy-1) );
160 for( xx=0 ; xx<sizeXOrg ; xx++)
162 // ptrOrg=(char*)( imgOrg->GetScalarPointer(xx,yy,zz) );
163 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,xx,sizeYOrg-yy-1) );
164 memcpy ( ptrDst, ptrOrg , sizeBytes );
166 ptrDst+= sizeXDstBytes;
168 ptrDst = ptrDst - sizeXYDstBytes2;
170 ptrDst = ptrDst + sizeXYZDstBytes;
178 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
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;
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;
200 FILE *ffIn = fopen( fileNameIn.c_str() , "r");
204 newImage = vtkImageData::New();
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; }
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); }
241 newImage->Initialize();
245 std::string filename;
246 found = fileNameIn.find_last_of("/\\");
247 filename = fileNameIn.substr(0,found+1) + elementdatafile ;
249 long long lsize = dimX*dimY*width *dataSize;
251 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
253 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
254 #endif // defined(_WIN32)
257 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
258 fprintf(stderr, "%s\n", strerror(errno));
259 newImage=CreateDefaultImage();
262 if ((plane=="XY") && (fd>=0))
264 newImage->SetSpacing( spcX,spcY,spcZ );
265 newImage->SetDimensions( dimX,dimY,width );
266 newImage->SetExtent(0, dimX-1,0,dimY-1,0,width-1 );
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();
276 newImage->AllocateScalars( imageType,1 );
280 pos = dimX*dimY*(long long)slice*dataSize;
282 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
284 if (lseek64(fd, pos, SEEK_SET) < 0)
285 #endif // defined(_WIN32)
287 printf("EED ReadMHDPlane::Read64lseek \n");
288 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
291 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
293 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
297 if ((plane=="ZX") && (fd>=0))
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();
310 newImage->AllocateScalars( imageType, 1);
314 for (iWidth=0;iWidth<width;iWidth++)
316 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
319 if ((plane=="YZ") && (fd>=0))
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();
332 newImage->AllocateScalars( imageType ,1 );
336 for (iWidth=0;iWidth<width;iWidth++)
338 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
345 #endif // defined(_WIN32)
347 newImage=CreateDefaultImage();
349 bbSetOutputOut( newImage );
350 bbSetOutputOut2( ChangeOrientation(newImage) );
354 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
360 long long sizeBytesPlane = dimX*dimY*dataSize;
366 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
368 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
370 if (lseek64(fd, pos , SEEK_SET) < 0)
371 #endif // defined(_WIN32)
373 printf("EED ReadMHDPlane::Read64lseek \n");
374 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
378 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
379 if ((ret = read(fd, pImage , dataSize)) < 0)
381 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
389 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
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--)
400 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
402 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
403 #endif // defined(_WIN32)
405 printf("EED ReadMHDPlane::Read64lseek \n");
406 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
409 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
411 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
414 pImage = pImage+dimX*dataSize;
418 void ReadMHDPlane::Process()
420 if (bbGetInputActive()==true)
422 if (bbGetInputType()==0)
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);
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");
439 if (bbGetInputType()==1)
441 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
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)
449 void ReadMHDPlane::bbUserSetDefaultValues()
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("");
458 bbSetInputDirectionPlane("XY");
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)
464 void ReadMHDPlane::bbUserInitializeProcessing()
466 // THE INITIALIZATION METHOD BODY :
468 // but this is where you should allocate the internal/output pointers
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)
475 void ReadMHDPlane::bbUserFinalizeProcessing()
477 // THE FINALIZATION METHOD BODY :
479 // but this is where you should desallocate the internal/output pointers
483 } // EO namespace bbcreaVtk