Bitmap

class Bitmap

Bitmap describes a two-dimensional raster pixel array.

Bitmap is built on ImageInfo, containing integer width and height, ColorType and AlphaType describing the pixel format, and ColorSpace describing the range of colors. Bitmap points to PixelRef, which describes the physical array of pixels. ImageInfo bounds may be located anywhere fully inside PixelRef bounds.

Bitmap can be drawn using Canvas. Bitmap can be a drawing destination for Canvas draw member functions. Bitmap flexibility as a pixel container limits some optimizations available to the target platform.

If pixel array is primarily read-only, use Image for better performance. If pixel array is primarily written to, use Surface for better performance.

Declaring Bitmap const prevents altering ImageInfo: the Bitmap height, width, and so on cannot change. It does not affect PixelRef: a caller may write its pixels. Declaring Bitmap const affects Bitmap configuration, not its contents.

Bitmap is not thread safe. Each thread must have its own copy of Bitmap fields, although threads may share the underlying pixel array.

Bitmap supports buffer protocol. It is possible to mount Bitmap as array:

array = np.array(bitmap, copy=False)

Or mount array as Bitmap with ImageInfo:

buffer = np.zeros((100, 100, 4), np.uint8)
bitmap = skia.Bitmap()
bitmap.setInfo(skia.ImageInfo.MakeN32Premul(100, 100))
bitmap.setPixels(buffer)

Classes

AllocFlags

Members:

Methods

__init__

Creates a Bitmap.

allocN32Pixels

Sets ImageInfo to width, height, and the native color type; and allocates pixel memory.

allocPixels

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

allocPixelsFlags

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

alphaType

bounds

Returns IRect { 0, 0, width(), height() }.

bytesPerPixel

Returns number of bytes per pixel required by ColorType.

colorSpace

Returns ColorSpace, the range of colors, associated with ImageInfo.

colorType

computeByteSize

Returns minimum memory required for pixel storage.

dimensions

Returns ISize { width(), height() }.

drawsNothing

Returns true if width() or height() are zero, or if PixelRef is nullptr.

empty

Returns true if either width() or height() are zero.

erase

Overloaded function.

eraseARGB

Replaces pixel values with unpremultiplied color built from a, r, g, and b, interpreted as being in the sRGB ColorSpace.

extractAlpha

Sets dst to alpha described by pixels.

extractSubset

Shares PixelRef with dst.

getAlphaf

Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].

getBounds

Returns IRect { 0, 0, width(), height() }.

getColor

Returns pixel at (x, y) as unpremultiplied color.

getGenerationID

Returns a unique value corresponding to the pixels in PixelRef.

getPixels

Returns pixel address, the base address corresponding to the pixel origin.

getSubset

Returns the bounds of this bitmap, offset by its PixelRef origin.

height

Returns pixel row count.

info

Returns width, height, AlphaType, ColorType, and ColorSpace.

installPixels

Overloaded function.

isImmutable

Returns true if pixels can not change.

isNull

Returns true if PixelRef is nullptr.

isOpaque

Returns true if AlphaType is set to hint that all pixels are opaque; their alpha value is implicitly or explicitly 1.0.

makeShader

notifyPixelsChanged

Marks that pixels in PixelRef have changed.

peekPixels

Copies Bitmap pixel address, row bytes, and ImageInfo to pixmap, if address is available, and returns true.

pixmap

Returns a constant reference to the Pixmap holding the Bitmap pixel address, row bytes, and ImageInfo.

readPixels

Overloaded function.

readyToDraw

Returns true if Bitmap is can be drawn.

refColorSpace

Returns smart pointer to ColorSpace, the range of colors, associated with ImageInfo.

reset

Resets to its initial state; all fields are set to zero, as if Bitmap had been initialized by Bitmap.

rowBytes

Returns row bytes, the interval from one pixel row to the next.

rowBytesAsPixels

Returns number of pixels that fit on row.

setAlphaType

Sets AlphaType, if alphaType is compatible with ColorType.

setImmutable

Sets internal flag to mark Bitmap as immutable.

setInfo

Sets width, height, AlphaType, ColorType, ColorSpace, and optional rowBytes.

setPixels

Replaces PixelRef with pixels, preserving ImageInfo and rowBytes().

