use of java.awt.image in project jdk8u_jdk by JetBrains.
the class ImageCache method setImage.
/**
* Sets the cached image for the specified constraints.
*
* @param image The image to store in cache
* @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
* @param w The image width, used as part of cache key
* @param h The image height, used as part of cache key
* @param args Other arguments to use as part of the cache key
* @return true if the image could be cached or false if the image is too big
*/
public boolean setImage(Image image, GraphicsConfiguration config, int w, int h, Object... args) {
if (!isImageCachable(w, h))
return false;
int hash = hash(config, w, h, args);
lock.writeLock().lock();
try {
PixelCountSoftReference ref = map.get(hash);
// check if currently in map
if (ref != null && ref.get() == image) {
return true;
}
// clear out old
if (ref != null) {
currentPixelCount -= ref.pixelCount;
map.remove(hash);
}
// add new image to pixel count
int newPixelCount = image.getWidth(null) * image.getHeight(null);
currentPixelCount += newPixelCount;
// clean out lost references if not enough space
if (currentPixelCount > maxPixelCount) {
while ((ref = (PixelCountSoftReference) referenceQueue.poll()) != null) {
//reference lost
map.remove(ref.hash);
currentPixelCount -= ref.pixelCount;
}
}
// remove old items till there is enough free space
if (currentPixelCount > maxPixelCount) {
Iterator<Map.Entry<Integer, PixelCountSoftReference>> mapIter = map.entrySet().iterator();
while ((currentPixelCount > maxPixelCount) && mapIter.hasNext()) {
Map.Entry<Integer, PixelCountSoftReference> entry = mapIter.next();
mapIter.remove();
Image img = entry.getValue().get();
if (img != null)
img.flush();
currentPixelCount -= entry.getValue().pixelCount;
}
}
// finaly put new in map
map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount, hash, config, w, h, args));
return true;
} finally {
lock.writeLock().unlock();
}
}
use of java.awt.image in project jdk8u_jdk by JetBrains.
the class SunGraphics2D method getResolutionVariant.
private Image getResolutionVariant(MultiResolutionImage img, int srcWidth, int srcHeight, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, AffineTransform xform) {
if (srcWidth <= 0 || srcHeight <= 0) {
return null;
}
int sw = sx2 - sx1;
int sh = sy2 - sy1;
if (sw == 0 || sh == 0) {
return null;
}
AffineTransform tx;
if (xform == null) {
tx = transform;
} else {
tx = new AffineTransform(transform);
tx.concatenate(xform);
}
int type = tx.getType();
int dw = dx2 - dx1;
int dh = dy2 - dy1;
double destImageWidth;
double destImageHeight;
if (resolutionVariantHint == SunHints.INTVAL_RESOLUTION_VARIANT_BASE) {
destImageWidth = srcWidth;
destImageHeight = srcHeight;
} else if (resolutionVariantHint == SunHints.INTVAL_RESOLUTION_VARIANT_DPI_FIT) {
AffineTransform configTransform = getDefaultTransform();
if (configTransform.isIdentity()) {
destImageWidth = srcWidth;
destImageHeight = srcHeight;
} else {
destImageWidth = srcWidth * configTransform.getScaleX();
destImageHeight = srcHeight * configTransform.getScaleY();
}
} else {
double destRegionWidth;
double destRegionHeight;
if ((type & ~(TYPE_TRANSLATION | TYPE_FLIP)) == 0) {
destRegionWidth = dw;
destRegionHeight = dh;
} else if ((type & ~(TYPE_TRANSLATION | TYPE_FLIP | TYPE_MASK_SCALE)) == 0) {
destRegionWidth = dw * transform.getScaleX();
destRegionHeight = dh * transform.getScaleY();
} else {
destRegionWidth = dw * Math.hypot(transform.getScaleX(), transform.getShearY());
destRegionHeight = dh * Math.hypot(transform.getShearX(), transform.getScaleY());
}
destImageWidth = Math.abs(srcWidth * destRegionWidth / sw);
destImageHeight = Math.abs(srcHeight * destRegionHeight / sh);
}
Image resolutionVariant = img.getResolutionVariant(destImageWidth, destImageHeight);
if (resolutionVariant instanceof ToolkitImage && ((ToolkitImage) resolutionVariant).hasError()) {
return null;
}
return resolutionVariant;
}
use of java.awt.image in project jdk8u_jdk by JetBrains.
the class SunGraphics2D method drawHiDPIImage.
// end of text rendering methods
private Boolean drawHiDPIImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer, AffineTransform xform) {
if (img instanceof VolatileImage) {
final SurfaceData sd = SurfaceManager.getManager(img).getPrimarySurfaceData();
final double scaleX = sd.getDefaultScaleX();
final double scaleY = sd.getDefaultScaleY();
if (scaleX == 1 && scaleY == 1) {
return null;
}
sx1 = Region.clipScale(sx1, scaleX);
sx2 = Region.clipScale(sx2, scaleX);
sy1 = Region.clipScale(sy1, scaleY);
sy2 = Region.clipScale(sy2, scaleY);
AffineTransform tx = null;
if (xform != null) {
tx = new AffineTransform(transform);
transform(xform);
}
boolean result = scaleImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
if (tx != null) {
transform.setTransform(tx);
invalidateTransform();
}
return result;
} else if (resolutionVariantHint != SunHints.INTVAL_RESOLUTION_VARIANT_BASE && (img instanceof MultiResolutionImage)) {
// get scaled destination image size
int width = img.getWidth(observer);
int height = img.getHeight(observer);
MultiResolutionImage mrImage = (MultiResolutionImage) img;
Image resolutionVariant = getResolutionVariant(mrImage, width, height, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, xform);
if (resolutionVariant != img && resolutionVariant != null) {
// recalculate source region for the resolution variant
ImageObserver rvObserver = MultiResolutionToolkitImage.getResolutionVariantObserver(img, observer, width, height, -1, -1);
int rvWidth = resolutionVariant.getWidth(rvObserver);
int rvHeight = resolutionVariant.getHeight(rvObserver);
if (0 < width && 0 < height && 0 < rvWidth && 0 < rvHeight) {
double widthScale = ((double) rvWidth) / width;
double heightScale = ((double) rvHeight) / height;
sx1 = Region.clipScale(sx1, widthScale);
sy1 = Region.clipScale(sy1, heightScale);
sx2 = Region.clipScale(sx2, widthScale);
sy2 = Region.clipScale(sy2, heightScale);
observer = rvObserver;
img = resolutionVariant;
if (xform != null) {
assert dx1 == 0 && dy1 == 0;
assert dx2 == img.getWidth(observer);
assert dy2 == img.getHeight(observer);
AffineTransform renderTX = new AffineTransform(xform);
renderTX.scale(1 / widthScale, 1 / heightScale);
return transformImage(img, renderTX, observer);
}
return scaleImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
}
}
}
return null;
}
use of java.awt.image in project jdk8u_jdk by JetBrains.
the class ImageMediaEntry method addImage.
/**
* Adds a scaled image to the list of images being tracked
* by this media tracker. The image will eventually be
* rendered at the indicated width and height.
*
* @param image the image to be tracked
* @param id an identifier that can be used to track this image
* @param w the width at which the image is rendered
* @param h the height at which the image is rendered
*/
public synchronized void addImage(Image image, int id, int w, int h) {
addImageImpl(image, id, w, h);
Image rvImage = getResolutionVariant(image);
if (rvImage != null) {
addImageImpl(rvImage, id, w == -1 ? -1 : 2 * w, h == -1 ? -1 : 2 * h);
}
}
use of java.awt.image in project jdk8u_jdk by JetBrains.
the class BeansAppletStub method getImage.
public synchronized Image getImage(URL url) {
Object o = imageCache.get(url);
if (o != null) {
return (Image) o;
}
try {
o = url.getContent();
if (o == null) {
return null;
}
if (o instanceof Image) {
imageCache.put(url, o);
return (Image) o;
}
// Otherwise it must be an ImageProducer.
Image img = target.createImage((java.awt.image.ImageProducer) o);
imageCache.put(url, img);
return img;
} catch (Exception ex) {
return null;
}
}
Aggregations