[vtkusers] Read PNG from in-memory buffer?
Elvis Stansvik
elvis.stansvik at orexplore.com
Thu Sep 29 10:32:28 EDT 2016
2016-09-29 13:42 GMT+02:00 Andras Lasso <lasso at queensu.ca>:
> You can use libpng directly (it's included with VTK). See how it is done in the PLUS toolkit:
> DecodePngImage(unsigned char* pngBuffer, unsigned int pngBufferSize, vtkImageData* decodedImage)
> https://app.assembla.com/spaces/plus/subversion/source/HEAD/trunk/PlusLib/src/PlusDataCollection/BkProFocus/vtkPlusBkProFocusOemVideoSource.cxx#ln549
Thanks for the pointer Andras. In my case I was interested in reading
the PNG from the Qt resource system, and I found the
vtkQImageToImageSource helper class which fits like a glove.
Had I not found it, I might have gone down the route of writing my own
VTK "from memory" reader class using libpng.
Elvis
>
> Andras
>
> -----Original Message-----
> From: vtkusers [mailto:vtkusers-bounces at vtk.org] On Behalf Of Elvis Stansvik
> Sent: September 29, 2016 7:32
> To: VTK Users <vtkusers at vtk.org>
> Subject: Re: [vtkusers] Read PNG from in-memory buffer?
>
> 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