shiftPerPixel

Returns bit shift converting row bytes to row pixels.

swap

Swaps the fields of the two bitmaps.

tobytes

tryAllocN32Pixels

Sets ImageInfo to width, height, and native color type; and allocates pixel memory.

tryAllocPixels

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

tryAllocPixelsFlags

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

width

Returns pixel count in each row.

writePixels

Copies a Rect of pixels from src.

Attributes

kZeroPixels_AllocFlag

Methods

Bitmap.__init__(self: skia.Bitmap, src: skia.Bitmap = None) None

Creates a Bitmap.

When src is given, copies settings from src to returned Bitmap.

Shares pixels if src has pixels allocated, so both bitmaps reference the same pixels.

When src is None, creates an empty Bitmap without pixels, with kUnknown_ColorType, kUnknown_AlphaType, and with a width and height of zero.

PixelRef origin is set to (0, 0). Bitmap is not volatile.

Use setInfo() to associate ColorType, AlphaType, width, and height after Bitmap has been created.

Param:

src Bitmap to copy ImageInfo, and share PixelRef

Bitmap.allocN32Pixels(self: skia.Bitmap, width: int, height: int, isOpaque: bool) None

Sets ImageInfo to width, height, and the native color type; and allocates pixel memory.

If isOpaque is true, sets ImageInfo to kOpaque_AlphaType; otherwise, sets to kPremul_AlphaType.

Aborts if width exceeds 29 bits or is negative, or height is negative, or allocation fails. Abort steps may be provided by the user at compile time by defining SK_ABORT.

Use to create Bitmap that matches PMColor, the native pixel arrangement on the platform. Bitmap drawn to output device skips converting its pixel format.

Parameters:
  • width (int) – pixel column count; must be zero or greater

  • height (int) – pixel row count; must be zero or greater

  • isOpaque (bool) – true if pixels do not have transparency

Bitmap.allocPixels(self: skia.Bitmap, info: SkImageInfo = None, rowBytes: int = 0) None

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

rowBytes must equal or exceed info.:py:meth:width times info.bytesPerPixel, or equal zero. Pass in zero for rowBytes to compute the minimum valid value.

Aborts execution if ImageInfo could not be set, or memory could not be allocated. Abort steps may be provided by the user at compile time by defining SK_ABORT.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of malloc().

Parameters:
Bitmap.allocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) None

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

Memory is zeroed.

Aborts execution if ImageInfo could not be set, or memory could not be allocated, or memory could not optionally be zeroed. Abort steps may be provided by the user at compile time by defining SK_ABORT.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of calloc.

Parameters:
Bitmap.alphaType(self: skia.Bitmap) SkAlphaType
Bitmap.bounds(self: skia.Bitmap) skia.IRect

Returns IRect { 0, 0, width(), height() }.

Returns:

integral rectangle from origin to width() and height()

Bitmap.bytesPerPixel(self: skia.Bitmap) int

Returns number of bytes per pixel required by ColorType.

