thread pool update + refactor

This commit is contained in:
MihailRis
2024-04-11 15:48:27 +03:00
parent 1549a02731
commit 90d0b54a69
17 changed files with 271 additions and 103 deletions
+49
View File
@@ -0,0 +1,49 @@
#include "imageio.h"
#include "png.h"
#include "../graphics/core/ImageData.h"
#include <filesystem>
#include <functional>
#include <unordered_map>
namespace fs = std::filesystem;
using image_reader = std::function<ImageData*(const std::string&)>;
using image_writer = std::function<void(const std::string&, const ImageData*)>;
static std::unordered_map<std::string, image_reader> readers {
{".png", png::load_image},
};
static std::unordered_map<std::string, image_writer> writers {
{".png", png::write_image},
};
bool imageio::is_read_supported(const std::string& extension) {
return readers.find(extension) != readers.end();
}
bool imageio::is_write_supported(const std::string& extension) {
return writers.find(extension) != writers.end();
}
inline std::string extensionOf(const std::string& filename) {
return fs::u8path(filename).extension().u8string();
}
std::unique_ptr<ImageData> imageio::read(const std::string& filename) {
auto found = readers.find(extensionOf(filename));
if (found == readers.end()) {
throw std::runtime_error("file format is not supported (read): "+filename);
}
return std::unique_ptr<ImageData>(found->second(filename));
}
void imageio::write(const std::string& filename, const ImageData* image) {
auto found = writers.find(extensionOf(filename));
if (found == writers.end()) {
throw std::runtime_error("file format is not supported (write): "+filename);
}
return found->second(filename, image);
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef CODERS_IMAGEIO_H_
#define CODERS_IMAGEIO_H_
#include <string>
#include <memory>
class ImageData;
namespace imageio {
inline const std::string PNG = ".png";
bool is_read_supported(const std::string& extension);
bool is_write_supported(const std::string& extension);
std::unique_ptr<ImageData> read(const std::string& filename);
void write(const std::string& filename, const ImageData* image);
}
#endif // CODERS_IMAGEIO_H_
+3 -3
View File
@@ -340,7 +340,7 @@ ImageData* _png_load(const char* file){
}
#endif
ImageData* png::load_image(std::string filename) {
ImageData* png::load_image(const std::string& filename) {
ImageData* image (_png_load(filename.c_str()));
if (image == nullptr) {
throw std::runtime_error("could not load image "+filename);
@@ -348,13 +348,13 @@ ImageData* png::load_image(std::string filename) {
return image;
}
Texture* png::load_texture(std::string filename) {
Texture* png::load_texture(const std::string& filename) {
std::unique_ptr<ImageData> image (load_image(filename));
auto texture = Texture::from(image.get());
texture->setNearestFilter();
return texture;
}
void png::write_image(std::string filename, const ImageData* image) {
void png::write_image(const std::string& filename, const ImageData* image) {
_png_write(filename.c_str(), image->getWidth(), image->getHeight(), (const ubyte*)image->getData(), image->getFormat() == ImageFormat::rgba8888);
}
+3 -3
View File
@@ -8,9 +8,9 @@ class Texture;
class ImageData;
namespace png {
extern ImageData* load_image(std::string filename);
extern void write_image(std::string filename, const ImageData* image);
extern Texture* load_texture(std::string filename);
extern ImageData* load_image(const std::string& filename);
extern void write_image(const std::string& filename, const ImageData* image);
extern Texture* load_texture(const std::string& filename);
}
#endif /* CODERS_PNG_H_ */