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