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