--- nono/lib/bitmap.cpp 2026/04/29 17:05:15 1.1.1.3 +++ nono/lib/bitmap.cpp 2026/04/29 17:05:22 1.1.1.4 @@ -16,14 +16,11 @@ // // コンストラクタ -BitmapBase::BitmapBase() +BitmapBase::BitmapBase(int depth_, int width_, int height_, const void *buf_) + : depth(depth_) { -} - -// コンストラクタ -BitmapBase::BitmapBase(int width_, int height_, int depth_) -{ - Create(width_, height_, depth_); + assert(depth == 1 || (depth % 8) == 0); + Create(width_, height_, buf_); } // デストラクタ @@ -31,22 +28,41 @@ BitmapBase::~BitmapBase() { } -// 作成。width_ * height_ が面積を持っていれば true を返す。 -bool -BitmapBase::Create(int width_, int height_, int depth_) +// サイズを指定してビットマップを作成する。 +// width_, height_ は 0 が指定される場合もある(初期状態)。 +// buf_ が NULL ならこっちでメモリを確保。 +void +BitmapBase::Create(int width_, int height_, const void *buf_) { - assert(depth_ == 1 || depth_ == 8 || depth_ == 24); - - width = width_; + width = width_; height = height_; - if (depth_ == 1) { + if (depth == 1) { stride = (width + 7) / 8; } else { - stride = width * depth_ / 8; + stride = width * depth / 8; + } + + owned_buf.reset(); + if (buf_) { + buf = static_cast(const_cast(buf_)); + } else { + if (width > 0 && height > 0) { + owned_buf.reset(new uint8[stride * height]); + } + buf = owned_buf.get(); } +} + +// 全域をコピーしてくる。 +void +BitmapBase::CopyFrom(const BitmapBase *src) +{ + assert(src); + assert(stride == src->stride); + assert(height == src->height); - return (width > 0 && height > 0); + memcpy(buf, src->buf, stride * height); } @@ -56,20 +72,20 @@ BitmapBase::Create(int width_, int heigh // コンストラクタ BitmapI1::BitmapI1() + : BitmapI1(0, 0) // 移譲 { } // コンストラクタ BitmapI1::BitmapI1(int width_, int height_) + : BitmapI1(NULL, width_, height_) // 移譲 { - Create(width_, height_); } // コンストラクタ BitmapI1::BitmapI1(const void *buf_, int width_, int height_) - : inherited(width_, height_, 1) + : inherited(1, width_, height_, buf_) { - buf = static_cast(const_cast(buf_)); } // デストラクタ @@ -77,27 +93,12 @@ BitmapI1::~BitmapI1() { } -// 作成 -void -BitmapI1::Create(int width_, int height_) +// (x, y) のアドレスを返す関数だが、I1 ではサポートしていない。 +uint8 * +BitmapI1::GetPtr(int x, int y) const { - if (inherited::Create(width_, height_, 1)) { - owned_buf.reset(new uint8[stride * height]); - buf = owned_buf.get(); - } else { - owned_buf.reset(); - buf = NULL; - } -} - -// カラーコード cc で全体を塗りつぶす。 -// 未使用ビットも変更される。 -void -BitmapI1::Fill(int cc) -{ - assert(buf); - - memset(buf, (cc ? 0xff : 0), stride * height); + assertmsg(false, "GetPtr() not work for BitmapI1"); + return NULL; } @@ -107,19 +108,20 @@ BitmapI1::Fill(int cc) // コンストラクタ BitmapI8::BitmapI8() + : BitmapI8(0, 0) // 移譲 { } // コンストラクタ BitmapI8::BitmapI8(int width_, int height_) + : BitmapI8(NULL, width_, height_) // 移譲 { - Create(width_, height_); } // コンストラクタ BitmapI8::BitmapI8(const void *buf_, int width_, int height_) + : inherited(8, width_, height_, buf_) { - Create(buf_, width_, height_); } // デストラクタ @@ -127,30 +129,6 @@ BitmapI8::~BitmapI8() { } -// 作成 -void -BitmapI8::Create(int width_, int height_) -{ - if (inherited::Create(width_, height_, 8)) { - owned_buf.reset(new uint8[stride * height]); - buf = owned_buf.get(); - } else { - owned_buf.reset(); - buf = NULL; - } -} - -// 元データを指定して作成 -void -BitmapI8::Create(const void *buf_, int width_, int height_) -{ - if (inherited::Create(width_, height_, 8)) { - buf = static_cast(const_cast(buf_)); - } else { - buf = NULL; - } -} - // カラーコード cc で全体を塗りつぶす。 // 未使用ビットも変更される。 void @@ -168,20 +146,20 @@ BitmapI8::Fill(int cc) // コンストラクタ BitmapRGB::BitmapRGB() + : BitmapRGB(0, 0) // 移譲 { } // コンストラクタ BitmapRGB::BitmapRGB(int width_, int height_) + : BitmapRGB(NULL, width_, height_) // 移譲 { - Create(width_, height_); } // コンストラクタ BitmapRGB::BitmapRGB(const void *buf_, int width_, int height_) - : inherited(width_, height_, 24) + : inherited(24, width_, height_, buf_) { - buf = static_cast(const_cast(buf_)); } // デストラクタ @@ -189,26 +167,6 @@ BitmapRGB::~BitmapRGB() { } -// 作成 -void -BitmapRGB::Create(int width_, int height_) -{ - if (inherited::Create(width_, height_, 24)) { - owned_buf.reset(new uint8[stride * height]); - buf = owned_buf.get(); - } else { - owned_buf.reset(); - buf = NULL; - } -} - -// 色 c で全体を塗りつぶす。 -void -BitmapRGB::Fill(Color c) -{ - FillRect(c, 0, 0, width, height); -} - // (dx, dy) にビットマップ src を描画する。 // 描画範囲をはみ出さないこと。 void @@ -560,14 +518,6 @@ BitmapRGB::DrawLineV(Color c, int x1, in } } -// 矩形 (dx, dy) - (dw * dh) を色 c で塗りつぶす。 -// 描画範囲をはみ出さないこと。 -void -BitmapRGB::FillRect(Color c, int dx, int dy, int dw, int dh) -{ - FillRect(c, Rect(dx, dy, dw, dh)); -} - // 矩形 rect を色 c で塗りつぶす。 // 描画範囲をはみ出さないこと。 void @@ -615,3 +565,31 @@ BitmapRGB::DrawRect(Color c, const Rect& DrawLineH(c, r, b, l); // 下辺← DrawLineV(c, l, b, t); // 左辺↑ } + + +// +// 32bpp (RGBX) ビットマップ +// + +// コンストラクタ +BitmapRGBX::BitmapRGBX() + : BitmapRGBX(0, 0) // 移譲 +{ +} + +// コンストラクタ +BitmapRGBX::BitmapRGBX(int width_, int height_) + : BitmapRGBX(NULL, width_, height_) // 移譲 +{ +} + +// コンストラクタ +BitmapRGBX::BitmapRGBX(const void *buf_, int width_, int height_) + : inherited(32, width_, height_, buf_) +{ +} + +// デストラクタ +BitmapRGBX::~BitmapRGBX() +{ +}