Bitmap
- class Bitmap
Bitmap
describes a two-dimensional raster pixel array.Bitmap
is built onImageInfo
, containing integer width and height,ColorType
andAlphaType
describing the pixel format, andColorSpace
describing the range of colors.Bitmap
points toPixelRef
, which describes the physical array of pixels.ImageInfo
bounds may be located anywhere fully insidePixelRef
bounds.Bitmap
can be drawn usingCanvas
.Bitmap
can be a drawing destination forCanvas
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, useSurface
for better performance.Declaring
Bitmap
const prevents alteringImageInfo
: theBitmap
height, width, and so on cannot change. It does not affectPixelRef
: a caller may write its pixels. DeclaringBitmap
const affectsBitmap
configuration, not its contents.Bitmap
is not thread safe. Each thread must have its own copy ofBitmap
fields, although threads may share the underlying pixel array.Bitmap
supports buffer protocol. It is possible to mountBitmap
as array:array = np.array(bitmap, copy=False)
Or mount array as
Bitmap
withImageInfo
:buffer = np.zeros((100, 100, 4), np.uint8) bitmap = skia.Bitmap() bitmap.setInfo(skia.ImageInfo.MakeN32Premul(100, 100)) bitmap.setPixels(buffer)
Classes
Members:
Methods
Creates a
Bitmap
.Sets
ImageInfo
to width, height, and the native color type; and allocates pixel memory.Sets
ImageInfo
to info following the rules insetInfo()
and allocates pixel memory.Sets
ImageInfo
to info following the rules insetInfo()
and allocates pixel memory.Returns number of bytes per pixel required by
ColorType
.Returns
ColorSpace
, the range of colors, associated withImageInfo
.Returns minimum memory required for pixel storage.
Returns true if
width()
orheight()
are zero, or ifPixelRef
is nullptr.Overloaded function.
Replaces pixel values with unpremultiplied color built from a, r, g, and b, interpreted as being in the sRGB
ColorSpace
.Sets dst to alpha described by pixels.
Shares
PixelRef
with dst.Look up the pixel at (x,y) and return its alpha component, normalized to [0..1].
Returns pixel at (x, y) as unpremultiplied color.
Returns a unique value corresponding to the pixels in
PixelRef
.Returns pixel address, the base address corresponding to the pixel origin.
Returns the bounds of this bitmap, offset by its
PixelRef
origin.Returns pixel row count.
Returns width, height,
AlphaType
,ColorType
, andColorSpace
.Overloaded function.
Returns true if pixels can not change.
Returns true if
PixelRef
is nullptr.Returns true if
AlphaType
is set to hint that all pixels are opaque; their alpha value is implicitly or explicitly 1.0.Marks that pixels in
PixelRef
have changed.Copies
Bitmap
pixel address, row bytes, andImageInfo
to pixmap, if address is available, and returns true.Returns a constant reference to the
Pixmap
holding theBitmap
pixel address, row bytes, andImageInfo
.Overloaded function.
Returns true if
Bitmap
is can be drawn.Returns smart pointer to
ColorSpace
, the range of colors, associated withImageInfo
.Resets to its initial state; all fields are set to zero, as if
Bitmap
had been initialized byBitmap
.Returns row bytes, the interval from one pixel row to the next.
Returns number of pixels that fit on row.
Sets internal flag to mark
Bitmap
as immutable.Sets width, height,
AlphaType
,ColorType
,ColorSpace
, and optional rowBytes.Replaces
PixelRef
with pixels, preservingImageInfo
and rowBytes().Returns bit shift converting row bytes to row pixels.
Swaps the fields of the two bitmaps.
Sets
ImageInfo
to width, height, and native color type; and allocates pixel memory.Sets
ImageInfo
to info following the rules insetInfo()
and allocates pixel memory.Sets
ImageInfo
to info following the rules insetInfo()
and allocates pixel memory.Returns pixel count in each row.
Copies a
Rect
of pixels from src.Attributes
Methods
- Bitmap.__init__(self: skia.Bitmap, src: skia.Bitmap = None) None
Creates a
Bitmap
.When
src
is given, copies settings from src to returnedBitmap
.Shares pixels if src has pixels allocated, so both bitmaps reference the same pixels.
When
src
isNone
, creates an emptyBitmap
without pixels, withkUnknown_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 associateColorType
,AlphaType
, width, and height afterBitmap
has been created.
- 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
tokOpaque_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
Bitmap
that matchesPMColor
, 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 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
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:
info (skia.ImageInfo) – contains width, height,
AlphaType
,ColorType
,ColorSpace
rowBytes (int) – size of pixel row or larger; may be zero
- Bitmap.allocPixelsFlags(self: skia.Bitmap, info: SkImageInfo, flags: int) None
Sets
ImageInfo
to info following the rules insetInfo()
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:
info (skia.ImageInfo) – contains width, height,
AlphaType
,ColorType
,ColorSpace
flags (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
ColorSpace
is unchanged. The returnedColorSpace
is immutable.- Returns:
ColorSpace
inImageInfo
, 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 ifPixelRef
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()
orheight()
are zero.Does not check if
PixelRef
is 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_ColorType
orkRGB_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_ColorType
orkRGB_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
,MaskFilter
generates mask alpha fromBitmap
. Uses HeapAllocator to reserve memory for dstPixelRef
. Sets offset to top-left position for dst for alignment withBitmap
; (0, 0) unlessMaskFilter
generates mask.- Parameters:
dst – holds
PixelRef
to 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
PixelRef
with dst.Pixels are not copied;
Bitmap
and 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 –
Bitmap
set 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
ColorType
iskAlpha_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
iskUnknown_ColorType
or pixel address is nullptr.ColorSpace
inImageInfo
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 ifPixelRef
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 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
ImageInfo
to info following the rules insetInfo()
, and createsPixelRef
containing pixels and rowBytes.If
ImageInfo
could 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
Bitmap
andPixelRef
.- 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
installPixels(self: skia.Bitmap, pixmap: SkPixmap) -> bool
Sets
ImageInfo
to pixmap.info() following the rules insetInfo()
, and createsPixelRef
containing pixmap.addr() and pixmap.rowBytes().If
ImageInfo
could 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
Bitmap
andPixelRef
.
- 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()
orheight()
are zero; calldrawsNothing()
to checkwidth()
,height()
, andPixelRef
.- 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
iskOpaque_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, andImageInfo
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 theBitmap
pixel 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
Rect
of pixels fromBitmap
to dstPixels.Copy starts at (srcX, srcY), and does not exceed
Bitmap
(width()
,height()
).dstInfo specifies width, height,
ColorType
,AlphaType
, andColorSpace
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() iskGray_8_ColorType
, orkAlpha_8_ColorType
; dstInfo.colorType() must match. IfBitmap
colorType() iskGray_8_ColorType
, dstInfo.colorSpace() must match. IfBitmap
alphaType() iskOpaque_AlphaType
, dstInfo.alphaType() must match. IfBitmap
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
readPixels(self: skia.Bitmap, dst: SkPixmap, srcX: int = 0, srcY: int = 0) -> bool
Copies a
Rect
of pixels fromBitmap
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:dst pixel storage equals nullptr
dst.rowBytes is less than
ImageInfo.minRowBytes()
PixelRef
is nullptr
Pixels are copied only if pixel conversion is possible. If
Bitmap
colorType() iskGray_8_ColorType
, orkAlpha_8_ColorType
; dstColorType
must match. IfBitmap
colorType() iskGray_8_ColorType
, dstColorSpace
must match. IfBitmap
alphaType() iskOpaque_AlphaType
, dstAlphaType
must match. IfBitmap
colorSpace() is nullptr, dstColorSpace
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().
- 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 withImageInfo
.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
inImageInfo
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 byBitmap
.Sets width, height, row bytes to zero; pixel address to nullptr;
ColorType
tokUnknown_ColorType
; andAlphaType
tokUnknown_AlphaType
.If
PixelRef
is allocated, its reference count is decreased by one, releasing its memory ifBitmap
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()
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_AlphaType
and currentAlphaType
is notkUnknown_AlphaType
.Returns true if
ColorType
iskUnknown_ColorType
. alphaType is ignored, andAlphaType
remainskUnknown_AlphaType
.Returns true if
ColorType
iskRGB_565_ColorType
orkGray_8_ColorType
. alphaType is ignored, andAlphaType
remainskOpaque_AlphaType
.If
ColorType
iskARGB_4444_ColorType
,kRGBA_8888_ColorType
,kBGRA_8888_ColorType
, orkRGBA_F16_ColorType
: returns true unless alphaType iskUnknown_AlphaType
andAlphaType
is notkUnknown_AlphaType
. IfAlphaType
iskUnknown_AlphaType
, alphaType is ignored.If
ColorType
iskAlpha_8_ColorType
, returns true unless alphaType iskUnknown_AlphaType
andAlphaType
is notkUnknown_AlphaType
. IfAlphaType
iskUnknown_AlphaType
, alphaType is ignored. If alphaType iskUnpremul_AlphaType
, it is treated askPremul_AlphaType
.This changes
AlphaType
inPixelRef
; all bitmaps sharingPixelRef
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. OncePixelRef
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 tokUnknown_AlphaType
. If imageInfo.colorType() iskAlpha_8_ColorType
and imageInfo.alphaType() iskUnpremul_AlphaType
, imageInfo.alphaType() is replaced bykPremul_AlphaType
. If imageInfo.colorType() iskRGB_565_ColorType
orkGray_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 otherColorSpace
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:
imageInfo (skia.ImageInfo) – contains width, height,
AlphaType
,ColorType
,ColorSpace
rowBytes (int) – imageInfo.minRowBytes() or larger; or zero
- Returns:
true if
ImageInfo
set successfully
- Bitmap.setPixels(self: skia.Bitmap, pixels: Buffer) None
Replaces
PixelRef
with pixels, preservingImageInfo
and rowBytes().Sets
PixelRef
origin to (0, 0).If pixels is nullptr, or if info().colorType() equals
kUnknown_ColorType
; release reference toPixelRef
, and setPixelRef
to nullptr.Caller is responsible for handling ownership pixel memory for the lifetime of
Bitmap
andPixelRef
.- 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
tokOpaque_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
Bitmap
that matchesPMColor
, 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 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()
ifImageInfo
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:
info (skia.ImageInfo) – contains width, height,
AlphaType
,ColorType
,ColorSpace
rowBytes (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
ImageInfo
to info following the rules insetInfo()
and allocates pixel memory.Memory is zeroed.
Returns false and calls
reset()
ifImageInfo
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:
info (skia.ImageInfo) – contains width, height,
AlphaType
,ColorType
,ColorSpace
flags (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
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() iskGray_8_ColorType
, orkAlpha_8_ColorType
; srcColorType
must match. IfBitmap
colorType() iskGray_8_ColorType
, srcColorSpace
must match. IfBitmap
alphaType() iskOpaque_AlphaType
, srcAlphaType
must match. IfBitmap
colorSpace() is nullptr, srcColorSpace
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 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>