106#define GetBytesPerPixel(x) ((x) == BitmapFormat::A8_UNORM ? 1 \
107 : (x) == BitmapFormat::RG8_UNORM ? 2 \
108 : (x) == BitmapFormat::RGBA16F ? 8 \
109 : (x) == BitmapFormat::RGBA16UI ? 8 \
110 : (x) == BitmapFormat::RGBA32F ? 16 \
282 bool should_copy =
true);
345 virtual uint32_t
bpp()
const = 0;
360 virtual size_t size()
const = 0;
463 bool convert_to_straight_alpha =
true)
const = 0;
479 virtual bool WritePNG(
const char* path,
bool convert_to_rgba =
true,
480 bool convert_to_straight_alpha =
true)
const = 0;
549 explicit LockedPixels(T& lockable) : lockable_(lockable), data_(nullptr), size_(0) { lock(); }
553 lockable_->UnlockPixels();
564 size_t size() {
return size_; }
566 explicit operator bool()
const {
return !!lockable_; }
570 other.lockable_ =
nullptr;
571 other.data_ =
nullptr;
577 lockable_->UnlockPixels();
578 lockable_ = other.lockable_;
581 other.lockable_ =
nullptr;
582 other.data_ =
nullptr;
590 data_ = lockable_->LockPixels();
591 size_ = lockable_->size();
#define UExport
Definition Exports.h:22
virtual RefPtr< Buffer > EncodePNG(bool convert_to_rgba=true, bool convert_to_straight_alpha=true) const =0
Encode this Bitmap as a PNG image and store the encoded bytes in a Buffer.
virtual bool owns_pixels() const =0
Whether or not this Bitmap owns the pixel buffer and will destroy it at the end of its lifetime.
static RefPtr< Bitmap > Create()
Create an empty Bitmap.
virtual bool DrawBitmap(IntRect src_rect, IntRect dest_rect, RefPtr< Bitmap > src, bool pad_repeat)=0
Draw another bitmap to this bitmap.
void operator=(const Bitmap &)
virtual uint32_t height() const =0
Get the height in pixels.
virtual bool IsEmpty() const =0
Whether or not this Bitmap is empty (no pixels allocated).
static RefPtr< Bitmap > Create(const Bitmap &bitmap)
Create a bitmap from a deep copy of another Bitmap.
virtual uint32_t row_bytes() const =0
Get the number of bytes between each row of pixels.
virtual void Set(RefPtr< Bitmap > bitmap)=0
Assign another bitmap to this one.
static RefPtr< Bitmap > Create(uint32_t width, uint32_t height, BitmapFormat format)
Create a Bitmap with a certain configuration.
static RefPtr< Bitmap > Create(uint32_t width, uint32_t height, BitmapFormat format, uint32_t row_bytes, const void *pixels, size_t size, void *user_data, DestroyBitmapCallback destruction_callback)
Create a Bitmap that wraps existing pixel data, a user-defined destruction callback will be called wh...
virtual uint32_t width() const =0
Get the width in pixels.
static RefPtr< Bitmap > Create(uint32_t width, uint32_t height, BitmapFormat format, uint32_t alignment)
Create an aligned Bitmap with a certain configuration.
virtual void SwapRedBlueChannels()=0
Convert a BGRA bitmap to RGBA bitmap and vice-versa by swapping the red and blue channels.
virtual void Erase()=0
Erase the Bitmap (set all pixels to 0).
virtual void * LockPixels()=0
Lock the pixel buffer for reading/writing.
virtual bool WritePNG(const char *path, bool convert_to_rgba=true, bool convert_to_straight_alpha=true) const =0
Write this Bitmap out to a PNG image.
virtual IntRect bounds() const =0
Get the bounds as an IntRect.
virtual void ConvertToPremultipliedAlpha()=0
Convert a BGRA bitmap from straight alpha to premultiplied alpha.
virtual void UnlockPixels()=0
Unlock the pixel buffer.
static RefPtr< Bitmap > Create(uint32_t width, uint32_t height, BitmapFormat format, uint32_t row_bytes, const void *pixels, size_t size, bool should_copy=true)
Create a Bitmap with existing pixel data, a copy will be made unless should_copy is false.
virtual uint32_t bpp() const =0
Get the number of bytes per pixel.
virtual bool is_compressed() const =0
Whether or not this Bitmap uses a block-compressed (BCn) format.
virtual LockedPixels< RefPtr< Bitmap > > LockPixelsSafe() const =0
Lock the pixel buffer for reading/writing (safe version, automatically unlocks).
virtual const void * LockPixels() const =0
Lock the pixel buffer for reading/writing.
virtual void UnlockPixels() const =0
Unlock the pixel buffer.
virtual size_t size() const =0
Get the size in bytes of the pixel buffer.
virtual bool Resample(RefPtr< Bitmap > destination, bool high_quality)=0
Make a resized copy of this bitmap by writing to a pre-allocated destination bitmap.
virtual void ConvertToStraightAlpha()=0
Convert a BGRA bitmap from premultiplied alpha (the default) to straight alpha.
virtual BitmapFormat format() const =0
Get the pixel format.
static constexpr uint32_t MaxDimension
Maximum supported width or height, in pixels.
Definition Bitmap.h:216
virtual void * raw_pixels()=0
Get the raw pixel buffer.
Forward declaration for the LockedPixels class.
Definition Bitmap.h:544
void * data()
Access the locked pixel data.
Definition Bitmap.h:559
size_t size()
Access the size of the locked pixel data.
Definition Bitmap.h:564
~LockedPixels()
Definition Bitmap.h:551
LockedPixels(const LockedPixels &)=delete
LockedPixels & operator=(LockedPixels &&other)
Definition Bitmap.h:575
LockedPixels & operator=(const LockedPixels &)=delete
LockedPixels(T &lockable)
Definition Bitmap.h:549
LockedPixels(LockedPixels &&other)
Definition Bitmap.h:568
A nullable smart pointer.
Definition RefPtr.h:126
Interface for ref-counted objects that can additionally be observed via the WeakPtr<> smart pointer.
Definition RefPtr.h:83
Root namespace for every public Ultralight type, function, and enumeration.
bool IsCompressedFormat(BitmapFormat format)
Whether or not a given BitmapFormat is block-compressed (a BCn format).
BitmapFormatInfo GetBitmapFormatInfo(BitmapFormat format)
Get the block geometry for a given BitmapFormat.
void(*) DestroyBitmapCallback(void *user_data, void *data)
Function signature for a user-defined destruction callback to be optionally called when the Bitmap is...
Definition Bitmap.h:175
BitmapFormat
The various Bitmap formats.
Definition Bitmap.h:16
@ BC1_UNORM
BC1 block-compressed color (4x4 blocks, 8 bytes/block).
Definition Bitmap.h:51
@ RGBA32F
Four float channels (32 bits per channel, 128 bits per pixel).
Definition Bitmap.h:96
@ BC2_UNORM
BC2 block-compressed color (4x4 blocks, 16 bytes/block).
Definition Bitmap.h:58
@ RGBA16F
Four half-precision float channels (16 bits per channel, 64 bits per pixel).
Definition Bitmap.h:80
@ A8_UNORM
Alpha channel only, 8-bits per pixel.
Definition Bitmap.h:24
@ BGRA8_UNORM_SRGB
Blue Green Red Alpha channels, 32-bits per pixel.
Definition Bitmap.h:33
@ RG8_UNORM
Red Green channels, 16-bits per pixel.
Definition Bitmap.h:44
@ BC7_UNORM
BC7 block-compressed color (4x4 blocks, 16 bytes/block).
Definition Bitmap.h:72
@ RGBA16UI
Four unsigned integer channels (16 bits per channel, 64 bits per pixel).
Definition Bitmap.h:88
@ BC3_UNORM
BC3 block-compressed color (4x4 blocks, 16 bytes/block).
Definition Bitmap.h:65
Integer Rectangle Helper.
Definition Geometry.h:533