• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2010 The Android Open Source Project
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package android.graphics;
17 
18 import android.annotation.UnsupportedAppUsage;
19 import android.content.res.AssetManager;
20 import android.os.Build;
21 
22 import java.io.FileDescriptor;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 
27 /**
28  * BitmapRegionDecoder can be used to decode a rectangle region from an image.
29  * BitmapRegionDecoder is particularly useful when an original image is large and
30  * you only need parts of the image.
31  *
32  * <p>To create a BitmapRegionDecoder, call newInstance(...).
33  * Given a BitmapRegionDecoder, users can call decodeRegion() repeatedly
34  * to get a decoded Bitmap of the specified region.
35  *
36  */
37 public final class BitmapRegionDecoder {
38     private long mNativeBitmapRegionDecoder;
39     private boolean mRecycled;
40     // ensures that the native decoder object exists and that only one decode can
41     // occur at a time.
42     private final Object mNativeLock = new Object();
43 
44     /**
45      * Create a BitmapRegionDecoder from the specified byte array.
46      * Currently only the JPEG and PNG formats are supported.
47      *
48      * @param data byte array of compressed image data.
49      * @param offset offset into data for where the decoder should begin
50      *               parsing.
51      * @param length the number of bytes, beginning at offset, to parse
52      * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
53      *                    shallow reference to the input. If this is false,
54      *                    then the BitmapRegionDecoder will explicitly make a copy of the
55      *                    input data, and keep that. Even if sharing is allowed,
56      *                    the implementation may still decide to make a deep
57      *                    copy of the input data. If an image is progressively encoded,
58      *                    allowing sharing may degrade the decoding speed.
59      * @return BitmapRegionDecoder, or null if the image data could not be decoded.
60      * @throws IOException if the image format is not supported or can not be decoded.
61      */
newInstance(byte[] data, int offset, int length, boolean isShareable)62     public static BitmapRegionDecoder newInstance(byte[] data,
63             int offset, int length, boolean isShareable) throws IOException {
64         if ((offset | length) < 0 || data.length < offset + length) {
65             throw new ArrayIndexOutOfBoundsException();
66         }
67         return nativeNewInstance(data, offset, length, isShareable);
68     }
69 
70     /**
71      * Create a BitmapRegionDecoder from the file descriptor.
72      * The position within the descriptor will not be changed when
73      * this returns, so the descriptor can be used again as is.
74      * Currently only the JPEG and PNG formats are supported.
75      *
76      * @param fd The file descriptor containing the data to decode
77      * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
78      *                    shallow reference to the input. If this is false,
79      *                    then the BitmapRegionDecoder will explicitly make a copy of the
80      *                    input data, and keep that. Even if sharing is allowed,
81      *                    the implementation may still decide to make a deep
82      *                    copy of the input data. If an image is progressively encoded,
83      *                    allowing sharing may degrade the decoding speed.
84      * @return BitmapRegionDecoder, or null if the image data could not be decoded.
85      * @throws IOException if the image format is not supported or can not be decoded.
86      */
newInstance( FileDescriptor fd, boolean isShareable)87     public static BitmapRegionDecoder newInstance(
88             FileDescriptor fd, boolean isShareable) throws IOException {
89         return nativeNewInstance(fd, isShareable);
90     }
91 
92     /**
93      * Create a BitmapRegionDecoder from an input stream.
94      * The stream's position will be where ever it was after the encoded data
95      * was read.
96      * Currently only the JPEG and PNG formats are supported.
97      *
98      * @param is The input stream that holds the raw data to be decoded into a
99      *           BitmapRegionDecoder.
100      * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
101      *                    shallow reference to the input. If this is false,
102      *                    then the BitmapRegionDecoder will explicitly make a copy of the
103      *                    input data, and keep that. Even if sharing is allowed,
104      *                    the implementation may still decide to make a deep
105      *                    copy of the input data. If an image is progressively encoded,
106      *                    allowing sharing may degrade the decoding speed.
107      * @return BitmapRegionDecoder, or null if the image data could not be decoded.
108      * @throws IOException if the image format is not supported or can not be decoded.
109      *
110      * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT},
111      * if {@link InputStream#markSupported is.markSupported()} returns true,
112      * <code>is.mark(1024)</code> would be called. As of
113      * {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.</p>
114      */
newInstance(InputStream is, boolean isShareable)115     public static BitmapRegionDecoder newInstance(InputStream is,
116             boolean isShareable) throws IOException {
117         if (is instanceof AssetManager.AssetInputStream) {
118             return nativeNewInstance(
119                     ((AssetManager.AssetInputStream) is).getNativeAsset(),
120                     isShareable);
121         } else {
122             // pass some temp storage down to the native code. 1024 is made up,
123             // but should be large enough to avoid too many small calls back
124             // into is.read(...).
125             byte [] tempStorage = new byte[16 * 1024];
126             return nativeNewInstance(is, tempStorage, isShareable);
127         }
128     }
129 
130     /**
131      * Create a BitmapRegionDecoder from a file path.
132      * Currently only the JPEG and PNG formats are supported.
133      *
134      * @param pathName complete path name for the file to be decoded.
135      * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
136      *                    shallow reference to the input. If this is false,
137      *                    then the BitmapRegionDecoder will explicitly make a copy of the
138      *                    input data, and keep that. Even if sharing is allowed,
139      *                    the implementation may still decide to make a deep
140      *                    copy of the input data. If an image is progressively encoded,
141      *                    allowing sharing may degrade the decoding speed.
142      * @return BitmapRegionDecoder, or null if the image data could not be decoded.
143      * @throws IOException if the image format is not supported or can not be decoded.
144      */
newInstance(String pathName, boolean isShareable)145     public static BitmapRegionDecoder newInstance(String pathName,
146             boolean isShareable) throws IOException {
147         BitmapRegionDecoder decoder = null;
148         InputStream stream = null;
149 
150         try {
151             stream = new FileInputStream(pathName);
152             decoder = newInstance(stream, isShareable);
153         } finally {
154             if (stream != null) {
155                 try {
156                     stream.close();
157                 } catch (IOException e) {
158                     // do nothing here
159                 }
160             }
161         }
162         return decoder;
163     }
164 
165     /*  Private constructor that must receive an already allocated native
166         region decoder int (pointer).
167 
168         This can be called from JNI code.
169     */
170     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
BitmapRegionDecoder(long decoder)171     private BitmapRegionDecoder(long decoder) {
172         mNativeBitmapRegionDecoder = decoder;
173         mRecycled = false;
174     }
175 
176     /**
177      * Decodes a rectangle region in the image specified by rect.
178      *
179      * @param rect The rectangle that specified the region to be decode.
180      * @param options null-ok; Options that control downsampling.
181      *             inPurgeable is not supported.
182      * @return The decoded bitmap, or null if the image data could not be
183      *         decoded.
184      * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
185      *         is {@link android.graphics.Bitmap.Config#HARDWARE}
186      *         and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
187      *         is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
188      *         function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
189      */
decodeRegion(Rect rect, BitmapFactory.Options options)190     public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) {
191         BitmapFactory.Options.validate(options);
192         synchronized (mNativeLock) {
193             checkRecycled("decodeRegion called on recycled region decoder");
194             if (rect.right <= 0 || rect.bottom <= 0 || rect.left >= getWidth()
195                     || rect.top >= getHeight())
196                 throw new IllegalArgumentException("rectangle is outside the image");
197             return nativeDecodeRegion(mNativeBitmapRegionDecoder, rect.left, rect.top,
198                     rect.right - rect.left, rect.bottom - rect.top, options,
199                     BitmapFactory.Options.nativeInBitmap(options),
200                     BitmapFactory.Options.nativeColorSpace(options));
201         }
202     }
203 
204     /** Returns the original image's width */
getWidth()205     public int getWidth() {
206         synchronized (mNativeLock) {
207             checkRecycled("getWidth called on recycled region decoder");
208             return nativeGetWidth(mNativeBitmapRegionDecoder);
209         }
210     }
211 
212     /** Returns the original image's height */
getHeight()213     public int getHeight() {
214         synchronized (mNativeLock) {
215             checkRecycled("getHeight called on recycled region decoder");
216             return nativeGetHeight(mNativeBitmapRegionDecoder);
217         }
218     }
219 
220     /**
221      * Frees up the memory associated with this region decoder, and mark the
222      * region decoder as "dead", meaning it will throw an exception if decodeRegion(),
223      * getWidth() or getHeight() is called.
224      *
225      * <p>This operation cannot be reversed, so it should only be called if you are
226      * sure there are no further uses for the region decoder. This is an advanced call,
227      * and normally need not be called, since the normal GC process will free up this
228      * memory when there are no more references to this region decoder.
229      */
recycle()230     public void recycle() {
231         synchronized (mNativeLock) {
232             if (!mRecycled) {
233                 nativeClean(mNativeBitmapRegionDecoder);
234                 mRecycled = true;
235             }
236         }
237     }
238 
239     /**
240      * Returns true if this region decoder has been recycled.
241      * If so, then it is an error to try use its method.
242      *
243      * @return true if the region decoder has been recycled
244      */
isRecycled()245     public final boolean isRecycled() {
246         return mRecycled;
247     }
248 
249     /**
250      * Called by methods that want to throw an exception if the region decoder
251      * has already been recycled.
252      */
checkRecycled(String errorMessage)253     private void checkRecycled(String errorMessage) {
254         if (mRecycled) {
255             throw new IllegalStateException(errorMessage);
256         }
257     }
258 
259     @Override
finalize()260     protected void finalize() throws Throwable {
261         try {
262             recycle();
263         } finally {
264             super.finalize();
265         }
266     }
267 
nativeDecodeRegion(long lbm, int start_x, int start_y, int width, int height, BitmapFactory.Options options, long inBitmapHandle, long colorSpaceHandle)268     private static native Bitmap nativeDecodeRegion(long lbm,
269             int start_x, int start_y, int width, int height,
270             BitmapFactory.Options options, long inBitmapHandle,
271             long colorSpaceHandle);
nativeGetWidth(long lbm)272     private static native int nativeGetWidth(long lbm);
nativeGetHeight(long lbm)273     private static native int nativeGetHeight(long lbm);
nativeClean(long lbm)274     private static native void nativeClean(long lbm);
275 
276     @UnsupportedAppUsage
nativeNewInstance( byte[] data, int offset, int length, boolean isShareable)277     private static native BitmapRegionDecoder nativeNewInstance(
278             byte[] data, int offset, int length, boolean isShareable);
nativeNewInstance( FileDescriptor fd, boolean isShareable)279     private static native BitmapRegionDecoder nativeNewInstance(
280             FileDescriptor fd, boolean isShareable);
nativeNewInstance( InputStream is, byte[] storage, boolean isShareable)281     private static native BitmapRegionDecoder nativeNewInstance(
282             InputStream is, byte[] storage, boolean isShareable);
nativeNewInstance( long asset, boolean isShareable)283     private static native BitmapRegionDecoder nativeNewInstance(
284             long asset, boolean isShareable);
285 }
286