Returns zero if colorType( is ColorType.kUnknown_ColorType.

Returns:

bytes in pixel

Bitmap.colorSpace(self: skia.Bitmap) skia.ColorSpace

Returns ColorSpace, the range of colors, associated with ImageInfo.

The reference count of ColorSpace is unchanged. The returned ColorSpace is immutable.

Returns:

ColorSpace in ImageInfo, or nullptr

Bitmap.colorType(self: skia.Bitmap) SkColorType
Bitmap.computeByteSize(self: skia.Bitmap) int

Returns minimum memory required for pixel storage.

Does not include unused memory on last row when rowBytesAsPixels() exceeds width(). Returns SIZE_MAX if result does not fit in size_t. Returns zero if height() or width() is 0. Returns height() times rowBytes() if colorType() is kUnknown_ColorType.

Returns:

size in bytes of image buffer

Bitmap.dimensions(self: skia.Bitmap) skia.ISize

Returns ISize { width(), height() }.

Returns:

integral size of width() and height()

Bitmap.drawsNothing(self: skia.Bitmap) bool

Returns true if width() or height() are zero, or if PixelRef is nullptr.

If true, Bitmap has no effect when drawn or drawn into.

Returns:

true if drawing has no effect

Bitmap.empty(self: skia.Bitmap) bool

Returns true if either width() or height() are zero.

Does not check if PixelRef is nullptr; call drawsNothing() to check width(), height(), and PixelRef.

Returns:

true if dimensions do not enclose area

Bitmap.erase(*args, **kwargs)

Overloaded function.

  1. erase(self: skia.Bitmap, c: skia.Color4f, area: skia.IRect) -> None

    Replaces pixel values inside area with c.

    interpreted as being in the sRGB ColorSpace. If area does not intersect bounds(), call has no effect.

    If the colorType() is kGray_8_ColorType or kRGB_565_ColorType, then alpha is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_ColorType, then RGB is ignored.

    param int c:

    unpremultiplied color

    param skia.IRect area:

    rectangle to fill

  2. erase(self: skia.Bitmap, c: int, area: skia.IRect) -> None

    Deprecated.

    param int c:

    unpremultiplied color

    param skia.IRect area:

    rectangle to fill

Bitmap.eraseARGB(self: skia.Bitmap, a: int, r: int, g: int, b: int) None

Replaces pixel values with unpremultiplied color built from a, r, g, and b, interpreted as being in the sRGB ColorSpace.

All pixels contained by bounds() are affected. If the colorType() is kGray_8_ColorType or kRGB_565_ColorType, then alpha is ignored; RGB is treated as opaque. If colorType() is kAlpha_8_ColorType, then RGB is ignored.

Parameters:
  • a (int) – amount of alpha, from fully transparent (0) to fully opaque (255)

  • r (int) – amount of red, from no red (0) to full red (255)

  • g (int) – amount of green, from no green (0) to full green (255)

  • b (int) – amount of blue, from no blue (0) to full blue (255)

Bitmap.extractAlpha(self: skia.Bitmap, dst: skia.Bitmap, paint: SkPaint = None, offset: skia.IPoint = None) bool

Sets dst to alpha described by pixels.

Returns false if dst cannot be written to or dst pixels cannot be allocated.

If paint is not nullptr and contains MaskFilter, MaskFilter generates mask alpha from Bitmap. Uses HeapAllocator to reserve memory for dst PixelRef. Sets offset to top-left position for dst for alignment with Bitmap; (0, 0) unless MaskFilter generates mask.

Parameters:
  • dst – holds PixelRef to fill with alpha layer

  • paint – holds optional MaskFilter; may be nullptr

  • offset – top-left position for dst; may be nullptr

Returns:

true if alpha layer was constructed in dst PixelRef

Bitmap.extractSubset(self: skia.Bitmap, dst: skia.Bitmap, subset: skia.IRect) bool

Shares PixelRef with dst.

Pixels are not copied; Bitmap and dst point to the same pixels; dst bounds() are set to the intersection of subset and the original bounds().

subset may be larger than bounds(). Any area outside of bounds() is ignored.

Any contents of dst are discarded. isVolatile() setting is copied to dst. dst is set to colorType(), alphaType(), and colorSpace().

Return false if:

Parameters:
  • dstBitmap set to subset

  • subset – rectangle of pixels to reference

Returns:

true if dst is replaced by subset

Bitmap.getAlphaf(self: skia.Bitmap, x: int, y: int) float

Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].

This is roughly equivalent to GetColorA() of getColor(), but can be more efficent (and more precise if the pixels store more than 8 bits per component).

Parameters:
  • x (int) – column index, zero or greater, and less than width()

  • y (int) – row index, zero or greater, and less than height()

Returns:

alpha converted to normalized float

Bitmap.getBounds(self: skia.Bitmap) skia.IRect

Returns IRect { 0, 0, width(), height() }.

Returns:

integral rectangle

Bitmap.getColor(self: skia.Bitmap, x: int, y: int) int

Returns pixel at (x, y) as unpremultiplied color.

Returns black with alpha if ColorType is kAlpha_8_ColorType.

Input is not validated: out of bounds values of x or y trigger an assert() if built with SK_DEBUG defined; and returns undefined values or may crash if SK_RELEASE is defined. Fails if ColorType is kUnknown_ColorType or pixel address is nullptr.

ColorSpace in ImageInfo is ignored. Some color precision may be lost in the conversion to unpremultiplied color; original pixel data may have additional precision.

