[vtkusers] vtkImageImport Segfault

Randall Hand randall.hand at gmail.com
Thu Jun 2 11:57:43 EDT 2005


Ok, I finally resolved this.. Here's what it took:

printf("Opening %s for reading...\n", argv[1]);
fptr = fopen(argv[1], "r");
if ((data = (unsigned char*)malloc(file_info.st_size)) == NULL) {
perror("Unable to allocate memory!\n\t");
return -1;
}
dataptr= data;
fread(dataptr, 1, 4, fptr);
dataptr += 4;
printf("\n");
for(int i=0; i<xdim; i++) {
printf("\r[%c] Reading slice %i (%.1f%%)...", progresschar[i % 4],
i, (float(i) / float(xdim)) * 100.0);
fflush(stdout);
fread(dataptr, ydim,zdim, fptr);
dataptr += (ydim * zdim);
}
printf("\n\n");

vtkUnsignedCharArray *ucPointer = vtkUnsignedCharArray::New();
ucPointer->SetNumberOfComponents(1);
ucPointer->SetArray(data+4, file_info.st_size-8, 1);
ucPointer->SetName("image");

vtkImageData *image = vtkImageData::New();
image->Initialize();
image->SetDimensions(xdim, ydim, zdim);
image->SetExtent(1, xdim, 1, ydim, 1, zdim);
image->SetScalarTypeToUnsignedChar();
image->GetPointData()->SetScalars(ucPointer);

This is basically what Matthew suggested, and it works perfectly :) I was 
never able to get vtkImageImport or vtkImageReader to work successfully.


