[vtkusers] Fast conversion from vtkImageData to QImage?
Michael Jackson
mike.jackson at bluequartz.net
Fri Jan 29 08:14:13 EST 2010
The only way faster would be to try and work with the raw pointers
for both the vtkImageData and the QImage. You do lose lots of safety
checks, but from my experience calls like scalars-
>GetTupleValue(tupleIndex++, tuple); take longer due to the bounds
checking (which is good) but if you feel you can get along without the
bounds checking (because you already have checked) you can do it that
way.
BUT before doing ANYTHING, I would put some timing code in there so
you can MEASURE the current performance and properly figure out where
the bottleneck truly lies. Each platform has their own set of tools to
do this. On OS X there is "Shark" and "Instruments" that can help show
where the slow parts of your code are. Visual Studio and Linux both
have tools also.
"Premature optimization is the root of all evil"
_________________________________________________________
Mike Jackson mike.jackson at bluequartz.net
BlueQuartz Software www.bluequartz.net
Principal Software Engineer Dayton, Ohio
On Jan 29, 2010, at 6:09 AM, Prashanth Udupa wrote:
> Hi All,
>
> I have an application where I need to convert vtkImageData to
> QImage. I am using the following code for this.
>
> Assumptions: vtkImageData is 2D. dim[0] is width, dim[1] is height.
> Scalars associated with the image is always VTK_UNSIGNED_CHAR type.
>
> QImage ConvertToQImage(vtkImageData* imageData)
> {
> int dim[3];
> imageData->GetDimensions(dim);
> if(dim[0]*dim[1]*dim[2] == 0)
> return QImage();
>
> vtkUnsignedCharArray* scalars
> = vtkUnsignedCharArray::SafeDownCast(imageData-
> >GetPointData()->GetScalars());
> if(!scalars)
> return QImage();
>
> QImage qImage(dim[0], dim[1], QImage::Format_ARGB32);
> vtkIdType tupleIndex=0;
> unsigned char tuple[] = {0, 0, 0, 0};
>
> for(int j=0; j<dim[1]; j++)
> {
> for(int i=0; i<dim[0]; i++)
> {
> int r=0, g=0, b=0, a=0;
> scalars->GetTupleValue(tupleIndex++, tuple);
>
> switch(scalars->GetNumberOfComponents())
> {
> case 1:
> r = g = b = tuple[0];
> a = 255;
> break;
> case 2:
> r = g = b = tuple[0];
> a = tuple[1];
> break;
> case 3:
> r = tuple[0];
> g = tuple[1];
> b = tuple[2];
> a = 255;
> break;
> case 4:
> r = tuple[0];
> g = tuple[1];
> b = tuple[2];
> a = tuple[3];
> break;
> }
>
> QRgb color = qRgba(r, g, b, a);
> qImage.setPixel(i, j, color);
> }
> }
>
> return qImage;
> }
>
> I was wondering if there was a faster way to do this. If anyone has
> hints for me, please do share.
>
> Thanks very much.
>
> Regards,
> Prashanth N Udupa
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK 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