Parameters:
  • x (int) – column index, zero or greater, and less than width()

  • y (int) – row index, zero or greater, and less than height()

Returns:

pixel converted to unpremultiplied color

Bitmap.getGenerationID(self: skia.Bitmap) int

Returns a unique value corresponding to the pixels in PixelRef.

Returns a different value after notifyPixelsChanged() has been called. Returns zero if PixelRef is nullptr.

Determines if pixels have changed since last examined.

Returns:

unique value for pixels in PixelRef

Bitmap.getPixels(self: skia.Bitmap) object

Returns pixel address, the base address corresponding to the pixel origin.

Returns:

pixel address

Bitmap.getSubset(self: skia.Bitmap) skia.IRect

Returns the bounds of this bitmap, offset by its PixelRef origin.

Returns:

bounds within PixelRef bounds

Bitmap.height(self: skia.Bitmap) int

Returns pixel row count.

Maybe be less than pixelRef() . height(). Will not exceed pixelRef() . height() less pixelRefOrigin() . fY.

Returns:

pixel height in ImageInfo

Bitmap.info(self: skia.Bitmap) SkImageInfo

Returns width, height, AlphaType, ColorType, and ColorSpace.

Returns:

reference to ImageInfo

Bitmap.installPixels(*args, **kwargs)

Overloaded function.

  1. installPixels(self: skia.Bitmap, info: SkImageInfo, pixels: object, rowBytes: int) -> bool

    Sets ImageInfo to info following the rules in setInfo(), and creates PixelRef containing pixels and rowBytes.

    If ImageInfo could not be set, or rowBytes is less than info.minRowBytes(): calls reset(), and returns false.

    Otherwise, if pixels equals nullptr: sets ImageInfo, returns true.

    Caller must ensure that pixels are valid for the lifetime of Bitmap and PixelRef.

    info:

    contains width, height, AlphaType, ColorType, ColorSpace

    pixels:

    address or pixel storage buffer; may be nullptr

    rowBytes:

    size of pixel row or larger

    return:

    true if ImageInfo is set to info

  2. installPixels(self: skia.Bitmap, pixmap: SkPixmap) -> bool

    Sets ImageInfo to pixmap.info() following the rules in setInfo(), and creates PixelRef containing pixmap.addr() and pixmap.rowBytes().

    If ImageInfo could not be set, or pixmap.rowBytes() is less than ImageInfo.minRowBytes() : calls reset(), and returns false.

    Otherwise, if pixmap.addr() equals nullptr: sets ImageInfo, returns true.

    Caller must ensure that pixmap is valid for the lifetime of Bitmap and PixelRef.

    pixmap:

    ImageInfo, pixel address, and rowBytes()

    return:

    true if ImageInfo was set to pixmap.info()

Bitmap.isImmutable(self: skia.Bitmap) bool

Returns true if pixels can not change.

Most immutable Bitmap checks trigger an assert only on debug builds.

Returns:

true if pixels are immutable

Bitmap.isNull(self: skia.Bitmap) bool

Returns true if PixelRef is nullptr.

Does not check if width() or height() are zero; call drawsNothing() to check width(), height(), and PixelRef.

Returns:

true if no PixelRef is associated

Bitmap.isOpaque(self: skia.Bitmap) bool

Returns true if AlphaType is set to hint that all pixels are opaque; their alpha value is implicitly or explicitly 1.0.

If true, and all pixels are not opaque, Skia may draw incorrectly.

Does not check if ColorType allows alpha, or if any pixel value has transparency.

Returns:

true if ImageInfo AlphaType is kOpaque_AlphaType

Bitmap.makeShader(self: skia.Bitmap, tmx: skia.TileMode = skia.TileMode.kClamp, tmy: skia.TileMode = skia.TileMode.kClamp, sampling: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix = None) SkShader
Bitmap.notifyPixelsChanged(self: skia.Bitmap) None

Marks that pixels in PixelRef have changed.

Subsequent calls to getGenerationID() return a different value.

Bitmap.peekPixels(self: skia.Bitmap, pixmap: SkPixmap) bool

Copies Bitmap pixel address, row bytes, and ImageInfo to pixmap, if address is available, and returns true.

