Skip to content
Permalink
Browse files

Fix hillshading on Android 8+ #1131

  • Loading branch information...
devemux86 committed Sep 28, 2019
1 parent cdc700c commit 6ff0d3f4856195aa327e1100984e20e3835b62f3
@@ -2,6 +2,7 @@

## New since 0.12.0

- Fix hillshading on Android 8+ [#1131](https://github.com/mapsforge/mapsforge/pull/1131)
- Many other minor improvements and bug fixes
- [Solved issues](https://github.com/mapsforge/mapsforge/issues?q=is%3Aclosed+milestone%3A0.13.0)

@@ -62,6 +62,8 @@ void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int srcBot

void setClip(int left, int top, int width, int height);

void setClip(int left, int top, int width, int height, boolean save);

void setClipDifference(int left, int top, int width, int height);

void setFilterBitmap(boolean filter);
@@ -19,21 +19,15 @@
*/
package org.mapsforge.map.android.graphics;

import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.*;
import android.os.Build;

import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Canvas;
import org.mapsforge.core.graphics.Color;
import org.mapsforge.core.graphics.Filter;
import org.mapsforge.core.graphics.Matrix;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Path;
import org.mapsforge.core.graphics.*;
import org.mapsforge.core.model.Dimension;
import org.mapsforge.core.model.Rectangle;

@@ -262,6 +256,7 @@ public boolean isFilterBitmap() {
return this.bitmapPaint.isFilterBitmap();
}

@SuppressWarnings("deprecation")
@Override
public void resetClip() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -285,10 +280,19 @@ public void setBitmap(Bitmap bitmap) {

@Override
public void setClip(int left, int top, int width, int height) {
setClip(left, top, width, height, true);
}

@Override
public void setClip(int left, int top, int width, int height, boolean save) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.canvas.save();
if (save) {
this.canvas.save();
}
this.canvas.clipRect(left, top, left + width, top + height);
this.canvas.restore();
if (save) {
this.canvas.restore();
}
} else {
this.setClipInternal(left, top, width, height, Region.Op.REPLACE);
}
@@ -299,7 +303,8 @@ public void setClipDifference(int left, int top, int width, int height) {
this.setClipInternal(left, top, width, height, Region.Op.DIFFERENCE);
}

public void setClipInternal(int left, int top, int width, int height, Region.Op op) {
@SuppressWarnings("deprecation")
private void setClipInternal(int left, int top, int width, int height, Region.Op op) {
this.canvas.clipRect(left, top, left + width, top + height, op);
}

@@ -308,6 +313,7 @@ public void setFilterBitmap(boolean filter) {
this.bitmapPaint.setFilterBitmap(filter);
}

@SuppressWarnings("deprecation")
@Override
public void shadeBitmap(Bitmap bitmap, Rectangle hillRect, Rectangle tileRect, float magnitude) {
this.canvas.save();
@@ -322,9 +328,9 @@ public void shadeBitmap(Bitmap bitmap, Rectangle hillRect, Rectangle tileRect, f
if (bitmap == null) {
if (tileRect != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.canvas.save();
//this.canvas.save();
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom);
this.canvas.restore();
//this.canvas.restore();
} else {
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom, Region.Op.REPLACE);
}
@@ -343,9 +349,9 @@ public void shadeBitmap(Bitmap bitmap, Rectangle hillRect, Rectangle tileRect, f
if (horizontalScale < 1 && verticalScale < 1) {
// fast path for wide zoom (downscaling)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.canvas.save();
//this.canvas.save();
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom);
this.canvas.restore();
//this.canvas.restore();
} else {
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom, Region.Op.REPLACE);
}
@@ -508,16 +514,16 @@ Rect useAdr(int destLeft, int destTop, int destRight, int destBottom) {
return tmpBitmap;
}

public android.graphics.Paint useAlphaPaint(int alpha) {
android.graphics.Paint useAlphaPaint(int alpha) {
shadePaint.setAlpha(alpha);
return shadePaint;
}

public android.graphics.Bitmap useNeutralShadingPixel() {
android.graphics.Bitmap useNeutralShadingPixel() {
return neutralShadingPixel;
}

public android.graphics.Matrix useMatrix() {
android.graphics.Matrix useMatrix() {
if (tmpMatrix == null) {
tmpMatrix = new android.graphics.Matrix();
}
@@ -400,6 +400,11 @@ public void setBitmap(Bitmap bitmap) {

@Override
public void setClip(int left, int top, int width, int height) {
setClip(left, top, width, height, true);
}

@Override
public void setClip(int left, int top, int width, int height, boolean save) {
this.graphics2D.setClip(left, top, width, height);

This comment has been minimized.

Copy link
@Sublimis

Sublimis Sep 28, 2019

You forgot to pass the save parameter here

This comment has been minimized.

Copy link
@devemux86

devemux86 Sep 28, 2019

Author Collaborator

Canvas save / restore is used on Android.
Desktop platform does not have a use.

}

@@ -465,7 +470,7 @@ private void fillColor(java.awt.Color color) {
this.graphics2D.setComposite(originalComposite);
}

public void setColorAndStroke(AwtPaint awtPaint) {
void setColorAndStroke(AwtPaint awtPaint) {
this.graphics2D.setColor(awtPaint.color);
if (awtPaint.stroke != null) {
this.graphics2D.setStroke(awtPaint.stroke);
@@ -1,5 +1,6 @@
/*
* Copyright 2017 usrusr
* Copyright 2019 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
@@ -21,12 +22,7 @@

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
@@ -80,7 +76,7 @@ public int hashCode() {
}
}

class Lru {
private static class Lru {
public int getSize() {
return size;
}
@@ -92,7 +88,6 @@ public void setSize(int size) {
if (size < lru.size()) synchronized (lru) {
Iterator<Future<HillshadingBitmap>> iterator = lru.iterator();
while (lru.size() > size) {
Future<HillshadingBitmap> evicted = iterator.next();
iterator.remove();
}
}
@@ -151,7 +146,7 @@ void evict(Future<HillshadingBitmap> loadingFuture) {

hgtFiles = new LazyFuture<Map<TileKey, HgtFileInfo>>() {
@Override
protected Map<TileKey, HgtFileInfo> calculate() throws ExecutionException, InterruptedException {
protected Map<TileKey, HgtFileInfo> calculate() {
Map<TileKey, HgtFileInfo> map = new HashMap<>();
Matcher matcher = Pattern.compile("([ns])(\\d{1,2})([ew])(\\d{1,3})\\.hgt", Pattern.CASE_INSENSITIVE).matcher("");
crawl(HgtCache.this.demFolder, matcher, map, problems);
@@ -437,7 +432,7 @@ public String toString() {
}


public HillshadingBitmap getHillshadingBitmap(int northInt, int eastInt, double pxPerLat, double pxPerLng) throws InterruptedException, ExecutionException {
HillshadingBitmap getHillshadingBitmap(int northInt, int eastInt, double pxPerLat, double pxPerLng) throws InterruptedException, ExecutionException {
HgtFileInfo hgtFileInfo = hgtFiles.get().get(new TileKey(northInt, eastInt));
if (hgtFileInfo == null)
return null;
@@ -454,25 +449,25 @@ static void mergeSameSized(HillshadingBitmap center, HillshadingBitmap neighbor,
sink = center;
source = neighbor;
copyCanvas.setBitmap(sink);
copyCanvas.setClip(sink.getWidth() - padding, padding, padding, sink.getHeight() - 2 * padding);
copyCanvas.setClip(sink.getWidth() - padding, padding, padding, sink.getHeight() - 2 * padding, false);
copyCanvas.drawBitmap(source, (source.getWidth() - 2 * padding), 0);
} else if (border == HillshadingBitmap.Border.WEST) {
sink = center;
source = neighbor;
copyCanvas.setBitmap(sink);
copyCanvas.setClip(0, padding, padding, sink.getHeight() - 2 * padding);
copyCanvas.setClip(0, padding, padding, sink.getHeight() - 2 * padding, false);
copyCanvas.drawBitmap(source, 2 * padding - (source.getWidth()), 0);
} else if (border == HillshadingBitmap.Border.NORTH) {
sink = center;
source = neighbor;
copyCanvas.setBitmap(sink);
copyCanvas.setClip(padding, 0, sink.getWidth() - 2 * padding, padding);
copyCanvas.setClip(padding, 0, sink.getWidth() - 2 * padding, padding, false);
copyCanvas.drawBitmap(source, 0, 2 * padding - (source.getHeight()));
} else if (border == HillshadingBitmap.Border.SOUTH) {
sink = center;
source = neighbor;
copyCanvas.setBitmap(sink);
copyCanvas.setClip(padding, sink.getHeight() - padding, sink.getWidth() - 2 * padding, padding);
copyCanvas.setClip(padding, sink.getHeight() - padding, sink.getWidth() - 2 * padding, padding, false);
copyCanvas.drawBitmap(source, 0, (source.getHeight() - 2 * padding));
}
}

0 comments on commit 6ff0d3f

Please sign in to comment.
You can’t perform that action at this time.