diff --git a/src/vector/vbitmap.cpp b/src/vector/vbitmap.cpp index 370c69c873..84493daeaf 100644 --- a/src/vector/vbitmap.cpp +++ b/src/vector/vbitmap.cpp @@ -40,6 +40,13 @@ struct VBitmap::Impl { mOwnData(true), mRoData(false) { + reset(width, height, format); + } + + void reset(uint width, uint height, VBitmap::Format format) + { + if (mOwnData && mData) delete(mData); + mDepth = depth(format); uint stride = ((width * mDepth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4) @@ -49,11 +56,6 @@ struct VBitmap::Impl { mStride = stride; mBytes = mStride * mHeight; mData = reinterpret_cast(::operator new(mBytes)); - - if (!mData) { - // handle malloc failure - ; - } } Impl(uchar *data, uint w, uint h, uint bytesPerLine, VBitmap::Format format): @@ -150,6 +152,20 @@ VBitmap::VBitmap(uchar *data, uint width, uint height, uint bytesPerLine, mImpl = std::make_shared(data, width, height, bytesPerLine, format); } +void VBitmap::reset(uint w, uint h, VBitmap::Format format) +{ + if (mImpl) { + if (w == mImpl->width() && + h == mImpl->height() && + format == mImpl->format()) { + return; + } + mImpl->reset(w, h, format); + } else { + mImpl = std::make_shared(w, h, format); + } +} + uint VBitmap::stride() const { return mImpl ? mImpl->stride() : 0; diff --git a/src/vector/vbitmap.h b/src/vector/vbitmap.h index c7f4702d89..a21c680a24 100644 --- a/src/vector/vbitmap.h +++ b/src/vector/vbitmap.h @@ -37,6 +37,7 @@ public: VBitmap(uint w, uint h, VBitmap::Format format); VBitmap(uchar *data, uint w, uint h, uint bytesPerLine, VBitmap::Format format); + void reset(uint w, uint h, VBitmap::Format format=Format::ARGB32_Premultiplied); uint stride() const; uint width() const; uint height() const;