From 55dfcbd0feeccbc56ea4eba28e4e5c8155a690ce Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 3 Mar 2018 23:25:48 +0100 Subject: [PATCH] Improve markers rendering. --- res/marker_mask.bin | Bin 0 -> 35144 bytes .../drawing/components/BasicDrawing.java | 15 +-- .../drawing/overlays/MarkerUtils.java | 126 +++++++----------- 3 files changed, 53 insertions(+), 88 deletions(-) create mode 100644 res/marker_mask.bin diff --git a/res/marker_mask.bin b/res/marker_mask.bin new file mode 100644 index 0000000000000000000000000000000000000000..0e894637db55d8bc71cb5c8d2948f757630766a9 GIT binary patch literal 35144 zcmd5_iBl6v7oY!h|B2Mr?$%aq)mmHbR~`r-DYG9E!75k}N;C-SBJtq(R8~L)QA`ve zihu-=kYrv@*quqznIqjZ{dz8@Dm6WQyfDRM|yB%VtQ_6t*}>r3TrEK(-R|u=}v$7%B1cXn#pbK9BRikO(O*A43PI>{0-ZrDEam`)JoGH~@5)60 zM{oE!u^~U1JEmm__kEm8v1-TR_upLTvO|kW^5-1H8K19c>?qz#o;uOfUw@|iC}XI@4QM^(woGiLqRT@zuB zp_C!cH<9Ao2Z;MA5ac>3aOTn#+1*JlFq(f1fO@oJ_1RCT$OZxxn4=wd-q znn6>)?JSHBUaCKX;|n|ARN09wDO{us8Y{cY8YfgYcP(emCT!2y%yOwF$E0#=6nn=Z z=;{q>;jhy+EX|M8e--5HM(>*Z=_$$-Xm|1q3ytA-W^&iHDC|JkYc~0;E{MnnwADTy z8pFSDRYHua*#?7_(b7yRV{T}Sura&^z1c^n&_fq&{^+SA(u%%?D(y z*BeeE(YuD_`*GbC{1`cQk?o`ioJ1nIjRaJCYzWXgE*0!u5QCFQTtE&(WL<}o#LaTx za99*hA~Af3k|EgDO=dT~q0*?&#Ni|o&rtmceABhEhbTB??RN~G-*GYx4;5t|BhFCm z$G}M>`cdYj+PreMS5eFxcSj4cOyuH-9fxrW$G!{=9W1}D&ojsT~Npe^?i3G+x zst>9Cn8CD`!Q;tb`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1v}nr&pJ6majP z-1?!lFMvu@SjozFyu79SeJZcADk>jqR(^$57o+1q zYs*0Sy~K`#mOPWq@A)N_47n}e2|?wyBvH3ytF7Xq9nRYb+{g%rN#FRuxUE5$s9VHY z$qB1A>z1p0SPFf@wO+Bny6rzBe97~sI%Q-~!b8h-&XifJ!JbCr)bBtCtHqak@Z!YRcsdm#nk zOnpSmiVqrrYt1a!Dbe$!upiWfodv`6dnGs*Bd^RfVY;r1S%IPCxHBs>Ok%g!k`KX8 za@a+h78>O%q4&)cU9U(V4F^FO3n@wWx5ynC*cDQDiteE|MY^C{?s)_m+U;!vhV9mM zSwT|f{1Sv5;#|&XTzL{QQJ3r&97-7=_r<$p zEfth;3J`uHx)fbrqO^UdQj|tICZsO=G>i!TXB9KAKs=w@dLT{c1XUn}Ihl;vY`+yh zo0~ZlaNa%E>s4VlK3#*twhm#@EHF{Z?DvAT#mC(H7zpoJ1gH|5s%rAea-lLD8TWO? z{G9vLQWD`y)K)rH*_T>9kS2Z7gO#tW-%RW1S}n2_CO51jra;Hu8yYy%)Xo9Lca^oE4?B z+2AWDp?B69^;@AX&`|iz%C#8XjawjLB}ND~(ogFGL}J&SDX46tzOwW??sulXv{F71 zqnE*p0b?=M|53ca*qSuXsp>bg29}}UjEp(PNtj#Eav1+26VIX-MPPp$c3+aW`^EV+ jYy6R2OshJqa%Xq<_lu8MA@TT~ztq>47WnUL0d)TY)hWRr literal 0 HcmV?d00001 diff --git a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java index 9ceb0ac..ac79f0e 100644 --- a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java @@ -91,19 +91,20 @@ public class BasicDrawing extends JPanel implements Drawing { private class BasicMarkerOverlay extends BasicOverlay implements MarkerOverlay { - // Default marker width - private static final int DEFAULT_MARKER_WIDTH = 20; - // Point of the marker. private Point point; // Color of the marker. private final Color color; + // Image to draw + private final BufferedImage image; + public BasicMarkerOverlay(Point point, Color color) { super(); this.point = point; this.color = color; + this.image = MarkerUtils.getMarkerForColor(color); } @Override @@ -128,12 +129,8 @@ public class BasicDrawing extends JPanel implements Drawing { int px = BasicDrawing.this.projx(getPoint().getLongitude()); int py = BasicDrawing.this.projy(getPoint().getLatitude()); - BufferedImage img = MarkerUtils.getMarkerForColor(color); - Graphics2D gr = img.createGraphics(); - double scale = DEFAULT_MARKER_WIDTH / (double) img.getHeight(); - gr.scale(scale, scale); - - graphics.drawImage(img, px - img.getWidth() / 2, py - img.getHeight(), BasicDrawing.this); + graphics.drawImage(this.image, px - this.image.getWidth() / 2, py - this.image.getHeight(), + BasicDrawing.this); } }; diff --git a/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java b/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java index a9916bd..42e8a88 100644 --- a/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java +++ b/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java @@ -1,19 +1,15 @@ package org.insa.graphics.drawing.overlays; import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Image; import java.awt.image.BufferedImage; +import java.io.DataInputStream; +import java.io.FileInputStream; public class MarkerUtils { - /** - * Return a color mapping with the given color as the main color. - * - * @param color - * @return - */ - protected static Color[] getColorMapping(Color color) { - return new Color[] { new Color(0, 0, 0, 0), color, Color.BLACK }; - } + private static final String MARKER_MASK_FILE = "res/marker_mask.bin"; /** * Create a Bitmap representing a marker of the given color. @@ -23,83 +19,55 @@ public class MarkerUtils { */ public static BufferedImage getMarkerForColor(Color color) { // create image - BufferedImage image = new BufferedImage(MARKER_MASK[0].length, MARKER_MASK.length, - BufferedImage.TYPE_4BYTE_ABGR); + int[][] mask = readMarkerMask(); + BufferedImage image = new BufferedImage(mask[0].length, mask.length, BufferedImage.TYPE_4BYTE_ABGR); - Color[] map = getColorMapping(color); + // Color[] map = getColorMapping(color); + int rgb = color.getRGB() & 0x00ffffff; for (int i = 0; i < image.getHeight(); ++i) { for (int j = 0; j < image.getWidth(); ++j) { - image.setRGB(j, i, map[MARKER_MASK[i][j]].getRGB()); + // image.setRGB(j, i, map[MARKER_MASK[i][j]].getRGB()); + image.setRGB(j, i, rgb | (mask[i][j] << 24)); } } + BufferedImage scaleImage = new BufferedImage(28, 48, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D graphics = scaleImage.createGraphics(); + graphics.drawImage(image.getScaledInstance(28, 48, Image.SCALE_SMOOTH), 0, 0, null); + graphics.dispose(); + // Create Bitmap and return it. - return image; + return scaleImage; + } + + // Mask cache + private static int[][] MASK_CACHE = null; + + /** + * @return Retrieve the mask from the mask file or from the cache. + */ + private static int[][] readMarkerMask() { + if (MASK_CACHE == null) { + try { + DataInputStream dis = new DataInputStream(new FileInputStream(MARKER_MASK_FILE)); + + int nrows = dis.readInt(); + int ncols = dis.readInt(); + + MASK_CACHE = new int[nrows][ncols]; + for (int i = 0; i < nrows; ++i) { + for (int j = 0; j < ncols; ++j) { + MASK_CACHE[i][j] = dis.readUnsignedByte(); + } + } + dis.close(); + } + catch (Exception e) { + e.printStackTrace(); + MASK_CACHE = null; + } + } + return MASK_CACHE; } - // Mask - private static byte[][] MARKER_MASK = new byte[][] { - // @formatter:off - {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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