POT textures #334 improvements and samples

This commit is contained in:
Emux
2017-03-16 14:21:32 +02:00
parent d1fdca170e
commit 4c9354c326
16 changed files with 151 additions and 246 deletions

View File

@@ -39,16 +39,16 @@ public interface Canvas {
*/
void drawText(String string, float x, float y, Paint fill, Paint stroke);
/**
* Draw Bitmap to fill target by stretching.
*/
void drawBitmap(Bitmap bitmap);
/**
* Draw Bitmap to Canvas.
*/
void drawBitmap(Bitmap bitmap, float x, float y);
/**
* Draw scaled Bitmap to fill target.
*/
void drawBitmapScaled(Bitmap bitmap);
void drawCircle(float x, float y, float radius, Paint paint);
void drawLine(float x1, float y1, float x2, float y2, Paint paint);

View File

@@ -23,7 +23,6 @@ package org.oscim.theme;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.XMLReaderAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
import org.oscim.backend.canvas.Color;
import org.oscim.backend.canvas.Paint.Cap;
import org.oscim.backend.canvas.Paint.FontFamily;
@@ -31,7 +30,6 @@ import org.oscim.backend.canvas.Paint.FontStyle;
import org.oscim.renderer.atlas.TextureAtlas;
import org.oscim.renderer.atlas.TextureAtlas.Rect;
import org.oscim.renderer.atlas.TextureRegion;
import org.oscim.renderer.bucket.TextureItem;
import org.oscim.theme.IRenderTheme.ThemeException;
import org.oscim.theme.rule.Rule;
import org.oscim.theme.rule.Rule.Closed;
@@ -50,7 +48,7 @@ import org.oscim.theme.styles.SymbolStyle;
import org.oscim.theme.styles.SymbolStyle.SymbolBuilder;
import org.oscim.theme.styles.TextStyle;
import org.oscim.theme.styles.TextStyle.TextBuilder;
import org.oscim.utils.math.MathUtils;
import org.oscim.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
@@ -561,7 +559,7 @@ public class XmlThemeBuilder extends DefaultHandler {
logUnknownAttribute(elementName, name, value, i);
}
b.texture = loadTexture(mTheme.getRelativePathPrefix(), src, b.symbolWidth, b.symbolHeight, b.symbolPercent);
b.texture = Utils.loadTexture(mTheme.getRelativePathPrefix(), src, b.symbolWidth, b.symbolHeight, b.symbolPercent);
/*if (b.texture != null)
b.texture.mipmap = true;*/
@@ -654,40 +652,11 @@ public class XmlThemeBuilder extends DefaultHandler {
logUnknownAttribute(elementName, name, value, i);
}
b.texture = loadTexture(mTheme.getRelativePathPrefix(), src, b.symbolWidth, b.symbolHeight, b.symbolPercent);
b.texture = Utils.loadTexture(mTheme.getRelativePathPrefix(), src, b.symbolWidth, b.symbolHeight, b.symbolPercent);
return b.build();
}
public static TextureItem loadTexture(String relativePathPrefix, String src, int width, int height, int percent) {
if (src == null || src.length() == 0)
return null;
try {
Bitmap bitmap = CanvasAdapter.getBitmapAsset(relativePathPrefix, src, width, height, percent);
if (bitmap != null) {
log.debug("loading {}", src);
if (ThemeLoader.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.drawBitmap(bitmap);
bitmap = potBitmap;
}
}
return new TextureItem(bitmap, true);
}
} catch (Exception e) {
log.debug("missing file / {}", e.getMessage());
}
return null;
}
private LineStyle createOutline(String style, Attributes attributes) {
if (style != null) {
LineStyle line = (LineStyle) mStyles.get(OUTLINE_STYLE + style);

View File

@@ -1,5 +1,6 @@
/*
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -14,8 +15,19 @@
*/
package org.oscim.utils;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
import org.oscim.renderer.bucket.TextureItem;
import org.oscim.theme.ThemeLoader;
import org.oscim.utils.math.MathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Utils {
private static final Logger log = LoggerFactory.getLogger(Utils.class);
/**
* Null safe equals.
*/
@@ -23,6 +35,38 @@ public final class Utils {
return (o1 == o2) || (o1 != null && o1.equals(o2));
}
/**
* Load a texture from a specified location and optional dimensions.
*/
public static TextureItem loadTexture(String relativePathPrefix, String src, int width, int height, int percent) {
if (src == null || src.length() == 0)
return null;
try {
Bitmap bitmap = CanvasAdapter.getBitmapAsset(relativePathPrefix, src, width, height, percent);
if (bitmap != null) {
log.debug("loading {}", src);
if (ThemeLoader.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) {
log.debug("missing file / {}", e.getMessage());
}
return null;
}
private Utils() {
throw new IllegalStateException();
}