If pixel address is not available, return false and leave pixmap unchanged.

pixmap contents become invalid on any future change to Bitmap.

Parameters:

pixmap (skia.Pixmap) – storage for pixel state if pixels are readable; otherwise, ignored

Returns:

true if Bitmap has direct access to pixels

Bitmap.pixmap(self: skia.Bitmap) SkPixmap

Returns a constant reference to the Pixmap holding the Bitmap pixel address, row bytes, and ImageInfo.

Returns:

reference to Pixmap describing this Bitmap

Bitmap.readPixels(*args, **kwargs)

Overloaded function.

  1. readPixels(self: skia.Bitmap, dstInfo: SkImageInfo, dstPixels: Buffer, dstRowBytes: int = 0, srcX: int = 0, srcY: int = 0) -> bool

    Copies a Rect of pixels from Bitmap to dstPixels.

    Copy starts at (srcX, srcY), and does not exceed Bitmap ( width(), height()).

    dstInfo specifies width, height, ColorType, AlphaType, and ColorSpace of destination. dstRowBytes specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:

    • dstInfo has no address

    • dstRowBytes is less than dstInfo.minRowBytes()

    • PixelRef is nullptr

    Pixels are copied only if pixel conversion is possible. If Bitmap colorType() is kGray_8_ColorType, or kAlpha_8_ColorType; dstInfo.colorType() must match. If Bitmap colorType() is kGray_8_ColorType, dstInfo.colorSpace() must match. If Bitmap alphaType() is kOpaque_AlphaType, dstInfo.alphaType() must match. If Bitmap colorSpace() is nullptr, dstInfo.colorSpace() must match. Returns false if pixel conversion is not possible.

    srcX and srcY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height().

    dstInfo:

    destination width, height, ColorType, AlphaType, ColorSpace

    dstPixels:

    destination pixel storage

    dstRowBytes:

    destination row length

    srcX:

    column index whose absolute value is less than width()

    srcY:

    row index whose absolute value is less than height()

    return:

    true if pixels are copied to dstPixels

  2. readPixels(self: skia.Bitmap, dst: SkPixmap, srcX: int = 0, srcY: int = 0) -> bool

    Copies a Rect of pixels from Bitmap to dst.

    Copy starts at (srcX, srcY), and does not exceed Bitmap ( width(), height()).

    dst specifies width, height, ColorType, AlphaType, ColorSpace, pixel storage, and row bytes of destination. dst.rowBytes() specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:

    Pixels are copied only if pixel conversion is possible. If Bitmap colorType() is kGray_8_ColorType, or kAlpha_8_ColorType; dst ColorType must match. If Bitmap colorType() is kGray_8_ColorType, dst ColorSpace must match. If Bitmap alphaType() is kOpaque_AlphaType, dst AlphaType must match. If Bitmap colorSpace() is nullptr, dst ColorSpace must match. Returns false if pixel conversion is not possible.

    srcX and srcY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(srcX) >= Bitmap width(), or if abs(srcY) >= Bitmap height().

    dst:

    destination Pixmap: ImageInfo, pixels, row bytes

    srcX:

    column index whose absolute value is less than width()

    srcY:

    row index whose absolute value is less than height()

    return:

    true if pixels are copied to dst

Bitmap.readyToDraw(self: skia.Bitmap) bool

Returns true if Bitmap is can be drawn.

Returns:

true if getPixels is not nullptr

Bitmap.refColorSpace(self: skia.Bitmap) skia.ColorSpace

Returns smart pointer to ColorSpace, the range of colors, associated with ImageInfo.

The smart pointer tracks the number of objects sharing this ColorSpace reference so the memory is released when the owners destruct.

The returned ColorSpace is immutable.

Returns:

ColorSpace in ImageInfo wrapped in a smart pointer

Bitmap.reset(self: skia.Bitmap) None

Resets to its initial state; all fields are set to zero, as if Bitmap had been initialized by Bitmap.

Sets width, height, row bytes to zero; pixel address to nullptr; ColorType to kUnknown_ColorType; and AlphaType to kUnknown_AlphaType.

If PixelRef is allocated, its reference count is decreased by one, releasing its memory if Bitmap is the sole owner.

Bitmap.rowBytes(self: skia.Bitmap) int

