Fix POT textures in render themes (#443)

This commit is contained in:
Longri
2017-11-12 07:34:40 +01:00
committed by Emux
parent a178357ca8
commit df784f6f68
3 changed files with 26 additions and 18 deletions

View File

@@ -616,7 +616,7 @@ public class XmlMapsforgeThemeBuilder extends DefaultHandler {
x += f; x += f;
transparent = !transparent; transparent = !transparent;
} }
b.texture = new TextureItem(bitmap); b.texture = new TextureItem(Utils.potBitmap(bitmap));
b.texture.mipmap = true; b.texture.mipmap = true;
b.randomOffset = false; b.randomOffset = false;
b.stipple = width; b.stipple = width;
@@ -633,7 +633,7 @@ public class XmlMapsforgeThemeBuilder extends DefaultHandler {
Canvas canvas = CanvasAdapter.newCanvas(); Canvas canvas = CanvasAdapter.newCanvas();
canvas.setBitmap(bitmap); canvas.setBitmap(bitmap);
canvas.drawBitmap(b.texture.bitmap, b.repeatStart, 0); canvas.drawBitmap(b.texture.bitmap, b.repeatStart, 0);
b.texture = new TextureItem(bitmap); b.texture = new TextureItem(Utils.potBitmap(bitmap));
b.texture.mipmap = true; b.texture.mipmap = true;
b.fixed = true; b.fixed = true;
b.randomOffset = false; b.randomOffset = false;

View File

@@ -608,7 +608,7 @@ public class XmlThemeBuilder extends DefaultHandler {
x += f; x += f;
transparent = !transparent; transparent = !transparent;
} }
b.texture = new TextureItem(bitmap); b.texture = new TextureItem(Utils.potBitmap(bitmap));
b.texture.mipmap = true; b.texture.mipmap = true;
b.randomOffset = false; b.randomOffset = false;
b.stipple = width; b.stipple = width;
@@ -625,7 +625,7 @@ public class XmlThemeBuilder extends DefaultHandler {
Canvas canvas = CanvasAdapter.newCanvas(); Canvas canvas = CanvasAdapter.newCanvas();
canvas.setBitmap(bitmap); canvas.setBitmap(bitmap);
canvas.drawBitmap(b.texture.bitmap, b.repeatStart, 0); canvas.drawBitmap(b.texture.bitmap, b.repeatStart, 0);
b.texture = new TextureItem(bitmap); b.texture = new TextureItem(Utils.potBitmap(bitmap));
b.texture.mipmap = true; b.texture.mipmap = true;
b.fixed = true; b.fixed = true;
b.randomOffset = false; b.randomOffset = false;

View File

@@ -45,20 +45,7 @@ public final class Utils {
Bitmap bitmap = CanvasAdapter.getBitmapAsset(relativePathPrefix, src, width, height, percent); Bitmap bitmap = CanvasAdapter.getBitmapAsset(relativePathPrefix, src, width, height, percent);
if (bitmap != null) { if (bitmap != null) {
log.debug("loading {}", src); log.debug("loading {}", src);
return new TextureItem(potBitmap(bitmap), true);
if (Parameters.POT_TEXTURES) {
int potWidth = MathUtils.nextPowerOfTwo(bitmap.getWidth());
int potHeight = MathUtils.nextPowerOfTwo(bitmap.getHeight());
if (potWidth != bitmap.getWidth() || potHeight != bitmap.getHeight()) {
log.debug("POT texture: {}x{} -> {}x{}", bitmap.getWidth(), bitmap.getHeight(), potWidth, potHeight);
Bitmap potBitmap = CanvasAdapter.newBitmap(potWidth, potHeight, 0);
Canvas canvas = CanvasAdapter.newCanvas();
canvas.setBitmap(potBitmap);
canvas.drawBitmapScaled(bitmap);
bitmap = potBitmap;
}
}
return new TextureItem(bitmap, true);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("{}: missing file / {}", src, e.getMessage()); log.error("{}: missing file / {}", src, e.getMessage());
@@ -66,6 +53,27 @@ public final class Utils {
return null; return null;
} }
/**
* Returns a Bitmap with POT size, if {@link Parameters#POT_TEXTURES} is true.
* Else the returned Bitmap is the same instance of given Bitmap.
* If the given Bitmap has POT size, the given instance is returned.
*/
public static Bitmap potBitmap(Bitmap bitmap) {
if (Parameters.POT_TEXTURES) {
int potWidth = MathUtils.nextPowerOfTwo(bitmap.getWidth());
int potHeight = MathUtils.nextPowerOfTwo(bitmap.getHeight());
if (potWidth != bitmap.getWidth() || potHeight != bitmap.getHeight()) {
log.debug("POT texture: {}x{} -> {}x{}", bitmap.getWidth(), bitmap.getHeight(), potWidth, potHeight);
Bitmap potBitmap = CanvasAdapter.newBitmap(potWidth, potHeight, 0);
Canvas potCanvas = CanvasAdapter.newCanvas();
potCanvas.setBitmap(potBitmap);
potCanvas.drawBitmapScaled(bitmap);
bitmap = potBitmap;
}
}
return bitmap;
}
private Utils() { private Utils() {
throw new IllegalStateException(); throw new IllegalStateException();
} }