Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 332076ce authored by wilsonshih's avatar wilsonshih
Browse files

Crop extra width from memory alignment padding

The ImageReader can allocate wider memory to add padding bytes to each
row for alignment purposes. When taking all the bytes from the buffer
using copyPixelsFromBuffer, these padding bytes are also included.

To exclude the padding,create a final Bitmap by cropping the temporary
one to the correct dimensions.

Bug: 438072503
Flag: EXEMPT bugfix
Test: Pull the captured snapshot from the device and verify that the
snapshot image does not contain any black bars on the right side.

Change-Id: Iff9833565c470537f3af0d0f3b87ae522169c370
parent 2c325801
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -171,14 +171,24 @@ public class TaskSnapshotConvertUtil {
                final Image.Plane plane = planes[0];
                final int rowPadding = plane.getRowStride() - plane.getPixelStride()
                        * image.getWidth();
                final int widthPadding = rowPadding / plane.getPixelStride();
                final Bitmap swBitmap = Bitmap.createBitmap(
                        image.getWidth() + rowPadding / plane.getPixelStride() /* width */,
                        image.getWidth() + widthPadding /* width */,
                        image.getHeight() /* height */,
                        pixelFormat == PixelFormat.RGB_565
                                ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888);
                swBitmap.copyPixelsFromBuffer(plane.getBuffer());
                if (widthPadding == 0) {
                    return swBitmap;
                }
                // Crop the full memory width of the image data (rowStride), which often includes
                // extra padding bytes on the side for memory alignment.
                final Bitmap finalBitmap = Bitmap.createBitmap(swBitmap, 0 /* x */, 0 /* y */,
                        width, // Crop to the required width of the image.
                        height);
                swBitmap.recycle();
                return finalBitmap;
            }
        }
    }
}