-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "image.h" | ||
|
||
#include <algorithm> | ||
#include <cstring> | ||
#include <span> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#define STB_IMAGE_IMPLEMENTATION | ||
#include "stb_image.h" | ||
#define STB_IMAGE_WRITE_IMPLEMENTATION | ||
#include "stb_image_write.h" | ||
|
||
Image::Image(std::span<const uint8_t> data) { | ||
data_ = (uint32_t *)stbi_load_from_memory(data.data(), data.size(), &width, | ||
&height, nullptr, 4); | ||
if (data_ == nullptr) { | ||
throw std::runtime_error("failed to load image"); | ||
} | ||
} | ||
|
||
Image::Image(const std::string &path) { | ||
data_ = (uint32_t *)stbi_load(path.c_str(), &width, &height, nullptr, 4); | ||
if (data_ == nullptr) { | ||
throw std::runtime_error("failed to load image"); | ||
} | ||
} | ||
|
||
Image::Image(int width, int height) : width(width), height(height) { | ||
data_ = (uint32_t *)malloc(width * height * 4); | ||
std::memset(data_, 0, width * height * 4); | ||
} | ||
|
||
Image::Image(Image &&other) noexcept { | ||
data_ = other.data_; | ||
width = other.width; | ||
height = other.height; | ||
|
||
other.data_ = nullptr; | ||
other.width = 0; | ||
other.height = 0; | ||
} | ||
|
||
Image::Image(const char *data, size_t size) { | ||
data_ = (uint32_t *)stbi_load_from_memory((stbi_uc *)data, size, &width, | ||
&height, nullptr, 4); | ||
if (data_ == nullptr) { | ||
throw std::runtime_error("failed to load image"); | ||
} | ||
} | ||
|
||
Image::~Image() { free(data_); } | ||
|
||
void Image::save_png(const std::string &path) { | ||
stbi_write_png(path.c_str(), width, height, 4, data_, width * 4); | ||
} | ||
|
||
const void *Image::get_png(int *len) { | ||
return stbi_write_png_to_mem((const unsigned char *)data_, width * 4, width, | ||
height, 4, len); | ||
} | ||
|
||
void Image::save_png_from_data(const std::string &path, const void *data, int w, | ||
int h) { | ||
stbi_write_png(path.c_str(), w, h, 4, data, w * 4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#pragma once | ||
|
||
#include <span> | ||
#include <string> | ||
#include <vector> | ||
|
||
struct S32Vec2 { | ||
int x; | ||
int y; | ||
}; | ||
|
||
struct FVec2 { | ||
float x; | ||
float y; | ||
}; | ||
|
||
struct U16Vec2 { | ||
uint16_t x; | ||
uint16_t y; | ||
}; | ||
|
||
class Image { | ||
uint32_t *data_ = nullptr; | ||
int width = 0, height = 0; | ||
|
||
public: | ||
Image() {} | ||
Image(std::span<const uint8_t> data); | ||
Image(const std::string &path); | ||
Image(int width, int height); | ||
Image(S32Vec2 size) : Image(size.x, size.y) {} | ||
Image(const char *data, size_t size); | ||
|
||
Image(Image &&other) noexcept; | ||
Image(const Image &) = delete; | ||
Image &operator=(const Image &) = delete; | ||
|
||
~Image(); | ||
|
||
void save_png(const std::string &path); | ||
const void *get_png(int *len); | ||
|
||
S32Vec2 size() const { return {width, height}; } | ||
uint32_t *data() { return data_; } | ||
const uint32_t *data() const { return data_; } | ||
|
||
uint32_t operator()(int x, int y) const { return data_[x + y * width]; } | ||
uint32_t &operator()(int x, int y) { return data_[x + y * width]; } | ||
|
||
bool operator==(const Image &other) const { | ||
if (width != other.width || height != other.height) | ||
return false; | ||
for (int i = 0; i < width * height; ++i) { | ||
if (data_[i] != other.data_[i]) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
static void save_png_from_data(const std::string &path, const void *data, | ||
int w, int h); | ||
}; |