root / tags / v1_1_Build_1010 / libraries / libCq_CMS_praster / src / org / cresques / io / JpegWriter.java @ 12804
History | View | Annotate | Download (13.3 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
|
29 |
import org.cresques.cts.IProjection; |
30 |
import org.cresques.geo.ViewPortData; |
31 |
import org.cresques.px.PxRaster; |
32 |
|
33 |
import es.gva.cit.jgdal.Gdal; |
34 |
import es.gva.cit.jgdal.GdalDriver; |
35 |
import es.gva.cit.jgdal.GdalException; |
36 |
|
37 |
|
38 |
/**
|
39 |
* Driver para la escritura de Jpeg.
|
40 |
* Este driver utiliza GdalWriter para salvar Jpeg.
|
41 |
* La escritura de un jpeg no es posible utilizando un servidor de datos
|
42 |
* como el que usan los drivers comunes por lo que ser? necesario salvar antes
|
43 |
* a Tif con el driver de Gdal para posteriormente convertir la imagen completa
|
44 |
* a jpg.
|
45 |
*
|
46 |
@author Nacho Brodin (brodin_ign@gva.es)
|
47 |
*/
|
48 |
public class JpegWriter extends GeoRasterWriter { |
49 |
|
50 |
public final int windowSizeX = 386; |
51 |
public final int windowSizeY = 220; |
52 |
public final int panelSizeX = 358; |
53 |
public final int panelSizeY = 125; |
54 |
|
55 |
//Datos de registro de drivers
|
56 |
static {
|
57 |
GeoRasterWriter.registerWriterExtension("jpg", JpegWriter.class);
|
58 |
//GeoRasterWriter.registerWriterExtension("jpeg", JpegWriter.class);
|
59 |
typeList.put("jpg", "Jpeg"); |
60 |
//typeList.put("jpeg", "Jpeg");
|
61 |
} |
62 |
|
63 |
private GdalWriter gdalWriter = null; |
64 |
private JpegSupportOptions support = null; |
65 |
private String outTif = null; |
66 |
private String outJpg = null; |
67 |
private String[] props = null; |
68 |
private ViewPortData viewPort = null; |
69 |
private String[] params = null; //Par?metros de creaci?n del dataset. |
70 |
|
71 |
/**
|
72 |
* Constructor para la obtenci?n de par?metros del driver
|
73 |
* @param drvType Tipo de driver
|
74 |
*/
|
75 |
public JpegWriter(String fileName) { |
76 |
ident = fileName.toLowerCase().substring(fileName.lastIndexOf(".") + 1); |
77 |
driver = (String)typeList.get(ident);
|
78 |
support = new JpegSupportOptions(driver);
|
79 |
gdalWriter = new GdalWriter(fileName);
|
80 |
gdalWriter.getSupport().setTfw(true);
|
81 |
setParams(); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Constructor para salvar una sola imagen completa
|
86 |
* @param raster PxRaster de la imagen de origen
|
87 |
* @param outfilename Nombre del fichero de salida
|
88 |
* @param infilename Nombre del fichero de entrada
|
89 |
* @param drvType Tipo de driver
|
90 |
*/
|
91 |
public JpegWriter(PxRaster raster, String outFileName, String inFileName) throws GdalException, IOException { |
92 |
ident = inFileName.toLowerCase().substring(inFileName.lastIndexOf(".") + 1); |
93 |
driver = (String)typeList.get(ident);
|
94 |
support = new JpegSupportOptions(driver);
|
95 |
gdalWriter = new GdalWriter(raster, outFileName, inFileName);
|
96 |
gdalWriter.getSupport().setTfw(true);
|
97 |
setParams(); |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Constructor para salvar datos servidos por el cliente
|
102 |
* @param dataWriter Objeto servidor de datos para el driver de escritura
|
103 |
* @param outSizeX N?mero de pixels en X de la imagen de salida
|
104 |
* @param outSizeY N?mero de pixels en Y de la imagen de salida
|
105 |
* @param outFilename Fichero de salida
|
106 |
* @param extentMaxX Posici?n en X m?xima del extent
|
107 |
* @param extentMinX Posici?n en X m?nima del extent
|
108 |
* @param extentMaxY Posici?n en Y m?xima del extent
|
109 |
* @param extentMinY Posici?n en Y m?nima del extent
|
110 |
* @param nBands N?mero de bandas
|
111 |
* @param drvType Tipo de driver
|
112 |
* @throws GdalException
|
113 |
* @throws IOException
|
114 |
*/
|
115 |
public JpegWriter( IDataWriter dataWriter,
|
116 |
String outFileName,
|
117 |
Integer blockSize,
|
118 |
Integer nBands,
|
119 |
ViewPortData vp, |
120 |
Integer compresion,
|
121 |
Integer outSizeX,
|
122 |
Integer outSizeY,
|
123 |
Integer dataType)throws GdalException, IOException { |
124 |
ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); |
125 |
driver = (String)typeList.get(ident);
|
126 |
support = new JpegSupportOptions(driver);
|
127 |
outJpg = outFileName; |
128 |
outTif = outFileName.substring(0, outFileName.lastIndexOf(".") + 1); |
129 |
outTif = outTif + "tif";
|
130 |
this.viewPort = vp;
|
131 |
gdalWriter = new GdalWriter(dataWriter, outTif, blockSize, nBands,
|
132 |
vp, compresion, outSizeX, outSizeY, dataType); |
133 |
gdalWriter.getSupport().setTfw(true);
|
134 |
setParams(); |
135 |
} |
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 |
* A partir de un elemento que contiene una propiedad y un valor
|
148 |
* lo parsea y asigna el valor a su variable.
|
149 |
* @param propValue elemento con la forma propiedad=valor
|
150 |
*/
|
151 |
private void readProperty(String propValue) { |
152 |
String prop = propValue.substring(0, propValue.indexOf("=")); |
153 |
|
154 |
if (propValue.startsWith(prop)) {
|
155 |
String value = propValue.substring(propValue.indexOf("=") + 1, |
156 |
propValue.length()); |
157 |
|
158 |
if ((value != null) && !value.equals("")) { |
159 |
if (prop.equals("COMPRESSION")) |
160 |
support.setCompressionDefault(Integer.parseInt(value));
|
161 |
|
162 |
if (prop.equals("PROGRESSIVE")) { |
163 |
boolean prog = true; |
164 |
|
165 |
if (value.equals("yes")) { |
166 |
prog = true;
|
167 |
} else {
|
168 |
prog = false;
|
169 |
} |
170 |
|
171 |
support.setProgressive(prog); |
172 |
} |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Asigna propiedades al driver a partir de un vector de
|
179 |
* strings donde cada elemento tiene la estructura de
|
180 |
* propiedad=valor.
|
181 |
* @param props Propiedades
|
182 |
*/
|
183 |
public void setProps(String[] props) { |
184 |
this.props = props;
|
185 |
for (int iProps = 0; iProps < props.length; iProps++) |
186 |
readProperty(props[iProps]); |
187 |
setParams(); |
188 |
} |
189 |
|
190 |
/**
|
191 |
* Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
|
192 |
* @throws IOException
|
193 |
*/
|
194 |
public void fileWrite() throws IOException { |
195 |
gdalWriter.fileWrite(); |
196 |
} |
197 |
|
198 |
/**
|
199 |
* Realiza una copia en el formato especificado.
|
200 |
* @throws IOException
|
201 |
*/
|
202 |
public static void createCopy(GdalDriver driverDst, String dst, String src, |
203 |
boolean bstrict, String[] params, IProjection proj) throws IOException, GdalException { |
204 |
GdalWriter.createCopy(driverDst, dst, src, bstrict, params, proj); |
205 |
} |
206 |
|
207 |
/**
|
208 |
*Asigna par?metros de creaci?n del dataset de Gdal
|
209 |
*/
|
210 |
private void setParams() { |
211 |
params = new String[3]; |
212 |
|
213 |
params[0] = new String("WORLDFILE=ON"); |
214 |
params[1] = new String("QUALITY=" + support.getCompressionRealValue()); |
215 |
String prog = null; |
216 |
|
217 |
if (support.isProgressive()) {
|
218 |
prog = new String("ON"); |
219 |
} else {
|
220 |
prog = new String("NO"); |
221 |
} |
222 |
params[2] = new String("PROGRESSIVE=" + prog); |
223 |
} |
224 |
|
225 |
/**
|
226 |
* Realiza la escritura de datos con los datos que le pasa el cliente.
|
227 |
* @throws IOException
|
228 |
*/
|
229 |
public void dataWrite() throws IOException { |
230 |
gdalWriter.dataWrite(); |
231 |
if(gdalWriter.isWrite()){
|
232 |
gdalWriter.writeClose(); |
233 |
if(outTif != null){ |
234 |
GdalDriver driver = null;
|
235 |
try{
|
236 |
driver = Gdal.getDriverByName("JPEG");
|
237 |
|
238 |
GdalWriter.createCopy(driver, outJpg, outTif, false,
|
239 |
params, viewPort.getProjection()); |
240 |
}catch(GdalException exc){
|
241 |
System.err.println("No se ha podido obtener el driver."); |
242 |
} |
243 |
File file = new File(outTif); |
244 |
file.delete(); |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
/**
|
250 |
* Cierra el compresor ecw.
|
251 |
* @throws GdalException
|
252 |
*/
|
253 |
public void writeClose() { |
254 |
//El close del tif se hizo en dataWrite
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* Cancela el salvado de datos.
|
259 |
*/
|
260 |
public void writeCancel() { |
261 |
gdalWriter.setWrite(false);
|
262 |
} |
263 |
|
264 |
/**
|
265 |
* Devuelve la configuraci?n de la ventana de dialogo
|
266 |
* para las propiedades del driver de escritura de Gdal.
|
267 |
* @return XML de configuraci?n del dialogo.
|
268 |
*/
|
269 |
public String getXMLPropertiesDialog() { |
270 |
StringBuffer options = null; |
271 |
options = new StringBuffer(); |
272 |
options.append("<window sizex=\"" + windowSizeX + "\" sizey=\"" + |
273 |
windowSizeY + "\">");
|
274 |
options.append("<panel sizex=\"" + panelSizeX + "\" sizey=\"" + |
275 |
panelSizeY + "\" layout=\"" + gdalWriter.panelLayout +
|
276 |
"\" border=\"yes\">");
|
277 |
|
278 |
options.append("<panel layout=\"FlowLayout\" position=\"North\" align=\"left\">");
|
279 |
options.append("<label>block_size</label>");
|
280 |
options.append("<combo ident=\"BLOCKSIZE\" selected=\"" +
|
281 |
gdalWriter.getSupport().getBlockSize() + "\">");
|
282 |
|
283 |
for (int i = 0; i < gdalWriter.getSupport().getBlockSizeList().length; i++) |
284 |
options.append("<elem>" + gdalWriter.getSupport().getBlockSizeList()[i] +
|
285 |
"</elem>");
|
286 |
|
287 |
options.append("</combo>");
|
288 |
options.append("</panel>");
|
289 |
|
290 |
|
291 |
options.append("<panel layout=\"FlowLayout\" position=\"Center\">");
|
292 |
options.append("<slider ident=\"COMPRESSION\" name=\"Compression\" sizex=\"370\" sizey=\"20\">");
|
293 |
options.append("<min>" + support.getCompressionList()[0] + |
294 |
"</min>");
|
295 |
options.append("<max>" + support.getCompressionList()[1] + |
296 |
"</max>");
|
297 |
options.append("<value>" + support.getCompressionDefault() + "</value>"); |
298 |
options.append("<minorspacing>" + support.getCompressionList()[3] + |
299 |
"</minorspacing>");
|
300 |
options.append("<majorspacing>" + support.getCompressionList()[4] + |
301 |
"</majorspacing>");
|
302 |
options.append("</slider>");
|
303 |
options.append("</panel>");
|
304 |
|
305 |
|
306 |
options.append("<panel layout=\"FlowLayout\" position=\"South\" align=\"left\">");
|
307 |
options.append("<label>progressive</label>");
|
308 |
String sel = null; |
309 |
if (support.isProgressive()) {
|
310 |
sel = new String("yes"); |
311 |
} else {
|
312 |
sel = new String("no"); |
313 |
} |
314 |
options.append("<check ident=\"PROGRESSIVE\" selected=\"" + sel +
|
315 |
"\" text=\"\">");
|
316 |
options.append("</check>");
|
317 |
|
318 |
options.append("</panel>");
|
319 |
|
320 |
options.append("</panel>");
|
321 |
options.append("</window>");
|
322 |
|
323 |
return options.toString();
|
324 |
} |
325 |
|
326 |
/**
|
327 |
*
|
328 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
329 |
*
|
330 |
* Opciones que soporta el driver de escritura de Jpeg.
|
331 |
*/
|
332 |
public class JpegSupportOptions extends WriterSupportOptions { |
333 |
private boolean progressive = true; |
334 |
private String[] compresionListValues = { "10", "100", "16", "5", "30" }; //min, max, valor defecto, intervalo peque?o, intervalo grande; |
335 |
private int compresionDefaultValue = 16; |
336 |
|
337 |
public JpegSupportOptions(String ext) { |
338 |
super(ext);
|
339 |
} |
340 |
|
341 |
/**
|
342 |
* Obtiene la compresion seleccionada
|
343 |
* @return Compresi?n seleccionada
|
344 |
*/
|
345 |
public int getCompressionDefault() { |
346 |
return compresionDefaultValue;
|
347 |
} |
348 |
|
349 |
/**
|
350 |
* Obtiene la compresion seleccionada en el valor real que necesita gdal. Por defecto gdal
|
351 |
* tiene un valor de 75 en un rango de 10-100 donde 10 es la m?xima compresi?n.
|
352 |
* Como el slider est? en un rango de 0-100 donde el 100 es la m?xima compresi?n y 0
|
353 |
* la m?nima hay que hacer una conversi?n.
|
354 |
* @return Compresi?n seleccionada
|
355 |
*/
|
356 |
public int getCompressionRealValue() { |
357 |
return 110 - compresionDefaultValue; |
358 |
} |
359 |
|
360 |
|
361 |
/**
|
362 |
* Obtiene la lista de los valores para la generaci?n de la barra de
|
363 |
* compresi?n. Son cinco valores m?nimo, m?ximo, seleccionado,
|
364 |
* intervalo peque?o, intervalo grande.
|
365 |
* @return lista de valores de compresi?n
|
366 |
*/
|
367 |
public String[] getCompressionList() { |
368 |
return compresionListValues;
|
369 |
} |
370 |
|
371 |
/**
|
372 |
* Asigna el nivel de compresi?npor defecto
|
373 |
* @param compress Nivel de compresi?n
|
374 |
*/
|
375 |
public void setCompressionDefault(int compress) { |
376 |
this.compresionDefaultValue = compress;
|
377 |
} |
378 |
|
379 |
/**
|
380 |
* Asigna la lista de valores de compresi?n
|
381 |
* @param compresion lista de valores
|
382 |
*/
|
383 |
public void setCompressionList(String[] compresion) { |
384 |
this.compresionListValues = compresion;
|
385 |
} |
386 |
|
387 |
public boolean isProgressive() { |
388 |
return progressive;
|
389 |
} |
390 |
|
391 |
public void setProgressive(boolean progressive) { |
392 |
this.progressive = progressive;
|
393 |
} |
394 |
} |
395 |
} |