svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster_io / src / org / gvsig / fmap / dal / coverage / dataset / io / PngWriter.java @ 29713
History | View | Annotate | Download (6.42 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2008 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.fmap.dal.coverage.dataset.io; |
20 |
|
21 |
import java.awt.geom.AffineTransform; |
22 |
import java.io.File; |
23 |
import java.io.IOException; |
24 |
|
25 |
import org.cresques.cts.IProjection; |
26 |
import org.gvsig.fmap.dal.coverage.dataset.io.features.PngFeatures; |
27 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
28 |
import org.gvsig.raster.dataset.IDataWriter; |
29 |
import org.gvsig.raster.dataset.Params; |
30 |
import org.gvsig.raster.dataset.WriteFileFormatFeatures; |
31 |
import org.gvsig.raster.util.RasterUtilities; |
32 |
import org.gvsig.tools.ToolsLocator; |
33 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
34 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
35 |
|
36 |
import es.gva.cit.jgdal.Gdal; |
37 |
import es.gva.cit.jgdal.GdalDriver; |
38 |
import es.gva.cit.jgdal.GdalException; |
39 |
/**
|
40 |
* Driver para la escritura de Png.
|
41 |
* Este driver utiliza GdalWriter para salvar Png.
|
42 |
* La escritura de un png no es posible utilizando un servidor de datos
|
43 |
* como el que usan los drivers comunes por lo que ser? necesario salvar antes
|
44 |
* a Tif con el driver de Gdal para posteriormente convertir la imagen completa
|
45 |
* a png. El problema que tiene es el mismo del jpg y es que el dataset solo soporta la
|
46 |
* escritura rw y no rw+.
|
47 |
*
|
48 |
* @version 22/07/2008
|
49 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
50 |
*/
|
51 |
public class PngWriter extends GeoRasterWriter { |
52 |
|
53 |
// Datos de registro de drivers
|
54 |
public static void register() { |
55 |
ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager(); |
56 |
ExtensionPoint point=extensionPoints.get("RasterWriter");
|
57 |
point.append("png", "", PngWriter.class); |
58 |
fileFeature.put("png", new PngFeatures()); |
59 |
} |
60 |
|
61 |
private GdalWriter gdalWriter = null; |
62 |
private String outTif = null; |
63 |
private String outPng = null; |
64 |
|
65 |
/**
|
66 |
* Carga los par?metros de este driver.
|
67 |
*/
|
68 |
public void loadParams(String ident) { |
69 |
WriteFileFormatFeatures wfff = (WriteFileFormatFeatures) fileFeature.get(ident); |
70 |
wfff.loadParams(); |
71 |
driverParams = wfff.getParams(); |
72 |
} |
73 |
|
74 |
/**
|
75 |
* Constructor para la obtenci?n de par?metros del driver
|
76 |
* @param drvType Tipo de driver
|
77 |
*/
|
78 |
public PngWriter(String fileName) { |
79 |
ident = RasterUtilities.getExtensionFromFileName(fileName); |
80 |
driver = ((WriteFileFormatFeatures) fileFeature.get(ident)).getDriverName(); |
81 |
gdalWriter = new GdalWriter(fileName);
|
82 |
|
83 |
loadParams(ident); |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Constructor para salvar datos servidos por el cliente
|
88 |
* @param dataWriter Objeto servidor de datos para el driver de escritura
|
89 |
* @param outSizeX N?mero de pixels en X de la imagen de salida
|
90 |
* @param outSizeY N?mero de pixels en Y de la imagen de salida
|
91 |
* @param outFilename Fichero de salida
|
92 |
* @param extentMaxX Posici?n en X m?xima del extent
|
93 |
* @param extentMinX Posici?n en X m?nima del extent
|
94 |
* @param extentMaxY Posici?n en Y m?xima del extent
|
95 |
* @param extentMinY Posici?n en Y m?nima del extent
|
96 |
* @param nBands N?mero de bandas
|
97 |
* @param drvType Tipo de driver
|
98 |
* @throws GdalException
|
99 |
* @throws IOException
|
100 |
*/
|
101 |
public PngWriter(IDataWriter dataWriter,
|
102 |
String outFileName,
|
103 |
Integer nBands,
|
104 |
AffineTransform at,
|
105 |
Integer outSizeX,
|
106 |
Integer outSizeY,
|
107 |
Integer dataType,
|
108 |
Params params, |
109 |
IProjection proj, |
110 |
Boolean geo)throws GdalException, IOException { |
111 |
ident = RasterUtilities.getExtensionFromFileName(outFileName); |
112 |
driver = ((WriteFileFormatFeatures) fileFeature.get(ident)).getDriverName(); |
113 |
outPng = outFileName; |
114 |
outTif = outFileName.substring(0, outFileName.lastIndexOf(".")); |
115 |
outTif += ".tif";
|
116 |
this.at = at;
|
117 |
|
118 |
gdalWriter = new GdalWriter(dataWriter, outTif, nBands, at, outSizeX, outSizeY, dataType, params, proj, geo);
|
119 |
if (params == null) |
120 |
loadParams(ident); |
121 |
else
|
122 |
this.driverParams = params;
|
123 |
} |
124 |
|
125 |
/**
|
126 |
* Asigna el tipo de driver con el que se salvar? la imagen
|
127 |
* @param drvType Tipo de driver
|
128 |
*/
|
129 |
public void setDriverType(String drvType) { |
130 |
gdalWriter.setDriverType(drvType); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
|
135 |
* @throws IOException
|
136 |
*/
|
137 |
public void fileWrite() throws IOException, InterruptedException { |
138 |
gdalWriter.fileWrite(); |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Realiza una copia en el formato especificado.
|
143 |
* @throws IOException
|
144 |
*/
|
145 |
public static void createCopy(GdalDriver driverDst, String dst, String src, |
146 |
boolean bstrict, String[] params, IProjection proj) throws IOException, GdalException { |
147 |
GdalWriter.createCopy(driverDst, dst, src, bstrict, params, proj); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Realiza la escritura de datos con los datos que le pasa el cliente.
|
152 |
* @throws IOException
|
153 |
*/
|
154 |
public void dataWrite() throws IOException, InterruptedException { |
155 |
if(colorInterp != null) |
156 |
gdalWriter.setColorBandsInterpretation(colorInterp.getValues()); |
157 |
gdalWriter.dataWrite(); |
158 |
if (gdalWriter.isWrite()) {
|
159 |
gdalWriter.writeClose(); |
160 |
if (outTif != null) { |
161 |
GdalDriver driver = null;
|
162 |
try {
|
163 |
driver = Gdal.getDriverByName("PNG");
|
164 |
GdalWriter.createCopy(driver, outPng, outTif, false, gdalWriter.gdalParamsFromRasterParams(driverParams), null); |
165 |
} catch (GdalException exc) {
|
166 |
throw new IOException("No se ha podido obtener el driver."); |
167 |
} |
168 |
File file = new File(outTif); |
169 |
file.delete(); |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
/**
|
175 |
* Cancela el salvado de datos.
|
176 |
*/
|
177 |
public void writeCancel() { |
178 |
gdalWriter.setWrite(false);
|
179 |
} |
180 |
|
181 |
/*
|
182 |
* (non-Javadoc)
|
183 |
* @see org.gvsig.raster.dataset.GeoRasterWriter#setParams(org.gvsig.raster.dataset.Params)
|
184 |
*/
|
185 |
public void setParams(Params params) { |
186 |
driverParams = params; |
187 |
if (gdalWriter != null) |
188 |
gdalWriter.setParams(params); |
189 |
} |
190 |
|
191 |
/**
|
192 |
* Cierra el compresor ecw.
|
193 |
* @throws GdalException
|
194 |
*/
|
195 |
public void writeClose() { |
196 |
// El close del tif se hizo en dataWrite
|
197 |
} |
198 |
|
199 |
public void setWkt(String wkt) {} |
200 |
} |