root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / saveraster / operations / RasterizerLayer.java @ 11975
History | View | Annotate | Download (8.61 KB)
1 | 11267 | nacho | /* 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.cresques.filter.RasterBuf; |
||
28 | 11307 | nacho | import org.gvsig.gui.beans.incrementabletask.IIncrementable; |
29 | 11283 | nacho | import org.gvsig.raster.dataset.IDataWriter; |
30 | 11267 | nacho | |
31 | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
||
32 | 11307 | nacho | import com.iver.andami.PluginServices; |
33 | 11267 | nacho | import com.iver.cit.gvsig.fmap.ViewPort; |
34 | import com.iver.cit.gvsig.fmap.layers.FLayers; |
||
35 | import com.iver.cit.gvsig.fmap.layers.FLyrRaster; |
||
36 | import com.iver.utiles.swing.threads.Cancellable; |
||
37 | /**
|
||
38 | 11975 | bsanchez | * Sirve datos solicitados por los drivers que salvan a raster. Hereda de
|
39 | * Rasterizer y reescribe el m?todo readData que es el que ser? llamado desde el
|
||
40 | * driver cada vez que vacie el buffer y necesite m?s datos.
|
||
41 | *
|
||
42 | * @version 04/06/2007
|
||
43 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
44 | 11267 | nacho | */
|
45 | 11975 | bsanchez | public class RasterizerLayer implements IDataWriter, IIncrementable { |
46 | 11267 | nacho | |
47 | private ViewPort viewPort = null; |
||
48 | private ViewPort viewPortBlock = null; |
||
49 | private FLayers flayers = null; |
||
50 | private Color backgroundColor = null; |
||
51 | private boolean firstRead = true; |
||
52 | 11975 | bsanchez | private int nBlocks = 0; |
53 | private double percentMax = 100.0D; |
||
54 | protected double wcIntervalo = 0; |
||
55 | protected Dimension dimension = null; |
||
56 | protected int blockSize = 0; |
||
57 | protected double wcAlto = 0; |
||
58 | protected int lastBlock = 0; |
||
59 | protected BufferedImage image = null; |
||
60 | protected int[] rasterData = null; |
||
61 | protected int contBlocks = 1; |
||
62 | protected int percent = 0; |
||
63 | protected int imgHeight = 0; |
||
64 | |||
65 | 11267 | nacho | /**
|
66 | * Calculo del viewPort
|
||
67 | * @param vp
|
||
68 | */
|
||
69 | 11975 | bsanchez | private void calcViewPort(ViewPort vp) { |
70 | 11267 | nacho | Rectangle2D ext = null; |
71 | |||
72 | 11975 | bsanchez | if (viewPortBlock == null) { |
73 | 11267 | nacho | ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
74 | vp.getExtent().getMaxY() - wcIntervalo, |
||
75 | vp.getExtent().getWidth(), |
||
76 | wcIntervalo |
||
77 | ); |
||
78 | 11975 | bsanchez | } else {
|
79 | 11267 | nacho | ext = new Rectangle2D.Double( vp.getExtent().getMinX(), |
80 | vp.getExtent().getMinY() - wcIntervalo, |
||
81 | vp.getExtent().getWidth(), |
||
82 | wcIntervalo |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | viewPortBlock = new ViewPort(vp.getProjection());
|
||
87 | viewPortBlock.setExtent(ext); |
||
88 | viewPortBlock.setImageSize(dimension); |
||
89 | viewPortBlock.refreshExtent(); |
||
90 | } |
||
91 | |||
92 | /**
|
||
93 | * Constructor
|
||
94 | * @param flyrs capas
|
||
95 | * @param vp viewport
|
||
96 | 11307 | nacho | * @param blockSize altura del bloque que se lee de una vez en la imagen de entrada
|
97 | 11267 | nacho | * @param mapCtrl Mapcontrol
|
98 | */
|
||
99 | 11325 | nacho | public RasterizerLayer(FLayers flyrs, ViewPort vp, int blockSize) { |
100 | 11307 | nacho | this.blockSize = blockSize;
|
101 | 11267 | nacho | backgroundColor = vp.getBackColor(); |
102 | viewPort = new ViewPort(vp.getProjection());
|
||
103 | viewPort.setImageSize(vp.getImageSize()); |
||
104 | viewPort.setExtent(vp.getExtent()); |
||
105 | |||
106 | 11975 | bsanchez | // Calculo del viewPort del primer bloque
|
107 | 11267 | nacho | |
108 | wcAlto = viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY(); |
||
109 | 11307 | nacho | wcIntervalo = (blockSize * wcAlto) / viewPort.getImageHeight(); |
110 | dimension = new Dimension(viewPort.getImageWidth(), blockSize); |
||
111 | 11267 | nacho | |
112 | 11521 | nacho | imgHeight = vp.getImageHeight(); |
113 | 11975 | bsanchez | nBlocks = (int) (vp.getImageHeight() / blockSize);
|
114 | |||
115 | // Tama?o de ?ltimo bloque en pixeles
|
||
116 | 11307 | nacho | lastBlock = vp.getImageHeight() - (nBlocks * blockSize); |
117 | 11267 | nacho | |
118 | calcViewPort(viewPort); |
||
119 | |||
120 | this.flayers = flyrs;
|
||
121 | } |
||
122 | |||
123 | /**
|
||
124 | * Compatibilidad con el piloto de raster
|
||
125 | * @see readData
|
||
126 | */
|
||
127 | public int[] readARGBData(int sX, int sY, int nBand){ |
||
128 | return readData( sX, sY, nBand);
|
||
129 | } |
||
130 | 11975 | bsanchez | |
131 | 11267 | nacho | public int[] readData(int sX, int sY, int nBand) { |
132 | 11975 | bsanchez | if (nBand == 0) { // Con nBand==0 se devuelven las 3 bandas |
133 | nBlocks = (int) Math.ceil(imgHeight / (double) blockSize); |
||
134 | 11267 | nacho | image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB); |
135 | Graphics2D g = (Graphics2D) image.getGraphics(); |
||
136 | 11975 | bsanchez | BufferedImage img = new BufferedImage(viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight(), BufferedImage.TYPE_INT_ARGB); |
137 | 11267 | nacho | g.setColor(backgroundColor); |
138 | g.fillRect(0, 0, viewPortBlock.getImageWidth(), viewPortBlock.getImageHeight()); |
||
139 | 11975 | bsanchez | try {
|
140 | // TODO: FUNCIONALIDAD: Salvar los m?ximos y m?nimos para salvar 16 bits
|
||
141 | |||
142 | // Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
||
143 | // de realce si la imagen es de 16 bits.
|
||
144 | if (firstRead) {
|
||
145 | for (int i = 0; i < flayers.getLayersCount(); i++) { |
||
146 | if (flayers.getLayer(i) instanceof FLyrRaster) { |
||
147 | FLyrRaster raster = (FLyrRaster) flayers.getLayer(i); |
||
148 | if (raster.getSource().getDataType() == RasterBuf.TYPE_SHORT || raster.getSource().getDataType() == RasterBuf.TYPE_USHORT) {
|
||
149 | 11267 | nacho | //Statistic stats = raster.getSource().getFilterStack().getStats();
|
150 | //stats.history.add(stats.new History(raster.getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue));
|
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | firstRead = false;
|
||
155 | } |
||
156 | |||
157 | Cancellable cancel = new Cancellable() {
|
||
158 | public boolean isCanceled() { |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | public void setCanceled(boolean canceled) {} |
||
163 | }; |
||
164 | |||
165 | flayers.setDirty(true);
|
||
166 | flayers.draw(image, g, viewPortBlock, cancel, flayers.getMapContext().getScaleView()); |
||
167 | |||
168 | 11975 | bsanchez | // Si es el ?ltimo bloque vaciamos el historial de m?ximos y m?nimos
|
169 | if ((contBlocks + 1) == nBlocks) { |
||
170 | for (int i = 0; i < flayers.getLayersCount(); i++) { |
||
171 | if (flayers.getLayer(i) instanceof FLyrRaster) { |
||
172 | FLyrRaster raster = (FLyrRaster) flayers.getLayer(i); |
||
173 | if (raster.getSource().getDataType() == RasterBuf.TYPE_SHORT || raster.getSource().getDataType() == RasterBuf.TYPE_USHORT) {
|
||
174 | 11267 | nacho | raster.getSource().getFilterStack().getStats().history.clear(); |
175 | //Statistic stats = raster.getSource().getFilterStack().getStats();
|
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | |||
181 | } catch (ReadDriverException e) {
|
||
182 | e.printStackTrace(); |
||
183 | } |
||
184 | rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX); |
||
185 | |||
186 | 11975 | bsanchez | // Calculamos el viewPort del sgte bloque
|
187 | 11267 | nacho | |
188 | 11975 | bsanchez | if (((contBlocks + 1) * blockSize) <= viewPort.getImageHeight()) |
189 | 11267 | nacho | dimension = new Dimension(sX, sY); |
190 | 11975 | bsanchez | else { // Calculo de la altura del ?ltimo bloque |
191 | dimension = new Dimension(sX, (int) (viewPort.getImageHeight() - (contBlocks * blockSize))); |
||
192 | wcIntervalo = (lastBlock * wcAlto) / viewPort.getImageHeight(); |
||
193 | 11267 | nacho | } |
194 | |||
195 | calcViewPort(viewPortBlock); |
||
196 | |||
197 | 11975 | bsanchez | percent = (int) ((100 * (contBlocks)) / nBlocks); |
198 | contBlocks++; |
||
199 | |||
200 | 11267 | nacho | return rasterData;
|
201 | } |
||
202 | |||
203 | return null; |
||
204 | } |
||
205 | 11975 | bsanchez | |
206 | 11267 | nacho | /**
|
207 | 11975 | bsanchez | * Asigna el ancho del bloque
|
208 | * @param sizeBlock Ancho del bloque en pixeles
|
||
209 | */
|
||
210 | 11307 | nacho | public void setBlockSize(int blockSize) { |
211 | this.blockSize = blockSize;
|
||
212 | 11267 | nacho | } |
213 | |||
214 | 11283 | nacho | /**
|
215 | * No tiene uso en RasterizerLayer
|
||
216 | */
|
||
217 | public byte[][] readByteData(int sizeX, int sizeY) { |
||
218 | return null; |
||
219 | } |
||
220 | |||
221 | /**
|
||
222 | * No tiene uso en RasterizerLayer
|
||
223 | */
|
||
224 | public double[][] readDoubleData(int sizeX, int sizeY) { |
||
225 | return null; |
||
226 | } |
||
227 | |||
228 | /**
|
||
229 | * No tiene uso en RasterizerLayer
|
||
230 | */
|
||
231 | public float[][] readFloatData(int sizeX, int sizeY) { |
||
232 | return null; |
||
233 | } |
||
234 | |||
235 | /**
|
||
236 | * No tiene uso en RasterizerLayer
|
||
237 | */
|
||
238 | public int[][] readIntData(int sizeX, int sizeY) { |
||
239 | return null; |
||
240 | } |
||
241 | |||
242 | /**
|
||
243 | * No tiene uso en RasterizerLayer
|
||
244 | */
|
||
245 | public short[][] readShortData(int sizeX, int sizeY) { |
||
246 | return null; |
||
247 | } |
||
248 | 11975 | bsanchez | |
249 | 11307 | nacho | /*
|
250 | 11975 | bsanchez | * (non-Javadoc)
|
251 | 11307 | nacho | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getTitle()
|
252 | */
|
||
253 | public String getTitle() { |
||
254 | return PluginServices.getText(this, "salvando_raster"); |
||
255 | } |
||
256 | 11283 | nacho | |
257 | 11307 | nacho | /*
|
258 | 11975 | bsanchez | * (non-Javadoc)
|
259 | 11307 | nacho | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLog()
|
260 | */
|
||
261 | public String getLog() { |
||
262 | return PluginServices.getText(this, "salvando_bloque") + " " + Math.min(nBlocks, contBlocks) + " " + PluginServices.getText(this, "de") + " " + nBlocks; |
||
263 | } |
||
264 | |||
265 | /*
|
||
266 | 11975 | bsanchez | * (non-Javadoc)
|
267 | 11307 | nacho | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getLabel()
|
268 | */
|
||
269 | public String getLabel() { |
||
270 | 11647 | nacho | return PluginServices.getText(this, "rasterizando") + "..."; |
271 | 11307 | nacho | } |
272 | |||
273 | /*
|
||
274 | 11975 | bsanchez | * (non-Javadoc)
|
275 | 11307 | nacho | * @see org.gvsig.gui.beans.incrementabletask.IIncrementable#getPercent()
|
276 | */
|
||
277 | public int getPercent() { |
||
278 | return percent;
|
||
279 | } |
||
280 | 11267 | nacho | } |