From a530070ecf91cd3189ed78d42324e723b2c92468 Mon Sep 17 00:00:00 2001 From: Emux Date: Thu, 11 Oct 2018 10:56:58 +0300 Subject: [PATCH] Render themes: PNG scaling, fix #595 --- docs/Changelog.md | 1 + .../oscim/android/canvas/AndroidBitmap.java | 25 +++++++- .../oscim/android/canvas/AndroidGraphics.java | 7 ++- .../android/canvas/AndroidSvgBitmap.java | 33 ++-------- vtm-desktop/src/org/oscim/awt/AwtBitmap.java | 59 ++++++++++------- .../src/org/oscim/awt/AwtGraphics.java | 7 ++- .../src/org/oscim/awt/AwtSvgBitmap.java | 27 +------- .../src/org/oscim/ios/backend/IosBitmap.java | 22 ++++++- .../org/oscim/ios/backend/IosGraphics.java | 7 ++- .../org/oscim/ios/backend/IosSvgBitmap.java | 29 ++------- .../src/org/oscim/gdx/client/GwtBitmap.java | 7 ++- .../org/oscim/gdx/client/GwtGdxGraphics.java | 8 ++- vtm/src/org/oscim/backend/CanvasAdapter.java | 17 ++++- vtm/src/org/oscim/backend/canvas/Bitmap.java | 3 + vtm/src/org/oscim/utils/GraphicUtils.java | 63 +++++++++++++++++++ 15 files changed, 207 insertions(+), 108 deletions(-) create mode 100644 vtm/src/org/oscim/utils/GraphicUtils.java diff --git a/docs/Changelog.md b/docs/Changelog.md index 274611a2..fdf30108 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -3,6 +3,7 @@ ## New since 0.10.0 - Render themes: tag transform [#420](https://github.com/mapsforge/vtm/issues/420) +- Render themes: PNG scaling [#595](https://github.com/mapsforge/vtm/issues/595) - PathLayer(s) scaled width [#594](https://github.com/mapsforge/vtm/issues/594) - vtm-models module [#580](https://github.com/mapsforge/vtm/issues/580) - Many other minor improvements and bug fixes diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidBitmap.java b/vtm-android/src/org/oscim/android/canvas/AndroidBitmap.java index 6b7b9b48..f24f70f3 100644 --- a/vtm-android/src/org/oscim/android/canvas/AndroidBitmap.java +++ b/vtm-android/src/org/oscim/android/canvas/AndroidBitmap.java @@ -1,7 +1,7 @@ /* * Copyright 2013 Hannes Janetzek * Copyright 2016 Longri - * Copyright 2016 devemux86 + * Copyright 2016-2018 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -23,6 +23,8 @@ import android.graphics.BitmapFactory; import android.opengl.GLES20; import android.opengl.GLUtils; +import org.oscim.backend.CanvasAdapter; +import org.oscim.utils.GraphicUtils; import org.oscim.utils.IOUtils; import java.io.ByteArrayOutputStream; @@ -31,7 +33,7 @@ import java.io.InputStream; import static android.graphics.Bitmap.Config.ARGB_8888; public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap { - final Bitmap mBitmap; + Bitmap mBitmap; public AndroidBitmap(InputStream inputStream) { Bitmap bitmap = BitmapFactory.decodeStream(inputStream); @@ -43,6 +45,12 @@ public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap { mBitmap = bitmap; } + public AndroidBitmap(InputStream inputStream, int width, int height, int percent) { + this(inputStream); + float[] newSize = GraphicUtils.imageSize(getWidth(), getHeight(), CanvasAdapter.getScale(), width, height, percent); + scaleTo((int) newSize[0], (int) newSize[1]); + } + @Override public boolean isValid() { return mBitmap != null; @@ -117,4 +125,17 @@ public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap { IOUtils.closeQuietly(outputStream); } } + + @Override + public void scaleTo(int width, int height) { + if (getWidth() != width || getHeight() != height) { + // The effect of the filter argument to createScaledBitmap is not well documented in the + // official android docs, but according to + // http://stackoverflow.com/questions/2895065/what-does-the-filter-parameter-to-createscaledbitmap-do + // passing true results in smoother edges, less pixelation. + // If smoother corners improve the readability of map labels is perhaps debatable. + android.graphics.Bitmap scaledBitmap = android.graphics.Bitmap.createScaledBitmap(mBitmap, width, height, true); + mBitmap = scaledBitmap; + } + } } diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java b/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java index c97bbf52..bf589131 100644 --- a/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java +++ b/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java @@ -1,6 +1,6 @@ /* * Copyright 2010, 2011, 2012 mapsforge.org - * Copyright 2016-2017 devemux86 + * Copyright 2016-2018 devemux86 * Copyright 2017 Longri * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). @@ -58,6 +58,11 @@ public final class AndroidGraphics extends CanvasAdapter { return new AndroidBitmap(inputStream); } + @Override + public Bitmap decodeBitmapImpl(InputStream inputStream, int width, int height, int percent) { + return new AndroidBitmap(inputStream, width, height, percent); + } + @Override public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { return new AndroidSvgBitmap(inputStream, width, height, percent); diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java b/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java index 1dcbf287..26ff56dd 100644 --- a/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java +++ b/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java @@ -1,7 +1,7 @@ /* * Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2013-2014 Ludwig M Brinckmann - * Copyright 2014-2017 devemux86 + * Copyright 2014-2018 devemux86 * * 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 +24,7 @@ import android.graphics.RectF; import com.caverock.androidsvg.SVG; import org.oscim.backend.CanvasAdapter; +import org.oscim.utils.GraphicUtils; import java.io.IOException; import java.io.InputStream; @@ -41,34 +42,12 @@ public class AndroidSvgBitmap extends AndroidBitmap { double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize); - float bitmapWidth = (float) (picture.getWidth() * scale); - float bitmapHeight = (float) (picture.getHeight() * scale); + float[] bmpSize = GraphicUtils.imageSize(picture.getWidth(), picture.getHeight(), (float) scale, width, height, percent); - 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; - } - - android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth), - (int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888); + android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bmpSize[0]), + (int) Math.ceil(bmpSize[1]), Bitmap.Config.ARGB_8888); 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) { diff --git a/vtm-desktop/src/org/oscim/awt/AwtBitmap.java b/vtm-desktop/src/org/oscim/awt/AwtBitmap.java index 1a2554a2..15acd768 100644 --- a/vtm-desktop/src/org/oscim/awt/AwtBitmap.java +++ b/vtm-desktop/src/org/oscim/awt/AwtBitmap.java @@ -1,6 +1,6 @@ /* * Copyright 2013 Hannes Janetzek - * Copyright 2016-2017 devemux86 + * Copyright 2016-2018 devemux86 * Copyright 2016-2017 Longri * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). @@ -21,13 +21,18 @@ package org.oscim.awt; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.utils.BufferUtils; +import org.oscim.backend.CanvasAdapter; import org.oscim.backend.GL; import org.oscim.backend.canvas.Bitmap; import org.oscim.renderer.bucket.TextureBucket; +import org.oscim.utils.GraphicUtils; import org.oscim.utils.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.awt.AlphaComposite; +import java.awt.Graphics2D; +import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; @@ -41,17 +46,10 @@ public class AwtBitmap implements Bitmap { private static final Logger log = LoggerFactory.getLogger(AwtBitmap.class); BufferedImage bitmap; - int width; - int height; - - boolean internal; public AwtBitmap(int width, int height, int format) { bitmap = new BufferedImage(width, height, format != 0 ? format : BufferedImage.TYPE_INT_ARGB); - this.width = width; - this.height = height; - internal = true; // if (!this.bitmap.isAlphaPremultiplied()) // this.bitmap.coerceData(true); } @@ -59,17 +57,19 @@ public class AwtBitmap implements Bitmap { AwtBitmap(InputStream inputStream) throws IOException { this.bitmap = ImageIO.read(inputStream); - this.width = this.bitmap.getWidth(); - this.height = this.bitmap.getHeight(); if (!this.bitmap.isAlphaPremultiplied() && this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB) this.bitmap.coerceData(true); } + AwtBitmap(InputStream inputStream, int width, int height, int percent) throws IOException { + this(inputStream); + float[] newSize = GraphicUtils.imageSize(getWidth(), getHeight(), CanvasAdapter.getScale(), width, height, percent); + scaleTo((int) newSize[0], (int) newSize[1]); + } + public AwtBitmap(BufferedImage bitmap) { this.bitmap = bitmap; - this.width = this.bitmap.getWidth(); - this.height = this.bitmap.getHeight(); if (!this.bitmap.isAlphaPremultiplied() && this.bitmap.getType() == BufferedImage.TYPE_INT_ARGB) this.bitmap.coerceData(true); @@ -77,12 +77,12 @@ public class AwtBitmap implements Bitmap { @Override public int getWidth() { - return width; + return bitmap.getWidth(); } @Override public int getHeight() { - return height; + return bitmap.getHeight(); } @Override @@ -108,17 +108,17 @@ public class AwtBitmap implements Bitmap { int[] pixels; IntBuffer buffer; - if (width * height < TextureBucket.TEXTURE_HEIGHT * TextureBucket.TEXTURE_WIDTH) { + if (bitmap.getWidth() * bitmap.getHeight() < TextureBucket.TEXTURE_HEIGHT * TextureBucket.TEXTURE_WIDTH) { pixels = tmpPixel; buffer = tmpBuffer; buffer.clear(); } else { - pixels = new int[width * height]; - buffer = BufferUtils.newIntBuffer(width * height); + pixels = new int[bitmap.getWidth() * bitmap.getHeight()]; + buffer = BufferUtils.newIntBuffer(bitmap.getWidth() * bitmap.getHeight()); } // FIXME dont convert to argb when there data is greyscale - bitmap.getRGB(0, 0, width, height, pixels, 0, width); + bitmap.getRGB(0, 0, bitmap.getWidth(), bitmap.getHeight(), pixels, 0, bitmap.getWidth()); if (WRITE_TEX) { try { @@ -131,7 +131,7 @@ public class AwtBitmap implements Bitmap { } } - for (int i = 0, n = width * height; i < n; i++) { + for (int i = 0, n = bitmap.getWidth() * bitmap.getHeight(); i < n; i++) { int c = pixels[i]; if (c == 0) continue; @@ -143,11 +143,11 @@ public class AwtBitmap implements Bitmap { pixels[i] = (c & 0xff000000) | r << 16 | g << 8 | b; } - buffer.put(pixels, 0, width * height); + buffer.put(pixels, 0, bitmap.getWidth() * bitmap.getHeight()); buffer.flip(); - Gdx.gl20.glTexImage2D(GL.TEXTURE_2D, 0, GL.RGBA, width, - height, 0, GL.RGBA, GL.UNSIGNED_BYTE, buffer); + Gdx.gl20.glTexImage2D(GL.TEXTURE_2D, 0, GL.RGBA, bitmap.getWidth(), + bitmap.getHeight(), 0, GL.RGBA, GL.UNSIGNED_BYTE, buffer); } @Override @@ -173,4 +173,19 @@ public class AwtBitmap implements Bitmap { } return null; } + + @Override + public void scaleTo(int width, int height) { + if (getWidth() != width || getHeight() != height) { + BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics2D graphics = resizedImage.createGraphics(); + graphics.setComposite(AlphaComposite.Src); + graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + graphics.drawImage(bitmap, 0, 0, width, height, null); + graphics.dispose(); + bitmap = resizedImage; + } + } } diff --git a/vtm-desktop/src/org/oscim/awt/AwtGraphics.java b/vtm-desktop/src/org/oscim/awt/AwtGraphics.java index 5065aa38..54aa6da5 100644 --- a/vtm-desktop/src/org/oscim/awt/AwtGraphics.java +++ b/vtm-desktop/src/org/oscim/awt/AwtGraphics.java @@ -1,6 +1,6 @@ /* * Copyright 2013 Hannes Janetzek - * Copyright 2016 devemux86 + * Copyright 2016-2018 devemux86 * Copyright 2017 Longri * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). @@ -108,6 +108,11 @@ public class AwtGraphics extends CanvasAdapter { return new AwtBitmap(inputStream); } + @Override + public Bitmap decodeBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { + return new AwtBitmap(inputStream, width, height, percent); + } + @Override public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { return new AwtSvgBitmap(inputStream, width, height, percent); diff --git a/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java b/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java index afc0ea93..75e95b8f 100644 --- a/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java +++ b/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java @@ -19,6 +19,7 @@ import com.kitfox.svg.SVGDiagram; import com.kitfox.svg.app.beans.SVGIcon; import org.oscim.backend.CanvasAdapter; +import org.oscim.utils.GraphicUtils; import java.awt.Dimension; import java.awt.image.BufferedImage; @@ -39,34 +40,12 @@ public class AwtSvgBitmap extends AwtBitmap { 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); diff --git a/vtm-ios/src/org/oscim/ios/backend/IosBitmap.java b/vtm-ios/src/org/oscim/ios/backend/IosBitmap.java index 7af27097..67acf5c8 100644 --- a/vtm-ios/src/org/oscim/ios/backend/IosBitmap.java +++ b/vtm-ios/src/org/oscim/ios/backend/IosBitmap.java @@ -1,5 +1,6 @@ /* * Copyright 2016-2017 Longri + * Copyright 2018 devemux86 * * 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 @@ -82,13 +83,25 @@ public class IosBitmap implements Bitmap { CGImage image = new UIImage(data).getCGImage(); this.width = (int) image.getWidth(); this.height = (int) image.getHeight(); - this.cgBitmapContext = CGBitmapContext.create(width, height, 8, 4 * width, + this.cgBitmapContext = CGBitmapContext.create(this.width, this.height, 8, 4 * this.width, CGColorSpace.createDeviceRGB(), CGImageAlphaInfo.PremultipliedLast); - this.cgBitmapContext.drawImage(new CGRect(0, 0, width, height), image); + this.cgBitmapContext.drawImage(new CGRect(0, 0, this.width, this.height), image); image.dispose(); } + /** + * Constructor
+ * Create a IosBitmap from given InputStream + * + * @param inputStream + * @throws IOException + */ + public IosBitmap(InputStream inputStream, int width, int height, int percent) throws IOException { + // TODO Scaling + this(inputStream); + } + /** * Constructor
* Load a IosBitmap from Asset with given FilePath @@ -256,4 +269,9 @@ public class IosBitmap implements Bitmap { NSData data = uiImage.toPNGData(); return data.getBytes(); } + + @Override + public void scaleTo(int width, int height) { + // TODO + } } diff --git a/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java b/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java index 52f3308e..253fe649 100644 --- a/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java +++ b/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java @@ -1,6 +1,6 @@ /* * Copyright 2016 Longri - * Copyright 2016 devemux86 + * Copyright 2016-2018 devemux86 * Copyright 2017 Longri * * This program is free software: you can redistribute it and/or modify it under the @@ -59,6 +59,11 @@ public class IosGraphics extends CanvasAdapter { return new IosBitmap(inputStream); } + @Override + protected Bitmap decodeBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { + return new IosBitmap(inputStream, width, height, percent); + } + @Override protected Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { return new IosSvgBitmap(inputStream, width, height, percent); diff --git a/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java b/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java index 760f0281..cd01d007 100644 --- a/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java +++ b/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java @@ -1,6 +1,6 @@ /* * Copyright 2016 Longri - * Copyright 2016-2017 devemux86 + * Copyright 2016-2018 devemux86 * * 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 @@ -16,6 +16,7 @@ package org.oscim.ios.backend; import org.oscim.backend.CanvasAdapter; +import org.oscim.utils.GraphicUtils; import org.oscim.utils.IOUtils; import org.robovm.apple.coregraphics.CGRect; import org.robovm.apple.coregraphics.CGSize; @@ -62,31 +63,9 @@ public class IosSvgBitmap extends IosBitmap { double scale = scaleFactor / Math.sqrt((viewRect.getHeight() * viewRect.getWidth()) / defaultSize); - float bitmapWidth = (float) (viewRect.getWidth() * scale); - float bitmapHeight = (float) (viewRect.getHeight() * scale); + float[] bmpSize = GraphicUtils.imageSize((float) viewRect.getWidth(), (float) viewRect.getHeight(), (float) scale, width, height, percent); - float aspectRatio = (float) (viewRect.getWidth() / viewRect.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; - } - - return renderer.asImageWithSize(new CGSize(bitmapWidth, bitmapHeight), 1); + return renderer.asImageWithSize(new CGSize(bmpSize[0], bmpSize[1]), 1); } private static UIImage getResourceBitmapImpl(InputStream inputStream, int width, int height, int percent) { diff --git a/vtm-web/src/org/oscim/gdx/client/GwtBitmap.java b/vtm-web/src/org/oscim/gdx/client/GwtBitmap.java index 7a99b46f..7cff375f 100644 --- a/vtm-web/src/org/oscim/gdx/client/GwtBitmap.java +++ b/vtm-web/src/org/oscim/gdx/client/GwtBitmap.java @@ -1,6 +1,6 @@ /* * Copyright 2013 Hannes Janetzek - * Copyright 2016 devemux86 + * Copyright 2016-2018 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -105,4 +105,9 @@ public class GwtBitmap implements Bitmap { // TODO return null; } + + @Override + public void scaleTo(int width, int height) { + // TODO + } } diff --git a/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java b/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java index 69824073..270b3419 100644 --- a/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java +++ b/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java @@ -1,6 +1,6 @@ /* * Copyright 2013 Hannes Janetzek - * Copyright 2016 devemux86 + * Copyright 2016-2018 devemux86 * Copyright 2017 Longri * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). @@ -55,6 +55,12 @@ public class GwtGdxGraphics extends CanvasAdapter { return null; } + @Override + public Bitmap decodeBitmapImpl(InputStream inputStream, int width, int height, int percent) { + // TODO + return null; + } + @Override public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) { // TODO diff --git a/vtm/src/org/oscim/backend/CanvasAdapter.java b/vtm/src/org/oscim/backend/CanvasAdapter.java index 5d800c50..05814757 100644 --- a/vtm/src/org/oscim/backend/CanvasAdapter.java +++ b/vtm/src/org/oscim/backend/CanvasAdapter.java @@ -113,10 +113,25 @@ public abstract class CanvasAdapter { */ protected abstract Bitmap decodeBitmapImpl(InputStream inputStream) throws IOException; + /** + * Create {@link Bitmap} from InputStream. + * + * @param inputStream the input stream + * @param width requested width (0: no change) + * @param height requested height (0: no change) + * @param percent requested scale percent (100: no change) + * @return the bitmap + */ + protected abstract Bitmap decodeBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException; + public static Bitmap decodeBitmap(InputStream inputStream) throws IOException { return g.decodeBitmapImpl(inputStream); } + public static Bitmap decodeBitmap(InputStream inputStream, int width, int height, int percent) throws IOException { + return g.decodeBitmapImpl(inputStream, width, height, percent); + } + /** * Create SVG {@link Bitmap} from InputStream. * @@ -182,7 +197,7 @@ public abstract class CanvasAdapter { if (src.toLowerCase(Locale.ENGLISH).endsWith(".svg")) bitmap = decodeSvgBitmap(inputStream, width, height, percent); else - bitmap = decodeBitmap(inputStream); + bitmap = decodeBitmap(inputStream, width, height, percent); inputStream.close(); return bitmap; } diff --git a/vtm/src/org/oscim/backend/canvas/Bitmap.java b/vtm/src/org/oscim/backend/canvas/Bitmap.java index 59435726..ec1c883e 100644 --- a/vtm/src/org/oscim/backend/canvas/Bitmap.java +++ b/vtm/src/org/oscim/backend/canvas/Bitmap.java @@ -1,6 +1,7 @@ /* * Copyright 2013 Hannes Janetzek * Copyright 2016 Longri + * Copyright 2018 devemux86 * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -65,4 +66,6 @@ public interface Bitmap { boolean isValid(); byte[] getPngEncodedData(); + + void scaleTo(int width, int height); } diff --git a/vtm/src/org/oscim/utils/GraphicUtils.java b/vtm/src/org/oscim/utils/GraphicUtils.java new file mode 100644 index 00000000..e4eb47cd --- /dev/null +++ b/vtm/src/org/oscim/utils/GraphicUtils.java @@ -0,0 +1,63 @@ +/* + * Copyright 2018 devemux86 + * + * 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 + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see . + */ +package org.oscim.utils; + +/** + * Utility class for graphics operations. + */ +public final class GraphicUtils { + + /** + * 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() { + } +}