int level, int xoffset, int yoffset,
int textureFormat, int imageFormat,
int imgXOffset, int imgYOffset,
int tilew, int width, int height,
int dataType, Object data) {
GL2 gl = context(ctx).getGL().getGL2();
int format = 0, internalFormat=0;
int numBytes = 0;
int type = GL2.GL_UNSIGNED_INT_8_8_8_8;
boolean forceAlphaToOne = false;
boolean pixelStore = false;
if (imgXOffset > 0 || (width < tilew)) {
pixelStore = true;
gl.glPixelStorei(GL2.GL_UNPACK_ROW_LENGTH, tilew);
}
switch (textureFormat) {
case Texture.INTENSITY:
internalFormat = GL2.GL_INTENSITY;
break;
case Texture.LUMINANCE:
internalFormat = GL.GL_LUMINANCE;
break;
case Texture.ALPHA:
internalFormat = GL.GL_ALPHA;
break;
case Texture.LUMINANCE_ALPHA:
internalFormat = GL.GL_LUMINANCE_ALPHA;
break;
case Texture.RGB:
internalFormat = GL.GL_RGB;
break;
case Texture.RGBA:
internalFormat = GL.GL_RGBA;
break;
default:
assert false;
}
if((dataType == ImageComponentRetained.IMAGE_DATA_TYPE_BYTE_ARRAY) ||
(dataType == ImageComponentRetained.IMAGE_DATA_TYPE_BYTE_BUFFER)) {
switch (imageFormat) {
case ImageComponentRetained.TYPE_BYTE_BGR:
format = GL2.GL_BGR;
numBytes = 3;
break;
case ImageComponentRetained.TYPE_BYTE_RGB:
format = GL.GL_RGB;
numBytes = 3;
break;
case ImageComponentRetained.TYPE_BYTE_ABGR:
if (gl.isExtensionAvailable("GL_EXT_abgr")) { // If its zero, should never come here!
format = GL2.GL_ABGR_EXT;
numBytes = 4;
} else {
assert false;
return;
}
break;
case ImageComponentRetained.TYPE_BYTE_RGBA:
// all RGB types are stored as RGBA
format = GL.GL_RGBA;
numBytes = 4;
break;
case ImageComponentRetained.TYPE_BYTE_LA:
// all LA types are stored as LA8
format = GL.GL_LUMINANCE_ALPHA;
numBytes = 2;
break;
case ImageComponentRetained.TYPE_BYTE_GRAY:
if (internalFormat == GL.GL_ALPHA) {
format = GL.GL_ALPHA;
numBytes = 1;
} else {
format = GL.GL_LUMINANCE;
numBytes = 1;
}
break;
case ImageComponentRetained.TYPE_USHORT_GRAY:
case ImageComponentRetained.TYPE_INT_BGR:
case ImageComponentRetained.TYPE_INT_RGB:
case ImageComponentRetained.TYPE_INT_ARGB:
default:
assert false;
return;
}
ByteBuffer buf = null;
if(dataType == ImageComponentRetained.IMAGE_DATA_TYPE_BYTE_ARRAY) {
buf = ByteBuffer.wrap((byte[]) data);
}
else {
buf = (ByteBuffer) data;
}
// offset by the imageOffset
buf.position((tilew * imgYOffset + imgXOffset) * numBytes);
gl.glTexSubImage2D(target, level, xoffset, yoffset, width, height,
format, GL.GL_UNSIGNED_BYTE, buf);
} else if((dataType == ImageComponentRetained.IMAGE_DATA_TYPE_INT_ARRAY) ||
(dataType == ImageComponentRetained.IMAGE_DATA_TYPE_INT_BUFFER)) {
switch (imageFormat) {
/* GL_BGR */
case ImageComponentRetained.TYPE_INT_BGR: /* Assume XBGR format */
format = GL.GL_RGBA;
type = GL2.GL_UNSIGNED_INT_8_8_8_8_REV;
forceAlphaToOne = true;
break;
case ImageComponentRetained.TYPE_INT_RGB: /* Assume XRGB format */
forceAlphaToOne = true;
/* Fall through to next case */
case ImageComponentRetained.TYPE_INT_ARGB:
format = GL2.GL_BGRA;
type = GL2.GL_UNSIGNED_INT_8_8_8_8_REV;
break;
/* This method only supports 3 and 4 components formats and INT types. */
case ImageComponentRetained.TYPE_BYTE_LA:
case ImageComponentRetained.TYPE_BYTE_GRAY:
case ImageComponentRetained.TYPE_USHORT_GRAY:
case ImageComponentRetained.TYPE_BYTE_BGR:
case ImageComponentRetained.TYPE_BYTE_RGB:
case ImageComponentRetained.TYPE_BYTE_RGBA:
case ImageComponentRetained.TYPE_BYTE_ABGR:
default:
assert false;
return;
}
/* Force Alpha to 1.0 if needed */
if(forceAlphaToOne) {
gl.glPixelTransferf(GL2.GL_ALPHA_SCALE, 0.0f);
gl.glPixelTransferf(GL2.GL_ALPHA_BIAS, 1.0f);
}
IntBuffer buf = null;
if(dataType == ImageComponentRetained.IMAGE_DATA_TYPE_INT_ARRAY) {
buf = IntBuffer.wrap((int[]) data);
}
else {
buf = (IntBuffer) data;
}
// offset by the imageOffset
buf.position(tilew * imgYOffset + imgXOffset);
gl.glTexSubImage2D(target, level, xoffset, yoffset, width, height,
format, type, buf);
/* Restore Alpha scale and bias */
if(forceAlphaToOne) {
gl.glPixelTransferf(GL2.GL_ALPHA_SCALE, 1.0f);
gl.glPixelTransferf(GL2.GL_ALPHA_BIAS, 0.0f);
}
} else {
assert false;
return;
}
if (pixelStore) {
gl.glPixelStorei(GL2.GL_UNPACK_ROW_LENGTH, 0);
}
}