Class Bitmap
- All Implemented Interfaces:
AutoCloseable
Bitmap is created with immutable width, height, row bytes, and memory allocation
(memory address), its contents may be changed. You can allocate zero-initialized
Bitmap via the static factory methods in this class, i.e. createBitmap(int, int, Format)
.
You can also obtain Bitmap from encoded data (PNG, TGA, BMP, JPEG, HDR, PSD, PBM, PGM, and PPM) via
BitmapFactory
.
The color space of Bitmap defaults to sRGB and can only be in RGB model space
or null, color space may be changed via setColorSpace(ColorSpace)
.
The alpha defaults to non-premultiplied (independent of RGB channels),
alpha type may be changed via setPremultiplied(boolean)
.
The Bitmap's format may also be changed via setFormat(Format)
,
as long as the bytes-per-pixel of the old and new formats are the same.
For example, when using BitmapFactory to read a single channel image,
the default format is GRAY_8. You can re-interpret it as ALPHA_8 type via
setFormat(Format)
, and the image data remains unchanged.
The Bitmap provides several methods for manipulating the pixel data,
there are single pixel and bulk access methods, which may perform zero or more
of the following transformations: pixel format (i.e. Format), alpha type
(i.e. premultiplied or not), and color space.
The pixel data of a bitmap can be marked as immutable through setImmutable()
.
This way, only read-only operations are allowed in the future, avoiding the
memory copy in some operations.
This class is not thread safe, but memory safe. Its internal state may
be shared by multiple threads/owners. Nevertheless, it's recommended to call
close()
explicitly, or within a try-with-resource block.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Describes a layout of pixel data in CPU memory.static enum
List of supported formats a bitmap can be saved as (encoding or compressing). -
Field Summary
Modifier and TypeFieldDescriptionstatic final com.ibm.icu.text.DateFormat
static final org.apache.logging.log4j.Marker
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Fills the bitmap's pixels with the specified color and rectangular area.void
close()
Clear the reference to the pixel data.boolean
copyPixelsFromBuffer
(Buffer src, int rowBytes, int dstX, int dstY, int width, int height) Copy the pixels from the specified buffer into bitmap.boolean
copyPixelsToBuffer
(Buffer dst, int rowBytes, int srcX, int srcY, int width, int height) Copy the bitmap's pixels into the specified buffer.static Bitmap
createBitmap
(int width, int height, Bitmap.Format format) Creates a mutable bitmap and its allocation, the content are initialized to zeros.static Bitmap
createBitmap
(int width, int height, Bitmap.Format format, boolean isPremultiplied, ColorSpace colorSpace) Creates a mutable bitmap and its allocation, the content are initialized to zeros.long
The address ofvoid *pixels
in native.int
int
Returns the number of channels.float[]
getColor4f
(int x, int y, float[] dst) Gets the pixel value at the specified location.Returns the color space associated with this bitmap.int
Returns the current pixel format used to interpret pixel data.int
Returns the height of the bitmap.getInfo()
int
getPixelARGB
(int x, int y) Returns theColor
at the specified location.The ref of current pixel data, which may be shared across instances.void
getPixels
(float[] dst, int offset, int stride, int srcX, int srcY, int width, int height) Copies a Rect of pixels to dst.void
getPixels
(int[] dst, int offset, int stride, int srcX, int srcY, int width, int height) Copies a Rect of pixels to dst.void
Copies a Rect of pixels from this bitmap to dst bitmap at (dstX, dstY).The current pixel map, which is usually paired withPixels
.int
Return the distance, in bytes, between the start of one pixel row and the next, including any unused space between them.int
The distance, in bytes, between the start of one pixel row and the next, including any unused space between them.long
getSize()
Returns the number of bytes that can be used to store this bitmap's pixels.int
getWidth()
Returns the width of the bitmap.boolean
hasAlpha()
Returns true if the bitmap's format supports per-pixel alpha, and if the pixels may contain non-opaque alpha values.boolean
isClosed()
Returns true if this bitmap has been closed.boolean
Returns true if the bitmap is marked as immutable.boolean
Indicates whether pixels stored in this bitmaps are stored premultiplied.boolean
static String
openDialogGet
(Bitmap.SaveFormat format, CharSequence title, CharSequence defaultPathAndFile) Display an OS file open dialog to select a supported image file.static String[]
openDialogGets
(Bitmap.SaveFormat format, CharSequence title, CharSequence defaultPathAndFile) Display an OS file open dialog to select multiple supported image files.void
recycle()
Same asclose()
.boolean
saveDialog
(Bitmap.SaveFormat format, int quality, String name) Save this bitmap to specified path as specified format.static String
saveDialogGet
(Bitmap.SaveFormat format, CharSequence title, String name) Display an OS file save dialog to select the path to save the bitmap.void
saveToChannel
(Bitmap.SaveFormat format, int quality, WritableByteChannel channel) Save this bitmap to specified channel with specified format.void
saveToFile
(Bitmap.SaveFormat format, int quality, File file) Save this bitmap to specified path with specified format.void
saveToPath
(Bitmap.SaveFormat format, int quality, Path path) Save this bitmap to specified path with specified format.void
saveToStream
(Bitmap.SaveFormat format, int quality, OutputStream stream) Save this bitmap to specified stream with specified format.void
setColor4f
(int x, int y, float[] src) Sets the pixel value at the specified location.void
setColorSpace
(ColorSpace newColorSpace) Modifies the bitmap to have the specifiedColorSpace
, without affecting the underlying allocation backing the bitmap.void
setFormat
(Bitmap.Format format) Modifies the bitmap to have a specifiedBitmap.Format
, without affecting the underlying allocation backing the bitmap.void
Marks the Bitmap as immutable.void
setPixels
(float[] src, int offset, int stride, int dstX, int dstY, int width, int height) Copies a Rect of pixels from src.void
setPixels
(int[] src, int offset, int stride, int dstX, int dstY, int width, int height) Copies a Rect of pixels from src.void
Copies a Rect of pixels from src bitmap at (srcX, srcY) to this bitmap.void
setPremultiplied
(boolean premultiplied) Sets whether the bitmap should treat its data as premultiplied or not.toString()
static Bitmap
wrap
(long address, int rowBytes, LongConsumer freeFn, int width, int height, Bitmap.Format format, boolean isPremultiplied, ColorSpace colorSpace) Create a mutable bitmap by wrapping an existing address in an unsafe manner.
-
Field Details
-
MARKER
public static final org.apache.logging.log4j.Marker MARKER -
DATE_FORMAT
public static final com.ibm.icu.text.DateFormat DATE_FORMAT
-
-
Method Details
-
createBitmap
@NonNull public static Bitmap createBitmap(@Size(min=1L) int width, @Size(min=1L) int height, @NonNull Bitmap.Format format) Creates a mutable bitmap and its allocation, the content are initialized to zeros.The newly created bitmap will have non-premultiplied alpha if the format has an alpha channel. The newly created bitmap is in the
sRGB
color space, unless the format is alpha only, in which case the color space is null. You may change the format, alpha type, and color space after creating the bitmap.- Parameters:
width
- width in pixels, must be > 0height
- height in pixels, must be > 0format
- the number of channels and the bit depth- Throws:
IllegalArgumentException
- width or height out of rangeOutOfMemoryError
- out of native memory
-
createBitmap
@NonNull public static Bitmap createBitmap(@Size(min=1L) int width, @Size(min=1L) int height, @NonNull Bitmap.Format format, boolean isPremultiplied, @Nullable ColorSpace colorSpace) Creates a mutable bitmap and its allocation, the content are initialized to zeros.You may change the format, alpha type, and color space after creating the bitmap.
- Parameters:
width
- width in pixels, must be > 0height
- height in pixels, must be > 0format
- a color interpretationisPremultiplied
- an alpha interpretationcolorSpace
- a color space describing the pixels- Throws:
IllegalArgumentException
- width or height out of rangeOutOfMemoryError
- out of native memory
-
wrap
@Experimental @NonNull public static Bitmap wrap(long address, int rowBytes, @Nullable LongConsumer freeFn, @Size(min=1L) int width, @Size(min=1L) int height, @NonNull Bitmap.Format format, boolean isPremultiplied, @Nullable ColorSpace colorSpace) Create a mutable bitmap by wrapping an existing address in an unsafe manner.- Parameters:
address
- base addressrowBytes
- size of one row of buffer; width times bpp, or largerfreeFn
- free function for addresswidth
- width of pixelsheight
- height of pixelsformat
- a color interpretationisPremultiplied
- an alpha interpretationcolorSpace
- a color space describing the pixels- Returns:
- a bitmap
-
openDialogGet
@Nullable public static String openDialogGet(@Nullable Bitmap.SaveFormat format, @Nullable CharSequence title, @Nullable CharSequence defaultPathAndFile) Display an OS file open dialog to select a supported image file. The dialog will block the current thread until method return.- Parameters:
format
- the specified image file format to open, ornull
to use all supported formatstitle
- the dialog title ornull
to use OS defaultdefaultPathAndFile
- the default path and/or file ornull
to use OS default- Returns:
- the selected path or
null
if selects nothing (dismissed or closed)
-
openDialogGets
@Nullable public static String[] openDialogGets(@Nullable Bitmap.SaveFormat format, @Nullable CharSequence title, @Nullable CharSequence defaultPathAndFile) Display an OS file open dialog to select multiple supported image files. The dialog will block the current thread until method return.- Parameters:
format
- the specified image file format to open, ornull
to use all supported formatstitle
- the dialog title ornull
to use OS defaultdefaultPathAndFile
- the default path and/or file ornull
to use OS default- Returns:
- the selected paths or
null
if selects nothing (dismissed or closed)
-
saveDialogGet
@Nullable public static String saveDialogGet(@Nullable Bitmap.SaveFormat format, @Nullable CharSequence title, @Nullable String name) Display an OS file save dialog to select the path to save the bitmap. The dialog will block the current thread until method return.- Parameters:
format
- the specified image file format to filter, ornull
to filter all supported formatstitle
- the dialog title ornull
to use OS defaultname
- the file name without extension name- Returns:
- the path or
null
if selects nothing
-
getInfo
-
getWidth
public int getWidth()Returns the width of the bitmap. -
getHeight
public int getHeight()Returns the height of the bitmap. -
getSize
public long getSize()Returns the number of bytes that can be used to store this bitmap's pixels. -
getColorType
@Internal public int getColorType() -
getAlphaType
@Internal public int getAlphaType() -
getFormat
Returns the current pixel format used to interpret pixel data. The bitmap's format can be changed usingsetFormat(Format)
.- Returns:
- the pixel format used to interpret colors
-
setFormat
Modifies the bitmap to have a specifiedBitmap.Format
, without affecting the underlying allocation backing the bitmap. Bitmap pixel data is not re-initialized for the new format. By calling this method, further operations will reinterpret the colors using the specified color type.This method can be used to map a channel to another channel. For example,
Bitmap.Format.GRAY_8
,Bitmap.Format.ALPHA_8
,Bitmap.Format.R_8
have the same pixel layout and are interchangeable. If the bytes-per-pixel of new format and original format do not match, anIllegalArgumentException
will be thrown and the bitmap will not be modified.- Parameters:
format
- a new interpretation of existing pixel data- Throws:
IllegalArgumentException
- bpp of new format is not equal to bpp of original format
-
getChannels
public int getChannels()Returns the number of channels. -
hasAlpha
public boolean hasAlpha()Returns true if the bitmap's format supports per-pixel alpha, and if the pixels may contain non-opaque alpha values. For some formats, this is always false (e.g.Bitmap.Format.RGB_888
), since they do not support per-pixel alpha. However, for formats that do, the bitmap may be flagged to be known that all of its pixels are opaque. In this case hasAlpha() will also return false. If a format such asBitmap.Format.RGBA_8888
is not so flagged, it will return true by default. -
isPremultiplied
public boolean isPremultiplied()Indicates whether pixels stored in this bitmaps are stored premultiplied. When a pixel is premultiplied, the RGB components have been multiplied by the alpha component. For instance, if the original color is a 50% translucent red
(128, 255, 0, 0)
, the premultiplied form is(128, 128, 0, 0)
.This method only returns true if
hasAlpha()
returns true. A bitmap with no alpha channel can be used both as a premultiplied and as a non premultiplied bitmap.- Returns:
- true if the underlying pixels have been premultiplied, false otherwise
-
setPremultiplied
public void setPremultiplied(boolean premultiplied) Sets whether the bitmap should treat its data as premultiplied or not.By calling this method, further operations will reinterpret the colors using the specified alpha type, existing pixel data will remain unchanged.
If the current format does not have an alpha channel (
hasAlpha()
returns false), then the specified alpha type is recorded and will work in the future, seesetFormat(Format)
.- Parameters:
premultiplied
- whether premultiplied alpha type is requested
-
getColorSpace
Returns the color space associated with this bitmap. If the color space is unknown, this method returns null. -
setColorSpace
Modifies the bitmap to have the specified
ColorSpace
, without affecting the underlying allocation backing the bitmap.This affects how the framework will interpret the color at each pixel.
- Parameters:
newColorSpace
- to assign to the bitmap- Throws:
IllegalArgumentException
- If the specified color space is notRGB
, has a transfer function that is not anICC parametric curve
, or whose components min/max values reduce the numerical range compared to the previously assigned color space.
-
getAddress
@Internal public long getAddress()The address ofvoid *pixels
in native. The address is valid until bitmap closed.- Returns:
- the pointer of pixel data, or NULL if released
-
getRowStride
@Obsolete public int getRowStride()The distance, in bytes, between the start of one pixel row and the next, including any unused space between them.This method is obsolete in favor of
getRowBytes()
.- Returns:
- the scanline size in bytes
-
getRowBytes
public int getRowBytes()Return the distance, in bytes, between the start of one pixel row and the next, including any unused space between them.- Returns:
- number of bytes between rows of the native bitmap pixels
-
isImmutable
public boolean isImmutable()Returns true if the bitmap is marked as immutable. -
setImmutable
public void setImmutable()Marks the Bitmap as immutable. Further modifications to this Bitmap are disallowed. After this method is called, this Bitmap cannot be made mutable again. -
getPixelARGB
Returns theColor
at the specified location. Throws an exception if x or y are out of bounds (negative or >= to the width or height respectively). The returned color is a non-premultiplied ARGB value in thesRGB
color space, the format is the same asBitmap.Format.BGRA_8888_PACK32
.If the max bits per channel for the color type is greater than 8, or colors are premultiplied, then color precision may be lost in the conversion. Otherwise, precision will not be lost. If the color space is not sRGB, then this method will perform color space transformation, which can be slow.
Batch version of this operation:
getPixels(int[], int, int, int, int, int, int)
.- Parameters:
x
- The x coordinate (0...width-1) of the pixel to returny
- The y coordinate (0...height-1) of the pixel to return- Returns:
- The argb
Color
at the specified coordinate - Throws:
IllegalArgumentException
- if x, y exceed the bitmap's bounds- See Also:
-
getColor4f
Gets the pixel value at the specified location. Throws an exception if x or y are out of bounds (negative or >= to the width or height respectively). This method won't perform alpha type or color space conversion, then the result color is converted to RGBA float color, and its color space and alpha type remain unchanged (seegetColorSpace()
andisPremultiplied()
).Batch version of this operation:
getPixels(float[], int, int, int, int, int, int)
.- Parameters:
x
- The x coordinate (0...width-1) of the pixel to returny
- The y coordinate (0...height-1) of the pixel to returndst
- The result RGBA float color at the specified coordinate- Returns:
- the passed float array that contains r,g,b,a values
- Throws:
IllegalStateException
- the bitmap is releasedIllegalArgumentException
- if x, y exceed the bitmap's bounds- See Also:
-
setColor4f
Sets the pixel value at the specified location. The given float color will convert to bitmap's format. This method won't perform alpha type or color space conversion, then the given color must be in the bitmap's color spacegetColorSpace()
, its alpha type must be the same asisPremultiplied()
). This bitmap must be mutable, that is,isImmutable()
must return false.Batch version of this operation:
setPixels(float[], int, int, int, int, int, int)
.- Parameters:
x
- The x coordinate of the pixel to replace (0...width-1)y
- The y coordinate of the pixel to replace (0...height-1)src
- The RGBA float color to write into the bitmap- Throws:
IllegalStateException
- the bitmap is released or immutableIllegalArgumentException
- if x, y exceed the bitmap's bounds- See Also:
-
getPixels
public void getPixels(@NonNull @ColorInt int[] dst, int offset, int stride, int srcX, int srcY, int width, int height) Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not exceed (width, height). Each value is a packed int representing aColor
. The stride parameter allows the caller to allow for gaps in the returned pixels array between rows. For normal packed results, just pass width for the stride value. The returned colors are non-premultiplied ARGB values in thesRGB
color space, i.e. it hasBitmap.Format.BGRA_8888_PACK32
pixel format.If the max bits per channel of
getFormat()
is greater than 8, or colors are premultiplied, then color precision may be lost in the conversion. SeegetPixels(float[], int, int, int, int, int, int)
.- Parameters:
dst
- The destination array to receive the bitmap's colorsoffset
- The first index to write into dststride
- The number of entries in dst to skip between rows (must be >= width)srcX
- The srcX coordinate of the first pixel to read from the bitmapsrcY
- The srcY coordinate of the first pixel to read from the bitmapwidth
- The number of pixels to read from each rowheight
- The number of rows to read- Throws:
IllegalStateException
- the bitmap is releasedIllegalArgumentException
- if srcX, srcY, width, height exceed the bounds of the bitmap, or if stride invalid input: '<' widthArrayIndexOutOfBoundsException
- if the pixels array is too small to receive the specified number of pixels- See Also:
-
setPixels
public void setPixels(@NonNull @ColorInt int[] src, int offset, int stride, int dstX, int dstY, int width, int height) Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed (width, height). Each element in the array is a packed int representing a non-premultiplied ARGBColor
in thesRGB
color space, i.e. it hasBitmap.Format.BGRA_8888_PACK32
pixel format. The stride parameter allows the caller to allow for gaps in the source pixels array between rows. For normal packed pixels, just pass width for the stride value.- Parameters:
src
- The source colors to write to the bitmapoffset
- The index of the first color to read from srcstride
- The number of colors in src to skip between rows Normally this value will be the same as the width of the bitmap, but it can be largerdstX
- The x coordinate of the first pixel to write to in the bitmap.dstY
- The y coordinate of the first pixel to write to in the bitmap.width
- The number of colors to copy from src per rowheight
- The number of rows to write to the bitmap- See Also:
-
getPixels
public void getPixels(@NonNull @Size(multiple=4L) float[] dst, int offset, int stride, int srcX, int srcY, int width, int height) Copies a Rect of pixels to dst. Copy starts at (srcX, srcY), and does not exceed (width, height). The returned colors are (R,G,B,A) float color quadruples in the source color spacegetColorSpace()
, in the source alpha typeisPremultiplied()
, inBitmap.Format.RGBA_F32
pixel format. The stride parameter allows the caller to allow for gaps in the returned pixels array between rows. For normal packed results, just pass width for the stride value.Throws
ArrayIndexOutOfBoundsException
if(offset + (height - 1) * stride + width) * 4 > dst.length
.- Parameters:
dst
- The destination array to receive the bitmap's colorsoffset
- The first color to write into dststride
- The number of colors in dst to skip between rows (must be >= width)srcX
- The srcX coordinate of the first pixel to read from the bitmapsrcY
- The srcY coordinate of the first pixel to read from the bitmapwidth
- The number of pixels to read from each rowheight
- The number of rows to read- Throws:
IllegalStateException
- the bitmap is releasedIllegalArgumentException
- if srcX, srcY, width, height exceed the bounds of the bitmap, or if stride invalid input: '<' widthArrayIndexOutOfBoundsException
- if the pixels array is too small to receive the specified number of pixels- See Also:
-
setPixels
public void setPixels(@NonNull @Size(multiple=4L) float[] src, int offset, int stride, int dstX, int dstY, int width, int height) Copies a Rect of pixels from src. Copy starts at (dstX, dstY), and does not exceed (width, height). Each element in the array is a quadruple that contains (R,G,B,A) float color components source color spacegetColorSpace()
, in the source alpha typeisPremultiplied()
, inBitmap.Format.RGBA_F32
pixel format. The stride parameter allows the caller to allow for gaps in the source pixels array between rows. For normal packed pixels, just pass width for the stride value.Throws
ArrayIndexOutOfBoundsException
if(offset + (height - 1) * stride + width) * 4 > dst.length
.- Parameters:
src
- The source colors to write to the bitmapoffset
- The index of the first color to read from srcstride
- The number of colors in src to skip between rows Normally this value will be the same as the width of the bitmap, but it can be largerdstX
- The x coordinate of the first pixel to write to in the bitmap.dstY
- The y coordinate of the first pixel to write to in the bitmap.width
- The number of colors to copy from src per rowheight
- The number of rows to write to the bitmap- See Also:
-
getPixels
public void getPixels(@NonNull Bitmap dst, int dstX, int dstY, int srcX, int srcY, int width, int height) Copies a Rect of pixels from this bitmap to dst bitmap at (dstX, dstY). Copy starts at (srcX, srcY), and does not exceed (width, height). This method will perform pixel format and alpha type conversion (seegetFormat()
andisPremultiplied()
), and color space transformation (seegetColorSpace()
).The behavior is undefined if there is memory overlap between this bitmap and the destination bitmap.
- Parameters:
dst
- The destination bitmap to receive this bitmap's colorsdstX
- The dstX coordinate of the first pixel to write to the bitmapdstY
- The dstY coordinate of the first pixel to write to the bitmapsrcX
- The srcX coordinate of the first pixel to read from the bitmapsrcY
- The srcY coordinate of the first pixel to read from the bitmapwidth
- The number of pixels to read from each rowheight
- The number of rows to read- Throws:
IllegalStateException
- the bitmap is released or immutableIllegalArgumentException
- if srcX, srcY, dstX, dstY, width, height exceed the bounds of the bitmap- See Also:
-
setPixels
public void setPixels(@NonNull Bitmap src, int srcX, int srcY, int dstX, int dstY, int width, int height) Copies a Rect of pixels from src bitmap at (srcX, srcY) to this bitmap. Copy starts at (dstX, dstY), and does not exceed (width, height). This method will perform pixel format and alpha type conversion (seegetFormat()
andisPremultiplied()
), and color space transformation (seegetColorSpace()
).The behavior is undefined if there is memory overlap between this bitmap and the source bitmap.
- Parameters:
src
- The source bitmap to write to this bitmapsrcX
- The srcX coordinate of the first pixel to read from the bitmapsrcY
- The srcY coordinate of the first pixel to read from the bitmapdstX
- The dstX coordinate of the first pixel to write to the bitmapdstY
- The dstY coordinate of the first pixel to write to the bitmapwidth
- The number of pixels to read from each rowheight
- The number of rows to read- Throws:
IllegalStateException
- the bitmap is released or immutableIllegalArgumentException
- if srcX, srcY, dstX, dstY, width, height exceed the bounds of the bitmap- See Also:
-
copyPixelsToBuffer
public boolean copyPixelsToBuffer(@NonNull Buffer dst, int rowBytes, int srcX, int srcY, int width, int height) Copy the bitmap's pixels into the specified buffer. The buffer can be a heap buffer, a direct buffer, or a native buffer (by wrapping an address). Buffer's base array, address and position will be used, other properties likeBuffer.limit()
are ignored, this method always assumes the byte order is the host endianness, i.e.ByteOrder.nativeOrder()
, and reinterpret the data to buffer's basic type.This method does not check for invalid, out of bounds, or misaligned addresses. If the memory is managed by Buffer (non-wrapped direct buffer), it will be guaranteed to be alive. Note
Buffer.isReadOnly()
must return false.The content of the bitmap is copied into the buffer as-is. This means that if this bitmap has premultiplied colors (see
isPremultiplied()
), the values in the buffer will also be premultiplied. The pixels remain in theBitmap.Format
and color space of the bitmap.After this method returns, the current position of the buffer remains unchanged.
This method may return false if dst is nullptr or address it not aligned to the bytes-per-pixel or the size of basic data type (according to
Bitmap.Format
). May return false if rowBytes is less than the minRowBytes (width * bpp).- Returns:
- true if pixels are copied to dst
- Throws:
IllegalStateException
- the bitmap is releasedIllegalArgumentException
- if srcX, srcY, width, height exceed the bounds of the bitmap- See Also:
-
copyPixelsFromBuffer
public boolean copyPixelsFromBuffer(@NonNull Buffer src, int rowBytes, int dstX, int dstY, int width, int height) Copy the pixels from the specified buffer into bitmap. The buffer can be a heap buffer, a direct buffer, or a native buffer (by wrapping an address). Buffer's base array, address and position will be used, other properties likeBuffer.limit()
are ignored, this method always assumes the byte order is the host endianness, i.e.ByteOrder.nativeOrder()
, and reinterpret the data from buffer's basic type.This method does not check for invalid, out of bounds, or misaligned addresses. If the memory is managed by Buffer (non-wrapped direct buffer), it will be guaranteed to be alive. Note
Buffer.isReadOnly()
must return false.The content of the buffer is copied into the bitmap as-is. This means that if this bitmap has premultiplied colors (see
isPremultiplied()
), the values in the buffer should also be premultiplied. The pixels remain in theBitmap.Format
and color space of the bitmap.After this method returns, the current position of the buffer remains unchanged.
This method may return false if dst is nullptr or address it not aligned to the bytes-per-pixel or the size of basic data type (according to
Bitmap.Format
). May return false if rowBytes is less than the minRowBytes (width * bpp).- Returns:
- true if src pixels are copied to bitmap
- Throws:
IllegalStateException
- the bitmap is releasedIllegalArgumentException
- if dstX, dstY, width, height exceed the bounds of the bitmap- See Also:
-
clear
Fills the bitmap's pixels with the specified color and rectangular area. The given float color is in the source color spacegetColorSpace()
, in the source alpha typeisPremultiplied()
, inBitmap.Format.RGBA_F32
pixel format. That is, this method will only perform pixel format conversion, and will not perform alpha type or color space transformation.- Parameters:
color
- the (R,G,B,A) color to fillarea
- bounding box of pixels to fill; null means full bitmap- Returns:
- true if pixels are changed
- Throws:
IllegalStateException
- the bitmap is released or immutable
-
getPixmap
The current pixel map, which is usually paired withPixels
. This method is UNSAFE, use with caution!- Returns:
- the current pixel map
-
getPixels
The ref of current pixel data, which may be shared across instances. Calling this method won't affect the ref cnt. Every bitmap object ref thePixels
on create, and unref onclose()
.This method is UNSAFE, use with caution!
- Returns:
- the ref of pixel data, or null if released
-
saveDialog
@WorkerThread public boolean saveDialog(@NonNull Bitmap.SaveFormat format, int quality, @Nullable String name) throws IOException Save this bitmap to specified path as specified format. This will open a save dialog to select the path, block the current thread until the encoding is done.- Parameters:
format
- the format of the saved imagequality
- the compress quality, 0-100, only work for JPEG format.name
- the file name without extension name- Returns:
- true if selected a path, otherwise canceled
- Throws:
IOException
- selected a path, but saving is not successfulIllegalArgumentException
- bad argumentsIllegalStateException
- failed to process
-
saveToFile
@WorkerThread public void saveToFile(@NonNull Bitmap.SaveFormat format, int quality, @NonNull File file) throws IOException Save this bitmap to specified path with specified format.- Parameters:
format
- the format of the saved imagequality
- the compress quality, 0-100, only work for JPEG format.file
- the image file- Throws:
IOException
- saving is not successfulIllegalArgumentException
- bad argumentsIllegalStateException
- failed to process
-
saveToPath
@WorkerThread public void saveToPath(@NonNull Bitmap.SaveFormat format, int quality, @NonNull Path path) throws IOException Save this bitmap to specified path with specified format.- Parameters:
format
- the format of the saved imagequality
- the compress quality, 0-100, only work for JPEG format.path
- the image path- Throws:
IOException
- saving is not successfulIllegalArgumentException
- bad argumentsIllegalStateException
- failed to process
-
saveToStream
@WorkerThread public void saveToStream(@NonNull Bitmap.SaveFormat format, int quality, @NonNull OutputStream stream) throws IOException Save this bitmap to specified stream with specified format. The stream will NOT be closed by this method.- Parameters:
format
- the format of the saved imagequality
- the compress quality, 0-100, only work for JPEG formatstream
- the stream to write image data- Throws:
IOException
- saving is not successfulIllegalArgumentException
- bad argumentsIllegalStateException
- failed to process
-
saveToChannel
@WorkerThread public void saveToChannel(@NonNull Bitmap.SaveFormat format, int quality, @NonNull WritableByteChannel channel) throws IOException Save this bitmap to specified channel with specified format. The channel will NOT be closed by this method.- Parameters:
format
- the format of the saved imagequality
- the compress quality, 0-100, only work for JPEG formatchannel
- the channel to write image data- Throws:
IOException
- saving is not successfulIllegalArgumentException
- bad argumentsIllegalStateException
- failed to process
-
close
public void close()Clear the reference to the pixel data. The bitmap is marked as "dead", and then it is an error to try to access its pixels.The system can ensure that the native allocation of a Bitmap to be released when the Bitmap object becomes phantom-reachable. However, it tends to take a very long time to perform automatic cleanup.
- Specified by:
close
in interfaceAutoCloseable
-
recycle
public void recycle()Same asclose()
. -
isClosed
public boolean isClosed()Returns true if this bitmap has been closed. If so, then it is an error to try to access its pixels.- Returns:
- true if the bitmap has been closed
-
isRecycled
public boolean isRecycled()- Returns:
- the same as
isClosed()
-
toString
-