[vtkusers] Read PNG from in-memory buffer?

Elvis Stansvik elvis.stansvik at orexplore.com
Thu Sep 29 10:29:46 EDT 2016


2016-09-29 14:01 GMT+02:00 David Gobbi <david.gobbi at gmail.com>:
> You mentioned that QImage.bits() does a "deep copy", but that seems
> meaningless for your use case because Qt owns all copies and the returned
> pointer will become invalid when the QImage destructs.  It would be
> different if you were passing the pointer to another Qt image, because then
> Qt could "track" the pointer.

You're absolutely right, I misread this passage in the QImage::bits docs:

"Note that QImage uses implicit data sharing. This function performs a
deep copy of the shared pixel data, thus ensuring that this QImage is
the only one using the current return value."

I thought it meant that the function will return a newly allocated
copy (to be owned by me as a caller), when in fact it just means it'll
detach the underlying shared pointer. So Qt will still own the data.

Now that I found the vtkQImageToImageSource convenience class, I'll of
course that instead.

Thanks!

Elvis

>
> VTK should be the one to create the copy.
>
>     imageImport->CopyImportVoidPointer(image.constBits(),
> image.byteCount());
>
> Other than that, your usage of vtkImageImport looks fine.
>
>  - David
>
> On Thu, Sep 29, 2016 at 5:32 AM, Elvis Stansvik
> <elvis.stansvik at orexplore.com> wrote:
>>
>> 2016-09-29 13:29 GMT+02:00 Elvis Stansvik <elvis.stansvik at orexplore.com>:
>> > 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?
>>
>> Whoa. Just found
>> http://www.vtk.org/doc/nightly/html/classvtkQImageToImageSource.html
>> :) I'll of course use that. Sorry for the noise.
>>
>> Still interested in why the above approach wouldn't work, out of
>> curiosity.
>>
>> Elvis
>>
>> >
>> > Elvis
>> >
>> >>
>> >> Elvis
>> >>
>> >>>
>> >>> Elvis
>> _______________________________________________
>> 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
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers
>
>


More information about the vtkusers mailing list