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