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

Commit 3849f93b authored by Romain Guy's avatar Romain Guy
Browse files

Add Bitmap.isPremultiplied()

This change also adds extra information in the documentation about
premultiplied values.

Change-Id: I51aacb8696340d23354ebf6d7284605d1a790b69
parent 9c469ca0
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -8121,6 +8121,7 @@ package android.graphics {
    method public final int getWidth();
    method public final int getWidth();
    method public final boolean hasAlpha();
    method public final boolean hasAlpha();
    method public final boolean isMutable();
    method public final boolean isMutable();
    method public final boolean isPremultiplied();
    method public final boolean isRecycled();
    method public final boolean isRecycled();
    method public void prepareToDraw();
    method public void prepareToDraw();
    method public void recycle();
    method public void recycle();
+47 −18
Original line number Original line Diff line number Diff line
@@ -27,7 +27,6 @@ import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.nio.ShortBuffer;


public final class Bitmap implements Parcelable {
public final class Bitmap implements Parcelable {

    /**
    /**
     * Indicates that the bitmap was created for an unknown pixel density.
     * Indicates that the bitmap was created for an unknown pixel density.
     *
     *
@@ -64,7 +63,7 @@ public final class Bitmap implements Parcelable {
    private boolean mRecycled;
    private boolean mRecycled;


    // Package-scoped for fast access.
    // Package-scoped for fast access.
    /*package*/ int mDensity = sDefaultDensity = getDefaultDensity();
    int mDensity = sDefaultDensity = getDefaultDensity();


    private static volatile Matrix sScaleMatrix;
    private static volatile Matrix sScaleMatrix;


@@ -79,7 +78,7 @@ public final class Bitmap implements Parcelable {
        sDefaultDensity = density;
        sDefaultDensity = density;
    }
    }


    /*package*/ static int getDefaultDensity() {
    static int getDefaultDensity() {
        if (sDefaultDensity >= 0) {
        if (sDefaultDensity >= 0) {
            return sDefaultDensity;
            return sDefaultDensity;
        }
        }
@@ -95,7 +94,7 @@ public final class Bitmap implements Parcelable {


        This can be called from JNI code.
        This can be called from JNI code.
    */
    */
    /*package*/ Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk,
    Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk,
            int density) {
            int density) {
        this(nativeBitmap, buffer, isMutable, ninePatchChunk, null, density);
        this(nativeBitmap, buffer, isMutable, ninePatchChunk, null, density);
    }
    }
@@ -108,7 +107,7 @@ public final class Bitmap implements Parcelable {


        This can be called from JNI code.
        This can be called from JNI code.
    */
    */
    /*package*/ Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk,
    Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk,
            int[] layoutBounds, int density) {
            int[] layoutBounds, int density) {
        if (nativeBitmap == 0) {
        if (nativeBitmap == 0) {
            throw new RuntimeException("internal error: native bitmap is 0");
            throw new RuntimeException("internal error: native bitmap is 0");
@@ -347,11 +346,15 @@ public final class Bitmap implements Parcelable {
    }
    }


    /**
    /**
     * Copy the bitmap's pixels into the specified buffer (allocated by the
     * <p>Copy the bitmap's pixels into the specified buffer (allocated by the
     * caller). An exception is thrown if the buffer is not large enough to
     * caller). An exception is thrown if the buffer is not large enough to
     * hold all of the pixels (taking into account the number of bytes per
     * hold all of the pixels (taking into account the number of bytes per
     * pixel) or if the Buffer subclass is not one of the support types
     * pixel) or if the Buffer subclass is not one of the support types
     * (ByteBuffer, ShortBuffer, IntBuffer).
     * (ByteBuffer, ShortBuffer, IntBuffer).</p>
     * <p>The content of the bitmap is copied into the buffer as-is. This means
     * that if this bitmap stores its pixels pre-multiplied
     * (see {@link #isPremultiplied()}, the values in the buffer will also be
     * pre-multiplied.</p>
     */
     */
    public void copyPixelsToBuffer(Buffer dst) {
    public void copyPixelsToBuffer(Buffer dst) {
        int elements = dst.remaining();
        int elements = dst.remaining();
@@ -382,10 +385,10 @@ public final class Bitmap implements Parcelable {
    }
    }


    /**
    /**
     * Copy the pixels from the buffer, beginning at the current position,
     * <p>Copy the pixels from the buffer, beginning at the current position,
     * overwriting the bitmap's pixels. The data in the buffer is not changed
     * overwriting the bitmap's pixels. The data in the buffer is not changed
     * in any way (unlike setPixels(), which converts from unpremultipled 32bit
     * in any way (unlike setPixels(), which converts from unpremultipled 32bit
     * to whatever the bitmap's native format is.
     * to whatever the bitmap's native format is.</p>
     */
     */
    public void copyPixelsFromBuffer(Buffer src) {
    public void copyPixelsFromBuffer(Buffer src) {
        checkRecycled("copyPixelsFromBuffer called on recycled bitmap");
        checkRecycled("copyPixelsFromBuffer called on recycled bitmap");
@@ -780,6 +783,25 @@ public final class Bitmap implements Parcelable {
        return mIsMutable;
        return mIsMutable;
    }
    }


    /**
     * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied.
     * When a pixel is pre-multiplied, the RGB components have been multiplied by
     * the alpha component. For instance, if the original color is a 50%
     * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is 
     * <code>(128, 128, 0, 0)</code>.</p>
     * 
     * <p>This method always returns false if {@link #getConfig()} is
     * {@link Bitmap.Config#ALPHA_8} or {@link Bitmap.Config#RGB_565}.</p>
     * 
     * @return true if the underlying pixels have been pre-multiplied, false
     *         otherwise
     */
    public final boolean isPremultiplied() {
        final Config config = getConfig();
        //noinspection deprecation
        return config == Config.ARGB_8888 || config == Config.ARGB_4444;
    }

    /** Returns the bitmap's width */
    /** Returns the bitmap's width */
    public final int getWidth() {
    public final int getWidth() {
        return mWidth == -1 ? mWidth = nativeWidth(mNativeBitmap) : mWidth;
        return mWidth == -1 ? mWidth = nativeWidth(mNativeBitmap) : mWidth;
@@ -926,7 +948,7 @@ public final class Bitmap implements Parcelable {
    /**
    /**
     * Returns the {@link Color} at the specified location. Throws an exception
     * Returns the {@link Color} at the specified location. Throws an exception
     * if x or y are out of bounds (negative or >= to the width or height
     * if x or y are out of bounds (negative or >= to the width or height
     * respectively).
     * respectively). The returned color is a non-premultiplied ARGB value.
     *
     *
     * @param x    The x coordinate (0...width-1) of the pixel to return
     * @param x    The x coordinate (0...width-1) of the pixel to return
     * @param y    The y coordinate (0...height-1) of the pixel to return
     * @param y    The y coordinate (0...height-1) of the pixel to return
@@ -944,6 +966,7 @@ public final class Bitmap implements Parcelable {
     * a packed int representing a {@link Color}. The stride parameter allows
     * a packed int representing a {@link Color}. The stride parameter allows
     * the caller to allow for gaps in the returned pixels array between
     * the caller to allow for gaps in the returned pixels array between
     * rows. For normal packed results, just pass width for the stride value.
     * rows. For normal packed results, just pass width for the stride value.
     * The returned colors are non-premultiplied ARGB values.
     *
     *
     * @param pixels   The array to receive the bitmap's colors
     * @param pixels   The array to receive the bitmap's colors
     * @param offset   The first index to write into pixels[]
     * @param offset   The first index to write into pixels[]
@@ -955,6 +978,7 @@ public final class Bitmap implements Parcelable {
     *                 the bitmap
     *                 the bitmap
     * @param width    The number of pixels to read from each row
     * @param width    The number of pixels to read from each row
     * @param height   The number of rows to read
     * @param height   The number of rows to read
     *
     * @throws IllegalArgumentException if x, y, width, height exceed the
     * @throws IllegalArgumentException if x, y, width, height exceed the
     *         bounds of the bitmap, or if abs(stride) < width.
     *         bounds of the bitmap, or if abs(stride) < width.
     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
@@ -974,6 +998,7 @@ public final class Bitmap implements Parcelable {
    /**
    /**
     * Shared code to check for illegal arguments passed to getPixel()
     * Shared code to check for illegal arguments passed to getPixel()
     * or setPixel()
     * or setPixel()
     * 
     * @param x x coordinate of the pixel
     * @param x x coordinate of the pixel
     * @param y y coordinate of the pixel
     * @param y y coordinate of the pixel
     */
     */
@@ -1029,12 +1054,14 @@ public final class Bitmap implements Parcelable {
    }
    }


    /**
    /**
     * Write the specified {@link Color} into the bitmap (assuming it is
     * <p>Write the specified {@link Color} into the bitmap (assuming it is
     * mutable) at the x,y coordinate.
     * mutable) at the x,y coordinate. The color must be a
     * non-premultiplied ARGB value.</p>
     *
     *
     * @param x     The x coordinate of the pixel to replace (0...width-1)
     * @param x     The x coordinate of the pixel to replace (0...width-1)
     * @param y     The y coordinate of the pixel to replace (0...height-1)
     * @param y     The y coordinate of the pixel to replace (0...height-1)
     * @param color The {@link Color} to write into the bitmap
     * @param color The ARGB color to write into the bitmap
     *
     * @throws IllegalStateException if the bitmap is not mutable
     * @throws IllegalStateException if the bitmap is not mutable
     * @throws IllegalArgumentException if x, y are outside of the bitmap's
     * @throws IllegalArgumentException if x, y are outside of the bitmap's
     *         bounds.
     *         bounds.
@@ -1049,8 +1076,9 @@ public final class Bitmap implements Parcelable {
    }
    }


    /**
    /**
     * Replace pixels in the bitmap with the colors in the array. Each element
     * <p>Replace pixels in the bitmap with the colors in the array. Each element
     * in the array is a packed int prepresenting a {@link Color}
     * in the array is a packed int prepresenting a non-premultiplied ARGB
     * {@link Color}.</p>
     *
     *
     * @param pixels   The colors to write to the bitmap
     * @param pixels   The colors to write to the bitmap
     * @param offset   The index of the first color to read from pixels[]
     * @param offset   The index of the first color to read from pixels[]
@@ -1063,6 +1091,7 @@ public final class Bitmap implements Parcelable {
     *                 the bitmap.
     *                 the bitmap.
     * @param width    The number of colors to copy from pixels[] per row
     * @param width    The number of colors to copy from pixels[] per row
     * @param height   The number of rows to write to the bitmap
     * @param height   The number of rows to write to the bitmap
     *
     * @throws IllegalStateException if the bitmap is not mutable
     * @throws IllegalStateException if the bitmap is not mutable
     * @throws IllegalArgumentException if x, y, width, height are outside of
     * @throws IllegalArgumentException if x, y, width, height are outside of
     *         the bitmap's bounds.
     *         the bitmap's bounds.