Shift all imageIO and file I/O to async thread

Minus the version change in plugin.yml.
This commit is contained in:
Mike Primm 2011-05-10 23:03:11 -05:00 committed by FrozenCow
parent 619485212d
commit 0a8f2a182a
4 changed files with 150 additions and 88 deletions

View file

@ -11,7 +11,10 @@ import java.util.Map;
import javax.imageio.ImageIO;
import org.bukkit.World;
import org.dynmap.Client;
import org.dynmap.ColorScheme;
import org.dynmap.MapManager;
import org.dynmap.MapTile;
import org.dynmap.debug.Debug;
public class DefaultTileRenderer implements MapTileRenderer {
@ -104,12 +107,28 @@ public class DefaultTileRenderer implements MapTileRenderer {
iz--;
}
/* save the generated tile */
saveImage(im, outputFile);
im.flush();
tile.file = outputFile;
((KzedMap) tile.getMap()).invalidateTile(new KzedZoomedMapTile(world, (KzedMap) tile.getMap(), tile));
/* Hand encoding and writing file off to MapManager */
final File fname = outputFile;
final KzedMapTile mtile = tile;
final BufferedImage img = im;
MapManager.mapman.enqueueImageWrite(new Runnable() {
public void run() {
Debug.debug("saving image " + fname.getPath());
try {
ImageIO.write(img, "png", fname);
} catch (IOException e) {
Debug.error("Failed to save image: " + fname.getPath(), e);
} catch (java.lang.NullPointerException e) {
Debug.error("Failed to save image (NullPointerException): " + fname.getPath(), e);
}
img.flush();
mtile.file = fname;
((KzedMap)mtile.getMap()).invalidateTile(
new KzedZoomedMapTile(mtile.getWorld(), (KzedMap) mtile.getMap(), mtile));
MapManager.mapman.pushUpdate(mtile.getWorld(),
new Client.Tile(mtile.getFilename()));
}
});
return !isempty;
}
@ -170,17 +189,4 @@ public class DefaultTileRenderer implements MapTileRenderer {
}
}
}
/* save rendered tile, update zoom-out tile */
public void saveImage(BufferedImage im, File outputFile) {
Debug.debug("saving image " + outputFile.getPath());
/* save image */
try {
ImageIO.write(im, "png", outputFile);
} catch (IOException e) {
Debug.error("Failed to save image: " + outputFile.getPath(), e);
} catch (java.lang.NullPointerException e) {
Debug.error("Failed to save image (NullPointerException): " + outputFile.getPath(), e);
}
}
}

View file

@ -9,79 +9,89 @@ import java.util.Map;
import javax.imageio.ImageIO;
import org.dynmap.Client;
import org.dynmap.MapManager;
import org.dynmap.debug.Debug;
public class ZoomedTileRenderer {
public ZoomedTileRenderer(Map<String, Object> configuration) {
}
public void render(KzedZoomedMapTile zt, File outputPath) {
KzedMapTile originalTile = zt.originalTile;
int px = originalTile.px;
int py = originalTile.py;
int zpx = zt.getTileX();
int zpy = zt.getTileY();
public void render(final KzedZoomedMapTile zt, final File outputPath) {
/* Hand it all to map manager write thread */
MapManager.mapman.enqueueImageWrite(new Runnable() {
public void run() {
KzedMapTile originalTile = zt.originalTile;
int px = originalTile.px;
int py = originalTile.py;
int zpx = zt.getTileX();
int zpy = zt.getTileY();
BufferedImage image = null;
try {
image = ImageIO.read(originalTile.file);
} catch (IOException e) {
} catch (IndexOutOfBoundsException e) {
}
BufferedImage image = null;
try {
image = ImageIO.read(originalTile.file);
} catch (IOException e) {
} catch (IndexOutOfBoundsException e) {
}
if (image == null) {
Debug.debug("Could not load original tile, won't render zoom-out tile.");
return;
}
if (image == null) {
Debug.debug("Could not load original tile, won't render zoom-out tile.");
return;
}
BufferedImage zIm = null;
File zoomFile = outputPath;
try {
zIm = ImageIO.read(zoomFile);
} catch (IOException e) {
} catch (IndexOutOfBoundsException e) {
}
BufferedImage zIm = null;
File zoomFile = outputPath;
try {
zIm = ImageIO.read(zoomFile);
} catch (IOException e) {
} catch (IndexOutOfBoundsException e) {
}
if (zIm == null) {
/* create new one */
zIm = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
Debug.debug("New zoom-out tile created " + zt.getFilename());
} else {
Debug.debug("Loaded zoom-out tile from " + zt.getFilename());
}
if (zIm == null) {
/* create new one */
zIm = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
Debug.debug("New zoom-out tile created " + zt.getFilename());
} else {
Debug.debug("Loaded zoom-out tile from " + zt.getFilename());
}
/* update zoom-out tile */
/* update zoom-out tile */
/* scaled size */
int scw = KzedMap.tileWidth / 2;
int sch = KzedMap.tileHeight / 2;
/* scaled size */
int scw = KzedMap.tileWidth / 2;
int sch = KzedMap.tileHeight / 2;
/* origin in zoomed-out tile */
int ox = 0;
int oy = 0;
/* origin in zoomed-out tile */
int ox = 0;
int oy = 0;
if (zpx != px)
ox = scw;
if (zpy != py)
oy = sch;
if (zpx != px)
ox = scw;
if (zpy != py)
oy = sch;
/* blit scaled rendered tile onto zoom-out tile */
// WritableRaster zr = zIm.getRaster();
Graphics2D g2 = zIm.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, ox, oy, scw, sch, null);
/* blit scaled rendered tile onto zoom-out tile */
// WritableRaster zr = zIm.getRaster();
Graphics2D g2 = zIm.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, ox, oy, scw, sch, null);
image.flush();
image.flush();
/* save zoom-out tile */
try {
ImageIO.write(zIm, "png", zoomFile);
Debug.debug("Saved zoom-out tile at " + zoomFile.getName());
} catch (IOException e) {
Debug.error("Failed to save zoom-out tile: " + zoomFile.getName(), e);
} catch (java.lang.NullPointerException e) {
Debug.error("Failed to save zoom-out tile (NullPointerException): " + zoomFile.getName(), e);
}
zIm.flush();
/* save zoom-out tile */
try {
ImageIO.write(zIm, "png", zoomFile);
Debug.debug("Saved zoom-out tile at " + zoomFile.getName());
} catch (IOException e) {
Debug.error("Failed to save zoom-out tile: " + zoomFile.getName(), e);
} catch (java.lang.NullPointerException e) {
Debug.error("Failed to save zoom-out tile (NullPointerException): " + zoomFile.getName(), e);
}
zIm.flush();
MapManager.mapman.pushUpdate(zt.getWorld(),
new Client.Tile(zt.getFilename()));
}
});
}
}