Backport more common classes
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user