Bitmap.java revision 6e3658a63843096058ed444d073fbcd191fd7e1b
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
28a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winsonimport libcore.util.NativeAllocationRegistry;
29a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson
308cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.io.OutputStream;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.Buffer;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.ByteBuffer;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.IntBuffer;
348cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.nio.ShortBuffer;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
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
51f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    // Convenience for JNI access
52f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    private final long mNativePtr;
53e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final boolean mIsMutable;
551abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
561abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    /**
5757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * Represents whether the Bitmap's content is requested to be pre-multiplied.
581abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Note that isPremultiplied() does not directly return this value, because
5957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * isPremultiplied() may never return true for a 565 Bitmap or a bitmap
6057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * without alpha.
611abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
621abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * setPremultiplied() does directly set the value so that setConfig() and
631abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * setPremultiplied() aren't order dependent, despite being setters.
6457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     *
6557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * The native bitmap's premultiplication state is kept up to date by
6657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * pushing down this preference for every config change.
671abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     */
6857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private boolean mRequestPremultiplied;
698790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
7047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    private byte[] mNinePatchChunk; // may be null
7147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    private NinePatch.InsetStruct mNinePatchInsets; // may be null
721abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    private int mWidth;
731abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    private int mHeight;
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean mRecycled;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
76caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck    /** @hide */
77caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck    public int mDensity = getDefaultDensity();
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    private static volatile Matrix sScaleMatrix;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    private static volatile int sDefaultDensity = -1;
8202890fd0f98b3b8d98baf0bda1ea906afd723d8bRomain Guy
8396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    /**
8496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * For backwards compatibility, allows the app layer to change the default
8596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * density when running old apps.
8696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * @hide
8796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     */
8896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    public static void setDefaultDensity(int density) {
8996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        sDefaultDensity = density;
9096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    }
913849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy
92f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    @SuppressWarnings("deprecation")
933849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    static int getDefaultDensity() {
9496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        if (sDefaultDensity >= 0) {
9596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn            return sDefaultDensity;
9696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        }
9796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        sDefaultDensity = DisplayMetrics.DENSITY_DEVICE;
9896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        return sDefaultDensity;
9996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn    }
100dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1029f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Private constructor that must received an already allocated native bitmap
1039f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * int (pointer).
104ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     */
105f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    // called from JNI
1064508218850faedea95371188da587b6734f5f3dasergeyv    Bitmap(long nativeBitmap, int width, int height, int density,
10757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            boolean isMutable, boolean requestPremultiplied,
10847cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            byte[] ninePatchChunk, NinePatch.InsetStruct ninePatchInsets) {
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (nativeBitmap == 0) {
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("internal error: native bitmap is 0");
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1128cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
1131abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mWidth = width;
1141abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mHeight = height;
1151abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mIsMutable = isMutable;
11657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = requestPremultiplied;
1174cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNinePatchChunk = ninePatchChunk;
11947cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        mNinePatchInsets = ninePatchInsets;
120de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn        if (density >= 0) {
121de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn            mDensity = density;
122de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn        }
1234cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik
124f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        mNativePtr = nativeBitmap;
1254508218850faedea95371188da587b6734f5f3dasergeyv        long nativeSize = NATIVE_ALLOCATION_SIZE + getAllocationByteCount();
126775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        NativeAllocationRegistry registry = new NativeAllocationRegistry(
1270ebdbb4a8470bb77f38c33a42c220ecb8a7adcc6Richard Uhler            Bitmap.class.getClassLoader(), nativeGetNativeFinalizer(), nativeSize);
128775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        registry.registerNativeAllocation(this, nativeBitmap);
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1324c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger     * Return the pointer to the native object.
133caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck     * @hide
1344c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger     */
135caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck    public long getNativeInstance() {
1364c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        return mNativePtr;
1374c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    }
1384c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
1394c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    /**
1401abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Native bitmap has been reconfigured, so set premult and cached
1411abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * width/height values
1429f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     */
1439505a6552764461c22ce48f1ac13d025d23e1579Romain Guy    @SuppressWarnings("unused") // called from JNI
14457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    void reinit(int width, int height, boolean requestPremultiplied) {
1451abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mWidth = width;
1461abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        mHeight = height;
14757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = requestPremultiplied;
1489f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    }
1499f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
1509f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    /**
15111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * <p>Returns the density for this bitmap.</p>
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * <p>The default density is the same density as the current display,
15496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * unless the current application does not support different screen
15596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * densities in which case it is
15696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * {@link android.util.DisplayMetrics#DENSITY_DEFAULT}.  Note that
15796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * compatibility mode is determined by the application that was initially
15896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * loaded into a process -- applications that share the same process should
15996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * all have the same compatibility, or ensure they explicitly set the
16096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * density of their bitmaps appropriately.</p>
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * @return A scaling factor of the default density or {@link #DENSITY_NONE}
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         if the scaling factor is unknown.
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #setDensity(int)
166a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn     * @see android.util.DisplayMetrics#DENSITY_DEFAULT
16711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see android.util.DisplayMetrics#densityDpi
16811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #DENSITY_NONE
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getDensity() {
17101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
17201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getDensity() on a recycle()'d bitmap! This is undefined behavior!");
17301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
17411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return mDensity;
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * <p>Specifies the density for this bitmap.  When the bitmap is
17911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * drawn to a Canvas that also has a density, it will be scaled
18011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * appropriately.</p>
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
18211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param density The density scaling factor to use with this bitmap or
18311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     *        {@link #DENSITY_NONE} if the density is unknown.
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
18511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #getDensity()
186a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn     * @see android.util.DisplayMetrics#DENSITY_DEFAULT
18711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see android.util.DisplayMetrics#densityDpi
18811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @see #DENSITY_NONE
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public void setDensity(int density) {
19111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        mDensity = density;
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
193c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
194c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
195c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Modifies the bitmap to have a specified width, height, and {@link
196c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * Config}, without affecting the underlying allocation backing the bitmap.
197c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * Bitmap pixel data is not re-initialized for the new configuration.</p>
198c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
199c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>This method can be used to avoid allocating a new bitmap, instead
200c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * reusing an existing bitmap's allocation for a new configuration of equal
201c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * or lesser size. If the Bitmap's allocation isn't large enough to support
202c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the new configuration, an IllegalArgumentException will be thrown and the
203c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * bitmap will not be modified.</p>
204c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
205c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>The result of {@link #getByteCount()} will reflect the new configuration,
206c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * while {@link #getAllocationByteCount()} will reflect that of the initial
207c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * configuration.</p>
208c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
20917a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * <p>Note: This may change this result of hasAlpha(). When converting to 565,
21017a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * the new bitmap will always be considered opaque. When converting from 565,
21117a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * the new bitmap will be considered non-opaque, and will respect the value
21217a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     * set by setPremultiplied().</p>
21317a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III     *
2146d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <p>WARNING: This method should NOT be called on a bitmap currently in use
2156d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * by the view system, Canvas, or the AndroidBitmap NDK API. It does not
2166d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * make guarantees about how the underlying pixel buffer is remapped to the
2176d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * new config, just that the allocation is reused. Additionally, the view
2186d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * system does not account for bitmap properties being modifying during use,
2196d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * e.g. while attached to drawables.</p>
2206d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *
2216d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <p>In order to safely ensure that a Bitmap is no longer in use by the
2226d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * View system it is necessary to wait for a draw pass to occur after
2236d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * invalidate()'ing any view that had previously drawn the Bitmap in the last
2246d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * draw pass due to hardware acceleration's caching of draw commands. As
2256d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * an example, here is how this can be done for an ImageView:
2266d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * <pre class="prettyprint">
2276d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      ImageView myImageView = ...;
2286d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      final Bitmap myBitmap = ...;
2296d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      myImageView.setImageDrawable(null);
2306d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      myImageView.post(new Runnable() {
2316d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *          public void run() {
2326d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              // myBitmap is now no longer in use by the ImageView
2336d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              // and can be safely reconfigured.
2346d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *              myBitmap.reconfigure(...);
2356d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *          }
2366d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     *      });
2376d86e57bdadd56be160eb3e543cc53622cae46f2John Reck     * </pre></p>
238c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
239c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
240c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
241c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
242c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
243c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void reconfigure(int width, int height, Config config) {
244c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        checkRecycled("Can't call reconfigure() on a recycled bitmap");
245c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (width <= 0 || height <= 0) {
246c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik            throw new IllegalArgumentException("width and height must be > 0");
247c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
248c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        if (!isMutable()) {
2499c2cde0583d02ba7551877dbe7a5ecaee6defb56Chris Craik            throw new IllegalStateException("only mutable bitmaps may be reconfigured");
250c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        }
251c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
2524508218850faedea95371188da587b6734f5f3dasergeyv        nativeReconfigure(mNativePtr, width, height, config.nativeInt, mRequestPremultiplied);
253c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        mWidth = width;
254c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        mHeight = height;
255c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
256c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
257c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
258c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
259c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current height and config.</p>
260c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
261c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
262c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
263c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
264c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
265c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
266c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
267c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
268c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
269c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setWidth(int width) {
270c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(width, getHeight(), getConfig());
271c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
272c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
273c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
274c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
275c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current width and config.</p>
276c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
277c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
278c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
279c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
280c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
281c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
282c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
283c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setConfig(Config)
284c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
285c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setHeight(int height) {
286c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(getWidth(), height, getConfig());
287c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
288c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
289c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    /**
290c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>Convenience method for calling {@link #reconfigure(int, int, Config)}
291c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * with the current height and width.</p>
292c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
293c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>WARNING: this method should not be used on bitmaps currently used by
294c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the view system, see {@link #reconfigure(int, int, Config)} for more
295c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * details.</p>
296c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
297c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
298c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setWidth(int)
299c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #setHeight(int)
300c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     */
301c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    public void setConfig(Config config) {
302c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik        reconfigure(getWidth(), getHeight(), config);
303c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik    }
304c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the nine patch chunk.
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param chunk The definition of the nine patch
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setNinePatchChunk(byte[] chunk) {
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNinePatchChunk = chunk;
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3158cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
317a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * Free the native object associated with this bitmap, and clear the
318a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * reference to the pixel data. This will not free the pixel data synchronously;
319a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * it simply allows it to be garbage collected if there are no other references.
320a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * The bitmap is marked as "dead", meaning it will throw an exception if
321a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * getPixels() or setPixels() is called, and will draw nothing. This operation
322a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy     * cannot be reversed, so it should only be called if you are sure there are no
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * further uses for the bitmap. This is an advanced call, and normally need
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * not be called, since the normal GC process will free up this memory when
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * there are no more references to this bitmap.
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void recycle() {
328775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (!mRecycled && mNativePtr != 0) {
329775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            if (nativeRecycle(mNativePtr)) {
330547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // return value indicates whether native pixel object was actually recycled.
331547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // false indicates that it is still in use at the native level and these
332547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // objects should not be collected now. They will be collected later when the
333547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                // Bitmap itself is collected.
334547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase                mNinePatchChunk = null;
335547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase            }
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mRecycled = true;
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true if this bitmap has been recycled. If so, then it is an error
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to try to access its pixels, and the bitmap will not draw.
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the bitmap has been recycled
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isRecycled() {
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mRecycled;
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3498cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3510bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * Returns the generation ID of this bitmap. The generation ID changes
3520bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * whenever the bitmap is modified. This can be used as an efficient way to
3530bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * check if a bitmap has changed.
354f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
3550bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     * @return The current generation ID for this bitmap.
3560bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy     */
3570bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    public int getGenerationId() {
35801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
35901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getGenerationId() on a recycle()'d bitmap! This is undefined behavior!");
36001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
361775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeGenerationId(mNativePtr);
3620bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
363f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
3640bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    /**
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This is called by methods that want to throw an exception if the bitmap
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * has already been recycled.
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkRecycled(String errorMessage) {
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mRecycled) {
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException(errorMessage);
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3738cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
375980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * This is called by methods that want to throw an exception if the bitmap
376980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * is {@link Config#HARDWARE}.
377980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     */
378980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv    private void checkHardware(String errorMessage) {
379980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        if (getConfig() == Config.HARDWARE) {
380980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv            throw new IllegalStateException(errorMessage);
381980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        }
382980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv    }
383980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv
384980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv    /**
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Common code for checking that x and y are >= 0
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x x coordinate to ensure is >= 0
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y y coordinate to ensure is >= 0
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void checkXYSign(int x, int y) {
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x < 0) {
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x must be >= 0");
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y < 0) {
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y must be >= 0");
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Common code for checking that width and height are > 0
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width  width to ensure is > 0
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height height to ensure is > 0
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void checkWidthHeight(int width, int height) {
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width <= 0) {
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("width must be > 0");
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (height <= 0) {
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("height must be > 0");
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
414676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy    /**
415676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * Possible bitmap configurations. A bitmap configuration describes
416676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * how pixels are stored. This affects the quality (color depth) as
417676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     * well as the ability to display transparent/translucent colors.
418676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy     */
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum Config {
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // these native values must match up with the enum in SkBitmap.h
421676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
422676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
423676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored as a single translucency (alpha) channel.
424676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This is very useful to efficiently store masks for instance.
425676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * No color information is stored.
426676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * With this configuration, each pixel requires 1 byte of memory.
427676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
428b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ALPHA_8     (1),
429676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
430676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
431676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 2 bytes and only the RGB channels are
432676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * encoded: red is stored with 5 bits of precision (32 possible
433676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * values), green is stored with 6 bits of precision (64 possible
434676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * values) and blue is stored with 5 bits of precision.
435f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
436676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration can produce slight visual artifacts depending
437676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * on the configuration of the source. For instance, without
438676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * dithering, the result might show a greenish tint. To get better
439676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * results dithering should be applied.
440f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
441676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration may be useful when using opaque bitmaps
442676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * that do not require high color fidelity.
443676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
444b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        RGB_565     (3),
445676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
446676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
447676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 2 bytes. The three RGB color channels
448676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * and the alpha channel (translucency) are stored with a 4 bits
449676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * precision (16 possible values.)
450f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
451676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration is mostly useful if the application needs
452676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * to store translucency information but also needs to save
453676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * memory.
454f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
455676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * It is recommended to use {@link #ARGB_8888} instead of this
456676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * configuration.
45725ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         *
458e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase         * Note: as of {@link android.os.Build.VERSION_CODES#KITKAT},
45925ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * any bitmap created with this configuration will be created
46025ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * using {@link #ARGB_8888} instead.
461f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
462676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * @deprecated Because of the poor quality of this configuration,
463676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         *             it is advised to use {@link #ARGB_8888} instead.
464676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
465676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        @Deprecated
466b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        ARGB_4444   (4),
467676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
468676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        /**
469676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * Each pixel is stored on 4 bytes. Each channel (RGB and alpha
470676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * for translucency) is stored with 8 bits of precision (256
471676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * possible values.)
472f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck         *
473676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * This configuration is very flexible and offers the best
474676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         * quality. It should be used whenever possible.
475676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy         */
476da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv        ARGB_8888   (5),
477da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv
4789505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        /**
4799505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * Each pixels is stored on 8 bytes. Each channel (RGB and alpha
4809505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * for translucency) is stored as a
4819505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * {@link android.util.Half half-precision floating point value}.
4829505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         *
4839505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * This configuration is particularly suited for wide-gamut and
4849505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         * HDR content.
4859505a6552764461c22ce48f1ac13d025d23e1579Romain Guy         */
4869505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        RGBA_F16    (6),
487da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv
488da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv        /**
4899fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * Special configuration, when bitmap is stored only in graphic memory.
4909fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * Bitmaps in this configuration are always immutable.
4919fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         *
4929fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * It is optimal for cases, when the only operation with the bitmap is to draw it on a
4939fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * screen.
494da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv         */
4959505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        HARDWARE    (7);
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
497676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        final int nativeInt;
498676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy
499676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        private static Config sConfigs[] = {
5009505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE
501676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        };
502f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Config(int ni) {
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            this.nativeInt = ni;
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5068cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
507676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy        static Config nativeToConfig(int ni) {
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return sConfigs[ni];
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5118cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5133849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Copy the bitmap's pixels into the specified buffer (allocated by the
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * caller). An exception is thrown if the buffer is not large enough to
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * hold all of the pixels (taking into account the number of bytes per
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pixel) or if the Buffer subclass is not one of the support types
5173849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * (ByteBuffer, ShortBuffer, IntBuffer).</p>
5183849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>The content of the bitmap is copied into the buffer as-is. This means
5193849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * that if this bitmap stores its pixels pre-multiplied
5203849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * (see {@link #isPremultiplied()}, the values in the buffer will also be
5213849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * pre-multiplied.</p>
5221f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * <p>After this method returns, the current position of the buffer is
5231f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * updated: the position is incremented by the number of elements written
5241f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * in the buffer.</p>
525980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void copyPixelsToBuffer(Buffer dst) {
528980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to copyPixelsToBuffer, "
529980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv                + "pixel access is not supported on Config#HARDWARE bitmaps");
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int elements = dst.remaining();
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int shift;
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (dst instanceof ByteBuffer) {
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 0;
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (dst instanceof ShortBuffer) {
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 1;
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (dst instanceof IntBuffer) {
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 2;
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("unsupported Buffer subclass");
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5418cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        long bufferSize = (long)elements << shift;
543f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        long pixelSize = getByteCount();
5448cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bufferSize < pixelSize) {
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Buffer not large enough for pixels");
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5488cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
549775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeCopyPixelsToBuffer(mNativePtr, dst);
5508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // now update the buffer's position
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int position = dst.position();
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        position += pixelSize >> shift;
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst.position(position);
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5583849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Copy the pixels from the buffer, beginning at the current position,
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * overwriting the bitmap's pixels. The data in the buffer is not changed
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in any way (unlike setPixels(), which converts from unpremultipled 32bit
5613849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * to whatever the bitmap's native format is.</p>
5621f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * <p>After this method returns, the current position of the buffer is
5631f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * updated: the position is incremented by the number of elements read from
5641f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * the buffer. If you need to read the bitmap from the buffer again you must
5651f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy     * first rewind the buffer.</p>
566980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void copyPixelsFromBuffer(Buffer src) {
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("copyPixelsFromBuffer called on recycled bitmap");
570980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to copyPixelsFromBuffer, Config#HARDWARE bitmaps are immutable");
5718cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int elements = src.remaining();
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int shift;
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (src instanceof ByteBuffer) {
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 0;
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (src instanceof ShortBuffer) {
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 1;
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (src instanceof IntBuffer) {
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            shift = 2;
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("unsupported Buffer subclass");
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5838cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5843849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy        long bufferBytes = (long) elements << shift;
585f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        long bitmapBytes = getByteCount();
5868cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bufferBytes < bitmapBytes) {
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Buffer not large enough for pixels");
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5908cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
591775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeCopyPixelsFromBuffer(mNativePtr, src);
59255adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen
59355adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        // now update the buffer's position
59455adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        int position = src.position();
59555adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        position += bitmapBytes >> shift;
59655adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen        src.position(position);
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5988cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Tries to make a new bitmap based on the dimensions of this bitmap,
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * setting the new bitmap's config to the one specified, and then copying
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this bitmap's pixels into the new bitmap. If the conversion is not
60396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * supported, or the allocator fails, then this returns NULL.  The returned
60496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * bitmap initially has the same density as the original.
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config    The desired config for the resulting bitmap
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param isMutable True if the resulting bitmap should be mutable (i.e.
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                  its pixels can be modified)
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the new bitmap, or null if the copy could not be made.
61005126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv     * @throws IllegalArgumentException if config is {@link Config#HARDWARE} and isMutable is true
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap copy(Config config, boolean isMutable) {
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't copy a recycled bitmap");
61405126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv        if (config == Config.HARDWARE && isMutable) {
61505126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv            throw new IllegalArgumentException("Hardware bitmaps are always immutable");
61605126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv        }
617775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable);
61896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        if (b != null) {
61957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III            b.setPremultiplied(mRequestPremultiplied);
62096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn            b.mDensity = mDensity;
62196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        }
62296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        return b;
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
62502d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick    /**
626721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * Creates a new immutable bitmap backed by ashmem which can efficiently
627721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * be passed between processes.
628721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     *
629721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     * @hide
630721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews     */
631721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    public Bitmap createAshmemBitmap() {
632721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        checkRecycled("Can't copy a recycled bitmap");
633775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap b = nativeCopyAshmem(mNativePtr);
634721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        if (b != null) {
635721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews            b.setPremultiplied(mRequestPremultiplied);
636721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews            b.mDensity = mDensity;
637721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        }
638721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews        return b;
639721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    }
640721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews
641721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    /**
642a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     * Creates a new immutable bitmap backed by ashmem which can efficiently
643a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     * be passed between processes.
644a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     *
645a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     * @hide
646a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson     */
647a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    public Bitmap createAshmemBitmap(Config config) {
648a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        checkRecycled("Can't copy a recycled bitmap");
649a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        Bitmap b = nativeCopyAshmemConfig(mNativePtr, config.nativeInt);
650a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        if (b != null) {
651a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson            b.setPremultiplied(mRequestPremultiplied);
652a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson            b.mDensity = mDensity;
653a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        }
654a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson        return b;
655a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    }
656a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson
657a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    /**
6580a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     * Create hardware bitmap backed GraphicBuffer.
6590a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     *
6600a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     * @return Bitmap or null if this GraphicBuffer has unsupported PixelFormat.
6610a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     *         currently PIXEL_FORMAT_RGBA_8888 is the only supported format
6620a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     * @hide
6630a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv     */
6640a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    public static Bitmap createHardwareBitmap(GraphicBuffer graphicBuffer) {
6650a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv        return nativeCreateHardwareBitmap(graphicBuffer);
6660a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    }
6670a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv
6680a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    /**
669f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
670f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     * specified width and height are the same as the current width and height of
6719f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * the source bitmap, the source bitmap is returned and no new bitmap is
672f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * created.
67302d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     *
67402d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param src       The source bitmap.
67502d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param dstWidth  The new bitmap's desired width.
67602d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param dstHeight The new bitmap's desired height.
67702d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     * @param filter    true if the source should be filtered.
678f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * @return The new scaled bitmap or the source bitmap if no scaling is required.
679790552b240c05d58407f7c14acba656b2e85c523Romain Guy     * @throws IllegalArgumentException if width is <= 0, or height is <= 0
68002d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick     */
681f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy    public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
682f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy            boolean filter) {
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Matrix m;
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (Bitmap.class) {
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // small pool of just 1 matrix
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m = sScaleMatrix;
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            sScaleMatrix = null;
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (m == null) {
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m = new Matrix();
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6938cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int width = src.getWidth();
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int height = src.getHeight();
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final float sx = dstWidth  / (float)width;
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final float sy = dstHeight / (float)height;
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        m.setScale(sx, sy);
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized (Bitmap.class) {
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // do we need to check for null? why not just assign everytime?
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (sScaleMatrix == null) {
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                sScaleMatrix = m;
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7088cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen        return b;
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7108cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from the source bitmap. The new bitmap may
71396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * be the same object as source, or a copy may have been made.  It is
71496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initialized with the same density as the original bitmap.
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(Bitmap src) {
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from the specified subset of the source
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * bitmap. The new bitmap may be the same object as source, or a copy may
723f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * have been made. It is initialized with the same density as the original
724f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * bitmap.
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param source   The bitmap we are subsetting
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel in source
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel in source
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels in each row
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows
731f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * @return A copy of a subset of the source bitmap or the source bitmap itself.
732790552b240c05d58407f7c14acba656b2e85c523Romain Guy     * @throws IllegalArgumentException if the x, y, width, height values are
733790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         outside of the dimensions of the source bitmap, or width is <= 0,
734790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         or height is <= 0
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return createBitmap(source, x, y, width, height, null, false);
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7398cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an immutable bitmap from subset of the source bitmap,
742f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * transformed by the optional matrix. The new bitmap may be the
743f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * same object as source, or a copy may have been made. It is
74496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initialized with the same density as the original bitmap.
745f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
746f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * If the source bitmap is immutable and the requested subset is the
747f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * same as the source bitmap itself, then the source bitmap is
748f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy     * returned and no new bitmap is created.
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param source   The bitmap we are subsetting
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel in source
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel in source
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels in each row
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows
75560b88edea7132ddce90f2dced07c6706f1502270Ken Shirriff     * @param m        Optional matrix to be applied to the pixels
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param filter   true if the source should be filtered.
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   Only applies if the matrix contains more than just
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   translation.
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return A bitmap that represents the specified subset of source
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the x, y, width, height values are
761790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         outside of the dimensions of the source bitmap, or width is <= 0,
762790552b240c05d58407f7c14acba656b2e85c523Romain Guy     *         or height is <= 0
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Matrix m, boolean filter) {
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkWidthHeight(width, height);
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x + width > source.getWidth()) {
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x + width must be <= bitmap.width()");
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y + height > source.getHeight()) {
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y + height must be <= bitmap.height()");
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // check if we can just return our argument unchanged
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() &&
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                height == source.getHeight() && (m == null || m.isIdentity())) {
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return source;
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7818cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
78281f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        boolean isHardware = source.getConfig() == Config.HARDWARE;
78381f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        if (isHardware) {
78481f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv            source = nativeCopyPreserveInternalConfig(source.mNativePtr);
78581f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        }
78681f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int neww = width;
7889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int newh = height;
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Canvas canvas = new Canvas();
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bitmap;
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Paint paint;
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Rect srcR = new Rect(x, y, x + width, y + height);
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        RectF dstR = new RectF(0, 0, width, height);
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
796feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        Config newConfig = Config.ARGB_8888;
797feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        final Config config = source.getConfig();
798feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        // GIF files generate null configs, assume ARGB_8888
799feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        if (config != null) {
800feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy            switch (config) {
801feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case RGB_565:
802feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.RGB_565;
803feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
804feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ALPHA_8:
805feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.ALPHA_8;
806feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
8079505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                case RGBA_F16:
8089505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                    newConfig = Config.RGBA_F16;
8099505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                    break;
810676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy                //noinspection deprecation
811feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ARGB_4444:
812feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                case ARGB_8888:
813feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                default:
814feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    newConfig = Config.ARGB_8888;
815feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy                    break;
816feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy            }
817feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy        }
818d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (m == null || m.isIdentity()) {
820d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha());
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint = null;   // not needed
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
823d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            final boolean transformed = !m.rectStaysRect();
824d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            RectF deviceR = new RectF();
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            m.mapRect(deviceR, dstR);
827d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            neww = Math.round(deviceR.width());
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            newh = Math.round(deviceR.height());
830d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8319505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            Config transformedConfig = newConfig;
8329505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            if (transformed) {
8339505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                if (transformedConfig != Config.ARGB_8888 && transformedConfig != Config.RGBA_F16) {
8349505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                    transformedConfig = Config.ARGB_8888;
8359505a6552764461c22ce48f1ac13d025d23e1579Romain Guy                }
8369505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            }
8379505a6552764461c22ce48f1ac13d025d23e1579Romain Guy            bitmap = createBitmap(neww, newh, transformedConfig, transformed || source.hasAlpha());
838d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas.translate(-deviceR.left, -deviceR.top);
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas.concat(m);
841d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint = new Paint();
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            paint.setFilterBitmap(filter);
844d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy            if (transformed) {
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                paint.setAntiAlias(true);
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8488790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // The new bitmap was created from a known bitmap source so assume that
85011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        // they use the same density
85111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        bitmap.mDensity = source.mDensity;
85257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        bitmap.setHasAlpha(source.hasAlpha());
85357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        bitmap.setPremultiplied(source.mRequestPremultiplied);
8548790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III
85596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        canvas.setBitmap(bitmap);
85696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        canvas.drawBitmap(source, srcR, dstR, paint);
8576311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn        canvas.setBitmap(null);
85881f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        if (isHardware) {
85981f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv            return bitmap.copy(Config.HARDWARE, false);
86081f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv        }
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bitmap;
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8638cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
86596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
86696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initial density is as per {@link #getDensity}.
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create.
8719fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
8729fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(int width, int height, Config config) {
875d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        return createBitmap(width, height, config, true);
876d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    }
877d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy
878d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    /**
879d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * Returns a mutable bitmap with the specified width and height.  Its
880dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
881dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
882dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
883dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
884dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
885dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
886dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create.
8879fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
8889fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
889dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
890dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    public static Bitmap createBitmap(DisplayMetrics display, int width,
891dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            int height, Config config) {
892dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(display, width, height, config, true);
893dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
894dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
895dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
896dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
897d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * initial density is as per {@link #getDensity}.
898d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     *
899d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param width    The width of the bitmap
900d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param height   The height of the bitmap
901d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     * @param config   The bitmap config to create.
9029505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to
9039505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     *                 mark the bitmap as opaque. Doing so will clear the bitmap in black
904f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *                 instead of transparent.
905f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
9069fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
9079fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
908d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy     */
9099505a6552764461c22ce48f1ac13d025d23e1579Romain Guy    public static Bitmap createBitmap(int width, int height, Config config, boolean hasAlpha) {
910dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, width, height, config, hasAlpha);
911dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
912dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
913dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
914dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a mutable bitmap with the specified width and height.  Its
915dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
916dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
917dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
918dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
919dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
920dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
921dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create.
9229505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to
9239505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     *                 mark the bitmap as opaque. Doing so will clear the bitmap in black
924f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *                 instead of transparent.
925f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
9269fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if the width or height are <= 0, or if
9279fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         Config is Config.HARDWARE, because hardware bitmaps are always immutable
928dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
9299505a6552764461c22ce48f1ac13d025d23e1579Romain Guy    public static Bitmap createBitmap(DisplayMetrics display, int width, int height,
930dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            Config config, boolean hasAlpha) {
931e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        if (width <= 0 || height <= 0) {
932e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            throw new IllegalArgumentException("width and height must be > 0");
933e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        }
9349fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        if (config == Config.HARDWARE) {
9359fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv            throw new IllegalArgumentException("can't create mutable bitmap with Config.HARDWARE");
9369fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        }
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);
938dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        if (display != null) {
939dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            bm.mDensity = display.densityDpi;
940dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        }
9418790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III        bm.setHasAlpha(hasAlpha);
9429505a6552764461c22ce48f1ac13d025d23e1579Romain Guy        if ((config == Config.ARGB_8888 || config == Config.RGBA_F16) && !hasAlpha) {
943775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler            nativeErase(bm.mNativePtr, 0xff000000);
944d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy        }
9453b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // No need to initialize the bitmap to zeroes with other configs;
9463b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // it is backed by a VM byte array which is by definition preinitialized
9473b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        // to all zeroes.
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9508cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a immutable bitmap with the specified width and height, with each
95396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
95496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initial density is as per {@link #getDensity}.
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
9569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param colors   Array of {@link Color} used to initialize the pixels.
9579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   Number of values to skip before the first color in the
9589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 array of colors.
9599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   Number of colors in the array between rows (must be >=
9609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 width or <= -width).
9619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
9629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
9639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create. If the config does not
9649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
9659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 bytes in the colors[] will be ignored (assumed to be FF)
9669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0, or if
9679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the color array's length is less than the number of pixels.
9689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(int colors[], int offset, int stride,
9709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int width, int height, Config config) {
971dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, colors, offset, stride, width, height, config);
972dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
973dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
974dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
975dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a immutable bitmap with the specified width and height, with each
976dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
977dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
978dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
979dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
980dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
981dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param colors   Array of {@link Color} used to initialize the pixels.
982dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param offset   Number of values to skip before the first color in the
983dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 array of colors.
984dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param stride   Number of colors in the array between rows (must be >=
985dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 width or <= -width).
986dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
987dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
988dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create. If the config does not
989dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
990dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bytes in the colors[] will be ignored (assumed to be FF)
991dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0, or if
992dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *         the color array's length is less than the number of pixels.
993dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
994dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    public static Bitmap createBitmap(DisplayMetrics display, int colors[],
995dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            int offset, int stride, int width, int height, Config config) {
9969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkWidthHeight(width, height);
9989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (Math.abs(stride) < width) {
9999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("abs(stride) must be >= width");
10009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int lastScanline = offset + (height - 1) * stride;
10029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int length = colors.length;
10039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
10049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (lastScanline + width > length)) {
10059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
10069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1007e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        if (width <= 0 || height <= 0) {
1008e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy            throw new IllegalArgumentException("width and height must be > 0");
1009e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy        }
1010dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        Bitmap bm = nativeCreate(colors, offset, stride, width, height,
10119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            config.nativeInt, false);
1012dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        if (display != null) {
1013dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            bm.mDensity = display.densityDpi;
1014dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        }
1015dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return bm;
10169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a immutable bitmap with the specified width and height, with each
102096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
102196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * initial density is as per {@link #getDensity}.
10229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param colors   Array of {@link Color} used to initialize the pixels.
10249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 This array must be at least as large as width * height.
10259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The width of the bitmap
10269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The height of the bitmap
10279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param config   The bitmap config to create. If the config does not
10289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
10299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 bytes in the colors[] will be ignored (assumed to be FF)
10309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if the width or height are <= 0, or if
10319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the color array's length is less than the number of pixels.
10329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap createBitmap(int colors[], int width, int height, Config config) {
1034dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(null, colors, 0, width, width, height, config);
1035dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    }
1036dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn
1037dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    /**
1038dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * Returns a immutable bitmap with the specified width and height, with each
1039dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * pixel value set to the corresponding value in the colors array.  Its
1040dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * initial density is determined from the given {@link DisplayMetrics}.
1041dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *
1042dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param display  Display metrics for the display this bitmap will be
1043dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 drawn on.
1044dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param colors   Array of {@link Color} used to initialize the pixels.
1045dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 This array must be at least as large as width * height.
1046dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param width    The width of the bitmap
1047dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param height   The height of the bitmap
1048dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @param config   The bitmap config to create. If the config does not
1049dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 support per-pixel alpha (e.g. RGB_565), then the alpha
1050dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *                 bytes in the colors[] will be ignored (assumed to be FF)
1051dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     * @throws IllegalArgumentException if the width or height are <= 0, or if
1052dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     *         the color array's length is less than the number of pixels.
1053dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn     */
1054dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn    public static Bitmap createBitmap(DisplayMetrics display, int colors[],
1055dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn            int width, int height, Config config) {
1056dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn        return createBitmap(display, colors, 0, width, width, height, config);
10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns an optional array of private data, used by the UI system for
10619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * some bitmaps. Not intended to be called by applications.
10629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public byte[] getNinePatchChunk() {
10649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mNinePatchChunk;
10659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1068c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     * Populates a rectangle with the bitmap's optical insets.
1069c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     *
1070c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette     * @param outInsets Rect to populate with optical insets
1071ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     * @hide
1072ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani     */
1073c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette    public void getOpticalInsets(@NonNull Rect outInsets) {
107447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        if (mNinePatchInsets == null) {
1075c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette            outInsets.setEmpty();
1076c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette        } else {
107747cd8e921db73e894f94ec4729ade90da50996f5Chris Craik            outInsets.set(mNinePatchInsets.opticalRect);
1078c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette        }
1079ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    }
1080ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani
108147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    /** @hide */
108247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    public NinePatch.InsetStruct getNinePatchInsets() {
108347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik        return mNinePatchInsets;
108447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik    }
108547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik
1086ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani    /**
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Specifies the known formats a bitmap can be compressed into
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public enum CompressFormat {
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JPEG    (0),
10912305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        PNG     (1),
10922305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora        WEBP    (2);
10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        CompressFormat(int nativeInt) {
10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            this.nativeInt = nativeInt;
10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int nativeInt;
10989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Number of bytes of temp storage we use for communicating between the
11029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * native compressor and the java OutputStream.
11039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final static int WORKING_COMPRESS_STORAGE = 4096;
11059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write a compressed version of the bitmap to the specified outputstream.
11089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If this returns true, the bitmap can be reconstructed by passing a
11099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * corresponding inputstream to BitmapFactory.decodeStream(). Note: not
11109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * all Formats support all bitmap configs directly, so it is possible that
11119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the returned bitmap from BitmapFactory could be in a different bitdepth,
11129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque
11139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pixels).
11149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param format   The format of the compressed image
11169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
11179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 small size, 100 meaning compress for max quality. Some
11189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 formats, like PNG which is lossless, will ignore the
11199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 quality setting
11209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stream   The outputstream to write the compressed data.
11219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if successfully compressed to the specified stream.
11229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean compress(CompressFormat format, int quality, OutputStream stream) {
11249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't compress a recycled bitmap");
11259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // do explicit check before calling the native method
11269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (stream == null) {
11279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new NullPointerException();
11289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (quality < 0 || quality > 100) {
11309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("quality must be 0..100");
11319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11320df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress");
1133775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        boolean result = nativeCompress(mNativePtr, format.nativeInt,
1134f29ed28c7b878ef28058bc730715d0d32445bc57John Reck                quality, stream, new byte[WORKING_COMPRESS_STORAGE]);
11350df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
11360df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck        return result;
11379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11388cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
11399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1140ff236f5fc1a088b1210d37a67b0e88b06c0fbdb2Newton Allen     * Returns true if the bitmap is marked as mutable (i.e.&nbsp;can be drawn into)
11419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isMutable() {
11439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mIsMutable;
11449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11463849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    /**
11473849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied.
11483849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * When a pixel is pre-multiplied, the RGB components have been multiplied by
11493849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * the alpha component. For instance, if the original color is a 50%
1150f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is
11513849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <code>(128, 128, 0, 0)</code>.</p>
1152f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
11533849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>This method always returns false if {@link #getConfig()} is
11544ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * {@link Bitmap.Config#RGB_565}.</p>
1155f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
115657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * <p>The return value is undefined if {@link #getConfig()} is
115757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     * {@link Bitmap.Config#ALPHA_8}.</p>
115857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III     *
11594ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * <p>This method only returns true if {@link #hasAlpha()} returns true.
11604ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * A bitmap with no alpha channel can be used both as a pre-multiplied and
11614ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy     * as a non pre-multiplied bitmap.</p>
11621abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11631abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>Only pre-multiplied bitmaps may be drawn by the view system or
11641abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * {@link Canvas}. If a non-pre-multiplied bitmap with an alpha channel is
11651abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * drawn to a Canvas, a RuntimeException will be thrown.</p>
11661abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11673849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * @return true if the underlying pixels have been pre-multiplied, false
11683849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *         otherwise
11691abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11701abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see Bitmap#setPremultiplied(boolean)
11711abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see BitmapFactory.Options#inPremultiplied
11723849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     */
11733849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    public final boolean isPremultiplied() {
117401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
117501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called isPremultiplied() on a recycle()'d bitmap! This is undefined behavior!");
117601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1177775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeIsPremultiplied(mNativePtr);
11781abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    }
11791abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
11801abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    /**
11811abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * Sets whether the bitmap should treat its data as pre-multiplied.
11821abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11831abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>Bitmaps are always treated as pre-multiplied by the view system and
11841abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * {@link Canvas} for performance reasons. Storing un-pre-multiplied data in
11851abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * a Bitmap (through {@link #setPixel}, {@link #setPixels}, or {@link
11861abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * BitmapFactory.Options#inPremultiplied BitmapFactory.Options.inPremultiplied})
11871abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * can lead to incorrect blending if drawn by the framework.</p>
11881abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
11891abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * <p>This method will not affect the behavior of a bitmap without an alpha
11901abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * channel, or if {@link #hasAlpha()} returns false.</p>
11911abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     *
1192889a1ddd00b74933d5f7daa651d98133bcb034baBen Murdoch     * <p>Calling {@link #createBitmap} or {@link #createScaledBitmap} with a source
11938790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * Bitmap whose colors are not pre-multiplied may result in a RuntimeException,
11948790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * since those functions require drawing the source, which is not supported for
11958790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     * un-pre-multiplied Bitmaps.</p>
11968790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III     *
11971abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see Bitmap#isPremultiplied()
11981abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     * @see BitmapFactory.Options#inPremultiplied
11991abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik     */
12001abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    public final void setPremultiplied(boolean premultiplied) {
12013e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setPremultiplied called on a recycled bitmap");
120257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III        mRequestPremultiplied = premultiplied;
1203775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPremultiplied(mNativePtr, premultiplied);
12043849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy    }
12053849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy
12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Returns the bitmap's width */
12079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getWidth() {
120801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
120901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getWidth() on a recycle()'d bitmap! This is undefined behavior!");
121001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
12111abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        return mWidth;
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Returns the bitmap's height */
12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getHeight() {
121601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
121701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getHeight() on a recycle()'d bitmap! This is undefined behavior!");
121801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
12191abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        return mHeight;
12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12218cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
122311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledWidth(int)} with the target
122411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link Canvas}.
12259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12262784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    public int getScaledWidth(Canvas canvas) {
122711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, canvas.mDensity);
12282784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    }
12292784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn
12302784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    /**
123111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledHeight(int)} with the target
123211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link Canvas}.
12332784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     */
12342784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    public int getScaledHeight(Canvas canvas) {
123511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, canvas.mDensity);
123611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
123711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
123811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
123911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledWidth(int)} with the target
124011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link DisplayMetrics}.
124111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
124211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledWidth(DisplayMetrics metrics) {
124311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, metrics.densityDpi);
124411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
124511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
124611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
124711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Convenience for calling {@link #getScaledHeight(int)} with the target
124811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * density of the given {@link DisplayMetrics}.
124911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
125011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledHeight(DisplayMetrics metrics) {
125111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, metrics.densityDpi);
12522784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    }
12532784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn
12542784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn    /**
12552784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * Convenience method that returns the width of this bitmap divided
12562784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * by the density scale factor.
12572784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     *
125811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param targetDensity The density of the target canvas of the bitmap.
12592784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     * @return The scaled width of this bitmap, according to the density scale factor.
12602784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn     */
126111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledWidth(int targetDensity) {
126211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getWidth(), mDensity, targetDensity);
12639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Convenience method that returns the height of this bitmap divided
12679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * by the density scale factor.
12689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
126911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @param targetDensity The density of the target canvas of the bitmap.
12709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The scaled height of this bitmap, according to the density scale factor.
12719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
127211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public int getScaledHeight(int targetDensity) {
127311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return scaleFromDensity(getHeight(), mDensity, targetDensity);
127411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    }
1275f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
127611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    /**
127711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * @hide
127811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     */
127911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    static public int scaleFromDensity(int size, int sdensity, int tdensity) {
12800b68477f8287fe5ddac1beb1c9d0811ded034dadRomain Guy        if (sdensity == DENSITY_NONE || tdensity == DENSITY_NONE || sdensity == tdensity) {
128111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            return size;
12822784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn        }
1283f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
128411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        // Scale by tdensity / sdensity, rounding up.
1285366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy        return ((size * tdensity) + (sdensity >> 1)) / sdensity;
12869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1287f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
12889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Return the number of bytes between rows in the bitmap's pixels. Note that
12909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this refers to the pixels as stored natively by the bitmap. If you call
12919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * getPixels() or setPixels(), then the pixels are uniformly treated as
12929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * 32bit values, packed according to the Color class.
12939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1294e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, this method
12959f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * should not be used to calculate the memory usage of the bitmap. Instead,
12969f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * see {@link #getAllocationByteCount()}.
12979f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
12989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return number of bytes between rows of the native bitmap pixels.
12999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final int getRowBytes() {
130101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
130201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getRowBytes() on a recycle()'d bitmap! This is undefined behavior!");
130301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1304775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeRowBytes(mNativePtr);
13059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13068cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
13079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13089f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Returns the minimum number of bytes that can be used to store this bitmap's pixels.
13099f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
1310e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase     * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can
13119f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * no longer be used to determine memory usage of a bitmap. See {@link
1312c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #getAllocationByteCount()}.</p>
1313f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson     */
1314f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    public final int getByteCount() {
13153bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        if (mRecycled) {
13163bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            Log.w(TAG, "Called getByteCount() on a recycle()'d bitmap! "
13173bdecbf0fd2d4b6137477094e1ea96502c353248John Reck                    + "This is undefined behavior!");
13183bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            return 0;
13193bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        }
1320f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        // int result permits bitmaps up to 46,340 x 46,340
1321f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson        return getRowBytes() * getHeight();
1322f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    }
1323f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson
1324f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson    /**
13259f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * Returns the size of the allocated memory used to store this bitmap's pixels.
13269f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
13279f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     * <p>This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to
1328c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * decode other bitmaps of smaller size, or by manual reconfiguration. See {@link
1329c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link
1330c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap
1331c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be
1332c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * the same as that returned by {@link #getByteCount()}.</p>
1333c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     *
1334c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * <p>This value will not change over the lifetime of a Bitmap.</p>
13359f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     *
1336c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik     * @see #reconfigure(int, int, Config)
13379f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik     */
13389f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    public final int getAllocationByteCount() {
13393bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        if (mRecycled) {
13403bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            Log.w(TAG, "Called getAllocationByteCount() on a recycle()'d bitmap! "
13413bdecbf0fd2d4b6137477094e1ea96502c353248John Reck                    + "This is undefined behavior!");
13423bdecbf0fd2d4b6137477094e1ea96502c353248John Reck            return 0;
13433bdecbf0fd2d4b6137477094e1ea96502c353248John Reck        }
13444508218850faedea95371188da587b6734f5f3dasergeyv        return nativeGetAllocationByteCount(mNativePtr);
13459f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    }
13469f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik
13479f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik    /**
13489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the bitmap's internal config is in one of the public formats, return
13499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that config, otherwise return null.
13509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final Config getConfig() {
135201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
135301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called getConfig() on a recycle()'d bitmap! This is undefined behavior!");
135401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1355775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return Config.nativeToConfig(nativeConfig(mNativePtr));
13569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1358a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    /** Returns true if the bitmap's config supports per-pixel alpha, and
1359a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * if the pixels may contain non-opaque alpha values. For some configs,
1360a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * this is always false (e.g. RGB_565), since they do not support per-pixel
1361a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * alpha. However, for configs that do, the bitmap may be flagged to be
1362a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * known that all of its pixels are opaque. In this case hasAlpha() will
1363a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * also return false. If a config such as ARGB_8888 is not so flagged,
1364a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * it will return true by default.
1365a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     */
13669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean hasAlpha() {
136701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
136801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!");
136901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1370775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeHasAlpha(mNativePtr);
13719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
13729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1374a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * Tell the bitmap if all of the pixels are known to be opaque (false)
1375a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * or if some of the pixels may contain non-opaque alpha values (true).
1376366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy     * Note, for some configs (e.g. RGB_565) this call is ignored, since it
1377366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy     * does not support per-pixel alpha values.
1378a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     *
1379a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * This is meant as a drawing hint, as in some cases a bitmap that is known
1380a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * to be opaque can take a faster drawing case than one that may have
1381a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     * non-opaque per-pixel alpha values.
1382a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed     */
1383a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    public void setHasAlpha(boolean hasAlpha) {
13843e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setHasAlpha called on a recycled bitmap");
1385775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetHasAlpha(mNativePtr, hasAlpha, mRequestPremultiplied);
1386a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    }
1387a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed
1388a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed    /**
1389713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Indicates whether the renderer responsible for drawing this
1390713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * bitmap should attempt to use mipmaps when this bitmap is drawn
1391713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * scaled down.
1392f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1393713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * If you know that you are going to draw this bitmap at less than
1394713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * 50% of its original size, you may be able to obtain a higher
1395713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * quality
1396f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1397713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * This property is only a suggestion that can be ignored by the
1398713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * renderer. It is not guaranteed to have any effect.
1399f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1400713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @return true if the renderer should attempt to use mipmaps,
1401713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *         false otherwise
1402f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1403713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @see #setHasMipMap(boolean)
1404713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     */
1405713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    public final boolean hasMipMap() {
140601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        if (mRecycled) {
140701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck            Log.w(TAG, "Called hasMipMap() on a recycle()'d bitmap! This is undefined behavior!");
140801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck        }
1409775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeHasMipMap(mNativePtr);
1410713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    }
1411713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
1412713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    /**
1413713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Set a hint for the renderer responsible for drawing this bitmap
1414713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * indicating that it should attempt to use mipmaps when this bitmap
1415713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * is drawn scaled down.
1416713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1417713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * If you know that you are going to draw this bitmap at less than
1418713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * 50% of its original size, you may be able to obtain a higher
1419713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * quality by turning this property on.
1420f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
1421713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * Note that if the renderer respects this hint it might have to
1422713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * allocate extra memory to hold the mipmap levels for this bitmap.
1423713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1424713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * This property is only a suggestion that can be ignored by the
1425713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * renderer. It is not guaranteed to have any effect.
1426713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1427713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @param hasMipMap indicates whether the renderer should attempt
1428713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *                  to use mipmaps
1429713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     *
1430713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     * @see #hasMipMap()
1431713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy     */
1432713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    public final void setHasMipMap(boolean hasMipMap) {
14333e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck        checkRecycled("setHasMipMap called on a recycled bitmap");
1434775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetHasMipMap(mNativePtr, hasMipMap);
1435713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    }
1436713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy
1437713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy    /**
14389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Fills the bitmap's pixels with the specified {@link Color}.
14399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable.
14419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
144280756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void eraseColor(@ColorInt int c) {
14439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't erase a recycled bitmap");
14449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
14459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException("cannot erase immutable bitmaps");
14469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1447775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeErase(mNativePtr, c);
14489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the {@link Color} at the specified location. Throws an exception
14529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * if x or y are out of bounds (negative or >= to the width or height
14533849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * respectively). The returned color is a non-premultiplied ARGB value.
14549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x    The x coordinate (0...width-1) of the pixel to return
14569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y    The y coordinate (0...height-1) of the pixel to return
14579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return     The argb {@link Color} at the specified coordinate
14589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y exceed the bitmap's bounds
145957eafc6f90a1ce2dd97079dd82b4b2f8dcd9e00fsergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
14609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
146180756e38882720860db52f1fcc21fa1505a02abfTor Norbye    @ColorInt
14629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPixel(int x, int y) {
14639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call getPixel() on a recycled bitmap");
1464980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to getPixel(), "
1465980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv                + "pixel access is not supported on Config#HARDWARE bitmaps");
14669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelAccess(x, y);
1467775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeGetPixel(mNativePtr, x, y);
14689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14698cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
14709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
14719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns in pixels[] a copy of the data in the bitmap. Each value is
14729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a packed int representing a {@link Color}. The stride parameter allows
14739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the caller to allow for gaps in the returned pixels array between
14749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rows. For normal packed results, just pass width for the stride value.
14753849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * The returned colors are non-premultiplied ARGB values.
14769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
14779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels   The array to receive the bitmap's colors
14789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   The first index to write into pixels[]
14799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   The number of entries in pixels[] to skip between
14809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 rows (must be >= bitmap's width). Can be negative.
14819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel to read from
14829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap
14839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel to read from
14849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap
14859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of pixels to read from each row
14869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows to read
14873849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
14889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y, width, height exceed the
14899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         bounds of the bitmap, or if abs(stride) < width.
14909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
14919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         to receive the specified number of pixels.
149257eafc6f90a1ce2dd97079dd82b4b2f8dcd9e00fsergeyv     * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE}
14939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
149480756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void getPixels(@ColorInt int[] pixels, int offset, int stride,
14959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          int x, int y, int width, int height) {
14969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call getPixels() on a recycled bitmap");
1497980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv        checkHardware("unable to getPixels(), "
1498980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv                + "pixel access is not supported on Config#HARDWARE bitmaps");
14999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width == 0 || height == 0) {
15009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // nothing to do
15019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
1503775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeGetPixels(mNativePtr, pixels, offset, stride,
150457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                        x, y, width, height);
15059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15068cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Shared code to check for illegal arguments passed to getPixel()
15099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setPixel()
1510f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
15119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x x coordinate of the pixel
15129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y y coordinate of the pixel
15139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkPixelAccess(int x, int y) {
15159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
15169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x >= getWidth()) {
15179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("x must be < bitmap.width()");
15189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y >= getHeight()) {
15209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("y must be < bitmap.height()");
15219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Shared code to check for illegal arguments passed to getPixels()
15269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setPixels()
15279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x left edge of the area of pixels to access
15299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y top edge of the area of pixels to access
15309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width width of the area of pixels to access
15319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height height of the area of pixels to access
15329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset offset into pixels[] array
15339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride number of elements in pixels[] between each logical row
15349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels array to hold the area of pixels being accessed
15359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    */
15369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void checkPixelsAccess(int x, int y, int width, int height,
15379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                   int offset, int stride, int pixels[]) {
15389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkXYSign(x, y);
15399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width < 0) {
15409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("width must be >= 0");
15419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (height < 0) {
15439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("height must be >= 0");
15449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x + width > getWidth()) {
15469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException(
15479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    "x + width must be <= bitmap.width()");
15489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y + height > getHeight()) {
15509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException(
15519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    "y + height must be <= bitmap.height()");
15529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (Math.abs(stride) < width) {
15549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalArgumentException("abs(stride) must be >= width");
15559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int lastScanline = offset + (height - 1) * stride;
15579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int length = pixels.length;
15589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (offset < 0 || (offset + width > length)
15599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || lastScanline < 0
15609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || (lastScanline + width > length)) {
15619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
15629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15648cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15663849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Write the specified {@link Color} into the bitmap (assuming it is
15673849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * mutable) at the x,y coordinate. The color must be a
15683849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * non-premultiplied ARGB value.</p>
15699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x     The x coordinate of the pixel to replace (0...width-1)
15719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y     The y coordinate of the pixel to replace (0...height-1)
15723849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * @param color The ARGB color to write into the bitmap
15733849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
15749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable
15759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y are outside of the bitmap's
15769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         bounds.
15779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
157880756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setPixel(int x, int y, @ColorInt int color) {
15799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call setPixel() on a recycled bitmap");
15809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
15819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException();
15829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelAccess(x, y);
1584775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPixel(mNativePtr, x, y, color);
15859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15868cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
15879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15883849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * <p>Replace pixels in the bitmap with the colors in the array. Each element
15899505a6552764461c22ce48f1ac13d025d23e1579Romain Guy     * in the array is a packed int representing a non-premultiplied ARGB
15903849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     * {@link Color}.</p>
15919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pixels   The colors to write to the bitmap
15939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset   The index of the first color to read from pixels[]
15949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param stride   The number of colors in pixels[] to skip between rows.
15959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 Normally this value will be the same as the width of
15969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap, but it can be larger (or negative).
15979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x        The x coordinate of the first pixel to write to in
15989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap.
15999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y        The y coordinate of the first pixel to write to in
16009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 the bitmap.
16019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param width    The number of colors to copy from pixels[] per row
16029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param height   The number of rows to write to the bitmap
16033849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy     *
16049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException if the bitmap is not mutable
16059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalArgumentException if x, y, width, height are outside of
16069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         the bitmap's bounds.
16079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws ArrayIndexOutOfBoundsException if the pixels array is too small
16089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         to receive the specified number of pixels.
16099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
161080756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setPixels(@ColorInt int[] pixels, int offset, int stride,
16113849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy            int x, int y, int width, int height) {
16129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't call setPixels() on a recycled bitmap");
16139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!isMutable()) {
16149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new IllegalStateException();
16159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (width == 0 || height == 0) {
16179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // nothing to do
16189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkPixelsAccess(x, y, width, height, offset, stride, pixels);
1620775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        nativeSetPixels(mNativePtr, pixels, offset, stride,
162157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                        x, y, width, height);
16229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16238cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
16249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final Parcelable.Creator<Bitmap> CREATOR
16259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            = new Parcelable.Creator<Bitmap>() {
16269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
16279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Rebuilds a bitmap previously stored with writeToParcel().
16289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *
16299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * @param p    Parcel object to read the bitmap from
16309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * @return a new bitmap created from the data in the parcel
16319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
16329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Bitmap createFromParcel(Parcel p) {
16339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Bitmap bm = nativeCreateFromParcel(p);
16349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (bm == null) {
16359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                throw new RuntimeException("Failed to unparcel Bitmap");
16369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
16379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return bm;
16389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Bitmap[] newArray(int size) {
16409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return new Bitmap[size];
16419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
16439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * No special parcel contents.
16469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int describeContents() {
1648b6377170960d40e66858d8b4d335a95eac773762Bart Sears        return 0;
16499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write the bitmap and its pixels to the parcel. The bitmap can be
16539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rebuilt from the parcel by calling CREATOR.createFromParcel().
1654f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     *
1655f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     * If this bitmap is {@link Config#HARDWARE}, it may be unparceled with a different pixel
1656f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     * format (e.g. 565, 8888), but the content will be preserved to the best quality permitted
1657f161dce70eab5ba1011bd26226c14e31675d5955sergeyv     * by the final pixel format
16589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param p    Parcel object to write the bitmap data into
16599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void writeToParcel(Parcel p, int flags) {
16619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't parcel a recycled bitmap");
1662775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        if (!nativeWriteToParcel(mNativePtr, mIsMutable, mDensity, p)) {
16639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("native writeToParcel failed");
16649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new bitmap that captures the alpha values of the original.
16699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This may be drawn with Canvas.drawBitmap(), where the color(s) will be
16709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * taken from the paint that is passed to the draw call.
16719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return new bitmap containing the alpha channel of the original bitmap.
16739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16741c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye    @CheckResult
16759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap extractAlpha() {
16769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return extractAlpha(null, null);
16779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16788cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
16799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns a new bitmap that captures the alpha values of the original.
16819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * These values may be affected by the optional Paint parameter, which
16829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * can contain its own alpha, and may also contain a MaskFilter which
16839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * could change the actual dimensions of the resulting bitmap (e.g.
16849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a blur maskfilter might enlarge the resulting bitmap). If offsetXY
16859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * is not null, it returns the amount to offset the returned bitmap so
16869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that it will logically align with the original. For example, if the
16879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * paint contains a blur of radius 2, then offsetXY[] would contains
16889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * -2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then
16899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * drawing the original would result in the blur visually aligning with
16909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the original.
1691f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
169296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn     * <p>The initial density of the returned bitmap is the same as the original's.
1693f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck     *
16949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param paint Optional paint used to modify the alpha values in the
16959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              resulting bitmap. Pass null for default behavior.
16969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offsetXY Optional array that returns the X (index 0) and Y
16979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 (index 1) offset needed to position the returned bitmap
16989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                 so that it visually lines up with the original.
16999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return new bitmap containing the (optionally modified by paint) alpha
17009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         channel of the original bitmap. This may be drawn with
17019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         Canvas.drawBitmap(), where the color(s) will be taken from the
17029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         paint that is passed to the draw call.
17039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17041c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye    @CheckResult
17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public Bitmap extractAlpha(Paint paint, int[] offsetXY) {
17069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        checkRecycled("Can't extractAlpha on a recycled bitmap");
1707dfba4d3d11bbf47dff45f94d61d4d97510b3034aDerek Sollenberger        long nativePaint = paint != null ? paint.getNativeInstance() : 0;
1708775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        Bitmap bm = nativeExtractAlpha(mNativePtr, nativePaint, offsetXY);
17099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bm == null) {
17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new RuntimeException("Failed to extractAlpha on Bitmap");
17119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
171296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn        bm.mDensity = mDensity;
17139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
17149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17168cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    /**
171776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  Given another bitmap, return true if it has the same dimensions, config,
171876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  and pixel data as this bitmap. If any of those differ, return false.
171976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     *  If other is null, return false.
172076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed     */
172176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    public boolean sameAs(Bitmap other) {
17229298c5414172344f2462beda70e624af5f774483John Reck        checkRecycled("Can't call sameAs on a recycled bitmap!");
17233df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck        if (this == other) return true;
17243df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck        if (other == null) return false;
17259298c5414172344f2462beda70e624af5f774483John Reck        if (other.isRecycled()) {
17269298c5414172344f2462beda70e624af5f774483John Reck            throw new IllegalArgumentException("Can't compare to a recycled bitmap!");
17279298c5414172344f2462beda70e624af5f774483John Reck        }
1728775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler        return nativeSameAs(mNativePtr, other.mNativePtr);
172976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    }
173076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed
173176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed    /**
17328cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * Rebuilds any caches associated with the bitmap that are used for
17338cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * drawing it. In the case of purgeable bitmaps, this call will attempt to
17348cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * ensure that the pixels have been decoded.
17358cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * If this is called on more than one bitmap in sequence, the priority is
17368cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * given in LRU order (i.e. the last bitmap called will be given highest
17378cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * priority).
17388cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     *
17398cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * For bitmaps with no associated caches, this call is effectively a no-op,
17408cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     * and therefore is harmless.
17418cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen     */
17428cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen    public void prepareToDraw() {
1743ad57442fd00e7fd7f1885d23372ec5c85e176820John Reck        checkRecycled("Can't prepareToDraw on a recycled bitmap!");
17444387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        // Kick off an update/upload of the bitmap outside of the normal
17454387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        // draw path.
17464387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        nativePrepareToDraw(mNativePtr);
1747f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck    }
1748f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck
17496e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    /**
17506e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     *
17516e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     * @return {@link GraphicBuffer} which is internally used by hardware bitmap
17526e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     * @hide
17536e3658a63843096058ed444d073fbcd191fd7e1bsergeyv     */
17546e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    public GraphicBuffer createGraphicBufferHandle() {
17556e3658a63843096058ed444d073fbcd191fd7e1bsergeyv        return nativeCreateGraphicBufferHandle(mNativePtr);
17566e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    }
17576e3658a63843096058ed444d073fbcd191fd7e1bsergeyv
17589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //////////// native methods
17599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeCreate(int[] colors, int offset,
17619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                              int stride, int width, int height,
1762c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik                                              int nativeConfig, boolean mutable);
176336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig,
17649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            boolean isMutable);
1765721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews    private static native Bitmap nativeCopyAshmem(long nativeSrcBitmap);
1766a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson    private static native Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig);
1767775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    private static native long nativeGetNativeFinalizer();
176836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeRecycle(long nativeBitmap);
176936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeReconfigure(long nativeBitmap, int width, int height,
17704508218850faedea95371188da587b6734f5f3dasergeyv                                                 int config, boolean isPremultiplied);
17719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
177236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeCompress(long nativeBitmap, int format,
17739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            int quality, OutputStream stream,
17749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            byte[] tempStorage);
177536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeErase(long nativeBitmap, int color);
177636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeRowBytes(long nativeBitmap);
177736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeConfig(long nativeBitmap);
17788cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
177957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native int nativeGetPixel(long nativeBitmap, int x, int y);
178036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeGetPixels(long nativeBitmap, int[] pixels,
17811abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik                                               int offset, int stride, int x, int y,
178257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                               int width, int height);
17838cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen
178457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetPixel(long nativeBitmap, int x, int y, int color);
178536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeSetPixels(long nativeBitmap, int[] colors,
17861abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik                                               int offset, int stride, int x, int y,
178757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                               int width, int height);
178836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeCopyPixelsToBuffer(long nativeBitmap,
17899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                        Buffer dst);
179036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src);
179136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native int nativeGenerationId(long nativeBitmap);
17929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeCreateFromParcel(Parcel p);
17949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // returns true on success
179536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeWriteToParcel(long nativeBitmap,
17969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                      boolean isMutable,
1797de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn                                                      int density,
17989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                      Parcel p);
17999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // returns a new bitmap built from the native bitmap's alpha, and the paint
180036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeExtractAlpha(long nativeBitmap,
180136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat                                                    long nativePaint,
18029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                    int[] offsetXY);
18039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
180436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeHasAlpha(long nativeBitmap);
180557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native boolean nativeIsPremultiplied(long nativeBitmap);
180657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetPremultiplied(long nativeBitmap,
180757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                      boolean isPremul);
180857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III    private static native void nativeSetHasAlpha(long nativeBitmap,
180957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                 boolean hasAlpha,
181057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III                                                 boolean requestPremul);
181136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeHasMipMap(long nativeBitmap);
181236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap);
181336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1);
18144387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    private static native void nativePrepareToDraw(long nativeBitmap);
18154508218850faedea95371188da587b6734f5f3dasergeyv    private static native int nativeGetAllocationByteCount(long nativeBitmap);
181681f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv    private static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap);
18170a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv    private static native Bitmap nativeCreateHardwareBitmap(GraphicBuffer buffer);
18186e3658a63843096058ed444d073fbcd191fd7e1bsergeyv    private static native GraphicBuffer nativeCreateGraphicBufferHandle(long nativeBitmap);
18199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1820