implement png image loader and refactor existing loader classes

This commit is contained in:
ohyzha
2024-07-31 12:53:27 +03:00
parent 51608425c1
commit 847b8660b5
5 changed files with 108 additions and 15 deletions

View 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));
}
}