Backport more common classes

This commit is contained in:
Connor McLaughlin
2022-07-11 19:45:31 +10:00
parent f6b3652ae6
commit af91fcf195
24 changed files with 1381 additions and 134 deletions

View File

@ -2,12 +2,11 @@
#include "assert.h"
#include "types.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <optional>
#include <vector>
class ByteStream;
namespace Common {
template<typename PixelType>
class Image
@ -86,17 +85,46 @@ public:
std::memcpy(m_pixels.data(), pixels, width * height * sizeof(PixelType));
}
private:
void SetPixels(u32 width, u32 height, std::vector<PixelType> pixels)
{
m_width = width;
m_height = height;
m_pixels = std::move(pixels);
}
std::vector<PixelType> TakePixels()
{
m_width = 0;
m_height = 0;
return std::move(m_pixels);
}
protected:
u32 m_width = 0;
u32 m_height = 0;
std::vector<PixelType> m_pixels;
};
using RGBA8Image = Image<u32>;
class RGBA8Image : public Image<u32>
{
public:
static constexpr int DEFAULT_SAVE_QUALITY = 85;
bool LoadImageFromFile(Common::RGBA8Image* image, const char* filename);
bool LoadImageFromBuffer(Common::RGBA8Image* image, const void* buffer, std::size_t buffer_size);
bool LoadImageFromStream(Common::RGBA8Image* image, ByteStream* stream);
bool WriteImageToFile(const Common::RGBA8Image& image, const char* filename);
RGBA8Image();
RGBA8Image(u32 width, u32 height, const u32* pixels);
RGBA8Image(const RGBA8Image& copy);
RGBA8Image(RGBA8Image&& move);
} // namespace Common
RGBA8Image& operator=(const RGBA8Image& copy);
RGBA8Image& operator=(RGBA8Image&& move);
bool LoadFromFile(const char* filename);
bool LoadFromFile(const char* filename, std::FILE* fp);
bool LoadFromBuffer(const char* filename, const void* buffer, size_t buffer_size);
bool SaveToFile(const char* filename, int quality = DEFAULT_SAVE_QUALITY) const;
bool SaveToFile(const char* filename, std::FILE* fp, int quality = DEFAULT_SAVE_QUALITY) const;
std::optional<std::vector<u8>> SaveToBuffer(const char* filename, int quality = DEFAULT_SAVE_QUALITY) const;
};
} // namespace Common