BitmapPixelTestUtil


@UnstableApi
public class BitmapPixelTestUtil


Utilities for pixel tests.

Summary

Nested types

Represents a ByteBuffer read from an Image.

Constants

static final float

Maximum allowed average pixel difference between bitmaps generated.

static final float

Maximum allowed average pixel difference between bitmaps generated using devices.

static final float

Maximum allowed average pixel difference between bitmaps with 16-bit primaries generated using devices.

static final float

Maximum allowed average pixel difference between bitmaps generated from luma values.

Public methods

static BitmapPixelTestUtil.ImageBuffer

Returns the ImageBuffer that is copied from the Image's internal buffer.

static void

Copies image data from the specified Bitmap into the Image, which must be an RGBA_8888 image.

static Bitmap

Creates a ARGB_8888 bitmap with the values of the focused OpenGL framebuffer.

static Bitmap

Returns a bitmap with the same information as the provided alpha/red/green/blue 8-bits per component image.

static Bitmap
static Bitmap
createArgb8888BitmapWithSolidColor(int width, int height, int color)

Returns a solid Bitmap with every pixel having the same color.

static Bitmap
@RequiresApi(value = 26)
createFp16BitmapFromFocusedGlFramebuffer(int width, int height)

Creates a RGBA_F16 bitmap with the values of the focused OpenGL framebuffer.

static int

Creates a 2-dimensional OpenGL texture with the bitmap's contents.

static Bitmap
createGrayscaleBitmapFromYuv420888Image(
    Image image,
    Bitmap.Config bitmapConfig
)

Returns a grayscale bitmap from the Luma channel in the YUV_420_888 image.

static Bitmap
createUnpremultipliedArgb8888BitmapFromFocusedGlFramebuffer(
    int width,
    int height
)

Creates a ARGB_8888 bitmap with the values of the focused OpenGL framebuffer.

static Bitmap
static float
getBitmapAveragePixelAbsoluteDifferenceArgb8888(
    Bitmap expected,
    Bitmap actual,
    @Nullable String testId
)

Returns the average difference between the expected and actual bitmaps, calculated using the maximum difference across all color channels for each pixel, then divided by the total number of pixels in the image, without saving the difference bitmap.

static float
getBitmapAveragePixelAbsoluteDifferenceArgb8888(
    Bitmap expected,
    Bitmap actual,
    @Nullable String testId,
    @Nullable String differencesBitmapPath
)

Returns the average difference between the expected and actual bitmaps.

static float
@RequiresApi(value = 29)
getBitmapAveragePixelAbsoluteDifferenceFp16(
    Bitmap expected,
    Bitmap actual
)

Returns the average difference between the expected and actual bitmaps.

static void
maybeSaveTestBitmap(
    String testId,
    String bitmapLabel,
    Bitmap bitmap,
    @Nullable String path
)

Tries to save the Bitmap as a PNG to the <path>, and if not provided, tries to save to the cache directory.

static Bitmap
readBitmap(String assetString)

Reads a bitmap from the specified asset location.

static Bitmap

Reads a bitmap with unpremultiplied alpha from the specified asset location.

Constants

MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE

public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE

Maximum allowed average pixel difference between bitmaps generated.

This value is for for 8-bit primaries in pixel difference-based tests.

The value is chosen so that differences in decoder behavior across devices don't affect whether the test passes, but substantial distortions introduced by changes in tested components will cause the test to fail.

When the difference is close to the threshold, manually inspect expected/actual bitmaps to confirm failure, as it's possible this is caused by a difference in the codec or graphics implementation as opposed to an issue in the tested component.

The value is the same as MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_DIFFERENT_DEVICE if running on physical devices.

MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_DIFFERENT_DEVICE

public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_DIFFERENT_DEVICE

Maximum allowed average pixel difference between bitmaps generated using devices.

This value is for for 8-bit primaries in pixel difference-based tests.

The value is chosen so that differences in decoder behavior across devices don't affect whether the test passes, but substantial distortions introduced by changes in tested components will cause the test to fail.

When the difference is close to the threshold, manually inspect expected/actual bitmaps to confirm failure, as it's possible this is caused by a difference in the codec or graphics implementation as opposed to an issue in the tested component.

