[vtkusers] Fast conversion from vtkImageData to QImage?
Prashanth Udupa
prashanth.udupa at gmail.com
Fri Jan 29 06:09:57 EST 2010
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100129/8ac94bbf/attachment.htm>
More information about the vtkusers
mailing list