fixed indent

This commit is contained in:
Vlammar 2020-08-20 23:55:16 +02:00
parent 06cdb97be2
commit f4cfe934a7
2 changed files with 84 additions and 125 deletions

View File

@ -59,44 +59,34 @@ import java.util.concurrent.Callable;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@WorkerAttributes(name = "Image Renderer", queriesMainThread = true) @WorkerAttributes(name = "Image Renderer", queriesMainThread = true)
public class ImageRendererExecutor extends Worker public class ImageRendererExecutor extends Worker {
{ private static URLConnection connecting(URL url) throws IOException {
private static URLConnection connecting(URL url)throws IOException{
final URLConnection connection = url.openConnection(); 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.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
connection.connect(); connection.connect();
if (connection instanceof HttpURLConnection) if (connection instanceof HttpURLConnection) {
{
final HttpURLConnection httpConnection = (HttpURLConnection) connection; final HttpURLConnection httpConnection = (HttpURLConnection) connection;
final int httpCode = httpConnection.getResponseCode(); 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())); throw new IOException(I.t("HTTP error: {0} {1}", httpCode, httpConnection.getResponseMessage()));
} }
} }
return connection; 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<ImageMap> callback) {
static public void render(final URL url, final ImageUtils.ScalingType scaling, final UUID playerUUID, final int width, final int height, WorkerCallback<ImageMap> callback) submitQuery(new WorkerRunnable<ImageMap>() {
{
submitQuery(new WorkerRunnable<ImageMap>()
{
@Override @Override
public ImageMap run() throws Throwable { public ImageMap run() throws Throwable {
BufferedImage image=null; BufferedImage image = null;
//If the link is an imgur one //If the link is an imgur one
if (url.toString().toLowerCase().startsWith("https://imgur.com/")) { if (url.toString().toLowerCase().startsWith("https://imgur.com/")) {
//Not handled, can't with the hash only access the image in i.imgur.com/<hash>.<extension> //Not handled, can't with the hash only access the image in i.imgur.com/<hash>.<extension>
if (url.toString().contains("gallery/")) { 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:) "); 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 //If not an Imgur link
else { else {
//Try connecting //Try connecting
URLConnection connection = connecting(url); URLConnection connection = connecting(url);
@ -130,8 +118,6 @@ public class ImageRendererExecutor extends Worker
image = ImageIO.read(stream); image = ImageIO.read(stream);
} }
if (image == null) throw new IOException(I.t("The given URL is not a valid image")); 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. // 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) { 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 image.flush();//Safe to free
return ret; return ret;
} }
@ -160,26 +146,20 @@ public class ImageRendererExecutor extends Worker
}, callback); }, 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); MapManager.checkMapLimit(1, playerUUID);
final Future<Integer> futureMapID = submitToMainThread(new Callable<Integer>() final Future<Integer> futureMapID = submitToMainThread(new Callable<Integer>() {
{
@Override @Override
public Integer call() throws Exception public Integer call() throws Exception {
{
return MapManager.getNewMapsIds(1)[0]; return MapManager.getNewMapsIds(1)[0];
} }
}); });
final int mapID = futureMapID.get(); final int mapID = futureMapID.get();
ImageIOExecutor.saveImage(mapID,image); ImageIOExecutor.saveImage(mapID, image);
submitToMainThread(new Callable<Void>() submitToMainThread(new Callable<Void>() {
{
@Override @Override
public Void call() throws Exception public Void call() throws Exception {
{
Renderer.installRenderer(image, mapID); Renderer.installRenderer(image, mapID);
return null; return null;
} }
@ -187,16 +167,13 @@ public class ImageRendererExecutor extends Worker
return MapManager.createMap(playerUUID, mapID); 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 PosterImage poster = new PosterImage(image);
final int mapCount = poster.getImagesCount(); final int mapCount = poster.getImagesCount();
MapManager.checkMapLimit(mapCount, playerUUID); MapManager.checkMapLimit(mapCount, playerUUID);
final Future<int[]> futureMapsIds = submitToMainThread(new Callable<int[]>() final Future<int[]> futureMapsIds = submitToMainThread(new Callable<int[]>() {
{
@Override @Override
public int[] call() throws Exception public int[] call() throws Exception {
{
return MapManager.getNewMapsIds(mapCount); return MapManager.getNewMapsIds(mapCount);
} }
}); });
@ -204,23 +181,24 @@ public class ImageRendererExecutor extends Worker
final int[] mapsIDs = futureMapsIds.get(); final int[] mapsIDs = futureMapsIds.get();
ImageIOExecutor.saveImage(mapsIDs,poster); ImageIOExecutor.saveImage(mapsIDs, poster);
if (PluginConfiguration.SAVE_FULL_IMAGE.get()) if (PluginConfiguration.SAVE_FULL_IMAGE.get()) {
{
ImageIOExecutor.saveImage(ImageMap.getFullImageFile(mapsIDs[0], mapsIDs[mapsIDs.length - 1]), image); ImageIOExecutor.saveImage(ImageMap.getFullImageFile(mapsIDs[0], mapsIDs[mapsIDs.length - 1]), image);
} }
submitToMainThread(new Callable<Void>() submitToMainThread(new Callable<Void>() {
{
@Override @Override
public Void call() throws Exception public Void call() throws Exception {
{
Renderer.installRenderer(poster, mapsIDs); Renderer.installRenderer(poster, mapsIDs);
return null; return null;
} }
}); });
poster.getImage().flush();//Safe? poster.getImage().flush();//Safe to free
return MapManager.createMap(poster, playerUUID, mapsIDs); return MapManager.createMap(poster, playerUUID, mapsIDs);
} }
private enum extension {
png, jpg, jpeg, gif
}
} }

View File

@ -36,88 +36,84 @@
package fr.moribus.imageonmap.image; package fr.moribus.imageonmap.image;
import java.awt.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
/** /**
* This class represents an image split into pieces * This class represents an image split into pieces
*/ */
public class PosterImage public class PosterImage {
{
static private final int WIDTH = 128; static private final int WIDTH = 128;
static private final int HEIGHT = 128; static private final int HEIGHT = 128;
private BufferedImage originalImage; private BufferedImage originalImage;
private BufferedImage[] cutImages; private BufferedImage[] cutImages;
private int lines; private int lines;
private int columns; private int columns;
private int cutImagesCount; private int cutImagesCount;
private int remainderX, remainderY; private int remainderX, remainderY;
/** /**
* Creates a new Poster from an entire image * Creates a new Poster from an entire image
*
* @param originalImage the original image * @param originalImage the original image
*/ */
public PosterImage(BufferedImage originalImage) public PosterImage(BufferedImage originalImage) {
{
this.originalImage = originalImage; this.originalImage = originalImage;
calculateDimensions(); calculateDimensions();
} }
private void calculateDimensions() private void calculateDimensions() {
{
int originalWidth = originalImage.getWidth(); int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight(); int originalHeight = originalImage.getHeight();
columns = (int) Math.ceil(originalWidth / WIDTH); columns = (int) Math.ceil(originalWidth / WIDTH);
lines = (int) Math.ceil(originalHeight / HEIGHT); lines = (int) Math.ceil(originalHeight / HEIGHT);
remainderX = originalWidth % WIDTH; remainderX = originalWidth % WIDTH;
remainderY = originalHeight % HEIGHT; remainderY = originalHeight % HEIGHT;
if(remainderX > 0) columns++; if (remainderX > 0) columns++;
if(remainderY > 0) lines++; if (remainderY > 0) lines++;
cutImagesCount = columns * lines; cutImagesCount = columns * lines;
} }
public void splitImages() public void splitImages() {
{ try {
try{ cutImages = new BufferedImage[cutImagesCount];
cutImages = new BufferedImage[cutImagesCount];
int imageX;
int imageX; int imageY = remainderY == 0 ? 0 : (remainderY - HEIGHT) / 2;
int imageY = remainderY == 0 ? 0 :(remainderY - HEIGHT) / 2; for (int i = 0; i < lines; i++) {
for(int i = 0; i < lines; i++) imageX = remainderX == 0 ? 0 : (remainderX - WIDTH) / 2;
{ for (int j = 0; j < columns; j++) {
imageX = remainderX == 0 ? 0 :(remainderX - WIDTH) / 2; cutImages[i * columns + j] = makeSubImage(originalImage, imageX, imageY);
for(int j = 0; j < columns; j++) imageX += WIDTH;
{ }
cutImages[i * columns + j] = makeSubImage(originalImage, imageX, imageY); imageY += HEIGHT;
imageX += WIDTH;
} }
imageY += HEIGHT; } catch (final Throwable e) {
} if (cutImages != null)
} for (BufferedImage bi : cutImages) {
catch(final Throwable e){ if (bi != null) {
if(cutImages!=null)
for(BufferedImage bi: cutImages){
if(bi!=null){
bi.flush();//Safe to free bi.flush();//Safe to free
} }
} }
throw e;} throw e;
}
} }
/** /**
* Generates the subimage that intersects with the given map rectangle. * Generates the subimage that intersects with the given map rectangle.
*
* @param x X coordinate of top-left point of the map. * @param x X coordinate of top-left point of the map.
* @param y Y coordinate of top-left point of the map. * @param y Y coordinate of top-left point of the map.
* @return the requested subimage. * @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); BufferedImage newImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
@ -126,79 +122,64 @@ public class PosterImage
graphics.drawImage(originalImage, -x, -y, null); graphics.drawImage(originalImage, -x, -y, null);
return newImage; return newImage;
} }
/** /**
*
* @return the split images * @return the split images
*/ */
public BufferedImage[] getImages() public BufferedImage[] getImages() {
{
return cutImages; return cutImages;
} }
public BufferedImage getImageAt(int i) public BufferedImage getImageAt(int i) {
{
return cutImages[i]; return cutImages[i];
} }
public BufferedImage getImage()
{ public BufferedImage getImage() {
return originalImage; return originalImage;
} }
public int getColumnAt(int i) public int getColumnAt(int i) {
{
return i % columns; return i % columns;
} }
public int getLineAt(int i) public int getLineAt(int i) {
{
return i / columns; return i / columns;
} }
/** /**
*
* @return the number of lines of the poster * @return the number of lines of the poster
*/ */
public int getLines() public int getLines() {
{
return lines; return lines;
} }
/** /**
*
* @return the number of columns of the poster * @return the number of columns of the poster
*/ */
public int getColumns() public int getColumns() {
{
return columns; return columns;
} }
/** /**
*
* @return the number of split images * @return the number of split images
*/ */
public int getImagesCount() public int getImagesCount() {
{
return cutImagesCount; return cutImagesCount;
} }
public int getRemainderX() public int getRemainderX() {
{
return remainderX; return remainderX;
} }
public void setRemainderX(int remainderX) public void setRemainderX(int remainderX) {
{
this.remainderX = remainderX; this.remainderX = remainderX;
} }
public int getRemainderY() public int getRemainderY() {
{
return remainderY; return remainderY;
} }
public void setRemainderY(int remainderY) public void setRemainderY(int remainderY) {
{
this.remainderY = remainderY; this.remainderY = remainderY;
} }
} }