This value is larger than MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE to support the larger variance in decoder outputs between different physical devices and emulators.

MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_DIFFERENT_DEVICE_FP16

public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_DIFFERENT_DEVICE_FP16 = 0.01f

Maximum allowed average pixel difference between bitmaps with 16-bit primaries generated using devices.

The value is chosen so that differences in decoder behavior across devices in pixel difference-based tests don't affect whether the test passes, but substantial distortions introduced by changes in tested components will cause the test to fail.

When the difference is close to the threshold, manually inspect expected/actual bitmaps to confirm failure, as it's possible this is caused by a difference in the codec or graphics implementation as opposed to an issue in the tested component.

This value is larger than MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE to support the larger variance in decoder outputs between different physical devices and emulators.

MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_LUMA

public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE_LUMA = 8.0f

Maximum allowed average pixel difference between bitmaps generated from luma values.

Public methods

copyByteBufferFromRbga8888Image

public static BitmapPixelTestUtil.ImageBuffer copyByteBufferFromRbga8888Image(Image image)

Returns the ImageBuffer that is copied from the Image's internal buffer.

copyRbga8888BitmapToImage

public static void copyRbga8888BitmapToImage(Bitmap bitmap, Image image)

Copies image data from the specified Bitmap into the Image, which must be an RGBA_8888 image.

createArgb8888BitmapFromFocusedGlFramebuffer

public static Bitmap createArgb8888BitmapFromFocusedGlFramebuffer(int width, int height)

Creates a ARGB_8888 bitmap with the values of the focused OpenGL framebuffer.

This method may block until any previously called OpenGL commands are complete.

This method incorrectly marks the output Bitmap as premultiplied, even though OpenGL typically outputs only non-premultiplied alpha. Use createUnpremultipliedArgb8888BitmapFromFocusedGlFramebuffer to properly handle alpha.

Parameters
int width

The width of the pixel rectangle to read.

int height

The height of the pixel rectangle to read.

Returns
Bitmap

A Bitmap with the framebuffer's values.

createArgb8888BitmapFromRgba8888Image

public static Bitmap createArgb8888BitmapFromRgba8888Image(Image image)

Returns a bitmap with the same information as the provided alpha/red/green/blue 8-bits per component image.

createArgb8888BitmapFromRgba8888ImageBuffer

public static Bitmap createArgb8888BitmapFromRgba8888ImageBuffer(
    BitmapPixelTestUtil.ImageBuffer imageBuffer
)

createArgb8888BitmapWithSolidColor

public static Bitmap createArgb8888BitmapWithSolidColor(int width, int height, int color)

Returns a solid Bitmap with every pixel having the same color.

Parameters
int width

The width of image to create, in pixels.

int height

The height of image to create, in pixels.

int color

An RGBA color created by Color.

createFp16BitmapFromFocusedGlFramebuffer

@RequiresApi(value = 26)
public static Bitmap createFp16BitmapFromFocusedGlFramebuffer(int width, int height)

Creates a RGBA_F16 bitmap with the values of the focused OpenGL framebuffer.

This method may block until any previously called OpenGL commands are complete.

This method incorrectly marks the output Bitmap as premultiplied, even though OpenGL typically outputs only non-premultiplied alpha. Call setPremultiplied with false on the output bitmap to properly handle alpha.

Parameters
int width

The width of the pixel rectangle to read.

int height

The height of the pixel rectangle to read.

Returns
Bitmap

A Bitmap with the framebuffer's values.

createGlTextureFromBitmap

public static int createGlTextureFromBitmap(Bitmap bitmap)

Creates a 2-dimensional OpenGL texture with the bitmap's contents.

Parameters
Bitmap bitmap

A Bitmap.

Returns
int

The identifier of the newly created texture.

createGrayscaleBitmapFromYuv420888Image

public static Bitmap createGrayscaleBitmapFromYuv420888Image(
    Image image,
    Bitmap.Config bitmapConfig
)

Returns a grayscale bitmap from the Luma channel in the YUV_420_888 image.

createUnpremultipliedArgb8888BitmapFromFocusedGlFramebuffer

