[vtkusers] How does one enforce recognition of little endian data in VTK?
Jeff Lee
jeff at cdnorthamerica.com
Tue May 3 11:27:43 EDT 2005
Sanatan Sahgal wrote:
> Yes, I am setting the scalar type. I set up the image data as follows:
>
> //setup image data
> imageData = vtkImageData::New();
> imageData->SetDimensions(512, 512, 1);
> imageData->SetScalarType(VTK_SHORT);
> imageData->SetOrigin(0.0, 0.0, 0.0);
> imageData->SetSpacing(1.0, 1.0, 1.0);
> imageData->SetNumberOfScalarComponents(1);
> imageData->GetPointData()->SetScalars(pixelArrayVtk);
>
> pixelArrayVtk was created and set as follows:
> pixelArrayVtk = CreateVtkDataArray(); //returns a VTK_SHORT array for
> 2 byte signed data type
> pixelArrayVtk->SetVoidArray(imageRawData, totalSamples, 1);
>
> And imageRawData is the pointer to the memory location where the raw
> image data in little endian format is stored.
>
> Is something wrong with this setup code? Is there another
> SetScalarType(...) method I should be using?
SetScalarTypeToShort()...
are you sure that imageRawData is an array of shorts? what if you were
to loop from 0 to totalSamples and populate the pixelArrayVtk that way -
do you get similiar results? If that fixes your problem, then your data
in memory isn't an array of shorts.
i.e.
pixelArrayVtk->SetNumberOfComponents(1);
pixelArrrayVtk->SetNumberOfValues(totalSamples);
for (int i = 0; i < totalSamples; ++i) {
pixelArrayVtk->SetValue(i, sample[i]);
}
>
> thanks,
> Sanatan
>
>>
>> I meant 4-bytes...
>>
>> Jeff Lee wrote:
>>
>>>
>>>
>>> Sanatan Sahgal wrote:
>>>
>>>> Again, I am not using a VTK reader/importer classes. I do not want
>>>> to use these classes because they do not fulfill the needs of my
>>>> application.
>>>>
>>>> As explained in my original post, I set the data to VTK in the
>>>> following manner:
>>>>
>>>> I am using the vtkImageData class. I feed data to the imageData
>>>> instance as follows:
>>>> imageData->GetPointData()->SetScalars(pixelArrayVtk);
>>>>
>>>> The pixelArrayVtk instance is of type vtkDataArray and it is
>>>> setup as follows:
>>>> pixelArrayVtk = CreateVtkDataArray(); //Creates instance of
>>>> specific VtkDataArray type. Example: vtkShortArray::New()
>>>> pixelArrayVtk->SetVoidArray(imageRawData, totalSamples, 1);
>>>>
>>>> The imageRawData parameter is a pointer to the memory location
>>>> which contains the raw image data in little endian format.
>>>>
>>>> VTK expects this raw image data to be big endian for some reason.
>>>> According to your response it should be happy with the little
>>>> endian format. But, it is not! If the data is in big endian format
>>>> I see the correct image on the screen. If it is in little endian
>>>> format I do not see a meaningful image on the screen. The issue is
>>>> that one would expect the opposite to be true because I am working
>>>> on an Intel platform which has the native format of little endian
>>>> byte ordering!
>>>>
>>>> Are you saying that there is a compile time option which will tell
>>>> VTK the native byte ordering (without using any of the
>>>> reader/importer classes). If so, what is that compile time option
>>>> and how does one set it?
>>>
>>>
>>>
>>> oh, sorry I didn't read carefully enough. I don't think this is a
>>> byte-swap issue, but a datatype issue. first, vtkImageData has some
>>> methods to set the ScalarType - which one are you using? It should
>>> match the type returned from your CreateVtkDataArray. The fact that
>>> it works when you swap bytes is probably because by default, vtk is
>>> expecting a Float type (4 bits) and you are perhaps passing in a
>>> short? Try setting the scalarType and see what happens...
>>> -J
>>>
>>>>
>>>> -Sanatan
>>>>
>>>>>
>>>>> If you compile vtk on a little-endian machine, then byteswapping
>>>>> will only occur when reading big-endian. vtk always swaps to
>>>>> native byte ordering. So your problem probably lies in the
>>>>> importer/reader not setting the DataByteOrder properly. something
>>>>> like reader->SetDataByteOrderToLittleEndian() should do it.
>>>>> -Jeff
>>>>>
>>>>> Sanatan Sahgal wrote:
>>>>>
>>>>>>
>>>>>> Because it is an extra overhead especially for large datasets and
>>>>>> the app will take a performance hit everytime I swap bytes for
>>>>>> each dataset. As stated in my first e-mail, I already know the
>>>>>> problem can be solved by doing byte swapping before feeding the
>>>>>> data to VTK. I am looking for a cleaner solution where the extra
>>>>>> overhead of byte swapping can be avoided.
>>>>>>
>>>>>> I think the root cause of the problem is that VTK expects Big
>>>>>> Endian data on Little Endian machines. If VTK binaries run on
>>>>>> computers with little endian architecture then it is only natural
>>>>>> to expect that the byte ordering of the data will also be little
>>>>>> endian. Why does the data have to be in big endian format?
>>>>>>
>>>>>> Anyway, doesn't VTK eventually have to swap the data back to
>>>>>> little endian format for the Intel architecture? It just seems
>>>>>> that on computers bound to little endian architecture there is a
>>>>>> lot of swapping necessary for no reason at all. Perhaps I am
>>>>>> missing something. I think the inconsistent endianness is a major
>>>>>> source of confusion especially for someone who has no need of
>>>>>> using the VTK reader classes.
>>>>>>
>>>>>> -Sanatan
>>>>>>
>>>>>>>
>>>>>>> Interesting, so why don't you change the type from big endian to
>>>>>>> little endian when you are reading the file during your
>>>>>>> 'pre-processing' steps.
>>>>>>>
>>>>>>> Anyway the whole byte swapping code is in vtkByteSwap
>>>>>>>
>>>>>>> http://www.vtk.org/doc/nightly/html/classvtkByteSwap.html
>>>>>>>
>>>>>>> HTH
>>>>>>> Mathieu
>>>>>>>
>>>>>>> Sanatan Sahgal wrote:
>>>>>>>
>>>>>>>> Mathieu,
>>>>>>>>
>>>>>>>> Thanks for the prompt response.
>>>>>>>>
>>>>>>>> The data is not coming from a file. Prior to the data hitting
>>>>>>>> any of the VTK classes it undergoes some pre-processing steps
>>>>>>>> that I need to do. The bottom line is that the data feed to VTK
>>>>>>>> is not from a file. So, using vtkImageReader class will not
>>>>>>>> work. Is there any other way?
>>>>>>>>
>>>>>>>> thanks,
>>>>>>>> Sanatan
>>>>>>>>
>>>>>>>>>
>>>>>>>>> Sanatan,
>>>>>>>>>
>>>>>>>>> Since you are reading from a file you should rather have a
>>>>>>>>> look at the vtkImageReader class:
>>>>>>>>>
>>>>>>>>> http://www.vtk.org/doc/nightly/html/classvtkImageReader.html
>>>>>>>>>
>>>>>>>>> Since there is a member function that you need:
>>>>>>>>>
>>>>>>>>> vtkImageReader2::SetDataByteOrderToBigEndian()
>>>>>>>>>
>>>>>>>>> HTH
>>>>>>>>> Mathieu
>>>>>>>>>
>>>>>>>>> Sanatan Sahgal wrote:
>>>>>>>>>
>>>>>>>>>> Hello,
>>>>>>>>>>
>>>>>>>>>> I am using VTK on an Intel platform running Windows XP
>>>>>>>>>> professional. I have a 2 bytes per pixel grayscale image
>>>>>>>>>> which I wish to display on the screen using VTK.
>>>>>>>>>>
>>>>>>>>>> My image raw data is in little endian format and when I feed
>>>>>>>>>> it to VTK I see garbage on the screen because VTK seems to
>>>>>>>>>> expect it in big endian format. If I swap the bytes and then
>>>>>>>>>> feed it to VTK the image shows up correctly on my screen.
>>>>>>>>>>
>>>>>>>>>> I don't want this extra overhead of swapping the bytes. I
>>>>>>>>>> want VTK to recognize the raw little endian data just as it
>>>>>>>>>> is. Is there any way to tell VTK that my data is in little
>>>>>>>>>> endian format and it should read it as such?
>>>>>>>>>>
>>>>>>>>>> I am using the vtkImageData class. I feed data to the
>>>>>>>>>> imageData instance as follows:
>>>>>>>>>> imageData->GetPointData()->SetScalars(pixelArrayVtk);
>>>>>>>>>>
>>>>>>>>>> The pixelArrayVtk instance is of type vtkDataArray and it is
>>>>>>>>>> setup as follows:
>>>>>>>>>> pixelArrayVtk = CreateVtkDataArray(); //Creates instance of
>>>>>>>>>> specific VtkDataArray type. Example: vtkShortArray::New()
>>>>>>>>>> pixelArrayVtk->SetVoidArray(imageRawData, totalSamples, 1);
>>>>>>>>>>
>>>>>>>>>> The imageRawData parameter is a pointer to the memory
>>>>>>>>>> location which contains the raw image data in little endian
>>>>>>>>>> format.
>>>>>>>>>>
>>>>>>>>>> Any help with this issue is appreciated.
>>>>>>>>>>
>>>>>>>>>> thanks,
>>>>>>>>>> Sanatan
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> _______________________________________________
>>>>>>>>>> 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
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> 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
>>>>>>
>>>>>>
>>>>
>>>>
>>>>
>>> _______________________________________________
>>> 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
>>>
>>>
>
>
> _______________________________________________
> 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
>
>
More information about the vtkusers
mailing list