implement png image loader and refactor existing loader classes
This commit is contained in:
53
openVulkanoCpp/Image/ImageLoader.cpp
Normal file
53
openVulkanoCpp/Image/ImageLoader.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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 "ImageLoader.hpp"
|
||||
#include "Base/Logger.hpp"
|
||||
#include <memory>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
namespace OpenVulkano::Image
|
||||
{
|
||||
std::unique_ptr<Image> IImageLoader::loadData(const uint8_t* data, int size, int desiredChannels)
|
||||
{
|
||||
Image result;
|
||||
int rows, cols, channels;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
uint8_t* pixelData = stbi_load_from_memory(data, static_cast<int>(size), &rows, &cols, &channels, desiredChannels);
|
||||
if (desiredChannels != 0 && channels < desiredChannels)
|
||||
{
|
||||
Logger::INPUT->warn(
|
||||
"Stbi load image channels mismatch. Desired channels = {}, actual amount of channels in image = {}",
|
||||
desiredChannels, channels);
|
||||
}
|
||||
result.data = OpenVulkano::Array<uint8_t>(cols * rows * channels);
|
||||
switch (channels)
|
||||
{
|
||||
case 1:
|
||||
result.dataFormat = OpenVulkano::DataFormat::R8_UNORM;
|
||||
break;
|
||||
case 2:
|
||||
result.dataFormat = OpenVulkano::DataFormat::R8G8_UNORM;
|
||||
break;
|
||||
case 3:
|
||||
result.dataFormat = OpenVulkano::DataFormat::R8G8B8_UNORM;
|
||||
break;
|
||||
case 4:
|
||||
result.dataFormat = OpenVulkano::DataFormat::R8G8B8A8_UNORM;
|
||||
break;
|
||||
}
|
||||
result.resolution.x = cols;
|
||||
result.resolution.y = rows;
|
||||
result.resolution.z = 1;
|
||||
std::memcpy(result.data.Data(), pixelData, result.data.Size());
|
||||
stbi_image_free(pixelData);
|
||||
return std::make_unique<Image>(std::move(result));
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ namespace OpenVulkano::Image
|
||||
public:
|
||||
virtual ~IImageLoader() = default;
|
||||
|
||||
static std::unique_ptr<Image> loadData(const uint8_t* data, int size, int desiredChannels = 0);
|
||||
virtual std::unique_ptr<Image> loadFromFile(const std::string& filePath) = 0;
|
||||
virtual std::unique_ptr<Image> loadFromMemory(const std::vector<uint8_t>& buffer) = 0;
|
||||
};
|
||||
|
||||
@@ -33,11 +33,11 @@ namespace OpenVulkano::Image
|
||||
|
||||
std::unique_ptr<Image> ImageLoaderJpeg::loadJpeg(const uint8_t* data, size_t size)
|
||||
{
|
||||
Image result;
|
||||
|
||||
int rows, cols;
|
||||
#if __has_include("turbojpeg.h")
|
||||
{
|
||||
Image result;
|
||||
|
||||
int rows, cols;
|
||||
unsigned char* compressedImage = const_cast<uint8_t*>(data);
|
||||
|
||||
int jpegSubsamp;
|
||||
@@ -51,22 +51,16 @@ namespace OpenVulkano::Image
|
||||
tjDecompress2(jpegDecompressor, compressedImage, size, result.data.Data(), cols, 0 /*pitch*/, rows,
|
||||
TJPF_RGBA, TJFLAG_FASTDCT);
|
||||
tjDestroy(jpegDecompressor);
|
||||
result.resolution.x = cols;
|
||||
result.resolution.y = rows;
|
||||
result.resolution.z = 1;
|
||||
|
||||
return std::make_unique<Image>(std::move(result));
|
||||
}
|
||||
#else
|
||||
{
|
||||
int channels;
|
||||
uint8_t* pixelData = stbi_load_from_memory(data, size, &cols, &rows, &channels, 3);
|
||||
result.data = OpenVulkano::Array<uint8_t>(cols * rows * channels);
|
||||
result.dataFormat = channels == 3 ? OpenVulkano::DataFormat::R8G8B8_UINT :
|
||||
OpenVulkano::DataFormat::R8G8B8A8_UINT;
|
||||
std::memcpy(result.data.Data(), pixelData, result.data.Size());
|
||||
stbi_image_free(pixelData);
|
||||
return loadData(data, static_cast<int>(size), 3);
|
||||
}
|
||||
#endif
|
||||
result.resolution.x = cols;
|
||||
result.resolution.y = rows;
|
||||
result.resolution.z = 1;
|
||||
|
||||
return std::make_unique<Image>(std::move(result));
|
||||
}
|
||||
}
|
||||
26
openVulkanoCpp/Image/ImageLoaderPng.cpp
Normal file
26
openVulkanoCpp/Image/ImageLoaderPng.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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 "ImageLoaderPng.hpp"
|
||||
#include <Data/Containers/Array.hpp>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
|
||||
namespace OpenVulkano::Image
|
||||
{
|
||||
std::unique_ptr<Image> ImageLoaderPng::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 loadData(buffer.data(), static_cast<int>(buffer.size()));
|
||||
}
|
||||
|
||||
std::unique_ptr<Image> ImageLoaderPng::loadFromMemory(const std::vector<uint8_t>& buffer)
|
||||
{
|
||||
return loadData(buffer.data(), static_cast<int>(buffer.size()));
|
||||
}
|
||||
}
|
||||
19
openVulkanoCpp/Image/ImageLoaderPng.hpp
Normal file
19
openVulkanoCpp/Image/ImageLoaderPng.hpp
Normal 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 "ImageLoader.hpp"
|
||||
|
||||
namespace OpenVulkano::Image
|
||||
{
|
||||
class ImageLoaderPng : 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;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user