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>
18 #endif // defined(_WIN32)
29 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,ReadMHDPlane)
30 BBTK_BLACK_BOX_IMPLEMENTATION(ReadMHDPlane,bbtk::AtomicBlackBox);
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)
36 vtkImageData* ReadMHDPlane::CreateDefaultImage()
39 int sizeX, sizeY, sizeZ;
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();
53 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
54 for (i=0; i<sizeX; i++)
56 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
57 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
62 vtkImageData* ReadMHDPlane::ChangeOrientation(vtkImageData* imgOrg)
64 int width = bbGetInputWidth();
66 int sizeXOrg, sizeYOrg,sizeZOrg;
67 int sizeXDst, sizeYDst,sizeZDst;
68 imgOrg->GetWholeExtent(ext);
71 sizeXOrg = ext[1]-ext[0]+1;
72 sizeYOrg = ext[3]-ext[2]+1;
73 sizeZOrg = ext[5]-ext[4]+1;
75 if (bbGetInputDirectionPlane()=="XY")
77 sizeXDst = ext[1]-ext[0]+1;
78 sizeYDst = ext[3]-ext[2]+1;
81 if (bbGetInputDirectionPlane()=="YZ")
84 sizeYDst = ext[1]-ext[0]+1;
85 sizeZDst = ext[3]-ext[2]+1;
88 if (bbGetInputDirectionPlane()=="ZX")
90 sizeXDst = ext[1]-ext[0]+1;
92 sizeZDst = ext[3]-ext[2]+1;
95 vtkImageData *imgDst = vtkImageData::New();
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();
106 char *ptrDst,*ptrOrg;
107 int sizeBytes = imgOrg->GetScalarSize();
108 int sizeLineBytes = sizeLine*imgOrg->GetScalarSize();
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;
117 ptrOrg = (char*)( imgOrg->GetScalarPointer() );
119 if (bbGetInputDirectionPlane()=="XY")
121 memcpy ( imgDst->GetScalarPointer(), ptrOrg , sizeXDst*sizeYDst*sizeZDst*(imgOrg->GetScalarSize()) );
124 if (bbGetInputDirectionPlane()=="ZX")
126 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-00-1) );
127 for( zz=0 ; zz<sizeZOrg ; zz++)
129 // ptrDst=(char*)( imgDst->GetScalarPointer(0,zz,sizeYOrg-00-1) );
130 for( yy=0 ; yy<sizeYOrg ; yy++)
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;
138 ptrDst = ptrDst + sizeXDstBytes;
139 ptrDst = ptrDst + sizeXYZDstBytes;
143 if (bbGetInputDirectionPlane()=="YZ")
145 ptrDst = (char*)( imgDst->GetScalarPointer(0,0,sizeYOrg-0-1) );
146 for( zz=0 ; zz<sizeZOrg ; zz++)
148 ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-0-1) );
149 for( yy=0 ; yy<sizeYOrg ; yy++)
151 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,0,sizeYOrg-yy-1) );
152 for( xx=0 ; xx<sizeXOrg ; xx++)
154 // ptrOrg=(char*)( imgOrg->GetScalarPointer(xx,yy,zz) );
155 // ptrDst=(char*)( imgDst->GetScalarPointer(zz,xx,sizeYOrg-yy-1) );
156 memcpy ( ptrDst, ptrOrg , sizeBytes );
158 ptrDst+= sizeXDstBytes;
160 ptrDst = ptrDst - sizeXYDstBytes2;
162 ptrDst = ptrDst + sizeXYZDstBytes;
170 void ReadMHDPlane::Read64lseek(std::string fileNameIn, std::string plane)
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;
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;
189 FILE *ffIn = fopen( fileNameIn.c_str() , "r");
193 newImage = vtkImageData::New();
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); }
226 newImage->Initialize();
230 std::string filename;
231 found = fileNameIn.find_last_of("/\\");
232 filename = fileNameIn.substr(0,found+1) + elementdatafile ;
234 long long lsize = dimX*dimY*width *dataSize;
236 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
238 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
239 #endif // defined(_WIN32)
242 printf("EED ReadMHDPlane::Read64lseek WARNNING! raw file not exist\n");
243 fprintf(stderr, "%s\n", strerror(errno));
244 newImage=CreateDefaultImage();
247 if ((plane=="XY") && (fd>=0))
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();
256 pos = dimX*dimY*(long long)slice*dataSize;
258 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
260 if (lseek64(fd, pos, SEEK_SET) < 0)
261 #endif // defined(_WIN32)
263 printf("EED ReadMHDPlane::Read64lseek \n");
264 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
267 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
269 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
273 if ((plane=="ZX") && (fd>=0))
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();
283 for (iWidth=0;iWidth<width;iWidth++)
285 copy_ZX_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
288 if ((plane=="YZ") && (fd>=0))
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();
298 for (iWidth=0;iWidth<width;iWidth++)
300 copy_YZ_plane(fd,newImage,slice+iWidth,iWidth,dimX,dimY,dimZ,dataSize);
307 #endif // defined(_WIN32)
309 newImage=CreateDefaultImage();
311 bbSetOutputOut( newImage );
312 bbSetOutputOut2( ChangeOrientation(newImage) );
316 void ReadMHDPlane::copy_YZ_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
322 long long sizeBytesPlane = dimX*dimY*dataSize;
328 pos = ((long long int)slice+iWidth + i*dimX + j*dimX*dimY)*dataSize ;
330 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
332 if (lseek64(fd, pos , SEEK_SET) < 0)
333 #endif // defined(_WIN32)
335 printf("EED ReadMHDPlane::Read64lseek \n");
336 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
340 pImage=(char*)(newImage->GetScalarPointer(i, dimZ-1-j,iWidth ));
341 if ((ret = read(fd, pImage , dataSize)) < 0)
343 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
351 void ReadMHDPlane::copy_ZX_plane(int fd,vtkImageData *newImage,int slice,int iWidth,int dimX,int dimY,int dimZ,int dataSize)
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--)
362 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
364 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
365 #endif // defined(_WIN32)
367 printf("EED ReadMHDPlane::Read64lseek \n");
368 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
371 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
373 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
376 pImage = pImage+dimX*dataSize;
380 void ReadMHDPlane::Process()
382 if (bbGetInputActive()==true)
384 printf("EED ReadMHDPlane::Process %d \n", bbGetInputSlice() );
385 if (bbGetInputType()==0)
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);
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");
402 if (bbGetInputType()==1)
404 Read64lseek( bbGetInputFileName(), bbGetInputDirectionPlane() );
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)
412 void ReadMHDPlane::bbUserSetDefaultValues()
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("");
421 bbSetInputDirectionPlane("XY");
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)
427 void ReadMHDPlane::bbUserInitializeProcessing()
429 // THE INITIALIZATION METHOD BODY :
431 // but this is where you should allocate the internal/output pointers
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)
438 void ReadMHDPlane::bbUserFinalizeProcessing()
440 // THE FINALIZATION METHOD BODY :
442 // but this is where you should desallocate the internal/output pointers
446 } // EO namespace bbcreaVtk