Bitmap.java revision b1d2f0a1d0900ba5651f98021b4b7b4229ebb490
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; 22efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guyimport android.annotation.Nullable; 238242656f495847c50c9ceaea92839dce78218a75Romain Guyimport android.annotation.Size; 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcel; 258cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport android.os.Parcelable; 269fd259c9b0878ba52fec86173d76976e70977fcdsergeyvimport android.os.StrictMode; 270df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reckimport android.os.Trace; 282784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackbornimport android.util.DisplayMetrics; 2901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reckimport android.util.Log; 30a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winsonimport libcore.util.NativeAllocationRegistry; 31a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson 328cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.io.OutputStream; 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.Buffer; 349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.ByteBuffer; 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.nio.IntBuffer; 368cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chenimport java.nio.ShortBuffer; 379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class Bitmap implements Parcelable { 3901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck private static final String TAG = "Bitmap"; 4001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Indicates that the bitmap was created for an unknown pixel density. 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see Bitmap#getDensity() 4511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see Bitmap#setDensity(int) 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public static final int DENSITY_NONE = 0; 48f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 49775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler // Estimated size of the Bitmap native allocation, not including 50775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler // pixel data. 51775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler private static final long NATIVE_ALLOCATION_SIZE = 32; 52775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler 53f29ed28c7b878ef28058bc730715d0d32445bc57John Reck // Convenience for JNI access 54f29ed28c7b878ef28058bc730715d0d32445bc57John Reck private final long mNativePtr; 55e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private final boolean mIsMutable; 571abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik 581abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik /** 5957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * Represents whether the Bitmap's content is requested to be pre-multiplied. 601abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * Note that isPremultiplied() does not directly return this value, because 6157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * isPremultiplied() may never return true for a 565 Bitmap or a bitmap 6257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * without alpha. 631abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 641abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * setPremultiplied() does directly set the value so that setConfig() and 651abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * setPremultiplied() aren't order dependent, despite being setters. 6657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * 6757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * The native bitmap's premultiplication state is kept up to date by 6857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * pushing down this preference for every config change. 691abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik */ 7057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III private boolean mRequestPremultiplied; 718790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III 7247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik private byte[] mNinePatchChunk; // may be null 7347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik private NinePatch.InsetStruct mNinePatchInsets; // may be null 741abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik private int mWidth; 751abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik private int mHeight; 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private boolean mRecycled; 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 78efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy private ColorSpace mColorSpace; 79efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy 80caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck /** @hide */ 81caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck public int mDensity = getDefaultDensity(); 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 8396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn private static volatile int sDefaultDensity = -1; 8402890fd0f98b3b8d98baf0bda1ea906afd723d8bRomain Guy 8596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn /** 8696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * For backwards compatibility, allows the app layer to change the default 8796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * density when running old apps. 8896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * @hide 8996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn */ 9096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn public static void setDefaultDensity(int density) { 9196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn sDefaultDensity = density; 9296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn } 933849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy 94f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck @SuppressWarnings("deprecation") 953849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy static int getDefaultDensity() { 9696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn if (sDefaultDensity >= 0) { 9796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn return sDefaultDensity; 9896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn } 9996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn sDefaultDensity = DisplayMetrics.DENSITY_DEVICE; 10096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn return sDefaultDensity; 10196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn } 102dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1049f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * Private constructor that must received an already allocated native bitmap 1059f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * int (pointer). 106ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani */ 107f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck // called from JNI 1084508218850faedea95371188da587b6734f5f3dasergeyv Bitmap(long nativeBitmap, int width, int height, int density, 10957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III boolean isMutable, boolean requestPremultiplied, 11047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik byte[] ninePatchChunk, NinePatch.InsetStruct ninePatchInsets) { 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (nativeBitmap == 0) { 1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("internal error: native bitmap is 0"); 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1148cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 1151abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik mWidth = width; 1161abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik mHeight = height; 1171abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik mIsMutable = isMutable; 11857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III mRequestPremultiplied = requestPremultiplied; 1194cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik 1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mNinePatchChunk = ninePatchChunk; 12147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik mNinePatchInsets = ninePatchInsets; 122de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn if (density >= 0) { 123de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn mDensity = density; 124de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn } 1254cd7dbc90f93893f521dce32d8cd25c02a185a24Chris Craik 126f29ed28c7b878ef28058bc730715d0d32445bc57John Reck mNativePtr = nativeBitmap; 1274508218850faedea95371188da587b6734f5f3dasergeyv long nativeSize = NATIVE_ALLOCATION_SIZE + getAllocationByteCount(); 128775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler NativeAllocationRegistry registry = new NativeAllocationRegistry( 1290ebdbb4a8470bb77f38c33a42c220ecb8a7adcc6Richard Uhler Bitmap.class.getClassLoader(), nativeGetNativeFinalizer(), nativeSize); 130775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler registry.registerNativeAllocation(this, nativeBitmap); 1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1344c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger * Return the pointer to the native object. 135caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck * @hide 1364c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger */ 137caa08ff5e9ee004634a95776fc72bb769f1286deJohn Reck public long getNativeInstance() { 1384c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger return mNativePtr; 1394c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger } 1404c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger 1414c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger /** 1421abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * Native bitmap has been reconfigured, so set premult and cached 1431abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * width/height values 1449f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik */ 1459505a6552764461c22ce48f1ac13d025d23e1579Romain Guy @SuppressWarnings("unused") // called from JNI 14657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III void reinit(int width, int height, boolean requestPremultiplied) { 1471abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik mWidth = width; 1481abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik mHeight = height; 14957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III mRequestPremultiplied = requestPremultiplied; 1505acc476878bce8b72c0059e05cbbd5b43ffee5d5Romain Guy mColorSpace = null; 1519f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik } 1529f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik 1539f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik /** 15411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * <p>Returns the density for this bitmap.</p> 1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 15696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * <p>The default density is the same density as the current display, 15796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * unless the current application does not support different screen 15896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * densities in which case it is 15996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * {@link android.util.DisplayMetrics#DENSITY_DEFAULT}. Note that 16096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * compatibility mode is determined by the application that was initially 16196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * loaded into a process -- applications that share the same process should 16296e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * all have the same compatibility, or ensure they explicitly set the 16396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * density of their bitmaps appropriately.</p> 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 16596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * @return A scaling factor of the default density or {@link #DENSITY_NONE} 1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * if the scaling factor is unknown. 1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 16811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see #setDensity(int) 169a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn * @see android.util.DisplayMetrics#DENSITY_DEFAULT 17011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see android.util.DisplayMetrics#densityDpi 17111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see #DENSITY_NONE 1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 17311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public int getDensity() { 17401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 17501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called getDensity() on a recycle()'d bitmap! This is undefined behavior!"); 17601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 17711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return mDensity; 1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 18111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * <p>Specifies the density for this bitmap. When the bitmap is 18211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * drawn to a Canvas that also has a density, it will be scaled 18311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * appropriately.</p> 1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 18511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @param density The density scaling factor to use with this bitmap or 18611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * {@link #DENSITY_NONE} if the density is unknown. 1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 18811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see #getDensity() 189a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn * @see android.util.DisplayMetrics#DENSITY_DEFAULT 19011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see android.util.DisplayMetrics#densityDpi 19111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @see #DENSITY_NONE 1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 19311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public void setDensity(int density) { 19411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn mDensity = density; 1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 196c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik 197c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik /** 198c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>Modifies the bitmap to have a specified width, height, and {@link 199c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * Config}, without affecting the underlying allocation backing the bitmap. 200c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * Bitmap pixel data is not re-initialized for the new configuration.</p> 201c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 202c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>This method can be used to avoid allocating a new bitmap, instead 203c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * reusing an existing bitmap's allocation for a new configuration of equal 204c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * or lesser size. If the Bitmap's allocation isn't large enough to support 205c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * the new configuration, an IllegalArgumentException will be thrown and the 206c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * bitmap will not be modified.</p> 207c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 208c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>The result of {@link #getByteCount()} will reflect the new configuration, 209c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * while {@link #getAllocationByteCount()} will reflect that of the initial 210c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * configuration.</p> 211c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 21217a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III * <p>Note: This may change this result of hasAlpha(). When converting to 565, 21317a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III * the new bitmap will always be considered opaque. When converting from 565, 21417a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III * the new bitmap will be considered non-opaque, and will respect the value 21517a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III * set by setPremultiplied().</p> 21617a8bfc38a565ae96f43d36b223779be840bb50cLeon Scroggins III * 2176d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * <p>WARNING: This method should NOT be called on a bitmap currently in use 2186d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * by the view system, Canvas, or the AndroidBitmap NDK API. It does not 2196d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * make guarantees about how the underlying pixel buffer is remapped to the 2206d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * new config, just that the allocation is reused. Additionally, the view 2216d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * system does not account for bitmap properties being modifying during use, 2226d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * e.g. while attached to drawables.</p> 2236d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * 2246d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * <p>In order to safely ensure that a Bitmap is no longer in use by the 2256d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * View system it is necessary to wait for a draw pass to occur after 2266d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * invalidate()'ing any view that had previously drawn the Bitmap in the last 2276d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * draw pass due to hardware acceleration's caching of draw commands. As 2286d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * an example, here is how this can be done for an ImageView: 2296d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * <pre class="prettyprint"> 2306d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * ImageView myImageView = ...; 2316d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * final Bitmap myBitmap = ...; 2326d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * myImageView.setImageDrawable(null); 2336d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * myImageView.post(new Runnable() { 2346d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * public void run() { 2356d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * // myBitmap is now no longer in use by the ImageView 2366d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * // and can be safely reconfigured. 2376d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * myBitmap.reconfigure(...); 2386d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * } 2396d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * }); 2406d86e57bdadd56be160eb3e543cc53622cae46f2John Reck * </pre></p> 241c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 242c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setWidth(int) 243c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setHeight(int) 244c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setConfig(Config) 245c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik */ 246c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik public void reconfigure(int width, int height, Config config) { 247c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik checkRecycled("Can't call reconfigure() on a recycled bitmap"); 248c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik if (width <= 0 || height <= 0) { 249c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik throw new IllegalArgumentException("width and height must be > 0"); 250c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik } 251c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik if (!isMutable()) { 2529c2cde0583d02ba7551877dbe7a5ecaee6defb56Chris Craik throw new IllegalStateException("only mutable bitmaps may be reconfigured"); 253c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik } 254c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik 2554508218850faedea95371188da587b6734f5f3dasergeyv nativeReconfigure(mNativePtr, width, height, config.nativeInt, mRequestPremultiplied); 256c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik mWidth = width; 257c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik mHeight = height; 2585acc476878bce8b72c0059e05cbbd5b43ffee5d5Romain Guy mColorSpace = null; 259c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik } 260c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik 261c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik /** 262c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>Convenience method for calling {@link #reconfigure(int, int, Config)} 263c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * with the current height and config.</p> 264c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 265c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>WARNING: this method should not be used on bitmaps currently used by 266c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * the view system, see {@link #reconfigure(int, int, Config)} for more 267c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * details.</p> 268c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 269c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #reconfigure(int, int, Config) 270c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setHeight(int) 271c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setConfig(Config) 272c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik */ 273c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik public void setWidth(int width) { 274c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik reconfigure(width, getHeight(), getConfig()); 275c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik } 276c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik 277c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik /** 278c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>Convenience method for calling {@link #reconfigure(int, int, Config)} 279c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * with the current width and config.</p> 280c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 281c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>WARNING: this method should not be used on bitmaps currently used by 282c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * the view system, see {@link #reconfigure(int, int, Config)} for more 283c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * details.</p> 284c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 285c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #reconfigure(int, int, Config) 286c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setWidth(int) 287c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setConfig(Config) 288c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik */ 289c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik public void setHeight(int height) { 290c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik reconfigure(getWidth(), height, getConfig()); 291c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik } 292c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik 293c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik /** 294c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>Convenience method for calling {@link #reconfigure(int, int, Config)} 295c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * with the current height and width.</p> 296c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 297c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>WARNING: this method should not be used on bitmaps currently used by 298c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * the view system, see {@link #reconfigure(int, int, Config)} for more 299c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * details.</p> 300c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 301c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #reconfigure(int, int, Config) 302c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setWidth(int) 303c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #setHeight(int) 304c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik */ 305c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik public void setConfig(Config config) { 306c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik reconfigure(getWidth(), getHeight(), config); 307c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik } 308c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik 3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the nine patch chunk. 3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param chunk The definition of the nine patch 3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @hide 3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setNinePatchChunk(byte[] chunk) { 3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mNinePatchChunk = chunk; 3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3198cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 321a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy * Free the native object associated with this bitmap, and clear the 322a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy * reference to the pixel data. This will not free the pixel data synchronously; 323a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy * it simply allows it to be garbage collected if there are no other references. 324a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy * The bitmap is marked as "dead", meaning it will throw an exception if 325a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy * getPixels() or setPixels() is called, and will draw nothing. This operation 326a383b3f646cbc1fd870f2694d4fac24340834916Patrick Dubroy * cannot be reversed, so it should only be called if you are sure there are no 3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * further uses for the bitmap. This is an advanced call, and normally need 3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * not be called, since the normal GC process will free up this memory when 3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * there are no more references to this bitmap. 3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void recycle() { 332775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler if (!mRecycled && mNativePtr != 0) { 333775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler if (nativeRecycle(mNativePtr)) { 334547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase // return value indicates whether native pixel object was actually recycled. 335547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase // false indicates that it is still in use at the native level and these 336547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase // objects should not be collected now. They will be collected later when the 337547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase // Bitmap itself is collected. 338547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase mNinePatchChunk = null; 339547e66531d521eb1eadac87edb0f79f8c2f1bbe0Chet Haase } 3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mRecycled = true; 3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns true if this bitmap has been recycled. If so, then it is an error 3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to try to access its pixels, and the bitmap will not draw. 3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return true if the bitmap has been recycled 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final boolean isRecycled() { 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mRecycled; 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3538cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3550bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * Returns the generation ID of this bitmap. The generation ID changes 3560bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * whenever the bitmap is modified. This can be used as an efficient way to 3570bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * check if a bitmap has changed. 358f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 3590bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * @return The current generation ID for this bitmap. 3600bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy */ 3610bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy public int getGenerationId() { 36201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 36301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called getGenerationId() on a recycle()'d bitmap! This is undefined behavior!"); 36401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 365775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeGenerationId(mNativePtr); 3660bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy } 367f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 3680bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy /** 3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * This is called by methods that want to throw an exception if the bitmap 3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * has already been recycled. 3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private void checkRecycled(String errorMessage) { 3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (mRecycled) { 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalStateException(errorMessage); 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3778cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 379980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv * This is called by methods that want to throw an exception if the bitmap 380980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv * is {@link Config#HARDWARE}. 381980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv */ 382980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv private void checkHardware(String errorMessage) { 383980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv if (getConfig() == Config.HARDWARE) { 384980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv throw new IllegalStateException(errorMessage); 385980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv } 386980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv } 387980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv 388980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv /** 3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Common code for checking that x and y are >= 0 3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x x coordinate to ensure is >= 0 3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y y coordinate to ensure is >= 0 3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static void checkXYSign(int x, int y) { 3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (x < 0) { 3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("x must be >= 0"); 3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (y < 0) { 3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("y must be >= 0"); 4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Common code for checking that width and height are > 0 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width width to ensure is > 0 4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height height to ensure is > 0 4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static void checkWidthHeight(int width, int height) { 4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (width <= 0) { 4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("width must be > 0"); 4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (height <= 0) { 4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("height must be > 0"); 4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 418676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy /** 419676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * Possible bitmap configurations. A bitmap configuration describes 420676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * how pixels are stored. This affects the quality (color depth) as 421676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * well as the ability to display transparent/translucent colors. 422676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy */ 4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public enum Config { 4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // these native values must match up with the enum in SkBitmap.h 425676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy 426676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy /** 427676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * Each pixel is stored as a single translucency (alpha) channel. 428676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * This is very useful to efficiently store masks for instance. 429676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * No color information is stored. 430676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * With this configuration, each pixel requires 1 byte of memory. 431676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy */ 432b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger ALPHA_8 (1), 433676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy 434676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy /** 435676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * Each pixel is stored on 2 bytes and only the RGB channels are 436676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * encoded: red is stored with 5 bits of precision (32 possible 437676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * values), green is stored with 6 bits of precision (64 possible 438676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * values) and blue is stored with 5 bits of precision. 439f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 440676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * This configuration can produce slight visual artifacts depending 441676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * on the configuration of the source. For instance, without 442676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * dithering, the result might show a greenish tint. To get better 443676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * results dithering should be applied. 444f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 445676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * This configuration may be useful when using opaque bitmaps 446676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * that do not require high color fidelity. 447676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy */ 448b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger RGB_565 (3), 449676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy 450676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy /** 451676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * Each pixel is stored on 2 bytes. The three RGB color channels 452676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * and the alpha channel (translucency) are stored with a 4 bits 453676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * precision (16 possible values.) 454f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 455676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * This configuration is mostly useful if the application needs 456676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * to store translucency information but also needs to save 457676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * memory. 458f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 459676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * It is recommended to use {@link #ARGB_8888} instead of this 460676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * configuration. 46125ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy * 462e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase * Note: as of {@link android.os.Build.VERSION_CODES#KITKAT}, 46325ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy * any bitmap created with this configuration will be created 46425ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy * using {@link #ARGB_8888} instead. 465f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 466676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * @deprecated Because of the poor quality of this configuration, 467676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * it is advised to use {@link #ARGB_8888} instead. 468676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy */ 469676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy @Deprecated 470b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger ARGB_4444 (4), 471676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy 472676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy /** 473676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * Each pixel is stored on 4 bytes. Each channel (RGB and alpha 474676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * for translucency) is stored with 8 bits of precision (256 475676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * possible values.) 476f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 477676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * This configuration is very flexible and offers the best 478676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy * quality. It should be used whenever possible. 479676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy */ 480da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv ARGB_8888 (5), 481da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv 4829505a6552764461c22ce48f1ac13d025d23e1579Romain Guy /** 4839505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * Each pixels is stored on 8 bytes. Each channel (RGB and alpha 4849505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * for translucency) is stored as a 4859505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * {@link android.util.Half half-precision floating point value}. 4869505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * 4879505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * This configuration is particularly suited for wide-gamut and 4889505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * HDR content. 4899505a6552764461c22ce48f1ac13d025d23e1579Romain Guy */ 4909505a6552764461c22ce48f1ac13d025d23e1579Romain Guy RGBA_F16 (6), 491da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv 492da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv /** 4939fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * Special configuration, when bitmap is stored only in graphic memory. 4949fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * Bitmaps in this configuration are always immutable. 4959fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * 4969fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * It is optimal for cases, when the only operation with the bitmap is to draw it on a 4979fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * screen. 498da6c8ffcc594493c3936f83255efccdbf964cd21sergeyv */ 4999505a6552764461c22ce48f1ac13d025d23e1579Romain Guy HARDWARE (7); 5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 501676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy final int nativeInt; 502676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy 503676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy private static Config sConfigs[] = { 5049505a6552764461c22ce48f1ac13d025d23e1579Romain Guy null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888, RGBA_F16, HARDWARE 505676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy }; 506f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Config(int ni) { 5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project this.nativeInt = ni; 5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5108cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 511676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy static Config nativeToConfig(int ni) { 5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return sConfigs[ni]; 5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5158cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 5173849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>Copy the bitmap's pixels into the specified buffer (allocated by the 5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * caller). An exception is thrown if the buffer is not large enough to 5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * hold all of the pixels (taking into account the number of bytes per 5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * pixel) or if the Buffer subclass is not one of the support types 5213849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * (ByteBuffer, ShortBuffer, IntBuffer).</p> 5223849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>The content of the bitmap is copied into the buffer as-is. This means 5233849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * that if this bitmap stores its pixels pre-multiplied 5243849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * (see {@link #isPremultiplied()}, the values in the buffer will also be 525ce217faddb4b40c1b3e698944da1951027080427Romain Guy * pre-multiplied. The pixels remain in the color space of the bitmap.</p> 5261f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * <p>After this method returns, the current position of the buffer is 5271f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * updated: the position is incremented by the number of elements written 5281f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * in the buffer.</p> 529980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE} 5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void copyPixelsToBuffer(Buffer dst) { 532980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv checkHardware("unable to copyPixelsToBuffer, " 533980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv + "pixel access is not supported on Config#HARDWARE bitmaps"); 5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int elements = dst.remaining(); 5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int shift; 5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (dst instanceof ByteBuffer) { 5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project shift = 0; 5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else if (dst instanceof ShortBuffer) { 5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project shift = 1; 5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else if (dst instanceof IntBuffer) { 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project shift = 2; 5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else { 5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("unsupported Buffer subclass"); 5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5458cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project long bufferSize = (long)elements << shift; 547f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson long pixelSize = getByteCount(); 5488cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (bufferSize < pixelSize) { 5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("Buffer not large enough for pixels"); 5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5528cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 553775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeCopyPixelsToBuffer(mNativePtr, dst); 5548cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // now update the buffer's position 5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int position = dst.position(); 5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project position += pixelSize >> shift; 5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dst.position(position); 5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 5623849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>Copy the pixels from the buffer, beginning at the current position, 5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * overwriting the bitmap's pixels. The data in the buffer is not changed 5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * in any way (unlike setPixels(), which converts from unpremultipled 32bit 565ce217faddb4b40c1b3e698944da1951027080427Romain Guy * to whatever the bitmap's native format is. The pixels in the source 566ce217faddb4b40c1b3e698944da1951027080427Romain Guy * buffer are assumed to be in the bitmap's color space.</p> 5671f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * <p>After this method returns, the current position of the buffer is 5681f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * updated: the position is incremented by the number of elements read from 5691f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * the buffer. If you need to read the bitmap from the buffer again you must 5701f58497e7dbab1b16e97c73b65559b852d0c2803Romain Guy * first rewind the buffer.</p> 571980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE} 5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void copyPixelsFromBuffer(Buffer src) { 5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("copyPixelsFromBuffer called on recycled bitmap"); 575980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv checkHardware("unable to copyPixelsFromBuffer, Config#HARDWARE bitmaps are immutable"); 5768cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int elements = src.remaining(); 5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int shift; 5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (src instanceof ByteBuffer) { 5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project shift = 0; 5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else if (src instanceof ShortBuffer) { 5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project shift = 1; 5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else if (src instanceof IntBuffer) { 5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project shift = 2; 5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else { 5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("unsupported Buffer subclass"); 5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5888cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5893849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy long bufferBytes = (long) elements << shift; 590f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson long bitmapBytes = getByteCount(); 5918cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (bufferBytes < bitmapBytes) { 5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("Buffer not large enough for pixels"); 5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5958cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 596775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeCopyPixelsFromBuffer(mNativePtr, src); 59755adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen 59855adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen // now update the buffer's position 59955adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen int position = src.position(); 60055adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen position += bitmapBytes >> shift; 60155adc145d460be4b21de0d77be1f6076c3591e3cMarco Nelissen src.position(position); 6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6038cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 6049fd259c9b0878ba52fec86173d76976e70977fcdsergeyv private void noteHardwareBitmapSlowCall() { 6059fd259c9b0878ba52fec86173d76976e70977fcdsergeyv if (getConfig() == Config.HARDWARE) { 6069fd259c9b0878ba52fec86173d76976e70977fcdsergeyv StrictMode.noteSlowCall("Warning: attempt to read pixels from hardware " 6079fd259c9b0878ba52fec86173d76976e70977fcdsergeyv + "bitmap, which is very slow operation"); 6089fd259c9b0878ba52fec86173d76976e70977fcdsergeyv } 6099fd259c9b0878ba52fec86173d76976e70977fcdsergeyv } 6109fd259c9b0878ba52fec86173d76976e70977fcdsergeyv 6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Tries to make a new bitmap based on the dimensions of this bitmap, 6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * setting the new bitmap's config to the one specified, and then copying 6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * this bitmap's pixels into the new bitmap. If the conversion is not 61596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * supported, or the allocator fails, then this returns NULL. The returned 6168242656f495847c50c9ceaea92839dce78218a75Romain Guy * bitmap has the same density and color space as the original. 6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param config The desired config for the resulting bitmap 6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param isMutable True if the resulting bitmap should be mutable (i.e. 6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * its pixels can be modified) 6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return the new bitmap, or null if the copy could not be made. 62205126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv * @throws IllegalArgumentException if config is {@link Config#HARDWARE} and isMutable is true 6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Bitmap copy(Config config, boolean isMutable) { 6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't copy a recycled bitmap"); 62605126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv if (config == Config.HARDWARE && isMutable) { 62705126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv throw new IllegalArgumentException("Hardware bitmaps are always immutable"); 62805126d151eb3caa85bd3a039cffb6e37940c3fa4sergeyv } 6299fd259c9b0878ba52fec86173d76976e70977fcdsergeyv noteHardwareBitmapSlowCall(); 630775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable); 63196e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn if (b != null) { 63257ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III b.setPremultiplied(mRequestPremultiplied); 63396e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn b.mDensity = mDensity; 63496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn } 63596e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn return b; 6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 63802d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick /** 639721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews * Creates a new immutable bitmap backed by ashmem which can efficiently 6408242656f495847c50c9ceaea92839dce78218a75Romain Guy * be passed between processes. The bitmap is assumed to be in the sRGB 6418242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 642721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews * 643721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews * @hide 644721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews */ 645721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews public Bitmap createAshmemBitmap() { 646721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews checkRecycled("Can't copy a recycled bitmap"); 6479fd259c9b0878ba52fec86173d76976e70977fcdsergeyv noteHardwareBitmapSlowCall(); 648775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler Bitmap b = nativeCopyAshmem(mNativePtr); 649721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews if (b != null) { 650721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews b.setPremultiplied(mRequestPremultiplied); 651721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews b.mDensity = mDensity; 652721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews } 653721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews return b; 654721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews } 655721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews 656721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews /** 657a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson * Creates a new immutable bitmap backed by ashmem which can efficiently 6588242656f495847c50c9ceaea92839dce78218a75Romain Guy * be passed between processes. The bitmap is assumed to be in the sRGB 6598242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 660a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson * 661a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson * @hide 662a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson */ 663a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson public Bitmap createAshmemBitmap(Config config) { 664a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson checkRecycled("Can't copy a recycled bitmap"); 6659fd259c9b0878ba52fec86173d76976e70977fcdsergeyv noteHardwareBitmapSlowCall(); 666a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson Bitmap b = nativeCopyAshmemConfig(mNativePtr, config.nativeInt); 667a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson if (b != null) { 668a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson b.setPremultiplied(mRequestPremultiplied); 669a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson b.mDensity = mDensity; 670a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson } 671a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson return b; 672a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson } 673a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson 674a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson /** 6750a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv * Create hardware bitmap backed GraphicBuffer. 6760a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv * 6770a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv * @return Bitmap or null if this GraphicBuffer has unsupported PixelFormat. 6780a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv * currently PIXEL_FORMAT_RGBA_8888 is the only supported format 6790a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv * @hide 6800a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv */ 6818242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createHardwareBitmap(@NonNull GraphicBuffer graphicBuffer) { 6820a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv return nativeCreateHardwareBitmap(graphicBuffer); 6830a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv } 6840a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv 6850a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv /** 686f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * Creates a new bitmap, scaled from an existing bitmap, when possible. If the 687f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * specified width and height are the same as the current width and height of 6889f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * the source bitmap, the source bitmap is returned and no new bitmap is 689f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * created. 69002d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick * 69102d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick * @param src The source bitmap. 69202d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick * @param dstWidth The new bitmap's desired width. 69302d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick * @param dstHeight The new bitmap's desired height. 69402d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick * @param filter true if the source should be filtered. 695f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * @return The new scaled bitmap or the source bitmap if no scaling is required. 696790552b240c05d58407f7c14acba656b2e85c523Romain Guy * @throws IllegalArgumentException if width is <= 0, or height is <= 0 69702d9102b3bdebac6989c2b7d12c080fb7c1f8e2bBrad Fitzpatrick */ 6988242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, 699f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy boolean filter) { 7008242656f495847c50c9ceaea92839dce78218a75Romain Guy Matrix m = new Matrix(); 7018cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final int width = src.getWidth(); 7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final int height = src.getHeight(); 7048242656f495847c50c9ceaea92839dce78218a75Romain Guy if (width != dstWidth || height != dstHeight) { 7058242656f495847c50c9ceaea92839dce78218a75Romain Guy final float sx = dstWidth / (float) width; 7068242656f495847c50c9ceaea92839dce78218a75Romain Guy final float sy = dstHeight / (float) height; 7078242656f495847c50c9ceaea92839dce78218a75Romain Guy m.setScale(sx, sy); 7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7098242656f495847c50c9ceaea92839dce78218a75Romain Guy return Bitmap.createBitmap(src, 0, 0, width, height, m, filter); 7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7118cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns an immutable bitmap from the source bitmap. The new bitmap may 71496e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * be the same object as source, or a copy may have been made. It is 7158242656f495847c50c9ceaea92839dce78218a75Romain Guy * initialized with the same density and color space as the original bitmap. 7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 7178242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@NonNull Bitmap src) { 7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return createBitmap(src, 0, 0, src.getWidth(), src.getHeight()); 7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns an immutable bitmap from the specified subset of the source 7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * bitmap. The new bitmap may be the same object as source, or a copy may 7248242656f495847c50c9ceaea92839dce78218a75Romain Guy * have been made. It is initialized with the same density and color space 7258242656f495847c50c9ceaea92839dce78218a75Romain Guy * as the original bitmap. 7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param source The bitmap we are subsetting 7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x The x coordinate of the first pixel in source 7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y The y coordinate of the first pixel in source 7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The number of pixels in each row 7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The number of rows 732f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * @return A copy of a subset of the source bitmap or the source bitmap itself. 733790552b240c05d58407f7c14acba656b2e85c523Romain Guy * @throws IllegalArgumentException if the x, y, width, height values are 734790552b240c05d58407f7c14acba656b2e85c523Romain Guy * outside of the dimensions of the source bitmap, or width is <= 0, 735790552b240c05d58407f7c14acba656b2e85c523Romain Guy * or height is <= 0 7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 7378242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height) { 7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return createBitmap(source, x, y, width, height, null, false); 7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7408cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns an immutable bitmap from subset of the source bitmap, 743f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * transformed by the optional matrix. The new bitmap may be the 744f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * same object as source, or a copy may have been made. It is 7458242656f495847c50c9ceaea92839dce78218a75Romain Guy * initialized with the same density and color space as the original 7468242656f495847c50c9ceaea92839dce78218a75Romain Guy * bitmap. 747f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 748f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * If the source bitmap is immutable and the requested subset is the 749f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * same as the source bitmap itself, then the source bitmap is 750f12f6f058f169ead83a546a1c3a51e49452a30b3Romain Guy * returned and no new bitmap is created. 7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param source The bitmap we are subsetting 7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x The x coordinate of the first pixel in source 7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y The y coordinate of the first pixel in source 7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The number of pixels in each row 7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The number of rows 75760b88edea7132ddce90f2dced07c6706f1502270Ken Shirriff * @param m Optional matrix to be applied to the pixels 7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param filter true if the source should be filtered. 7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Only applies if the matrix contains more than just 7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * translation. 7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return A bitmap that represents the specified subset of source 7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if the x, y, width, height values are 763790552b240c05d58407f7c14acba656b2e85c523Romain Guy * outside of the dimensions of the source bitmap, or width is <= 0, 764790552b240c05d58407f7c14acba656b2e85c523Romain Guy * or height is <= 0 7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 7668242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@NonNull Bitmap source, int x, int y, int width, int height, 7678242656f495847c50c9ceaea92839dce78218a75Romain Guy @Nullable Matrix m, boolean filter) { 7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkXYSign(x, y); 7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkWidthHeight(width, height); 7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (x + width > source.getWidth()) { 7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("x + width must be <= bitmap.width()"); 7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (y + height > source.getHeight()) { 7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("y + height must be <= bitmap.height()"); 7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // check if we can just return our argument unchanged 7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!source.isMutable() && x == 0 && y == 0 && width == source.getWidth() && 7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project height == source.getHeight() && (m == null || m.isIdentity())) { 7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return source; 7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 7838cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 78481f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv boolean isHardware = source.getConfig() == Config.HARDWARE; 78581f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv if (isHardware) { 7869fd259c9b0878ba52fec86173d76976e70977fcdsergeyv source.noteHardwareBitmapSlowCall(); 78781f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv source = nativeCopyPreserveInternalConfig(source.mNativePtr); 78881f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv } 78981f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv 7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int neww = width; 7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int newh = height; 7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Bitmap bitmap; 7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Paint paint; 7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Rect srcR = new Rect(x, y, x + width, y + height); 7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project RectF dstR = new RectF(0, 0, width, height); 79729cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger RectF deviceR = new RectF(); 7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 799feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy Config newConfig = Config.ARGB_8888; 800feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy final Config config = source.getConfig(); 801feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy // GIF files generate null configs, assume ARGB_8888 802feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy if (config != null) { 803feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy switch (config) { 804feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy case RGB_565: 805feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy newConfig = Config.RGB_565; 806feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy break; 807feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy case ALPHA_8: 808feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy newConfig = Config.ALPHA_8; 809feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy break; 8109505a6552764461c22ce48f1ac13d025d23e1579Romain Guy case RGBA_F16: 8119505a6552764461c22ce48f1ac13d025d23e1579Romain Guy newConfig = Config.RGBA_F16; 8129505a6552764461c22ce48f1ac13d025d23e1579Romain Guy break; 813676b17391fb6583e5df944c36b1bd4c1be835689Romain Guy //noinspection deprecation 814feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy case ARGB_4444: 815feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy case ARGB_8888: 816feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy default: 817feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy newConfig = Config.ARGB_8888; 818feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy break; 819feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy } 820feeea8f07c854e6b0ae2fec3aa2d239c17f32b5bRomain Guy } 821d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy 8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (m == null || m.isIdentity()) { 823d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy bitmap = createBitmap(neww, newh, newConfig, source.hasAlpha()); 8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project paint = null; // not needed 8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else { 826d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy final boolean transformed = !m.rectStaysRect(); 827d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy 8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project m.mapRect(deviceR, dstR); 829d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy 8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project neww = Math.round(deviceR.width()); 8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project newh = Math.round(deviceR.height()); 832d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy 8339505a6552764461c22ce48f1ac13d025d23e1579Romain Guy Config transformedConfig = newConfig; 8349505a6552764461c22ce48f1ac13d025d23e1579Romain Guy if (transformed) { 8359505a6552764461c22ce48f1ac13d025d23e1579Romain Guy if (transformedConfig != Config.ARGB_8888 && transformedConfig != Config.RGBA_F16) { 8369505a6552764461c22ce48f1ac13d025d23e1579Romain Guy transformedConfig = Config.ARGB_8888; 8379505a6552764461c22ce48f1ac13d025d23e1579Romain Guy } 8389505a6552764461c22ce48f1ac13d025d23e1579Romain Guy } 8399505a6552764461c22ce48f1ac13d025d23e1579Romain Guy bitmap = createBitmap(neww, newh, transformedConfig, transformed || source.hasAlpha()); 840d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy 8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project paint = new Paint(); 8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project paint.setFilterBitmap(filter); 843d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy if (transformed) { 8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project paint.setAntiAlias(true); 8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 8478790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III 8488242656f495847c50c9ceaea92839dce78218a75Romain Guy nativeCopyColorSpace(source.mNativePtr, bitmap.mNativePtr); 8498242656f495847c50c9ceaea92839dce78218a75Romain Guy 8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // The new bitmap was created from a known bitmap source so assume that 85111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn // they use the same density 85211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn bitmap.mDensity = source.mDensity; 85357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III bitmap.setHasAlpha(source.hasAlpha()); 85457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III bitmap.setPremultiplied(source.mRequestPremultiplied); 8558790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III 85629cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger Canvas canvas = new Canvas(bitmap); 85729cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger canvas.translate(-deviceR.left, -deviceR.top); 85829cd3e922612afff4cd5fa9694013e5e8ae93661Derek Sollenberger canvas.concat(m); 85996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn canvas.drawBitmap(source, srcR, dstR, paint); 8606311d0a079702b29984c0d31937345be105e1a5eDianne Hackborn canvas.setBitmap(null); 86181f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv if (isHardware) { 86281f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv return bitmap.copy(Config.HARDWARE, false); 86381f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv } 8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return bitmap; 8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 8668cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 86896e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * Returns a mutable bitmap with the specified width and height. Its 8698242656f495847c50c9ceaea92839dce78218a75Romain Guy * initial density is as per {@link #getDensity}. The newly created 8708242656f495847c50c9ceaea92839dce78218a75Romain Guy * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space. 8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The width of the bitmap 8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The height of the bitmap 8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param config The bitmap config to create. 8759fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * @throws IllegalArgumentException if the width or height are <= 0, or if 8769fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * Config is Config.HARDWARE, because hardware bitmaps are always immutable 8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 8788242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(int width, int height, @NonNull Config config) { 879d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy return createBitmap(width, height, config, true); 880d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy } 881d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy 882d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy /** 883d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy * Returns a mutable bitmap with the specified width and height. Its 884dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * initial density is determined from the given {@link DisplayMetrics}. 8858242656f495847c50c9ceaea92839dce78218a75Romain Guy * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB} 8868242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 887dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * 888dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param display Display metrics for the display this bitmap will be 889dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * drawn on. 890dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param width The width of the bitmap 891dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param height The height of the bitmap 892dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param config The bitmap config to create. 8939fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * @throws IllegalArgumentException if the width or height are <= 0, or if 8949fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * Config is Config.HARDWARE, because hardware bitmaps are always immutable 895dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn */ 8968242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, 8978242656f495847c50c9ceaea92839dce78218a75Romain Guy int height, @NonNull Config config) { 898dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return createBitmap(display, width, height, config, true); 899dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn } 900dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn 901dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn /** 902dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * Returns a mutable bitmap with the specified width and height. Its 9038242656f495847c50c9ceaea92839dce78218a75Romain Guy * initial density is as per {@link #getDensity}. The newly created 9048242656f495847c50c9ceaea92839dce78218a75Romain Guy * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space. 905d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy * 906d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy * @param width The width of the bitmap 907d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy * @param height The height of the bitmap 908d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy * @param config The bitmap config to create. 9099505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to 9109505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * mark the bitmap as opaque. Doing so will clear the bitmap in black 911f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * instead of transparent. 912f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 9139fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * @throws IllegalArgumentException if the width or height are <= 0, or if 9149fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * Config is Config.HARDWARE, because hardware bitmaps are always immutable 915d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy */ 9168242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(int width, int height, 9178242656f495847c50c9ceaea92839dce78218a75Romain Guy @NonNull Config config, boolean hasAlpha) { 918dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return createBitmap(null, width, height, config, hasAlpha); 919dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn } 920dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn 921dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn /** 922dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * Returns a mutable bitmap with the specified width and height. Its 9238242656f495847c50c9ceaea92839dce78218a75Romain Guy * initial density is as per {@link #getDensity}. 9248242656f495847c50c9ceaea92839dce78218a75Romain Guy * 9258242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param width The width of the bitmap 9268242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param height The height of the bitmap 9278242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param config The bitmap config to create. 9288242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to 9298242656f495847c50c9ceaea92839dce78218a75Romain Guy * mark the bitmap as opaque. Doing so will clear the bitmap in black 9308242656f495847c50c9ceaea92839dce78218a75Romain Guy * instead of transparent. 9311fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * @param colorSpace The color space of the bitmap. If the config is {@link Config#RGBA_F16}, 9321fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * {@link ColorSpace.Named#EXTENDED_SRGB scRGB} is assumed, and if the 9331fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * config is not {@link Config#ARGB_8888}, {@link ColorSpace.Named#SRGB sRGB} 9341fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * is assumed. 9358242656f495847c50c9ceaea92839dce78218a75Romain Guy * 9368242656f495847c50c9ceaea92839dce78218a75Romain Guy * @throws IllegalArgumentException if the width or height are <= 0, if 9378242656f495847c50c9ceaea92839dce78218a75Romain Guy * Config is Config.HARDWARE (because hardware bitmaps are always 9388242656f495847c50c9ceaea92839dce78218a75Romain Guy * immutable), if the specified color space is not {@link ColorSpace.Model#RGB RGB}, 9391fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * if the specified color space's transfer function is not an 9401fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}, or if 9411fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * the color space is null 9428242656f495847c50c9ceaea92839dce78218a75Romain Guy */ 9438242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(int width, int height, @NonNull Config config, 9441fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy boolean hasAlpha, @NonNull ColorSpace colorSpace) { 9458242656f495847c50c9ceaea92839dce78218a75Romain Guy return createBitmap(null, width, height, config, hasAlpha, colorSpace); 9468242656f495847c50c9ceaea92839dce78218a75Romain Guy } 9478242656f495847c50c9ceaea92839dce78218a75Romain Guy 9488242656f495847c50c9ceaea92839dce78218a75Romain Guy /** 9498242656f495847c50c9ceaea92839dce78218a75Romain Guy * Returns a mutable bitmap with the specified width and height. Its 950dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * initial density is determined from the given {@link DisplayMetrics}. 9518242656f495847c50c9ceaea92839dce78218a75Romain Guy * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB} 9528242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 953dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * 954dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param display Display metrics for the display this bitmap will be 955dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * drawn on. 956dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param width The width of the bitmap 957dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param height The height of the bitmap 958dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param config The bitmap config to create. 9599505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to 9609505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * mark the bitmap as opaque. Doing so will clear the bitmap in black 961f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * instead of transparent. 962f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 9639fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * @throws IllegalArgumentException if the width or height are <= 0, or if 9649fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv * Config is Config.HARDWARE, because hardware bitmaps are always immutable 965dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn */ 9668242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height, 9678242656f495847c50c9ceaea92839dce78218a75Romain Guy @NonNull Config config, boolean hasAlpha) { 9681fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy return createBitmap(display, width, height, config, hasAlpha, 9691fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy ColorSpace.get(ColorSpace.Named.SRGB)); 9708242656f495847c50c9ceaea92839dce78218a75Romain Guy } 9718242656f495847c50c9ceaea92839dce78218a75Romain Guy 9728242656f495847c50c9ceaea92839dce78218a75Romain Guy /** 9738242656f495847c50c9ceaea92839dce78218a75Romain Guy * Returns a mutable bitmap with the specified width and height. Its 9748242656f495847c50c9ceaea92839dce78218a75Romain Guy * initial density is determined from the given {@link DisplayMetrics}. 9758242656f495847c50c9ceaea92839dce78218a75Romain Guy * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB} 9768242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 9778242656f495847c50c9ceaea92839dce78218a75Romain Guy * 9788242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param display Display metrics for the display this bitmap will be 9798242656f495847c50c9ceaea92839dce78218a75Romain Guy * drawn on. 9808242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param width The width of the bitmap 9818242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param height The height of the bitmap 9828242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param config The bitmap config to create. 9838242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param hasAlpha If the bitmap is ARGB_8888 or RGBA_16F this flag can be used to 9848242656f495847c50c9ceaea92839dce78218a75Romain Guy * mark the bitmap as opaque. Doing so will clear the bitmap in black 9858242656f495847c50c9ceaea92839dce78218a75Romain Guy * instead of transparent. 9861fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * @param colorSpace The color space of the bitmap. If the config is {@link Config#RGBA_F16}, 9871fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * {@link ColorSpace.Named#EXTENDED_SRGB scRGB} is assumed, and if the 9881fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * config is not {@link Config#ARGB_8888}, {@link ColorSpace.Named#SRGB sRGB} 9891fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * is assumed. 9908242656f495847c50c9ceaea92839dce78218a75Romain Guy * 9918242656f495847c50c9ceaea92839dce78218a75Romain Guy * @throws IllegalArgumentException if the width or height are <= 0, if 9928242656f495847c50c9ceaea92839dce78218a75Romain Guy * Config is Config.HARDWARE (because hardware bitmaps are always 9938242656f495847c50c9ceaea92839dce78218a75Romain Guy * immutable), if the specified color space is not {@link ColorSpace.Model#RGB RGB}, 9941fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * if the specified color space's transfer function is not an 9951fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}, or if 9961fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy * the color space is null 9978242656f495847c50c9ceaea92839dce78218a75Romain Guy */ 9988242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height, 9991fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy @NonNull Config config, boolean hasAlpha, @NonNull ColorSpace colorSpace) { 1000e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy if (width <= 0 || height <= 0) { 1001e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy throw new IllegalArgumentException("width and height must be > 0"); 1002e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy } 10039fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv if (config == Config.HARDWARE) { 10049fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv throw new IllegalArgumentException("can't create mutable bitmap with Config.HARDWARE"); 10059fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv } 10061fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy if (colorSpace == null) { 10071fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy throw new IllegalArgumentException("can't create bitmap without a color space"); 10081fa9d86ea4ce1d2344deaccad002413f88317010Romain Guy } 10098242656f495847c50c9ceaea92839dce78218a75Romain Guy 10108242656f495847c50c9ceaea92839dce78218a75Romain Guy Bitmap bm; 101108587d45b103d749c1483ec7fae80888985b5eaaRomain Guy // nullptr color spaces have a particular meaning in native and are interpreted as sRGB 101208587d45b103d749c1483ec7fae80888985b5eaaRomain Guy // (we also avoid the unnecessary extra work of the else branch) 101308587d45b103d749c1483ec7fae80888985b5eaaRomain Guy if (config != Config.ARGB_8888 || colorSpace == ColorSpace.get(ColorSpace.Named.SRGB)) { 10148242656f495847c50c9ceaea92839dce78218a75Romain Guy bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true, null, null); 10158242656f495847c50c9ceaea92839dce78218a75Romain Guy } else { 10168242656f495847c50c9ceaea92839dce78218a75Romain Guy if (!(colorSpace instanceof ColorSpace.Rgb)) { 10178242656f495847c50c9ceaea92839dce78218a75Romain Guy throw new IllegalArgumentException("colorSpace must be an RGB color space"); 10188242656f495847c50c9ceaea92839dce78218a75Romain Guy } 10198242656f495847c50c9ceaea92839dce78218a75Romain Guy ColorSpace.Rgb rgb = (ColorSpace.Rgb) colorSpace; 10208242656f495847c50c9ceaea92839dce78218a75Romain Guy ColorSpace.Rgb.TransferParameters parameters = rgb.getTransferParameters(); 10218242656f495847c50c9ceaea92839dce78218a75Romain Guy if (parameters == null) { 10228242656f495847c50c9ceaea92839dce78218a75Romain Guy throw new IllegalArgumentException("colorSpace must use an ICC " 10238242656f495847c50c9ceaea92839dce78218a75Romain Guy + "parametric transfer function"); 10248242656f495847c50c9ceaea92839dce78218a75Romain Guy } 10258242656f495847c50c9ceaea92839dce78218a75Romain Guy 10268242656f495847c50c9ceaea92839dce78218a75Romain Guy ColorSpace.Rgb d50 = (ColorSpace.Rgb) ColorSpace.adapt(rgb, ColorSpace.ILLUMINANT_D50); 10278242656f495847c50c9ceaea92839dce78218a75Romain Guy bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true, 10288242656f495847c50c9ceaea92839dce78218a75Romain Guy d50.getTransform(), parameters); 10298242656f495847c50c9ceaea92839dce78218a75Romain Guy } 10308242656f495847c50c9ceaea92839dce78218a75Romain Guy 1031dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn if (display != null) { 1032dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn bm.mDensity = display.densityDpi; 1033dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn } 10348790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III bm.setHasAlpha(hasAlpha); 10359505a6552764461c22ce48f1ac13d025d23e1579Romain Guy if ((config == Config.ARGB_8888 || config == Config.RGBA_F16) && !hasAlpha) { 1036775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeErase(bm.mNativePtr, 0xff000000); 1037d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy } 10383b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy // No need to initialize the bitmap to zeroes with other configs; 10393b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy // it is backed by a VM byte array which is by definition preinitialized 10403b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy // to all zeroes. 10419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return bm; 10429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 10438cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 10449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 10459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns a immutable bitmap with the specified width and height, with each 104696e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * pixel value set to the corresponding value in the colors array. Its 10478242656f495847c50c9ceaea92839dce78218a75Romain Guy * initial density is as per {@link #getDensity}. The newly created 10488242656f495847c50c9ceaea92839dce78218a75Romain Guy * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space. 10499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 10508242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param colors Array of sRGB {@link Color colors} used to initialize the pixels. 10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param offset Number of values to skip before the first color in the 10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * array of colors. 10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param stride Number of colors in the array between rows (must be >= 10549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * width or <= -width). 10559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The width of the bitmap 10569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The height of the bitmap 10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param config The bitmap config to create. If the config does not 10589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * support per-pixel alpha (e.g. RGB_565), then the alpha 10599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * bytes in the colors[] will be ignored (assumed to be FF) 10609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if the width or height are <= 0, or if 10619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the color array's length is less than the number of pixels. 10629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 10638242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@NonNull @ColorInt int[] colors, int offset, int stride, 10648242656f495847c50c9ceaea92839dce78218a75Romain Guy int width, int height, @NonNull Config config) { 1065dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return createBitmap(null, colors, offset, stride, width, height, config); 1066dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn } 1067dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn 1068dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn /** 1069dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * Returns a immutable bitmap with the specified width and height, with each 1070dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * pixel value set to the corresponding value in the colors array. Its 1071dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * initial density is determined from the given {@link DisplayMetrics}. 10728242656f495847c50c9ceaea92839dce78218a75Romain Guy * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB} 10738242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 1074dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * 1075dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param display Display metrics for the display this bitmap will be 1076dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * drawn on. 10778242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param colors Array of sRGB {@link Color colors} used to initialize the pixels. 1078dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param offset Number of values to skip before the first color in the 1079dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * array of colors. 1080dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param stride Number of colors in the array between rows (must be >= 1081dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * width or <= -width). 1082dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param width The width of the bitmap 1083dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param height The height of the bitmap 1084dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param config The bitmap config to create. If the config does not 1085dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * support per-pixel alpha (e.g. RGB_565), then the alpha 1086dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * bytes in the colors[] will be ignored (assumed to be FF) 1087dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @throws IllegalArgumentException if the width or height are <= 0, or if 1088dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * the color array's length is less than the number of pixels. 1089dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn */ 10908242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@NonNull DisplayMetrics display, 10918242656f495847c50c9ceaea92839dce78218a75Romain Guy @NonNull @ColorInt int[] colors, int offset, int stride, 10928242656f495847c50c9ceaea92839dce78218a75Romain Guy int width, int height, @NonNull Config config) { 10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkWidthHeight(width, height); 10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (Math.abs(stride) < width) { 10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("abs(stride) must be >= width"); 10979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 10989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int lastScanline = offset + (height - 1) * stride; 10999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int length = colors.length; 11009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (offset < 0 || (offset + width > length) || lastScanline < 0 || 11019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project (lastScanline + width > length)) { 11029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new ArrayIndexOutOfBoundsException(); 11039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1104e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy if (width <= 0 || height <= 0) { 1105e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy throw new IllegalArgumentException("width and height must be > 0"); 1106e4ac2d6b5723c95e648c489b187ddde449452c13Patrick Dubroy } 1107dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn Bitmap bm = nativeCreate(colors, offset, stride, width, height, 11088242656f495847c50c9ceaea92839dce78218a75Romain Guy config.nativeInt, false, null, null); 1109dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn if (display != null) { 1110dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn bm.mDensity = display.densityDpi; 1111dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn } 1112dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return bm; 11139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 11149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 11159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 11169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns a immutable bitmap with the specified width and height, with each 111796e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * pixel value set to the corresponding value in the colors array. Its 11188242656f495847c50c9ceaea92839dce78218a75Romain Guy * initial density is as per {@link #getDensity}. The newly created 11198242656f495847c50c9ceaea92839dce78218a75Romain Guy * bitmap is in the {@link ColorSpace.Named#SRGB sRGB} color space. 11209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 11218242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param colors Array of sRGB {@link Color colors} used to initialize the pixels. 11229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * This array must be at least as large as width * height. 11239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The width of the bitmap 11249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The height of the bitmap 11259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param config The bitmap config to create. If the config does not 11269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * support per-pixel alpha (e.g. RGB_565), then the alpha 11279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * bytes in the colors[] will be ignored (assumed to be FF) 11289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if the width or height are <= 0, or if 11299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the color array's length is less than the number of pixels. 11309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 11318242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@NonNull @ColorInt int[] colors, 11328242656f495847c50c9ceaea92839dce78218a75Romain Guy int width, int height, Config config) { 1133dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return createBitmap(null, colors, 0, width, width, height, config); 1134dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn } 1135dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn 1136dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn /** 1137dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * Returns a immutable bitmap with the specified width and height, with each 1138dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * pixel value set to the corresponding value in the colors array. Its 1139dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * initial density is determined from the given {@link DisplayMetrics}. 11408242656f495847c50c9ceaea92839dce78218a75Romain Guy * The newly created bitmap is in the {@link ColorSpace.Named#SRGB sRGB} 11418242656f495847c50c9ceaea92839dce78218a75Romain Guy * color space. 1142dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * 1143dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param display Display metrics for the display this bitmap will be 1144dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * drawn on. 11458242656f495847c50c9ceaea92839dce78218a75Romain Guy * @param colors Array of sRGB {@link Color colors} used to initialize the pixels. 1146dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * This array must be at least as large as width * height. 1147dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param width The width of the bitmap 1148dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param height The height of the bitmap 1149dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @param config The bitmap config to create. If the config does not 1150dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * support per-pixel alpha (e.g. RGB_565), then the alpha 1151dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * bytes in the colors[] will be ignored (assumed to be FF) 1152dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * @throws IllegalArgumentException if the width or height are <= 0, or if 1153dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn * the color array's length is less than the number of pixels. 1154dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn */ 11558242656f495847c50c9ceaea92839dce78218a75Romain Guy public static Bitmap createBitmap(@Nullable DisplayMetrics display, 11568242656f495847c50c9ceaea92839dce78218a75Romain Guy @NonNull @ColorInt int colors[], int width, int height, @NonNull Config config) { 1157dde331cebd87982faded6818ad5f9927ff994c96Dianne Hackborn return createBitmap(display, colors, 0, width, width, height, config); 11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 11609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 11619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns an optional array of private data, used by the UI system for 11629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * some bitmaps. Not intended to be called by applications. 11639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 11649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public byte[] getNinePatchChunk() { 11659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mNinePatchChunk; 11669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 11679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 11689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1169c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette * Populates a rectangle with the bitmap's optical insets. 1170c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette * 1171c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette * @param outInsets Rect to populate with optical insets 1172ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani * @hide 1173ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani */ 1174c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette public void getOpticalInsets(@NonNull Rect outInsets) { 117547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik if (mNinePatchInsets == null) { 1176c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette outInsets.setEmpty(); 1177c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette } else { 117847cd8e921db73e894f94ec4729ade90da50996f5Chris Craik outInsets.set(mNinePatchInsets.opticalRect); 1179c054966b719c8a7255f7cf4120cca5050acb68bdAlan Viverette } 1180ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani } 1181ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani 118247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik /** @hide */ 118347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik public NinePatch.InsetStruct getNinePatchInsets() { 118447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik return mNinePatchInsets; 118547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik } 118647cd8e921db73e894f94ec4729ade90da50996f5Chris Craik 1187ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani /** 11889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Specifies the known formats a bitmap can be compressed into 11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public enum CompressFormat { 11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project JPEG (0), 11922305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora PNG (1), 11932305ac9e4a262ed09fd034ae417e9b1dda4c0ccbVikas Arora WEBP (2); 11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project CompressFormat(int nativeInt) { 11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project this.nativeInt = nativeInt; 11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final int nativeInt; 11999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 12009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 12019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Number of bytes of temp storage we use for communicating between the 12039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * native compressor and the java OutputStream. 12049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 12059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private final static int WORKING_COMPRESS_STORAGE = 4096; 12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 12079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 12089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Write a compressed version of the bitmap to the specified outputstream. 12099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * If this returns true, the bitmap can be reconstructed by passing a 12109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * corresponding inputstream to BitmapFactory.decodeStream(). Note: not 12119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * all Formats support all bitmap configs directly, so it is possible that 12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the returned bitmap from BitmapFactory could be in a different bitdepth, 12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque 12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * pixels). 12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param format The format of the compressed image 12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param quality Hint to the compressor, 0-100. 0 meaning compress for 12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * small size, 100 meaning compress for max quality. Some 12199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * formats, like PNG which is lossless, will ignore the 12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * quality setting 12219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param stream The outputstream to write the compressed data. 12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return true if successfully compressed to the specified stream. 12239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 12249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public boolean compress(CompressFormat format, int quality, OutputStream stream) { 12259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't compress a recycled bitmap"); 12269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // do explicit check before calling the native method 12279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (stream == null) { 12289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new NullPointerException(); 12299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 12309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (quality < 0 || quality > 100) { 12319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("quality must be 0..100"); 12329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 12339fd259c9b0878ba52fec86173d76976e70977fcdsergeyv StrictMode.noteSlowCall("Compression of a bitmap is slow"); 12340df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress"); 1235775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler boolean result = nativeCompress(mNativePtr, format.nativeInt, 1236f29ed28c7b878ef28058bc730715d0d32445bc57John Reck quality, stream, new byte[WORKING_COMPRESS_STORAGE]); 12370df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); 12380df3bd5594bbbbfbf6058ecb31e9b94a1cd22f7dJohn Reck return result; 12399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 12408cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 12419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1242ff236f5fc1a088b1210d37a67b0e88b06c0fbdb2Newton Allen * Returns true if the bitmap is marked as mutable (i.e. can be drawn into) 12439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 12449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final boolean isMutable() { 12459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mIsMutable; 12469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 12479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 12483849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy /** 12493849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied. 12503849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * When a pixel is pre-multiplied, the RGB components have been multiplied by 12513849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * the alpha component. For instance, if the original color is a 50% 1252f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is 12533849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <code>(128, 128, 0, 0)</code>.</p> 1254f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 12553849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>This method always returns false if {@link #getConfig()} is 12564ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy * {@link Bitmap.Config#RGB_565}.</p> 1257f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 125857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * <p>The return value is undefined if {@link #getConfig()} is 125957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * {@link Bitmap.Config#ALPHA_8}.</p> 126057ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III * 12614ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy * <p>This method only returns true if {@link #hasAlpha()} returns true. 12624ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy * A bitmap with no alpha channel can be used both as a pre-multiplied and 12634ff0cf4b83605bff630c4e6f1fabe4f72a3f93a1Romain Guy * as a non pre-multiplied bitmap.</p> 12641abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 12651abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * <p>Only pre-multiplied bitmaps may be drawn by the view system or 12661abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * {@link Canvas}. If a non-pre-multiplied bitmap with an alpha channel is 12671abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * drawn to a Canvas, a RuntimeException will be thrown.</p> 12681abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 12693849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * @return true if the underlying pixels have been pre-multiplied, false 12703849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * otherwise 12711abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 12721abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * @see Bitmap#setPremultiplied(boolean) 12731abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * @see BitmapFactory.Options#inPremultiplied 12743849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy */ 12753849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy public final boolean isPremultiplied() { 127601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 127701a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called isPremultiplied() on a recycle()'d bitmap! This is undefined behavior!"); 127801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 1279775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeIsPremultiplied(mNativePtr); 12801abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik } 12811abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik 12821abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik /** 12831abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * Sets whether the bitmap should treat its data as pre-multiplied. 12841abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 12851abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * <p>Bitmaps are always treated as pre-multiplied by the view system and 12861abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * {@link Canvas} for performance reasons. Storing un-pre-multiplied data in 12871abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * a Bitmap (through {@link #setPixel}, {@link #setPixels}, or {@link 12881abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * BitmapFactory.Options#inPremultiplied BitmapFactory.Options.inPremultiplied}) 12891abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * can lead to incorrect blending if drawn by the framework.</p> 12901abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 12911abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * <p>This method will not affect the behavior of a bitmap without an alpha 12921abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * channel, or if {@link #hasAlpha()} returns false.</p> 12931abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * 1294889a1ddd00b74933d5f7daa651d98133bcb034baBen Murdoch * <p>Calling {@link #createBitmap} or {@link #createScaledBitmap} with a source 12958790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III * Bitmap whose colors are not pre-multiplied may result in a RuntimeException, 12968790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III * since those functions require drawing the source, which is not supported for 12978790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III * un-pre-multiplied Bitmaps.</p> 12988790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III * 12991abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * @see Bitmap#isPremultiplied() 13001abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik * @see BitmapFactory.Options#inPremultiplied 13011abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik */ 13021abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik public final void setPremultiplied(boolean premultiplied) { 13033e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck checkRecycled("setPremultiplied called on a recycled bitmap"); 130457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III mRequestPremultiplied = premultiplied; 1305775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeSetPremultiplied(mNativePtr, premultiplied); 13063849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy } 13073849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy 13089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** Returns the bitmap's width */ 13099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final int getWidth() { 131001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 131101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called getWidth() on a recycle()'d bitmap! This is undefined behavior!"); 131201a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 13131abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik return mWidth; 13149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 13159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 13169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** Returns the bitmap's height */ 13179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final int getHeight() { 131801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 131901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called getHeight() on a recycle()'d bitmap! This is undefined behavior!"); 132001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 13211abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik return mHeight; 13229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 13238cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 13249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 132511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * Convenience for calling {@link #getScaledWidth(int)} with the target 132611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * density of the given {@link Canvas}. 13279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 13282784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn public int getScaledWidth(Canvas canvas) { 132911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return scaleFromDensity(getWidth(), mDensity, canvas.mDensity); 13302784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn } 13312784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn 13322784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn /** 133311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * Convenience for calling {@link #getScaledHeight(int)} with the target 133411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * density of the given {@link Canvas}. 13352784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn */ 13362784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn public int getScaledHeight(Canvas canvas) { 133711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return scaleFromDensity(getHeight(), mDensity, canvas.mDensity); 133811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn } 133911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn 134011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn /** 134111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * Convenience for calling {@link #getScaledWidth(int)} with the target 134211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * density of the given {@link DisplayMetrics}. 134311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn */ 134411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public int getScaledWidth(DisplayMetrics metrics) { 134511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return scaleFromDensity(getWidth(), mDensity, metrics.densityDpi); 134611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn } 134711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn 134811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn /** 134911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * Convenience for calling {@link #getScaledHeight(int)} with the target 135011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * density of the given {@link DisplayMetrics}. 135111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn */ 135211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public int getScaledHeight(DisplayMetrics metrics) { 135311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return scaleFromDensity(getHeight(), mDensity, metrics.densityDpi); 13542784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn } 13552784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn 13562784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn /** 13572784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn * Convenience method that returns the width of this bitmap divided 13582784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn * by the density scale factor. 13592784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn * 136011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @param targetDensity The density of the target canvas of the bitmap. 13612784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn * @return The scaled width of this bitmap, according to the density scale factor. 13622784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn */ 136311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public int getScaledWidth(int targetDensity) { 136411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return scaleFromDensity(getWidth(), mDensity, targetDensity); 13659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 13669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 13679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 13689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Convenience method that returns the height of this bitmap divided 13699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * by the density scale factor. 13709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 137111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @param targetDensity The density of the target canvas of the bitmap. 13729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The scaled height of this bitmap, according to the density scale factor. 13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 137411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public int getScaledHeight(int targetDensity) { 137511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return scaleFromDensity(getHeight(), mDensity, targetDensity); 137611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn } 1377f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 137811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn /** 137911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * @hide 138011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn */ 138111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn static public int scaleFromDensity(int size, int sdensity, int tdensity) { 13820b68477f8287fe5ddac1beb1c9d0811ded034dadRomain Guy if (sdensity == DENSITY_NONE || tdensity == DENSITY_NONE || sdensity == tdensity) { 138311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return size; 13842784ff0af88128f66ae690b73d48fb7e4a211e68Dianne Hackborn } 1385f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 138611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn // Scale by tdensity / sdensity, rounding up. 1387366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy return ((size * tdensity) + (sdensity >> 1)) / sdensity; 13889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1389f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 13909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 13919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return the number of bytes between rows in the bitmap's pixels. Note that 13929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * this refers to the pixels as stored natively by the bitmap. If you call 13939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * getPixels() or setPixels(), then the pixels are uniformly treated as 13949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 32bit values, packed according to the Color class. 13959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 1396e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, this method 13979f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * should not be used to calculate the memory usage of the bitmap. Instead, 13989f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * see {@link #getAllocationByteCount()}. 13999f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * 14009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return number of bytes between rows of the native bitmap pixels. 14019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 14029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final int getRowBytes() { 140301a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 140401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called getRowBytes() on a recycle()'d bitmap! This is undefined behavior!"); 140501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 1406775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeRowBytes(mNativePtr); 14079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 14088cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 14099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 14109f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * Returns the minimum number of bytes that can be used to store this bitmap's pixels. 14119f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * 1412e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, the result of this method can 14139f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * no longer be used to determine memory usage of a bitmap. See {@link 1414c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * #getAllocationByteCount()}.</p> 1415f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson */ 1416f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson public final int getByteCount() { 14173bdecbf0fd2d4b6137477094e1ea96502c353248John Reck if (mRecycled) { 14183bdecbf0fd2d4b6137477094e1ea96502c353248John Reck Log.w(TAG, "Called getByteCount() on a recycle()'d bitmap! " 14193bdecbf0fd2d4b6137477094e1ea96502c353248John Reck + "This is undefined behavior!"); 14203bdecbf0fd2d4b6137477094e1ea96502c353248John Reck return 0; 14213bdecbf0fd2d4b6137477094e1ea96502c353248John Reck } 1422f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson // int result permits bitmaps up to 46,340 x 46,340 1423f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson return getRowBytes() * getHeight(); 1424f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson } 1425f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson 1426f7f9d9c39df22ad6929f001f07588469f77e8bf5Jesse Wilson /** 14279f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * Returns the size of the allocated memory used to store this bitmap's pixels. 14289f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * 14299f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * <p>This can be larger than the result of {@link #getByteCount()} if a bitmap is reused to 1430c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * decode other bitmaps of smaller size, or by manual reconfiguration. See {@link 1431c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * #reconfigure(int, int, Config)}, {@link #setWidth(int)}, {@link #setHeight(int)}, {@link 1432c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * #setConfig(Bitmap.Config)}, and {@link BitmapFactory.Options#inBitmap 1433c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * BitmapFactory.Options.inBitmap}. If a bitmap is not modified in this way, this value will be 1434c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * the same as that returned by {@link #getByteCount()}.</p> 1435c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * 1436c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * <p>This value will not change over the lifetime of a Bitmap.</p> 14379f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik * 1438c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik * @see #reconfigure(int, int, Config) 14399f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik */ 14409f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik public final int getAllocationByteCount() { 14413bdecbf0fd2d4b6137477094e1ea96502c353248John Reck if (mRecycled) { 14423bdecbf0fd2d4b6137477094e1ea96502c353248John Reck Log.w(TAG, "Called getAllocationByteCount() on a recycle()'d bitmap! " 14433bdecbf0fd2d4b6137477094e1ea96502c353248John Reck + "This is undefined behavior!"); 14443bdecbf0fd2d4b6137477094e1ea96502c353248John Reck return 0; 14453bdecbf0fd2d4b6137477094e1ea96502c353248John Reck } 14464508218850faedea95371188da587b6734f5f3dasergeyv return nativeGetAllocationByteCount(mNativePtr); 14479f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik } 14489f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik 14499f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik /** 14509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * If the bitmap's internal config is in one of the public formats, return 14519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * that config, otherwise return null. 14529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 14539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final Config getConfig() { 145401a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 145501a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called getConfig() on a recycle()'d bitmap! This is undefined behavior!"); 145601a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 1457775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return Config.nativeToConfig(nativeConfig(mNativePtr)); 14589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 14599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1460a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed /** Returns true if the bitmap's config supports per-pixel alpha, and 1461a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * if the pixels may contain non-opaque alpha values. For some configs, 1462a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * this is always false (e.g. RGB_565), since they do not support per-pixel 1463a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * alpha. However, for configs that do, the bitmap may be flagged to be 1464a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * known that all of its pixels are opaque. In this case hasAlpha() will 1465a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * also return false. If a config such as ARGB_8888 is not so flagged, 1466a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * it will return true by default. 1467a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed */ 14689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final boolean hasAlpha() { 146901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 147001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!"); 147101a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 1472775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeHasAlpha(mNativePtr); 14739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 14749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 14759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1476a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * Tell the bitmap if all of the pixels are known to be opaque (false) 1477a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * or if some of the pixels may contain non-opaque alpha values (true). 1478366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy * Note, for some configs (e.g. RGB_565) this call is ignored, since it 1479366a84056c9a695ee6702d9d30bf9f3b521ba7ccRomain Guy * does not support per-pixel alpha values. 1480a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * 1481a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * This is meant as a drawing hint, as in some cases a bitmap that is known 1482a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * to be opaque can take a faster drawing case than one that may have 1483a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed * non-opaque per-pixel alpha values. 1484a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed */ 1485a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed public void setHasAlpha(boolean hasAlpha) { 14863e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck checkRecycled("setHasAlpha called on a recycled bitmap"); 1487775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeSetHasAlpha(mNativePtr, hasAlpha, mRequestPremultiplied); 1488a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed } 1489a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed 1490a78b0a2d9ebb38b86ed802b3d86de07d0b301262Mike Reed /** 1491713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * Indicates whether the renderer responsible for drawing this 1492713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * bitmap should attempt to use mipmaps when this bitmap is drawn 1493713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * scaled down. 1494f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 1495713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * If you know that you are going to draw this bitmap at less than 1496713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * 50% of its original size, you may be able to obtain a higher 1497713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * quality 1498f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 1499713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * This property is only a suggestion that can be ignored by the 1500713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * renderer. It is not guaranteed to have any effect. 1501f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 1502713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * @return true if the renderer should attempt to use mipmaps, 1503713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * false otherwise 1504f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 1505713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * @see #setHasMipMap(boolean) 1506713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy */ 1507713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy public final boolean hasMipMap() { 150801a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck if (mRecycled) { 150901a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck Log.w(TAG, "Called hasMipMap() on a recycle()'d bitmap! This is undefined behavior!"); 151001a0af31d7d418f400ce5d3f752eba6a35aa00e2John Reck } 1511775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeHasMipMap(mNativePtr); 1512713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy } 1513713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy 1514713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy /** 1515713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * Set a hint for the renderer responsible for drawing this bitmap 1516713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * indicating that it should attempt to use mipmaps when this bitmap 1517713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * is drawn scaled down. 1518713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * 1519713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * If you know that you are going to draw this bitmap at less than 1520713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * 50% of its original size, you may be able to obtain a higher 1521713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * quality by turning this property on. 1522f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 1523713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * Note that if the renderer respects this hint it might have to 1524713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * allocate extra memory to hold the mipmap levels for this bitmap. 1525713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * 1526713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * This property is only a suggestion that can be ignored by the 1527713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * renderer. It is not guaranteed to have any effect. 1528713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * 1529713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * @param hasMipMap indicates whether the renderer should attempt 1530713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * to use mipmaps 1531713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * 1532713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy * @see #hasMipMap() 1533713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy */ 1534713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy public final void setHasMipMap(boolean hasMipMap) { 15353e776dee3e8af646cdcfb7a60e0e301c99f05293John Reck checkRecycled("setHasMipMap called on a recycled bitmap"); 1536775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeSetHasMipMap(mNativePtr, hasMipMap); 1537713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy } 1538713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy 1539713e1bb9df6bdfc21bd5c40d1a6ecf6c822a4be5Romain Guy /** 1540efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy * Returns the color space associated with this bitmap. If the color 1541efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy * space is unknown, this method returns null. 1542efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy */ 1543efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy @Nullable 1544efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy public final ColorSpace getColorSpace() { 1545efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy // A reconfigure can change the configuration and rgba16f is 1546efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy // always linear scRGB at this time 1547efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy if (getConfig() == Config.RGBA_F16) { 1548efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy // Reset the color space for potential future reconfigurations 1549efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy mColorSpace = null; 1550efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy return ColorSpace.get(ColorSpace.Named.LINEAR_EXTENDED_SRGB); 1551efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } 1552efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy 1553efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy // Cache the color space retrieval since it can be fairly expensive 1554efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy if (mColorSpace == null) { 1555efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy if (nativeIsSRGB(mNativePtr)) { 1556efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy mColorSpace = ColorSpace.get(ColorSpace.Named.SRGB); 1557efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } else { 1558efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy float[] xyz = new float[9]; 1559efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy float[] params = new float[7]; 1560efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy 1561efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy boolean hasColorSpace = nativeGetColorSpace(mNativePtr, xyz, params); 1562efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy if (hasColorSpace) { 1563efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy ColorSpace.Rgb.TransferParameters parameters = 1564efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy new ColorSpace.Rgb.TransferParameters( 1565efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy params[0], params[1], params[2], 1566efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy params[3], params[4], params[5], params[6]); 1567efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy ColorSpace cs = ColorSpace.match(xyz, parameters); 1568efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy if (cs != null) { 1569efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy mColorSpace = cs; 1570efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } else { 1571efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy mColorSpace = new ColorSpace.Rgb("Unknown", xyz, parameters); 1572efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } 1573efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } 1574efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } 1575efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } 1576efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy 1577efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy return mColorSpace; 1578efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy } 1579efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy 1580efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy /** 15819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Fills the bitmap's pixels with the specified {@link Color}. 15829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 15839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalStateException if the bitmap is not mutable. 15849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 158580756e38882720860db52f1fcc21fa1505a02abfTor Norbye public void eraseColor(@ColorInt int c) { 15869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't erase a recycled bitmap"); 15879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!isMutable()) { 15889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalStateException("cannot erase immutable bitmaps"); 15899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1590775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeErase(mNativePtr, c); 15919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 15929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 15939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 15949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the {@link Color} at the specified location. Throws an exception 15959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * if x or y are out of bounds (negative or >= to the width or height 1596ce217faddb4b40c1b3e698944da1951027080427Romain Guy * respectively). The returned color is a non-premultiplied ARGB value in 1597ce217faddb4b40c1b3e698944da1951027080427Romain Guy * the {@link ColorSpace.Named#SRGB sRGB} color space. 15989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 15999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x The x coordinate (0...width-1) of the pixel to return 16009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y The y coordinate (0...height-1) of the pixel to return 16019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The argb {@link Color} at the specified coordinate 16029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if x, y exceed the bitmap's bounds 160357eafc6f90a1ce2dd97079dd82b4b2f8dcd9e00fsergeyv * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE} 16049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 160580756e38882720860db52f1fcc21fa1505a02abfTor Norbye @ColorInt 16069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public int getPixel(int x, int y) { 16079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't call getPixel() on a recycled bitmap"); 1608980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv checkHardware("unable to getPixel(), " 1609980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv + "pixel access is not supported on Config#HARDWARE bitmaps"); 16109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkPixelAccess(x, y); 1611775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeGetPixel(mNativePtr, x, y); 16129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16138cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 16149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 16159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns in pixels[] a copy of the data in the bitmap. Each value is 16169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * a packed int representing a {@link Color}. The stride parameter allows 16179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the caller to allow for gaps in the returned pixels array between 16189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * rows. For normal packed results, just pass width for the stride value. 1619ce217faddb4b40c1b3e698944da1951027080427Romain Guy * The returned colors are non-premultiplied ARGB values in the 1620ce217faddb4b40c1b3e698944da1951027080427Romain Guy * {@link ColorSpace.Named#SRGB sRGB} color space. 16219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 16229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param pixels The array to receive the bitmap's colors 16239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param offset The first index to write into pixels[] 16249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param stride The number of entries in pixels[] to skip between 16259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * rows (must be >= bitmap's width). Can be negative. 16269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x The x coordinate of the first pixel to read from 16279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the bitmap 16289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y The y coordinate of the first pixel to read from 16299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the bitmap 16309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The number of pixels to read from each row 16319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The number of rows to read 16323849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * 16339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if x, y, width, height exceed the 16349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * bounds of the bitmap, or if abs(stride) < width. 16359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws ArrayIndexOutOfBoundsException if the pixels array is too small 16369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to receive the specified number of pixels. 163757eafc6f90a1ce2dd97079dd82b4b2f8dcd9e00fsergeyv * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE} 16389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 163980756e38882720860db52f1fcc21fa1505a02abfTor Norbye public void getPixels(@ColorInt int[] pixels, int offset, int stride, 16409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int x, int y, int width, int height) { 16419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't call getPixels() on a recycled bitmap"); 1642980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv checkHardware("unable to getPixels(), " 1643980bead518fc5ddb52ae8d00258f91b087c1f91asergeyv + "pixel access is not supported on Config#HARDWARE bitmaps"); 16449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (width == 0 || height == 0) { 16459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; // nothing to do 16469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkPixelsAccess(x, y, width, height, offset, stride, pixels); 1648775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeGetPixels(mNativePtr, pixels, offset, stride, 164957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III x, y, width, height); 16509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16518cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 16529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 16539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Shared code to check for illegal arguments passed to getPixel() 16549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * or setPixel() 1655f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 16569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x x coordinate of the pixel 16579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y y coordinate of the pixel 16589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 16599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private void checkPixelAccess(int x, int y) { 16609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkXYSign(x, y); 16619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (x >= getWidth()) { 16629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("x must be < bitmap.width()"); 16639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (y >= getHeight()) { 16659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("y must be < bitmap.height()"); 16669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 16699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 16709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Shared code to check for illegal arguments passed to getPixels() 16719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * or setPixels() 16729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 16739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x left edge of the area of pixels to access 16749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y top edge of the area of pixels to access 16759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width width of the area of pixels to access 16769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height height of the area of pixels to access 16779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param offset offset into pixels[] array 16789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param stride number of elements in pixels[] between each logical row 16799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param pixels array to hold the area of pixels being accessed 16809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 16819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private void checkPixelsAccess(int x, int y, int width, int height, 16829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int offset, int stride, int pixels[]) { 16839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkXYSign(x, y); 16849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (width < 0) { 16859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("width must be >= 0"); 16869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (height < 0) { 16889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("height must be >= 0"); 16899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (x + width > getWidth()) { 16919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException( 16929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "x + width must be <= bitmap.width()"); 16939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (y + height > getHeight()) { 16959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException( 16969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project "y + height must be <= bitmap.height()"); 16979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (Math.abs(stride) < width) { 16999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalArgumentException("abs(stride) must be >= width"); 17009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int lastScanline = offset + (height - 1) * stride; 17029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int length = pixels.length; 17039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (offset < 0 || (offset + width > length) 17049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project || lastScanline < 0 17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project || (lastScanline + width > length)) { 17069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new ArrayIndexOutOfBoundsException(); 17079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17098cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 17113849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>Write the specified {@link Color} into the bitmap (assuming it is 17123849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * mutable) at the x,y coordinate. The color must be a 1713ce217faddb4b40c1b3e698944da1951027080427Romain Guy * non-premultiplied ARGB value in the {@link ColorSpace.Named#SRGB sRGB} 1714ce217faddb4b40c1b3e698944da1951027080427Romain Guy * color space.</p> 17159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 17169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x The x coordinate of the pixel to replace (0...width-1) 17179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y The y coordinate of the pixel to replace (0...height-1) 17183849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * @param color The ARGB color to write into the bitmap 17193849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * 17209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalStateException if the bitmap is not mutable 17219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if x, y are outside of the bitmap's 17229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * bounds. 17239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 172480756e38882720860db52f1fcc21fa1505a02abfTor Norbye public void setPixel(int x, int y, @ColorInt int color) { 17259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't call setPixel() on a recycled bitmap"); 17269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!isMutable()) { 17279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalStateException(); 17289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkPixelAccess(x, y); 1730775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeSetPixel(mNativePtr, x, y, color); 17319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17328cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 17339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 17343849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * <p>Replace pixels in the bitmap with the colors in the array. Each element 17359505a6552764461c22ce48f1ac13d025d23e1579Romain Guy * in the array is a packed int representing a non-premultiplied ARGB 1736ce217faddb4b40c1b3e698944da1951027080427Romain Guy * {@link Color} in the {@link ColorSpace.Named#SRGB sRGB} color space.</p> 17379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 17389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param pixels The colors to write to the bitmap 17399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param offset The index of the first color to read from pixels[] 17409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param stride The number of colors in pixels[] to skip between rows. 17419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Normally this value will be the same as the width of 17429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the bitmap, but it can be larger (or negative). 17439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param x The x coordinate of the first pixel to write to in 17449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the bitmap. 17459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param y The y coordinate of the first pixel to write to in 17469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the bitmap. 17479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param width The number of colors to copy from pixels[] per row 17489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param height The number of rows to write to the bitmap 17493849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy * 17509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalStateException if the bitmap is not mutable 17519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws IllegalArgumentException if x, y, width, height are outside of 17529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the bitmap's bounds. 17539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @throws ArrayIndexOutOfBoundsException if the pixels array is too small 17549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * to receive the specified number of pixels. 17559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 175680756e38882720860db52f1fcc21fa1505a02abfTor Norbye public void setPixels(@ColorInt int[] pixels, int offset, int stride, 17573849f93b3c2b29ca3873b602897dccac039a0b98Romain Guy int x, int y, int width, int height) { 17589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't call setPixels() on a recycled bitmap"); 17599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!isMutable()) { 17609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new IllegalStateException(); 17619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (width == 0 || height == 0) { 17639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; // nothing to do 17649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkPixelsAccess(x, y, width, height, offset, stride, pixels); 1766775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler nativeSetPixels(mNativePtr, pixels, offset, stride, 176757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III x, y, width, height); 17689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17698cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 17709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final Parcelable.Creator<Bitmap> CREATOR 17719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project = new Parcelable.Creator<Bitmap>() { 17729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 17739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Rebuilds a bitmap previously stored with writeToParcel(). 17749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 17759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param p Parcel object to read the bitmap from 17769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return a new bitmap created from the data in the parcel 17779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 17789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Bitmap createFromParcel(Parcel p) { 17799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Bitmap bm = nativeCreateFromParcel(p); 17809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (bm == null) { 17819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("Failed to unparcel Bitmap"); 17829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return bm; 17849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Bitmap[] newArray(int size) { 17869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return new Bitmap[size]; 17879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project }; 17899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 17909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 17919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * No special parcel contents. 17929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 17939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public int describeContents() { 1794b6377170960d40e66858d8b4d335a95eac773762Bart Sears return 0; 17959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 17969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 17979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 17989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Write the bitmap and its pixels to the parcel. The bitmap can be 17999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * rebuilt from the parcel by calling CREATOR.createFromParcel(). 1800f161dce70eab5ba1011bd26226c14e31675d5955sergeyv * 1801f161dce70eab5ba1011bd26226c14e31675d5955sergeyv * If this bitmap is {@link Config#HARDWARE}, it may be unparceled with a different pixel 1802f161dce70eab5ba1011bd26226c14e31675d5955sergeyv * format (e.g. 565, 8888), but the content will be preserved to the best quality permitted 1803f161dce70eab5ba1011bd26226c14e31675d5955sergeyv * by the final pixel format 18049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param p Parcel object to write the bitmap data into 18059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 18069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void writeToParcel(Parcel p, int flags) { 18079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't parcel a recycled bitmap"); 18089fd259c9b0878ba52fec86173d76976e70977fcdsergeyv noteHardwareBitmapSlowCall(); 1809775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler if (!nativeWriteToParcel(mNativePtr, mIsMutable, mDensity, p)) { 18109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("native writeToParcel failed"); 18119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 18129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 18139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 18149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 18159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns a new bitmap that captures the alpha values of the original. 18169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * This may be drawn with Canvas.drawBitmap(), where the color(s) will be 18179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * taken from the paint that is passed to the draw call. 18189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 18199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return new bitmap containing the alpha channel of the original bitmap. 18209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 18211c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye @CheckResult 18229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Bitmap extractAlpha() { 18239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return extractAlpha(null, null); 18249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 18258cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 18269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 18279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns a new bitmap that captures the alpha values of the original. 18289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * These values may be affected by the optional Paint parameter, which 18299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * can contain its own alpha, and may also contain a MaskFilter which 18309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * could change the actual dimensions of the resulting bitmap (e.g. 18319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * a blur maskfilter might enlarge the resulting bitmap). If offsetXY 18329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * is not null, it returns the amount to offset the returned bitmap so 18339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * that it will logically align with the original. For example, if the 18349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * paint contains a blur of radius 2, then offsetXY[] would contains 18359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * -2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then 18369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * drawing the original would result in the blur visually aligning with 18379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the original. 1838f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 183996e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn * <p>The initial density of the returned bitmap is the same as the original's. 1840f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck * 18419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param paint Optional paint used to modify the alpha values in the 18429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * resulting bitmap. Pass null for default behavior. 18439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param offsetXY Optional array that returns the X (index 0) and Y 18449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * (index 1) offset needed to position the returned bitmap 18459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * so that it visually lines up with the original. 18469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return new bitmap containing the (optionally modified by paint) alpha 18479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * channel of the original bitmap. This may be drawn with 18489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Canvas.drawBitmap(), where the color(s) will be taken from the 18499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * paint that is passed to the draw call. 18509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 18511c2bf03d1082fc6b7eb42cbd163c60c07cf2bcccTor Norbye @CheckResult 18529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Bitmap extractAlpha(Paint paint, int[] offsetXY) { 18539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project checkRecycled("Can't extractAlpha on a recycled bitmap"); 1854dfba4d3d11bbf47dff45f94d61d4d97510b3034aDerek Sollenberger long nativePaint = paint != null ? paint.getNativeInstance() : 0; 18559fd259c9b0878ba52fec86173d76976e70977fcdsergeyv noteHardwareBitmapSlowCall(); 1856775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler Bitmap bm = nativeExtractAlpha(mNativePtr, nativePaint, offsetXY); 18579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (bm == null) { 18589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project throw new RuntimeException("Failed to extractAlpha on Bitmap"); 18599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 186096e240f25a97c10bba863df328ed73a82c34ff61Dianne Hackborn bm.mDensity = mDensity; 18619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return bm; 18629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 18639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 18648cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen /** 186576d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed * Given another bitmap, return true if it has the same dimensions, config, 186676d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed * and pixel data as this bitmap. If any of those differ, return false. 186776d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed * If other is null, return false. 186876d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed */ 186976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed public boolean sameAs(Bitmap other) { 18709298c5414172344f2462beda70e624af5f774483John Reck checkRecycled("Can't call sameAs on a recycled bitmap!"); 18719fd259c9b0878ba52fec86173d76976e70977fcdsergeyv noteHardwareBitmapSlowCall(); 18723df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck if (this == other) return true; 18733df4869a77bdeb72d5810bbcf7819ed1b8dd0ec7John Reck if (other == null) return false; 1874e1fc36d197639ee13194de74b2e7e42c39b12286sergeyv other.noteHardwareBitmapSlowCall(); 18759298c5414172344f2462beda70e624af5f774483John Reck if (other.isRecycled()) { 18769298c5414172344f2462beda70e624af5f774483John Reck throw new IllegalArgumentException("Can't compare to a recycled bitmap!"); 18779298c5414172344f2462beda70e624af5f774483John Reck } 1878775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler return nativeSameAs(mNativePtr, other.mNativePtr); 187976d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed } 188076d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed 188176d1e01d5e65c4631c827831e98ad4e300d99eabMike Reed /** 1882b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * Builds caches associated with the bitmap that are used for drawing it. 1883b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * 1884b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * <p>Starting in {@link android.os.Build.VERSION_CODES#N}, this call initiates an asynchronous 1885b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * upload to the GPU on RenderThread, if the Bitmap is not already uploaded. With Hardware 1886b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * Acceleration, Bitmaps must be uploaded to the GPU in order to be rendered. This is done by 1887b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * default the first time a Bitmap is drawn, but the process can take several milliseconds, 1888b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * depending on the size of the Bitmap. Each time a Bitmap is modified and drawn again, it must 1889b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * be re-uploaded.</p> 1890b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * 1891b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * <p>Calling this method in advance can save time in the first frame it's used. For example, it 1892b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * is recommended to call this on an image decoding worker thread when a decoded Bitmap is about 1893b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * to be displayed. It is recommended to make any pre-draw modifications to the Bitmap before 1894b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * calling this method, so the cached, uploaded copy may be reused without re-uploading.</p> 1895b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * 1896b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * In {@link android.os.Build.VERSION_CODES#KITKAT} and below, for purgeable bitmaps, this call 1897b1d2f0a1d0900ba5651f98021b4b7b4229ebb490Chris Craik * would attempt to ensure that the pixels have been decoded. 18988cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen */ 18998cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen public void prepareToDraw() { 1900ad57442fd00e7fd7f1885d23372ec5c85e176820John Reck checkRecycled("Can't prepareToDraw on a recycled bitmap!"); 19014387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck // Kick off an update/upload of the bitmap outside of the normal 19024387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck // draw path. 19034387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck nativePrepareToDraw(mNativePtr); 1904f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck } 1905f4faeac3525fe1ce3707ab785a1651aec367589dJohn Reck 19066e3658a63843096058ed444d073fbcd191fd7e1bsergeyv /** 19076e3658a63843096058ed444d073fbcd191fd7e1bsergeyv * 19086e3658a63843096058ed444d073fbcd191fd7e1bsergeyv * @return {@link GraphicBuffer} which is internally used by hardware bitmap 19096e3658a63843096058ed444d073fbcd191fd7e1bsergeyv * @hide 19106e3658a63843096058ed444d073fbcd191fd7e1bsergeyv */ 19116e3658a63843096058ed444d073fbcd191fd7e1bsergeyv public GraphicBuffer createGraphicBufferHandle() { 19126e3658a63843096058ed444d073fbcd191fd7e1bsergeyv return nativeCreateGraphicBufferHandle(mNativePtr); 19136e3658a63843096058ed444d073fbcd191fd7e1bsergeyv } 19146e3658a63843096058ed444d073fbcd191fd7e1bsergeyv 19159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project //////////// native methods 19169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 19179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static native Bitmap nativeCreate(int[] colors, int offset, 19189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int stride, int width, int height, 19198242656f495847c50c9ceaea92839dce78218a75Romain Guy int nativeConfig, boolean mutable, 19208242656f495847c50c9ceaea92839dce78218a75Romain Guy @Nullable @Size(9) float[] xyzD50, 19218242656f495847c50c9ceaea92839dce78218a75Romain Guy @Nullable ColorSpace.Rgb.TransferParameters p); 192236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native Bitmap nativeCopy(long nativeSrcBitmap, int nativeConfig, 19239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project boolean isMutable); 1924721ae5fec5f1fd4f93aa2a361a0ac298e15ce353Riley Andrews private static native Bitmap nativeCopyAshmem(long nativeSrcBitmap); 1925a5fdde9d15fbdf1aaeb61e1cf0c128af3aeb7b67Winson private static native Bitmap nativeCopyAshmemConfig(long nativeSrcBitmap, int nativeConfig); 1926775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler private static native long nativeGetNativeFinalizer(); 192736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nativeRecycle(long nativeBitmap); 192836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeReconfigure(long nativeBitmap, int width, int height, 19294508218850faedea95371188da587b6734f5f3dasergeyv int config, boolean isPremultiplied); 19309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 193136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nativeCompress(long nativeBitmap, int format, 19329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int quality, OutputStream stream, 19339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project byte[] tempStorage); 193436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeErase(long nativeBitmap, int color); 193536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native int nativeRowBytes(long nativeBitmap); 193636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native int nativeConfig(long nativeBitmap); 19378cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 193857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III private static native int nativeGetPixel(long nativeBitmap, int x, int y); 193936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeGetPixels(long nativeBitmap, int[] pixels, 19401abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik int offset, int stride, int x, int y, 194157ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III int width, int height); 19428cdcb12752b716d0407733fecefcf1d9e926310aWei-Ta Chen 194357ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III private static native void nativeSetPixel(long nativeBitmap, int x, int y, int color); 194436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeSetPixels(long nativeBitmap, int[] colors, 19451abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik int offset, int stride, int x, int y, 194657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III int width, int height); 194736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeCopyPixelsToBuffer(long nativeBitmap, 19489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Buffer dst); 194936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeCopyPixelsFromBuffer(long nativeBitmap, Buffer src); 195036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native int nativeGenerationId(long nativeBitmap); 19519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 19529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static native Bitmap nativeCreateFromParcel(Parcel p); 19539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // returns true on success 195436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nativeWriteToParcel(long nativeBitmap, 19559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project boolean isMutable, 1956de0dfb7b65a02d4dd74c271b558adee0973fc267Dianne Hackborn int density, 19579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Parcel p); 19589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // returns a new bitmap built from the native bitmap's alpha, and the paint 195936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native Bitmap nativeExtractAlpha(long nativeBitmap, 196036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat long nativePaint, 19619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int[] offsetXY); 19629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 196336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nativeHasAlpha(long nativeBitmap); 196457ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III private static native boolean nativeIsPremultiplied(long nativeBitmap); 196557ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III private static native void nativeSetPremultiplied(long nativeBitmap, 196657ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III boolean isPremul); 196757ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III private static native void nativeSetHasAlpha(long nativeBitmap, 196857ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III boolean hasAlpha, 196957ee620ced8caed1eb8651717f6a6d2d5f1f9a5bLeon Scroggins III boolean requestPremul); 197036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nativeHasMipMap(long nativeBitmap); 197136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap); 197236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1); 19734387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck private static native void nativePrepareToDraw(long nativeBitmap); 19744508218850faedea95371188da587b6734f5f3dasergeyv private static native int nativeGetAllocationByteCount(long nativeBitmap); 197581f97ee47ccf0d011cdc4f38b6ea5c45b70dedc0sergeyv private static native Bitmap nativeCopyPreserveInternalConfig(long nativeBitmap); 19760a0f23163a7a62900d46c7f81d109320d73d6c6bsergeyv private static native Bitmap nativeCreateHardwareBitmap(GraphicBuffer buffer); 19776e3658a63843096058ed444d073fbcd191fd7e1bsergeyv private static native GraphicBuffer nativeCreateGraphicBufferHandle(long nativeBitmap); 1978efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy private static native boolean nativeGetColorSpace(long nativePtr, float[] xyz, float[] params); 1979efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy private static native boolean nativeIsSRGB(long nativePtr); 19808242656f495847c50c9ceaea92839dce78218a75Romain Guy private static native void nativeCopyColorSpace(long srcBitmap, long dstBitmap); 19819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1982