Revision 11914

View differences:

branches/v10/applications/appgvSIG/src/com/iver/cit/gvsig/Export.java
47 47
package com.iver.cit.gvsig;
48 48

  
49 49
import java.awt.Component;
50
import java.awt.Image;
50
import java.awt.Dimension;
51
import java.awt.image.BufferedImage;
51 52
import java.io.File;
52
import java.io.FileNotFoundException;
53
import java.io.FileOutputStream;
54 53
import java.io.IOException;
54
import java.util.Hashtable;
55
import java.util.Iterator;
55 56

  
56 57
import javax.swing.JFileChooser;
58
import javax.swing.JOptionPane;
57 59
import javax.swing.filechooser.FileFilter;
58 60

  
61
import org.cresques.geo.ViewPortData;
62
import org.cresques.io.GeoRasterWriter;
63
import org.cresques.io.IDataWriter;
64
import org.cresques.io.data.RasterBuf;
65
import org.cresques.px.Extent;
66

  
59 67
import com.iver.andami.PluginServices;
60 68
import com.iver.andami.messages.NotificationManager;
61 69
import com.iver.andami.plugins.Extension;
62 70
import com.iver.cit.gvsig.fmap.layers.FLayers;
71
import com.iver.cit.gvsig.project.Project;
63 72
import com.iver.cit.gvsig.project.documents.view.gui.View;
64
import com.iver.utiles.GenericFileFilter;
65 73
import com.sun.jimi.core.Jimi;
66 74
import com.sun.jimi.core.JimiException;
67
import com.sun.jimi.core.raster.JimiRasterImage;
68 75

  
69 76

  
70 77
/**
......
74 81
 */
75 82
public class Export extends Extension {
76 83
	private String lastPath = null;
84
	private Hashtable cmsExtensionsSupported = null; 
85
	private Hashtable jimiExtensionsSupported = null;
77 86

  
78 87
	/**
79 88
	 * @see com.iver.andami.plugins.IExtension#isEnabled()
......
112 121
	public void initialize() {
113 122
	}
114 123

  
124
	/* (non-Javadoc)
125
	 * @see com.iver.andami.plugins.Extension#postInitialize()
126
	 */
127
	public void postInitialize() {
128
		cmsExtensionsSupported = new Hashtable(); 
129
		jimiExtensionsSupported = new Hashtable();
130
		
131
		String[] ext;
132
		ext = GeoRasterWriter.getDriversExtensions(); 
133
		int i;
134
		for (i=0;i< ext.length;i++){
135
			cmsExtensionsSupported.put(ext[i], new MyFileFilter(ext[i],
136
					PluginServices.getText(this, ext[i]), "cms"));
137
		}
138
		
139
//		ext = Jimi.getEncoderTypes();
140
//		for (i=0;i<ext.length;i++){
141
//			if (!cmsExtensionsSupported.contains( ext[i])) {
142
//				jimiExtensionSupported.put(ext[i], new GenericFileFilter(ext[i],
143
//						PluginServices.getText(this, ext[i])));
144
//			}
145
//		}
146
		
147
		jimiExtensionsSupported.put("png",new MyFileFilter("png",
148
				PluginServices.getText(this, "png"), "jimi"));
149
		jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp",
150
				PluginServices.getText(this, "bmp"), "jimi"));		
151
		jimiExtensionsSupported.put("tga",new MyFileFilter("tga",
152
				PluginServices.getText(this, "tga"), "jimi"));
153

  
154

  
155
		
156
		
157
		
158
	}
159

  
160
	public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws IOException{
161
		RasterizerImage data = new RasterizerImage(srcImage);
162
		Extent ex = new Extent(0,0,srcImage.getWidth(),srcImage.getHeight());
163
		ViewPortData vpData = new ViewPortData(Project.getDefaultProjection(), ex, new Dimension(srcImage.getWidth(),srcImage.getHeight()));
164
		GeoRasterWriter writer = GeoRasterWriter.getWriter(data,fileDst.getAbsolutePath(),GeoRasterWriter.blockSizeDefault,3,vpData,10,srcImage.getWidth(),srcImage.getHeight(),RasterBuf.TYPE_IMAGE);
165
		if (writer == null){
166
			PluginServices.getLogger().error("No supported Format: " + fileDst.getAbsolutePath());
167
			return false;
168
		}
169
		writer.dataWrite();
170
		writer.writeClose(); 
171
		return true;
172
	}
173
	
174
	
175
	
176
	public static boolean saveImageJimi(File fileDst,BufferedImage srcImage) throws Exception{
177
		try {
178

  
179
			Jimi.putImage(srcImage, fileDst.getAbsolutePath());
180
			
181
		} catch (JimiException e) {
182
			throw new Exception(fileDst.getAbsolutePath(),e);
183
		}
184
		return true;
185

  
186
	}
