#include #include #include // TODO: use headers in pic.h // TODO: create this struct or use std // https://en.cppreference.com/w/cpp/header/filesystem struct File {}; struct RGBPixel { uint8_t r; uint8_t g; uint8_t b; }; class Canvas { public: Canvas(std::size_t w, std::size_t h) : _width(w), _height(h) { _pixels = new RGBPixel[w * h]; } ~Canvas() { if (_pixels != nullptr) delete _pixels; } // there is a dynamic resource, no copy is allowed (deep nor shallow) Canvas(const Canvas& other) = delete; // moving is ok tho Canvas(Canvas&& other) = default; // get pixels // TODO: pay attention when the size of _pixels change no pointers are // pointing at that memory location RGBPixel * pixels() { if (_pixels == nullptr) throw std::logic_error("This canvas has no pixels, something went wrong"); return _pixels; } // get width and height std::size_t width() const { return _width; } std::size_t height() const { return _height; } // TODO: when width or height changes, _pixels needs to be reallocated with // the correct size std::size_t width(std::size_t new_width) {} std::size_t height(std::size_t new_height) {} private: std::size_t _width; std::size_t _height; RGBPixel *_pixels = nullptr; }; struct Picture { // map format to magic number enum class Format : unsigned long { GIF = 0xBEEFBEEFBEEF, JPG = 0x010101010101, }; Canvas canvas; Format format; Picture(Canvas&& c, Format f) : canvas(std::move(c)), format(f) {} // TODO: make it work std::size_t write(File f) const { unsigned long magic = static_cast(format); switch (format) { case Format::GIF: break; case Format::JPG: break; default: return 0; } } }; int main() { Picture pic(Canvas(1920, 1080), Picture::Format::GIF); return 0; }