1 /* Copyright (C) 2005-2011 Fabio Riccardi */
2 
3 package com.lightcrafts.jai.opimage;
4 
5 import com.lightcrafts.jai.JAIContext;
6 import com.lightcrafts.jai.utils.Functions;
7 
8 import com.lightcrafts.mediax.jai.PlanarImage;
9 import com.lightcrafts.mediax.jai.TileCache;
10 import com.lightcrafts.mediax.jai.ImageLayout;
11 import com.lightcrafts.mediax.jai.RasterFactory;
12 import java.awt.image.*;
13 import java.awt.*;
14 
15 /**
16  * Created by IntelliJ IDEA.
17  * User: fabio
18  * Date: Oct 25, 2005
19  * Time: 8:43:49 AM
20  * To change this template use File | Settings | File Templates.
21  */
22 public class CachedImage extends PlanarImage {
23     private final TileCache cache;
24 
25     // Provide an easy mechanism to generate sRGB images, workaround for a rendering bug on windogs
getsRGBImageLayout(PlanarImage image)26     static ImageLayout getsRGBImageLayout(PlanarImage image) {
27         ImageLayout layout = new ImageLayout(image);
28         layout.setColorModel(new ComponentColorModel(JAIContext.sRGBColorSpace, false, false,
29                                                      Transparency.OPAQUE, DataBuffer.TYPE_BYTE));
30         return layout;
31     }
32 
33     // never create instances directly, go through cacheImage()
CachedImage(PlanarImage image, TileCache cache)34     public CachedImage(PlanarImage image, TileCache cache) {
35         this(image, cache, false);
36         setProperty(JAIContext.PERSISTENT_CACHE_TAG, Boolean.TRUE);
37     }
38 
CachedImage(PlanarImage image, TileCache cache, boolean castTosRGB)39     public CachedImage(PlanarImage image, TileCache cache, boolean castTosRGB) {
40         super(castTosRGB ? getsRGBImageLayout(image) : new ImageLayout(image), null, null);
41         setProperty(JAIContext.PERSISTENT_CACHE_TAG, Boolean.TRUE);
42         this.cache = cache;
43         cache.addTiles(this, image.getTileIndices(image.getBounds()), image.getTiles(), null);
44     }
45 
CachedImage(ImageLayout layout, TileCache cache)46     public CachedImage(ImageLayout layout, TileCache cache) {
47         super(layout, null, null);
48         setProperty(JAIContext.PERSISTENT_CACHE_TAG, Boolean.TRUE);
49         this.cache = cache;
50     }
51 
addTile(int tileX, int tileY)52     private synchronized WritableRaster addTile(int tileX, int tileY) {
53         WritableRaster raster = RasterFactory.createWritableRaster(getSampleModel(), new Point(tileX * getTileWidth(),
54                                                                                                tileY * getTileHeight()));
55         cache.add(this, tileX, tileY, raster);
56         return raster;
57     }
58 
getTile(int tileX, int tileY)59     public Raster getTile(int tileX, int tileY) {
60         return cache.getTile(this, tileX, tileY);
61     }
62 
getWritableTile(int tileX, int tileY)63     public synchronized WritableRaster getWritableTile(int tileX, int tileY) {
64         Raster raster = cache.getTile(this, tileX, tileY);
65         if (raster == null)
66             raster = addTile(tileX, tileY);
67         return (WritableRaster) raster;
68     }
69 
setData(Raster r)70     public synchronized void setData(Raster r) {
71         // Return if the intersection of the image and Raster bounds is empty.
72         Rectangle rBounds = r.getBounds();
73         if((rBounds = rBounds.intersection(getBounds())).isEmpty()) {
74             return;
75         }
76 
77         // Set tile index limits.
78         int txMin = XToTileX(rBounds.x);
79         int tyMin = YToTileY(rBounds.y);
80         int txMax = XToTileX(rBounds.x + rBounds.width - 1);
81         int tyMax = YToTileY(rBounds.y + rBounds.height - 1);
82 
83         for(int ty = tyMin; ty <= tyMax; ty++) {
84             for(int tx = txMin; tx <= txMax; tx++) {
85                 WritableRaster wr = getWritableTile(tx, ty);
86                 if(wr != null)
87                     Functions.copyData(wr, r);
88             }
89         }
90     }
91 /*
92     public static PlanarImage cacheImage(PlanarImage image, TileCache cache) {
93         return cacheImage(image, cache, false);
94     }
95 
96     public static PlanarImage cacheImage(PlanarImage image, TileCache cache, boolean castTosRGB) {
97         if (image instanceof CachedImage)
98             return image;
99         else
100             return new CachedImage(image, cache, castTosRGB);
101     }
102 */
103 }
104