android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / android / src / BitmapUtil.java
blobd6096a19be398e197c22a00c0e312c25300af77c
1 /* Copyright_License {
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.
23 package org.xcsoar;
25 import android.util.Log;
26 import android.graphics.Bitmap;
27 import static android.opengl.GLES11.*;
28 import android.opengl.GLUtils;
30 /**
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);
40 /**
41 * Initialize the current texture and load the specified Bitmap into
42 * it.
44 private static boolean loadTexture(Bitmap bmp) {
45 int internalFormat, format, type;
46 int unpackAlignment;
48 switch (bmp.getConfig()) {
49 case ARGB_4444:
50 case ARGB_8888:
51 internalFormat = format = GL_RGBA;
52 type = GL_UNSIGNED_BYTE;
53 unpackAlignment = 4;
54 break;
56 case RGB_565:
57 internalFormat = format = GL_RGB;
58 type = GL_UNSIGNED_SHORT_5_6_5;
59 unpackAlignment = 2;
60 break;
62 case ALPHA_8:
63 internalFormat = format = GL_LUMINANCE;
64 type = GL_UNSIGNED_BYTE;
65 unpackAlignment = 1;
66 break;
68 default:
69 return false;
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);
80 return true;
83 /**
84 * Loads an Android Bitmap as OpenGL texture.
86 * @param result an array of 3 integers: texture id, width, height
87 * (all output)
88 * @return true on success
90 public static boolean bitmapToOpenGL(Bitmap bmp, int[] result) {
91 if (bmp == null)
92 return false;
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);
100 bmp.recycle();
102 if (tmp == null)
103 return false;
105 bmp = tmp;
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,
117 GL_NEAREST);
118 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
119 GL_NEAREST);
121 try {
122 if (!loadTexture(bmp)) {
123 glDeleteTextures(1, result, 0);
124 return false;
126 } catch (Exception e) {
127 glDeleteTextures(1, result, 0);
128 Log.e(TAG, "GLUtils error: " + e);
129 return false;
130 } finally {
131 bmp.recycle();
134 /* done */
136 return true;