Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCacheService / src / org / gvsig / cacheservice / RasterCacheService.java @ 18218

History | View | Annotate | Download (4.71 KB)

1
package org.gvsig.cacheservice;
2

    
3
import java.awt.Image;
4
import java.awt.Point;
5
import java.awt.geom.Rectangle2D;
6
import java.awt.image.RenderedImage;
7
import java.io.File;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.net.MalformedURLException;
11

    
12
import javax.imageio.ImageIO;
13

    
14
import com.sun.media.jai.codec.ImageCodec;
15
import com.sun.media.jai.codec.ImageEncoder;
16
import com.sun.media.jai.codec.PNGEncodeParam;
17
import com.sun.media.jai.codec.TIFFEncodeParam;
18

    
19
//import javax.imageio.ImageIO;
20

    
21
public abstract class RasterCacheService extends CacheService {
22

    
23
        private String _ext = ".jpg";
24

    
25
        public RasterCacheService(String planet, String name) {
26
                super(planet, name);
27
                // TODO Auto-generated constructor stub
28
        }
29

    
30
        /**
31
         * Obtains the full path of the file associated to the given tilenum and the
32
         * current rastercacheservice configuration.
33
         * 
34
         * @param tileNum
35
         * @return Full path to the filename.
36
         */
37
        protected String getTileFileName(TileNum tileNum) {
38
                String tileId = tileNum.getTileId(getTileIdType());
39

    
40
                String fileName = getCacheDir();
41
                // Adds HasdDir level to cache image file path.
42
                if (isWithHashDirs()) {
43
                        Point num = tileNum.getNum();
44
                        fileName += getHashDir(num) + "/";
45
                }
46

    
47
                return fileName + tileId + getFileExtension();
48
        }
49

    
50
        /**
51
         * Process the tile an returns the image.
52
         * 
53
         * @param tileNum
54
         * @return The image of the.
55
         * @throws CacheServiceException
56
         */
57
        abstract public Image getTileAsImage(TileNum tileNum, Rectangle2D extent)
58
                        throws CacheServiceException;
59

    
60
        public Image getTileAsImage(int level, Point num, Rectangle2D extent)
61
                        throws CacheServiceException {
62
                return getTileAsImage(new TileNum(level, num), extent);
63
        }
64

    
65
        /**
66
         * devuelve el nombre del fichero en el que se salv� el tile.
67
         * 
68
         * @param tileNum
69
         * @return path completo del fichero de tile.
70
         * @throws MalformedURLException
71
         * @throws IOException
72
         */
73
        abstract public String getTileAsFName(TileNum tileNum, Rectangle2D extent)
74
                        throws CacheServiceException;
75

    
76
        /**
77
         * devuelve el nombre del fichero en el que se salv� el tile.
78
         * 
79
         * @param level
80
         *            nivel de zoom
81
         * @param num
82
         *            posici�n del tile
83
         * @return path completo del fichero de tile.
84
         * @throws MalformedURLException
85
         * @throws IOException
86
         */
87
        public String getTileAsFName(int level, Point num, Rectangle2D extent)
88
                        throws CacheServiceException {
89
                return getTileAsFName(new TileNum(level, num), extent);
90
        }
91

    
92
        public Image getImageFromCachedFile(String fName) throws IOException {
93
                File cacheFile = new File(fName);
94
                return ImageIO.read(cacheFile);
95
        }
96

    
97
        public void saveCachedFile(Image img, String format, String fName)
98
                        throws IOException {
99
                // ImageIO.write((RenderedImage) img, format, new File(fName));
100
                File file = new File(fName);
101
                FileOutputStream out = new FileOutputStream(file);
102

    
103
                if (format.equals("png")) {
104
                        PNGEncodeParam params = new PNGEncodeParam.RGB();
105
                        // params.setsetCompression(TIFFEncodeParam.COMPRESSION_NONE);
106
                        ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out,
107
                                        params);
108
                        if (encoder == null) {
109
                                System.out.println("imageEncoder is null");
110
                                System.exit(0);
111
                        }
112
                        encoder.encode((RenderedImage) img);
113
                } else if (format.equals("tif")) {
114
                        TIFFEncodeParam params = new TIFFEncodeParam();
115
                        params.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
116
                        ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
117
                                        params);
118
                        if (encoder == null) {
119
                                System.out.println("imageEncoder is null");
120
                                System.exit(0);
121
                        }
122
                        encoder.encode((RenderedImage) img);
123
                }
124

    
125
        }
126

    
127
        public void saveCachedFile(Image img, String format, File f)
128
                        throws IOException {
129
//                ImageIO.write((RenderedImage) img, format, f);
130
//                File file = new File(fName);
131
                FileOutputStream out = new FileOutputStream(f);
132

    
133
                if (format.equals("png")) {
134
                        PNGEncodeParam params = new PNGEncodeParam.RGB();
135
                        // params.setsetCompression(TIFFEncodeParam.COMPRESSION_NONE);
136
                        ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out,
137
                                        params);
138
                        if (encoder == null) {
139
                                System.out.println("imageEncoder is null");
140
                                System.exit(0);
141
                        }
142
                        encoder.encode((RenderedImage) img);
143
                } else if (format.equals("tif")) {
144
                        TIFFEncodeParam params = new TIFFEncodeParam();
145
                        params.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
146
                        ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
147
                                        params);
148
                        if (encoder == null) {
149
                                System.out.println("imageEncoder is null");
150
                                System.exit(0);
151
                        }
152
                        encoder.encode((RenderedImage) img);
153
                }
154

    
155
        }
156

    
157
        /**
158
         * @return Returns the ext.
159
         */
160
        public String getFileExtension() {
161
                // TODO Auto-generated method stub
162
                return _ext;
163
        }
164

    
165
        /**
166
         * @param ext
167
         *            The ext to set.
168
         */
169
        public void setFileExtension(String ext) {
170
                _ext = ext;
171
        }
172

    
173
}