OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / opengl / java / android / opengl / Texture.java
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package android.opengl;
18
19 import java.io.InputStream;
20 import java.io.IOException;
21 import java.nio.Buffer;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import javax.microedition.khronos.opengles.GL10;
25
26 import android.content.res.AssetManager;
27
28 /**
29  * {@hide}
30  */
31 public class Texture {
32
33     private int width, height, bpp;
34     private ByteBuffer data;
35     private int name = -1;
36
37     // Texture maps have the following format.  All integers
38     // are 16 bits, high byte first.  Pixels are in 5/6/5
39     // RGB format, low byte first.
40     //
41     // width
42     // height
43     // pixel (0, 0)
44     // pixel (1, 0)
45     // ...
46     // pixel (width - 1, height - 1)
47
48     private int readInt16(InputStream is) throws IOException {
49         return is.read() | (is.read() << 8);
50     }
51
52     public Texture(InputStream is) throws IOException {
53         this.width  = readInt16(is);
54         this.height  = readInt16(is);
55         this.bpp = 2;
56
57         int npixels = width*height;
58         int nbytes = npixels*bpp;
59         byte[] arr = new byte[nbytes];
60
61         int idx = 0;
62         while (idx < nbytes) {
63             int nread = is.read(arr, idx, nbytes - idx);
64             idx += nread;
65         }
66
67         if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
68             // Swap pairs of bytes on big-endian platforms
69             for (int i = 0; i < npixels; i++) {
70                 int j = i*2;
71                 int k = j + 1;
72
73                 byte tmp = arr[j];
74                 arr[j] = arr[k];
75                 arr[k] = tmp;
76             }
77         }
78
79         this.data = ByteBuffer.allocateDirect(arr.length);
80         this.data.order(ByteOrder.nativeOrder());
81         data.put(arr);
82         data.position(0);
83     }
84
85     private int loadTexture(GL10 gl,
86             int textureUnit,
87             int minFilter, int magFilter,
88             int wrapS, int wrapT,
89             int mode,
90             int width, int height,
91             int dataType,
92             Buffer data) {
93         int[] texture = new int[1];
94         gl.glGenTextures(1, texture, 0);
95
96         gl.glEnable(gl.GL_TEXTURE_2D);
97         gl.glClientActiveTexture(textureUnit);
98         gl.glBindTexture(gl.GL_TEXTURE_2D, texture[0]);
99         gl.glTexParameterf(gl.GL_TEXTURE_2D,
100                 gl.GL_TEXTURE_MIN_FILTER,
101                 minFilter);
102         gl.glTexParameterf(gl.GL_TEXTURE_2D,
103                 gl.GL_TEXTURE_MAG_FILTER,
104                 magFilter);
105         gl.glTexParameterf(gl.GL_TEXTURE_2D,
106                 gl.GL_TEXTURE_WRAP_S,
107                 wrapS);
108         gl.glTexParameterf(gl.GL_TEXTURE_2D,
109                 gl.GL_TEXTURE_WRAP_T,
110                 wrapT);
111         gl.glTexEnvf(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE, mode);
112
113         gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB,
114                 width, height,
115                 0, gl.GL_RGB, dataType,
116                 data);
117
118         return texture[0];
119     }
120
121     public void setTextureParameters(GL10 gl) {
122         if (name < 0) {
123             name = loadTexture(gl,
124                     gl.GL_TEXTURE0,
125                     gl.GL_NEAREST, gl.GL_NEAREST,
126                     gl.GL_REPEAT, gl.GL_REPEAT,
127                     gl.GL_MODULATE,
128                     width, height,
129                     gl.GL_UNSIGNED_SHORT_5_6_5,
130                     data);
131         }
132
133         gl.glBindTexture(gl.GL_TEXTURE_2D, name);
134     }
135 }