Returns row bytes, the interval from one pixel row to the next.

Row bytes is at least as large as: width() * info() . bytesPerPixel().

Returns zero if colorType() is kUnknown_ColorType, or if row bytes supplied to setInfo() is not large enough to hold a row of pixels.

Returns:

byte length of pixel row

Bitmap.rowBytesAsPixels(self: skia.Bitmap) int

Returns number of pixels that fit on row.

Should be greater than or equal to width().

Returns:

maximum pixels per row

Bitmap.setAlphaType(self: skia.Bitmap, alphaType: SkAlphaType) bool

Sets AlphaType, if alphaType is compatible with ColorType.

Returns true unless alphaType is kUnknown_AlphaType and current AlphaType is not kUnknown_AlphaType.

Returns true if ColorType is kUnknown_ColorType. alphaType is ignored, and AlphaType remains kUnknown_AlphaType.

Returns true if ColorType is kRGB_565_ColorType or kGray_8_ColorType. alphaType is ignored, and AlphaType remains kOpaque_AlphaType.

If ColorType is kARGB_4444_ColorType, kRGBA_8888_ColorType, kBGRA_8888_ColorType, or kRGBA_F16_ColorType: returns true unless alphaType is kUnknown_AlphaType and AlphaType is not kUnknown_AlphaType. If AlphaType is kUnknown_AlphaType, alphaType is ignored.

If ColorType is kAlpha_8_ColorType, returns true unless alphaType is kUnknown_AlphaType and AlphaType is not kUnknown_AlphaType. If AlphaType is kUnknown_AlphaType, alphaType is ignored. If alphaType is kUnpremul_AlphaType, it is treated as kPremul_AlphaType.

This changes AlphaType in PixelRef; all bitmaps sharing PixelRef are affected.

Returns:

true if AlphaType is set

Bitmap.setImmutable(self: skia.Bitmap) None

Sets internal flag to mark Bitmap as immutable.

Once set, pixels can not change. Any other bitmap sharing the same PixelRef are also marked as immutable. Once PixelRef is marked immutable, the setting cannot be cleared.

Writing to immutable Bitmap pixels triggers an assert on debug builds.

Bitmap.setInfo(self: skia.Bitmap, imageInfo: SkImageInfo, rowBytes: int = 0) bool

Sets width, height, AlphaType, ColorType, ColorSpace, and optional rowBytes.

Frees pixels, and returns true if successful.

imageInfo.alphaType() may be altered to a value permitted by imageInfo.colorSpace(). If imageInfo.colorType() is kUnknown_ColorType, imageInfo.alphaType() is set to kUnknown_AlphaType. If imageInfo.colorType() is kAlpha_8_ColorType and imageInfo.alphaType() is kUnpremul_AlphaType, imageInfo.alphaType() is replaced by kPremul_AlphaType. If imageInfo.colorType() is kRGB_565_ColorType or kGray_8_ColorType, imageInfo.alphaType() is set to kOpaque_AlphaType. If imageInfo.colorType() is kARGB_4444_ColorType, kRGBA_8888_ColorType, kBGRA_8888_ColorType, or kRGBA_F16_ColorType: imageInfo.alphaType() remains unchanged.

rowBytes must equal or exceed imageInfo.minRowBytes(). If imageInfo.colorSpace() is kUnknown_ColorType, rowBytes is ignored and treated as zero; for all other ColorSpace values, rowBytes of zero is treated as imageInfo.minRowBytes().

Calls reset() and returns false if:

  • rowBytes exceeds 31 bits

  • imageInfo.width() is negative

  • imageInfo.height() is negative

  • rowBytes is positive and less than imageInfo.width() times

    imageInfo.bytesPerPixel()

Parameters:
Returns:

true if ImageInfo set successfully

Bitmap.setPixels(self: skia.Bitmap, pixels: Buffer) None

Replaces PixelRef with pixels, preserving ImageInfo and rowBytes().

Sets PixelRef origin to (0, 0).

If pixels is nullptr, or if info().colorType() equals kUnknown_ColorType; release reference to PixelRef, and set PixelRef to nullptr.

Caller is responsible for handling ownership pixel memory for the lifetime of Bitmap and PixelRef.

Parameters:

