Refactor for easier use in other projects

This commit is contained in:
2024-08-04 13:03:16 -04:00
parent e161d0d9a0
commit bca63534d1
3 changed files with 21 additions and 21 deletions

View File

@@ -5,7 +5,7 @@
#include <ReTexture/flags.h> #include <ReTexture/flags.h>
namespace ReTexture { namespace ReTexture {
class Texture { class SoftwareTexture {
private: private:
unsigned int width = 0; unsigned int width = 0;
unsigned int height = 0; unsigned int height = 0;
@@ -17,10 +17,10 @@ namespace ReTexture {
void invertY(); void invertY();
public: public:
std::vector<unsigned char> pixelData; std::vector<unsigned char> pixelData;
explicit Texture(const std::string& file); explicit SoftwareTexture(const std::string& file);
Texture(const std::string& file, const TextureFlag& flags); SoftwareTexture(const std::string& file, const TextureFlag& flags);
Texture(const std::vector<unsigned char>& pixel_data, TextureFormat format, unsigned int width, unsigned int height); SoftwareTexture(const std::vector<unsigned char>& pixel_data, TextureFormat format, unsigned int width, unsigned int height);
Texture downscale(unsigned int rhs); SoftwareTexture downscale(unsigned int rhs);
[[nodiscard]] unsigned int getWidth() const; [[nodiscard]] unsigned int getWidth() const;
[[nodiscard]] unsigned int getHeight() const; [[nodiscard]] unsigned int getHeight() const;
TextureFormat getTextureFormat(); TextureFormat getTextureFormat();

View File

@@ -3,7 +3,7 @@
using namespace ReTexture; using namespace ReTexture;
int main() { int main() {
auto* bmp = new Texture("testImages/1.bmp", {TextureFlag::INVERT_Y}); auto* bmp = new SoftwareTexture("testImages/1.bmp", {TextureFlag::INVERT_Y});
std::cout << "Bitmap Width: " << bmp->getWidth() << std::endl; std::cout << "Bitmap Width: " << bmp->getWidth() << std::endl;
std::cout << "Bitmap Height: " << bmp->getHeight() << std::endl; std::cout << "Bitmap Height: " << bmp->getHeight() << std::endl;
@@ -14,7 +14,7 @@ int main() {
std::cout << "Bitmap Format: RGBA" << std::endl; std::cout << "Bitmap Format: RGBA" << std::endl;
delete bmp; delete bmp;
auto* png = new Texture("testImages/1.png", TextureFlag::INVERT_Y); auto* png = new SoftwareTexture("testImages/1.png", TextureFlag::INVERT_Y);
std::cout << "PNG width: " << png->getWidth() << std::endl; std::cout << "PNG width: " << png->getWidth() << std::endl;
std::cout << "PNG height: " << png->getHeight() << std::endl; std::cout << "PNG height: " << png->getHeight() << std::endl;

View File

@@ -8,7 +8,7 @@
namespace ReTexture { namespace ReTexture {
Texture::Texture(const std::string &file, const TextureFlag& flags) { SoftwareTexture::SoftwareTexture(const std::string &file, const TextureFlag& flags) {
load(file); load(file);
if (flags & TextureFlag::INVERT_Y) if (flags & TextureFlag::INVERT_Y)
invertY(); invertY();
@@ -16,11 +16,11 @@ namespace ReTexture {
this->flags = flags; this->flags = flags;
} }
Texture::Texture(const std::string &file) { SoftwareTexture::SoftwareTexture(const std::string &file) {
*this = Texture(file, {}); *this = SoftwareTexture(file, {});
} }
Texture::Texture(const std::vector<unsigned char>& pixel_data, TextureFormat format, unsigned int width, unsigned int height) { SoftwareTexture::SoftwareTexture(const std::vector<unsigned char>& pixel_data, TextureFormat format, unsigned int width, unsigned int height) {
pixelData = pixel_data; pixelData = pixel_data;
this->width = width; this->width = width;
this->height = height; this->height = height;
@@ -29,7 +29,7 @@ namespace ReTexture {
} }
void Texture::load(const std::string &file) { void SoftwareTexture::load(const std::string &file) {
std::ifstream ifStream(file, std::ios::in | std::ios::binary); std::ifstream ifStream(file, std::ios::in | std::ios::binary);
unsigned char bmpFileHeader[14]; unsigned char bmpFileHeader[14];
@@ -45,7 +45,7 @@ namespace ReTexture {
loadPNG(file); loadPNG(file);
} }
void Texture::loadBMP(const std::string &file) { void SoftwareTexture::loadBMP(const std::string &file) {
std::ifstream bmpFile(file, std::ios::in | std::ios::binary); std::ifstream bmpFile(file, std::ios::in | std::ios::binary);
if (!bmpFile.is_open()) if (!bmpFile.is_open())
return; return;
@@ -75,7 +75,7 @@ namespace ReTexture {
format = TextureFormat::RGB; format = TextureFormat::RGB;
} }
void Texture::loadPNG(const std::string &file) { void SoftwareTexture::loadPNG(const std::string &file) {
int channels, w, h; int channels, w, h;
unsigned char* imageData = stbi_load(file.c_str(), &w, &h, &channels, 0); unsigned char* imageData = stbi_load(file.c_str(), &w, &h, &channels, 0);
@@ -95,7 +95,7 @@ namespace ReTexture {
stbi_image_free(imageData); stbi_image_free(imageData);
} }
void Texture::invertY() { void SoftwareTexture::invertY() {
unsigned int rowSize; unsigned int rowSize;
if (format == TextureFormat::RGB) if (format == TextureFormat::RGB)
rowSize = width * 3; rowSize = width * 3;
@@ -112,23 +112,23 @@ namespace ReTexture {
} }
} }
unsigned int Texture::getWidth() const { unsigned int SoftwareTexture::getWidth() const {
return width; return width;
} }
unsigned int Texture::getHeight() const { unsigned int SoftwareTexture::getHeight() const {
return height; return height;
} }
TextureFormat Texture::getTextureFormat() { TextureFormat SoftwareTexture::getTextureFormat() {
return format; return format;
} }
TextureFlag Texture::getFlags() { TextureFlag SoftwareTexture::getFlags() {
return flags; return flags;
} }
Texture Texture::downscale(unsigned int rhs) { SoftwareTexture SoftwareTexture::downscale(unsigned int rhs) {
std::vector<unsigned char> result; std::vector<unsigned char> result;
if (format == TextureFormat::RGB) if (format == TextureFormat::RGB)
@@ -139,6 +139,6 @@ namespace ReTexture {
result.resize((width / rhs) * (height / rhs) * 4), result.resize((width / rhs) * (height / rhs) * 4),
stbir_resize_uint8_linear(pixelData.data(), width, height, 0, result.data(), (width / rhs), (height / rhs), 0, stbir_pixel_layout::STBIR_RGBA); stbir_resize_uint8_linear(pixelData.data(), width, height, 0, result.data(), (width / rhs), (height / rhs), 0, stbir_pixel_layout::STBIR_RGBA);
return Texture(result, format, (width / rhs), (height / rhs)); return SoftwareTexture(result, format, (width / rhs), (height / rhs));
} }
} }