vtk-m2/vtkm/io/ImageReaderPNG.cxx
Kenneth Moreland 1e91b32ace Update image readers to style and library
Updated the image reader classes to better follow VTK-m naming
convention (i.e. use `ImageReaderPNG` instead of `PNGReader`).
The structure of the image reader class now also better matches
the interface for the VTK reader.

Also put the implementation of the image readers into the `vtkm_io`
library so that it only has to be compiled once.
2020-06-01 10:00:18 -06:00

104 lines
2.9 KiB
C++

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//============================================================================
#include <vtkm/io/ImageReaderPNG.h>
#include <vtkm/io/PixelTypes.h>
namespace
{
VTKM_CONT
template <typename PixelType>
vtkm::io::ImageReaderBase::ColorArrayType ReadFromPNG(const std::string& fileName,
vtkm::Id& width,
vtkm::Id& height)
{
unsigned char* imageData;
unsigned uwidth, uheight;
vtkm::png::lodepng_decode_file(&imageData,
&uwidth,
&uheight,
fileName.c_str(),
PixelType::PNG_COLOR_TYPE,
PixelType::BIT_DEPTH);
width = static_cast<vtkm::Id>(uwidth);
height = static_cast<vtkm::Id>(uheight);
// Fill in the data starting from the end (Images are read Top-Left to Bottom-Right,
// but are stored from Bottom-Left to Top-Right)
vtkm::cont::ArrayHandle<vtkm::Vec4f_32> array;
array.Allocate(width * height);
auto portal = array.WritePortal();
vtkm::Id vtkmIndex = 0;
for (vtkm::Id yIndex = static_cast<vtkm::Id>(height - 1); yIndex >= 0; yIndex--)
{
for (vtkm::Id xIndex = 0; xIndex < static_cast<vtkm::Id>(width); xIndex++)
{
vtkm::Id pngIndex = static_cast<vtkm::Id>(yIndex * width + xIndex);
portal.Set(vtkmIndex, PixelType(imageData, pngIndex).ToVec4f());
vtkmIndex++;
}
}
free(imageData);
return array;
}
} // anonymous namespace
namespace vtkm
{
namespace io
{
ImageReaderPNG::~ImageReaderPNG() noexcept
{
}
const vtkm::cont::DataSet& ImageReaderPNG::ReadDataSet(PixelType pixelType)
{
this->Read(pixelType);
return this->GetDataSet();
}
void ImageReaderPNG::Read()
{
this->Read(PixelType::RGB_16);
}
void ImageReaderPNG::Read(PixelType pixelType)
{
vtkm::io::ImageReaderBase::ColorArrayType pixelArray;
vtkm::Id width;
vtkm::Id height;
switch (pixelType)
{
case PixelType::RGB_8:
pixelArray = ReadFromPNG<vtkm::io::RGBPixel_8>(this->FileName, width, height);
break;
case PixelType::RGB_16:
pixelArray = ReadFromPNG<vtkm::io::RGBPixel_16>(this->FileName, width, height);
break;
case PixelType::GREY_8:
pixelArray = ReadFromPNG<vtkm::io::GreyPixel_8>(this->FileName, width, height);
break;
case PixelType::GREY_16:
pixelArray = ReadFromPNG<vtkm::io::GreyPixel_16>(this->FileName, width, height);
break;
}
this->InitializeImageDataSet(width, height, pixelArray);
}
}
} // namespace vtkm::io