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"
15 #define _LARGEFILE64_SOURCE
18 #include <sys/types.h>
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)
35 void ReadMHDPlane::ReadNormalMHD()
37 // THE MAIN PROCESSING METHOD BODY
38 // Here we simply set the input 'In' value to the output 'Out'
39 // And print out the output value
40 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
41 // void bbSet{Input|Output}NAME(const TYPE&)
42 // const TYPE& bbGet{Input|Output}NAME() const
44 // * NAME is the name of the input/output
45 // (the one provided in the attribute 'name' of the tag 'input')
46 // * TYPE is the C++ type of the input/output
47 // (the one provided in the attribute 'type' of the tag 'input')
49 // bbSetOutputOut( bbGetInputIn() );
50 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
52 std::string inputfilename;
54 if (bbGetInputDirectionPlane()=="XY")
56 inputfilename=bbGetInputFileName();
60 if ((bbGetInputDirectionPlane()=="YZ") || (bbGetInputDirectionPlane()=="ZX"))
64 std::size_t found = bbGetInputFileName().find_last_of("/\\");
65 std::string path = bbGetInputFileName().substr(0,found+1);
66 std::string filename = bbGetInputFileName().substr(found+1);
74 if (bbGetInputDirectionPlane()=="YZ")
76 inputfilename = path+filename+"_YZ.mhd";
79 if (bbGetInputDirectionPlane()=="ZX")
81 inputfilename = path+filename+"_ZX.mhd";
85 vtkImageData *newImage;
86 long long int newHeaderSize;
87 std::string newFileName=inputfilename+"-OneSlice";
93 FILE *ffIn = fopen(inputfilename.c_str(),"r");
96 FILE *ffOut = fopen(newFileName.c_str(),"w");
100 strcpy(mystring,"\n");
101 fgets(mystring,250,ffIn);
102 if (strncmp("NDims",mystring,5)==0) {strcpy(mystring,"NDims = 2\n");}
103 if (strncmp("DimSize",mystring,6)==0)
105 sscanf(mystring,"%s %s %d %d %d",strTmp, strTmp, &sx, &sy,&sz);
106 sprintf(mystring,"DimSize = %d %d\n",sx,sy);
107 newHeaderSize = (long long int)sx*(long long int)sy*(long long int)bbGetInputSlice();
108 if (bbGetInputSlice()>=sz) {ok=false;}
110 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newHeaderSize=newHeaderSize*1; }
111 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newHeaderSize=newHeaderSize*1; }
112 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newHeaderSize=newHeaderSize*2; }
113 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newHeaderSize=newHeaderSize*2; }
114 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; }
115 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newHeaderSize=newHeaderSize*4; }
116 if (strncmp("Offset",mystring,6)==0) {strcpy(mystring,"Offset = 0 0 0\n");}
117 if (strncmp("HeaderSize",mystring,10)==0) {strcpy(mystring,"");}
118 if (strncmp("ElementDataFile",mystring,15)==0)
120 fprintf(ffOut,"HeaderSize = %lld\n\n", newHeaderSize );
122 fprintf(ffOut,mystring);
129 vtkMetaImageReader * reader = vtkMetaImageReader::New();
130 reader->SetFileName( newFileName.c_str() );
132 newImage = reader->GetOutput();
136 printf("EED ERROR: Problem openin:%s\n", inputfilename.c_str() );
142 int sizeX, sizeY, sizeZ;
146 newImage = vtkImageData::New();
147 newImage->Initialize();
148 newImage->SetScalarTypeToUnsignedChar();
149 newImage->SetSpacing( 1,1,1 );
150 newImage->SetDimensions( sizeX,sizeY,sizeZ );
151 newImage->SetWholeExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
152 newImage->SetExtent(0, sizeX-1,0,sizeY-1,0,sizeZ-1 );
153 newImage->SetNumberOfScalarComponents(1);
154 newImage->AllocateScalars();
156 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
157 for (i=0; i<sizeX; i++)
159 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
160 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
164 bbSetOutputOut( newImage );
168 void ReadMHDPlane::Read64lseek()
171 int dimX=-1,dimY=-1,dimZ=-1;
173 std::string formattype("VOID");
174 std::string elementdatafile("VOID");
175 float spcX=-1,spcY=-1,spcZ=-1;
176 float ox=-1,oy=-1,oz=-1;
177 long int headersize=0;
179 vtkImageData *newImage;
184 FILE *ffIn = fopen(bbGetInputFileName().c_str(),"r");
191 newImage = vtkImageData::New();
195 strcpy(mystring,"\n");
196 fgets(mystring,250,ffIn);
197 if (strncmp("NDims",mystring,5)==0) { sscanf(mystring,"%s %s %d" ,strTmp, strTmp, &dim); }
198 if (strncmp("DimSize",mystring,6)==0) { sscanf(mystring,"%s %s %d %d %d" ,strTmp, strTmp, &dimX, &dimY,&dimZ); }
199 if (strncmp("ElementType",mystring,11)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); formattype=strTmp2; }
200 if (strncmp("ElementSpacing",mystring,11)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &spcX,&spcY,&spcZ); }
201 if (strncmp("Offset",mystring,6)==0) { sscanf(mystring,"%s %s %f %f %f" ,strTmp, strTmp, &ox, &oy, &oz); }
202 if (strncmp("HeaderSize",mystring,10)==0) { sscanf(mystring,"%s %s %ld" ,strTmp, strTmp, &headersize); }
203 if (strncmp("ElementDataFile",mystring,15)==0) { sscanf(mystring,"%s %s %s" ,strTmp, strTmp, strTmp2); elementdatafile=strTmp2; }
205 if (strncmp("ElementType = MET_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
206 if (strncmp("ElementType = VTK_CHAR",mystring,22)==0) { newImage->SetScalarTypeToChar(); dataSize=sizeof(char); }
207 if (strncmp("ElementType = MET_UCHAR",mystring,23)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
208 if (strncmp("ElementType = VTK_UNSIGNED_CHAR",mystring,31)==0) { newImage->SetScalarTypeToUnsignedChar(); dataSize=sizeof(unsigned char); }
209 if (strncmp("ElementType = MET_USHORT",mystring,24)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
210 if (strncmp("ElementType = VTK_UNSIGNED_SHORT",mystring,32)==0) { newImage->SetScalarTypeToUnsignedShort(); dataSize=sizeof(unsigned short);}
211 if (strncmp("ElementType = MET_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
212 if (strncmp("ElementType = VTK_SHORT",mystring,23)==0) { newImage->SetScalarTypeToShort(); dataSize=sizeof(short); }
213 if (strncmp("ElementType = MET_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
214 if (strncmp("ElementType = VTK_FLOAT",mystring,23)==0) { newImage->SetScalarTypeToFloat(); dataSize=sizeof(float); }
215 if (strncmp("ElementType = MET_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
216 if (strncmp("ElementType = VTK_DOUBLE",mystring,24)==0) { newImage->SetScalarTypeToDouble(); dataSize=sizeof(double); }
221 newImage->Initialize();
227 std::string filename;
228 found = bbGetInputFileName().find_last_of("/\\");
229 filename = bbGetInputFileName().substr(0,found+1) + elementdatafile ;
232 long long lsize = dimX*dimY*1 *dataSize;
233 fd = open ( filename.c_str() , O_RDONLY|O_LARGEFILE );
236 printf("EED ReadMHDPlane::Read64lseek \n");
237 fprintf(stderr, "%s\n", strerror(errno));
241 if (bbGetInputDirectionPlane()=="XY")
243 newImage->SetSpacing( spcX,spcY,spcZ );
244 newImage->SetDimensions( dimX,dimY,1 );
245 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,0 );
246 newImage->SetExtent(0, dimX-1,0,dimY-1,0,0 );
247 newImage->SetNumberOfScalarComponents(1);
248 newImage->AllocateScalars();
250 pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
251 if (lseek64(fd, pos, SEEK_SET) < 0)
253 printf("EED ReadMHDPlane::Read64lseek \n");
254 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
258 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
260 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
266 if (bbGetInputDirectionPlane()=="XZ")
269 newImage->SetSpacing( spcX,spcZ,spcY );
270 newImage->SetDimensions( dimX,dimZ,1 );
271 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,0 );
272 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,0 );
273 newImage->SetNumberOfScalarComponents(1);
274 newImage->AllocateScalars();
276 long long sizeBytesPlane = dimX*dimY*dataSize;
277 pos = dimX*(long long int)bbGetInputSlice()*dataSize;
278 void *pImage = newImage->GetScalarPointer();
281 printf("EED EED ReadMHDPlane::Read64lseek XZ j=%d \n", j);
282 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
284 printf("EED ReadMHDPlane::Read64lseek \n");
285 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
289 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
291 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
294 pImage=pImage+dimX*dataSize;
299 if (bbGetInputDirectionPlane()=="YZ")
303 newImage->SetSpacing( spcY,spcZ,spcX );
304 newImage->SetDimensions( dimY,dimZ,1 );
305 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,0 );
306 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,0 );
307 newImage->SetNumberOfScalarComponents(1);
308 newImage->AllocateScalars();
310 long long sizeBytesPlane = dimX*dimY*dataSize;
311 // pos = dimX*(long long int)bbGetInputSlice()*dataSize;
315 printf("EED EED ReadMHDPlane::Read64lseek YZ i=%d j=%d \n", i,j);
318 pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
319 if (lseek64(fd, pos , SEEK_SET) < 0)
321 printf("EED ReadMHDPlane::Read64lseek \n");
322 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
326 pImage=newImage->GetScalarPointer(i, j,0 );
327 if ((ret = read(fd, pImage , dataSize)) < 0)
329 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
339 bbSetOutputOut( newImage );
346 void ReadMHDPlane::Process()
348 if (bbGetInputType()==0)
353 if (bbGetInputType()==1)
360 // 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)
362 void ReadMHDPlane::bbUserSetDefaultValues()
365 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
366 // Here we initialize the input 'In' to 0
367 bbSetInputFileName("");
370 bbSetInputDirectionPlane("XY");
374 // 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)
376 void ReadMHDPlane::bbUserInitializeProcessing()
379 // THE INITIALIZATION METHOD BODY :
381 // but this is where you should allocate the internal/output pointers
387 // 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)
389 void ReadMHDPlane::bbUserFinalizeProcessing()
392 // THE FINALIZATION METHOD BODY :
394 // but this is where you should desallocate the internal/output pointers
399 // EO namespace bbcreaVtk