187
	
115 188
	/**
116 189
	 * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
117 190
	 */
118 191
	public void execute(String actionCommand) {
119
		FileFilter pngFilter = new GenericFileFilter("png",
120
				PluginServices.getText(this, "png"));
121
		String[] s={"jpg","jpeg"};
122
		FileFilter jpgFilter = new GenericFileFilter(s,
123
				PluginServices.getText(this, "jpg"));
124
		FileFilter bmpFilter = new GenericFileFilter("bmp",
125
				PluginServices.getText(this, "bmp"));
126

  
127 192
		JFileChooser jfc = new JFileChooser(lastPath);
128
		jfc.addChoosableFileFilter(pngFilter);
129
		jfc.addChoosableFileFilter(jpgFilter);
130
		jfc.addChoosableFileFilter(bmpFilter);
131
		jfc.setFileFilter(pngFilter);
132
		if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
133
			Image tempImage;
193
		
194
		jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter());
195
		
196
		Iterator iter;
197
		
198
		iter = cmsExtensionsSupported.values().iterator();
199
		while (iter.hasNext()){
200
			jfc.addChoosableFileFilter((FileFilter)iter.next());
201
		}
134 202

  
203
		iter = jimiExtensionsSupported.values().iterator();
204
		while (iter.hasNext()){
205
			jfc.addChoosableFileFilter((FileFilter)iter.next());
206
		}
207

  
208
		jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png"));
