Class BitmapPixelTestUtil


  • public class BitmapPixelTestUtil
    extends Object
    Utilities for pixel tests.
    • Field Detail

      • MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE

        public static final float MAXIMUM_AVERAGE_PIXEL_ABSOLUTE_DIFFERENCE
        Maximum allowed average pixel difference between bitmaps generated using emulators.

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

        The value is chosen so that differences in decoder behavior across emulator versions 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.

        See Also:
        Constant Field Values
      • 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
        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.

        See Also:
        Constant Field Values
    • Method Detail

      • readBitmap

        public static Bitmap readBitmap​(String assetString)
                                 throws IOException
        Reads a bitmap from the specified asset location.
        Parameters:
        assetString - Relative path to the asset within the assets directory.
        Returns:
        A Bitmap.
        Throws:
        IOException - If the bitmap can't be read.
      • createArgb8888BitmapFromRgba8888Image

        @RequiresApi(19)
        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.
      • createArgb8888BitmapWithSolidColor

        public static Bitmap createArgb8888BitmapWithSolidColor​(int width,
                                                                int height,
                                                                int color)
        Returns a solid Bitmap with every pixel having the same color.
        Parameters:
        width - The width of image to create, in pixels.
        height - The height of image to create, in pixels.
        color - An RGBA color created by Color.
      • 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 Bitmap.Config.ARGB_8888.

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

        Parameters:
        expected - The expected Bitmap.
        actual - The actual Bitmap produced by the test.
        testId - The name of the test that produced the Bitmap, or null if the differences bitmap should not be saved to cache.
        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:
        The average of the maximum absolute pixel-wise differences between the expected and actual bitmaps.
      • getBitmapAveragePixelAbsoluteDifferenceFp16

        @RequiresApi(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 Bitmap.Config.RGBA_F16.

        Parameters:
        expected - The expected Bitmap.
        actual - The actual Bitmap produced by the test.
        Returns:
        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.

        Parameters:
        testId - Name of the test that produced the Bitmap.
        bitmapLabel - Label to identify the bitmap.
        bitmap - The Bitmap to save.
        path - Folder path for the supplied Bitmap to be saved in or null if the cache directory should be saved in.
      • createArgb8888BitmapFromCurrentGlFramebuffer

        public static Bitmap createArgb8888BitmapFromCurrentGlFramebuffer​(int width,
                                                                          int height)
                                                                   throws GlUtil.GlException
        Creates a Bitmap.Config.ARGB_8888 bitmap with the values of the current OpenGL framebuffer.

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

        Parameters:
        width - The width of the pixel rectangle to read.
        height - The height of the pixel rectangle to read.
        Returns:
        A Bitmap with the framebuffer's values.
        Throws:
        GlUtil.GlException
      • createFp16BitmapFromCurrentGlFramebuffer

        @RequiresApi(26)
        public static Bitmap createFp16BitmapFromCurrentGlFramebuffer​(int width,
                                                                      int height)
                                                               throws GlUtil.GlException
        Creates a Bitmap.Config.RGBA_F16 bitmap with the values of the current OpenGL framebuffer.

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

        Parameters:
        width - The width of the pixel rectangle to read.
        height - The height of the pixel rectangle to read.
        Returns:
        A Bitmap with the framebuffer's values.
        Throws:
        GlUtil.GlException
      • flipBitmapVertically

        public static Bitmap flipBitmapVertically​(Bitmap bitmap)