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

Commit d2391faa authored by Wei-Ta Chen's avatar Wei-Ta Chen
Browse files

Modify the API of YuvImage, so that callers do not need to deal with

the yuv format when specifying a rectangle region.

Bug: 2285598
parent ca35953c
Loading
Loading
Loading
Loading
+24 −21
Original line number Diff line number Diff line
@@ -66274,6 +66274,10 @@
</parameter>
<parameter name="format" type="int">
</parameter>
<parameter name="width" type="int">
</parameter>
<parameter name="height" type="int">
</parameter>
<parameter name="strides" type="int[]">
</parameter>
</constructor>
@@ -66287,17 +66291,24 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="width" type="int">
</parameter>
<parameter name="height" type="int">
</parameter>
<parameter name="offsets" type="int[]">
<parameter name="rectangle" type="android.graphics.Rect">
</parameter>
<parameter name="quality" type="int">
</parameter>
<parameter name="stream" type="java.io.OutputStream">
</parameter>
</method>
<method name="getHeight"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getStrides"
 return="int[]"
 abstract="false"
@@ -66309,8 +66320,8 @@
 visibility="public"
>
</method>
<method name="getYuvData"
 return="byte[]"
<method name="getWidth"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
@@ -66320,8 +66331,8 @@
 visibility="public"
>
</method>
<method name="getYuvFormat"
 return="int"
<method name="getYuvData"
 return="byte[]"
 abstract="false"
 native="false"
 synchronized="false"
@@ -66331,24 +66342,16 @@
 visibility="public"
>
</method>
<method name="validate"
 return="boolean"
<method name="getYuvFormat"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="protected"
 visibility="public"
>
<parameter name="format" type="int">
</parameter>
<parameter name="width" type="int">
</parameter>
<parameter name="height" type="int">
</parameter>
<parameter name="offsets" type="int[]">
</parameter>
</method>
</class>
</package>
@@ -72580,7 +72583,7 @@
 type="float"
 transient="false"
 volatile="false"
 value="0.0010f"
 value="0.001f"
 static="true"
 final="true"
 deprecated="not deprecated"
+125 −54
Original line number Diff line number Diff line
@@ -21,13 +21,11 @@ import java.io.OutputStream;
/**
 * YuvImage contains YUV data and provides a method that compresses a region of
 * the YUV data to a Jpeg. The YUV data should be provided as a single byte
 * array irrespective of the number of image planes in it. The stride of each
 * image plane should be provided as well.
 * array irrespective of the number of image planes in it.
 * Currently only PixelFormat.YCbCr_420_SP and PixelFormat.YCbCr_422_I are supported.
 *
 * To compress a rectangle region in the YUV data, users have to specify a
 * region by width, height and offsets, where each image plane has a
 * corresponding offset. All offsets are measured as a displacement in bytes
 * from yuv[0], where yuv[0] is the beginning of the yuv data.
 * To compress a rectangle region in the YUV data, users have to specify the
 * region by left, top, width and height.
 */