pixels (Union[bytes,bytearray,memoryview]) – address of pixel storage, managed by caller

Bitmap.shiftPerPixel(self: skia.Bitmap) int

Returns bit shift converting row bytes to row pixels.

Returns zero for kUnknown_ColorType.

Returns:

one of: 0, 1, 2, 3; left shift to convert pixels to bytes

Bitmap.swap(self: skia.Bitmap, other: skia.Bitmap) None

Swaps the fields of the two bitmaps.

Parameters:

other (skia.Bitmap) – Bitmap exchanged with original

Bitmap.tobytes(self: skia.Bitmap) object
Bitmap.tryAllocN32Pixels(self: skia.Bitmap, width: int, height: int, isOpaque: bool) bool

Sets ImageInfo to width, height, and native color type; and allocates pixel memory.

If isOpaque is true, sets ImageInfo to kOpaque_AlphaType; otherwise, sets to kPremul_AlphaType.

Calls reset() and returns false if width exceeds 29 bits or is negative, or height is negative.

Returns false if allocation fails.

Use to create Bitmap that matches PMColor, the native pixel arrangement on the platform. Bitmap drawn to output device skips converting its pixel format.

Parameters:
  • width (int) – pixel column count; must be zero or greater

  • height (int) – pixel row count; must be zero or greater

  • isOpaque (bool) – true if pixels do not have transparency

Returns:

true if pixel storage is allocated

Bitmap.tryAllocPixels(self: skia.Bitmap, info: SkImageInfo = None, rowBytes: int = 0) bool

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

rowBytes must equal or exceed info.:py:meth:width times info.bytesPerPixel, or equal zero. Pass in zero for rowBytes to compute the minimum valid value.

Returns false and calls reset() if ImageInfo could not be set, or memory could not be allocated.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of malloc().

Parameters:
Returns:

true if pixel storage is allocated

Bitmap.tryAllocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) bool

Sets ImageInfo to info following the rules in setInfo() and allocates pixel memory.

Memory is zeroed.

Returns false and calls reset() if ImageInfo could not be set, or memory could not be allocated, or memory could not optionally be zeroed.

On most platforms, allocating pixel memory may succeed even though there is not sufficient memory to hold pixels; allocation does not take place until the pixels are written to. The actual behavior depends on the platform implementation of calloc.

Parameters:
Returns:

true if pixels allocation is successful

Bitmap.width(self: skia.Bitmap) int

Returns pixel count in each row.

Should be equal or less than rowBytes() / info() . bytesPerPixel().

May be less than pixelRef() . width(). Will not exceed pixelRef() . width() less pixelRefOrigin() . fX.

Returns:

pixel width in SkImageInfo

Bitmap.writePixels(self: skia.Bitmap, src: SkPixmap, dstX: int = 0, dstY: int = 0) bool

Copies a Rect of pixels from src.

Copy starts at (dstX, dstY), and does not exceed (src.width(), src.height()).

src specifies width, height, ColorType, AlphaType, ColorSpace, pixel storage, and row bytes of source. src.rowBytes() specifics the gap from one source row to the next. Returns true if pixels are copied. Returns false if:

  • src pixel storage equals nullptr

  • src.rowBytes is less than ImageInfo::minRowBytes()

  • PixelRef is nullptr

Pixels are copied only if pixel conversion is possible. If Bitmap colorType() is kGray_8_ColorType, or kAlpha_8_ColorType; src ColorType must match. If Bitmap colorType() is kGray_8_ColorType, src ColorSpace must match. If Bitmap alphaType() is kOpaque_AlphaType, src AlphaType must match. If Bitmap colorSpace() is nullptr, src ColorSpace must match. Returns false if pixel conversion is not possible.

dstX and dstY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(dstX) >= Bitmap width(), or if abs(dstY) >= Bitmap height().

Parameters:
  • src (skia.Pixmap) – source Pixmap: ImageInfo, pixels, row bytes

  • dstX (int) – column index whose absolute value is less than width()

  • dstY (int) – row index whose absolute value is less than height()

Returns:

true if src pixels are copied to Bitmap

Attributes

Bitmap.kZeroPixels_AllocFlag = <AllocFlags.kZeroPixels_AllocFlag: 1>