Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / saveraster / operations / RasterizerLayer.java @ 29718

History | View | Annotate | Download (9.52 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*/
19
package org.gvsig.rastertools.saveraster.operations;
20

    
21
import java.awt.Color;
22
import java.awt.Dimension;
23
import java.awt.Graphics2D;
24
import java.awt.geom.Rectangle2D;
25
import java.awt.image.BufferedImage;
26

    
27
import org.gvsig.gui.beans.incrementabletask.IIncrementable;
28
import org.gvsig.raster.dataset.IDataWriter;
29
import org.gvsig.raster.process.RasterTask;
30
import org.gvsig.raster.process.RasterTaskQueue;
31

    
32
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
33
import com.iver.andami.PluginServices;
34
import com.iver.andami.messages.NotificationManager;
35
import com.iver.cit.gvsig.fmap.DefaultMapContextDrawer;
36
import com.iver.cit.gvsig.fmap.ViewPort;
37
import com.iver.cit.gvsig.fmap.layers.FLayers;
38
import com.iver.utiles.swing.threads.Cancellable;
39
/**
40
 * Sirve datos solicitados por los drivers que salvan a raster. Hereda de
41
 * Rasterizer y reescribe el m?todo readData que es el que ser? llamado desde el
42
 * driver cada vez que vacie el buffer y necesite m?s datos.
43
 *
44
 * @version 04/06/2007
45
 * @author Nacho Brodin (nachobrodin@gmail.com)
46
 */
47
public class RasterizerLayer implements IDataWriter, IIncrementable {
48

    
49
        private ViewPort                                viewPort = null;
50
        private ViewPort                                viewPortBlock = null;
51
        private FLayers                                        flayers = null;
52
        private Color                                        backgroundColor = null;
53
        private boolean                                        firstRead = true;
54
        private int                                         nBlocks = 0;
55
//        private double                                         percentMax = 100.0D;
56
        protected double                                 wcIntervalo = 0;
57
        protected Dimension                         dimension = null;
58
        protected int                                         blockSize = 0;
59
        protected double                                 wcAlto = 0;
60
        protected int                                         lastBlock = 0;
61
        protected BufferedImage                 image = null;
62
        protected int[]                                 rasterData = null;
63
        protected int                                         contBlocks = 1;
64
        protected int                                        percent = 0;
65
        protected int                                        imgHeight = 0;
66

    
67
        /**
68
         * Calculo del viewPort
69
         * @param vp
70
         */
71
        private void calcViewPort(ViewPort vp) {
72
                Rectangle2D ext = null;
73

    
74
                if (viewPortBlock == null) {
75
                        ext = new Rectangle2D.Double(        vp.getExtent().getMinX(),
76
                                                                                        vp.getExtent().getMaxY() - wcIntervalo,
77
                                                                                        vp.getExtent().getWidth(),
78
                                                                                        wcIntervalo
79
                                                                                );
80
                } else {
81
                        ext = new Rectangle2D.Double(        vp.getExtent().getMinX(),
82
                                                                                        vp.getExtent().getMinY() - wcIntervalo,
83
                                                                                        vp.getExtent().getWidth(),
84
                                                                                        wcIntervalo
85
                                                                                );
86
                }
87

    
88
                viewPortBlock = new ViewPort(vp.getProjection());
89
                viewPortBlock.setExtent(ext);
90
                viewPortBlock.setImageSize(dimension);
91
                viewPortBlock.refreshExtent();
92
        }
93

    
94
        /**
95
         * Constructor
96
         * @param flyrs capas
97
         * @param vp viewport
98
         * @param blockSize altura del bloque que se lee de una vez en la imagen de entrada
99
         * @param mapCtrl Mapcontrol
100
         */
101
        public RasterizerLayer(FLayers flyrs, ViewPort vp, int blockSize) {
102
                this.blockSize = blockSize;
103
                backgroundColor = vp.getBackColor();
104
                viewPort = new ViewPort(vp.getProjection());
105
                viewPort.setImageSize(vp.getImageSize());
106
                viewPort.setExtent(vp.getExtent());
107

    
108
                // Calculo del viewPort del primer bloque
109

    
110
                wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY();
111
                wcIntervalo = (blockSize * wcAlto) / viewPort.getImageHeight();
112
                dimension = new Dimension(viewPort.getImageWidth(), blockSize);
113

    
114
                imgHeight = vp.getImageHeight();
115
                nBlocks = (int) (vp.getImageHeight() / blockSize);
116

    
117
                // Tama?o de ?ltimo bloque en pixeles
118
                lastBlock = vp.getImageHeight() - (nBlocks * blockSize);
119

    
120
                calcViewPort(viewPort);
121

    
122
                this.flayers = flyrs;
123
        }
124

    
125
        /**
126
         * Compatibilidad con el piloto de raster
127
         * @throws InterruptedException 
128
         * @see readData
129
         */
130
        public int[] readARGBData(int sX, int sY, int nBand) throws InterruptedException, OutOfMemoryError {
131
                return readData( sX, sY, nBand);
132
        }
133

    
134
        public int[] readData(int sX, int sY, int nBand) throws InterruptedException, OutOfMemoryError {
135
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
136
                if (nBand == 0) { // Con nBand==0 se devuelven las 3 bandas
137
                        nBlocks = (int) Math.ceil(imgHeight / (double) blockSize);
138
                        image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB);
139
                        Graphics2D g = (Graphics2D) image.getGraphics();
140
                        g.setColor(backgroundColor);
141
                        g.fillRect(0, 0, viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight());
142
                        
143
                        if(task.getEvent() != null)
144
                                task.manageEvent(task.getEvent());
145
                        
146
                        try {
147
                                // TODO: FUNCIONALIDAD: Salvar los m?ximos y m?nimos para salvar 16 bits
148

    
149
                                // Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
150
                                // de realce si la imagen es de 16 bits.
151
//                                if (firstRead) {
152
//                                        for (int i = 0; i < flayers.getLayersCount(); i++) {
153
//                                                if (flayers.getLayer(i) instanceof FLyrRasterSE) {
154
//                                                        FLyrRasterSE raster = (FLyrRasterSE) flayers.getLayer(i);
155
//                                                        if (raster.getDataType()[0] == IBuffer.TYPE_SHORT || raster.getDataType()[0] == IBuffer.TYPE_USHORT) {
156
//                                                                Statistic stats = raster.getSource().getFilterStack().getStats();
157
//                                                                stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue));
158
//                                                        }
159
//                                                }
160
//                                        }
161
//                                        firstRead = false;
162
//                                }
163

    
164
                                Cancellable cancel = new Cancellable() {
165
                                        public boolean isCanceled() {
166
                                                return false;
167
                                        }
168

    
169
                                        public void setCanceled(boolean canceled) {}
170
                                };
171

    
172
                                DefaultMapContextDrawer mapContextDrawer = new DefaultMapContextDrawer();
173
                                mapContextDrawer.setMapContext(flayers.getMapContext());
174
                                mapContextDrawer.setViewPort(viewPortBlock);
175
                                if(task.getEvent() != null)
176
                                        task.manageEvent(task.getEvent());
177
                                mapContextDrawer.draw(flayers, image, g, cancel, flayers.getMapContext().getScaleView());
178

    
179
                                // Si es el ?ltimo bloque vaciamos el historial de m?ximos y m?nimos
180
//                                if ((contBlocks + 1) == nBlocks) {
181
//                                        for (int i = 0; i < flayers.getLayersCount(); i++) {
182
//                                                if (flayers.getLayer(i) instanceof FLyrRasterSE) {
183
//                                                        FLyrRasterSE raster = (FLyrRasterSE) flayers.getLayer(i);
184
//                                                        if (raster.getDataType()[0] == IBuffer.TYPE_SHORT || raster.getDataType()[0] == IBuffer.TYPE_USHORT) {
185
//                                                                raster.getDatasource().getFilterStack().getStats().history.clear();
186
//                                                                Statistic stats = raster.getSource().getFilterStack().getStats();
187
//                                                        }
188
//                                                }
189
//                                        }
190
//                                }
191

    
192
                        } catch (ReadDriverException e) {
193
                                NotificationManager.addError("Error en el draw de capa", e);
194
                        }
195
                        rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX);
196

    
197
                        // Calculamos el viewPort del sgte bloque
198

    
199
                        if (((contBlocks + 1) * blockSize) <= viewPort.getImageHeight())
200
                                dimension = new Dimension(sX, sY);
201
                        else { // Calculo de la altura del ?ltimo bloque
202
                                dimension = new Dimension(sX, (int) (viewPort.getImageHeight() - (contBlocks * blockSize)));
203
                                wcIntervalo = (lastBlock * wcAlto) / viewPort.getImageHeight();
204
                        }
205

    
206
                        if(task.getEvent() != null)
207
                                task.manageEvent(task.getEvent());
208
                        
209
                        calcViewPort(viewPortBlock);
210

    
211
                        percent = (int) ((100 * (contBlocks)) / nBlocks);
212
                        contBlocks++;
213

    
214
                        return rasterData;
215
                }
216

    
217
                return null;
218
        }
