TMP / Fixed image.cpp

This commit is contained in:
OLEGSHA 2023-03-28 22:31:15 +02:00
parent 2fb324672a
commit 770409140f

View File

@ -4,6 +4,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <limits>
#include <vector> #include <vector>
#include "stb/stb_image.h" #include "stb/stb_image.h"
@ -23,7 +24,7 @@ Image::Byte *Image::getData() { return data.data(); }
Image loadImage(const std::string &path) { Image loadImage(const std::string &path) {
auto resource = __embedded_resources::getEmbeddedResource(path.c_str()); auto resource = __embedded_resources::getEmbeddedResource(path);
if (resource.data == nullptr) { if (resource.data == nullptr) {
// REPORT_ERROR // REPORT_ERROR
@ -35,15 +36,22 @@ Image loadImage(const std::string &path) {
std::vector<Image::Byte> png(resource.data, std::vector<Image::Byte> png(resource.data,
resource.data + resource.length); resource.data + resource.length);
int width; if (png.size() > std::numeric_limits<int>::max()) {
int height; // REPORT_ERROR
int channelsInFile; progressia::main::logging::fatal()
<< "Could not load \"" << path << "\": image file too large";
exit(1);
}
Image::Byte *stbAllocatedData = int dataSize = static_cast<int>(png.size());
stbi_load_from_memory(png.data(), png.size(), &width, &height, int width = 0;
&channelsInFile, STBI_rgb_alpha); int height = 0;
int channelsInFile = 0;
if (stbAllocatedData == NULL) { Image::Byte *stbAllocatedData = stbi_load_from_memory(
png.data(), dataSize, &width, &height, &channelsInFile, STBI_rgb_alpha);
if (stbAllocatedData == nullptr) {
fatal() << "Could not decode a PNG image from file " << path; fatal() << "Could not decode a PNG image from file " << path;
// REPORT_ERROR // REPORT_ERROR
exit(1); exit(1);