gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / viewexport / Export.java @ 1174
History | View | Annotate | Download (11.2 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.tools.app.basic.tool.viewexport; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.awt.geom.AffineTransform; |
26 |
import java.awt.image.BufferedImage; |
27 |
import java.io.File; |
28 |
import java.io.IOException; |
29 |
import java.util.Hashtable; |
30 |
import java.util.Iterator; |
31 |
|
32 |
import javax.swing.JFileChooser; |
33 |
import javax.swing.JOptionPane; |
34 |
import javax.swing.filechooser.FileFilter; |
35 |
|
36 |
import org.gvsig.andami.PluginServices; |
37 |
import org.gvsig.andami.messages.NotificationManager; |
38 |
import org.gvsig.andami.plugins.Extension; |
39 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
40 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
41 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
42 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
43 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
44 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
45 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
46 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
47 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
48 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
49 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
50 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
51 |
|
52 |
import com.sun.jimi.core.Jimi; |
53 |
import com.sun.jimi.core.JimiException; |
54 |
|
55 |
|
56 |
/**
|
57 |
* Extensi?n para exportar en algunos formatos raster la vista actual.
|
58 |
*
|
59 |
* @author Fernando Gonz?lez Cort?s
|
60 |
*/
|
61 |
public class Export extends Extension { |
62 |
private String lastPath = null; |
63 |
private Hashtable<String, MyFileFilter> cmsExtensionsSupported = null; |
64 |
private Hashtable<String, MyFileFilter> jimiExtensionsSupported = null; |
65 |
|
66 |
/**
|
67 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
68 |
*/
|
69 |
public boolean isEnabled() { |
70 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
71 |
|
72 |
if (f == null || !(f instanceof AbstractViewPanel)) { |
73 |
return false; |
74 |
} |
75 |
|
76 |
AbstractViewPanel avp = (AbstractViewPanel)f; |
77 |
FLayers layers = avp.getMapControl().getMapContext().getLayers(); |
78 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
79 |
if(layers.getLayer(i) != null) |
80 |
return layers.getLayer(i).isAvailable();
|
81 |
} |
82 |
return false; |
83 |
} |
84 |
|
85 |
/**
|
86 |
* @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
87 |
*/
|
88 |
public boolean isVisible() { |
89 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
90 |
if (f == null) { |
91 |
return false; |
92 |
} |
93 |
|
94 |
return (f instanceof AbstractViewPanel); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
99 |
*/
|
100 |
public void initialize() { |
101 |
} |
102 |
|
103 |
/* (non-Javadoc)
|
104 |
* @see com.iver.andami.plugins.Extension#postInitialize()
|
105 |
*/
|
106 |
public void postInitialize() { |
107 |
cmsExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
108 |
jimiExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
109 |
cmsExtensionsSupported.put("jpg", new MyFileFilter("jpg", |
110 |
PluginServices.getText(this, "jpg"), "cms")); |
111 |
jimiExtensionsSupported.put("png",new MyFileFilter("png", |
112 |
PluginServices.getText(this, "png"), "cms")); |
113 |
jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp", |
114 |
PluginServices.getText(this, "bmp"), "cms")); |
115 |
cmsExtensionsSupported.put("tif",new MyFileFilter("tif", |
116 |
PluginServices.getText(this, "tif"), "cms")); |
117 |
} |
118 |
|
119 |
public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws IOException, NotSupportedExtensionException, RasterDriverException { |
120 |
RasterManager manager = (RasterManager)RasterLocator.getManager(); |
121 |
DataServerWriter data = new RasterizerImage(srcImage);
|
122 |
RasterWriter writer = null;
|
123 |
try {
|
124 |
Params params = manager.createWriter(fileDst.getAbsolutePath()).getParams(); |
125 |
params.changeParamValue("blocksize", "7"); //posici?n 7 del array -> 512 |
126 |
params.changeParamValue("tfw", "false"); |
127 |
params.changeParamValue("interleave", new Integer(1));//posici?n 1 del array -> PIXEL |
128 |
writer = manager.createWriter(data, |
129 |
fileDst.getAbsolutePath(), |
130 |
3,
|
131 |
new AffineTransform(), |
132 |
srcImage.getWidth(), |
133 |
srcImage.getHeight(), |
134 |
Buffer.TYPE_IMAGE,
|
135 |
params, |
136 |
null);
|
137 |
if (writer == null) { |
138 |
PluginServices.getLogger().error("No supported Format: " + fileDst.getAbsolutePath());
|
139 |
return false; |
140 |
} |
141 |
} catch (NotSupportedExtensionException e) {
|
142 |
throw new RasterDriverException(""); |
143 |
} |
144 |
try {
|
145 |
writer.dataWrite(); |
146 |
} catch (ProcessInterruptedException e) {
|
147 |
} |
148 |
writer.writeClose(); |
149 |
return true; |
150 |
} |
151 |
|
152 |
|
153 |
|
154 |
public static boolean saveImageJimi(File fileDst,BufferedImage srcImage) throws Exception{ |
155 |
try {
|
156 |
|
157 |
Jimi.putImage(srcImage, fileDst.getAbsolutePath()); |
158 |
|
159 |
} catch (JimiException e) {
|
160 |
throw new Exception(fileDst.getAbsolutePath(),e); |
161 |
} |
162 |
return true; |
163 |
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
168 |
*/
|
169 |
public void execute(String actionCommand) { |
170 |
JFileChooser jfc = new JFileChooser(lastPath); |
171 |
|
172 |
jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); |
173 |
|
174 |
Iterator<MyFileFilter> iter = cmsExtensionsSupported.values().iterator();
|
175 |
while (iter.hasNext()){
|
176 |
jfc.addChoosableFileFilter((FileFilter)iter.next());
|
177 |
} |
178 |
|
179 |
iter = jimiExtensionsSupported.values().iterator(); |
180 |
while (iter.hasNext()){
|
181 |
jfc.addChoosableFileFilter((FileFilter)iter.next());
|
182 |
} |
183 |
|
184 |
jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png")); |
185 |
if (jfc.showSaveDialog(
|
186 |
(Component) PluginServices.getMainFrame()
|
187 |
) == JFileChooser.APPROVE_OPTION) {
|
188 |
|
189 |
BufferedImage tempImage;
|
190 |
|
191 |
tempImage = ((AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow()).getImage(); |
192 |
|
193 |
File f = jfc.getSelectedFile();
|
194 |
|
195 |
lastPath = f.getParent(); |
196 |
|
197 |
MyFileFilter filter = (MyFileFilter)jfc.getFileFilter(); |
198 |
f = filter.normalizeExtension(f); |
199 |
|
200 |
|
201 |
if (f.exists()){
|
202 |
int resp = JOptionPane.showConfirmDialog( |
203 |
(Component) PluginServices.getMainFrame(),
|
204 |
PluginServices.getText(this,
|
205 |
"fichero_ya_existe_seguro_desea_guardarlo")+
|
206 |
"\n"+
|
207 |
f.getAbsolutePath(), |
208 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
209 |
if (resp != JOptionPane.YES_OPTION) { |
210 |
return;
|
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
if (filter.getInfo().equalsIgnoreCase("cms")){ |
216 |
|
217 |
try {
|
218 |
saveImageCMS(f, tempImage); |
219 |
} catch (IOException e) { |
220 |
NotificationManager.addError("Error exportando la imagen", e);
|
221 |
} catch (NotSupportedExtensionException e) {
|
222 |
NotificationManager.addError("Error exportando la imagen: formato no soportado", e);
|
223 |
} catch (RasterDriverException e) {
|
224 |
NotificationManager.addError("Error exportando la imagen", e);
|
225 |
} |
226 |
}else if (filter.getInfo().equalsIgnoreCase("jimi")) { |
227 |
try {
|
228 |
saveImageJimi(f, tempImage); |
229 |
} catch (Exception e) { |
230 |
NotificationManager.addError("Error exportando la imagen", e);
|
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
|
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Servidor de datos desde un BufferedImage. Cada petici?n es de un tama?o
|
242 |
* de bloque indicado por el escritor en los par?metros de la llamada por lo
|
243 |
* que en la siguiente petici?n habr? que escribir el bloque siguiente.
|
244 |
*/
|
245 |
class RasterizerImage implements DataServerWriter { |
246 |
private BufferedImage source = null; |
247 |
private int[] data = null; |
248 |
private int y = 0; |
249 |
|
250 |
public RasterizerImage(BufferedImage source) { |
251 |
this.source = source;
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* Solo necesita implementar el m?todo RGB porque solo se
|
256 |
* utiliza para exportar la vista.
|
257 |
*/
|
258 |
public int[] readARGBData(int sX, int sY, int nBand){ |
259 |
return readData( sX, sY, nBand);
|
260 |
} |
261 |
|
262 |
public int[] readData(int sizeX, int sizeY, int nBand) { |
263 |
if(nBand == 0) { //Con nBand==0 se devuelven las 3 bandas |
264 |
this.data = this.source.getRGB(0, y, sizeX, sizeY, this.data, 0, sizeX); |
265 |
y += sizeY; |
266 |
return this.data; |
267 |
} |
268 |
return null; |
269 |
} |
270 |
|
271 |
public byte[][] readByteData(int sizeX, int sizeY) { |
272 |
return null; |
273 |
} |
274 |
|
275 |
public double[][] readDoubleData(int sizeX, int sizeY) { |
276 |
return null; |
277 |
} |
278 |
|
279 |
public float[][] readFloatData(int sizeX, int sizeY) { |
280 |
return null; |
281 |
} |
282 |
|
283 |
public int[][] readIntData(int sizeX, int sizeY) { |
284 |
return null; |
285 |
} |
286 |
|
287 |
public short[][] readShortData(int sizeX, int sizeY) { |
288 |
return null; |
289 |
} |
290 |
|
291 |
public int getPercent() { |
292 |
return 0; |
293 |
} |
294 |
|
295 |
public void setAlphaBuffer(Buffer alphaBuffer) { |
296 |
|
297 |
} |
298 |
|
299 |
public void setBuffer(Buffer buffer, int nband) { |
300 |
|
301 |
} |
302 |
|
303 |
public Buffer getBuffer() { |
304 |
return null; |
305 |
} |
306 |
|
307 |
public void setBand(int nband) { |
308 |
|
309 |
} |
310 |
|
311 |
public void dispose() { |
312 |
// TODO Auto-generated method stub
|
313 |
|
314 |
} |
315 |
|
316 |
public Buffer getSource() { |
317 |
return null; |
318 |
} |
319 |
|
320 |
public void setPercent(int value) { |
321 |
// TODO Auto-generated method stub
|
322 |
|
323 |
} |
324 |
} |
325 |
|
326 |
class MyFileFilter extends FileFilter{ |
327 |
|
328 |
private String[] extensiones = new String[1]; |
329 |
private String description = null; |
330 |
private boolean dirs = true; |
331 |
private String info = null; |
332 |
|
333 |
public MyFileFilter(String[] ext, String desc) { |
334 |
extensiones = ext; |
335 |
description = desc; |
336 |
} |
337 |
|
338 |
public MyFileFilter(String[] ext, String desc,String info) { |
339 |
extensiones = ext; |
340 |
description = desc; |
341 |
this.info = info;
|
342 |
} |
343 |
|
344 |
public MyFileFilter(String ext, String desc) { |
345 |
extensiones[0] = ext;
|
346 |
description = desc; |
347 |
} |
348 |
|
349 |
public MyFileFilter(String ext, String desc,String info) { |
350 |
extensiones[0] = ext;
|
351 |
description = desc; |
352 |
this.info = info;
|
353 |
} |
354 |
|
355 |
public MyFileFilter(String ext, String desc, boolean dirs) { |
356 |
extensiones[0] = ext;
|
357 |
description = desc; |
358 |
this.dirs = dirs;
|
359 |
} |
360 |
|
361 |
public MyFileFilter(String ext, String desc, boolean dirs,String info) { |
362 |
extensiones[0] = ext;
|
363 |
description = desc; |
364 |
this.dirs = dirs;
|
365 |
this.info = info;
|
366 |
} |
367 |
|
368 |
public boolean accept(File f) { |
369 |
if (f.isDirectory()) {
|
370 |
if (dirs) {
|
371 |
return true; |
372 |
} else {
|
373 |
return false; |
374 |
} |
375 |
} |
376 |
for (int i = 0; i < extensiones.length; i++) { |
377 |
if (extensiones[i].equals("")) { |
378 |
continue;
|
379 |
} |
380 |
if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])) {
|
381 |
return true; |
382 |
} |
383 |
} |
384 |
|
385 |
return false; |
386 |
} |
387 |
|
388 |
/**
|
389 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
390 |
*/
|
391 |
public String getDescription() { |
392 |
return description;
|
393 |
} |
394 |
|
395 |
public String[] getExtensions() { |
396 |
return extensiones;
|
397 |
} |
398 |
|
399 |
public boolean isDirectory(){ |
400 |
return dirs;
|
401 |
} |
402 |
|
403 |
private String getExtensionOfAFile(File file){ |
404 |
String name;
|
405 |
int dotPos;
|
406 |
name = file.getName(); |
407 |
dotPos = name.lastIndexOf(".");
|
408 |
if (dotPos < 1){ |
409 |
return ""; |
410 |
} |
411 |
return name.substring(dotPos+1); |
412 |
} |
413 |
|
414 |
public File normalizeExtension(File file){ |
415 |
String ext = getExtensionOfAFile(file);
|
416 |
if (ext.equals("") || !(this.accept(file))){ |
417 |
return new File(file.getAbsolutePath() + "." + extensiones[0]); |
418 |
} |
419 |
return file;
|
420 |
} |
421 |
|
422 |
public String getInfo(){ |
423 |
return this.info; |
424 |
} |
425 |
|
426 |
public void setInfo(String info){ |
427 |
this.info = info;
|
428 |
} |
429 |
|
430 |
} |