| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 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. |
| 13 | * |
| 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 |
| 17 | */ |
| 18 |
| 19 | #include "vbitmap.h" |
| 20 | #include <string.h> |
| 21 | #include "vdrawhelper.h" |
| 22 | #include "vglobal.h" |
| 23 |
| 24 | V_BEGIN_NAMESPACE |
| 25 |
| 26 | struct VBitmap::Impl { |
| 27 | uchar * mData{nullptr}; |
| 28 | uint mWidth{0}; |
| 29 | uint mHeight{0}; |
| 30 | uint mStride{0}; |
| 31 | uint mBytes{0}; |
| 32 | uint mDepth{0}; |
| 33 | VBitmap::Format mFormat{VBitmap::Format::Invalid}; |
| 34 | bool mOwnData; |
| 35 | bool mRoData; |
| 36 |
| 37 | Impl() = delete; |
| 38 |
| 39 | Impl(uint width, uint height, VBitmap::Format format) |
| 40 | : mOwnData(true), mRoData(false) |
| 41 | { |
| 42 | reset(width, height, format); |
| 43 | } |
| 44 |
| 45 | void reset(uint width, uint height, VBitmap::Format format) |
| 46 | { |
| 47 | if (mOwnData && mData) delete (mData); |
| 48 |
| 49 | mDepth = depth(format); |
| 50 | uint stride = ((width * mDepth + 31) >> 5) |
| 51 | << 2; // bytes per scanline (must be multiple of 4) |
| 52 |
| 53 | mWidth = width; |
| 54 | mHeight = height; |
| 55 | mFormat = format; |
| 56 | mStride = stride; |
| 57 | mBytes = mStride * mHeight; |
| 58 | mData = reinterpret_cast<uchar *>(::operator new(mBytes)); |
| 59 | } |
| 60 |
| 61 | Impl(uchar *data, uint w, uint h, uint bytesPerLine, VBitmap::Format format) |
| 62 | : mOwnData(false), mRoData(false) |
| 63 | { |
| 64 | mWidth = w; |
| 65 | mHeight = h; |
| 66 | mFormat = format; |
| 67 | mStride = bytesPerLine; |
| 68 | mBytes = mStride * mHeight; |
| 69 | mData = data; |
| 70 | mDepth = depth(format); |
| 71 | } |
| 72 |
| 73 | ~Impl() |
| 74 | { |
| 75 | if (mOwnData && mData) ::operator delete(mData); |
| 76 | } |
| 77 |
| 78 | uint stride() const { return mStride; } |
| 79 | uint width() const { return mWidth; } |
| 80 | uint height() const { return mHeight; } |
| 81 | VBitmap::Format format() const { return mFormat; } |
| 82 | uchar * data() { return mData; } |
| 83 |
| 84 | static uint depth(VBitmap::Format format) |
| 85 | { |
| 86 | uint depth = 1; |
| 87 | switch (format) { |
| 88 | case VBitmap::Format::Alpha8: |
| 89 | depth = 8; |
| 90 | break; |
| 91 | case VBitmap::Format::ARGB32: |
| 92 | case VBitmap::Format::ARGB32_Premultiplied: |
| 93 | depth = 32; |
| 94 | break; |
| 95 | default: |
| 96 | break; |
| 97 | } |
| 98 | return depth; |
| 99 | } |
| 100 | void fill(uint /*pixel*/) |
| 101 | { |
| 102 | //@TODO |
| 103 | } |
| 104 |
| 105 | void updateLuma() |
| 106 | { |
| 107 | if (mFormat != VBitmap::Format::ARGB32_Premultiplied) return; |
| 108 |
| 109 | for (uint col = 0; col < mHeight; col++) { |
| 110 | uint *pixel = (uint *)(mData + mStride * col); |
| 111 | for (uint row = 0; row < mWidth; row++) { |
| 112 | int alpha = vAlpha(*pixel); |
| 113 | if (alpha == 0) { |
| 114 | pixel++; |
| 115 | continue; |
| 116 | } |
| 117 |
| 118 | int red = vRed(*pixel); |
| 119 | int green = vGreen(*pixel); |
| 120 | int blue = vBlue(*pixel); |
| 121 |
| 122 | if (alpha != 255) { |
| 123 | // un multiply |
| 124 | red = (red * 255) / alpha; |
| 125 | green = (green * 255) / alpha; |
| 126 | blue = (blue * 255) / alpha; |
| 127 | } |
| 128 | int luminosity = (0.299 * red + 0.587 * green + 0.114 * blue); |
| 129 | *pixel = luminosity << 24; |
| 130 | pixel++; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | }; |
| 135 |
| 136 | VBitmap::VBitmap(uint width, uint height, VBitmap::Format format) |
| 137 | { |
| 138 | if (width <= 0 || height <= 0 || format == Format::Invalid) return; |
| 139 |
| 140 | mImpl = std::make_shared<Impl>(width, height, format); |
| 141 | } |
| 142 |
| 143 | VBitmap::VBitmap(uchar *data, uint width, uint height, uint bytesPerLine, |
| 144 | VBitmap::Format format) |
| 145 | { |
| 146 | if (!data || width <= 0 || height <= 0 || bytesPerLine <= 0 || |
| 147 | format == Format::Invalid) |
| 148 | return; |
| 149 |
| 150 | mImpl = std::make_shared<Impl>(data, width, height, bytesPerLine, format); |
| 151 | } |
| 152 |
| 153 | void VBitmap::reset(uint w, uint h, VBitmap::Format format) |
| 154 | { |
| 155 | if (mImpl) { |
| 156 | if (w == mImpl->width() && h == mImpl->height() && |
| 157 | format == mImpl->format()) { |
| 158 | return; |
| 159 | } |
| 160 | mImpl->reset(w, h, format); |
| 161 | } else { |
| 162 | mImpl = std::make_shared<Impl>(w, h, format); |
| 163 | } |
| 164 | } |
| 165 |
| 166 | uint VBitmap::stride() const |
| 167 | { |
| 168 | return mImpl ? mImpl->stride() : 0; |
| 169 | } |
| 170 |
| 171 | uint VBitmap::width() const |
| 172 | { |
| 173 | return mImpl ? mImpl->width() : 0; |
| 174 | } |
| 175 |
| 176 | uint VBitmap::height() const |
| 177 | { |
| 178 | return mImpl ? mImpl->height() : 0; |
| 179 | } |
| 180 |
| 181 | uint VBitmap::depth() const |
| 182 | { |
| 183 | return mImpl ? mImpl->mDepth : 0; |
| 184 | } |
| 185 |
| 186 | uchar *VBitmap::data() |
| 187 | { |
| 188 | return mImpl ? mImpl->data() : nullptr; |
| 189 | } |
| 190 |
| 191 | uchar *VBitmap::data() const |
| 192 | { |
| 193 | return mImpl ? mImpl->data() : nullptr; |
| 194 | } |
| 195 |
| 196 | bool VBitmap::valid() const |
| 197 | { |
| 198 | return mImpl ? true : false; |
| 199 | } |
| 200 |
| 201 | VBitmap::Format VBitmap::format() const |
| 202 | { |
| 203 | return mImpl ? mImpl->format() : VBitmap::Format::Invalid; |
| 204 | } |
| 205 |
| 206 | void VBitmap::fill(uint pixel) |
| 207 | { |
| 208 | if (mImpl) mImpl->fill(pixel); |
| 209 | } |
| 210 |
| 211 | /* |
| 212 | * This is special function which converts |
| 213 | * RGB value to Luminosity and stores it in |
| 214 | * the Alpha component of the pixel. |
| 215 | * After this conversion the bitmap data is no more |
| 216 | * in RGB space. but the Alpha component contains the |
| 217 | * Luminosity value of the pixel in HSL color space. |
| 218 | * NOTE: this api has its own special usecase |
| 219 | * make sure you know what you are doing before using |
| 220 | * this api. |
| 221 | */ |
| 222 | void VBitmap::updateLuma() |
| 223 | { |
| 224 | if (mImpl) mImpl->updateLuma(); |
| 225 | } |
| 226 |
| 227 | V_END_NAMESPACE |
| 228 |