diff --git a/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java b/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java index 218ae5e..2da4df4 100644 --- a/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java +++ b/src/main/java/fr/moribus/imageonmap/image/ImageRendererExecutor.java @@ -59,44 +59,34 @@ import java.util.concurrent.Callable; import java.util.concurrent.Future; @WorkerAttributes(name = "Image Renderer", queriesMainThread = true) -public class ImageRendererExecutor extends Worker -{ - private static URLConnection connecting(URL url)throws IOException{ +public class ImageRendererExecutor extends Worker { + private static URLConnection connecting(URL url) throws IOException { final URLConnection connection = url.openConnection(); connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"); connection.connect(); - if (connection instanceof HttpURLConnection) - { + if (connection instanceof HttpURLConnection) { final HttpURLConnection httpConnection = (HttpURLConnection) connection; final int httpCode = httpConnection.getResponseCode(); - if ((httpCode / 100) != 2) - { + if ((httpCode / 100) != 2) { throw new IOException(I.t("HTTP error: {0} {1}", httpCode, httpConnection.getResponseMessage())); } } return connection; } - private enum extension{ - png, jpg, jpeg, gif - } - - static public void render(final URL url, final ImageUtils.ScalingType scaling, final UUID playerUUID, final int width, final int height, WorkerCallback callback) - { - submitQuery(new WorkerRunnable() - { + static public void render(final URL url, final ImageUtils.ScalingType scaling, final UUID playerUUID, final int width, final int height, WorkerCallback callback) { + submitQuery(new WorkerRunnable() { @Override public ImageMap run() throws Throwable { - BufferedImage image=null; + BufferedImage image = null; //If the link is an imgur one if (url.toString().toLowerCase().startsWith("https://imgur.com/")) { //Not handled, can't with the hash only access the image in i.imgur.com/. - if (url.toString().contains("gallery/")) { throw new IOException("We do not support imgur gallery yet, please use direct link to image instead. Right click on the picture you want to use then select copy picture link:) "); } @@ -117,12 +107,10 @@ public class ImageRendererExecutor extends Worker } - } //If not an Imgur link else { - //Try connecting URLConnection connection = connecting(url); @@ -130,8 +118,6 @@ public class ImageRendererExecutor extends Worker image = ImageIO.read(stream); - - } if (image == null) throw new IOException(I.t("The given URL is not a valid image")); // Limits are in place and the player does NOT have rights to avoid them. @@ -148,7 +134,7 @@ public class ImageRendererExecutor extends Worker if (scaling != ImageUtils.ScalingType.NONE && height <= 1 && width <= 1) { - ImageMap ret=renderSingle(scaling.resize(image, ImageMap.WIDTH, ImageMap.HEIGHT), playerUUID); + ImageMap ret = renderSingle(scaling.resize(image, ImageMap.WIDTH, ImageMap.HEIGHT), playerUUID); image.flush();//Safe to free return ret; } @@ -160,26 +146,20 @@ public class ImageRendererExecutor extends Worker }, callback); } - - static private ImageMap renderSingle(final BufferedImage image, final UUID playerUUID) throws Throwable - { + static private ImageMap renderSingle(final BufferedImage image, final UUID playerUUID) throws Throwable { MapManager.checkMapLimit(1, playerUUID); - final Future futureMapID = submitToMainThread(new Callable() - { + final Future futureMapID = submitToMainThread(new Callable() { @Override - public Integer call() throws Exception - { + public Integer call() throws Exception { return MapManager.getNewMapsIds(1)[0]; } }); final int mapID = futureMapID.get(); - ImageIOExecutor.saveImage(mapID,image); - submitToMainThread(new Callable() - { + ImageIOExecutor.saveImage(mapID, image); + submitToMainThread(new Callable() { @Override - public Void call() throws Exception - { + public Void call() throws Exception { Renderer.installRenderer(image, mapID); return null; } @@ -187,16 +167,13 @@ public class ImageRendererExecutor extends Worker return MapManager.createMap(playerUUID, mapID); } - static private ImageMap renderPoster(final BufferedImage image, final UUID playerUUID) throws Throwable - { + static private ImageMap renderPoster(final BufferedImage image, final UUID playerUUID) throws Throwable { final PosterImage poster = new PosterImage(image); final int mapCount = poster.getImagesCount(); MapManager.checkMapLimit(mapCount, playerUUID); - final Future futureMapsIds = submitToMainThread(new Callable() - { + final Future futureMapsIds = submitToMainThread(new Callable() { @Override - public int[] call() throws Exception - { + public int[] call() throws Exception { return MapManager.getNewMapsIds(mapCount); } }); @@ -204,23 +181,24 @@ public class ImageRendererExecutor extends Worker final int[] mapsIDs = futureMapsIds.get(); - ImageIOExecutor.saveImage(mapsIDs,poster); - if (PluginConfiguration.SAVE_FULL_IMAGE.get()) - { + ImageIOExecutor.saveImage(mapsIDs, poster); + if (PluginConfiguration.SAVE_FULL_IMAGE.get()) { ImageIOExecutor.saveImage(ImageMap.getFullImageFile(mapsIDs[0], mapsIDs[mapsIDs.length - 1]), image); } - submitToMainThread(new Callable() - { + submitToMainThread(new Callable() { @Override - public Void call() throws Exception - { + public Void call() throws Exception { Renderer.installRenderer(poster, mapsIDs); return null; } }); - poster.getImage().flush();//Safe? + poster.getImage().flush();//Safe to free return MapManager.createMap(poster, playerUUID, mapsIDs); } + + private enum extension { + png, jpg, jpeg, gif + } } diff --git a/src/main/java/fr/moribus/imageonmap/image/PosterImage.java b/src/main/java/fr/moribus/imageonmap/image/PosterImage.java index 1ddf79d..9ede73e 100644 --- a/src/main/java/fr/moribus/imageonmap/image/PosterImage.java +++ b/src/main/java/fr/moribus/imageonmap/image/PosterImage.java @@ -36,88 +36,84 @@ package fr.moribus.imageonmap.image; -import java.awt.*; + +import java.awt.Graphics; import java.awt.image.BufferedImage; /** * This class represents an image split into pieces */ -public class PosterImage -{ +public class PosterImage { static private final int WIDTH = 128; static private final int HEIGHT = 128; - + private BufferedImage originalImage; private BufferedImage[] cutImages; private int lines; private int columns; private int cutImagesCount; private int remainderX, remainderY; - + /** * Creates a new Poster from an entire image + * * @param originalImage the original image */ - public PosterImage(BufferedImage originalImage) - { + public PosterImage(BufferedImage originalImage) { this.originalImage = originalImage; calculateDimensions(); } - - private void calculateDimensions() - { + + private void calculateDimensions() { int originalWidth = originalImage.getWidth(); int originalHeight = originalImage.getHeight(); - + columns = (int) Math.ceil(originalWidth / WIDTH); lines = (int) Math.ceil(originalHeight / HEIGHT); - + remainderX = originalWidth % WIDTH; remainderY = originalHeight % HEIGHT; - - if(remainderX > 0) columns++; - if(remainderY > 0) lines++; - + + if (remainderX > 0) columns++; + if (remainderY > 0) lines++; + cutImagesCount = columns * lines; } - - public void splitImages() - { - try{ - cutImages = new BufferedImage[cutImagesCount]; - - int imageX; - int imageY = remainderY == 0 ? 0 :(remainderY - HEIGHT) / 2; - for(int i = 0; i < lines; i++) - { - imageX = remainderX == 0 ? 0 :(remainderX - WIDTH) / 2; - for(int j = 0; j < columns; j++) - { - cutImages[i * columns + j] = makeSubImage(originalImage, imageX, imageY); - imageX += WIDTH; + + public void splitImages() { + try { + cutImages = new BufferedImage[cutImagesCount]; + + int imageX; + int imageY = remainderY == 0 ? 0 : (remainderY - HEIGHT) / 2; + for (int i = 0; i < lines; i++) { + imageX = remainderX == 0 ? 0 : (remainderX - WIDTH) / 2; + for (int j = 0; j < columns; j++) { + cutImages[i * columns + j] = makeSubImage(originalImage, imageX, imageY); + imageX += WIDTH; + } + imageY += HEIGHT; } - imageY += HEIGHT; - } - } - catch(final Throwable e){ - if(cutImages!=null) - for(BufferedImage bi: cutImages){ - if(bi!=null){ + } catch (final Throwable e) { + if (cutImages != null) + for (BufferedImage bi : cutImages) { + if (bi != null) { bi.flush();//Safe to free } } - throw e;} + throw e; + } } - + /** * Generates the subimage that intersects with the given map rectangle. + * * @param x X coordinate of top-left point of the map. * @param y Y coordinate of top-left point of the map. * @return the requested subimage. */ - private BufferedImage makeSubImage(BufferedImage originalImage, int x, int y) - { + private BufferedImage makeSubImage(BufferedImage originalImage, int x, int y) { BufferedImage newImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB); @@ -126,79 +122,64 @@ public class PosterImage graphics.drawImage(originalImage, -x, -y, null); return newImage; } - + /** - * * @return the split images */ - public BufferedImage[] getImages() - { + public BufferedImage[] getImages() { return cutImages; } - - public BufferedImage getImageAt(int i) - { + + public BufferedImage getImageAt(int i) { return cutImages[i]; } - public BufferedImage getImage() - { + + public BufferedImage getImage() { return originalImage; } - - public int getColumnAt(int i) - { + + public int getColumnAt(int i) { return i % columns; } - - public int getLineAt(int i) - { + + public int getLineAt(int i) { return i / columns; } - + /** - * * @return the number of lines of the poster */ - public int getLines() - { + public int getLines() { return lines; } - + /** - * * @return the number of columns of the poster */ - public int getColumns() - { + public int getColumns() { return columns; } - + /** - * * @return the number of split images */ - public int getImagesCount() - { + public int getImagesCount() { return cutImagesCount; } - public int getRemainderX() - { + public int getRemainderX() { return remainderX; } - public void setRemainderX(int remainderX) - { + public void setRemainderX(int remainderX) { this.remainderX = remainderX; } - public int getRemainderY() - { + public int getRemainderY() { return remainderY; } - public void setRemainderY(int remainderY) - { + public void setRemainderY(int remainderY) { this.remainderY = remainderY; } }