Beatmup Java package
Bitmap.java
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 package Beatmup.Android;
20 
21 import android.graphics.BitmapFactory;
22 
23 import java.io.FileDescriptor;
24 import java.io.InputStream;
25 
26 import Beatmup.Context;
27 
28 /**
29  * Android bitmap wrapping.
30  * Enables access to Android bitmaps from inside the Beatmup engine without memory copy.
31  */
32 public class Bitmap extends Beatmup.Bitmap {
33  private android.graphics.Bitmap source; //!< wrapped Android bitmap
34 
35  /**
36  * Creates new bitmap from Android bitmap object without memory copy.
37  * @param context Beatmup context
38  * @param bitmap source bitmap
39  */
40  private Bitmap(Context context, android.graphics.Bitmap bitmap) {
41  super(context, bitmap);
42  source = bitmap;
43  }
44 
45  /**
46  * Creates new bitmap from a source Android bitmap converting the data into appropriate pixel format if necessary.
47  * @param context the context handling the new bitmap
48  * @param bitmap the source; may be recycled
49  * @return new bitmap
50  */
51  private static Bitmap createEnsuringPixelFormat(Context context, android.graphics.Bitmap bitmap) {
52  if (bitmap.getConfig() != android.graphics.Bitmap.Config.ALPHA_8 && bitmap.getConfig() != android.graphics.Bitmap.Config.ARGB_8888) {
53  android.graphics.Bitmap copy = bitmap.copy(android.graphics.Bitmap.Config.ARGB_8888, true);
54  bitmap.recycle();
55  return new Bitmap(context, copy);
56  } else
57  return new Bitmap(context, bitmap);
58  }
59 
60  /**
61  * Decodes a bitmap from stream.
62  * @param context a Beatmup context
63  * @param inputStream the stream to decode
64  * @return bitmap containing the decoded stream content
65  */
66  public static Bitmap decodeStream(Context context, InputStream inputStream) throws OutOfMemoryError {
67  android.graphics.Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
68  if (bitmap == null)
69  return null;
70  return createEnsuringPixelFormat(context, bitmap);
71  }
72 
73  /**
74  * Loads a bitmap from file. If the pixel format is not supported, this function tries to convert the source data to a 32-bit color bitmap.
75  * @param context a Beatmup context
76  * @param path path to the file to decode
77  * @param options bitmap loader options
78  * @return bitmap from the file
79  */
80  public static Bitmap decodeFile(Context context, String path, BitmapFactory.Options options) throws OutOfMemoryError {
81  android.graphics.Bitmap bitmap = BitmapFactory.decodeFile(path, options);
82  if (bitmap == null)
83  return null;
84  return createEnsuringPixelFormat(context, bitmap);
85  }
86 
87  /**
88  * Loads a bitmap from file.
89  * @param context a Beatmup context
90  * @param file a file descriptor of the content to decode
91  * @param outPadding outPadding returned by BitmapFactory.decodeFileDescriptor
92  * @param options decoding options
93  * @return bitmap from the file
94  */
96  Context context,
97  FileDescriptor file,
98  android.graphics.Rect outPadding,
99  BitmapFactory.Options options
100  ) throws OutOfMemoryError
101  {
102  android.graphics.Bitmap bitmap = BitmapFactory.decodeFileDescriptor(file, outPadding, options);
103  if (bitmap == null)
104  return null;
105  return createEnsuringPixelFormat(context, bitmap);
106  }
107 
108  /**
109  * Creates empty ARGB bitmap of specified size
110  * @param context a Beatmup context
111  * @param width width of the new bitmap in pixels
112  * @param height height of the new bitmap in pixels
113  * @return the new bitmap
114  */
115  public static Bitmap createColorBitmap(Context context, int width, int height) {
116  return new Bitmap(context, android.graphics.Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888));
117  }
118 
119  /**
120  * Creates empty grayscale bitmap of specified size
121  * @param context a Beatmup context
122  * @param width width of the new bitmap in pixels
123  * @param height height of the new bitmap in pixels
124  * @return the new bitmap
125  */
126  public static Bitmap createGrayscaleBitmap(Context context, int width, int height) {
127  return new Bitmap(context, android.graphics.Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ALPHA_8));
128  }
129 
130  /**
131  * @return the wrapped Android bitmap instance.
132  */
133  public android.graphics.Bitmap getBitmap() {
134  return source;
135  }
136 
137  @Override
138  public synchronized void dispose() {
139  if (source != null) {
140  source.recycle();
141  source = null;
142  }
143  super.dispose();
144  }
145 
146  /**
147  * @return copy of this bitmap
148  */
149  public Bitmap clone() {
150  return new Bitmap(context, source.copy(source.getConfig(), true));
151  }
152 }
Android bitmap wrapping.
Definition: Bitmap.java:32
static Bitmap decodeStream(Context context, InputStream inputStream)
Decodes a bitmap from stream.
Definition: Bitmap.java:66
android.graphics.Bitmap getBitmap()
Definition: Bitmap.java:133
static Bitmap createEnsuringPixelFormat(Context context, android.graphics.Bitmap bitmap)
Creates new bitmap from a source Android bitmap converting the data into appropriate pixel format if ...
Definition: Bitmap.java:51
android.graphics.Bitmap source
wrapped Android bitmap
Definition: Bitmap.java:33
static Bitmap createColorBitmap(Context context, int width, int height)
Creates empty ARGB bitmap of specified size.
Definition: Bitmap.java:115
static Bitmap decodeFileDescriptor(Context context, FileDescriptor file, android.graphics.Rect outPadding, BitmapFactory.Options options)
Loads a bitmap from file.
Definition: Bitmap.java:95
static Bitmap createGrayscaleBitmap(Context context, int width, int height)
Creates empty grayscale bitmap of specified size.
Definition: Bitmap.java:126
static Bitmap decodeFile(Context context, String path, BitmapFactory.Options options)
Loads a bitmap from file.
Definition: Bitmap.java:80
Bitmap(Context context, android.graphics.Bitmap bitmap)
Creates new bitmap from Android bitmap object without memory copy.
Definition: Bitmap.java:40
synchronized void dispose()
Destroys the native object.
Definition: Bitmap.java:138
Beatmup engine context for Android.
Definition: Context.java:24