Fix issues with image loader jpeg

This commit is contained in:
Georg Hagen
2024-09-17 16:45:52 +02:00
parent 323e0feb4d
commit 53ccf4e0f1

View File

@@ -5,7 +5,9 @@
*/
#include "ImageLoaderJpeg.hpp"
#include <Data/Containers/Array.hpp>
#include "Base/Utils.hpp"
#include "Base/Logger.hpp"
#include "Data/Containers/Array.hpp"
#include <fstream>
#include <cstring>
@@ -20,10 +22,8 @@ namespace OpenVulkano::Image
{
std::unique_ptr<Image> ImageLoaderJpeg::loadFromFile(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::binary);
if (!file) { throw std::runtime_error("Could not open file: " + filePath); }
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return loadJpeg(buffer.data(), buffer.size());
const auto file = Utils::ReadFile(filePath);
return loadJpeg(reinterpret_cast<const uint8_t*>(file.Data()), file.Size());
}
std::unique_ptr<Image> ImageLoaderJpeg::loadFromMemory(const std::vector<uint8_t>& buffer)
@@ -35,21 +35,31 @@ namespace OpenVulkano::Image
{
Image result;
int rows, cols;
int rows = 0, cols = 0;
#if __has_include("turbojpeg.h")
{
unsigned char* compressedImage = const_cast<uint8_t*>(data);
int jpegSubsamp;
int jpegSubsamp = 0;
tjhandle jpegDecompressor = tjInitDecompress();
tjDecompressHeader2(jpegDecompressor, compressedImage, size, &cols, &rows, &jpegSubsamp);
if (!jpegDecompressor)
{
Logger::FILESYS->error("Failed to read jpeg header. Error: {}", tjGetErrorStr());
return nullptr;
}
int status = tjDecompressHeader2(jpegDecompressor, compressedImage, size, &cols, &rows, &jpegSubsamp);
if (status != 0)
{
Logger::FILESYS->error("Failed to read jpeg header. Error: {}", tjGetErrorStr());
return nullptr;
}
const int channels = 4;
result.data = OpenVulkano::Array<uint8_t>(cols * rows * channels);
result.dataFormat = OpenVulkano::DataFormat::R8G8B8A8_UINT;
result.dataFormat = OpenVulkano::DataFormat::B8G8R8A8_UINT;
tjDecompress2(jpegDecompressor, compressedImage, size, result.data.Data(), cols, 0 /*pitch*/, rows,
TJPF_RGBA, TJFLAG_FASTDCT);
TJPF_BGRA, TJFLAG_FASTDCT);
tjDestroy(jpegDecompressor);
}
#else