Commit e987132e authored by Adrien Boucaud's avatar Adrien Boucaud

vtkPNGReader: MemoryBuffer support and other improvements.

This adds support for MemoryBuffer in the vtkPNGReader as specified in vtkImageReader2.
This also modernizes and reorganizes parts of the class.
Also adds tests for using the class to read from a png file and from the MemoryBuffer
parent 7d1ae844
......@@ -73,6 +73,14 @@ vtk_add_test_cxx(vtkIOImageCxxTests tests
TestWriteToMemoryBMP,TestWriteToMemory.cxx,NO_DATA NO_VALID NO_OUTPUT
"test.bmp")
vtk_add_test_cxx(vtkIOImageCxxTests tests
TestPNGReader.cxx,NO_OUTPUT
"DATA{${_vtk_build_TEST_INPUT_DATA_DIRECTORY}/Data/vtk.png}")
vtk_add_test_cxx(vtkIOImageCxxTests tests
TestPNGReaderReadFromMemory.cxx,NO_OUTPUT
"DATA{${_vtk_build_TEST_INPUT_DATA_DIRECTORY}/Data/vtk.png}")
if (VTK_USE_LARGE_DATA)
vtk_add_test_cxx(vtkIOImageCxxTests large_data_tests
TestMRCReader,TestMRCReader.cxx,NO_OUTPUT
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestJPEGReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME Test of vtkPNGReader
// .SECTION Description
//
#include "vtkPNGReader.h"
#include "vtkImageData.h"
#include "vtkImageViewer.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
int TestPNGReader(int argc, char* argv[])
{
if (argc <= 1)
{
cout << "Usage: " << argv[0] << " <png file>" << endl;
return EXIT_FAILURE;
}
std::string filename = argv[1];
vtkNew<vtkPNGReader> PNGReader;
// Check the image can be read
if (!PNGReader->CanReadFile(filename.c_str()))
{
cerr << "CanReadFile failed for " << filename.c_str() << "\n";
return EXIT_FAILURE;
}
// Read the input image
PNGReader->SetFileName(filename.c_str());
PNGReader->Update();
// Read and display the image properties
const char* fileExtensions = PNGReader->GetFileExtensions();
cout << "File extensions: " << fileExtensions << endl;
const char* descriptiveName = PNGReader->GetDescriptiveName();
cout << "Descriptive name: " << descriptiveName << endl;
// Visualize
vtkNew<vtkImageViewer> imageViewer;
imageViewer->SetInputConnection(PNGReader->GetOutputPort());
imageViewer->SetColorWindow(256);
imageViewer->SetColorLevel(127.5);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
renderWindowInteractor->Start();
vtkRenderWindow *renWin = imageViewer->GetRenderWindow();
int retVal = vtkRegressionTestImage(renWin);
if(retVal == vtkRegressionTester::DO_INTERACTOR)
{
renderWindowInteractor->Start();
}
return !retVal;
}
/*=========================================================================
Program: Visualization Toolkit
Module: TestJPEGReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME Test of vtkPNGReader's reading from memory functionality
// .SECTION Description
//
#include "vtkPNGReader.h"
#include "vtkImageData.h"
#include "vtkImageViewer.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include <fstream>
#include <vector>
int TestPNGReaderReadFromMemory(int argc, char* argv[])
{
if (argc <= 1)
{
cout << "Usage: " << argv[0] << " <png file>" << endl;
return EXIT_FAILURE;
}
std::string filename = argv[1];
// Open the file
std::ifstream stream(filename, std::ios::in | std::ios::binary);
if (!stream.is_open())
{
std::cerr << "Could not open file " << filename.c_str() << std::endl;
}
// Get file size
stream.seekg(0, ios::end);
unsigned int len = stream.tellg();
stream.seekg(0, ios::beg);
// Load the file into a buffer
std::vector<char> buffer = std::vector<char>(std::istreambuf_iterator<char>(stream), {});
// Initialize reader
vtkNew<vtkPNGReader> PNGReader;
PNGReader->SetMemoryBuffer(buffer.data());
PNGReader->SetMemoryBufferLength(len);
// Visualize
vtkNew<vtkImageViewer> imageViewer;
imageViewer->SetInputConnection(PNGReader->GetOutputPort());
imageViewer->SetColorWindow(256);
imageViewer->SetColorLevel(127.5);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
renderWindowInteractor->Start();
vtkRenderWindow* renWin = imageViewer->GetRenderWindow();
int retVal = vtkRegressionTestImage(renWin);
if (retVal == vtkRegressionTester::DO_INTERACTOR)
{
renderWindowInteractor->Start();
}
return !retVal;
}
2087623175d3079d0402fce808218e68b35905849f6bc7f5fd2b2cd17abde48e9f9408d72212ea8180b2b6db49d4e0ff238a8b0b78c4a73dc92fa390f2cc5892
2087623175d3079d0402fce808218e68b35905849f6bc7f5fd2b2cd17abde48e9f9408d72212ea8180b2b6db49d4e0ff238a8b0b78c4a73dc92fa390f2cc5892
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment