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)
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;
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)
245 printf("EED ReadMHDPlane::Read64lseek \n");
246 fprintf(stderr, "%s\n", strerror(errno));
250 if (bbGetInputDirectionPlane()=="XY")
252 newImage->SetSpacing( spcX,spcY,spcZ );
253 newImage->SetDimensions( dimX,dimY,1 );
254 newImage->SetWholeExtent(0, dimX-1,0,dimY-1,0,0 );
255 newImage->SetExtent(0, dimX-1,0,dimY-1,0,0 );
256 newImage->SetNumberOfScalarComponents(1);
257 newImage->AllocateScalars();
259 pos = dimX*dimY*(long long)bbGetInputSlice()*dataSize;
264 if (_lseeki64( fd, pos, SEEK_SET ) < 0)
266 if (lseek64(fd, pos, SEEK_SET) < 0)
267 #endif // defined(_WIN32)
270 printf("EED ReadMHDPlane::Read64lseek \n");
271 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
275 if ((ret = read(fd, newImage->GetScalarPointer() , lsize)) < 0)
277 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
283 if (bbGetInputDirectionPlane()=="XZ")
286 newImage->SetSpacing( spcX,spcZ,spcY );
287 newImage->SetDimensions( dimX,dimZ,1 );
288 newImage->SetWholeExtent(0, dimX-1,0,dimZ-1,0,0 );
289 newImage->SetExtent(0, dimX-1,0,dimZ-1,0,0 );
290 newImage->SetNumberOfScalarComponents(1);
291 newImage->AllocateScalars();
293 long long sizeBytesPlane = dimX*dimY*dataSize;
294 pos = dimX*(long long int)bbGetInputSlice()*dataSize;
295 void *pImage = newImage->GetScalarPointer();
298 printf("EED EED ReadMHDPlane::Read64lseek XZ j=%d \n", j);
299 if (lseek64(fd, pos + j*sizeBytesPlane , SEEK_SET) < 0)
301 printf("EED ReadMHDPlane::Read64lseek \n");
302 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
306 if ((ret = read(fd, pImage , dimX*dataSize)) < 0)
308 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
311 pImage=pImage+dimX*dataSize;
316 if (bbGetInputDirectionPlane()=="YZ")
320 newImage->SetSpacing( spcY,spcZ,spcX );
321 newImage->SetDimensions( dimY,dimZ,1 );
322 newImage->SetWholeExtent(0, dimY-1,0,dimZ-1,0,0 );
323 newImage->SetExtent(0, dimY-1,0,dimZ-1,0,0 );
324 newImage->SetNumberOfScalarComponents(1);
325 newImage->AllocateScalars();
327 long long sizeBytesPlane = dimX*dimY*dataSize;
328 // pos = dimX*(long long int)bbGetInputSlice()*dataSize;
332 printf("EED EED ReadMHDPlane::Read64lseek YZ i=%d j=%d \n", i,j);
335 pos = ((long long int)bbGetInputSlice() + i*dimX + j*dimX*dimY)*dataSize ;
336 if (lseek64(fd, pos , SEEK_SET) < 0)
338 printf("EED ReadMHDPlane::Read64lseek \n");
339 fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
343 pImage=newImage->GetScalarPointer(i, j,0 );
344 if ((ret = read(fd, pImage , dataSize)) < 0)
346 fprintf(stderr, "Failed reading: %s\n", strerror(errno));
358 #endif // defined(_WIN32)
360 bbSetOutputOut( newImage );
367 void ReadMHDPlane::Process()
369 if (bbGetInputType()==0)
374 if (bbGetInputType()==1)
381 // 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)
383 void ReadMHDPlane::bbUserSetDefaultValues()
386 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
387 // Here we initialize the input 'In' to 0
388 bbSetInputFileName("");
391 bbSetInputDirectionPlane("XY");
395 // 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)
397 void ReadMHDPlane::bbUserInitializeProcessing()
400 // THE INITIALIZATION METHOD BODY :
402 // but this is where you should allocate the internal/output pointers
408 // 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)
410 void ReadMHDPlane::bbUserFinalizeProcessing()
413 // THE FINALIZATION METHOD BODY :
415 // but this is where you should desallocate the internal/output pointers
420 // EO namespace bbcreaVtk