Hi All,<div><br></div><div>I have an application where I need to convert vtkImageData to QImage. I am using the following code for this.</div><div><br></div><div>Assumptions: vtkImageData is 2D. dim[0] is width, dim[1] is height. Scalars associated with the image is always VTK_UNSIGNED_CHAR type.</div>
<div><br></div><div><div>QImage ConvertToQImage(vtkImageData* imageData)</div><div>{</div><div> int dim[3];</div><div> imageData->GetDimensions(dim);</div><div> if(dim[0]*dim[1]*dim[2] == 0)</div><div> return QImage();</div>
<div><br></div><div> vtkUnsignedCharArray* scalars </div><div> = vtkUnsignedCharArray::SafeDownCast(imageData->GetPointData()->GetScalars());</div><div> if(!scalars)</div><div> return QImage();</div>
<div><br></div><div> QImage qImage(dim[0], dim[1], QImage::Format_ARGB32);</div><div> vtkIdType tupleIndex=0;</div><div> unsigned char tuple[] = {0, 0, 0, 0};</div><div><br></div><div> for(int j=0; j<dim[1]; j++)</div>
<div> {</div><div> for(int i=0; i<dim[0]; i++)</div><div> {</div><div> int r=0, g=0, b=0, a=0;</div><div> scalars->GetTupleValue(tupleIndex++, tuple);</div><div><br></div><div>
switch(scalars->GetNumberOfComponents())</div><div> {</div><div> case 1: </div><div> r = g = b = tuple[0];</div><div> a = 255;</div><div> break;</div>
<div> case 2:</div><div> r = g = b = tuple[0];</div><div> a = tuple[1];</div><div> break;</div><div> case 3:</div><div> r = tuple[0];</div>
<div> g = tuple[1];</div><div> b = tuple[2];</div><div> a = 255;</div><div> break;</div><div> case 4:</div><div> r = tuple[0];</div><div>
g = tuple[1];</div><div> b = tuple[2];</div><div> a = tuple[3];</div><div> break;</div><div> }</div><div><br></div><div> QRgb color = qRgba(r, g, b, a);</div>
<div> qImage.setPixel(i, j, color);</div><div> }</div><div> }</div><div><br></div><div> return qImage;</div><div>}</div><div><br></div><div>I was wondering if there was a faster way to do this. If anyone has hints for me, please do share.</div>
<div><br></div><div>Thanks very much.</div><div><br></div><div>Regards,</div><div>Prashanth N Udupa</div><br>
</div>