Permalink
Browse files

Render themes: PNG scaling (#1091)

  • Loading branch information...
Adrian Batzill authored and devemux86 committed Oct 3, 2018
1 parent 003deb0 commit 62e0875acea039a74426442562e65c6d895b0d60
@@ -2,6 +2,7 @@
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2014 Ludwig M Brinckmann
* Copyright 2017 usrusr
* Copyright 2018 Adrian Batzill
*
* 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
@@ -51,7 +52,7 @@
PointTextContainer createPointTextContainer(Point xy, Display display, int priority, String text, Paint paintFront, Paint paintBack,
SymbolContainer symbolContainer, Position position, int maxTextWidth);
ResourceBitmap createResourceBitmap(InputStream inputStream, int hash) throws IOException;
ResourceBitmap createResourceBitmap(InputStream inputStream, float scaleFactor, int width, int height, int percent, int hash) throws IOException;
TileBitmap createTileBitmap(InputStream inputStream, int tileSize, boolean isTransparent) throws IOException;
@@ -1,6 +1,7 @@
/*
* Copyright 2014 Ludwig M Brinckmann
* Copyright 2016 devemux86
* Copyright 2018 Adrian Batzill
*
* 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
@@ -58,6 +59,45 @@ public static int getAlpha(int color) {
return (color >> 24) & 0xff;
}
/**
* Given the original image size, as well as width, height, percent parameters,
* can compute the final image size.
*
* @param picWidth original image width
* @param picHeight original image height
* @param scaleFactor scale factor to screen DPI
* @param width requested width (0: no change)
* @param height requested height (0: no change)
* @param percent requested scale percent (100: no change)
*/
public static float[] imageSize(float picWidth, float picHeight, float scaleFactor, int width, int height, int percent) {
float bitmapWidth = picWidth * scaleFactor;
float bitmapHeight = picHeight * scaleFactor;
float aspectRatio = picWidth / picHeight;
if (width != 0 && height != 0) {
// both width and height set, override any other setting
bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
}
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
return new float[]{bitmapWidth, bitmapHeight};
}
private GraphicUtils() {
// noop, just to make tools happy.
}
@@ -3,6 +3,7 @@
* Copyright 2014 Ludwig M Brinckmann
* Copyright 2014-2017 devemux86
* Copyright 2017 usrusr
* Copyright 2018 Adrian Batzill
*
* 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
@@ -258,8 +259,8 @@ public PointTextContainer createPointTextContainer(Point xy, Display display, in
}
@Override
public ResourceBitmap createResourceBitmap(InputStream inputStream, int hash) throws IOException {
return new AndroidResourceBitmap(inputStream, hash);
public ResourceBitmap createResourceBitmap(InputStream inputStream, float scaleFactor, int width, int height, int percent, int hash) throws IOException {
return new AndroidResourceBitmap(inputStream, scaleFactor, width, height, percent, hash);
}
@Override
@@ -2,6 +2,7 @@
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013-2014 Ludwig M Brinckmann
* Copyright 2014 devemux86
* Copyright 2018 Adrian Batzill
*
* 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
@@ -20,6 +21,7 @@
import android.graphics.BitmapFactory;
import android.util.Pair;
import org.mapsforge.core.graphics.GraphicUtils;
import org.mapsforge.core.graphics.ResourceBitmap;
import java.io.IOException;
@@ -68,7 +70,7 @@ public static void clearResourceBitmaps() {
}
}
private static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, int hash) throws IOException {
private static android.graphics.Bitmap getResourceBitmap(InputStream inputStream, float scaleFactor, int width, int height, int percent, int hash) throws IOException {
synchronized (RESOURCE_BITMAPS) {
Pair<android.graphics.Bitmap, Integer> data = RESOURCE_BITMAPS.get(hash);
if (data != null) {
@@ -82,6 +84,9 @@ public static void clearResourceBitmaps() {
if (bitmap == null) {
throw new IOException("BitmapFactory failed to decodeStream");
}
float[] newSize = GraphicUtils.imageSize(bitmap.getWidth(), bitmap.getHeight(), scaleFactor, width, height, percent);
if ((int) newSize[0] != bitmap.getWidth() || (int) newSize[1] != bitmap.getHeight())
bitmap = Bitmap.createScaledBitmap(bitmap, (int) newSize[0], (int) newSize[1], true);
Pair<android.graphics.Bitmap, Integer> updated = new Pair<android.graphics.Bitmap, Integer>(bitmap,
Integer.valueOf(1));
RESOURCE_BITMAPS.put(hash, updated);
@@ -139,9 +144,9 @@ protected AndroidResourceBitmap(int hash) {
this.hash = hash;
}
AndroidResourceBitmap(InputStream inputStream, int hash) throws IOException {
AndroidResourceBitmap(InputStream inputStream, float scaleFactor, int width, int height, int percent, int hash) throws IOException {
this(hash);
this.bitmap = getResourceBitmap(inputStream, hash);
this.bitmap = getResourceBitmap(inputStream, scaleFactor, width, height, percent, hash);
}
// destroy is the super method here, which will take care of bitmap accounting
@@ -2,6 +2,7 @@
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013-2014 Ludwig M Brinckmann
* Copyright 2014-2016 devemux86
* Copyright 2018 Adrian Batzill
*
* 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
@@ -24,6 +25,8 @@
import com.caverock.androidsvg.SVG;
import org.mapsforge.core.graphics.GraphicUtils;
import java.io.IOException;
import java.io.InputStream;
@@ -41,34 +44,12 @@
double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);
float bitmapWidth = (float) (picture.getWidth() * scale);
float bitmapHeight = (float) (picture.getHeight() * scale);
float aspectRatio = (1f * picture.getWidth()) / picture.getHeight();
if (width != 0 && height != 0) {
// both width and height set, override any other setting
bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
}
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
float[] bmpSize = GraphicUtils.imageSize(picture.getWidth(), picture.getHeight(), (float) scale, width, height, percent);
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth),
(int) Math.ceil(bitmapHeight), AndroidGraphicFactory.TRANSPARENT_BITMAP);
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bmpSize[0]),
(int) Math.ceil(bmpSize[1]), AndroidGraphicFactory.TRANSPARENT_BITMAP);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight));
canvas.drawPicture(picture, new RectF(0, 0, bmpSize[0], bmpSize[1]));
return bitmap;
} catch (Exception e) {
@@ -5,6 +5,7 @@
* Copyright 2014 Develar
* Copyright 2015-2017 devemux86
* Copyright 2017 usrusr
* Copyright 2018 Adrian Batzill
*
* 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
@@ -194,8 +195,8 @@ public PointTextContainer createPointTextContainer(Point xy, Display display, in
}
@Override
public ResourceBitmap createResourceBitmap(InputStream inputStream, int hash) throws IOException {
return new AwtResourceBitmap(inputStream);
public ResourceBitmap createResourceBitmap(InputStream inputStream, float scaleFactor, int width, int height, int percent, int hash) throws IOException {
return new AwtResourceBitmap(inputStream, scaleFactor, width, height, percent);
}
@Override
@@ -1,6 +1,7 @@
/*
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2015 devemux86
* Copyright 2018 Adrian Batzill
*
* 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
@@ -15,6 +16,7 @@
*/
package org.mapsforge.map.awt.graphics;
import org.mapsforge.core.graphics.GraphicUtils;
import org.mapsforge.core.graphics.ResourceBitmap;
import java.awt.image.BufferedImage;
@@ -23,8 +25,10 @@
public class AwtResourceBitmap extends AwtBitmap implements ResourceBitmap {
AwtResourceBitmap(InputStream inputStream) throws IOException {
AwtResourceBitmap(InputStream inputStream, float scaleFactor, int width, int height, int percent) throws IOException {
super(inputStream);
float[] newSize = GraphicUtils.imageSize(getWidth(), getHeight(), scaleFactor, width, height, percent);
scaleTo((int) newSize[0], (int) newSize[1]);
}
AwtResourceBitmap(BufferedImage bufferedImage) {
@@ -1,5 +1,6 @@
/*
* Copyright 2015-2018 devemux86
* Copyright 2018 Adrian Batzill
*
* 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
@@ -18,6 +19,8 @@
import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.app.beans.SVGIcon;
import org.mapsforge.core.graphics.GraphicUtils;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
@@ -37,34 +40,12 @@ public static BufferedImage getResourceBitmap(InputStream inputStream, String na
double scale = scaleFactor / Math.sqrt((diagram.getHeight() * diagram.getWidth()) / defaultSize);
float bitmapWidth = (float) (diagram.getWidth() * scale);
float bitmapHeight = (float) (diagram.getHeight() * scale);
float aspectRatio = diagram.getWidth() / diagram.getHeight();
if (width != 0 && height != 0) {
// both width and height set, override any other setting
bitmapWidth = width;
bitmapHeight = height;
} else if (width == 0 && height != 0) {
// only width set, calculate from aspect ratio
bitmapWidth = height * aspectRatio;
bitmapHeight = height;
} else if (width != 0 && height == 0) {
// only height set, calculate from aspect ratio
bitmapHeight = width / aspectRatio;
bitmapWidth = width;
}
if (percent != 100) {
bitmapWidth *= percent / 100f;
bitmapHeight *= percent / 100f;
}
float[] bmpSize = GraphicUtils.imageSize(diagram.getWidth(), diagram.getHeight(), (float) scale, width, height, percent);
SVGIcon icon = new SVGIcon();
icon.setAntiAlias(true);
icon.setAutosize(SVGIcon.AUTOSIZE_STRETCH);
icon.setPreferredSize(new Dimension((int) bitmapWidth, (int) bitmapHeight));
icon.setPreferredSize(new Dimension((int) bmpSize[0], (int) bmpSize[1]));
icon.setSvgURI(uri);
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
icon.paintIcon(null, bufferedImage.createGraphics(), 0, 0);
@@ -2,6 +2,7 @@
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2014 Ludwig M Brinckmann
* Copyright 2014-2018 devemux86
* Copyright 2018 Adrian Batzill
*
* 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
@@ -68,7 +69,7 @@ public static ResourceBitmap createBitmap(GraphicFactory graphicFactory, Display
}
}
try {
return graphicFactory.createResourceBitmap(inputStream, absoluteName.hashCode());
return graphicFactory.createResourceBitmap(inputStream, displayModel.getScaleFactor(), width, height, percent, hash);
} catch (IOException e) {
throw new IOException("Reading bitmap file failed " + src, e);
}

0 comments on commit 62e0875

Please sign in to comment.