Bitmap.java revision 775873a66a946fae2b0535abb51df9817bd1b20c
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
191c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbyeimport android.annotation.CheckResult;
2080756e38882720860db52f1fcc21fa1505a02abfTor Norbyeimport android.annotation.ColorInt;
21c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viveretteimport android.annotation.NonNull;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcel;
238cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport android.os.Parcelable;
240df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reckimport android.os.Trace;
252784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackbornimport android.util.DisplayMetrics;
2601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reckimport android.util.Log;
27f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
288cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.io.OutputStream;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.Buffer;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.ByteBuffer;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.IntBuffer;
328cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.nio.ShortBuffer;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
34775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhlerimport libcore.util.NativeAllocationRegistry;
35775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class Bitmap implements Parcelable {
3701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck    private static final String TAG = "Bitmap";
3801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Indicates that the bitmap was created for an unknown pixel density.
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see Bitmap#getDensity()
4311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see Bitmap#setDensity(int)
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public static final int DENSITY_NONE = 0;
46f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
47775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    // Estimated size of the Bitmap native allocation, not including
48775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    // pixel data.
49775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    private static final long NATIVE_ALLOCATION_SIZE = 32;
50775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler
51e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy    /**
52e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy     * Backing buffer for the Bitmap.
53e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy     */
54f29d5a5b211786248d0557157c304c5fff428bd4Derek Sollenberger    private byte[] mBuffer;
55e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
56f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Convenience for JNI access
57f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    private final long mNativePtr;
58e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final boolean mIsMutable;
601abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
611abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    /**
6257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * Represents whether the Bitmap's content is requested to be pre-multiplied.
631abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Note that isPremultiplied() does not directly return this value, because
6457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * isPremultiplied() may never return true for a 565 Bitmap or a bitmap
6557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * without alpha.
661abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
671abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * setPremultiplied() does directly set the value so that setConfig() and
681abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * setPremultiplied() aren't order dependent, despite being setters.
6957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     *
7057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * The native bitmap's premultiplication state is kept up to date by
7157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * pushing down this preference for every config change.
721abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     */
7357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private boolean mRequestPremultiplied;
748790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
7547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    private byte[] mNinePatchChunk; // may be null
7647cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    private NinePatch.InsetStruct mNinePatchInsets; // may be null
771abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    private int mWidth;
781abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    private int mHeight;
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean mRecycled;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    // Package-scoped for fast access.
82dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    int mDensity = getDefaultDensity();
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    private static volatile Matrix sScaleMatrix;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    private static volatile int sDefaultDensity = -1;
8702890fd0f98b3b8d98baf0bda1ea906afd723d8bRomain Guy
8896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    /**
8996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * For backwards compatibility, allows the app layer to change the default
9096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * density when running old apps.
9196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * @hide
9296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     */
9396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    public static void setDefaultDensity(int density) {
9496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        sDefaultDensity = density;
9596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    }
963849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy
97f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    @SuppressWarnings("deprecation")
983849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    static int getDefaultDensity() {
9996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        if (sDefaultDensity >= 0) {
10096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn            return sDefaultDensity;
10196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        }
10296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        sDefaultDensity = DisplayMetrics.DENSITY_DEVICE;
10396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        return sDefaultDensity;
10496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    }
105dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1079f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Private constructor that must received an already allocated native bitmap
1089f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * int (pointer).
109ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     */
110f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    // called from JNI
11136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    Bitmap(long nativeBitmap, byte[] buffer, int width, int height, int density,
11257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            boolean isMutable, boolean requestPremultiplied,
11347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            byte[] ninePatchChunk, NinePatch.InsetStruct ninePatchInsets) {
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (nativeBitmap == 0) {
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("internal error: native bitmap is 0");
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1178cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
1181abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mWidth = width;
1191abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mHeight = height;
1201abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mIsMutable = isMutable;
12157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = requestPremultiplied;
122e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        mBuffer = buffer;
1234cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNinePatchChunk = ninePatchChunk;
12547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        mNinePatchInsets = ninePatchInsets;
126de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn        if (density >= 0) {
127de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn            mDensity = density;
128de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn        }
1294cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik
130f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mNativePtr = nativeBitmap;
131775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        long nativeSize = NATIVE_ALLOCATION_SIZE;
132775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (buffer == null) {
133775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            nativeSize += getByteCount();
134775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        }
135775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        NativeAllocationRegistry registry = new NativeAllocationRegistry(
136775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            nativeGetNativeFinalizer(), nativeSize);
137775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        registry.registerNativeAllocation(this, nativeBitmap);
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1414c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger     * Return the pointer to the native object.
1424c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger     */
1434c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    long getNativeInstance() {
1444c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        return mNativePtr;
1454c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    }
1464c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
1474c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    /**
1481abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Native bitmap has been reconfigured, so set premult and cached
1491abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * width/height values
1509f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     */
151f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    // called from JNI
15257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    void reinit(int width, int height, boolean requestPremultiplied) {
1531abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mWidth = width;
1541abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mHeight = height;
15557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = requestPremultiplied;
1569f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    }
1579f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
1589f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    /**
15911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * <p>Returns the density for this bitmap.</p>
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * <p>The default density is the same density as the current display,
16296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * unless the current application does not support different screen
16396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * densities in which case it is
16496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * {@link android.util.DisplayMetrics#DENSITY_DEFAULT}.  Note that
16596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * compatibility mode is determined by the application that was initially
16696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * loaded into a process -- applications that share the same process should
16796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * all have the same compatibility, or ensure they explicitly set the
16896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * density of their bitmaps appropriately.</p>
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * @return A scaling factor of the default density or {@link #DENSITY_NONE}
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         if the scaling factor is unknown.
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #setDensity(int)
174a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn     * @see android.util.DisplayMetrics#DENSITY_DEFAULT
17511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see android.util.DisplayMetrics#densityDpi
17611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #DENSITY_NONE
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getDensity() {
17901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
18001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getDensity() on a recycle()'d bitmap! This is undefined behavior!");
18101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
18211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return mDensity;
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * <p>Specifies the density for this bitmap.  When the bitmap is
18711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * drawn to a Canvas that also has a density, it will be scaled
18811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * appropriately.</p>
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
19011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param density The density scaling factor to use with this bitmap or
19111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     *        {@link #DENSITY_NONE} if the density is unknown.
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
19311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #getDensity()
194a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn     * @see android.util.DisplayMetrics#DENSITY_DEFAULT
19511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see android.util.DisplayMetrics#densityDpi
19611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #DENSITY_NONE
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public void setDensity(int density) {
19911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        mDensity = density;
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
201c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
202c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
203c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Modifies the bitmap to have a specified width, height, and {@link
204c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * Config}, without affecting the underlying allocation backing the bitmap.
205c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * Bitmap pixel data is not re-initialized for the new configuration.</p>
206c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
207c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>This method can be used to avoid allocating a new bitmap, instead
208c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * reusing an existing bitmap's allocation for a new configuration of equal
209c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * or lesser size. If the Bitmap's allocation isn't large enough to support
210c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the new configuration, an IllegalArgumentException will be thrown and the
211c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * bitmap will not be modified.</p>
212c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
213c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>The result of {@link #getByteCount()} will reflect the new configuration,
214c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * while {@link #getAllocationByteCount()} will reflect that of the initial
215c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * configuration.</p>
216c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
21717a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * <p>Note: This may change this result of hasAlpha(). When converting to 565,
21817a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * the new bitmap will always be considered opaque. When converting from 565,
21917a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * the new bitmap will be considered non-opaque, and will respect the value
22017a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * set by setPremultiplied().</p>
22117a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     *
2226d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <p>WARNING: This method should NOT be called on a bitmap currently in use
2236d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * by the view system, Canvas, or the AndroidBitmap NDK API. It does not
2246d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * make guarantees about how the underlying pixel buffer is remapped to the
2256d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * new config, just that the allocation is reused. Additionally, the view
2266d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * system does not account for bitmap properties being modifying during use,
2276d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * e.g. while attached to drawables.</p>
2286d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *
2296d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <p>In order to safely ensure that a Bitmap is no longer in use by the
2306d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * View system it is necessary to wait for a draw pass to occur after
2316d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * invalidate()'ing any view that had previously drawn the Bitmap in the last
2326d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * draw pass due to hardware acceleration's caching of draw commands. As
2336d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * an example, here is how this can be done for an ImageView:
2346d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <pre class="prettyprint">
2356d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      ImageView myImageView = ...;
2366d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      final Bitmap myBitmap = ...;
2376d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      myImageView.setImageDrawable(null);
2386d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      myImageView.post(new Runnable() {
2396d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *          public void run() {
2406d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              // myBitmap is now no longer in use by the ImageView
2416d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              // and can be safely reconfigured.
2426d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              myBitmap.reconfigure(...);
2436d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *          }
2446d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      });
2456d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * </pre></p>
246c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
247c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
248c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
249c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
250c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
251c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void reconfigure(int width, int height, Config config) {
252c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        checkRecycled("Can't call reconfigure() on a recycled bitmap");
253c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (width <= 0 || height <= 0) {
254c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik            throw new IllegalArgumentException("width and height must be > 0");
255c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
256c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (!isMutable()) {
2579c2cde0583d02ba7551877dbe7a5ecaee6defb56Chris Craik            throw new IllegalStateException("only mutable bitmaps may be reconfigured");
258c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
259c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (mBuffer == null) {
2601abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik            throw new IllegalStateException("native-backed bitmaps may not be reconfigured");
261c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
262c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
263775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeReconfigure(mNativePtr, width, height, config.nativeInt,
264f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                mBuffer.length, mRequestPremultiplied);
265c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        mWidth = width;
266c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        mHeight = height;
267c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
268c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
269c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
270c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
271c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current height and config.</p>
272c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
273c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
274c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
275c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
276c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
277c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
278c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
279c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
280c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
281c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setWidth(int width) {
282c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(width, getHeight(), getConfig());
283c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
284c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
285c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
286c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
287c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current width and config.</p>
288c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
289c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
290c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
291c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
292c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
293c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
294c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
295c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
296c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
297c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setHeight(int height) {
298c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(getWidth(), height, getConfig());
299c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
300c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
301c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
302c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
303c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current height and width.</p>
304c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
305c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
306c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
307c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
308c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
309c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
310c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
311c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
312c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
313c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setConfig(Config config) {
314c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(getWidth(), getHeight(), config);
315c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
316c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the nine patch chunk.
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param chunk The definition of the nine patch
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setNinePatchChunk(byte[] chunk) {
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNinePatchChunk = chunk;
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3278cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
329a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * Free the native object associated with this bitmap, and clear the
330a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * reference to the pixel data. This will not free the pixel data synchronously;
331a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * it simply allows it to be garbage collected if there are no other references.
332a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * The bitmap is marked as "dead", meaning it will throw an exception if
333a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * getPixels() or setPixels() is called, and will draw nothing. This operation
334a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * cannot be reversed, so it should only be called if you are sure there are no
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * further uses for the bitmap. This is an advanced call, and normally need
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * not be called, since the normal GC process will free up this memory when
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * there are no more references to this bitmap.
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void recycle() {
340775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (!mRecycled && mNativePtr != 0) {
341775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            if (nativeRecycle(mNativePtr)) {
342547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // return value indicates whether native pixel object was actually recycled.
343547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // false indicates that it is still in use at the native level and these
344547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // objects should not be collected now. They will be collected later when the
345547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // Bitmap itself is collected.
346547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                mBuffer = null;
347547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                mNinePatchChunk = null;
348547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase            }
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mRecycled = true;
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true if this bitmap has been recycled. If so, then it is an error
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to try to access its pixels, and the bitmap will not draw.
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the bitmap has been recycled
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isRecycled() {
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mRecycled;
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3628cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3640bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * Returns the generation ID of this bitmap. The generation ID changes
3650bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * whenever the bitmap is modified. This can be used as an efficient way to
3660bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * check if a bitmap has changed.
367f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
3680bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * @return The current generation ID for this bitmap.
3690bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     */
3700bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    public int getGenerationId() {
37101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
37201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getGenerationId() on a recycle()'d bitmap! This is undefined behavior!");
37301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
374775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeGenerationId(mNativePtr);
3750bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
376f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
3770bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    /**
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This is called by methods that want to throw an exception if the bitmap
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * has already been recycled.
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkRecycled(String errorMessage) {
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mRecycled) {
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException(errorMessage);
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3868cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Common code for checking that x and y are >= 0
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x x coordinate to ensure is >= 0
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y y coordinate to ensure is >= 0
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void checkXYSign(int x, int y) {
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x < 0) {
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x must be >= 0");
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y < 0) {
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y must be >= 0");
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Common code for checking that width and height are > 0
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width  width to ensure is > 0
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height height to ensure is > 0
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void checkWidthHeight(int width, int height) {
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width <= 0) {
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("width must be > 0");
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (height <= 0) {
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("height must be > 0");
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
417676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy    /**
418676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * Possible bitmap configurations. A bitmap configuration describes
419676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * how pixels are stored. This affects the quality (color depth) as
420676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * well as the ability to display transparent/translucent colors.
421676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     */
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum Config {
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // these native values must match up with the enum in SkBitmap.h
424676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
425676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
426676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored as a single translucency (alpha) channel.
427676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This is very useful to efficiently store masks for instance.
428676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * No color information is stored.
429676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * With this configuration, each pixel requires 1 byte of memory.
430676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
431b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ALPHA_8     (1),
432676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
433676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
434676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 2 bytes and only the RGB channels are
435676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * encoded: red is stored with 5 bits of precision (32 possible
436676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * values), green is stored with 6 bits of precision (64 possible
437676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * values) and blue is stored with 5 bits of precision.
438f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
439676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration can produce slight visual artifacts depending
440676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * on the configuration of the source. For instance, without
441676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * dithering, the result might show a greenish tint. To get better
442676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * results dithering should be applied.
443f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
444676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration may be useful when using opaque bitmaps
445676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * that do not require high color fidelity.
446676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
447b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        RGB_565     (3),
448676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
449676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
450676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 2 bytes. The three RGB color channels
451676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * and the alpha channel (translucency) are stored with a 4 bits
452676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * precision (16 possible values.)
453f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
454676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration is mostly useful if the application needs
455676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * to store translucency information but also needs to save
456676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * memory.
457f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
458676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * It is recommended to use {@link #ARGB_8888} instead of this
459676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * configuration.
46025ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         *
461e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase         * Note: as of {@link android.os.Build.VERSION_CODES#KITKAT},
46225ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * any bitmap created with this configuration will be created
46325ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * using {@link #ARGB_8888} instead.
464f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
465676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * @deprecated Because of the poor quality of this configuration,
466676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         *             it is advised to use {@link #ARGB_8888} instead.
467676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
468676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        @Deprecated
469b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ARGB_4444   (4),
470676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
471676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
472676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 4 bytes. Each channel (RGB and alpha
473676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * for translucency) is stored with 8 bits of precision (256
474676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * possible values.)
475f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
476676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration is very flexible and offers the best
477676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * quality. It should be used whenever possible.
478676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
479b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ARGB_8888   (5);
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
481676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        final int nativeInt;
482676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
483676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        private static Config sConfigs[] = {
484b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger            null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888
485676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        };
486f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Config(int ni) {
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            this.nativeInt = ni;
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4908cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
491676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        static Config nativeToConfig(int ni) {
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return sConfigs[ni];
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4958cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4973849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Copy the bitmap's pixels into the specified buffer (allocated by the
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * caller). An exception is thrown if the buffer is not large enough to
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * hold all of the pixels (taking into account the number of bytes per
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pixel) or if the Buffer subclass is not one of the support types
5013849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * (ByteBuffer, ShortBuffer, IntBuffer).</p>
5023849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>The content of the bitmap is copied into the buffer as-is. This means
5033849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * that if this bitmap stores its pixels pre-multiplied
5043849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * (see {@link #isPremultiplied()}, the values in the buffer will also be
5053849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * pre-multiplied.</p>
5061f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * <p>After this method returns, the current position of the buffer is
5071f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * updated: the position is incremented by the number of elements written
5081f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * in the buffer.</p>
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void copyPixelsToBuffer(Buffer dst) {
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int elements = dst.remaining();
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int shift;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (dst instanceof ByteBuffer) {
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 0;
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (dst instanceof ShortBuffer) {
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 1;
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (dst instanceof IntBuffer) {
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 2;
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("unsupported Buffer subclass");
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5228cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        long bufferSize = (long)elements << shift;
524f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        long pixelSize = getByteCount();
5258cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bufferSize < pixelSize) {
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Buffer not large enough for pixels");
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5298cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
530775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeCopyPixelsToBuffer(mNativePtr, dst);
5318cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // now update the buffer's position
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int position = dst.position();
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        position += pixelSize >> shift;
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst.position(position);
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5393849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Copy the pixels from the buffer, beginning at the current position,
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * overwriting the bitmap's pixels. The data in the buffer is not changed
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in any way (unlike setPixels(), which converts from unpremultipled 32bit
5423849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * to whatever the bitmap's native format is.</p>
5431f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * <p>After this method returns, the current position of the buffer is
5441f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * updated: the position is incremented by the number of elements read from
5451f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * the buffer. If you need to read the bitmap from the buffer again you must
5461f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * first rewind the buffer.</p>
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void copyPixelsFromBuffer(Buffer src) {
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("copyPixelsFromBuffer called on recycled bitmap");
5508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int elements = src.remaining();
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int shift;
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (src instanceof ByteBuffer) {
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 0;
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (src instanceof ShortBuffer) {
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 1;
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (src instanceof IntBuffer) {
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 2;
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("unsupported Buffer subclass");
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5628cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5633849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy        long bufferBytes = (long) elements << shift;
564f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        long bitmapBytes = getByteCount();
5658cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bufferBytes < bitmapBytes) {
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Buffer not large enough for pixels");
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5698cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
570775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeCopyPixelsFromBuffer(mNativePtr, src);
57155adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen
57255adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        // now update the buffer's position
57355adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        int position = src.position();
57455adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        position += bitmapBytes >> shift;
57555adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        src.position(position);
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5778cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Tries to make a new bitmap based on the dimensions of this bitmap,
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * setting the new bitmap's config to the one specified, and then copying
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this bitmap's pixels into the new bitmap. If the conversion is not
58296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * supported, or the allocator fails, then this returns NULL.  The returned
58396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * bitmap initially has the same density as the original.
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config    The desired config for the resulting bitmap
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param isMutable True if the resulting bitmap should be mutable (i.e.
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                  its pixels can be modified)
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the new bitmap, or null if the copy could not be made.
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap copy(Config config, boolean isMutable) {
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't copy a recycled bitmap");
592775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable);
59396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        if (b != null) {
59457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            b.setPremultiplied(mRequestPremultiplied);
59596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn            b.mDensity = mDensity;
59696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        }
59796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        return b;
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
60002d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick    /**
601721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * Creates a new immutable bitmap backed by ashmem which can efficiently
602721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * be passed between processes.
603721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     *
604721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * @hide
605721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     */
606721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    public Bitmap createAshmemBitmap() {
607721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        checkRecycled("Can't copy a recycled bitmap");
608775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap b = nativeCopyAshmem(mNativePtr);
609721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        if (b != null) {
610721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews            b.setPremultiplied(mRequestPremultiplied);
611721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews            b.mDensity = mDensity;
612721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        }
613721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        return b;
614721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    }
615721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews
616721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    /**
617f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
618f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     * specified width and height are the same as the current width and height of
6199f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * the source bitmap, the source bitmap is returned and no new bitmap is
620f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * created.
62102d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     *
62202d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param src       The source bitmap.
62302d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param dstWidth  The new bitmap's desired width.
62402d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param dstHeight The new bitmap's desired height.
62502d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param filter    true if the source should be filtered.
626f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * @return The new scaled bitmap or the source bitmap if no scaling is required.
627790552b240c05d58407f7c14acba656b2e85c523Romain Guy     * @throws IllegalArgumentException if width is <= 0, or height is <= 0
62802d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     */
629f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy    public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
630f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy            boolean filter) {
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Matrix m;
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (Bitmap.class) {
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // small pool of just 1 matrix
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m = sScaleMatrix;
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sScaleMatrix = null;
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (m == null) {
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m = new Matrix();
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6418cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int width = src.getWidth();
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int height = src.getHeight();
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final float sx = dstWidth  / (float)width;
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final float sy = dstHeight / (float)height;
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        m.setScale(sx, sy);
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (Bitmap.class) {
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // do we need to check for null? why not just assign everytime?
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (sScaleMatrix == null) {
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                sScaleMatrix = m;
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6568cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen        return b;
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6588cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from the source bitmap. The new bitmap may
66196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * be the same object as source, or a copy may have been made.  It is
66296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initialized with the same density as the original bitmap.
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(Bitmap src) {
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from the specified subset of the source
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * bitmap. The new bitmap may be the same object as source, or a copy may
671f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * have been made. It is initialized with the same density as the original
672f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * bitmap.
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param source   The bitmap we are subsetting
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel in source
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel in source
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels in each row
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows
679f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * @return A copy of a subset of the source bitmap or the source bitmap itself.
680790552b240c05d58407f7c14acba656b2e85c523Romain Guy     * @throws IllegalArgumentException if the x, y, width, height values are
681790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         outside of the dimensions of the source bitmap, or width is <= 0,
682790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         or height is <= 0
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return createBitmap(source, x, y, width, height, null, false);
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6878cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from subset of the source bitmap,
690f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * transformed by the optional matrix. The new bitmap may be the
691f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * same object as source, or a copy may have been made. It is
69296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initialized with the same density as the original bitmap.
693f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
694f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * If the source bitmap is immutable and the requested subset is the
695f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * same as the source bitmap itself, then the source bitmap is
696f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * returned and no new bitmap is created.
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param source   The bitmap we are subsetting
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel in source
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel in source
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels in each row
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows
70360b88edea7132ddce90f2dced07c6706f1502270Ken Shirriff     * @param m        Optional matrix to be applied to the pixels
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param filter   true if the source should be filtered.
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   Only applies if the matrix contains more than just
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   translation.
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return A bitmap that represents the specified subset of source
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the x, y, width, height values are
709790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         outside of the dimensions of the source bitmap, or width is <= 0,
710790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         or height is <= 0
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Matrix m, boolean filter) {
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkWidthHeight(width, height);
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x + width > source.getWidth()) {
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x + width must be <= bitmap.width()");
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y + height > source.getHeight()) {
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y + height must be <= bitmap.height()");
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // check if we can just return our argument unchanged
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                height == source.getHeight() && (m == null || m.isIdentity())) {
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return source;
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7298cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int neww = width;
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int newh = height;
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Canvas canvas = new Canvas();
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bitmap;
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Paint paint;
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Rect srcR = new Rect(x, y, x + width, y + height);
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        RectF dstR = new RectF(0, 0, width, height);
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
739feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        Config newConfig = Config.ARGB_8888;
740feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        final Config config = source.getConfig();
741feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        // GIF files generate null configs, assume ARGB_8888
742feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        if (config != null) {
743feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy            switch (config) {
744feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case RGB_565:
745feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.RGB_565;
746feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
747feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ALPHA_8:
748feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.ALPHA_8;
749feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
750676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy                //noinspection deprecation
751feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ARGB_4444:
752feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ARGB_8888:
753feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                default:
754feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.ARGB_8888;
755feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
756feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy            }
757feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        }
758d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (m == null || m.isIdentity()) {
760d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha());
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint = null;   // not needed
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
763d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            final boolean transformed = !m.rectStaysRect();
764d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            RectF deviceR = new RectF();
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m.mapRect(deviceR, dstR);
767d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            neww = Math.round(deviceR.width());
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            newh = Math.round(deviceR.height());
770d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
771d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            bitmap = createBitmap(neww, newh, transformed ? Config.ARGB_8888 : newConfig,
772d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy                    transformed || source.hasAlpha());
773d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas.translate(-deviceR.left, -deviceR.top);
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas.concat(m);
776d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint = new Paint();
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint.setFilterBitmap(filter);
779d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            if (transformed) {
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                paint.setAntiAlias(true);
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7838790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The new bitmap was created from a known bitmap source so assume that
78511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        // they use the same density
78611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        bitmap.mDensity = source.mDensity;
78757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        bitmap.setHasAlpha(source.hasAlpha());
78857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        bitmap.setPremultiplied(source.mRequestPremultiplied);
7898790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
79096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        canvas.setBitmap(bitmap);
79196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        canvas.drawBitmap(source, srcR, dstR, paint);
7926311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn        canvas.setBitmap(null);
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bitmap;
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7968cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
79896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
79996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initial density is as per {@link #getDensity}.
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create.
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(int width, int height, Config config) {
807d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        return createBitmap(width, height, config, true);
808d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    }
809d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
810d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    /**
811d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * Returns a mutable bitmap with the specified width and height.  Its
812dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
813dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
814dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
815dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
816dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
817dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
818dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create.
819dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0
820dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
821dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    public static Bitmap createBitmap(DisplayMetrics display, int width,
822dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            int height, Config config) {
823dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(display, width, height, config, true);
824dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
825dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
826dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
827dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
828d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * initial density is as per {@link #getDensity}.
829d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     *
830d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param width    The width of the bitmap
831d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param height   The height of the bitmap
832d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param config   The bitmap config to create.
833d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 this flag can be used to mark the
834d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     *                 bitmap as opaque. Doing so will clear the bitmap in black
835f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *                 instead of transparent.
836f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
837d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @throws IllegalArgumentException if the width or height are <= 0
838d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     */
839d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    private static Bitmap createBitmap(int width, int height, Config config, boolean hasAlpha) {
840dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, width, height, config, hasAlpha);
841dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
842dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
843dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
844dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
845dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
846dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
847dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
848dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
849dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
850dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
851dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create.
852dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param hasAlpha If the bitmap is ARGB_8888 this flag can be used to mark the
853dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bitmap as opaque. Doing so will clear the bitmap in black
854f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *                 instead of transparent.
855f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
856dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0
857dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
858dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    private static Bitmap createBitmap(DisplayMetrics display, int width, int height,
859dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            Config config, boolean hasAlpha) {
860e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        if (width <= 0 || height <= 0) {
861e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            throw new IllegalArgumentException("width and height must be > 0");
862e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        }
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);
864dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        if (display != null) {
865dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            bm.mDensity = display.densityDpi;
866dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        }
8678790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III        bm.setHasAlpha(hasAlpha);
868d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        if (config == Config.ARGB_8888 && !hasAlpha) {
869775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            nativeErase(bm.mNativePtr, 0xff000000);
870d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        }
8713b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // No need to initialize the bitmap to zeroes with other configs;
8723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // it is backed by a VM byte array which is by definition preinitialized
8733b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // to all zeroes.
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
8759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8768cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a immutable bitmap with the specified width and height, with each
87996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
88096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initial density is as per {@link #getDensity}.
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param colors   Array of {@link Color} used to initialize the pixels.
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   Number of values to skip before the first color in the
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 array of colors.
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   Number of colors in the array between rows (must be >=
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 width or <= -width).
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create. If the config does not
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 bytes in the colors[] will be ignored (assumed to be FF)
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0, or if
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the color array's length is less than the number of pixels.
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(int colors[], int offset, int stride,
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int width, int height, Config config) {
897dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, colors, offset, stride, width, height, config);
898dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
899dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
900dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
901dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a immutable bitmap with the specified width and height, with each
902dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
903dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
904dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
905dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
906dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
907dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param colors   Array of {@link Color} used to initialize the pixels.
908dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param offset   Number of values to skip before the first color in the
909dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 array of colors.
910dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param stride   Number of colors in the array between rows (must be >=
911dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 width or <= -width).
912dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
913dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
914dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create. If the config does not
915dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
916dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bytes in the colors[] will be ignored (assumed to be FF)
917dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0, or if
918dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *         the color array's length is less than the number of pixels.
919dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
920dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    public static Bitmap createBitmap(DisplayMetrics display, int colors[],
921dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            int offset, int stride, int width, int height, Config config) {
9229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkWidthHeight(width, height);
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (Math.abs(stride) < width) {
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("abs(stride) must be >= width");
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int lastScanline = offset + (height - 1) * stride;
9289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int length = colors.length;
9299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (lastScanline + width > length)) {
9319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
933e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        if (width <= 0 || height <= 0) {
934e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            throw new IllegalArgumentException("width and height must be > 0");
935e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        }
936dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        Bitmap bm = nativeCreate(colors, offset, stride, width, height,
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            config.nativeInt, false);
938dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        if (display != null) {
939dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            bm.mDensity = display.densityDpi;
940dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        }
941dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return bm;
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
9459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a immutable bitmap with the specified width and height, with each
94696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
94796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initial density is as per {@link #getDensity}.
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param colors   Array of {@link Color} used to initialize the pixels.
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 This array must be at least as large as width * height.
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create. If the config does not
9549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 bytes in the colors[] will be ignored (assumed to be FF)
9569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0, or if
9579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the color array's length is less than the number of pixels.
9589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(int colors[], int width, int height, Config config) {
960dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, colors, 0, width, width, height, config);
961dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
962dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
963dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
964dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a immutable bitmap with the specified width and height, with each
965dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
966dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
967dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
968dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
969dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
970dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param colors   Array of {@link Color} used to initialize the pixels.
971dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 This array must be at least as large as width * height.
972dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
973dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
974dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create. If the config does not
975dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
976dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bytes in the colors[] will be ignored (assumed to be FF)
977dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0, or if
978dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *         the color array's length is less than the number of pixels.
979dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
980dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    public static Bitmap createBitmap(DisplayMetrics display, int colors[],
981dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            int width, int height, Config config) {
982dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(display, colors, 0, width, width, height, config);
9839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
9869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an optional array of private data, used by the UI system for
9879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * some bitmaps. Not intended to be called by applications.
9889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public byte[] getNinePatchChunk() {
9909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mNinePatchChunk;
9919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
994c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     * Populates a rectangle with the bitmap's optical insets.
995c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     *
996c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     * @param outInsets Rect to populate with optical insets
997ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     * @hide
998ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     */
999c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette    public void getOpticalInsets(@NonNull Rect outInsets) {
100047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        if (mNinePatchInsets == null) {
1001c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette            outInsets.setEmpty();
1002c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette        } else {
100347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            outInsets.set(mNinePatchInsets.opticalRect);
1004c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette        }
1005ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    }
1006ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani
100747cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    /** @hide */
100847cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    public NinePatch.InsetStruct getNinePatchInsets() {
100947cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        return mNinePatchInsets;
101047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    }
101147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik
1012ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    /**
10139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Specifies the known formats a bitmap can be compressed into
10149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum CompressFormat {
10169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JPEG    (0),
10172305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        PNG     (1),
10182305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        WEBP    (2);
10199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CompressFormat(int nativeInt) {
10219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            this.nativeInt = nativeInt;
10229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int nativeInt;
10249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Number of bytes of temp storage we use for communicating between the
10289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * native compressor and the java OutputStream.
10299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final static int WORKING_COMPRESS_STORAGE = 4096;
10319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a compressed version of the bitmap to the specified outputstream.
10349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If this returns true, the bitmap can be reconstructed by passing a
10359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * corresponding inputstream to BitmapFactory.decodeStream(). Note: not
10369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * all Formats support all bitmap configs directly, so it is possible that
10379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the returned bitmap from BitmapFactory could be in a different bitdepth,
10389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque
10399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pixels).
10409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param format   The format of the compressed image
10429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
10439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 small size, 100 meaning compress for max quality. Some
10449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 formats, like PNG which is lossless, will ignore the
10459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 quality setting
10469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stream   The outputstream to write the compressed data.
10479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if successfully compressed to the specified stream.
10489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean compress(CompressFormat format, int quality, OutputStream stream) {
10509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't compress a recycled bitmap");
10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // do explicit check before calling the native method
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (stream == null) {
10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new NullPointerException();
10549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (quality < 0 || quality > 100) {
10569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("quality must be 0..100");
10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10580df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress");
1059775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        boolean result = nativeCompress(mNativePtr, format.nativeInt,
1060f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                quality, stream, new byte[WORKING_COMPRESS_STORAGE]);
10610df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
10620df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        return result;
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10648cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
10659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1066ff236f5fc1a088b1210d37a67b0e88b06c0fbdb2Newton Allen     * Returns true if the bitmap is marked as mutable (i.e.&nbsp;can be drawn into)
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isMutable() {
10699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mIsMutable;
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10723849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    /**
10733849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied.
10743849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * When a pixel is pre-multiplied, the RGB components have been multiplied by
10753849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * the alpha component. For instance, if the original color is a 50%
1076f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is
10773849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <code>(128, 128, 0, 0)</code>.</p>
1078f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
10793849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>This method always returns false if {@link #getConfig()} is
10804ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * {@link Bitmap.Config#RGB_565}.</p>
1081f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
108257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * <p>The return value is undefined if {@link #getConfig()} is
108357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * {@link Bitmap.Config#ALPHA_8}.</p>
108457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     *
10854ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * <p>This method only returns true if {@link #hasAlpha()} returns true.
10864ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * A bitmap with no alpha channel can be used both as a pre-multiplied and
10874ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * as a non pre-multiplied bitmap.</p>
10881abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
10891abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>Only pre-multiplied bitmaps may be drawn by the view system or
10901abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * {@link Canvas}. If a non-pre-multiplied bitmap with an alpha channel is
10911abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * drawn to a Canvas, a RuntimeException will be thrown.</p>
10921abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
10933849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * @return true if the underlying pixels have been pre-multiplied, false
10943849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *         otherwise
10951abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
10961abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see Bitmap#setPremultiplied(boolean)
10971abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see BitmapFactory.Options#inPremultiplied
10983849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     */
10993849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    public final boolean isPremultiplied() {
110001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
110101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called isPremultiplied() on a recycle()'d bitmap! This is undefined behavior!");
110201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1103775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeIsPremultiplied(mNativePtr);
11041abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    }
11051abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
11061abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    /**
11071abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Sets whether the bitmap should treat its data as pre-multiplied.
11081abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11091abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>Bitmaps are always treated as pre-multiplied by the view system and
11101abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * {@link Canvas} for performance reasons. Storing un-pre-multiplied data in
11111abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * a Bitmap (through {@link #setPixel}, {@link #setPixels}, or {@link
11121abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * BitmapFactory.Options#inPremultiplied BitmapFactory.Options.inPremultiplied})
11131abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * can lead to incorrect blending if drawn by the framework.</p>
11141abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11151abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>This method will not affect the behavior of a bitmap without an alpha
11161abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * channel, or if {@link #hasAlpha()} returns false.</p>
11171abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
1118889a1ddd00b74933d5f7daa651d98133bcb034baBen Murdoch     * <p>Calling {@link #createBitmap} or {@link #createScaledBitmap} with a source
11198790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * Bitmap whose colors are not pre-multiplied may result in a RuntimeException,
11208790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * since those functions require drawing the source, which is not supported for
11218790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * un-pre-multiplied Bitmaps.</p>
11228790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     *
11231abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see Bitmap#isPremultiplied()
11241abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see BitmapFactory.Options#inPremultiplied
11251abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     */
11261abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    public final void setPremultiplied(boolean premultiplied) {
11273e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setPremultiplied called on a recycled bitmap");
112857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = premultiplied;
1129775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPremultiplied(mNativePtr, premultiplied);
11303849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    }
11313849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy
11329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Returns the bitmap's width */
11339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getWidth() {
113401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
113501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getWidth() on a recycle()'d bitmap! This is undefined behavior!");
113601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
11371abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        return mWidth;
11389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Returns the bitmap's height */
11419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getHeight() {
114201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
114301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getHeight() on a recycle()'d bitmap! This is undefined behavior!");
114401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
11451abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        return mHeight;
11469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11478cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
11489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
114911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledWidth(int)} with the target
115011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link Canvas}.
11519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11522784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    public int getScaledWidth(Canvas canvas) {
115311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, canvas.mDensity);
11542784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    }
11552784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn
11562784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    /**
115711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledHeight(int)} with the target
115811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link Canvas}.
11592784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     */
11602784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    public int getScaledHeight(Canvas canvas) {
116111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, canvas.mDensity);
116211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
116311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
116411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
116511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledWidth(int)} with the target
116611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link DisplayMetrics}.
116711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
116811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledWidth(DisplayMetrics metrics) {
116911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, metrics.densityDpi);
117011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
117111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
117211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
117311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledHeight(int)} with the target
117411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link DisplayMetrics}.
117511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
117611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledHeight(DisplayMetrics metrics) {
117711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, metrics.densityDpi);
11782784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    }
11792784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn
11802784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    /**
11812784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * Convenience method that returns the width of this bitmap divided
11822784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * by the density scale factor.
11832784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     *
118411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param targetDensity The density of the target canvas of the bitmap.
11852784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * @return The scaled width of this bitmap, according to the density scale factor.
11862784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     */
118711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledWidth(int targetDensity) {
118811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, targetDensity);
11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convenience method that returns the height of this bitmap divided
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * by the density scale factor.
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
119511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param targetDensity The density of the target canvas of the bitmap.
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The scaled height of this bitmap, according to the density scale factor.
11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
119811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledHeight(int targetDensity) {
119911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, targetDensity);
120011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
1201f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
120211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
120311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @hide
120411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
120511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    static public int scaleFromDensity(int size, int sdensity, int tdensity) {
12060b68477f8287fe5ddac1beb1c9d0811ded034dadRomain Guy        if (sdensity == DENSITY_NONE || tdensity == DENSITY_NONE || sdensity == tdensity) {
120711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            return size;
12082784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn        }
1209f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
121011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        // Scale by tdensity / sdensity, rounding up.
1211366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy        return ((size * tdensity) + (sdensity >> 1)) / sdensity;
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1213f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the number of bytes between rows in the bitmap's pixels. Note that
12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this refers to the pixels as stored natively by the bitmap. If you call
12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * getPixels() or setPixels(), then the pixels are uniformly treated as
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * 32bit values, packed according to the Color class.
12199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1220e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, this method
12219f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * should not be used to calculate the memory usage of the bitmap. Instead,
12229f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * see {@link #getAllocationByteCount()}.
12239f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
12249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return number of bytes between rows of the native bitmap pixels.
12259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getRowBytes() {
122701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
122801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getRowBytes() on a recycle()'d bitmap! This is undefined behavior!");
122901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1230775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeRowBytes(mNativePtr);
12319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12328cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
12339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12349f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Returns the minimum number of bytes that can be used to store this bitmap's pixels.
12359f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
1236e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can
12379f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * no longer be used to determine memory usage of a bitmap. See {@link
1238c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #getAllocationByteCount()}.</p>
1239f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson     */
1240f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    public final int getByteCount() {
1241f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        // int result permits bitmaps up to 46,340 x 46,340
1242f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        return getRowBytes() * getHeight();
1243f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    }
1244f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson
1245f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    /**
12469f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Returns the size of the allocated memory used to store this bitmap's pixels.
12479f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
12489f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * <p>This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to
1249c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * decode other bitmaps of smaller size, or by manual reconfiguration. See {@link
1250c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link
1251c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap
1252c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be
1253c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the same as that returned by {@link #getByteCount()}.</p>
1254c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
1255c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>This value will not change over the lifetime of a Bitmap.</p>
12569f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
1257c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
12589f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     */
12599f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    public final int getAllocationByteCount() {
1260222f2ad294c9046b62af5e059f8b409aec044ce9Chris Craik        if (mBuffer == null) {
1261222f2ad294c9046b62af5e059f8b409aec044ce9Chris Craik            // native backed bitmaps don't support reconfiguration,
1262222f2ad294c9046b62af5e059f8b409aec044ce9Chris Craik            // so alloc size is always content size
1263222f2ad294c9046b62af5e059f8b409aec044ce9Chris Craik            return getByteCount();
1264222f2ad294c9046b62af5e059f8b409aec044ce9Chris Craik        }
12659f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik        return mBuffer.length;
12669f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    }
12679f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
12689f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    /**
12699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the bitmap's internal config is in one of the public formats, return
12709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that config, otherwise return null.
12719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final Config getConfig() {
127301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
127401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getConfig() on a recycle()'d bitmap! This is undefined behavior!");
127501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1276775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return Config.nativeToConfig(nativeConfig(mNativePtr));
12779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1279a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    /** Returns true if the bitmap's config supports per-pixel alpha, and
1280a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * if the pixels may contain non-opaque alpha values. For some configs,
1281a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * this is always false (e.g. RGB_565), since they do not support per-pixel
1282a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * alpha. However, for configs that do, the bitmap may be flagged to be
1283a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * known that all of its pixels are opaque. In this case hasAlpha() will
1284a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * also return false. If a config such as ARGB_8888 is not so flagged,
1285a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * it will return true by default.
1286a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     */
12879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean hasAlpha() {
128801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
128901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!");
129001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1291775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeHasAlpha(mNativePtr);
12929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1295a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * Tell the bitmap if all of the pixels are known to be opaque (false)
1296a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * or if some of the pixels may contain non-opaque alpha values (true).
1297366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy     * Note, for some configs (e.g. RGB_565) this call is ignored, since it
1298366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy     * does not support per-pixel alpha values.
1299a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     *
1300a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * This is meant as a drawing hint, as in some cases a bitmap that is known
1301a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * to be opaque can take a faster drawing case than one that may have
1302a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * non-opaque per-pixel alpha values.
1303a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     */
1304a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    public void setHasAlpha(boolean hasAlpha) {
13053e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setHasAlpha called on a recycled bitmap");
1306775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetHasAlpha(mNativePtr, hasAlpha, mRequestPremultiplied);
1307a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    }
1308a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed
1309a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    /**
1310713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Indicates whether the renderer responsible for drawing this
1311713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * bitmap should attempt to use mipmaps when this bitmap is drawn
1312713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * scaled down.
1313f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1314713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * If you know that you are going to draw this bitmap at less than
1315713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * 50% of its original size, you may be able to obtain a higher
1316713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * quality
1317f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1318713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * This property is only a suggestion that can be ignored by the
1319713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * renderer. It is not guaranteed to have any effect.
1320f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1321713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @return true if the renderer should attempt to use mipmaps,
1322713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *         false otherwise
1323f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1324713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @see #setHasMipMap(boolean)
1325713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     */
1326713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    public final boolean hasMipMap() {
132701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
132801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called hasMipMap() on a recycle()'d bitmap! This is undefined behavior!");
132901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1330775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeHasMipMap(mNativePtr);
1331713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    }
1332713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
1333713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    /**
1334713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Set a hint for the renderer responsible for drawing this bitmap
1335713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * indicating that it should attempt to use mipmaps when this bitmap
1336713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * is drawn scaled down.
1337713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1338713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * If you know that you are going to draw this bitmap at less than
1339713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * 50% of its original size, you may be able to obtain a higher
1340713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * quality by turning this property on.
1341f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1342713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Note that if the renderer respects this hint it might have to
1343713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * allocate extra memory to hold the mipmap levels for this bitmap.
1344713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1345713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * This property is only a suggestion that can be ignored by the
1346713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * renderer. It is not guaranteed to have any effect.
1347713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1348713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @param hasMipMap indicates whether the renderer should attempt
1349713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *                  to use mipmaps
1350713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1351713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @see #hasMipMap()
1352713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     */
1353713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    public final void setHasMipMap(boolean hasMipMap) {
13543e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setHasMipMap called on a recycled bitmap");
1355775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetHasMipMap(mNativePtr, hasMipMap);
1356713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    }
1357713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
1358713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    /**
13599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Fills the bitmap's pixels with the specified {@link Color}.
13609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable.
13629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
136380756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void eraseColor(@ColorInt int c) {
13649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't erase a recycled bitmap");
13659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
13669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException("cannot erase immutable bitmaps");
13679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1368775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeErase(mNativePtr, c);
13699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the {@link Color} at the specified location. Throws an exception
13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * if x or y are out of bounds (negative or >= to the width or height
13743849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * respectively). The returned color is a non-premultiplied ARGB value.
13759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x    The x coordinate (0...width-1) of the pixel to return
13779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y    The y coordinate (0...height-1) of the pixel to return
13789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return     The argb {@link Color} at the specified coordinate
13799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y exceed the bitmap's bounds
13809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
138180756e38882720860db52f1fcc21fa1505a02abfTor Norbye    @ColorInt
13829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPixel(int x, int y) {
13839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call getPixel() on a recycled bitmap");
13849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelAccess(x, y);
1385775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeGetPixel(mNativePtr, x, y);
13869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13878cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
13889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns in pixels[] a copy of the data in the bitmap. Each value is
13909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a packed int representing a {@link Color}. The stride parameter allows
13919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the caller to allow for gaps in the returned pixels array between
13929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rows. For normal packed results, just pass width for the stride value.
13933849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * The returned colors are non-premultiplied ARGB values.
13949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
13959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels   The array to receive the bitmap's colors
13969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   The first index to write into pixels[]
13979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   The number of entries in pixels[] to skip between
13989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 rows (must be >= bitmap's width). Can be negative.
13999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel to read from
14009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap
14019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel to read from
14029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap
14039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels to read from each row
14049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows to read
14053849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
14069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y, width, height exceed the
14079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         bounds of the bitmap, or if abs(stride) < width.
14089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
14099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         to receive the specified number of pixels.
14109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
141180756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void getPixels(@ColorInt int[] pixels, int offset, int stride,
14129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          int x, int y, int width, int height) {
14139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call getPixels() on a recycled bitmap");
14149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width == 0 || height == 0) {
14159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // nothing to do
14169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
1418775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeGetPixels(mNativePtr, pixels, offset, stride,
141957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                        x, y, width, height);
14209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14218cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
14229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Shared code to check for illegal arguments passed to getPixel()
14249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setPixel()
1425f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
14269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x x coordinate of the pixel
14279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y y coordinate of the pixel
14289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkPixelAccess(int x, int y) {
14309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
14319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x >= getWidth()) {
14329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x must be < bitmap.width()");
14339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y >= getHeight()) {
14359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y must be < bitmap.height()");
14369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Shared code to check for illegal arguments passed to getPixels()
14419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setPixels()
14429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x left edge of the area of pixels to access
14449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y top edge of the area of pixels to access
14459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width width of the area of pixels to access
14469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height height of the area of pixels to access
14479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset offset into pixels[] array
14489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride number of elements in pixels[] between each logical row
14499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels array to hold the area of pixels being accessed
14509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    */
14519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkPixelsAccess(int x, int y, int width, int height,
14529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   int offset, int stride, int pixels[]) {
14539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
14549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width < 0) {
14559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("width must be >= 0");
14569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (height < 0) {
14589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("height must be >= 0");
14599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x + width > getWidth()) {
14619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException(
14629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    "x + width must be <= bitmap.width()");
14639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y + height > getHeight()) {
14659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException(
14669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    "y + height must be <= bitmap.height()");
14679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (Math.abs(stride) < width) {
14699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("abs(stride) must be >= width");
14709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int lastScanline = offset + (height - 1) * stride;
14729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int length = pixels.length;
14739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (offset < 0 || (offset + width > length)
14749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || lastScanline < 0
14759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || (lastScanline + width > length)) {
14769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
14779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14798cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
14809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14813849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Write the specified {@link Color} into the bitmap (assuming it is
14823849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * mutable) at the x,y coordinate. The color must be a
14833849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * non-premultiplied ARGB value.</p>
14849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x     The x coordinate of the pixel to replace (0...width-1)
14869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y     The y coordinate of the pixel to replace (0...height-1)
14873849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * @param color The ARGB color to write into the bitmap
14883849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
14899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable
14909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y are outside of the bitmap's
14919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         bounds.
14929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
149380756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setPixel(int x, int y, @ColorInt int color) {
14949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call setPixel() on a recycled bitmap");
14959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
14969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException();
14979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelAccess(x, y);
1499775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPixel(mNativePtr, x, y, color);
15009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15018cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15033849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Replace pixels in the bitmap with the colors in the array. Each element
15043849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * in the array is a packed int prepresenting a non-premultiplied ARGB
15053849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * {@link Color}.</p>
15069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels   The colors to write to the bitmap
15089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   The index of the first color to read from pixels[]
15099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   The number of colors in pixels[] to skip between rows.
15109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 Normally this value will be the same as the width of
15119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap, but it can be larger (or negative).
15129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel to write to in
15139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap.
15149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel to write to in
15159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap.
15169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of colors to copy from pixels[] per row
15179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows to write to the bitmap
15183849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
15199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable
15209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y, width, height are outside of
15219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the bitmap's bounds.
15229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
15239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         to receive the specified number of pixels.
15249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
152580756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setPixels(@ColorInt int[] pixels, int offset, int stride,
15263849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy            int x, int y, int width, int height) {
15279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call setPixels() on a recycled bitmap");
15289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
15299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException();
15309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width == 0 || height == 0) {
15329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // nothing to do
15339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
1535775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPixels(mNativePtr, pixels, offset, stride,
153657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                        x, y, width, height);
15379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15388cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final Parcelable.Creator<Bitmap> CREATOR
15409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            = new Parcelable.Creator<Bitmap>() {
15419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
15429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Rebuilds a bitmap previously stored with writeToParcel().
15439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *
15449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * @param p    Parcel object to read the bitmap from
15459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * @return a new bitmap created from the data in the parcel
15469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
15479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Bitmap createFromParcel(Parcel p) {
15489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Bitmap bm = nativeCreateFromParcel(p);
15499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (bm == null) {
15509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                throw new RuntimeException("Failed to unparcel Bitmap");
15519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
15529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return bm;
15539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Bitmap[] newArray(int size) {
15559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return new Bitmap[size];
15569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
15589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * No special parcel contents.
15619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int describeContents() {
1563b6377170960d40e66858d8b4d335a95eac773762Bart Sears        return 0;
15649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write the bitmap and its pixels to the parcel. The bitmap can be
15689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rebuilt from the parcel by calling CREATOR.createFromParcel().
15699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param p    Parcel object to write the bitmap data into
15709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void writeToParcel(Parcel p, int flags) {
15729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't parcel a recycled bitmap");
1573775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (!nativeWriteToParcel(mNativePtr, mIsMutable, mDensity, p)) {
15749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("native writeToParcel failed");
15759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new bitmap that captures the alpha values of the original.
15809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This may be drawn with Canvas.drawBitmap(), where the color(s) will be
15819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * taken from the paint that is passed to the draw call.
15829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return new bitmap containing the alpha channel of the original bitmap.
15849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15851c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye    @CheckResult
15869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap extractAlpha() {
15879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return extractAlpha(null, null);
15889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15898cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new bitmap that captures the alpha values of the original.
15929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * These values may be affected by the optional Paint parameter, which
15939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * can contain its own alpha, and may also contain a MaskFilter which
15949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * could change the actual dimensions of the resulting bitmap (e.g.
15959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a blur maskfilter might enlarge the resulting bitmap). If offsetXY
15969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * is not null, it returns the amount to offset the returned bitmap so
15979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that it will logically align with the original. For example, if the
15989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * paint contains a blur of radius 2, then offsetXY[] would contains
15999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * -2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then
16009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * drawing the original would result in the blur visually aligning with
16019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the original.
1602f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
160396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * <p>The initial density of the returned bitmap is the same as the original's.
1604f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
16059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param paint Optional paint used to modify the alpha values in the
16069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              resulting bitmap. Pass null for default behavior.
16079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offsetXY Optional array that returns the X (index 0) and Y
16089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 (index 1) offset needed to position the returned bitmap
16099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 so that it visually lines up with the original.
16109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return new bitmap containing the (optionally modified by paint) alpha
16119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         channel of the original bitmap. This may be drawn with
16129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         Canvas.drawBitmap(), where the color(s) will be taken from the
16139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         paint that is passed to the draw call.
16149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16151c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye    @CheckResult
16169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap extractAlpha(Paint paint, int[] offsetXY) {
16179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't extractAlpha on a recycled bitmap");
1618dfba4d3d11bbf47dff45f94d61d4d97510b3034aDerek Sollenberger        long nativePaint = paint != null ? paint.getNativeInstance() : 0;
1619775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap bm = nativeExtractAlpha(mNativePtr, nativePaint, offsetXY);
16209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bm == null) {
16219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Failed to extractAlpha on Bitmap");
16229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
162396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        bm.mDensity = mDensity;
16249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
16259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16278cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    /**
162876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  Given another bitmap, return true if it has the same dimensions, config,
162976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  and pixel data as this bitmap. If any of those differ, return false.
163076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  If other is null, return false.
163176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     */
163276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    public boolean sameAs(Bitmap other) {
16339298c5414172344f2462beda70e624af5f774483John Reck        checkRecycled("Can't call sameAs on a recycled bitmap!");
16343df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck        if (this == other) return true;
16353df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck        if (other == null) return false;
16369298c5414172344f2462beda70e624af5f774483John Reck        if (other.isRecycled()) {
16379298c5414172344f2462beda70e624af5f774483John Reck            throw new IllegalArgumentException("Can't compare to a recycled bitmap!");
16389298c5414172344f2462beda70e624af5f774483John Reck        }
1639775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeSameAs(mNativePtr, other.mNativePtr);
164076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
164176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
164276d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    /**
16438cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * Rebuilds any caches associated with the bitmap that are used for
16448cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * drawing it. In the case of purgeable bitmaps, this call will attempt to
16458cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * ensure that the pixels have been decoded.
16468cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * If this is called on more than one bitmap in sequence, the priority is
16478cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * given in LRU order (i.e. the last bitmap called will be given highest
16488cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * priority).
16498cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     *
16508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * For bitmaps with no associated caches, this call is effectively a no-op,
16518cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * and therefore is harmless.
16528cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     */
16538cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    public void prepareToDraw() {
1654f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // TODO: Consider having this start an async upload?
1655f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // With inPurgeable no-op'd there's currently no use for this
1656f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        // method, but it could have interesting future uses.
1657ad57442fd00e7fd7f1885d23372ec5c85e176820John Reck        checkRecycled("Can't prepareToDraw on a recycled bitmap!");
1658f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    }
1659f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
1660c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    /**
1661c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck     * Refs the underlying SkPixelRef and returns a pointer to it.
1662c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck     *
1663c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck     * @hide
1664c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck     * */
1665c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    public final long refSkPixelRef() {
1666ad57442fd00e7fd7f1885d23372ec5c85e176820John Reck        checkRecycled("Can't refSkPixelRef on a recycled bitmap!");
1667f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return nativeRefPixelRef(mNativePtr);
1668c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    }
1669c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck
16709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //////////// native methods
16719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeCreate(int[] colors, int offset,
16739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              int stride, int width, int height,
1674c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik                                              int nativeConfig, boolean mutable);
167536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig,
16769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            boolean isMutable);
1677721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    private static native Bitmap nativeCopyAshmem(long nativeSrcBitmap);
1678775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    private static native long nativeGetNativeFinalizer();
167936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeRecycle(long nativeBitmap);
168036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeReconfigure(long nativeBitmap, int width, int height,
168117a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III                                                 int config, int allocSize,
168217a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III                                                 boolean isPremultiplied);
16839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
168436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeCompress(long nativeBitmap, int format,
16859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            int quality, OutputStream stream,
16869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            byte[] tempStorage);
168736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeErase(long nativeBitmap, int color);
168836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeRowBytes(long nativeBitmap);
168936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeConfig(long nativeBitmap);
16908cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
169157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native int nativeGetPixel(long nativeBitmap, int x, int y);
169236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeGetPixels(long nativeBitmap, int[] pixels,
16931abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik                                               int offset, int stride, int x, int y,
169457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                               int width, int height);
16958cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
169657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetPixel(long nativeBitmap, int x, int y, int color);
169736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeSetPixels(long nativeBitmap, int[] colors,
16981abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik                                               int offset, int stride, int x, int y,
169957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                               int width, int height);
170036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeCopyPixelsToBuffer(long nativeBitmap,
17019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                        Buffer dst);
170236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src);
170336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeGenerationId(long nativeBitmap);
17049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeCreateFromParcel(Parcel p);
17069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // returns true on success
170736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeWriteToParcel(long nativeBitmap,
17089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                      boolean isMutable,
1709de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                                      int density,
17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                      Parcel p);
17119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // returns a new bitmap built from the native bitmap's alpha, and the paint
171236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeExtractAlpha(long nativeBitmap,
171336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat                                                    long nativePaint,
17149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                    int[] offsetXY);
17159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
171636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeHasAlpha(long nativeBitmap);
171757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native boolean nativeIsPremultiplied(long nativeBitmap);
171857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetPremultiplied(long nativeBitmap,
171957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                      boolean isPremul);
172057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetHasAlpha(long nativeBitmap,
172157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                 boolean hasAlpha,
172257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                 boolean requestPremul);
172336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeHasMipMap(long nativeBitmap);
172436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap);
172536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1);
1726c6e2e8ff474ae44bab5b9eb665851118abd27b68John Reck    private static native long nativeRefPixelRef(long nativeBitmap);
17279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1728