Merge branch 'master' into LabelUpdate

This commit is contained in:
Georg Hagen
2025-01-06 18:04:18 +01:00
6 changed files with 537 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ImageLoaderPfm.hpp"
#include "Scene/DataFormat.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <sstream>
#include <stdexcept>
namespace
{
struct PfmImageHeader
{
int width;
int height;
bool isColor;
bool isLittleEndian;
};
PfmImageHeader ParseHeader(std::istringstream& stream)
{
PfmImageHeader header;
std::string magic;
stream >> magic;
header.isColor = (magic == "PF");
float endianess;
stream >> header.width >> header.height >> endianess;
stream.ignore();
header.isLittleEndian = endianess < 0;
return header;
}
}
namespace OpenVulkano::Image
{
std::unique_ptr<Image> ImageLoaderPfm::loadFromFile(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open())
{
throw std::runtime_error("Failed to open PFM file: " + filePath);
}
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return loadFromMemory(buffer);
}
std::unique_ptr<Image> ImageLoaderPfm::loadFromMemory(const std::vector<uint8_t>& buffer)
{
std::istringstream stream(std::string(buffer.begin(), buffer.end()), std::ios::binary);
PfmImageHeader header = ParseHeader(stream);
size_t numChannels = header.isColor ? 3 : 1;
size_t dataSize = header.width * header.height * numChannels * sizeof(float);
Array<float> data(header.width * header.height * numChannels);
size_t rowSize = header.width * numChannels * sizeof(float);
for (int y = header.height - 1; y >= 0; --y)
{
stream.read(reinterpret_cast<char*>(&data[y * header.width * numChannels]), rowSize);
if (!stream)
{
throw std::runtime_error("Error reading PFM image data");
}
}
if (!header.isLittleEndian)
{
for (float& value : data)
{
uint8_t* ptr = reinterpret_cast<uint8_t*>(&value);
std::reverse(ptr, ptr + sizeof(float));
}
}
auto image = std::make_unique<Image>();
image->resolution = { static_cast<uint32_t>(header.width), static_cast<uint32_t>(header.height), 1 };
image->dataFormat = header.isColor ? DataFormat::Format::R32G32B32_SFLOAT : DataFormat::Format::R32_SFLOAT;
image->data = std::move(*reinterpret_cast<Array<uint8_t>*>(&data));
return image;
}
Math::Vector2i ImageLoaderPfm::GetImageDimensions(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (!file.is_open())
{
throw std::runtime_error("Failed to open PFM file: " + filename);
}
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
std::istringstream stream(std::string(buffer.begin(), buffer.end()), std::ios::binary);
PfmImageHeader header = ParseHeader(stream);
return { header.width, header.height };
}
}

View File

@@ -0,0 +1,20 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Image/ImageLoader.hpp"
namespace OpenVulkano::Image
{
class ImageLoaderPfm : public IImageLoader
{
public:
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
Math::Vector2i GetImageDimensions(const std::string& filename) override;
};
}

View File