On 5/31/05, Randall Hand <randall.hand at gmail.com> wrote:
> 
> You're right.. i've been messing with those numbers thinking I was 
> violating my extents or an array boundary.. it hasn't helped tho.
> 
> On 5/31/05, Amy Squillacote <amy.squillacote at kitware.com > wrote:
> > 
> > I doubt that this will fix the seg fault, but shouldn't your DataExtent 
> > be set to *0*, xdim-1, *0*, ydim-1, *0*, zdim-1?
> > - Amy
> > 
> > At 11:41 AM 5/31/2005, Randall Hand wrote:
> > 
> > Well, I modified my code to do the following:
> > 
> > xdim = 2191;
> > ydim = 2191;
> > zdim = 800;
> > vtkImageReader *file = vtkImageReader::New();
> > file->SetFileName(argv[1]);
> > file->SetDataExtent(1, xdim-1,
> > 1, ydim-1,
> > 1, zdim-1);
> > file->SetHeaderSize(4);
> > file->SetDataScalarTypeToUnsignedChar();
> > file->SetScalarArrayName("data");
> > file->Update();
> > PrintStatistics(file->GetOutput());
> > 
> > and, sadly, it still segfaults:
> > [10:39:48am]% dbx imageimport
> > dbx version 7.3.4 (86441_Nov11 MR) Nov 11 2002 11:31:55
> > Core from signal SIGSEGV: Segmentation violation
> > (dbx) where
> > Thread 0x10000
> > > 0 ::vtkImageReaderUpdate2(vtkImageReader*,vtkImageData*,unsigned 
> > char*,unsigned char*)(self = 0x100375a0, data = 0x100380d0, inPtr = 
> > 0x1003adce = "", outPtr = (nil)) 
> > ["/viz/home/rhand/src/ezViz/Utilities/VTK/IO/vtkImageReader.cxx":333, 
> > 0x7b58fb4]
> > (dbx) quit
> > 
> > The data file:
> > [10:39:58am]% ls -la bytes.CY00000
> > -rwxr----- 1 rhand erdcvsta 3840384808 May 27 09:01 bytes.CY00000
> > 
> > 3,840,384,808 bytes. 2191 * 2191 * 800 = 3,840,384,800 .. 4 bytes header 
> > at both the beginning and the end.
> > 
> > 
> > On 5/31/05, *Amy Squillacote* < amy.squillacote at kitware.com> wrote:
> >  Did you try using the SetHeaderSize method in vtkImageReader2 (the 
> > superclass of vtkImageReader)? I know it isn't just specifying a number of 
> > bytes to read, but using that and the SetDataExtent method (also in 
> > vtkImageReader) should do the right thing.
> > 
> > - Amy
> > 
> > 
> > At 10:15 AM 5/31/2005, Randall Hand wrote:
> > 
> > Ok, I'm looking at this now (computers are finally back online :) ), and 
> > I can't figure out how to set the raw number of bytes to read. I see 
> > SetDataVOI, but nothing to "bypass" that and specify a number of bytes.
> > 
> > On 5/27/05, Mathieu Malaterre < mathieu.malaterre at kitware.com> wrote: 
> > Randall,
> >  Hum I believe you can read those bytes but never use it... Could you try 
> > to read 3,840,384,800 bytes + 4 but set properly the dimension. If you 
> > are lucky enough no test is done to check consistency of datasize and 
> > dimension of data.
> > My 2 cents, Mathieu
> > Randall Hand wrote: > Well, I have an addition 4 bytes at the end of the 
> > file to skip as > well.. I suppose I could postprocess & write a new 
> > file without these > final 4 bytes, but that's gonna be pretty wasteful 
> > of disk space. Is > there a way to skip them? > > > > On 5/27/05, 
> > *Mathieu Malaterre* < mathieu.malaterre at kitware.com > < mailto: 
> > mathieu.malaterre at kitware.com <%20mathieu.malaterre at kitware.com>>> 
> > wrote: > > Randall, > > Could you instead give vtkImageReader a try. 
> > Just specify > the lenght of > data you want to read: 3,840,384,800 
> > bytes. By default vtkImageReader > start reading from the end. Thefore 
> > you are garantee to skip a > header if > any (4 bytes in your case 
> > apparently). > > HTH > Mathieu > > Randall Hand wrote: > > I'm 
> > attempting to load a simple "Brick of Bytes (BoB)" format > file into > 
> > > VTK. It is 2191x2191x800 unsigned chars (3,840,384,800 bytes). > With 
> > 4 > > bytes at the beginning that I have to skip (Gotta love fortran). 
> > My > > code is like the following: > > > > if (stat(argv[1], &file_info) 
> > == -1) { > > perror("Unable to stat file!\n\t"); > > return -1; > > } > 
> > > fptr = fopen(argv[1], "r"); > > if ((data = 
> > (char*)malloc(file_info.st_size)) == NULL) { > > perror("Unable to 
> > allocate memory!\n\t"); > > return -1; > > } > > dataptr = data; > > 
> > fread(dataptr, 1, 4, fptr); > > dataptr += 4; > > printf("\n\n"); > > 
> > for(int i=0; i<xdim; i++) { > > fread(dataptr, ydim,zdim, fptr); > > 
> > dataptr += (ydim * zdim); > > } > > > > vtkImageImport *import = 
> > vtkImageImport::New(); > > 
> > import->AddObserver(vtkCommand::ProgressEvent, progress); > > 
> > import->SetDataScalarTypeToUnsignedChar(); > > 
> > import->SetNumberOfScalarComponents(1); > > import->SetWholeExtent(1, 
> > xdim-1, > > 1, ydim-1, > > 1, zdim-1); > > 
> > import->SetDataExtentToWholeExtent(); > > 
> > import->SetImportVoidPointer(data + skip); > > > > vtkDataSetWriter 
> > *writer = vtkDataSetWriter::New(); > > 
> > writer->SetInput(import->GetOutput()); > > writer->SetFileName("
> > resulting.vtk"); > > writer->Write(); > > > > > > When I run this, 
> > however, the output VTK file only contains the > following: > > # vtk 
> > DataFile Version 3.0 > > vtk output > > ASCII > > DATASET 
> > STRUCTURED_POINTS > > DIMENSIONS 2191 2191 800 > > SPACING 1 1 1 > > 
> > ORIGIN 1 1 1 > > > > No data is actually written to the file. When I try 
> > to connect > this to > > a rendering pipeline or start querying the 
> > resulting vtkDataSet for > > properties (Scalar ranges, number of 
> > points, etc) I get strange > results > > and eventually I get 
> > Segfault/Core dumps. Often times the number of > > points listed comes 
> > out as a large negative number, instead of the > > 3.8bil that it should 
> > be. > > > > A stack trace of the core dumps only reveals this: > > > > 
> > [8:50:48am]% dbx imageimport > > dbx version 7.3.4 (86441_Nov11 MR) Nov 
> > 11 2002 11:31:55 > > Core from signal SIGSEGV: Segmentation violation > 
> > > (dbx) where > > > > Thread 0x10000 > > > 0 
> > vtkDataArrayTemplate<unsigned > char>::ComputeScalarRange(int)(this > > 
> > = 0xf4eb83c0, comp = 0) > > > 
> > ["/viz/home/rhand/src/ezViz/Utilities/VTK/Common/vtkDataArrayTemplate.txx":644, 
> > > > 0x85f2758] > > (dbx) > > (dbx) quit > > > > > > > > -- > > Randall 
> > Hand > > http://www.yeraze.com > > > > > > > 
> > ------------------------------------------------------------------------ > 
> > > > > _______________________________________________ > > This is the 
> > private VTK discussion list. > > Please keep messages on-topic. Check 
> > the FAQ at: > http://www.vtk.org/Wiki/VTK_FAQ > > Follow this link to 
> > subscribe/unsubscribe: > > http://www.vtk.org/mailman/listinfo/vtkusers > 
> > > > > > -- > Randall Hand > http://www.yeraze.com < 
> > <http://www.yeraze.com/> http://www.yeraze.com>
> > 
> > 
> > 
> > 
> > -- 
> > Randall Hand
> > http://www.yeraze.com 
> > _______________________________________________
> > This is the private VTK discussion list. 
> > Please keep messages on-topic. Check the FAQ at: 
> > http://www.vtk.org/Wiki/VTK_FAQ
> > Follow this link to subscribe/unsubscribe:
> >  http://www.vtk.org/mailman/listinfo/vtkusers
> > 
> > 
> > 
> > 
> > 
> > -- 
> > Randall Hand
> > http://www.yeraze.com 
> > 
> > 
> 
> 
> -- 
> Randall Hand
> http://www.yeraze.com 
> 



-- 
Randall Hand
http://www.yeraze.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20050602/7bbc9d44/attachment.htm>


More information about the vtkusers mailing list