209
		if (jfc.showSaveDialog(
210
				(Component) PluginServices.getMainFrame()
211
				) == JFileChooser.APPROVE_OPTION) {
212
			BufferedImage tempImage;
213

  
135 214
			tempImage = ((View) PluginServices.getMDIManager().getActiveWindow()).getImage();
136 215

  
137 216
			File f = jfc.getSelectedFile();
138

  
139
			try {
140
				JimiRasterImage jrf = Jimi.createRasterImage(tempImage.getSource());
141
				String tempName = f.getName().toUpperCase().trim();
142
				if (jfc.getFileFilter().equals(pngFilter)){
143
					System.out.println("pngFilter");
144
					if (!(tempName.length()>3) || (!(tempName.substring(tempName.length()-4,tempName.length()).equalsIgnoreCase(".png")))){
145
						f=new File(f.getPath()+".png");
146
					}
147
						FileOutputStream fout = new FileOutputStream(f);
148
						Jimi.putImage("image/png", jrf, fout);
149
						fout.close();
150
				} else if (jfc.getFileFilter().equals(bmpFilter)){
151
					System.out.println("bmpFilter");
152
					if (!(tempName.length()>3) || (!(tempName.substring(tempName.length()-4,tempName.length()).equalsIgnoreCase(".bmp")))){
153
						f=new File(f.getPath()+".bmp");
154
					}
155
						FileOutputStream fout = new FileOutputStream(f);
156
						Jimi.putImage("image/bmp", jrf, fout);
157
						fout.close();
158
				}else if (jfc.getFileFilter().equals(jpgFilter)){
159
					System.out.println("jpgFilter");
160
					if (!(tempName.length()>3) || ((tempName.length()>3 && !(tempName.substring(tempName.length()-4,tempName.length()).equalsIgnoreCase(".jpg")) || (tempName.length()>4 &&tempName.substring(tempName.length()-5,tempName.length()).equalsIgnoreCase(".jpeg"))))){
161
						f=new File(f.getPath()+".jpg");
162
					}
163
						FileOutputStream fout = new FileOutputStream(f);
164
						Jimi.putImage("image/jpg", jrf, fout);
165
						fout.close();
166

  
217
			
218
			MyFileFilter filter = (MyFileFilter)jfc.getFileFilter();
219
			f = filter.normalizeExtension(f);
220
			
221
			
222
			if (f.exists()){
223
				int resp = JOptionPane.showConfirmDialog(
224
						(Component) PluginServices.getMainFrame(),
225
						PluginServices.getText(this,
226
								"fichero_ya_existe_seguro_desea_guardarlo")+
227
								"\n"+ 
228
								f.getAbsolutePath(),
229
						PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
230
				if (resp != JOptionPane.YES_OPTION) {
231
					return;
167 232
				}
168
				String filePath = f.getAbsolutePath();
169
				lastPath = filePath.substring(0, filePath.lastIndexOf(File.separatorChar));
170
				/*if (tempName.endsWith(".JPG")) {
171 233

  
172
					FileOutputStream fout = new FileOutputStream(f);
173
					Jimi.putImage("image/jpg", jrf, fout);
174
					fout.close();
175
				} else if (tempName.endsWith(".PNG")) {
176
					FileOutputStream fout = new FileOutputStream(f);
177
					Jimi.putImage("image/png", jrf, fout);
178
					fout.close();
179
				} else if (tempName.endsWith(".BMP")) {
180
					FileOutputStream fout = new FileOutputStream(f);
181
					Jimi.putImage("image/bmp", jrf, fout);
182
					fout.close();
234
			}
235
			
236
			if (filter.getInfo().equalsIgnoreCase("cms")){
237
				
238
				try {
239
					saveImageCMS(f, tempImage);
240
				} catch (IOException e) {
241
					NotificationManager.addError("Error exportando la imagen", e);
183 242
				}
184
				*/
185
			} catch (JimiException e) {
186
				NotificationManager.addError("Error exportando la imagen", e);
187
			} catch (FileNotFoundException e) {
188
				NotificationManager.addError("No se encontr? el fichero", e);
189
			} catch (IOException e) {
190
				NotificationManager.addError("Error con el fichero", e);
243
			} else if (filter.getInfo().equalsIgnoreCase("jimi")) {
244
				try {
245
					saveImageJimi(f, tempImage);
246
				} catch (Exception e) {
247
					NotificationManager.addError("Error exportando la imagen", e);
248
				}
249
				
191 250
			}
251

  
252
			
192 253
		}
193 254
	}
194 255
}
256

  
257
class RasterizerImage implements IDataWriter{
258
	private BufferedImage source;
259
	private int[] data = null;
260
	
261
	public RasterizerImage(BufferedImage source) {
262
		this.source = source;
263
	}
264

  
265
	public int[] readARGBData(int sX, int sY, int nBand){
266
		return readData( sX, sY, nBand);
267
	}
268
	
269
	public int[] readData(int sizeX, int sizeY, int nBand) {
270
		if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas
271
			if (this.data == null){					
272
				this.data = this.source.getRGB(0, 0, sizeX, sizeY, this.data, 0, sizeX);
273
			}
274
			return this.data;
275
		}
276
		return null;
277
	}
278
	
279
}
280

  
281
class MyFileFilter extends FileFilter{
282

  
283
	private String[] extensiones=new String[1];
284
	private String description;
285
	private boolean dirs = true;
286
	private String info= null;
287

  
288
	public MyFileFilter(String[] ext, String desc) {
289
		extensiones = ext;
290
		description = desc;
291
	}
292

  
293
	public MyFileFilter(String[] ext, String desc,String info) {
294
		extensiones = ext;
295
		description = desc;
296
		this.info = info;
297
	}
298

  
299
	public MyFileFilter(String ext, String desc) {
300
		extensiones[0] = ext;
301
		description = desc;
302
	}
303

  
304
	public MyFileFilter(String ext, String desc,String info) {
305
		extensiones[0] = ext;
306
		description = desc;
307
		this.info = info;
308
	}
309

  
310
	public MyFileFilter(String ext, String desc, boolean dirs) {
311
		extensiones[0] = ext;
312
		description = desc;
313
		this.dirs = dirs;
314
	}
315

  
316
	public MyFileFilter(String ext, String desc, boolean dirs,String info) {
317
		extensiones[0] = ext;
318
		description = desc;
319
		this.dirs = dirs;
320
		this.info = info;
321
	}
322

  
323
	public boolean accept(File f) {
324
		if (f.isDirectory()) {
325
			if (dirs) {
326
				return true;
327
			} else {
328
				return false;
329
			}
330
		}
331
		for (int i=0;i<extensiones.length;i++){
332
			if (extensiones[i].equals("")){
333
				continue;
334
			}
335
			if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])){
336
				return true;
337
			}
338
		}
339
		
340
		return false;
341
	}
342

  
343
	/**
344
	 * @see javax.swing.filechooser.FileFilter#getDescription()
345
	 */
346
	public String getDescription() {
347
		return description;
348
	}
349

  
350
	public String[] getExtensions() {
351
		return extensiones;
352
	}
353
	
354
	public boolean isDirectory(){
355
		return dirs;
356
	}
357
	
358
	private String getExtensionOfAFile(File file){
359
		String name;
360
		int dotPos;
361
		name = file.getName();
362
		dotPos = name.lastIndexOf(".");
363
		if (dotPos < 1){
364
			return "";
365
		}
366
		return name.substring(dotPos+1);
367
	}
368
	
369
	public File normalizeExtension(File file){
370
		String ext = getExtensionOfAFile(file);
371
		if (ext.equals("") || !(this.accept(file))){
372
			return new File(file.getAbsolutePath() + "." + extensiones[0]);
373
		}
374
		return file;		
375
	}
376
	
377
	public String getInfo(){
378
		return this.info;
379
	}
380
	
381
	public void setInfo(String info){
382
		this.info = info;
383
	}
384

  
385
}

Also available in: Unified diff