Pixmap
- class Pixmap
Pixmapprovides a utility to pairImageInfowith pixels and row bytes.Pixmapis a low level class which provides convenience functions to access raster destinations.Canvascan not drawPixmap, nor doesPixmapprovide a direct drawing destination.Use
Bitmapto draw pixels referenced byPixmap; useSurfaceto draw into pixels referenced byPixmap.Pixmapdoes not try to manage the lifetime of the pixel memory. UsePixelRefto manage pixel memory;PixelRefis safe across threads.Pixmapsupports buffer protocol. It is possible to mountPixmapas array:array = np.array(pixmap, copy=False)
Or mount array as
PixmapwithImageInfo:buffer = np.zeros((100, 100, 4), np.uint8) array = skia.Pixmap(skia.ImageInfo.MakeN32Premul(100, 100), buffer)
Methods
addr(self: skia.Pixmap) -> memoryview
addr16(self: skia.Pixmap) -> memoryview
addr32(self: skia.Pixmap) -> memoryview
addr64(self: skia.Pixmap) -> memoryview
addr8(self: skia.Pixmap) -> memoryview
alphaType(self: skia.Pixmap) -> skia.AlphaType
bounds(self: skia.Pixmap) -> skia.IRect
colorSpace(self: skia.Pixmap) -> skia.ColorSpace
colorType(self: skia.Pixmap) -> skia.ColorType
computeByteSize(self: skia.Pixmap) -> int
computeIsOpaque(self: skia.Pixmap) -> bool
dimensions(self: skia.Pixmap) -> skia.ISize
erase(self: skia.Pixmap, color: skia.Color4f, subset: skia.IRect = None) -> bool
extractSubset(self: skia.Pixmap, subset: skia.Pixmap, area: skia.IRect) -> bool
getAlphaf(self: skia.Pixmap, x: int, y: int) -> float
getColor(self: skia.Pixmap, x: int, y: int) -> int
height(self: skia.Pixmap) -> int
info(self: skia.Pixmap) -> skia.ImageInfo
isOpaque(self: skia.Pixmap) -> bool
refColorSpace(self: skia.Pixmap) -> skia.ColorSpace
rowBytes(self: skia.Pixmap) -> int
rowBytesAsPixels(self: skia.Pixmap) -> int
scalePixels(self: skia.Pixmap, dst: skia.Pixmap, samplingOptions: skia.SamplingOptions = skia.SamplingOptions()) -> bool
setColorSpace(self: skia.Pixmap, colorSpace: skia.ColorSpace) -> None
shiftPerPixel(self: skia.Pixmap) -> int
tobytes(self: skia.Pixmap) -> object
width(self: skia.Pixmap) -> int
writable_addr(self: skia.Pixmap) -> memoryview
Methods
- Pixmap.__init__(*args, **kwargs)
Overloaded function.
__init__(self: skia.Pixmap) -> None
Creates an empty
Pixmapwithout pixels, withkUnknown_ColorType, withkUnknown_AlphaType, and with a width and height of zero.Use
reset()to associate pixels,ColorType,AlphaType, width, and height afterPixmaphas been created.- return:
empty
Pixmap
__init__(self: skia.Pixmap, info: skia.ImageInfo, data: object, rowBytes: int) -> None
Creates
Pixmapfrom info width, height,AlphaType, andColorType.datapoints to pixels, or nullptr. rowBytes should beinfo.width()timesinfo.bytesPerPixel(), or larger.No parameter checking is performed; it is up to the caller to ensure that data and rowBytes agree with info.
The memory lifetime of pixels is managed by the caller. When
Pixmapgoes out of scope, data is unaffected.Pixmapmay be later modified byreset()to change its size, pixel type, or storage.__init__(self: skia.Pixmap, array: numpy.ndarray, colorType: skia.ColorType = skia.ColorType.kN32_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None) -> None
Creates
Pixmapbacked by numpy array.The memory lifetime of pixels is managed by the caller. When
Pixmapgoes out of scope, data is unaffected.- array:
numpy ndarray of shape=(height, width, channels). Must have non-zero width and height, and the valid number of channels for the specified color type.
- colorType:
color type of the array
- alphaType:
alpha type of the array
- colorSpace:
range of colors; may be nullptr
- Pixmap.addr(self: skia.Pixmap) memoryview
Returns pixel address, the base address corresponding to the pixel origin.
It is up to the
Pixmapcreator to ensure that pixel address is a useful value.- Returns:
pixel address
- Return type:
memoryview
- Pixmap.addr16(self: skia.Pixmap) memoryview
Returns readable base pixel address.
Result is addressable as unsigned 16-bit words. Will trigger an assert() if
ColorTypeis notkRGB_565_ColorTypeorkARGB_4444_ColorType, and is built with SK_DEBUG defined.One word corresponds to one pixel.
- Returns:
readable unsigned 16-bit pointer to pixels
- Return type:
memoryview
- Pixmap.addr32(self: skia.Pixmap) memoryview
Returns readable base pixel address.
Result is addressable as unsigned 32-bit words. Will trigger an assert() if
ColorTypeis notkRGBA_8888_ColorTypeorkBGRA_8888_ColorType, and is built with SK_DEBUG defined.One word corresponds to one pixel.
- Returns:
readable unsigned 32-bit pointer to pixels
- Return type:
memoryview
- Pixmap.addr64(self: skia.Pixmap) memoryview
Returns readable base pixel address.
Result is addressable as unsigned 64-bit words. Will trigger an assert() if
ColorTypeis notkRGBA_F16_ColorTypeand is built with SK_DEBUG defined.One word corresponds to one pixel.
- Returns:
readable unsigned 64-bit pointer to pixels
- Return type:
memoryview
- Pixmap.addr8(self: skia.Pixmap) memoryview
Returns readable base pixel address.
Result is addressable as unsigned 8-bit bytes. Will trigger an assert() if
ColorTypeis notkAlpha_8_ColorTypeorkGray_8_ColorType, and is built with SK_DEBUG defined.One byte corresponds to one pixel.
- Returns:
readable unsigned 8-bit pointer to pixels
- Return type:
memoryview
- Pixmap.alphaType(self: skia.Pixmap) skia.AlphaType
- Pixmap.bounds(self: skia.Pixmap) skia.IRect
- Pixmap.colorSpace(self: skia.Pixmap) skia.ColorSpace
Returns
ColorSpace, the range of colors, associated withImageInfo.The reference count of
ColorSpaceis unchanged. The returnedColorSpaceis immutable.- Returns:
ColorSpaceinImageInfo, or nullptr
- Pixmap.colorType(self: skia.Pixmap) skia.ColorType
- Pixmap.computeByteSize(self: skia.Pixmap) 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 ifheight()orwidth()is 0. Returnsheight()timesrowBytes()ifcolorType()iskUnknown_ColorType.- Returns:
size in bytes of image buffer
- Pixmap.computeIsOpaque(self: skia.Pixmap) bool
Returns true if all pixels are opaque.
ColorTypedetermines how pixels are encoded, and whether pixel describes alpha. Returns true forColorTypewithout alpha in each pixel; for otherColorType, returns true if all pixels have alpha values equivalent to 1.0 or greater.For
ColorTypekRGB_565_ColorTypeorkGray_8_ColorType: always returns true. ForColorTypekAlpha_8_ColorType,kBGRA_8888_ColorType,kRGBA_8888_ColorType: returns true if all pixel alpha values are 255. ForColorTypekARGB_4444_ColorType: returns true if all pixel alpha values are 15. ForkRGBA_F16_ColorType: returns true if all pixel alpha values are 1.0 or greater.Returns false for
kUnknown_ColorType.- Returns:
true if all pixels have opaque values or
ColorTypeis opaque
- Pixmap.dimensions(self: skia.Pixmap) skia.ISize
Return the dimensions of the pixmap (from its ImageInfo)
- Pixmap.erase(self: skia.Pixmap, color: skia.Color4f, subset: skia.IRect = None) bool
Writes color to pixels bounded by subset; returns true on success.
if subset is nullptr, writes colors pixels inside
bounds(). Returns false ifcolorType()iskUnknown_ColorType, if subset is not nullptr and does not intersectbounds(), or if subset is nullptr andbounds()is empty.- Parameters:
color – sRGB unpremultiplied color to write
subset – bounding integer
Rectof pixels to write; may be nullptr
- Returns:
true if pixels are changed
- Pixmap.extractSubset(self: skia.Pixmap, subset: skia.Pixmap, area: skia.IRect) bool
Sets subset width, height, pixel address to intersection of
Pixmapwith area, if intersection is not empty; and return true.Otherwise, leave subset unchanged and return false.
Failing to read the return value generates a compile time warning.
- Parameters:
subset (skia.Pixmap) – storage for width, height, pixel address of intersection
area (skia.IRect) – bounds to intersect with
Pixmap
- Returns:
true if intersection of
Pixmapand area is not empty
- Pixmap.getAlphaf(self: skia.Pixmap, 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`( :py:meth:`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
- Pixmap.getColor(self: skia.Pixmap, 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
- Pixmap.height(self: skia.Pixmap) int
Returns pixel row count.
- Returns:
pixel height in SkImageInfo
- Pixmap.info(self: skia.Pixmap) skia.ImageInfo
Returns width, height,
AlphaType,ColorType, andColorSpace.- Returns:
reference to
ImageInfo
- Pixmap.isOpaque(self: skia.Pixmap) bool
Returns true if
AlphaTypeiskOpaque_AlphaType.Does not check if
ColorTypeallows alpha, or if any pixel value has transparency.
- Pixmap.readPixels(*args, **kwargs)
Overloaded function.
readPixels(self: skia.Pixmap, dstInfo: skia.ImageInfo, dstPixels: Buffer, dstRowBytes: int = 0, srcX: int = 0, srcY: int = 0) -> bool
Copies
Rectof pixels to dstPixels.Copy starts at offset (srcX, srcY), and does not exceed
Pixmap(width(),height()).dstInfo specifies width, height,
ColorType,AlphaType, andColorSpaceof destination. dstRowBytes specifies the gap from one destination row to the next. Returns true if pixels are copied. Returns false if dstInfo address equals nullptr, or dstRowBytes is less thandstInfo.minRowBytes().Pixels are copied only if pixel conversion is possible. If
PixmapColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType;dstInfo.colorTypemust match. IfPixmapColorTypeiskGray_8_ColorType,dstInfo.colorSpacemust match. IfPixmapAlphaTypeiskOpaque_AlphaType,dstInfo.alphaTypemust match. IfPixmapColorSpaceis nullptr,dstInfo.colorSpacemust 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()orheight()is zero or negative. Returns false if abs(srcX) >= Pixmapwidth(), or if abs(srcY) >= Pixmapheight().- 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.Pixmap, dst: skia.Pixmap, srcX: int = 0, srcY: int = 0) -> bool
Copies
Rectof pixels to dst.Copy starts at offset (srcX, srcY), and does not exceed
Pixmap(width(),height()).dstInfo specifies width, height,
ColorType,AlphaType, andColorSpaceof destination. dstRowBytes specifies the gap from one destination row to the next. Returns true if pixels are copied. Returns false if dstInfo address equals nullptr, or dstRowBytes is less thandstInfo.minRowBytes().Pixels are copied only if pixel conversion is possible. If
PixmapColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType;dstInfo.colorTypemust match. IfPixmapColorTypeiskGray_8_ColorType,dstInfo.colorSpacemust match. IfPixmapAlphaTypeiskOpaque_AlphaType,dstInfo.alphaTypemust match. IfPixmapColorSpaceis nullptr,dstInfo.colorSpacemust 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()orheight()is zero or negative. Returns false if abs(srcX) >= Pixmapwidth(), or if abs(srcY) >= Pixmapheight().
- Pixmap.refColorSpace(self: skia.Pixmap) 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
- Pixmap.reset(*args, **kwargs)
Overloaded function.
reset(self: skia.Pixmap) -> None
Sets width, height, row bytes to zero; pixel address to nullptr;
ColorTypetokUnknown_ColorType; andAlphaTypetokUnknown_AlphaType.The prior pixels are unaffected; it is up to the caller to release pixels memory if desired.
reset(self: skia.Pixmap, info: skia.ImageInfo, data: object, rowBytes: int) -> None
Sets width, height,
AlphaType, andColorTypefrom info.Sets pixel address from data, which may be nullptr. Sets row bytes from rowBytes, which should be
info.width()timesinfo.bytesPerPixel(), or larger.Does not check data. Asserts if built with SK_DEBUG defined and if rowBytes is too small to hold one row of pixels.
The memory lifetime pixels are managed by the caller. When
Pixmapgoes out of scope, data is unaffected.
- Pixmap.rowBytes(self: skia.Pixmap) 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. It is up to theBitmapcreator to ensure that row bytes is a useful value.- Returns:
byte length of pixel row
- Pixmap.rowBytesAsPixels(self: skia.Pixmap) int
Returns number of pixels that fit on row.
Should be greater than or equal to
width().- Returns:
maximum pixels per row
- Pixmap.scalePixels(self: skia.Pixmap, dst: skia.Pixmap, samplingOptions: skia.SamplingOptions = skia.SamplingOptions()) bool
Copies
Pixmapto dst, scaling pixels to fitdst.width()anddst.height(), and converting pixels to matchdst.colorType()anddst.alphaType().Returns true if pixels are copied. Returns false if
dst.addr()is nullptr, ordst.rowBytes()is less than dstImageInfo.minRowBytes().Pixels are copied only if pixel conversion is possible. If
PixmapColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType;dst.colorType()must match. IfPixmapColorTypeiskGray_8_ColorType,dst.colorSpace()must match. IfPixmapAlphaTypeiskOpaque_AlphaType,dst.alphaType()must match. IfPixmapColorSpaceis nullptr,dst.colorSpace()must match. Returns false if pixel conversion is not possible.Returns false if
Pixmapwidth()orheight()is zero or negative.Scales the image, with filterQuality, to match
dst.width()anddst.height(). filterQualityNone_FilterQualityis fastest, typically implemented with nearest neighbor filter.kLow_FilterQualityis typically implemented with bilerp filter.kMedium_FilterQualityis typically implemented with bilerp filter, and mip-map filter when size is reduced.kHigh_FilterQualityis slowest, typically implemented with bicubic filter.- Parameters:
dst (skia.Pixmap) – destination
Pixmap:ImageInfo, pixels, row bytesoptions (skia.SamplingOptions) – sampling options
- Returns:
true if pixels are scaled to fit dst
- Pixmap.setColorSpace(self: skia.Pixmap, colorSpace: skia.ColorSpace) None
Changes
ColorSpaceinImageInfo; preserves width, height,AlphaType, andColorTypeinImage, and leaves pixel address and row bytes unchanged.ColorSpacereference count is incremented.- Parameters:
colorSpace (skia.ColorSpace) –
ColorSpacemoved toImageInfo
- Pixmap.shiftPerPixel(self: skia.Pixmap) 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
- Pixmap.tobytes(self: skia.Pixmap) object
- Pixmap.width(self: skia.Pixmap) int
Returns pixel count in each pixel row.
Should be equal or less than:
rowBytes()/info().bytesPerPixel().- Returns:
pixel width in
ImageInfo
- Pixmap.writable_addr(self: skia.Pixmap) memoryview
Returns writable base pixel address.
- Returns:
writable generic base pointer to pixels
- Return type:
memoryview