10 static VBitmapData *create(int width, int height, VBitmap::Format format);
17 VBitmap::Format format;
19 VBitmapCleanupFunction cleanupFunction;
25 VBitmapData::~VBitmapData()
27 if (cleanupFunction) cleanupFunction(cleanupInfo);
28 if (data && ownData) free(data);
32 VBitmapData::VBitmapData()
39 format(VBitmap::Format::ARGB32),
48 VBitmapData *VBitmapData::create(int width, int height, VBitmap::Format format)
50 if ((width <= 0) || (height <= 0) || format == VBitmap::Format::Invalid)
55 case VBitmap::Format::Alpha8:
58 case VBitmap::Format::ARGB32:
59 case VBitmap::Format::ARGB32_Premultiplied:
66 const int stride = ((width * depth + 31) >> 5)
67 << 2; // bytes per scanline (must be multiple of 4)
69 VBitmapData *d = new VBitmapData;
76 d->nBytes = d->stride * height;
77 d->data = (uchar *)malloc(d->nBytes);
87 inline void VBitmap::cleanUp(VBitmapData *d)
92 void VBitmap::detach()
95 if (d->ref.isShared() || d->roData) *this = copy();
103 if (!d->ref.deref()) cleanUp(d);
106 VBitmap::VBitmap() : d(nullptr) {}
108 VBitmap::VBitmap(const VBitmap &other)
114 VBitmap::VBitmap(VBitmap &&other) : d(other.d)
119 VBitmap &VBitmap::operator=(const VBitmap &other)
125 if (!d->ref.deref()) cleanUp(d);
133 inline VBitmap &VBitmap::operator=(VBitmap &&other)
135 if (d && !d->ref.deref()) cleanUp(d);
140 VBitmap::VBitmap(int w, int h, VBitmap::Format format) {}
141 VBitmap::VBitmap(uchar *data, int w, int h, int bytesPerLine,
142 VBitmap::Format format, VBitmapCleanupFunction f,
150 d->stride = bytesPerLine;
151 d->cleanupFunction = nullptr;
152 d->cleanupInfo = nullptr;
158 VBitmap VBitmap::copy(const VRect &r) const
160 // TODO implement properly.
164 int VBitmap::stride() const
166 return d ? d->stride : 0;
169 int VBitmap::width() const
171 return d ? d->width : 0;
174 int VBitmap::height() const
176 return d ? d->height : 0;
179 uchar *VBitmap::bits()
184 // In case detach ran out of memory...
190 const uchar *VBitmap::bits() const
192 return d ? d->data : 0;
195 bool VBitmap::isNull() const
200 uchar *VBitmap::scanLine(int i)
206 // In case detach() ran out of memory
209 return d->data + i * d->stride;
212 const uchar *VBitmap::scanLine(int i) const
216 // assert(i >= 0 && i < height());
217 return d->data + i * d->stride;
220 VBitmap::Format VBitmap::format() const
222 if (!d) return VBitmap::Format::Invalid;
226 void VBitmap::fill(uint pixel)