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 );
63 void ReadMHDPlane::ReadNormalMHD()
65 // THE MAIN PROCESSING METHOD BODY
66 // Here we simply set the input 'In' value to the output 'Out'
67 // And print out the output value
68 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
69 // void bbSet{Input|Output}NAME(const TYPE&)
70 // const TYPE& bbGet{Input|Output}NAME() const
72 // * NAME is the name of the input/output
73 // (the one provided in the attribute 'name' of the tag 'input')
74 // * TYPE is the C++ type of the input/output
75 // (the one provided in the attribute 'type' of the tag 'input')
77 // bbSetOutputOut( bbGetInputIn() );
78 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
80 std::string inputfilename;
82 if (bbGetInputDirectionPlane()=="XY")
84 inputfilename=bbGetInputFileName();
88 if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
92 std::size_t found = bbGetInputFileName().find_last_of("/\\");
93 std::string path = bbGetInputFileName().substr(0,found+1);
94 std::string filename = bbGetInputFileName().substr(found+1);
102 if (bbGetInputDirectionPlane()=="YZ")
104 inputfilename = path+filename+"_YZ.mhd";
107 if (bbGetInputDirectionPlane()=="ZX")
109 inputfilename = path+filename+"_ZX.mhd";
113 vtkImageData *newImage;
114 long long int newHeaderSize;
115 std::string newFileName=inputfilename+"-OneSlice";
121 FILE *ffIn = fopen(inputfilename.c_str(),"r");
124 FILE *ffOut = fopen(newFileName.c_str(),"w");
128 strcpy(mystring,"\n");
129 fgets(mystring,250,ffIn);
130 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
131 if (strncmp("DimSize",mystring,6)==0)
133 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
134 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
135 newHeaderSize = (long long int)sx*(long long int)sy*(long long int)bbGetInputSlice();
136 if (bbGetInputSlice()>=sz) {ok=false;}
138 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newHeaderSize=newHeaderSize*1; }
139 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newHeaderSize=newHeaderSize*1; }
140 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
141 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newHeaderSize=newHeaderSize*2; }
142 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; }
143 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; }
144 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
145 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
146 if (strncmp("ElementDataFile",mystring,15)==0)
148 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
150 fprintf(ffOut,mystring);
157 vtkMetaImageReader * reader = vtkMetaImageReader::New();
158 reader->SetFileName( newFileName.c_str() );
160 newImage = reader->GetOutput();
164 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
169 newImage=CreateDefaultImage();
172 bbSetOutputOut( newImage );
176 void ReadMHDPlane::Read64lseek()
179 int dimX=-1,dimY=-1,dimZ=-1;
181 std::string formattype("VOID");
182 std::string elementdatafile("VOID");
183 float spcX=-1,spcY=-1,spcZ=-1;
184 float ox=-1,oy=-1,oz=-1;
185 long int headersize=0;
187 vtkImageData *newImage=NULL;
192 FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r");
197 newImage = vtkImageData::New();
201 strcpy(mystring,"\n");
202 fgets(mystring,250,ffIn);
203 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
204 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
205 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
206 if (strncmp("ElementSpacing",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
207 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
208 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
209 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
211 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
212 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
213 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
214 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
215 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
216 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
217 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
218 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
219 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
220 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
221 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
222 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
227 newImage->Initialize();
233 std::string filename;
234 found = bbGetInputFileName().find_last_of("/\\");
235 filename = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
238 long long lsize = dimX*dimY*1 *dataSize;
242 _sopen_s( &fd, filename.c_str(), _O_RDONLY, _SH_DENYNO, 0 );
244 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
245 #endif // defined(_WIN32)
251 printf("EED ReadMHDPlane::Read64lseek \n");
252 fprintf(stderr, "%s\n", strerror(errno));
256 if (bbGetInputDirectionPlane()=="XY")
258 newImage->SetSpacing( spcX,spcY,spcZ );
259 newImage->SetDimensions( dimX,dimY,1 );
260 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,0 );
261 newImage->SetExtent(0, dimX-1,0,dimY-1,0,0 );
262 newImage->SetNumberOfScalarComponents(1);
263 newImage->AllocateScalars();
265 pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
267 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
269 if (lseek64(fd, pos, SEEK_SET) < 0)
270 #endif // defined(_WIN32)
272 printf("EED ReadMHDPlane::Read64lseek \n");
273 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
277 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
279 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
284 if (bbGetInputDirectionPlane()=="XZ")
287 newImage->SetSpacing( spcX,spcZ,spcY );
288 newImage->SetDimensions( dimX,dimZ,1 );
289 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,0 );
290 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,0 );
291 newImage->SetNumberOfScalarComponents(1);
292 newImage->AllocateScalars();
294 long long sizeBytesPlane = dimX*dimY*dataSize;
295 pos = dimX*(long long int)bbGetInputSlice()*dataSize;
296 char *pImage = (char*)(newImage->GetScalarPointer());
300 if (_lseeki64( fd, pos + j*sizeBytesPlane , SEEK_SET ) < 0)
302 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
303 #endif // defined(_WIN32)
305 printf("EED ReadMHDPlane::Read64lseek \n");
306 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
310 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
312 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
315 pImage = pImage+dimX*dataSize;
320 if (bbGetInputDirectionPlane()=="YZ")
324 newImage->SetSpacing( spcY,spcZ,spcX );
325 newImage->SetDimensions( dimY,dimZ,1 );
326 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,0 );
327 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,0 );
328 newImage->SetNumberOfScalarComponents(1);
329 newImage->AllocateScalars();
331 long long sizeBytesPlane = dimX*dimY*dataSize;
337 pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
339 if (_lseeki64( fd, pos , SEEK_SET ) < 0)
341 if (lseek64(fd, pos , SEEK_SET) < 0)
342 #endif // defined(_WIN32)
344 printf("EED ReadMHDPlane::Read64lseek \n");
345 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
349 pImage=(char*)(newImage->GetScalarPointer(i, j,0 ));
350 if ((ret = read(fd, pImage , dataSize)) < 0)
352 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
362 #endif // defined(_WIN32)
364 newImage=CreateDefaultImage();
367 bbSetOutputOut( newImage );
372 void ReadMHDPlane::Process()
374 if (bbGetInputType()==0)
379 if (bbGetInputType()==1)
386 // 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)
388 void ReadMHDPlane::bbUserSetDefaultValues()
391 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
392 // Here we initialize the input 'In' to 0
393 bbSetInputFileName("");
396 bbSetInputDirectionPlane("XY");
400 // 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)
402 void ReadMHDPlane::bbUserInitializeProcessing()
405 // THE INITIALIZATION METHOD BODY :
407 // but this is where you should allocate the internal/output pointers
413 // 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)
415 void ReadMHDPlane::bbUserFinalizeProcessing()
418 // THE FINALIZATION METHOD BODY :
420 // but this is where you should desallocate the internal/output pointers
425 // EO namespace bbcreaVtk