Files
Renderive/Core/render/Image.cpp
T
2026-08-02 03:43:39 +08:00

109 lines
2.6 KiB
C++

#include "Image_p.h"
namespace renderive {
Image::Image() : impl_(std::make_unique<Impl>()) {}
Image::Image(int width, int height) : impl_(std::make_unique<Impl>()) {
impl_->image.create(width, height, BL_FORMAT_PRGB32);
}
Image::~Image() = default;
Image::Image(const Image& other) : impl_(std::make_unique<Impl>()) {
if (!other.impl_->image.is_empty())
impl_->image.assign_deep(other.impl_->image);
}
Image::Image(Image&& other) noexcept = default;
Image& Image::operator=(const Image& other) {
if (this != &other) {
if (other.impl_->image.is_empty())
impl_->image.reset();
else
impl_->image.assign_deep(other.impl_->image);
}
return *this;
}
Image& Image::operator=(Image&& other) noexcept = default;
int Image::width() const {
return impl_->image.width();
}
int Image::height() const {
return impl_->image.height();
}
Size Image::size() const {
return {impl_->image.width(), impl_->image.height()};
}
bool Image::empty() const {
return impl_->image.is_empty();
}
std::size_t Image::byte_size() const {
BLImageData d;
if (impl_->image.get_data(&d) != BL_SUCCESS)
return 0;
return static_cast<std::size_t>(d.stride) * static_cast<std::size_t>(d.size.h);
}
void Image::clear() {
impl_->image.reset();
}
void Image::resize(int width, int height) {
impl_->image.create(width, height, BL_FORMAT_PRGB32);
}
void Image::fill(Color color) {
Pixel p = premultiply(color);
int w = impl_->image.width();
int h = impl_->image.height();
BLImageData d;
if (impl_->image.get_data(&d) != BL_SUCCESS)
return;
auto* ptr = static_cast<Pixel*>(d.pixel_data);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
ptr[x] = p;
}
ptr += d.stride / static_cast<int>(sizeof(Pixel));
}
}
Pixel* Image::row(int y) {
BLImageData d;
if (impl_->image.get_data(&d) != BL_SUCCESS)
return nullptr;
auto* ptr = static_cast<Pixel*>(d.pixel_data);
return ptr + y * (d.stride / static_cast<int>(sizeof(Pixel)));
}
const Pixel* Image::row(int y) const {
BLImageData d;
if (impl_->image.get_data(&d) != BL_SUCCESS)
return nullptr;
auto* ptr = static_cast<const Pixel*>(d.pixel_data);
return ptr + y * (d.stride / static_cast<int>(sizeof(Pixel)));
}
Image_View Image::view() const {
BLImageData d;
if (impl_->image.get_data(&d) != BL_SUCCESS)
return {};
return {
static_cast<const std::byte*>(d.pixel_data),
d.size.w,
d.size.h,
static_cast<int>(d.stride),
Pixel_Format::Premultiplied_32
};
}
} // namespace renderive