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