19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 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
199fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyvimport static android.graphics.BitmapFactory.Options.validate;
209fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.AssetManager;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.Resources;
23787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guyimport android.os.Trace;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.DisplayMetrics;
25b8151ecd6ef4faa5c16d0a4c3abb45ec84d1f97aAmith Yamasaniimport android.util.Log;
26eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chenimport android.util.TypedValue;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
28eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chenimport java.io.FileDescriptor;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileInputStream;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException;
31eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chenimport java.io.InputStream;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
34eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen * Creates Bitmap objects from various sources, including files, streams,
35eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen * and byte-arrays.
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class BitmapFactory {
387b2f8b8fb7064a1d3b6d942b978c30c24c9d7299Romain Guy    private static final int DECODE_BUFFER_SIZE = 16 * 1024;
397b2f8b8fb7064a1d3b6d942b978c30c24c9d7299Romain Guy
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static class Options {
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Create a default Options object, which if left unchanged will give
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * the same result from the decoder as if null were passed.
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public Options() {
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            inScaled = true;
471abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik            inPremultiplied = true;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
5137f74cad46c6f1799aec3c52e8f47598237f43d4Chet Haase         * If set, decode methods that take the Options object will attempt to
52c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * reuse this bitmap when loading content. If the decode operation
53a8f428627253f1060db1a0c7ff4bd3bb6c687fd2Mark Lu         * cannot use this bitmap, the decode method will throw an
54a8f428627253f1060db1a0c7ff4bd3bb6c687fd2Mark Lu         * {@link java.lang.IllegalArgumentException}. The
55c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * current implementation necessitates that the reused bitmap be
56c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * mutable, and the resulting reused bitmap will continue to remain
57c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * mutable even when decoding a resource which would normally result in
58c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * an immutable bitmap.</p>
599f58361e98be7386a4eadd3aa254e9b7d09d0a3bChris Craik         *
60352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <p>You should still always use the returned Bitmap of the decode
61352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * method and not assume that reusing the bitmap worked, due to the
62352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * constraints outlined above and failure situations that can occur.
63352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * Checking whether the return value matches the value of the inBitmap
64352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * set in the Options structure will indicate if the bitmap was reused,
65352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * but in all cases you should use the Bitmap returned by the decoding
66352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * function to ensure that you are using the bitmap that was used as the
67352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * decode destination.</p>
68352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         *
69352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <h3>Usage with BitmapFactory</h3>
70352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         *
71e8222dddaf2e3da14380101e818d4254899e0c0dChet Haase         * <p>As of {@link android.os.Build.VERSION_CODES#KITKAT}, any
72352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * mutable bitmap can be reused by {@link BitmapFactory} to decode any
73352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * other bitmaps as long as the resulting {@link Bitmap#getByteCount()
74352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * byte count} of the decoded bitmap is less than or equal to the {@link
75c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * Bitmap#getAllocationByteCount() allocated byte count} of the reused
76c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * bitmap. This can be because the intrinsic size is smaller, or its
77c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * size post scaling (for density / sample size) is smaller.</p>
78905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik         *
79352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT}
80c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * additional constraints apply: The image being decoded (whether as a
81c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * resource or as a stream) must be in jpeg or png format. Only equal
82c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * sized bitmaps are supported, with {@link #inSampleSize} set to 1.
83c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * Additionally, the {@link android.graphics.Bitmap.Config
84c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * configuration} of the reused bitmap will override the setting of
85c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         * {@link #inPreferredConfig}, if set.</p>
8637f74cad46c6f1799aec3c52e8f47598237f43d4Chet Haase         *
87352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <h3>Usage with BitmapRegionDecoder</h3>
88352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         *
89352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <p>BitmapRegionDecoder will draw its requested content into the Bitmap
90352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * provided, clipping if the output content size (post scaling) is larger
91352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * than the provided Bitmap. The provided Bitmap's width, height, and
92352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * {@link Bitmap.Config} will not be changed.
93352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         *
94352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <p class="note">BitmapRegionDecoder support for {@link #inBitmap} was
95352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * introduced in {@link android.os.Build.VERSION_CODES#JELLY_BEAN}. All
96352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * formats supported by BitmapRegionDecoder support Bitmap reuse via
97352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * {@link #inBitmap}.</p>
98c84d203da21c3ae3ded94c79c035d41b27809b3bChris Craik         *
9925ba1c86945a441428194d9ebcabbf31be75a45aRomain Guy         * @see Bitmap#reconfigure(int,int, android.graphics.Bitmap.Config)
10037f74cad46c6f1799aec3c52e8f47598237f43d4Chet Haase         */
10137f74cad46c6f1799aec3c52e8f47598237f43d4Chet Haase        public Bitmap inBitmap;
10237f74cad46c6f1799aec3c52e8f47598237f43d4Chet Haase
10337f74cad46c6f1799aec3c52e8f47598237f43d4Chet Haase        /**
1042361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy         * If set, decode methods will always return a mutable Bitmap instead of
1052361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy         * an immutable one. This can be used for instance to programmatically apply
1062361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy         * effects to a Bitmap loaded through BitmapFactory.
1079fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * <p>Can not be set simultaneously with inPreferredConfig =
1089fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * {@link android.graphics.Bitmap.Config#HARDWARE},
1099fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv         * because hardware bitmaps are always immutable.
1102361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy         */
1112361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy        @SuppressWarnings({"UnusedDeclaration"}) // used in native code
1122361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy        public boolean inMutable;
1132361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy
1142361098da3b9d9c3eeed410dc72ba62c0e9177cfRomain Guy        /**
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * If set to true, the decoder will return null (no bitmap), but
11695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * the <code>out...</code> fields will still be set, allowing the caller to
11795648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * query the bitmap without having to allocate the memory for its pixels.
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean inJustDecodeBounds;
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * If set to a value > 1, requests the decoder to subsample the original
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * image, returning a smaller image to save memory. The sample size is
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * the number of pixels in either dimension that correspond to a single
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * an image that is 1/4 the width/height of the original, and 1/16 the
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * number of pixels. Any value <= 1 is treated the same as 1. Note: the
128758333d70a749fb50276057abc585cbc0decc8d4Adam Koch         * decoder uses a final value based on powers of 2, any other value will
129758333d70a749fb50276057abc585cbc0decc8d4Adam Koch         * be rounded down to the nearest power of 2.
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public int inSampleSize;
132eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * If this is non-null, the decoder will try to decode into this
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * internal configuration. If it is null, or the request cannot be met,
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * the decoder will try to pick the best matching config based on the
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * system's screen depth, and characteristics of the original image such
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * as if it has per-pixel alpha (requiring a config that also does).
139207b3ab604bcbe47fa55f26f358cde60cf8a784dRomain Guy         *
140207b3ab604bcbe47fa55f26f358cde60cf8a784dRomain Guy         * Image are loaded with the {@link Bitmap.Config#ARGB_8888} config by
141207b3ab604bcbe47fa55f26f358cde60cf8a784dRomain Guy         * default.
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
143207b3ab604bcbe47fa55f26f358cde60cf8a784dRomain Guy        public Bitmap.Config inPreferredConfig = Bitmap.Config.ARGB_8888;
144eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
14695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * <p>If this is non-null, the decoder will try to decode into this
14795648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * color space. If it is null, or the request cannot be met,
14895648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * the decoder will pick either the color space embedded in the image
14995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * or the color space best suited for the requested image configuration
15095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * (for instance {@link ColorSpace.Named#SRGB sRGB} for
15195648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * the {@link Bitmap.Config#ARGB_8888} configuration).</p>
15295648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         *
15395648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * <p>{@link Bitmap.Config#RGBA_F16} always uses the
15495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * {@link ColorSpace.Named#LINEAR_EXTENDED_SRGB scRGB} color space).
15595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * Bitmaps in other configurations without an embedded color space are
15695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * assumed to be in the {@link ColorSpace.Named#SRGB sRGB} color space.</p>
15795648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         *
15895648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * <p class="note">Only {@link ColorSpace.Model#RGB} color spaces are
15995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * currently supported. An <code>IllegalArgumentException</code> will
16095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * be thrown by the decode methods when setting a non-RGB color space
16195648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * such as {@link ColorSpace.Named#CIE_LAB Lab}.</p>
16295648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         *
16395648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * <p class="note">The specified color space's transfer function must be
16495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}. An
16595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * <code>IllegalArgumentException</code> will be thrown by the decode methods
16695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * if calling {@link ColorSpace.Rgb#getTransferParameters()} on the
16795648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * specified color space returns null.</p>
16895648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         *
16995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * <p>After decode, the bitmap's color space is stored in
17095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         * {@link #outColorSpace}.</p>
17195648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy         */
17295648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy        public ColorSpace inPreferredColorSpace = null;
17395648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy
17495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy        /**
1751abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * If true (which is the default), the resulting bitmap will have its
1761abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * color channels pre-multipled by the alpha channel.
1771abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         *
1781abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * <p>This should NOT be set to false for images to be directly drawn by
1791abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * the view system or through a {@link Canvas}. The view system and
1801abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * {@link Canvas} assume all drawn images are pre-multiplied to simplify
1811abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * draw-time blending, and will throw a RuntimeException when
1821abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * un-premultiplied are drawn.</p>
1831abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         *
1841abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * <p>This is likely only useful if you want to manipulate raw encoded
1851abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * image data, e.g. with RenderScript or custom OpenGL.</p>
1861abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         *
1871abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * <p>This does not affect bitmaps without an alpha channel.</p>
1881abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         *
1898790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         * <p>Setting this flag to false while setting {@link #inScaled} to true
1908790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         * may result in incorrect colors.</p>
1918790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         *
1921abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * @see Bitmap#hasAlpha()
1931abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         * @see Bitmap#isPremultiplied()
1948790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         * @see #inScaled
1951abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik         */
1961abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        public boolean inPremultiplied;
1971abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik
1981abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik        /**
1995e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * @deprecated As of {@link android.os.Build.VERSION_CODES#N}, this is
2005e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * ignored.
2015e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *
2025e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * In {@link android.os.Build.VERSION_CODES#M} and below, if dither is
2035e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * true, the decoder will attempt to dither the decoded image.
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean inDither;
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
20811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * The pixel density to use for the bitmap.  This will always result
20911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * in the returned bitmap having a density set for it (see
2106e1df0e981c8c588e411b8ad6e55554fe4815655Gilles Debunne         * {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}).  In addition,
21111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * if {@link #inScaled} is set (which it is by default} and this
21211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * density does not match {@link #inTargetDensity}, then the bitmap
21311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * will be scaled to the target density before being returned.
21411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         *
21511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * <p>If this is 0,
21611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link BitmapFactory#decodeResource(Resources, int)},
21711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link BitmapFactory#decodeResource(Resources, int, android.graphics.BitmapFactory.Options)},
21811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * and {@link BitmapFactory#decodeResourceStream}
21911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * will fill in the density associated with the resource.  The other
22011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * functions will leave it as-is and no density will be applied.
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *
22211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inTargetDensity
22311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inScreenDensity
22411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inScaled
22511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see Bitmap#setDensity(int)
22611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see android.util.DisplayMetrics#densityDpi
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public int inDensity;
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
23111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * The pixel density of the destination this bitmap will be drawn to.
23211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * This is used in conjunction with {@link #inDensity} and
23311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link #inScaled} to determine if and how to scale the bitmap before
23411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * returning it.
23511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         *
23611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * <p>If this is 0,
23711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link BitmapFactory#decodeResource(Resources, int)},
23811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link BitmapFactory#decodeResource(Resources, int, android.graphics.BitmapFactory.Options)},
23911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * and {@link BitmapFactory#decodeResourceStream}
24011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * will fill in the density associated the Resources object's
24111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * DisplayMetrics.  The other
24211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * functions will leave it as-is and no scaling for density will be
24311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * performed.
24411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         *
24511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inDensity
24611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inScreenDensity
24711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inScaled
24811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see android.util.DisplayMetrics#densityDpi
24911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         */
25011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        public int inTargetDensity;
25111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
25211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        /**
25311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * The pixel density of the actual screen that is being used.  This is
25411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * purely for applications running in density compatibility code, where
25511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link #inTargetDensity} is actually the density the application
25611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * sees rather than the real screen density.
25711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         *
25811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * <p>By setting this, you
25911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * allow the loading code to avoid scaling a bitmap that is currently
26011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * in the screen density up/down to the compatibility density.  Instead,
26111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * if {@link #inDensity} is the same as {@link #inScreenDensity}, the
26211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * bitmap will be left as-is.  Anything using the resulting bitmap
26311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * must also used {@link Bitmap#getScaledWidth(int)
26411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * Bitmap.getScaledWidth} and {@link Bitmap#getScaledHeight
26511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * Bitmap.getScaledHeight} to account for any different between the
26611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * bitmap's density and the target's density.
26711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         *
26811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * <p>This is never set automatically for the caller by
26911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link BitmapFactory} itself.  It must be explicitly set, since the
27011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * caller must deal with the resulting bitmap in a density-aware way.
27111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         *
27211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inDensity
27311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inTargetDensity
27411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see #inScaled
27511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * @see android.util.DisplayMetrics#densityDpi
27611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         */
27711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        public int inScreenDensity;
27811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
27911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        /**
28011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * When this flag is set, if {@link #inDensity} and
28111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * {@link #inTargetDensity} are not 0, the
28211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * bitmap will be scaled to match {@link #inTargetDensity} when loaded,
28311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * rather than relying on the graphics system scaling it each time it
28411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * is drawn to a Canvas.
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *
286352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * <p>BitmapRegionDecoder ignores this flag, and will not scale output
287352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         * based on density. (though {@link #inSampleSize} is supported)</p>
288352919efd02808f0d6222f7a2cc657f3455c2049Chris Craik         *
28911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * <p>This flag is turned on by default and should be turned off if you need
29011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * a non-scaled version of the bitmap.  Nine-patch bitmaps ignore this
29111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn         * flag and are always scaled.
2928790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         *
2938790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         * <p>If {@link #inPremultiplied} is set to false, and the image has alpha,
2948790be6de3644e332ec6a17c855da89ffc13a9bfLeon Scroggins III         * setting this flag to true may result in incorrect colors.
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean inScaled;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
299955d8d69ea6caabce1461dc25b339b9bf9dc61a6Dianne Hackborn         * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this is
3000aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * ignored.
3010aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         *
3020aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * In {@link android.os.Build.VERSION_CODES#KITKAT} and below, if this
3030aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * is set to true, then the resulting bitmap will allocate its
304c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * pixels such that they can be purged if the system needs to reclaim
305c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * memory. In that instance, when the pixels need to be accessed again
306c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * (e.g. the bitmap is drawn, getPixels() is called), they will be
307c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * automatically re-decoded.
308c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         *
309e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * <p>For the re-decode to happen, the bitmap must have access to the
310c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * encoded data, either by sharing a reference to the input
311c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * or by making a copy of it. This distinction is controlled by
312c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * inInputShareable. If this is true, then the bitmap may keep a shallow
313c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * reference to the input. If this is false, then the bitmap will
314c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * explicitly make a copy of the input data, and keep that. Even if
315c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         * sharing is allowed, the implementation may still decide to make a
316e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * deep copy of the input data.</p>
317e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         *
318e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * <p>While inPurgeable can help avoid big Dalvik heap allocations (from
319e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * API level 11 onward), it sacrifices performance predictability since any
320e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * image that the view system tries to draw may incur a decode delay which
321e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * can lead to dropped frames. Therefore, most apps should avoid using
322e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * inPurgeable to allow for a fast and fluid UI. To minimize Dalvik heap
323e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * allocations use the {@link #inBitmap} flag instead.</p>
324e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         *
325e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * <p class="note"><strong>Note:</strong> This flag is ignored when used
326e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * with {@link #decodeResource(Resources, int,
327e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * android.graphics.BitmapFactory.Options)} or {@link #decodeFile(String,
328e1693df494dbdedfb5eac56bf59f86afc893b778Adam Koch         * android.graphics.BitmapFactory.Options)}.</p>
329c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         */
3300aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III        @Deprecated
331c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed        public boolean inPurgeable;
332c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed
333c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed        /**
334955d8d69ea6caabce1461dc25b339b9bf9dc61a6Dianne Hackborn         * @deprecated As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this is
3350aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * ignored.
3360aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         *
3370aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * In {@link android.os.Build.VERSION_CODES#KITKAT} and below, this
3380aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * field works in conjuction with inPurgeable. If inPurgeable is false,
3390aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * then this field is ignored. If inPurgeable is true, then this field
3400aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * determines whether the bitmap can share a reference to the input
3410aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III         * data (inputstream, array, etc.) or if it must make a deep copy.
342c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed         */
3430aa39dc2dcfca20f4d9cbeb1699d48a4808f2c70Leon Scroggins III        @Deprecated
344c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed        public boolean inInputShareable;
345c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed
346c70e06bbfac0d92ec218a32e35d9d7fa80f23cc9Mike Reed        /**
3475e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * @deprecated As of {@link android.os.Build.VERSION_CODES#N}, this is
3485e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * ignored.  The output will always be high quality.
3495e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *
3505e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * In {@link android.os.Build.VERSION_CODES#M} and below, if
3515e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         * inPreferQualityOverSpeed is set to true, the decoder will try to
352953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen         * decode the reconstructed image to a higher quality even at the
353953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen         * expense of the decoding speed. Currently the field only affects JPEG
354953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen         * decode, in the case of which a more accurate, but slightly slower,
355953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen         * IDCT method will be used instead.
356953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen         */
357953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen        public boolean inPreferQualityOverSpeed;
358953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen
359953f9094a2ec14594fa8501d5f3e2d9e300b1b62Wei-Ta Chen        /**
360544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * The resulting width of the bitmap. If {@link #inJustDecodeBounds} is
361544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * set to false, this will be width of the output bitmap after any
362544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * scaling is applied. If true, it will be the width of the input image
363544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * without any accounting for scaling.
364544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         *
365544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * <p>outWidth will be set to -1 if there is an error trying to decode.</p>
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public int outWidth;
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
370544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * The resulting height of the bitmap. If {@link #inJustDecodeBounds} is
371544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * set to false, this will be height of the output bitmap after any
372544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * scaling is applied. If true, it will be the height of the input image
373544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * without any accounting for scaling.
374544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         *
375544333b39ec612e0e3426d3ef90b21761ea0d751Chris Craik         * <p>outHeight will be set to -1 if there is an error trying to decode.</p>
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public int outHeight;
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * If known, this string is set to the mimetype of the decoded image.
381e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy         * If not known, or there is an error, it is set to null.
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public String outMimeType;
384eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
386e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy         * If known, the config the decoded bitmap will have.
387e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy         * If not known, or there is an error, it is set to null.
388e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy         */
389e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy        public Bitmap.Config outConfig;
390e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy
391e8d2ebb5c29efb996179f9a4b62c78e4d3037e14Romain Guy        /**
39290fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy         * If known, the color space the decoded bitmap will have. Note that the
39390fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy         * output color space is not guaranteed to be the color space the bitmap
39490fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy         * is encoded with. If not known (when the config is
39590fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy         * {@link Bitmap.Config#ALPHA_8} for instance), or there is an error,
39690fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy         * it is set to null.
39790fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy         */
39890fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy        public ColorSpace outColorSpace;
39990fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy
40090fc43b33de1439ae7d92b12c3e09038aa878f50Romain Guy        /**
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Temp storage to use for decoding.  Suggest 16K or so.
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public byte[] inTempStorage;
404eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
40651b75386a8f6a90e833002d2d1fed928039841e7Matt Sarett         * @deprecated As of {@link android.os.Build.VERSION_CODES#N}, see
40751b75386a8f6a90e833002d2d1fed928039841e7Matt Sarett         * comments on {@link #requestCancelDecode()}.
40851b75386a8f6a90e833002d2d1fed928039841e7Matt Sarett         *
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Flag to indicate that cancel has been called on this object.  This
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * is useful if there's an intermediary that wants to first decode the
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * bounds and then decode the image.  In that case the intermediary
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * can check, inbetween the bounds decode and the image decode, to see
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * if the operation is canceled.
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public boolean mCancel;
416eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
4185e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  @deprecated As of {@link android.os.Build.VERSION_CODES#N}, this
4195e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  will not affect the decode, though it will still set mCancel.
4205e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *
4215e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  In {@link android.os.Build.VERSION_CODES#M} and below, if this can
4225e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  be called from another thread while this options object is inside
4235e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  a decode... call. Calling this will notify the decoder that it
4245e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  should cancel its operation. This is not guaranteed to cancel the
4255e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  decode, but if it does, the decoder... operation will return null,
4265e2496bcee239ce9ebeff6022b7badf81d87492cMatt Sarett         *  or if inJustDecodeBounds is true, will set outWidth/outHeight
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         *  to -1
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void requestCancelDecode() {
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mCancel = true;
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4329fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv
4339fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        static void validate(Options opts) {
43495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy            if (opts == null) return;
43595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy
43608beea71c9c68a65e078e476f4747b66cf20128fsergeyv            if (opts.inBitmap != null && opts.inBitmap.getConfig() == Bitmap.Config.HARDWARE) {
4379fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv                throw new IllegalArgumentException("Bitmaps with Config.HARWARE are always immutable");
4389fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv            }
43995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy
44008beea71c9c68a65e078e476f4747b66cf20128fsergeyv            if (opts.inMutable && opts.inPreferredConfig == Bitmap.Config.HARDWARE) {
44108beea71c9c68a65e078e476f4747b66cf20128fsergeyv                throw new IllegalArgumentException("Bitmaps with Config.HARDWARE cannot be " +
44208beea71c9c68a65e078e476f4747b66cf20128fsergeyv                        "decoded into - they are immutable");
44308beea71c9c68a65e078e476f4747b66cf20128fsergeyv            }
44408beea71c9c68a65e078e476f4747b66cf20128fsergeyv
44595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy            if (opts.inPreferredColorSpace != null) {
44695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                if (!(opts.inPreferredColorSpace instanceof ColorSpace.Rgb)) {
44795648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                    throw new IllegalArgumentException("The destination color space must use the " +
44895648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                            "RGB color model");
44995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                }
45095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                if (((ColorSpace.Rgb) opts.inPreferredColorSpace).getTransferParameters() == null) {
45195648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                    throw new IllegalArgumentException("The destination color space must use an " +
45295648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                            "ICC parametric transfer function");
45395648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy                }
45495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy            }
4559fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        }
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode a file path into a bitmap. If the specified file name is null,
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or cannot be decoded into a bitmap, the function returns null.
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pathName complete path name for the file to be decoded.
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param opts null-ok; Options that control downsampling and whether the
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *             image should be completely decoded, or just is size returned.
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The decoded bitmap, or null if the image data could not be
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         decoded, or, if opts is non-null, if opts requested only the
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         size be returned (in opts.outWidth and opts.outHeight)
4689fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
4699fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         is {@link android.graphics.Bitmap.Config#HARDWARE}
47095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
47195648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
47295648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeFile(String pathName, Options opts) {
4759fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        validate(opts);
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bm = null;
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        InputStream stream = null;
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            stream = new FileInputStream(pathName);
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bm = decodeStream(stream, null, opts);
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (Exception e) {
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            /*  do nothing.
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                If the exception happened on open, bm will be null.
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            */
485b8151ecd6ef4faa5c16d0a4c3abb45ec84d1f97aAmith Yamasani            Log.e("BitmapFactory", "Unable to decode stream: " + e);
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } finally {
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (stream != null) {
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                try {
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    stream.close();
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } catch (IOException e) {
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    // do nothing here
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode a file path into a bitmap. If the specified file name is null,
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or cannot be decoded into a bitmap, the function returns null.
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pathName complete path name for the file to be decoded.
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the resulting decoded bitmap, or null if it could not be decoded.
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeFile(String pathName) {
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return decodeFile(pathName, null);
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode a new Bitmap from an InputStream. This InputStream was obtained from
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * resources, which we pass to be able to scale the bitmap accordingly.
5129fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
5139fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         is {@link android.graphics.Bitmap.Config#HARDWARE}
51495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
51595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
51695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
51811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn    public static Bitmap decodeResourceStream(Resources res, TypedValue value,
51911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            InputStream is, Rect pad, Options opts) {
5209fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        validate(opts);
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (opts == null) {
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            opts = new Options();
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
52511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        if (opts.inDensity == 0 && value != null) {
526a53b828635fce8b6b2d3e3377d74d72070056623Dianne Hackborn            final int density = value.density;
52711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            if (density == TypedValue.DENSITY_DEFAULT) {
52811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn                opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
52911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            } else if (density != TypedValue.DENSITY_NONE) {
53011ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn                opts.inDensity = density;
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
53311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
53411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        if (opts.inTargetDensity == 0 && res != null) {
53511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            opts.inTargetDensity = res.getDisplayMetrics().densityDpi;
53611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        }
53711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn
53811ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        return decodeStream(is, pad, opts);
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
54211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Synonym for opening the given resource and calling
54311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * {@link #decodeResourceStream}.
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param res   The resources object containing the image data
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param id The resource id of the image data
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param opts null-ok; Options that control downsampling and whether the
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *             image should be completely decoded, or just is size returned.
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The decoded bitmap, or null if the image data could not be
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         decoded, or, if opts is non-null, if opts requested only the
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         size be returned (in opts.outWidth and opts.outHeight)
5529fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
5539fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         is {@link android.graphics.Bitmap.Config#HARDWARE}
55495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
55595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
55695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeResource(Resources res, int id, Options opts) {
5599fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        validate(opts);
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Bitmap bm = null;
561650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy        InputStream is = null;
562650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            final TypedValue value = new TypedValue();
565650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy            is = res.openRawResource(id, value);
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
56711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            bm = decodeResourceStream(res, value, is, null, opts);
568650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy        } catch (Exception e) {
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            /*  do nothing.
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                If the exception happened on open, bm will be null.
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                If it happened on close, bm is still valid.
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            */
573650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy        } finally {
574650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy            try {
575650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy                if (is != null) is.close();
576650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy            } catch (IOException e) {
577650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy                // Ignore
578650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy            }
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
580650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
581decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase        if (bm == null && opts != null && opts.inBitmap != null) {
582decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase            throw new IllegalArgumentException("Problem decoding into existing bitmap");
583decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase        }
584decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
587eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
58911ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn     * Synonym for {@link #decodeResource(Resources, int, android.graphics.BitmapFactory.Options)}
5905b15b1717ef95ef8c91f8587fee1f789aec6ae49Shuhrat Dehkanov     * with null Options.
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param res The resources object containing the image data
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param id The resource id of the image data
594c502744f2ddcc80a34f5197b5d2c0d701c7d6e71Newton Allen     * @return The decoded bitmap, or null if the image could not be decoded.
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeResource(Resources res, int id) {
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return decodeResource(res, id, null);
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode an immutable bitmap from the specified byte array.
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param data byte array of compressed image data
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset offset into imageData for where the decoder should begin
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               parsing.
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param length the number of bytes, beginning at offset, to parse
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param opts null-ok; Options that control downsampling and whether the
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *             image should be completely decoded, or just is size returned.
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The decoded bitmap, or null if the image data could not be
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         decoded, or, if opts is non-null, if opts requested only the
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         size be returned (in opts.outWidth and opts.outHeight)
6129fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
6139fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         is {@link android.graphics.Bitmap.Config#HARDWARE}
61495648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
61595648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
61695648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) {
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((offset | length) < 0 || data.length < offset + length) {
6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw new ArrayIndexOutOfBoundsException();
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6229fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        validate(opts);
623ec4a50428d5f26a22df3edaf7e5b08f41d5cb54bAmith Yamasani
624787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        Bitmap bm;
625787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy
626787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
627787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        try {
628787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            bm = nativeDecodeByteArray(data, offset, length, opts);
629787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy
630787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            if (bm == null && opts != null && opts.inBitmap != null) {
631787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy                throw new IllegalArgumentException("Problem decoding into existing bitmap");
632787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            }
633787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            setDensityFromOptions(bm, opts);
634787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        } finally {
635787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
636decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase        }
637787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy
638decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase        return bm;
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
640eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode an immutable bitmap from the specified byte array.
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param data byte array of compressed image data
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param offset offset into imageData for where the decoder should begin
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               parsing.
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param length the number of bytes, beginning at offset, to parse
648c502744f2ddcc80a34f5197b5d2c0d701c7d6e71Newton Allen     * @return The decoded bitmap, or null if the image could not be decoded.
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeByteArray(byte[] data, int offset, int length) {
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return decodeByteArray(data, offset, length, null);
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
653eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
655905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik     * Set the newly decoded bitmap's density based on the Options.
656905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik     */
657905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik    private static void setDensityFromOptions(Bitmap outputBitmap, Options opts) {
658905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik        if (outputBitmap == null || opts == null) return;
659905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik
660905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik        final int density = opts.inDensity;
661905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik        if (density != 0) {
662905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            outputBitmap.setDensity(density);
663905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            final int targetDensity = opts.inTargetDensity;
664905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) {
665905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik                return;
666905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            }
667905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik
668905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            byte[] np = outputBitmap.getNinePatchChunk();
669905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np);
670905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            if (opts.inScaled || isNinePatch) {
671905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik                outputBitmap.setDensity(targetDensity);
672905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            }
673905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik        } else if (opts.inBitmap != null) {
674905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            // bitmap was reused, ensure density is reset
675905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik            outputBitmap.setDensity(Bitmap.getDefaultDensity());
676905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik        }
677905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik    }
678905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik
679905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik    /**
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode an input stream into a bitmap. If the input stream is null, or
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * cannot be used to decode a bitmap, the function returns null.
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The stream's position will be where ever it was after the encoded data
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * was read.
684eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen     *
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param is The input stream that holds the raw data to be decoded into a
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           bitmap.
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param outPadding If not null, return the padding rect for the bitmap if
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   it exists, otherwise set padding to [-1,-1,-1,-1]. If
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   no bitmap is returned (null) then padding is
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   unchanged.
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param opts null-ok; Options that control downsampling and whether the
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *             image should be completely decoded, or just is size returned.
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The decoded bitmap, or null if the image data could not be
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         decoded, or, if opts is non-null, if opts requested only the
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         size be returned (in opts.outWidth and opts.outHeight)
6969fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
6979fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         is {@link android.graphics.Bitmap.Config#HARDWARE}
69895648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
69995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
70095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
701321bc46b3c885a806f2e7e29db8212f7ea6c8670Leon Scroggins III     *
702321bc46b3c885a806f2e7e29db8212f7ea6c8670Leon Scroggins III     * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT},
703321bc46b3c885a806f2e7e29db8212f7ea6c8670Leon Scroggins III     * if {@link InputStream#markSupported is.markSupported()} returns true,
704321bc46b3c885a806f2e7e29db8212f7ea6c8670Leon Scroggins III     * <code>is.mark(1024)</code> would be called. As of
705321bc46b3c885a806f2e7e29db8212f7ea6c8670Leon Scroggins III     * {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.</p>
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) {
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // we don't throw in this case, thus allowing the caller to only check
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // the cache, and not force the image to be decoded.
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (is == null) {
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7139fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        validate(opts);
714eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
715ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III        Bitmap bm = null;
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
717787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
718787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        try {
719787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            if (is instanceof AssetManager.AssetInputStream) {
72036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat                final long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
721787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy                bm = nativeDecodeAsset(asset, outPadding, opts);
7225835157d762a5b6bffe3d892c588ebc475bccb44John Reck            } else {
7237315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III                bm = decodeStreamInternal(is, outPadding, opts);
724787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            }
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
726787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            if (bm == null && opts != null && opts.inBitmap != null) {
727787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy                throw new IllegalArgumentException("Problem decoding into existing bitmap");
728787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            }
7297b2f8b8fb7064a1d3b6d942b978c30c24c9d7299Romain Guy
730787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            setDensityFromOptions(bm, opts);
731787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy        } finally {
732787e35793f47abdb6cc10f328cd58d362c7bd250Romain Guy            Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
733decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase        }
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bm;
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
737905e8246ef0bd20ee28d81ce3da0c5e5fc8e3913Chris Craik
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7397315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III     * Private helper function for decoding an InputStream natively. Buffers the input enough to
7407315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III     * do a rewind as needed, and supplies temporary storage if necessary. is MUST NOT be null.
7417315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III     */
7427315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III    private static Bitmap decodeStreamInternal(InputStream is, Rect outPadding, Options opts) {
7437315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III        // ASSERT(is != null);
7447315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III        byte [] tempStorage = null;
7457315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III        if (opts != null) tempStorage = opts.inTempStorage;
7467315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III        if (tempStorage == null) tempStorage = new byte[DECODE_BUFFER_SIZE];
7477315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III        return nativeDecodeStream(is, tempStorage, outPadding, opts);
7487315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III    }
7497315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III
7507315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III    /**
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode an input stream into a bitmap. If the input stream is null, or
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * cannot be used to decode a bitmap, the function returns null.
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The stream's position will be where ever it was after the encoded data
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * was read.
755eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen     *
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param is The input stream that holds the raw data to be decoded into a
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           bitmap.
75803f0292744094ec107ffce71301c394503a31dedGilles Debunne     * @return The decoded bitmap, or null if the image data could not be decoded.
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeStream(InputStream is) {
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return decodeStream(is, null, null);
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode a bitmap from the file descriptor. If the bitmap cannot be decoded
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * return null. The position within the descriptor will not be changed when
7677341d7a104b47996445d069a695e155a07184606Dianne Hackborn     * this returns, so the descriptor can be used again as-is.
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param fd The file descriptor containing the bitmap data to decode
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param outPadding If not null, return the padding rect for the bitmap if
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   it exists, otherwise set padding to [-1,-1,-1,-1]. If
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   no bitmap is returned (null) then padding is
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *                   unchanged.
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param opts null-ok; Options that control downsampling and whether the
775ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III     *             image should be completely decoded, or just its size returned.
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the decoded bitmap, or null
7779fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
7789fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv     *         is {@link android.graphics.Bitmap.Config#HARDWARE}
77995648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
78095648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
78195648b8a590995e57948ff2fd57d234bdbf30be0Romain Guy     *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) {
7849fbb0b5ab321d86efbc7fa8774052bfb34de9557sergeyv        validate(opts);
785ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III        Bitmap bm;
786ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III
787ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III        Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeFileDescriptor");
788ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III        try {
789ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III            if (nativeIsSeekable(fd)) {
790ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                bm = nativeDecodeFileDescriptor(fd, outPadding, opts);
791ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III            } else {
792ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                FileInputStream fis = new FileInputStream(fd);
793ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                try {
7947315f1baee19476363235127bc1438e2a291fa15Leon Scroggins III                    bm = decodeStreamInternal(fis, outPadding, opts);
795ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                } finally {
796ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                    try {
797ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                        fis.close();
798ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                    } catch (Throwable t) {/* ignore */}
799ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III                }
800ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III            }
801ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III
802a9d0d47076ecf2d1739bb3534abc9deead8ebebdOwen Lin            if (bm == null && opts != null && opts.inBitmap != null) {
803a9d0d47076ecf2d1739bb3534abc9deead8ebebdOwen Lin                throw new IllegalArgumentException("Problem decoding into existing bitmap");
804a9d0d47076ecf2d1739bb3534abc9deead8ebebdOwen Lin            }
805ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III
806ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III            setDensityFromOptions(bm, opts);
807ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III        } finally {
808ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III            Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
809decc8cd41eca3770c8f5ee13d81b9cd5f0c25ccdChet Haase        }
810ca32021b43f326af7d3f4ae041f8db297f98a518Leon Scroggins III        return bm;
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
812eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decode a bitmap from the file descriptor. If the bitmap cannot be decoded
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * return null. The position within the descriptor will not be changed when
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this returns, so the descriptor can be used again as is.
8179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
8189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param fd The file descriptor containing the bitmap data to decode
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the decoded bitmap, or null
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static Bitmap decodeFileDescriptor(FileDescriptor fd) {
822984b5df3830fe26a45bee39c7e8dd86714c99ed0Marco Nelissen        return decodeFileDescriptor(fd, null, null);
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
824eb949674fd3b83b706f795fc6b16ab1c66250c93Wei-Ta Chen
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeDecodeStream(InputStream is, byte[] storage,
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Rect padding, Options opts);
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeDecodeFileDescriptor(FileDescriptor fd,
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Rect padding, Options opts);
82936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    private static native Bitmap nativeDecodeAsset(long nativeAsset, Rect padding, Options opts);
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static native Bitmap nativeDecodeByteArray(byte[] data, int offset,
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int length, Options opts);
832a9d0d47076ecf2d1739bb3534abc9deead8ebebdOwen Lin    private static native boolean nativeIsSeekable(FileDescriptor fd);
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
834