3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 import android
.util
.Log
;
26 import android
.graphics
.Bitmap
;
27 import static android
.opengl
.GLES11
.*;
28 import android
.opengl
.GLUtils
;
31 * Utilities for dealing with #Bitmap objects and OpenGL.
33 final class BitmapUtil
{
34 private static final String TAG
= "XCSoar";
36 public static int validateTextureSize(int i
) {
37 return NativeView
.validateTextureSize(i
);
41 * Initialize the current texture and load the specified Bitmap into
44 private static boolean loadTexture(Bitmap bmp
) {
45 int internalFormat
, format
, type
;
48 switch (bmp
.getConfig()) {
51 internalFormat
= format
= GL_RGBA
;
52 type
= GL_UNSIGNED_BYTE
;
57 internalFormat
= format
= GL_RGB
;
58 type
= GL_UNSIGNED_SHORT_5_6_5
;
63 internalFormat
= format
= GL_LUMINANCE
;
64 type
= GL_UNSIGNED_BYTE
;
72 /* create an empty texture, and load the Bitmap into it */
74 glTexImage2D(GL_TEXTURE_2D
, 0, internalFormat
,
75 validateTextureSize(bmp
.getWidth()),
76 validateTextureSize(bmp
.getHeight()),
77 0, format
, type
, null);
78 glPixelStorei(GL_UNPACK_ALIGNMENT
, unpackAlignment
);
79 GLUtils
.texSubImage2D(GL_TEXTURE_2D
, 0, 0, 0, bmp
, format
, type
);
84 * Loads an Android Bitmap as OpenGL texture.
86 * @param result an array of 3 integers: texture id, width, height
88 * @return true on success
90 public static boolean bitmapToOpenGL(Bitmap bmp
, int[] result
) {
94 result
[1] = bmp
.getWidth();
95 result
[2] = bmp
.getHeight();
97 if (bmp
.getConfig() == null) {
98 /* convert to a format compatible with OpenGL */
99 Bitmap tmp
= bmp
.copy(Bitmap
.Config
.RGB_565
, false);
108 /* create and configure an OpenGL texture */
110 glGenTextures(1, result
, 0);
111 glBindTexture(GL_TEXTURE_2D
, result
[0]);
112 glTexParameterf(GL_TEXTURE_2D
,
113 GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
114 glTexParameterf(GL_TEXTURE_2D
,
115 GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
116 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
118 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
,
122 if (!loadTexture(bmp
)) {
123 glDeleteTextures(1, result
, 0);
126 } catch (Exception e
) {
127 glDeleteTextures(1, result
, 0);
128 Log
.e(TAG
, "GLUtils error: " + e
);