#include "Image_p.h" namespace renderive { Image::Image() : impl_(std::make_unique()) {} Image::Image(int width, int height) : impl_(std::make_unique()) { impl_->image.create(width, height, BL_FORMAT_PRGB32); } Image::~Image() = default; Image::Image(const Image& other) : impl_(std::make_unique()) { 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::size_bytes() const { BLImageData d; if (impl_->image.get_data(&d) != BL_SUCCESS) return 0; return static_cast(d.stride) * static_cast(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(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(sizeof(Pixel)); } } Pixel* Image::row(int y) { BLImageData d; if (impl_->image.get_data(&d) != BL_SUCCESS) return nullptr; auto* ptr = static_cast(d.pixel_data); return ptr + y * (d.stride / static_cast(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(d.pixel_data); return ptr + y * (d.stride / static_cast(sizeof(Pixel))); } Image_View Image::view() const { BLImageData d; if (impl_->image.get_data(&d) != BL_SUCCESS) return {}; return { static_cast(d.pixel_data), d.size.w, d.size.h, static_cast(d.stride), Pixel_Format::Premultiplied_32 }; } } // namespace renderive