mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-04-30 00:00:01 -04:00
TMI-TIFF: Fixed several bugs in the LittleEndianDataInputStream needed for proper TIFF output (should affect other things as well...)
This commit is contained in:
+476
-45
@@ -30,6 +30,7 @@ package com.twelvemonkeys.image;
|
||||
|
||||
import com.twelvemonkeys.imageio.util.ProgressListenerBase;
|
||||
import com.twelvemonkeys.lang.StringUtil;
|
||||
import com.twelvemonkeys.util.LRUHashMap;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
@@ -38,17 +39,17 @@ import javax.imageio.ImageTypeSpecifier;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBuffer;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* MappedBufferImage
|
||||
@@ -59,7 +60,7 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class MappedBufferImage {
|
||||
private static int threads = Runtime.getRuntime().availableProcessors();
|
||||
private static ExecutorService executorService = Executors.newFixedThreadPool(threads);
|
||||
private static ExecutorService executorService = Executors.newFixedThreadPool(threads * 4);
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
int argIndex = 0;
|
||||
@@ -91,8 +92,9 @@ public class MappedBufferImage {
|
||||
|
||||
// TODO: Negotiate best layout according to the GraphicsConfiguration.
|
||||
|
||||
w = reader.getWidth(0);
|
||||
h = reader.getHeight(0);
|
||||
int sub = 1;
|
||||
w = reader.getWidth(0) / sub;
|
||||
h = reader.getHeight(0) / sub;
|
||||
|
||||
// GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
|
||||
// ColorModel cm2 = configuration.getColorModel(cm.getTransparency());
|
||||
@@ -111,8 +113,11 @@ public class MappedBufferImage {
|
||||
|
||||
System.out.println("image = " + image);
|
||||
|
||||
// TODO: Display image while reading
|
||||
|
||||
ImageReadParam param = reader.getDefaultReadParam();
|
||||
param.setDestination(image);
|
||||
param.setSourceSubsampling(sub, sub, 0, 0);
|
||||
|
||||
reader.addIIOReadProgressListener(new ConsoleProgressListener());
|
||||
reader.read(0, param);
|
||||
@@ -166,7 +171,7 @@ public class MappedBufferImage {
|
||||
return size;
|
||||
}
|
||||
};
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
JScrollPane scroll = new JScrollPane(new ImageComponent(image));
|
||||
scroll.setBorder(BorderFactory.createEmptyBorder());
|
||||
frame.add(scroll);
|
||||
@@ -184,13 +189,24 @@ public class MappedBufferImage {
|
||||
// NOTE: The createCompatibleDestImage takes the byte order/layout into account, unlike the cm.createCompatibleWritableRaster
|
||||
final BufferedImage output = new ResampleOp(width, height).createCompatibleDestImage(image, null);
|
||||
|
||||
final int inStep = (int) Math.ceil(image.getHeight() / (double) threads);
|
||||
final int outStep = (int) Math.ceil(height / (double) threads);
|
||||
final int steps = threads * height / 100;
|
||||
final int inStep = (int) Math.ceil(image.getHeight() / (double) steps);
|
||||
final int outStep = (int) Math.ceil(height / (double) steps);
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(threads);
|
||||
final CountDownLatch latch = new CountDownLatch(steps);
|
||||
|
||||
// System.out.println("Starting image scale on single thread, waiting for execution to complete...");
|
||||
// BufferedImage output = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS).filter(image, null);
|
||||
System.out.printf("Started image scale on %d threads, waiting for execution to complete...\n", threads);
|
||||
|
||||
System.out.print("[");
|
||||
final int dotsPerStep = 78 / steps;
|
||||
for (int j = 0; j < 78 - (steps * dotsPerStep); j++) {
|
||||
System.out.print(".");
|
||||
}
|
||||
|
||||
// Resample image in slices
|
||||
for (int i = 0; i < threads; i++) {
|
||||
for (int i = 0; i < steps; i++) {
|
||||
final int inY = i * inStep;
|
||||
final int outY = i * outStep;
|
||||
final int inHeight = Math.min(inStep, image.getHeight() - inY);
|
||||
@@ -200,10 +216,12 @@ public class MappedBufferImage {
|
||||
try {
|
||||
BufferedImage in = image.getSubimage(0, inY, image.getWidth(), inHeight);
|
||||
BufferedImage out = output.getSubimage(0, outY, width, outHeight);
|
||||
new ResampleOp(width, outHeight, ResampleOp.FILTER_LANCZOS).filter(in, out);
|
||||
// new ResampleOp(width, outHeight, ResampleOp.FILTER_LANCZOS).resample(in, out, ResampleOp.createFilter(ResampleOp.FILTER_LANCZOS));
|
||||
// BufferedImage out = new ResampleOp(width, outHeight, ResampleOp.FILTER_LANCZOS).filter(in, null);
|
||||
// ImageUtil.drawOnto(output.getSubimage(0, outY, width, outHeight), out);
|
||||
new ResampleOp(width, outHeight, ResampleOp.FILTER_TRIANGLE).filter(in, out);
|
||||
// new ResampleOp(width, outHeight, ResampleOp.FILTER_LANCZOS).filter(in, out);
|
||||
|
||||
for (int j = 0; j < dotsPerStep; j++) {
|
||||
System.out.print(".");
|
||||
}
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
e.printStackTrace();
|
||||
@@ -216,19 +234,17 @@ public class MappedBufferImage {
|
||||
});
|
||||
}
|
||||
|
||||
// System.out.println("Starting image scale on single thread, waiting for execution to complete...");
|
||||
// BufferedImage output = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS).filter(image, null);
|
||||
System.out.printf("Started image scale on %d threads, waiting for execution to complete...%n", threads);
|
||||
|
||||
Boolean done = null;
|
||||
try {
|
||||
done = latch.await(5L, TimeUnit.MINUTES);
|
||||
}
|
||||
catch (InterruptedException ignore) {
|
||||
}
|
||||
System.out.println("]");
|
||||
|
||||
System.out.printf("%s scaling image in %d ms%n", (done == null ? "Interrupted" : !done ? "Timed out" : "Done"), System.currentTimeMillis() - start);
|
||||
System.out.printf("%s scaling image in %d ms\n", (done == null ? "Interrupted" : !done ? "Timed out" : "Done"), System.currentTimeMillis() - start);
|
||||
System.out.println("image = " + output);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -358,10 +374,12 @@ public class MappedBufferImage {
|
||||
private static class ImageComponent extends JComponent implements Scrollable {
|
||||
private final BufferedImage image;
|
||||
private Paint texture;
|
||||
double zoom = 1;
|
||||
private double zoom = 1;
|
||||
|
||||
public ImageComponent(final BufferedImage image) {
|
||||
setOpaque(true); // Very important when subclassing JComponent...
|
||||
setOpaque(true); // Very important when sub classing JComponent...
|
||||
setDoubleBuffered(true);
|
||||
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
@@ -370,6 +388,68 @@ public class MappedBufferImage {
|
||||
super.addNotify();
|
||||
|
||||
texture = createTexture();
|
||||
|
||||
Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
|
||||
zoom = Math.min(1.0, Math.min(bounds.getWidth() / (double) image.getWidth(), bounds.getHeight() / (double) image.getHeight()));
|
||||
|
||||
// TODO: Take scroll pane into account when zooming (center around center point)
|
||||
AbstractAction zoomIn = new AbstractAction() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.err.println("ZOOM IN");
|
||||
setZoom(zoom * 2);
|
||||
}
|
||||
};
|
||||
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, getToolkit().getMenuShortcutKeyMask()), zoomIn);
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, getToolkit().getMenuShortcutKeyMask()), zoomIn);
|
||||
addAction(KeyStroke.getKeyStroke(Character.valueOf('+'), 0), zoomIn);
|
||||
addAction(KeyStroke.getKeyStroke(Character.valueOf('+'), getToolkit().getMenuShortcutKeyMask()), zoomIn);
|
||||
AbstractAction zoomOut = new AbstractAction() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.err.println("ZOOM OUT");
|
||||
setZoom(zoom / 2);
|
||||
}
|
||||
};
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, getToolkit().getMenuShortcutKeyMask()), zoomOut);
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, getToolkit().getMenuShortcutKeyMask()), zoomOut);
|
||||
addAction(KeyStroke.getKeyStroke(Character.valueOf('-'), 0), zoomOut);
|
||||
addAction(KeyStroke.getKeyStroke(Character.valueOf('-'), getToolkit().getMenuShortcutKeyMask()), zoomOut);
|
||||
AbstractAction zoomFit = new AbstractAction() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.err.println("ZOOM FIT");
|
||||
// Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
|
||||
Rectangle bounds = getVisibleRect();
|
||||
setZoom(Math.min(1.0, Math.min(bounds.getWidth() / (double) image.getWidth(), bounds.getHeight() / (double) image.getHeight())));
|
||||
}
|
||||
};
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, getToolkit().getMenuShortcutKeyMask()), zoomFit);
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_9, getToolkit().getMenuShortcutKeyMask()), zoomFit);
|
||||
addAction(KeyStroke.getKeyStroke(KeyEvent.VK_0, getToolkit().getMenuShortcutKeyMask()), new AbstractAction() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.err.println("ZOOM ACTUAL");
|
||||
setZoom(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setZoom(final double newZoom) {
|
||||
if (newZoom != zoom) {
|
||||
zoom = newZoom;
|
||||
// TODO: Add PCL support for zoom and discard tiles cache based on property change
|
||||
tiles = createTileCache();
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Point, Tile> createTileCache() {
|
||||
return Collections.synchronizedMap(new SizedLRUMap<Point, Tile>(16 * 1024 * 1024));
|
||||
}
|
||||
|
||||
private void addAction(final KeyStroke keyStroke, final AbstractAction action) {
|
||||
UUID key = UUID.randomUUID();
|
||||
getInputMap(WHEN_IN_FOCUSED_WINDOW).put(keyStroke, key);
|
||||
getActionMap().put(key, action);
|
||||
}
|
||||
|
||||
private Paint createTexture() {
|
||||
@@ -392,10 +472,17 @@ public class MappedBufferImage {
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
// TODO: Java 7 kills the performance from our custom painting... :-(
|
||||
|
||||
// TODO: Figure out why mouse wheel/track pad scroll repaints entire component,
|
||||
// unlike using the scroll bars of the JScrollPane.
|
||||
// Consider creating a custom mouse wheel listener as a workaround.
|
||||
|
||||
// TODO: Cache visible rect content in buffered/volatile image (s) + visible rect (+ zoom) to speed up repaints
|
||||
// - Blit the cahced image (possibly translated) (onto itself?)
|
||||
// - Paint only the necessary parts outside the cached image
|
||||
// - Async rendering into cached image
|
||||
|
||||
// We want to paint only the visible part of the image
|
||||
Rectangle visible = getVisibleRect();
|
||||
Rectangle clip = g.getClipBounds();
|
||||
@@ -405,9 +492,28 @@ public class MappedBufferImage {
|
||||
g2.setPaint(texture);
|
||||
g2.fillRect(rect.x, rect.y, rect.width, rect.height);
|
||||
|
||||
/*
|
||||
// Center image (might not be the best way to cooperate with the scroll pane)
|
||||
Rectangle imageSize = new Rectangle((int) Math.round(image.getWidth() * zoom), (int) Math.round(image.getHeight() * zoom));
|
||||
if (imageSize.width < getWidth()) {
|
||||
g2.translate((getWidth() - imageSize.width) / 2, 0);
|
||||
}
|
||||
if (imageSize.height < getHeight()) {
|
||||
g2.translate(0, (getHeight() - imageSize.height) / 2);
|
||||
}
|
||||
*/
|
||||
|
||||
// Zoom
|
||||
if (zoom != 1) {
|
||||
AffineTransform transform = AffineTransform.getScaleInstance(zoom, zoom);
|
||||
g2.setTransform(transform);
|
||||
// NOTE: This helps mostly when scaling up, or scaling down less than 50%
|
||||
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
|
||||
rect = new Rectangle(
|
||||
(int) Math.round(rect.x / zoom), (int) Math.round(rect.y / zoom),
|
||||
(int) Math.round(rect.width / zoom), (int) Math.round(rect.height / zoom)
|
||||
);
|
||||
|
||||
rect = rect.intersection(new Rectangle(image.getWidth(), image.getHeight()));
|
||||
}
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
@@ -415,39 +521,308 @@ public class MappedBufferImage {
|
||||
System.err.println("repaint: " + (System.currentTimeMillis() - start) + " ms");
|
||||
}
|
||||
|
||||
private void repaintImage(Rectangle rect, Graphics2D g2) {
|
||||
static class Tile {
|
||||
private final int size;
|
||||
|
||||
private final int x;
|
||||
private final int y;
|
||||
|
||||
private final Reference<BufferedImage> data;
|
||||
private final BufferedImage hardRef;
|
||||
|
||||
Tile(int x, int y, BufferedImage data) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.data = new SoftReference<BufferedImage>(data);
|
||||
|
||||
hardRef = data;
|
||||
|
||||
size = 16 + data.getWidth() * data.getHeight() * data.getRaster().getNumDataElements() * sizeOf(data.getRaster().getTransferType());
|
||||
}
|
||||
|
||||
private static int sizeOf(final int transferType) {
|
||||
switch (transferType) {
|
||||
case DataBuffer.TYPE_INT:
|
||||
return 4;
|
||||
case DataBuffer.TYPE_SHORT:
|
||||
return 2;
|
||||
case DataBuffer.TYPE_BYTE:
|
||||
return 1;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported transfer type: " + transferType);
|
||||
}
|
||||
}
|
||||
|
||||
public void drawTo(Graphics2D g) {
|
||||
BufferedImage img = data.get();
|
||||
|
||||
if (img != null) {
|
||||
g.drawImage(img, x, y, null);
|
||||
}
|
||||
|
||||
// g.setPaint(Color.GREEN);
|
||||
// g.drawString(String.format("[%d, %d]", x, y), x + 20, y + 20);
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
BufferedImage img = data.get();
|
||||
return img != null ? img.getWidth() : -1;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
BufferedImage img = data.get();
|
||||
return img != null ? img.getHeight() : -1;
|
||||
}
|
||||
|
||||
public Rectangle getRect() {
|
||||
BufferedImage img = data.get();
|
||||
return img != null ? new Rectangle(x, y, img.getWidth(), img.getHeight()) : null;
|
||||
}
|
||||
|
||||
public Point getLocation() {
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (other == null || getClass() != other.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Tile tile = (Tile) other;
|
||||
|
||||
return x == tile.x && y == tile.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 997 * x + y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Tile[%d, %d, %d, %d]", x, y, getWidth(), getHeight());
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Consider a fixed size (mem) LRUCache instead
|
||||
Map<Point, Tile> tiles = createTileCache();
|
||||
|
||||
private void repaintImage(final Rectangle rect, final Graphics2D g2) {
|
||||
// System.err.println("rect: " + rect);
|
||||
// System.err.println("tiles: " + tiles.size());
|
||||
// TODO: Fix rounding errors
|
||||
// FIx repaint bugs
|
||||
|
||||
try {
|
||||
// Paint tiles of the image, to preserve memory
|
||||
int sliceSize = 200;
|
||||
final int tileSize = 200;
|
||||
|
||||
int slicesW = rect.width / sliceSize;
|
||||
int slicesH = rect.height / sliceSize;
|
||||
int tilesW = 1 + rect.width / tileSize;
|
||||
int tilesH = 1 + rect.height / tileSize;
|
||||
|
||||
for (int sliceY = 0; sliceY <= slicesH; sliceY++) {
|
||||
for (int sliceX = 0; sliceX <= slicesW; sliceX++) {
|
||||
int x = rect.x + sliceX * sliceSize;
|
||||
int y = rect.y + sliceY * sliceSize;
|
||||
for (int yTile = 0; yTile <= tilesH; yTile++) {
|
||||
for (int xTile = 0; xTile <= tilesW; xTile++) {
|
||||
// Image (source) coordinates
|
||||
int x = rect.x + xTile * tileSize;
|
||||
int y = rect.y + yTile * tileSize;
|
||||
|
||||
int w = sliceX == slicesW ? Math.min(sliceSize, rect.x + rect.width - x) : sliceSize;
|
||||
int h = sliceY == slicesH ? Math.min(sliceSize, rect.y + rect.height - y) : sliceSize;
|
||||
int w = xTile == tilesW ? Math.min(tileSize, rect.x + rect.width - x) : tileSize;
|
||||
int h = yTile == tilesH ? Math.min(tileSize, rect.y + rect.height - y) : tileSize;
|
||||
|
||||
if (w == 0 || h == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// System.err.printf("%04d, %04d, %04d, %04d%n", x, y, w, h);
|
||||
BufferedImage img = image.getSubimage(x, y, w, h);
|
||||
g2.drawImage(img, x, y, null);
|
||||
|
||||
// - Get tile from cache
|
||||
// - If non-null, paint
|
||||
// - If null, request data for later use, with callback, and return
|
||||
// TODO: Could we use ImageProducer/ImageConsumer/ImageObserver interface??
|
||||
|
||||
// Destination (display) coordinates
|
||||
int dstX = (int) Math.round(x * zoom);
|
||||
int dstY = (int) Math.round(y * zoom);
|
||||
int dstW = (int) Math.round(w * zoom);
|
||||
int dstH = (int) Math.round(h * zoom);
|
||||
|
||||
if (dstW == 0 || dstH == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't create overlapping/duplicate tiles...
|
||||
// - Always start tile grid at 0,0
|
||||
// - Always occupy entire tile, unless edge
|
||||
|
||||
// Source (original) coordinates
|
||||
int tileSrcX = x - x % tileSize;
|
||||
int tileSrcY = y - y % tileSize;
|
||||
// final int tileSrcW = Math.min(tileSize, image.getWidth() - tileSrcX);
|
||||
// final int tileSrcH = Math.min(tileSize, image.getHeight() - tileSrcY);
|
||||
|
||||
// Destination (display) coordinates
|
||||
int tileDstX = (int) Math.round(tileSrcX * zoom);
|
||||
int tileDstY = (int) Math.round(tileSrcY * zoom);
|
||||
// final int tileDstW = (int) Math.round(tileSrcW * zoom);
|
||||
// final int tileDstH = (int) Math.round(tileSrcH * zoom);
|
||||
|
||||
List<Point> points = new ArrayList<Point>(4);
|
||||
points.add(new Point(tileDstX, tileDstY));
|
||||
if (tileDstX != dstX) {
|
||||
points.add(new Point(tileDstX + tileSize, tileDstY));
|
||||
}
|
||||
if (tileDstY != dstY) {
|
||||
points.add(new Point(tileDstX, tileDstY + tileSize));
|
||||
}
|
||||
if (tileDstX != dstX && tileDstY != dstY) {
|
||||
points.add(new Point(tileDstX + tileSize, tileDstY + tileSize));
|
||||
}
|
||||
|
||||
for (final Point point : points) {
|
||||
Tile tile = tiles.get(point);
|
||||
|
||||
if (tile != null) {
|
||||
Reference<BufferedImage> img = tile.data;
|
||||
if (img != null) {
|
||||
tile.drawTo(g2);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
tiles.remove(point);
|
||||
}
|
||||
}
|
||||
|
||||
// System.err.printf("Tile miss: [%d, %d]\n", dstX, dstY);
|
||||
|
||||
// Dispatch to off-thread worker
|
||||
final Map<Point, Tile> localTiles = tiles;
|
||||
executorService.submit(new Runnable() {
|
||||
public void run() {
|
||||
// TODO: Fix rounding issues... Problem is that sometimes the srcW/srcH is 1 pixel off filling the tile...
|
||||
int tileSrcX = (int) Math.round(point.x / zoom);
|
||||
int tileSrcY = (int) Math.round(point.y / zoom);
|
||||
int tileSrcW = Math.min(tileSize, image.getWidth() - tileSrcX);
|
||||
int tileSrcH = Math.min(tileSize, image.getHeight() - tileSrcY);
|
||||
int tileDstW = (int) Math.round(tileSrcW * zoom);
|
||||
int tileDstH = (int) Math.round(tileSrcH * zoom);
|
||||
|
||||
try {
|
||||
// TODO: Consider comparing zoom/local zoom
|
||||
if (localTiles != tiles) {
|
||||
return; // Return early after re-zoom
|
||||
}
|
||||
|
||||
if (localTiles.containsKey(point)) {
|
||||
// System.err.println("Skipping tile, already producing...");
|
||||
return;
|
||||
}
|
||||
|
||||
// Test against current view rect, to avoid computing tiles that will be thrown away immediately
|
||||
// TODO: EDT safe?
|
||||
if (!getVisibleRect().intersects(new Rectangle(point.x, point.y, tileDstW, tileDstH))) {
|
||||
return;
|
||||
}
|
||||
|
||||
// System.err.printf("Creating tile: [%d, %d]\n", tileDstX, tileDstY);
|
||||
|
||||
BufferedImage temp = getGraphicsConfiguration().createCompatibleImage(tileDstW, tileDstH);
|
||||
final Tile tile = new Tile(point.x, point.y, temp);
|
||||
localTiles.put(point, tile);
|
||||
|
||||
Graphics2D graphics = temp.createGraphics();
|
||||
try {
|
||||
Object hint = g2.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
|
||||
|
||||
if (hint != null) {
|
||||
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
|
||||
}
|
||||
|
||||
graphics.scale(zoom, zoom);
|
||||
graphics.drawImage(image.getSubimage(tileSrcX, tileSrcY, tileSrcW, tileSrcH), 0, 0, null);
|
||||
}
|
||||
finally {
|
||||
graphics.dispose();
|
||||
}
|
||||
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
repaint(10, tile.x, tile.y, tile.getWidth(), tile.getHeight());
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Throwable t) {
|
||||
localTiles.remove(point);
|
||||
System.err.println("Boooo: " + t.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// BufferedImage img = image.getSubimage(rect.x, rect.y, rect.width, rect.height);
|
||||
// g2.drawImage(img, rect.x, rect.y, null);
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// e.printStackTrace();
|
||||
// Happens whenever apple.awt.OSXCachingSufraceManager runs out of memory
|
||||
// Happens whenever apple.awt.OSXCachingSurfaceManager runs out of memory
|
||||
// TODO: Figure out why repaint(x,y,w,h) doesn't work any more..?
|
||||
System.err.println("Full repaint due to NullPointerException (probably out of memory).");
|
||||
repaint(); // NOTE: Might cause a brief flash while the component is redrawn
|
||||
}
|
||||
}
|
||||
|
||||
private void repaintImage0(final Rectangle rect, final Graphics2D g2) {
|
||||
g2.scale(zoom, zoom);
|
||||
|
||||
try {
|
||||
// Paint tiles of the image, to preserve memory
|
||||
final int tileSize = 200;
|
||||
|
||||
int tilesW = rect.width / tileSize;
|
||||
int tilesH = rect.height / tileSize;
|
||||
|
||||
for (int yTile = 0; yTile <= tilesH; yTile++) {
|
||||
for (int xTile = 0; xTile <= tilesW; xTile++) {
|
||||
// Image (source) coordinates
|
||||
final int x = rect.x + xTile * tileSize;
|
||||
final int y = rect.y + yTile * tileSize;
|
||||
|
||||
final int w = xTile == tilesW ? Math.min(tileSize, rect.x + rect.width - x) : tileSize;
|
||||
final int h = yTile == tilesH ? Math.min(tileSize, rect.y + rect.height - y) : tileSize;
|
||||
|
||||
if (w == 0 || h == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// System.err.printf("%04d, %04d, %04d, %04d%n", x, y, w, h);
|
||||
|
||||
BufferedImage img = image.getSubimage(x, y, w, h);
|
||||
g2.drawImage(img, x, y, null);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
// e.printStackTrace();
|
||||
// Happens whenever apple.awt.OSXCachingSurfaceManager runs out of memory
|
||||
// TODO: Figure out why repaint(x,y,w,h) doesn't work any more..?
|
||||
System.err.println("Full repaint due to NullPointerException (probably out of memory).");
|
||||
repaint(); // NOTE: Might cause a brief flash while the component is redrawn
|
||||
}
|
||||
}
|
||||
@@ -476,12 +851,68 @@ public class MappedBufferImage {
|
||||
}
|
||||
|
||||
public boolean getScrollableTracksViewportWidth() {
|
||||
return false;
|
||||
return getWidth() > getPreferredSize().width;
|
||||
}
|
||||
|
||||
public boolean getScrollableTracksViewportHeight() {
|
||||
return getHeight() > getPreferredSize().height;
|
||||
}
|
||||
}
|
||||
|
||||
final static class SizedLRUMap<K, V> extends LRUHashMap<K, V> {
|
||||
int currentSize;
|
||||
int maxSize;
|
||||
|
||||
public SizedLRUMap(int pMaxSize) {
|
||||
super(); // Note: super.maxSize doesn't count...
|
||||
maxSize = pMaxSize;
|
||||
}
|
||||
|
||||
|
||||
protected int sizeOf(final Object pValue) {
|
||||
ImageComponent.Tile cached = (ImageComponent.Tile) pValue;
|
||||
|
||||
if (cached == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return cached.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K pKey, V pValue) {
|
||||
currentSize += sizeOf(pValue);
|
||||
|
||||
V old = super.put(pKey, pValue);
|
||||
if (old != null) {
|
||||
currentSize -= sizeOf(old);
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(Object pKey) {
|
||||
V old = super.remove(pKey);
|
||||
if (old != null) {
|
||||
currentSize -= sizeOf(old);
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<K, V> pEldest) {
|
||||
if (maxSize <= currentSize) { // NOTE: maxSize here is mem size
|
||||
removeLRU();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLRU() {
|
||||
while (maxSize <= currentSize) { // NOTE: maxSize here is mem size
|
||||
super.removeLRU();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class PaintDotsTask implements Runnable {
|
||||
|
||||
Reference in New Issue
Block a user