@@ -0,0 +1,194 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ImageLoaderPnm.hpp"
#include <sstream>
#include <fstream>
namespace
{
struct PnmHeader
{
char magic = 0; // 1 for P1, 2 for P2 etc
int width = 0;
int height = 0;
int maxVal = 255; // Only used for formats P2, P3, P5, P6
};
void MaybeSkipComments(std::istream& stream)
{
std::string line;
while (std::getline(stream, line))
{
line.erase(0, line.find_first_not_of(" \t"));
if (line.empty() || line[0] != '#')
{
stream.seekg(-static_cast<std::streamoff>(line.length()) - 1, std::ios::cur);
break;
}
}
}
PnmHeader ParseHeader(std::istream& stream)
{
PnmHeader header;
MaybeSkipComments(stream);
char pMagic;
stream >> pMagic;
if (pMagic != 'P')
{
throw std::runtime_error("Unsupported magic");
}
stream >> header.magic;
header.magic -= '0';
if (header.magic < 1 || header.magic > 6)
{
throw std::runtime_error("Unsupported magic number: P" + (header.magic + '0'));
}
MaybeSkipComments(stream);
stream >> header.width >> header.height;
if (header.magic != 1 && header.magic != 4)
{
MaybeSkipComments(stream);
stream >> header.maxVal;
}
stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return header;
}
OpenVulkano::Array<uint8_t> ReadBinaryData(std::istream& stream, const PnmHeader& header)
{
size_t dataSize = header.width * header.height * ((header.magic == 6) ? 3 : 1);
OpenVulkano::Array<uint8_t> data(dataSize);
if (header.magic == 4)
{
size_t rowBytes = (header.width + 7) / 8;
OpenVulkano::Array<uint8_t> rowBuffer(rowBytes);
for (int y = header.height - 1; y >= 0; --y)
{
stream.read(reinterpret_cast<char*>(rowBuffer.Data()), rowBytes);
size_t rowStart = y * header.width;
for (int x = 0; x < header.width; ++x)
{
size_t byteIndex = x / 8;
size_t bitIndex = 7 - (x % 8);
data[rowStart + x] = (rowBuffer[byteIndex] >> bitIndex) & 1 ? 255 : 0;
}
}
}
else
{
size_t bytesPerRow = header.width * ((header.magic == 6) ? 3 : 1);
OpenVulkano::Array<uint8_t> rowBuffer(bytesPerRow);
for (int y = header.height - 1; y >= 0; --y)
{
stream.read(reinterpret_cast<char*>(rowBuffer.Data()), bytesPerRow);
size_t rowStart = y * bytesPerRow;
std::memcpy(data.Data() + rowStart, rowBuffer.Data(), bytesPerRow);
}
}
return data;
}
OpenVulkano::Array<uint8_t> ReadTextData(std::istream& stream, const PnmHeader& header)
{
const size_t numChannels = (header.magic == 3) ? 3 : 1;
OpenVulkano::Array<uint8_t> data(header.width * header.height * numChannels);
const int PRECOMPUTED_RATIO = 255 / header.maxVal;
for (int y = header.height - 1; y >= 0; --y)
{
for (size_t x = 0; x < header.width * numChannels; ++x)
{
int value;
stream >> value;
if (header.magic == 1)
{
data[y * header.width * numChannels + x] = static_cast<uint8_t>(value * 255);
}
else
{
data[y * header.width * numChannels + x] = static_cast<uint8_t>(value * PRECOMPUTED_RATIO);
}
}
}
return data;
}
}
namespace OpenVulkano::Image
{
std::unique_ptr<Image> ImageLoaderPnm::loadFromFile(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open())
{
throw std::runtime_error("Failed to open file: " + filePath);
}
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return loadFromMemory(buffer);
}
std::unique_ptr<Image> ImageLoaderPnm::loadFromMemory(const std::vector<uint8_t>& buffer)
{
std::istringstream stream(std::string(buffer.begin(), buffer.end()));
PnmHeader header = ParseHeader(stream);
std::unique_ptr<Image> result = std::make_unique<Image>();
result->resolution.x = header.width;
result->resolution.y = header.height;
result->resolution.z = 1;
if (header.magic <= 3)
{
result->data = ReadTextData(stream, header);
}
else if (header.magic <= 6)
{
result->data = ReadBinaryData(stream, header);
}
else
{
throw std::runtime_error("Unsupported format: P" + (header.magic + '0'));
}
if (header.magic == 1 || header.magic == 2 || header.magic == 4 || header.magic == 5)
{
result->dataFormat = DataFormat::Format::R8_UINT;
}
else
{
result->dataFormat = DataFormat::Format::R8G8B8_UINT;
}
return result;
}
Math::Vector2i ImageLoaderPnm::GetImageDimensions(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (!file.is_open())
{
throw std::runtime_error("Failed to open file: " + filename);
}
PnmHeader header = ParseHeader(file);
return Math::Vector2i(header.width, header.height);
}
};

View File

@@ -0,0 +1,19 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Image/ImageLoader.hpp"
namespace OpenVulkano::Image
{
class ImageLoaderPnm : public IImageLoader
{
public:
std::unique_ptr<Image> loadFromFile(const std::string& filePath) override;
std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) override;
Math::Vector2i GetImageDimensions(const std::string& filename) override;
};
}

View File

@@ -0,0 +1,84 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_test_macros.hpp>
#include <sstream>
#include <vector>
#include "Image/ImageLoaderPfm.hpp"
#include "Scene/DataFormat.hpp"
using namespace OpenVulkano;
using namespace OpenVulkano::Image;
TEST_CASE("ImageLoaderPfm loads PFM images", "[ImageLoaderPfm]")
{
ImageLoaderPfm loader;
SECTION("Grayscale Pf image")
{
std::ostringstream pfStream;
pfStream << "Pf\n"
<< "4 2\n"
<< "-1.0\n";
for (int i = 0; i < 8; ++i)
{
float pixelValue = 1.0f;
pfStream.write(reinterpret_cast<const char*>(&pixelValue), sizeof(float));
}
std::string pfData = pfStream.str();
std::vector<uint8_t> pfBuffer(pfData.begin(), pfData.end());
auto image = loader.loadFromMemory(pfBuffer);
REQUIRE(image != nullptr);
REQUIRE(image->resolution.x == 4);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R32_SFLOAT);
const float expectedValue = 1.0f;
for (size_t i = 0; i < image->data.Size(); i += sizeof(float))
{
float value;
std::memcpy(&value, &image->data[i], sizeof(float));
REQUIRE(value == expectedValue);
}
}
SECTION("Color PF image")
{
std::ostringstream pfStream;
pfStream << "PF\n"
<< "3 2\n"
<< "-1.0\n";
for (int i = 0; i < 6 * 3; ++i)
{
float pixelValue = 1.0f;
pfStream.write(reinterpret_cast<const char*>(&pixelValue), sizeof(float));
}
std::string pfData = pfStream.str();
std::vector<uint8_t> pfBuffer(pfData.begin(), pfData.end());
auto image = loader.loadFromMemory(pfBuffer);
REQUIRE(image != nullptr);
REQUIRE(image->resolution.x == 3);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R32G32B32_SFLOAT);
const float expectedValue = 1.0f;
for (size_t i = 0; i < image->data.Size(); i += sizeof(float))
{
float value;
std::memcpy(&value, &image->data[i], sizeof(float));
REQUIRE(value == expectedValue);
}
}
}

