added images flush and removed useless gc.dispose

This commit is contained in:
Vlammar 2020-07-08 09:06:54 +02:00
parent e894fb4f35
commit 219869c82b
4 changed files with 72 additions and 40 deletions

View File

@ -61,6 +61,7 @@ public class ImageIOExecutor extends Worker
{
BufferedImage image = ImageIO.read(file);
mapRenderer.setImage(image);
image.flush();//Safe to free
return null;
}
});
@ -88,7 +89,9 @@ public class ImageIOExecutor extends Worker
{
for(int i = 0, c = mapsIDs.length; i < c; i++)
{
ImageIOExecutor.saveImage(ImageOnMap.getPlugin().getImageFile(mapsIDs[i]), image.getImageAt(i));
BufferedImage img=image.getImageAt(i);
ImageIOExecutor.saveImage(ImageOnMap.getPlugin().getImageFile(mapsIDs[i]), img);
img.flush();//Safe to free
}
}

View File

@ -92,7 +92,7 @@ public class ImageRendererExecutor extends Worker
BufferedImage image=null;
//If the link is an imgur one
if (url.toString().contains("https://imgur.com/")) {
if (url.toString().startsWith("https://imgur.com/")) {
//Not handled, can't with the hash only access the image in i.imgur.com/<hash>.<extension>
@ -148,10 +148,13 @@ public class ImageRendererExecutor extends Worker
if (scaling != ImageUtils.ScalingType.NONE && height <= 1 && width <= 1) {
return 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;
}
final BufferedImage resizedImage = scaling.resize(image, ImageMap.WIDTH * width, ImageMap.HEIGHT * height);
//image.flush();
image.flush();//Safe to free
return renderPoster(resizedImage, playerUUID);
}
}, callback);
@ -171,19 +174,16 @@ public class ImageRendererExecutor extends Worker
});
final int mapID = futureMapID.get();
//ImageIOExecutor.saveImage(mapID, image);
ImageIOExecutor.saveImage(mapID,image);
submitToMainThread(new Callable<Void>()
{
@Override
public Void call() throws Exception
{
Renderer.installRenderer(image, mapID);
//image.flush();
return null;
}
});
//image.flush();
return MapManager.createMap(playerUUID, mapID);
}
@ -201,10 +201,10 @@ public class ImageRendererExecutor extends Worker
}
});
poster.splitImages();
final int[] mapsIDs = futureMapsIds.get();
// ImageIOExecutor.saveImage(mapsIDs, poster);
ImageIOExecutor.saveImage(mapsIDs,poster);
if (PluginConfiguration.SAVE_FULL_IMAGE.get())
{
ImageIOExecutor.saveImage(ImageMap.getFullImageFile(mapsIDs[0], mapsIDs[mapsIDs.length - 1]), image);
@ -220,9 +220,7 @@ public class ImageRendererExecutor extends Worker
}
});
// image.flush();
poster.getImage().flush();//Safe?
return MapManager.createMap(poster, playerUUID, mapsIDs);
}
}

View File

@ -36,9 +36,12 @@
package fr.moribus.imageonmap.image;
import fr.zcraft.zlib.tools.PluginLogger;
import java.awt.*;
import java.awt.image.BufferedImage;
//import java.awt.*;
/**
* Various image-related utilities
*/
@ -57,6 +60,7 @@ public class ImageUtils {
case COVERED: return ImageUtils.resize(source, destinationW, destinationH, true);
case STRETCHED: return resizeStretched(source, destinationW, destinationH);
default: return source;
}
}
}
@ -70,28 +74,31 @@ public class ImageUtils {
*/
static private BufferedImage resize(BufferedImage source, int destinationW, int destinationH, boolean covered)
{
float ratioW = (float)destinationW / (float)source.getWidth();
float ratioH = (float)destinationH / (float)source.getHeight();
int finalW, finalH;
int x, y;
try {
float ratioW = (float) destinationW / (float) source.getWidth();
float ratioH = (float) destinationH / (float) source.getHeight();
int finalW, finalH;
int x, y;
if(covered ? ratioW > ratioH : ratioW < ratioH)
{
finalW = destinationW;
finalH = (int)(source.getHeight() * ratioW);
if (covered ? ratioW > ratioH : ratioW < ratioH) {
finalW = destinationW;
finalH = (int) (source.getHeight() * ratioW);
} else {
finalW = (int) (source.getWidth() * ratioH);
finalH = destinationH;
}
x = (destinationW - finalW) / 2;
y = (destinationH - finalH) / 2;
return drawImage(source,
destinationW, destinationH,
x, y, finalW, finalH);
}
else
{
finalW = (int)(source.getWidth() * ratioH);
finalH = destinationH;
catch(final Throwable e){
throw e;
}
x = (destinationW - finalW) / 2;
y = (destinationH - finalH) / 2;
return drawImage(source,
destinationW, destinationH,
x, y, finalW, finalH);
}
/**
@ -123,11 +130,22 @@ public class ImageUtils {
int bufferW, int bufferH,
int posX, int posY,
int sourceW, int sourceH) {
BufferedImage newImage = new BufferedImage(bufferW, bufferH, BufferedImage.TYPE_INT_ARGB);
Graphics graphics=null;
BufferedImage newImage= null;
try{
newImage = new BufferedImage(bufferW, bufferH, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = newImage.getGraphics();
graphics = newImage.getGraphics();
graphics.drawImage(source, posX, posY, sourceW, sourceH, null);
graphics.dispose();
return newImage;
}
catch(final Throwable e) {
PluginLogger.warning("Exception/error at drawImage");
if(newImage!=null )
newImage.flush();//Safe to free
throw e;
}
}
}

View File

@ -36,7 +36,7 @@
package fr.moribus.imageonmap.image;
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
@ -83,6 +83,7 @@ public class PosterImage
public void splitImages()
{
try{
cutImages = new BufferedImage[cutImagesCount];
int imageX;
@ -97,8 +98,16 @@ public class PosterImage
}
imageY += HEIGHT;
}
originalImage = null;
}
catch(final Throwable e){
if(cutImages!=null)
for(BufferedImage bi: cutImages){
if(bi!=null){
bi.flush();//Safe to free
}
}
throw e;}
}
/**
@ -109,12 +118,12 @@ public class PosterImage
*/
private BufferedImage makeSubImage(BufferedImage originalImage, int x, int y)
{
BufferedImage newImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = newImage.getGraphics();
graphics.drawImage(originalImage, -x, -y, null);
graphics.dispose();
return newImage;
}
@ -131,6 +140,10 @@ public class PosterImage
{
return cutImages[i];
}
public BufferedImage getImage()
{
return originalImage;
}
public int getColumnAt(int i)
{