Bitmap
- class Bitmap
Bitmapdescribes a two-dimensional raster pixel array.Bitmapis built onImageInfo, containing integer width and height,ColorTypeandAlphaTypedescribing the pixel format, andColorSpacedescribing the range of colors.Bitmappoints toPixelRef, which describes the physical array of pixels.ImageInfobounds may be located anywhere fully insidePixelRefbounds.Bitmapcan be drawn usingCanvas.Bitmapcan be a drawing destination forCanvasdraw member functions.Bitmapflexibility as a pixel container limits some optimizations available to the target platform.If pixel array is primarily read-only, use
Imagefor better performance. If pixel array is primarily written to, useSurfacefor better performance.Declaring
Bitmapconst prevents alteringImageInfo: theBitmapheight, width, and so on cannot change. It does not affectPixelRef: a caller may write its pixels. DeclaringBitmapconst affectsBitmapconfiguration, not its contents.Bitmapis not thread safe. Each thread must have its own copy ofBitmapfields, although threads may share the underlying pixel array.Bitmapsupports buffer protocol. It is possible to mountBitmapas array:array = np.array(bitmap, copy=False)
Or mount array as
BitmapwithImageInfo:buffer = np.zeros((100, 100, 4), np.uint8) bitmap = skia.Bitmap() bitmap.setInfo(skia.ImageInfo.MakeN32Premul(100, 100)) bitmap.setPixels(buffer)
Classes
Members:
Methods
__init__(self: skia.Bitmap, src: skia.Bitmap = None) -> None
allocN32Pixels(self: skia.Bitmap, width: int, height: int, isOpaque: bool) -> None
allocPixels(self: skia.Bitmap, info: SkImageInfo = None, rowBytes: int = 0) -> None
allocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) -> None
alphaType(self: skia.Bitmap) -> SkAlphaType
bounds(self: skia.Bitmap) -> skia.IRect
bytesPerPixel(self: skia.Bitmap) -> int
colorSpace(self: skia.Bitmap) -> skia.ColorSpace
colorType(self: skia.Bitmap) -> SkColorType
computeByteSize(self: skia.Bitmap) -> int
dimensions(self: skia.Bitmap) -> skia.ISize
drawsNothing(self: skia.Bitmap) -> bool
empty(self: skia.Bitmap) -> bool
eraseARGB(self: skia.Bitmap, a: int, r: int, g: int, b: int) -> None
extractAlpha(self: skia.Bitmap, dst: skia.Bitmap, paint: SkPaint = None, offset: skia.IPoint = None) -> bool
extractSubset(self: skia.Bitmap, dst: skia.Bitmap, subset: skia.IRect) -> bool
getAlphaf(self: skia.Bitmap, x: int, y: int) -> float
getBounds(self: skia.Bitmap) -> skia.IRect
getColor(self: skia.Bitmap, x: int, y: int) -> int
getGenerationID(self: skia.Bitmap) -> int
getPixels(self: skia.Bitmap) -> object
getSubset(self: skia.Bitmap) -> skia.IRect
height(self: skia.Bitmap) -> int
info(self: skia.Bitmap) -> SkImageInfo
isImmutable(self: skia.Bitmap) -> bool
isNull(self: skia.Bitmap) -> bool
isOpaque(self: skia.Bitmap) -> bool
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
notifyPixelsChanged(self: skia.Bitmap) -> None
peekPixels(self: skia.Bitmap, pixmap: SkPixmap) -> bool
pixmap(self: skia.Bitmap) -> SkPixmap
readyToDraw(self: skia.Bitmap) -> bool
refColorSpace(self: skia.Bitmap) -> skia.ColorSpace
reset(self: skia.Bitmap) -> None
rowBytes(self: skia.Bitmap) -> int
rowBytesAsPixels(self: skia.Bitmap) -> int
setAlphaType(self: skia.Bitmap, alphaType: SkAlphaType) -> bool
setImmutable(self: skia.Bitmap) -> None
setInfo(self: skia.Bitmap, imageInfo: SkImageInfo, rowBytes: int = 0) -> bool
setPixels(self: skia.Bitmap, pixels: Buffer) -> None
shiftPerPixel(self: skia.Bitmap) -> int
swap(self: skia.Bitmap, other: skia.Bitmap) -> None
tobytes(self: skia.Bitmap) -> object
tryAllocN32Pixels(self: skia.Bitmap, width: int, height: int, isOpaque: bool) -> bool
tryAllocPixels(self: skia.Bitmap, info: SkImageInfo = None, rowBytes: int = 0) -> bool
tryAllocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) -> bool
width(self: skia.Bitmap) -> int
writePixels(self: skia.Bitmap, src: SkPixmap, dstX: int = 0, dstY: int = 0) -> bool
Attributes
Methods
- Bitmap.__init__(self: skia.Bitmap, src: skia.Bitmap = None) None
Creates a
Bitmap.When
srcis given, copies settings from src to returnedBitmap.Shares pixels if src has pixels allocated, so both bitmaps reference the same pixels.
When
srcisNone, creates an emptyBitmapwithout pixels, withkUnknown_ColorType,kUnknown_AlphaType, and with a width and height of zero.PixelReforigin is set to (0, 0).Bitmapis not volatile.Use
setInfo()to associateColorType,AlphaType, width, and height afterBitmaphas been created.
- Bitmap.allocN32Pixels(self: skia.Bitmap, width: int, height: int, isOpaque: bool) None
Sets
ImageInfoto width, height, and the native color type; and allocates pixel memory.If isOpaque is true, sets
ImageInfotokOpaque_AlphaType; otherwise, sets tokPremul_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
Bitmapthat matchesPMColor, the native pixel arrangement on the platform.Bitmapdrawn 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
ImageInfoto info following the rules insetInfo()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
ImageInfocould 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:
info (skia.ImageInfo) – contains width, height,
AlphaType,ColorType,ColorSpacerowBytes (int) – size of pixel row or larger; may be zero
- Bitmap.allocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) None
Sets
ImageInfoto info following the rules insetInfo()and allocates pixel memory.Memory is zeroed.
Aborts execution if
ImageInfocould 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:
info (skia.ImageInfo) – contains width, height,
AlphaType,ColorType,ColorSpaceflags (int) –
kZeroPixels_AllocFlag, or zero
- Bitmap.alphaType(self: skia.Bitmap) SkAlphaType
- Bitmap.bounds(self: skia.Bitmap) skia.IRect
- 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 withImageInfo.The reference count of
ColorSpaceis unchanged. The returnedColorSpaceis immutable.- Returns:
ColorSpaceinImageInfo, 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()exceedswidth(). Returns SIZE_MAX if result does not fit in size_t. Returns zero ifheight()orwidth()is 0. Returnsheight()timesrowBytes()ifcolorType()iskUnknown_ColorType.- Returns:
size in bytes of image buffer
- Bitmap.dimensions(self: skia.Bitmap) skia.ISize
- Bitmap.drawsNothing(self: skia.Bitmap) bool
Returns true if
width()orheight()are zero, or ifPixelRefis nullptr.If true,
Bitmaphas 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()orheight()are zero.Does not check if
PixelRefis nullptr; calldrawsNothing()to checkwidth(),height(), andPixelRef.- Returns:
true if dimensions do not enclose area
- Bitmap.erase(*args, **kwargs)
Overloaded function.
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_ColorTypeorkRGB_565_ColorType, then alpha is ignored; RGB is treated as opaque. If colorType() iskAlpha_8_ColorType, then RGB is ignored.- param int c:
unpremultiplied color
- param skia.IRect area:
rectangle to fill
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_ColorTypeorkRGB_565_ColorType, then alpha is ignored; RGB is treated as opaque. If colorType() iskAlpha_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,MaskFiltergenerates mask alpha fromBitmap. Uses HeapAllocator to reserve memory for dstPixelRef. Sets offset to top-left position for dst for alignment withBitmap; (0, 0) unlessMaskFiltergenerates mask.- Parameters:
dst – holds
PixelRefto fill with alpha layerpaint – holds optional
MaskFilter; may be nullptroffset – 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
PixelRefwith dst.Pixels are not copied;
Bitmapand dst point to the same pixels; dstbounds()are set to the intersection of subset and the originalbounds().subset may be larger than
bounds(). Any area outside ofbounds()is ignored.Any contents of dst are discarded.
isVolatile()setting is copied to dst. dst is set tocolorType(),alphaType(), andcolorSpace().Return false if:
- Parameters:
dst –
Bitmapset to subsetsubset – 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()ofgetColor(), but can be more efficent (and more precise if the pixels store more than 8 bits per component).
- 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
ColorTypeiskAlpha_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
ColorTypeiskUnknown_ColorTypeor pixel address is nullptr.ColorSpaceinImageInfois 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 ifPixelRefis 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
PixelReforigin.- Returns:
bounds within
PixelRefbounds
- Bitmap.height(self: skia.Bitmap) int
Returns pixel row count.
Maybe be less than
pixelRef().height(). Will not exceedpixelRef().height()lesspixelRefOrigin(). fY.- Returns:
pixel height in
ImageInfo
- Bitmap.info(self: skia.Bitmap) SkImageInfo
Returns width, height,
AlphaType,ColorType, andColorSpace.- Returns:
reference to
ImageInfo
- Bitmap.installPixels(*args, **kwargs)
Overloaded function.
installPixels(self: skia.Bitmap, info: SkImageInfo, pixels: object, rowBytes: int) -> bool
Sets
ImageInfoto info following the rules insetInfo(), and createsPixelRefcontaining pixels and rowBytes.If
ImageInfocould not be set, or rowBytes is less than info.minRowBytes(): callsreset(), and returns false.Otherwise, if pixels equals nullptr: sets
ImageInfo, returns true.Caller must ensure that pixels are valid for the lifetime of
BitmapandPixelRef.- 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
ImageInfois set to info
installPixels(self: skia.Bitmap, pixmap: SkPixmap) -> bool
Sets
ImageInfoto pixmap.info() following the rules insetInfo(), and createsPixelRefcontaining pixmap.addr() and pixmap.rowBytes().If
ImageInfocould not be set, or pixmap.rowBytes() is less thanImageInfo.minRowBytes(): callsreset(), and returns false.Otherwise, if pixmap.addr() equals nullptr: sets
ImageInfo, returns true.Caller must ensure that pixmap is valid for the lifetime of
BitmapandPixelRef.
- Bitmap.isImmutable(self: skia.Bitmap) bool
Returns true if pixels can not change.
Most immutable
Bitmapchecks trigger an assert only on debug builds.- Returns:
true if pixels are immutable
- Bitmap.isNull(self: skia.Bitmap) bool
Returns true if
PixelRefis nullptr.Does not check if
width()orheight()are zero; calldrawsNothing()to checkwidth(),height(), andPixelRef.- Returns:
true if no
PixelRefis associated
- Bitmap.isOpaque(self: skia.Bitmap) bool
Returns true if
AlphaTypeis 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
ColorTypeallows alpha, or if any pixel value has transparency.- Returns:
true if
ImageInfoAlphaTypeiskOpaque_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
PixelRefhave changed.Subsequent calls to
getGenerationID()return a different value.
- Bitmap.peekPixels(self: skia.Bitmap, pixmap: SkPixmap) bool
Copies
Bitmappixel address, row bytes, andImageInfoto 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
Bitmaphas direct access to pixels
- Bitmap.pixmap(self: skia.Bitmap) SkPixmap
Returns a constant reference to the
Pixmapholding theBitmappixel address, row bytes, andImageInfo.
- Bitmap.readPixels(*args, **kwargs)
Overloaded function.
readPixels(self: skia.Bitmap, dstInfo: SkImageInfo, dstPixels: Buffer, dstRowBytes: int = 0, srcX: int = 0, srcY: int = 0) -> bool
Copies a
Rectof pixels fromBitmapto dstPixels.Copy starts at (srcX, srcY), and does not exceed
Bitmap(width(),height()).dstInfo specifies width, height,
ColorType,AlphaType, andColorSpaceof 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()
PixelRefis nullptr
Pixels are copied only if pixel conversion is possible. If
BitmapcolorType() iskGray_8_ColorType, orkAlpha_8_ColorType; dstInfo.colorType() must match. IfBitmapcolorType() iskGray_8_ColorType, dstInfo.colorSpace() must match. IfBitmapalphaType() iskOpaque_AlphaType, dstInfo.alphaType() must match. IfBitmapcolorSpace() 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
readPixels(self: skia.Bitmap, dst: SkPixmap, srcX: int = 0, srcY: int = 0) -> bool
Copies a
Rectof pixels fromBitmapto 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:dst pixel storage equals nullptr
dst.rowBytes is less than
ImageInfo.minRowBytes()PixelRefis nullptr
Pixels are copied only if pixel conversion is possible. If
BitmapcolorType() iskGray_8_ColorType, orkAlpha_8_ColorType; dstColorTypemust match. IfBitmapcolorType() iskGray_8_ColorType, dstColorSpacemust match. IfBitmapalphaType() iskOpaque_AlphaType, dstAlphaTypemust match. IfBitmapcolorSpace() is nullptr, dstColorSpacemust 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().
- Bitmap.readyToDraw(self: skia.Bitmap) bool
Returns true if
Bitmapis can be drawn.- Returns:
true if
getPixelsis not nullptr
- Bitmap.refColorSpace(self: skia.Bitmap) skia.ColorSpace
Returns smart pointer to
ColorSpace, the range of colors, associated withImageInfo.The smart pointer tracks the number of objects sharing this
ColorSpacereference so the memory is released when the owners destruct.The returned
ColorSpaceis immutable.- Returns:
ColorSpaceinImageInfowrapped in a smart pointer
- Bitmap.reset(self: skia.Bitmap) None
Resets to its initial state; all fields are set to zero, as if
Bitmaphad been initialized byBitmap.Sets width, height, row bytes to zero; pixel address to nullptr;
ColorTypetokUnknown_ColorType; andAlphaTypetokUnknown_AlphaType.If
PixelRefis allocated, its reference count is decreased by one, releasing its memory ifBitmapis 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()iskUnknown_ColorType, or if row bytes supplied tosetInfo()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 withColorType.Returns true unless alphaType is
kUnknown_AlphaTypeand currentAlphaTypeis notkUnknown_AlphaType.Returns true if
ColorTypeiskUnknown_ColorType. alphaType is ignored, andAlphaTyperemainskUnknown_AlphaType.Returns true if
ColorTypeiskRGB_565_ColorTypeorkGray_8_ColorType. alphaType is ignored, andAlphaTyperemainskOpaque_AlphaType.If
ColorTypeiskARGB_4444_ColorType,kRGBA_8888_ColorType,kBGRA_8888_ColorType, orkRGBA_F16_ColorType: returns true unless alphaType iskUnknown_AlphaTypeandAlphaTypeis notkUnknown_AlphaType. IfAlphaTypeiskUnknown_AlphaType, alphaType is ignored.If
ColorTypeiskAlpha_8_ColorType, returns true unless alphaType iskUnknown_AlphaTypeandAlphaTypeis notkUnknown_AlphaType. IfAlphaTypeiskUnknown_AlphaType, alphaType is ignored. If alphaType iskUnpremul_AlphaType, it is treated askPremul_AlphaType.This changes
AlphaTypeinPixelRef; all bitmaps sharingPixelRefare affected.- Returns:
true if
AlphaTypeis set
- Bitmap.setImmutable(self: skia.Bitmap) None
Sets internal flag to mark
Bitmapas immutable.Once set, pixels can not change. Any other bitmap sharing the same
PixelRefare also marked as immutable. OncePixelRefis marked immutable, the setting cannot be cleared.Writing to immutable
Bitmappixels 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 tokUnknown_AlphaType. If imageInfo.colorType() iskAlpha_8_ColorTypeand imageInfo.alphaType() iskUnpremul_AlphaType, imageInfo.alphaType() is replaced bykPremul_AlphaType. If imageInfo.colorType() iskRGB_565_ColorTypeorkGray_8_ColorType, imageInfo.alphaType() is set tokOpaque_AlphaType. If imageInfo.colorType() iskARGB_4444_ColorType,kRGBA_8888_ColorType,kBGRA_8888_ColorType, orkRGBA_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 otherColorSpacevalues, 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:
imageInfo (skia.ImageInfo) – contains width, height,
AlphaType,ColorType,ColorSpacerowBytes (int) – imageInfo.minRowBytes() or larger; or zero
- Returns:
true if
ImageInfoset successfully
- Bitmap.setPixels(self: skia.Bitmap, pixels: Buffer) None
Replaces
PixelRefwith pixels, preservingImageInfoand rowBytes().Sets
PixelReforigin to (0, 0).If pixels is nullptr, or if info().colorType() equals
kUnknown_ColorType; release reference toPixelRef, and setPixelRefto nullptr.Caller is responsible for handling ownership pixel memory for the lifetime of
BitmapandPixelRef.- 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) –
Bitmapexchanged with original
- Bitmap.tobytes(self: skia.Bitmap) object
- Bitmap.tryAllocN32Pixels(self: skia.Bitmap, width: int, height: int, isOpaque: bool) bool
Sets
ImageInfoto width, height, and native color type; and allocates pixel memory.If isOpaque is true, sets
ImageInfotokOpaque_AlphaType; otherwise, sets tokPremul_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
Bitmapthat matchesPMColor, the native pixel arrangement on the platform.Bitmapdrawn 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
ImageInfoto info following the rules insetInfo()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()ifImageInfocould 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:
info (skia.ImageInfo) – contains width, height,
AlphaType,ColorType,ColorSpacerowBytes (int) – size of pixel row or larger; may be zero
- Returns:
true if pixel storage is allocated
- Bitmap.tryAllocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) bool
Sets
ImageInfoto info following the rules insetInfo()and allocates pixel memory.Memory is zeroed.
Returns false and calls
reset()ifImageInfocould 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:
info (skia.ImageInfo) – contains width, height,
AlphaType,ColorType,ColorSpaceflags (int) –
kZeroPixels_AllocFlag, or zero
- 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 exceedpixelRef().width()lesspixelRefOrigin(). fX.- Returns:
pixel width in SkImageInfo
- Bitmap.writePixels(self: skia.Bitmap, src: SkPixmap, dstX: int = 0, dstY: int = 0) bool
Copies a
Rectof 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()PixelRefis nullptr
Pixels are copied only if pixel conversion is possible. If
BitmapcolorType() iskGray_8_ColorType, orkAlpha_8_ColorType; srcColorTypemust match. IfBitmapcolorType() iskGray_8_ColorType, srcColorSpacemust match. IfBitmapalphaType() iskOpaque_AlphaType, srcAlphaTypemust match. IfBitmapcolorSpace() is nullptr, srcColorSpacemust 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 bytesdstX (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>