219

    
220
        /**
221
         * Asigna el ancho del bloque
222
         * @param sizeBlock Ancho del bloque en pixeles
223
         */
224
        public void setBlockSize(int blockSize) {
225
                this.blockSize = blockSize;
226
        }
227

    
228
        /**
229
         * No tiene uso en RasterizerLayer
230
         */
231
        public byte[][] readByteData(int sizeX, int sizeY) {
232
                return null;
233
        }
234

    
235
        /**
236
         * No tiene uso en RasterizerLayer
237
         */
238
        public double[][] readDoubleData(int sizeX, int sizeY) {
239
                return null;
240
        }
241

    
242
        /**
243
         * No tiene uso en RasterizerLayer
244
         */
245
        public float[][] readFloatData(int sizeX, int sizeY) {
246
                return null;
247
        }
248

    
249
        /**
250
         * No tiene uso en RasterizerLayer
251
         */
252
        public int[][] readIntData(int sizeX, int sizeY) {
253
                return null;
254
        }
255

    
256
        /**
257
         * No tiene uso en RasterizerLayer
258
         */
259
        public short[][] readShortData(int sizeX, int sizeY) {
260
                return null;
261
        }
262

    
263
        /*
264
         * (non-Javadoc)
265
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
266
         */
267
        public String getTitle() {
268
                return PluginServices.getText(this, "salvando_raster");
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
274
         */
275
        public String getLog() {
276
                return PluginServices.getText(this, "salvando_bloque") + " " + Math.min(nBlocks, contBlocks) + " " + PluginServices.getText(this, "de") + " " + nBlocks;
277
        }
278

    
279
        /*
280
         * (non-Javadoc)
281
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
282
         */
283
        public String getLabel() {
284
                return PluginServices.getText(this, "rasterizando") + "...";
285
        }
286

    
287
        /*
288
         * (non-Javadoc)
289
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
290
         */
291
        public int getPercent() {
292
                return percent;
293
        }
294

    
295
        /*
296
         * (non-Javadoc)
297
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#isCancelable()
298
         */
299
        public boolean isCancelable() {
300
                return true;
301
        }
302

    
303
        /*
304
         * (non-Javadoc)
305
         * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#isPausable()
306
         */
307
        public boolean isPausable() {
308
                return false;
309
        }
310
}