2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 static VBitmapData *create(int width, int height, VBitmap::Format format);
33 VBitmap::Format format;
35 VBitmapCleanupFunction cleanupFunction;
41 VBitmapData::~VBitmapData()
43 if (cleanupFunction) cleanupFunction(cleanupInfo);
44 if (data && ownData) free(data);
48 VBitmapData::VBitmapData()
55 format(VBitmap::Format::ARGB32),
64 VBitmapData *VBitmapData::create(int width, int height, VBitmap::Format format)
66 if ((width <= 0) || (height <= 0) || format == VBitmap::Format::Invalid)
71 case VBitmap::Format::Alpha8:
74 case VBitmap::Format::ARGB32:
75 case VBitmap::Format::ARGB32_Premultiplied:
82 const int stride = ((width * depth + 31) >> 5)
83 << 2; // bytes per scanline (must be multiple of 4)
85 VBitmapData *d = new VBitmapData;
92 d->nBytes = d->stride * height;
93 d->data = (uchar *)malloc(d->nBytes);
103 inline void VBitmap::cleanUp(VBitmapData *d)
108 void VBitmap::detach()
111 if (d->ref.isShared() || d->roData) *this = copy();
119 if (!d->ref.deref()) cleanUp(d);
122 VBitmap::VBitmap() : d(nullptr) {}
124 VBitmap::VBitmap(const VBitmap &other)
130 VBitmap::VBitmap(VBitmap &&other) : d(other.d)
135 VBitmap &VBitmap::operator=(const VBitmap &other)
141 if (!d->ref.deref()) cleanUp(d);
149 inline VBitmap &VBitmap::operator=(VBitmap &&other)
151 if (d && !d->ref.deref()) cleanUp(d);
156 VBitmap::VBitmap(int w, int h, VBitmap::Format format) {}
157 VBitmap::VBitmap(uchar *data, int w, int h, int bytesPerLine,
158 VBitmap::Format format, VBitmapCleanupFunction f,
166 d->stride = bytesPerLine;
167 d->cleanupFunction = nullptr;
168 d->cleanupInfo = nullptr;
174 VBitmap VBitmap::copy(const VRect &r) const
176 // TODO implement properly.
180 int VBitmap::stride() const
182 return d ? d->stride : 0;
185 int VBitmap::width() const
187 return d ? d->width : 0;
190 int VBitmap::height() const
192 return d ? d->height : 0;
195 uchar *VBitmap::bits()
200 // In case detach ran out of memory...
206 const uchar *VBitmap::bits() const
208 return d ? d->data : 0;
211 bool VBitmap::isNull() const
216 uchar *VBitmap::scanLine(int i)
222 // In case detach() ran out of memory
225 return d->data + i * d->stride;
228 const uchar *VBitmap::scanLine(int i) const
232 // assert(i >= 0 && i < height());
233 return d->data + i * d->stride;
236 VBitmap::Format VBitmap::format() const
238 if (!d) return VBitmap::Format::Invalid;
242 void VBitmap::fill(uint pixel)