2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 struct VBitmap::Impl {
26 uchar * mData{nullptr};
32 VBitmap::Format mFormat{VBitmap::Format::Invalid};
38 Impl(uint width, uint height, VBitmap::Format format):
42 mDepth = depth(format);
43 uint stride = ((width * mDepth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4)
49 mBytes = mStride * mHeight;
50 mData = reinterpret_cast<uchar *>(::operator new(mBytes));
53 // handle malloc failure
58 Impl(uchar *data, uint w, uint h, uint bytesPerLine, VBitmap::Format format):
65 mStride = bytesPerLine;
66 mBytes = mStride * mHeight;
68 mDepth = depth(format);
73 if (mOwnData && mData) ::operator delete(mData);
76 uint stride() const {return mStride;}
77 uint width() const {return mWidth;}
78 uint height() const {return mHeight;}
79 VBitmap::Format format() const {return mFormat;}
80 uchar* data() { return mData;}
82 static uint depth(VBitmap::Format format)
86 case VBitmap::Format::Alpha8:
89 case VBitmap::Format::ARGB32:
90 case VBitmap::Format::ARGB32_Premultiplied:
98 void fill(uint /*pixel*/)
104 VBitmap::VBitmap(uint width, uint height, VBitmap::Format format)
106 if (width <=0 || height <=0 || format == Format::Invalid) return;
108 mImpl = std::make_shared<Impl>(width, height, format);
112 VBitmap::VBitmap(uchar *data, uint width, uint height, uint bytesPerLine,
113 VBitmap::Format format)
116 width <=0 || height <=0 ||
118 format == Format::Invalid) return;
120 mImpl = std::make_shared<Impl>(data, width, height, bytesPerLine, format);
123 uint VBitmap::stride() const
125 return mImpl ? mImpl->stride() : 0;
128 uint VBitmap::width() const
130 return mImpl ? mImpl->width() : 0;
133 uint VBitmap::height() const
135 return mImpl ? mImpl->height() : 0;
138 uint VBitmap::depth() const
140 return mImpl ? mImpl->mDepth : 0;
143 uchar *VBitmap::data()
145 return mImpl ? mImpl->data() : nullptr;
148 uchar *VBitmap::data() const
150 return mImpl ? mImpl->data() : nullptr;
153 bool VBitmap::valid() const
155 return mImpl ? true : false;
158 VBitmap::Format VBitmap::format() const
160 return mImpl ? mImpl->format() : VBitmap::Format::Invalid;
163 void VBitmap::fill(uint pixel)
165 if (mImpl) mImpl->fill(pixel);