View File

@@ -0,0 +1,114 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_test_macros.hpp>
#include "Image/ImageLoaderPnm.hpp"
#include "Scene/DataFormat.hpp"
#include <fstream>
using namespace OpenVulkano;
using namespace OpenVulkano::Image;
TEST_CASE("ImageLoaderPnm - Load P1 (ASCII Black & White)")
{
std::vector<uint8_t> p1Buffer = { 'P', '1', '\n', '4', ' ', '2', '\n', '1', ' ', '0', ' ', '1',
' ', '0', '\n', '0', ' ', '1', ' ', '0', ' ', '1', '\n' };
ImageLoaderPnm loader;
auto image = loader.loadFromMemory(p1Buffer);
REQUIRE(image->resolution.x == 4);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
REQUIRE(image->data == Array<uint8_t> { 0, 255, 0, 255, 255, 0, 255, 0 });
}
TEST_CASE("ImageLoaderPnm - Load P2 (ASCII Grayscale)")
{
std::vector<uint8_t> p2Buffer = { 'P', '2', '\n', '4', ' ', '2', '\n', '2', '5', '5', '\n', '0', ' ',
'6', '4', ' ', '1', '2', '8', ' ', '2', '5', '5', '\n', '2', '5',
'5', ' ', '1', '2', '8', ' ', '6', '4', ' ', '0', '\n' };
ImageLoaderPnm loader;
auto image = loader.loadFromMemory(p2Buffer);
REQUIRE(image->resolution.x == 4);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
REQUIRE(image->data == Array<uint8_t> { 255, 128, 64, 0, 0, 64, 128, 255 });
}
TEST_CASE("ImageLoaderPnm - Load P3 (ASCII RGB)")
{
std::vector<uint8_t> p3Buffer = { 'P', '3', '\n', '2', ' ', '2', '\n', '2', '5', '5', '\n',
'2', '5', '5', ' ', '0', ' ', '0', ' ', '0', ' ', '2', '5', '5', ' ', '0', '\n',
'0', ' ', '0', ' ', '2', '5', '5', ' ', '2', '5', '5', ' ', '2', '5', '5', ' ', '2', '5', '5', '\n' };
ImageLoaderPnm loader;
auto image = loader.loadFromMemory(p3Buffer);
REQUIRE(image->resolution.x == 2);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R8G8B8_UINT);
REQUIRE(image->data == Array<uint8_t> { 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 255, 0 });
}
TEST_CASE("ImageLoaderPnm - Load P4 (Binary Black & White)")
{
std::vector<uint8_t> p4Buffer = { 'P', '4', '\n', '8', ' ', '2', '\n', 0b10101010, 0b01010101 };
ImageLoaderPnm loader;
auto image = loader.loadFromMemory(p4Buffer);
REQUIRE(image->resolution.x == 8);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
REQUIRE(image->data
== Array<uint8_t> {
0, 255, 0, 255, 0, 255, 0, 255, // Row 1
255, 0, 255, 0, 255, 0, 255, 0 // Row 2
});
}
TEST_CASE("ImageLoaderPnm - Load P5 (Binary Grayscale)")
{
std::vector<uint8_t> p5Buffer = { 'P', '5', '\n', '4', ' ', '2', '\n', '2', '5', '5',
'\n', 0, 64, 128, 255, 255, 128, 64, 0 };
ImageLoaderPnm loader;
auto image = loader.loadFromMemory(p5Buffer);
REQUIRE(image->resolution.x == 4);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R8_UINT);
REQUIRE(image->data == Array<uint8_t> { 255, 128, 64, 0, 0, 64, 128, 255 });
}
TEST_CASE("ImageLoaderPnm - Load P6 (Binary RGB)")
{
std::vector<uint8_t> p6Buffer = { 'P', '6', '\n', '2', ' ', '2', '\n', '2', '5', '5', '\n', 255,
0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255 };
ImageLoaderPnm loader;
auto image = loader.loadFromMemory(p6Buffer);
REQUIRE(image->resolution.x == 2);
REQUIRE(image->resolution.y == 2);
REQUIRE(image->dataFormat == DataFormat::Format::R8G8B8_UINT);
REQUIRE(image->data == Array<uint8_t> { 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 255, 0 });
}
TEST_CASE("ImageLoaderPnm - Get Dimensions")
{
ImageLoaderPnm loader;
std::string mockFile = "mock.pnm";
std::ofstream outFile(mockFile);
outFile << "P6\n2 2\n255\n";
outFile.close();
auto dimensions = loader.GetImageDimensions(mockFile);
REQUIRE(dimensions.x == 2);
REQUIRE(dimensions.y == 2);
std::remove(mockFile.c_str());
}