public static Bitmap createUnpremultipliedArgb8888BitmapFromFocusedGlFramebuffer(
    int width,
    int height
)

Creates a ARGB_8888 bitmap with the values of the focused OpenGL framebuffer.

This method may block until any previously called OpenGL commands are complete.

Parameters
int width

The width of the pixel rectangle to read.

int height

The height of the pixel rectangle to read.

Returns
Bitmap

A Bitmap with the framebuffer's values.

flipBitmapVertically

public static Bitmap flipBitmapVertically(Bitmap bitmap)

getBitmapAveragePixelAbsoluteDifferenceArgb8888

public static float getBitmapAveragePixelAbsoluteDifferenceArgb8888(
    Bitmap expected,
    Bitmap actual,
    @Nullable String testId
)

Returns the average difference between the expected and actual bitmaps, calculated using the maximum difference across all color channels for each pixel, then divided by the total number of pixels in the image, without saving the difference bitmap. See getBitmapAveragePixelAbsoluteDifferenceArgb8888.

This method is the overloaded version of getBitmapAveragePixelAbsoluteDifferenceArgb8888 without a specified saved path.

getBitmapAveragePixelAbsoluteDifferenceArgb8888

public static float getBitmapAveragePixelAbsoluteDifferenceArgb8888(
    Bitmap expected,
    Bitmap actual,
    @Nullable String testId,
    @Nullable String differencesBitmapPath
)

Returns the average difference between the expected and actual bitmaps.

Calculated using the maximum difference across all color channels for each pixel, then divided by the total number of pixels in the image. Bitmap resolutions must match and must use configuration ARGB_8888.

Tries to save a difference bitmap between expected and actual bitmaps.

Parameters
Bitmap expected

The expected Bitmap.

Bitmap actual

The actual Bitmap produced by the test.

@Nullable String testId

The name of the test that produced the Bitmap, or null if the differences bitmap should not be saved to cache.

@Nullable String differencesBitmapPath

Folder path for the produced pixel-wise difference Bitmap to be saved in or null if the assumed default save path should be used.

Returns
float

The average of the maximum absolute pixel-wise differences between the expected and actual bitmaps.

getBitmapAveragePixelAbsoluteDifferenceFp16

@RequiresApi(value = 29)
public static float getBitmapAveragePixelAbsoluteDifferenceFp16(
    Bitmap expected,
    Bitmap actual
)

Returns the average difference between the expected and actual bitmaps.

Calculated using the maximum difference across all color channels for each pixel, then divided by the total number of pixels in the image. Bitmap resolutions must match and must use configuration RGBA_F16.

Parameters
Bitmap expected

The expected Bitmap.

Bitmap actual

The actual Bitmap produced by the test.

Returns
float

The average of the maximum absolute pixel-wise differences between the expected and actual bitmaps.

maybeSaveTestBitmap

public static void maybeSaveTestBitmap(
    String testId,
    String bitmapLabel,
    Bitmap bitmap,
    @Nullable String path
)

Tries to save the Bitmap as a PNG to the <path>, and if not provided, tries to save to the cache directory.

File name will be <testId>_<bitmapLabel>.png. If the file failed to write, any IOException will be caught and logged. The path will be logged regardless of success.

Parameters
String testId

Name of the test that produced the Bitmap.

String bitmapLabel

Label to identify the bitmap.

Bitmap bitmap

The Bitmap to save.

@Nullable String path

Folder path for the supplied Bitmap to be saved in or null if the cache directory should be saved in.

readBitmap

public static Bitmap readBitmap(String assetString)

Reads a bitmap from the specified asset location.

Parameters
String assetString

Relative path to the asset within the assets directory.

Returns
Bitmap

A Bitmap.

Throws
java.io.IOException

If the bitmap can't be read.

readBitmapUnpremultipliedAlpha

public static Bitmap readBitmapUnpremultipliedAlpha(String assetString)

Reads a bitmap with unpremultiplied alpha from the specified asset location.

Parameters
String assetString

Relative path to the asset within the assets directory.

Returns
Bitmap

A Bitmap.

Throws
java.io.IOException

If the bitmap can't be read.