From 89a41fc962d3f9f3444eea11874d59a15d279d9a Mon Sep 17 00:00:00 2001 From: Paul Wedeck Date: Tue, 24 Jan 2023 19:58:32 +0100 Subject: [PATCH] add necessary format conversion to GLESDrawContext and OriginalImageProvider --- .../go/graphics/android/GLESDrawContext.java | 26 +++++++++++++++++-- .../core/resources/OriginalImageProvider.java | 16 +++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/go.graphics.android/src/main/java/go/graphics/android/GLESDrawContext.java b/go.graphics.android/src/main/java/go/graphics/android/GLESDrawContext.java index a9e2385f8..f03a81b9c 100644 --- a/go.graphics.android/src/main/java/go/graphics/android/GLESDrawContext.java +++ b/go.graphics.android/src/main/java/go/graphics/android/GLESDrawContext.java @@ -24,6 +24,7 @@ import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.nio.FloatBuffer; +import java.nio.IntBuffer; import java.util.ArrayList; import go.graphics.AbstractColor; @@ -117,9 +118,30 @@ public TextureHandle generateTexture(ImageData image, String name) { return texture; } + private ImageData fixFormat(ImageData orig) { + if(orig == null) return null; + + ImageData conv = new ImageData(orig.getWidth(), orig.getHeight()); + IntBuffer from = orig.getReadData32(); + IntBuffer to = conv.getWriteData32(); + while(from.hasRemaining()) { + int pixel = from.get(); + int r = pixel&0xFF; + int g = (pixel>>8)&0xFF; + int b = (pixel>>16)&0xFF; + int a = (pixel>>24)&0xFF; + + int reversed = r<<24 | g << 16 | b << 8 | a; + to.put(reversed); + } + + to.rewind(); + return conv; + } + public TextureHandle resizeTexture(TextureHandle textureIndex, ImageData image) { bindTexture(textureIndex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getReadData32()); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, fixFormat(image).getReadData32()); return textureIndex; } @@ -127,7 +149,7 @@ public void updateTexture(TextureHandle textureIndex, int left, int bottom, ImageData image) { bindTexture(textureIndex); glTexSubImage2D(GL_TEXTURE_2D, 0, left, bottom, image.getWidth(), image.getHeight(), - GL_RGBA, GL_UNSIGNED_BYTE, image.getReadData32()); + GL_RGBA, GL_UNSIGNED_BYTE, fixFormat(image).getReadData32()); } protected void bindTexture(TextureHandle texture) { diff --git a/jsettlers.main.android/src/main/java/jsettlers/main/android/core/resources/OriginalImageProvider.java b/jsettlers.main.android/src/main/java/jsettlers/main/android/core/resources/OriginalImageProvider.java index 50e498e7f..2bdc27822 100644 --- a/jsettlers.main.android/src/main/java/jsettlers/main/android/core/resources/OriginalImageProvider.java +++ b/jsettlers.main.android/src/main/java/jsettlers/main/android/core/resources/OriginalImageProvider.java @@ -15,6 +15,8 @@ package jsettlers.main.android.core.resources; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.IntBuffer; import java.nio.ShortBuffer; import java.util.ArrayList; @@ -97,6 +99,18 @@ public ImageReference(ImageLink image) { this.image = image; } + + private IntBuffer fixRGBA8Buffer(IntBuffer orig) { + IntBuffer bfr = ByteBuffer.allocateDirect(orig.remaining()*4).order(ByteOrder.nativeOrder()).asIntBuffer(); + while(orig.hasRemaining()) { + int color = orig.get(); + bfr.put(((color<<24)&0xFF000000) | ((color >> 8)&0xFFFFFF)); + } + orig.rewind(); + bfr.rewind(); + return bfr; + } + public void load() { Image loaded = ImageProvider.getInstance().getImage(image); int[] colors = new int[1]; @@ -105,7 +119,7 @@ public void load() { if (loaded instanceof SingleImage) { ImageData data = ((SingleImage) loaded).getData(); - IntBuffer bfr = data.getReadData32(); + IntBuffer bfr = fixRGBA8Buffer(data.getReadData32()); width = data.getWidth(); height = data.getHeight();