public class YuvImage {

@@ -54,22 +52,57 @@ public class YuvImage {
     */
    private int[] mStrides;

    /**
     * The width of the image.
     */
    private int mWidth;

    /**
     * The height of the the image.
     */
    private int mHeight;

    /**
     * Construct an YuvImage.
     *
     * @param yuv     The YUV data. In the case of more than one image plane, all the planes must be
     *                concatenated into a single byte array.
     * @param format  The YUV data format as defined in {@link PixelFormat}.
     * @param strides Row bytes of each image plane.
     * @param width   The width of the YuvImage.
     * @param height  The height of the YuvImage.
     * @param strides (Optional) Row bytes of each image plane. If yuv contains padding, the stride
     *                of each image must be provided. If strides is null, the method assumes no
     *                padding and derives the row bytes by format and width itself.
     * @throws IllegalArgumentException if format is not support; width or height <= 0; or yuv is
     *                null.
     */
    public YuvImage(byte[] yuv, int format, int[] strides) {
        if ((yuv == null) || (strides == null)) {
    public YuvImage(byte[] yuv, int format, int width, int height, int[] strides) {
        if (format != PixelFormat.YCbCr_420_SP &&
                format != PixelFormat.YCbCr_422_I) {
            throw new IllegalArgumentException(
                    "only support PixelFormat.YCbCr_420_SP " +
                    "and PixelFormat.YCbCr_422_I for now");
        }

        if (width <= 0  || height <= 0) {
            throw new IllegalArgumentException(
                    "yuv or strides cannot be null");
                    "width and height must large than 0");
        }

        if (yuv == null) {
            throw new IllegalArgumentException("yuv cannot be null");
        }

        if (strides == null) {
            mStrides = calculateStrides(width, format);
        } else {
            mStrides = strides;
        }

        mData = yuv;
        mFormat = format;
        mStrides = strides;
        mWidth = width;
        mHeight = height;
    }

    /**
@@ -77,22 +110,21 @@ public class YuvImage {
     * Only PixelFormat.YCbCr_420_SP and PixelFormat.YCbCr_422_I
     * are supported for now.
     *
     * @param width The width of the rectangle region.
     * @param height The height of the rectangle region.
     * @param offsets The offsets of the rectangle region in each image plane.
     *                The offsets are measured as a displacement in bytes from
     *                yuv[0], where yuv[0] is the beginning of the yuv data.
     * @param rectangle The rectangle region to be compressed. The medthod checks if rectangle is
     *                  inside the image. Also, the method modifies rectangle if the chroma pixels
     *                  in it are not matched with the luma pixels in it.
     * @param quality   Hint to the compressor, 0-100. 0 meaning compress for
     *                  small size, 100 meaning compress for max quality.
     * @param stream   The outputstream to write the compressed data.
     *
     * @return true if successfully compressed to the specified stream.
     *
     * @param stream    OutputStream to write the compressed data.
     * @return          True if the compression is successful.
     * @throws IllegalArgumentException if rectangle is invalid; quality is not within [0,
     *                  100]; or stream is null.
     */
    public boolean compressToJpeg(int width, int height, int[] offsets, int quality,
            OutputStream stream) {
        if (!validate(mFormat, width, height, offsets)) {
            return false;
    public boolean compressToJpeg(Rect rectangle, int quality, OutputStream stream) {
        Rect wholeImage = new Rect(0, 0, mWidth, mHeight);
        if (!wholeImage.contains(rectangle)) {
            throw new IllegalArgumentException(
                    "rectangle is not inside the image");
        }

        if (quality < 0 || quality > 100) {
@@ -100,13 +132,18 @@ public class YuvImage {
        }

        if (stream == null) {
            throw new NullPointerException();
            throw new IllegalArgumentException("stream cannot be null");
        }

        return nativeCompressToJpeg(mData, mFormat, width, height, offsets,
                mStrides, quality, stream, new byte[WORKING_COMPRESS_STORAGE]);
        adjustRectangle(rectangle);
        int[] offsets = calculateOffsets(rectangle.left, rectangle.top);

        return nativeCompressToJpeg(mData, mFormat, rectangle.width(),
                rectangle.height(), offsets, mStrides, quality, stream,
                new byte[WORKING_COMPRESS_STORAGE]);
    }


   /**
     * @return the YUV data.
     */
@@ -128,37 +165,71 @@ public class YuvImage {
        return mStrides;
    }

    protected boolean validate(int format, int width, int height, int[] offsets) {
        if (format != PixelFormat.YCbCr_420_SP &&
                format != PixelFormat.YCbCr_422_I) {
            throw new IllegalArgumentException(
                    "only support PixelFormat.YCbCr_420_SP " +
                    "and PixelFormat.YCbCr_422_I for now");
    /**
     * @return the width of the image.
     */
    public int getWidth() {
        return mWidth;
    }

        if (offsets.length != mStrides.length) {
            throw new IllegalArgumentException(
                    "the number of image planes are mismatched");
    /**
     * @return the height of the image.
     */
    public int getHeight() {
        return mHeight;
    }

        if (width <= 0  || height <= 0) {
            throw new IllegalArgumentException(
                    "width and height must large than 0");
    int[] calculateOffsets(int left, int top) {
        int[] offsets = null;
        if (mFormat == PixelFormat.YCbCr_420_SP) {
            offsets = new int[] {top * mStrides[0] + left,
                  mHeight * mStrides[0] + top / 2 * mStrides[1]
                  + left / 2 * 2 };
            return offsets;
        }

        if (mFormat == PixelFormat.YCbCr_422_I) {
            offsets = new int[] {top * mStrides[0] + left / 2 * 4};
            return offsets;
        }

        int requiredSize;
        return offsets;
    }

    private int[] calculateStrides(int width, int format) {
        int[] strides = null;
        if (format == PixelFormat.YCbCr_420_SP) {
            requiredSize = height * mStrides[0] +(height >> 1) * mStrides[1];
        } else {
            requiredSize = height * mStrides[0];
            strides = new int[] {width, width};
            return strides;
        }

        if (requiredSize > mData.length) {
            throw new IllegalArgumentException(
                    "width or/and height is larger than the yuv data");
        if (format == PixelFormat.YCbCr_422_I) {
            strides = new int[] {width * 2};
            return strides;
        }

        return strides;
    }

        return true;
   private void adjustRectangle(Rect rect) {
       int width = rect.width();
       int height = rect.height();
       if (mFormat == PixelFormat.YCbCr_420_SP) {
           // Make sure left, top, width and height are all even.
           width &= ~1;
           height &= ~1;
           rect.left &= ~1;
           rect.top &= ~1;
           rect.right = rect.left + width;
           rect.bottom = rect.top + height;
        }

        if (mFormat == PixelFormat.YCbCr_422_I) {
            // Make sure left and width are both even.
            width &= ~1;
            rect.left &= ~1;
            rect.right = rect.left + width;
        }
    }

    //////////// native methods