--- nono/lib/bitmap.h 2026/04/29 17:05:15 1.1.1.3 +++ nono/lib/bitmap.h 2026/04/29 17:05:22 1.1.1.4 @@ -18,13 +18,19 @@ class BitmapBase { protected: - BitmapBase(); - BitmapBase(int width_, int height_, int depth_); + BitmapBase(int depth_, int width_, int height_, const void *buf_); - bool Create(int width_, int height_, int depth_); + // サイズを指定してキャンバスを作成。 + // buf_ は NULL ならこっちでメモリを確保。 + void Create(int width_, int height_, const void *buf_); public: virtual ~BitmapBase(); + // サイズを指定してキャンバスを作成。 + void Create(int width_, int height_) { + Create(width_, height_, NULL); + } + int GetWidth() const { return width; } int GetHeight() const { return height; } int GetStride() const { return stride; } @@ -32,10 +38,27 @@ class BitmapBase uint8 *GetBuf() { return buf; } const uint8 *GetBuf() const { return buf; } + // y 行目先頭のアドレスを返す + uint8 *GetRowPtr(int y) const { + assertmsg(y < height, "y=%d height=%d", y, height); + return &buf[y * stride]; + } + + // (x, y) のアドレスを返す + virtual uint8 *GetPtr(int x, int y) const { + assertmsg(x < width, "x=%d width=%d", x, width); + assertmsg(y < height, "y=%d height=%d", y, height); + return &buf[y * stride + x * depth / 8]; + } + + // 全域をコピーしてくる。 + void CopyFrom(const BitmapBase *src); + protected: int width {}; int height {}; int stride {}; + int depth {}; uint8 *buf {}; std::unique_ptr owned_buf {}; @@ -51,10 +74,7 @@ class BitmapI1 : public BitmapBase BitmapI1(const void *buf_, int width_, int height_); ~BitmapI1() override; - void Create(int width_, int height_); - - // カラーコード cc で全体を塗りつぶす - void Fill(int cc); + uint8 *GetPtr(int x, int y) const override; }; // 8bpp ビットマップ @@ -67,21 +87,8 @@ class BitmapI8 : public BitmapBase BitmapI8(const void *buf_, int width_, int height_); ~BitmapI8() override; - void Create(int width_, int height_); - void Create(const void *buf_, int width_, int height_); - // カラーコード cc で全体を塗りつぶす void Fill(int cc); - - uint8 *GetRowPtr(int y) const { - assertmsg(y < height, "y=%d height=%d", y, height); - return &buf[y * stride]; - } - uint8 *GetPtr(int x, int y) const { - assertmsg(x < width, "x=%d width=%d", x, width); - assertmsg(y < height, "y=%d height=%d", y, height); - return &buf[y * stride + x]; - } }; // 24bpp ビットマップ @@ -94,18 +101,19 @@ class BitmapRGB : public BitmapBase BitmapRGB(const void *buf_, int width_, int height_); ~BitmapRGB() override; - void Create(int width_, int height_); - // 色 c で全体を塗りつぶす - void Fill(Color c); + void Fill(Color c) { + FillRect(c, Rect(0, 0, width, height)); + } + void FillRect(Color c, int dx, int dy, int dw, int dh) { + FillRect(c, Rect(dx, dy, dw, dh)); + } + void FillRect(Color c, const Rect& rect); void DrawLine(Color c, int x1, int y1, int x2, int y2); void DrawLineH(Color c, int x1, int y1, int x2); void DrawLineV(Color c, int x1, int y1, int y2); - void FillRect(Color c, const Rect& rect); - void FillRect(Color c, int dx, int dy, int dw, int dh); - void DrawRect(Color c, const Rect& rect); void DrawRect(Color c, int dx, int dy, int dw, int dh); @@ -121,16 +129,17 @@ class BitmapRGB : public BitmapBase void StretchDraw(Rect dr, const BitmapRGB& src); - uint8 *GetRowPtr(int y) const { - assertmsg(y < height, "y=%d height=%d", y, height); - return &buf[y * stride]; - } - uint8 *GetPtr(int x, int y) const { - assertmsg(x < width, "x=%d width=%d", x, width); - assertmsg(y < height, "y=%d height=%d", y, height); - return &buf[y * stride + x * 3]; - } - private: Color Mean(const RectD& sr) const; }; + +// 32bpp (RGBX) ビットマップ +class BitmapRGBX : public BitmapBase +{ + using inherited = BitmapBase; + public: + BitmapRGBX(); + BitmapRGBX(int width_, int height_); + BitmapRGBX(const void *buf_, int width_, int height_); + ~BitmapRGBX() override; +};