[vtkusers] Read PNG from in-memory buffer?

Elvis Stansvik elvis.stansvik at orexplore.com
Thu Sep 29 07:29:47 EDT 2016


2016-09-29 11:35 GMT+02:00 Elvis Stansvik <elvis.stansvik at orexplore.com>:
> 2016-09-29 9:29 GMT+02:00 Elvis Stansvik <elvis.stansvik at orexplore.com>:
>> I'd like to read a PNG from an in-memory buffer. Looking at
>> vtkPNGReader, it seems it only works with files in the file system. In
>> general, is there some way to use the reader classes in VTK with
>> in-memory data, or would I have to write my own custom reader?
>>
>> My need for this is not great, I was simply hoping to be able to keep
>> this particular PNG in the Qt resource system (so in the end, embedded
>> in my executable), load it using Qt file facilities, and use VTK on
>> the read in-memory data.
>
> I realize now that I can probable use QImage to load/decompress the
> image, and pass the uncompressed data to a vtkImageImport, but I can't
> find any docs on what format vtkImageImport expects for the pixels.
> Anyone know?

I had a go at this approach, using QImage::Format_RGBA8888 as I think
(?) that's the format vtkImportImage wants, but the resulting image
looks garbled when shown in VTK:

    // Load image with QImage
    QImage image(":/images/orexplore-logo-80x47.png");

    // Convert to RGBA8888
    image = image.convertToFormat(QImage::Format_RGBA8888);

    // Have a look at the image to make sure it's OK
    auto debugLabel = new QLabel();
    debugLabel->setPixmap(QPixmap::fromImage(image));
    debugLabel->show();

    // Import into a vtkImageData
    auto imageImport = vtkSmartPointer<vtkImageImport>::New();
    imageImport->SetDataSpacing(1, 1, 1);
    imageImport->SetDataOrigin(0, 0, 0);
    imageImport->SetWholeExtent(0, image.width() - 1, 0,
image.height() - 1, 0, 0);
    imageImport->SetDataExtentToWholeExtent();
    imageImport->SetDataScalarTypeToUnsignedChar();
    imageImport->SetNumberOfScalarComponents(4);
    imageImport->SetImportVoidPointer(image.bits(), 1); // Note:
QImage::bits() deep copies
    imageImport->Update();

    auto mapper = vtkSmartPointer<vtkImageMapper>::New();
    mapper->SetInputData(imageImport->GetOutput());

    auto actor = vtkSmartPointer<vtkActor2D>::New();
    actor->SetMapper(mapper);

Anyone know exactly what format vtkImageImport expects, or, if it
expects RGBA (unsigned char), why the above won't work?

Elvis

>
> Elvis
>
>>
>> Elvis


More information about the vtkusers mailing list