From 2d726be5c934a3eceb6c120ad4fde53c199675e5 Mon Sep 17 00:00:00 2001 From: Toke Eskildsen Date: Wed, 31 Aug 2016 21:09:18 +0200 Subject: [PATCH] Preliminary work on issue #24 (Dynamic background changes when zooming) --- .../dk/statsbiblioteket/nrtmosaic/Keeper.java | 25 +++++++++++-------- .../dk/statsbiblioteket/nrtmosaic/Prime.java | 19 +++++++------- .../nrtmosaic/TileProvider.java | 4 +-- .../statsbiblioteket/nrtmosaic/PrimeTest.java | 2 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main/java/dk/statsbiblioteket/nrtmosaic/Keeper.java b/src/main/java/dk/statsbiblioteket/nrtmosaic/Keeper.java index f1fd830..f466ad9 100644 --- a/src/main/java/dk/statsbiblioteket/nrtmosaic/Keeper.java +++ b/src/main/java/dk/statsbiblioteket/nrtmosaic/Keeper.java @@ -247,17 +247,22 @@ public PyramidGrey23 getClosestBottom(int primary, int secondary, Random random) } // Attempts to extract UUID from origin and use cached fill color - // TODO: This should be deprecated due to the dynamic fill option - public int getFillGrey(String origin) { - if (Util.DEFAULT_FILL_STYLE == Util.FILL_STYLE.fixed) { - return Util.FILL_COLOR_INT; - } - try { - return getFillGrey(new UUID(origin)); - } catch (IllegalArgumentException e) { - log.warn("Unable to extract UUID from '" + origin + "'. Using default fill grey " + Util.FILL_COLOR_INT); + public int getFillGrey(String origin, Integer dynamicGrey) { + switch (Util.DEFAULT_FILL_STYLE) { + case fixed: + return Util.FILL_COLOR_INT; + case average: + try { + return getFillGrey(new UUID(origin)); + } catch (IllegalArgumentException e) { + log.warn("Unable to extract UUID from '" + origin + "'. Using default grey " + Util.FILL_COLOR_INT); + return Util.FILL_COLOR_INT; + } + case dynamic: + return dynamicGrey == null ? Util.FILL_COLOR_INT : dynamicGrey; + default: throw new UnsupportedOperationException( + "The fill style '" + Util.DEFAULT_FILL_STYLE + "' is not supported yet"); } - return Util.FILL_COLOR_INT; } /** * Depending on property "tile.fill.style", this either returns the average grey for th pyramid or the default fill. diff --git a/src/main/java/dk/statsbiblioteket/nrtmosaic/Prime.java b/src/main/java/dk/statsbiblioteket/nrtmosaic/Prime.java index 7fe4c33..ca0e242 100644 --- a/src/main/java/dk/statsbiblioteket/nrtmosaic/Prime.java +++ b/src/main/java/dk/statsbiblioteket/nrtmosaic/Prime.java @@ -91,10 +91,10 @@ private Prime() { private static final Pattern DEEPZOOM = Pattern.compile("(.*)/([0-9]+)/([0-9]+)_([0-9]+)(.*)"); public BufferedImage deepzoom(String deepZoomSnippet, String gam, String cnt) throws IOException { - return deepzoom(deepZoomSnippet, gam, cnt, false, false); + return deepzoom(deepZoomSnippet, gam, cnt, false, false, null); } - public BufferedImage deepzoom(String deepZoomSnippet, String gam, String cnt, boolean pad, boolean border) - throws IOException { + public BufferedImage deepzoom(String deepZoomSnippet, String gam, String cnt, boolean pad, boolean border, + Integer dynamicGrey) throws IOException { // /avis-show/symlinks/9/c/0/5/9c05d958-b616-47c1-9e4f-63ec2dd9429e.jp2_files/0/0_0.jpg // Group 1 2 3 45 @@ -123,7 +123,7 @@ public BufferedImage deepzoom(String deepZoomSnippet, String gam, String cnt, bo } else if (level > LAST_BASIC_LEVEL) { result = deepzoomRender(pre, fx, fy, level, post, gam, cnt); } else { - result = deepZoomBasic(deepZoomSnippet, fx, fy, level, gam, cnt, pad); + result = deepZoomBasic(deepZoomSnippet, fx, fy, level, gam, cnt, pad, dynamicGrey); } if (border) { Util.drawBorder(result); @@ -133,7 +133,7 @@ public BufferedImage deepzoom(String deepZoomSnippet, String gam, String cnt, bo // Topmost levels where NRTMosaic works as a plain image server private BufferedImage deepZoomBasic(String deepZoomSnippet, long fx, long fy, int level, String gam, String cnt, - boolean pad) throws IOException { + boolean pad, Integer dynamicGrey) throws IOException { log.trace("deepzoom basic tile for " + deepZoomSnippet + ", pad=" + pad); final long startTime = System.nanoTime(); PyramidGrey23 pyramid = keeper.getPyramid(deepZoomSnippet); @@ -153,7 +153,7 @@ private BufferedImage deepZoomBasic(String deepZoomSnippet, long fx, long fy, in ", maxExistingX=" + maxExistingX);*/ if (fx > maxExistingX || fy > maxExistingY) { log.trace("Basic image has no tile for " + fx + "x" + fy + " at level " + level + ". Returning blank"); - return Util.getBlankTile(keeper.getFillGrey(deepZoomSnippet)); + return Util.getBlankTile(keeper.getFillGrey(deepZoomSnippet, dynamicGrey)); } } @@ -166,11 +166,11 @@ private BufferedImage deepZoomBasic(String deepZoomSnippet, long fx, long fy, in if (!pad) { return unpadded; } - return Util.pad(unpadded, edge, edge, keeper.getFillGrey(deepZoomSnippet)); + return Util.pad(unpadded, edge, edge, keeper.getFillGrey(deepZoomSnippet, dynamicGrey)); } catch (IIOException e) { if (pad) { log.debug("No basic tile at '" + deepZoomSnippet + "' but pad==true so default blank is returned"); - return Util.getBlankTile(keeper.getFillGrey(deepZoomSnippet)); + return Util.getBlankTile(keeper.getFillGrey(deepZoomSnippet, dynamicGrey)); } throw new IIOException("Unable to read '" + external + "' as an image", e); } finally { @@ -282,7 +282,8 @@ private BufferedImage deepzoomRedirect(String pre, long fx, long fy, int level, final String basicSnippet = toBasicDeepzoomSnippet(pyramid, redirectFX, redirectFY, level); log.debug("deepzoom redirect from " + pre + " " + fx + "x" + fy + ", level " + level + " to deepzoom " + basicSnippet + " in " + (System.nanoTime()-startTime)/1000000+"ms"); - return deepzoom(basicSnippet, gam, cnt, true, border); + // TODO: Resolve tile to derive dynamic fill grey and send it forward to the basic + return deepzoom(basicSnippet, gam, cnt, true, border, null); } private String toBasicDeepzoomSnippet(PyramidGrey23 pyramid, long redirectFX, long redirectFY, int level) { diff --git a/src/main/java/dk/statsbiblioteket/nrtmosaic/TileProvider.java b/src/main/java/dk/statsbiblioteket/nrtmosaic/TileProvider.java index e1b1c72..cd9ff45 100644 --- a/src/main/java/dk/statsbiblioteket/nrtmosaic/TileProvider.java +++ b/src/main/java/dk/statsbiblioteket/nrtmosaic/TileProvider.java @@ -80,7 +80,7 @@ public Tile23 getTile(String source, boolean allowNA) { } catch (IOException e) { if (allowNA) { log.debug("No tile at '" + source + "' but allowNA==true so default blank is used"); - image = Util.getBlankTile(keeper.getFillGrey(imageURL.toString())); + image = Util.getBlankTile(keeper.getFillGrey(imageURL.toString(), null)); } else { throw new RuntimeException("Unable to resolve tile for source=" + source); } @@ -89,7 +89,7 @@ public Tile23 getTile(String source, boolean allowNA) { throw new IllegalStateException("image is null for '" + source + "'"); } if (image.getWidth() != edge || image.getHeight() != edge) { - int fillGrey = keeper.getFillGrey(imageURL.toString()); + int fillGrey = keeper.getFillGrey(imageURL.toString(), null); log.trace("Padding tile '" + source + "' of " + image.getWidth() + "x" + image.getHeight() + " pixels to " + edge + "x" + edge + " pixels with fill " + fillGrey); image = Util.pad(image, edge, edge, fillGrey); diff --git a/src/test/java/dk/statsbiblioteket/nrtmosaic/PrimeTest.java b/src/test/java/dk/statsbiblioteket/nrtmosaic/PrimeTest.java index 27ea95d..62de1ab 100644 --- a/src/test/java/dk/statsbiblioteket/nrtmosaic/PrimeTest.java +++ b/src/test/java/dk/statsbiblioteket/nrtmosaic/PrimeTest.java @@ -124,7 +124,7 @@ private void showSome(String BASE, int level, int x, int y) throws IOException { for (int dy = 0; dy < 3; dy++) { for (int dx = 0; dx < 6; dx++) { tiles.add(Prime.instance().deepzoom(String.format( - "%s/%d/%d_%d", BASE, level, x + dx, y + dy), "2.0", "1.2", true, false)); + "%s/%d/%d_%d", BASE, level, x + dx, y + dy), "2.0", "1.2", true, false, null)); } } Util.show(tiles);