svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / pansharp / PanSharpeningByteFilter.java @ 12618
History | View | Annotate | Download (10.6 KB)
1 |
package org.gvsig.raster.grid.filter.pansharp; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.geom.AffineTransform; |
6 |
import java.awt.geom.Dimension2D; |
7 |
import java.awt.image.BufferedImage; |
8 |
import java.awt.image.PixelGrabber; |
9 |
import java.util.ArrayList; |
10 |
|
11 |
import org.gvsig.raster.buffer.BufferFactory; |
12 |
import org.gvsig.raster.buffer.RasterBuffer; |
13 |
import org.gvsig.raster.buffer.RasterBufferInvalidException; |
14 |
import org.gvsig.raster.dataset.IBuffer; |
15 |
import org.gvsig.raster.dataset.RasterDataset; |
16 |
import org.gvsig.raster.datastruct.ViewPortData; |
17 |
import org.gvsig.raster.grid.Grid; |
18 |
import org.gvsig.raster.grid.render.ImageDrawer; |
19 |
import org.gvsig.raster.grid.render.Rendering; |
20 |
import org.gvsig.raster.hierarchy.IRasterDataset; |
21 |
|
22 |
import com.iver.cit.gvsig.fmap.ViewPort; |
23 |
|
24 |
|
25 |
public class PanSharpeningByteFilter extends PanSharpeningFilter { |
26 |
|
27 |
public PanSharpeningByteFilter (){
|
28 |
super();
|
29 |
} |
30 |
|
31 |
public void pre(){ |
32 |
|
33 |
// Recogemos los parametros
|
34 |
loadParam(); |
35 |
checkInput(); |
36 |
|
37 |
Dimension2D dimension = new Dimension(width, height); |
38 |
//Creamos el buffer donde se pinta la pancromatica
|
39 |
imagePancr = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
40 |
|
41 |
/*// Cargamos la imagen pancromatica en el Buffer
|
42 |
BufferFactory ftryPancromatica=new BufferFactory(dataset.getGeoRasterMultiDataset().getDataset(pancrName));
|
43 |
Rendering rPancromatica= new Rendering(ftryPancromatica);
|
44 |
ViewPort vp = new ViewPort(null);
|
45 |
ViewPortData vpPancr = new ViewPortData(vp.getProjection(),dataset.getGeoRasterMultiDataset().getDataset(pancrName).getExtent(), dimension);
|
46 |
rPancromatica.draw((Graphics2D)imagePancr.getGraphics(), vpPancr);*/
|
47 |
|
48 |
|
49 |
//Creamos el buffer donde se pinta la imagen de multiespectral.
|
50 |
//Pintar Imagen Multiespectral en imageMultiespectral
|
51 |
imageMultiespec = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
52 |
BufferFactory ftryMultiespc=new BufferFactory(dataset.getMultiRasterDataset());
|
53 |
Grid a=null;
|
54 |
try {
|
55 |
a=new Grid(ftryMultiespc);
|
56 |
} catch (RasterBufferInvalidException e) {
|
57 |
// TODO Auto-generated catch block
|
58 |
e.printStackTrace(); |
59 |
} |
60 |
|
61 |
|
62 |
ImageDrawer imageM= new ImageDrawer();
|
63 |
imageM.setBuffer(a.getRasterBuf()); |
64 |
imageM.setBufferSize(a.getRasterBuf().getWidth(),a.getRasterBuf().getHeight()); |
65 |
imageMultiespec = imageM.drawBufferOverImageObject(true, new int[]{0,1,2}); |
66 |
|
67 |
rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, width, height, 3, true); |
68 |
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Validamos que haya alguna otra banda adem?s de la pancrom?tica y que la pancrom?tica
|
73 |
* sea de mayor resoluci?n que las dem?s.
|
74 |
*/
|
75 |
private void checkInput(){ |
76 |
exec = true;
|
77 |
if(heightMultiespec >= heightPancr || widthMultiespec >= widthPancr || heightMultiespec == 0 || widthMultiespec == 0) |
78 |
exec = false;
|
79 |
String ficheros[]= dataset.getFileName(); |
80 |
for(int i=0;i<ficheros.length;i++){ |
81 |
if(ficheros[i] != pancrName){
|
82 |
if(dataset.getMultiRasterDataset().getDataset(ficheros[i]).getHeight() != heightMultiespec || dataset.getMultiRasterDataset().getDataset(ficheros[i]).getWidth()!= widthMultiespec){
|
83 |
exec = false;
|
84 |
break;
|
85 |
} |
86 |
} |
87 |
} |
88 |
} |
89 |
|
90 |
|
91 |
|
92 |
private void loadParam(){ |
93 |
|
94 |
raster = (RasterBuffer) params.get("raster");
|
95 |
posPancromatica= ((Integer) params.get("posPancromatica")).intValue(); |
96 |
dataset= (IRasterDataset)params.get("dataset");
|
97 |
bandOrder = (ArrayList) params.get("order"); |
98 |
alpha = ((Integer) params.get("alpha")).intValue(); |
99 |
method = (String) params.get("method"); |
100 |
coef = ((Double) params.get("coef")).doubleValue(); |
101 |
coefBrovey = ((Integer) params.get("coefBrovey")).intValue(); |
102 |
height = raster.getHeight(); |
103 |
width = raster.getWidth(); |
104 |
// Name de pancromatica
|
105 |
pancrName=dataset.getMultiRasterDataset().getBands().getBand(posPancromatica).getFileName(); |
106 |
params.put("pancName", pancrName);
|
107 |
heightPancr = dataset.getMultiRasterDataset().getDataset(pancrName).getHeight(); |
108 |
widthPancr = dataset.getMultiRasterDataset().getDataset(pancrName).getWidth(); |
109 |
// Nombre de los ficheros que componen el dataser
|
110 |
String[] ficherosNames= dataset.getFileName(); |
111 |
for(int i=0;i<ficherosNames.length;i++){ |
112 |
if(ficherosNames[i] != pancrName){
|
113 |
heightMultiespec = dataset.getMultiRasterDataset().getDataset(ficherosNames[i]).getHeight(); |
114 |
widthMultiespec = dataset.getMultiRasterDataset().getDataset(ficherosNames[i]).getWidth(); |
115 |
} |
116 |
} |
117 |
|
118 |
relX = (int)widthPancr/widthMultiespec;
|
119 |
relY = (int)heightPancr/heightMultiespec;
|
120 |
|
121 |
} |
122 |
|
123 |
|
124 |
public void execute() { |
125 |
|
126 |
// Para cada linea
|
127 |
pre(); |
128 |
if(exec){
|
129 |
|
130 |
int[] pRGBArrayMultiesp = new int[width]; |
131 |
int[] pRGBArrayPancr = null; |
132 |
PixelGrabber pg = null; |
133 |
int widthDst = width * relX;
|
134 |
|
135 |
//Para cada linea leemos los valores RGB del image. Aplicamos el algoritmo
|
136 |
//y escribimos el resultado.
|
137 |
if(method.equals("hsl")){ |
138 |
for(int iLine=0;iLine<height ;iLine++){ |
139 |
pg = new PixelGrabber(imageMultiespec, 0, iLine, width, 1, pRGBArrayMultiesp, 0, width); |
140 |
try {
|
141 |
pg.grabPixels(); |
142 |
} catch (InterruptedException e) {e.printStackTrace();} |
143 |
pRGBArrayPancr = processIHS(pRGBArrayMultiesp, iLine); |
144 |
((BufferedImage)imagePancr).setRGB(0, iLine, width, 1, pRGBArrayPancr, 0, width); |
145 |
} |
146 |
}else{
|
147 |
for(int iLine=0;iLine<height ;iLine++){ |
148 |
pg = new PixelGrabber(imageMultiespec, 0, iLine, width, 1, pRGBArrayMultiesp, 0, width); |
149 |
try {
|
150 |
pg.grabPixels(); |
151 |
} catch (InterruptedException e) {e.printStackTrace();} |
152 |
pRGBArrayPancr = processBrovey(pRGBArrayMultiesp, iLine); |
153 |
((BufferedImage)imagePancr).setRGB(0, iLine, width, 1, pRGBArrayPancr, 0, width); |
154 |
} |
155 |
} |
156 |
} |
157 |
post(); |
158 |
System.out.print("ccc"); |
159 |
} |
160 |
|
161 |
|
162 |
public void post(){ |
163 |
|
164 |
// Pasar el Buffer de datos de la imgPancr a rasterResult
|
165 |
PixelGrabber pg = null; |
166 |
int[] datosLinea= new int[width]; |
167 |
for(int iLine=0;iLine<height ;iLine++){ |
168 |
//Recojo los datos RGB de la fila en Hexadecimal
|
169 |
pg = new PixelGrabber(imagePancr, 0, iLine, width, 1, datosLinea, 0, width); |
170 |
try {
|
171 |
pg.grabPixels(); |
172 |
} catch (InterruptedException e) { |
173 |
|
174 |
e.printStackTrace(); |
175 |
} |
176 |
for(int i=0;i<width; i++){ |
177 |
|
178 |
byte r = (byte)((datosLinea[i] >> 16) & 0xff); |
179 |
byte g = (byte)((datosLinea[i] >> 8) & 0xff); |
180 |
byte b = (byte)(datosLinea[i] & 0xff); |
181 |
|
182 |
rasterResult.setElem(iLine, i, 0, r);
|
183 |
rasterResult.setElem(iLine, i, 1, g);
|
184 |
rasterResult.setElem(iLine, i, 2, b);
|
185 |
} |
186 |
} |
187 |
|
188 |
} |
189 |
|
190 |
|
191 |
/**
|
192 |
* @return tipo de dato del buffer de entrada
|
193 |
* */
|
194 |
|
195 |
public int getInRasterDataType() { |
196 |
return RasterBuffer.TYPE_BYTE;
|
197 |
} |
198 |
|
199 |
|
200 |
/**
|
201 |
* @return tipo de dato del buffer de salida
|
202 |
* */
|
203 |
public int getOutRasterDataType() { |
204 |
return RasterBuffer.TYPE_BYTE;
|
205 |
} |
206 |
|
207 |
|
208 |
/**
|
209 |
* @return buffer resultante tras aplicar el filtro
|
210 |
* */
|
211 |
public Object getResult(String name) { |
212 |
if (name.equals("raster")) |
213 |
return (IBuffer)rasterResult;
|
214 |
return null; |
215 |
} |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
/**
|
222 |
* Aplica la operaci?n de refinamiento sobre el buffer Multiespectral que contiene
|
223 |
* el RGB que es pasado por par?metro utilizando la pancrom?tica.
|
224 |
* @param bufferInput Buffer rgb
|
225 |
* @param length longitud del buffer de la pancromatica utilizado
|
226 |
* @param iLine l?nea leida de la imagen multiespectral
|
227 |
* @return buffer con el resultado de la operaci?n
|
228 |
*/
|
229 |
private int[] processBrovey(int[] bufferInput, int iLine){ |
230 |
double[] hsl; |
231 |
int[] rgb; |
232 |
PixelGrabber pg = null; |
233 |
|
234 |
//Longitud del buffer de salida
|
235 |
int widthDst = width * relX;
|
236 |
//Buffer de salida
|
237 |
int[] bufferPancr = new int[width]; |
238 |
|
239 |
pg = new PixelGrabber(imagePancr, 0, iLine, width, 1, bufferPancr , 0, width); |
240 |
|
241 |
try {
|
242 |
pg.grabPixels(); |
243 |
} catch (InterruptedException e) { |
244 |
e.printStackTrace(); |
245 |
} |
246 |
|
247 |
for(int iElem=0; iElem<width; iElem++){ |
248 |
int r = ((bufferInput[iElem] >> 16) & 0xff); |
249 |
int g = ((bufferInput[iElem] >> 8) & 0xff); |
250 |
int b = (bufferInput[iElem] & 0xff); |
251 |
byte i = (byte)((bufferPancr[iElem] >> 16) & 0xff); |
252 |
double scale = (3.0*(i+coefBrovey))/(r+g+b+1.0); |
253 |
r *= scale;g *= scale;b *= scale; |
254 |
bufferPancr[iElem] = ((alpha << 24) & 0xff000000) | |
255 |
((r << 16) & 0x00ff0000)| |
256 |
((g << 8) & 0x0000ff00) | |
257 |
(b & 0x000000ff);
|
258 |
} |
259 |
return bufferPancr;
|
260 |
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* Aplica la operaci?n de refinamiento sobre el buffer Multiespectral que contiene
|
265 |
* el RGB que es pasado por par?metro utilizando la pancrom?tica.
|
266 |
* @param bufferInput Buffer rgb
|
267 |
* @param length longitud del buffer de la pancromatica utilizado
|
268 |
* @param iLine l?nea leida de la imagen multiespectral
|
269 |
* @return buffer con el resultado de la operaci?n
|
270 |
*/
|
271 |
private int[] processIHS(int[] bufferInput, int iLine){ |
272 |
|
273 |
int[] bufferPancr = new int[width]; |
274 |
/* double[] hsl;
|
275 |
int[] rgb;
|
276 |
PixelGrabber pg = null;
|
277 |
|
278 |
//Buffer de salida
|
279 |
|
280 |
|
281 |
pg = new PixelGrabber(imagePancr, 0, iLine, width, 1, bufferPancr , 0, width);
|
282 |
|
283 |
try {
|
284 |
pg.grabPixels();
|
285 |
} catch (InterruptedException e) {
|
286 |
e.printStackTrace();
|
287 |
}
|
288 |
|
289 |
int[] uvw , tmp = new int[3];
|
290 |
double[] xyz;
|
291 |
for(int iElem=0; iElem<width; iElem++){
|
292 |
xyz = ColorSpaceConversion.RGBtoHSL( (bufferInput[iElem] >> 16) & 0x000000ff,
|
293 |
(bufferInput[iElem] >> 8) & 0x000000ff,
|
294 |
bufferInput[iElem] & 0x000000ff);
|
295 |
|
296 |
xyz[2] = ((bufferPancr[iElem] & 0x000000ff)/255.0) + coef;
|
297 |
|
298 |
tmp[0] = (int)(255.0 * xyz[0] / 360.0 + 0.5);
|
299 |
tmp[2] = (int) (xyz[2]*255. + 0.5);
|
300 |
tmp[1] = (int) (xyz[1]*255. + 0.5);
|
301 |
|
302 |
uvw = ColorSpaceConversion.HSLtoRGB(tmp[ColorSpaceConversion.H],
|
303 |
tmp[ColorSpaceConversion.S],
|
304 |
tmp[ColorSpaceConversion.L]);
|
305 |
bufferPancr[iElem] = ((alpha << 24) & 0xff000000) |
|
306 |
((uvw[0] << 16) & 0x00ff0000)|
|
307 |
((uvw[1] << 8) & 0x0000ff00) |
|
308 |
(uvw[2] & 0x000000ff);
|
309 |
}*/
|
310 |
return bufferPancr;
|
311 |
|
312 |
} |
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
/**
|
320 |
* Aplica la operaci?n de refinamiento sobre el buffer Multiespectral que contiene
|
321 |
* el RGB que es pasado por par?metro utilizando la pancrom?tica.
|
322 |
* @param bufferInput Buffer rgb
|
323 |
* @param length longitud del buffer de la pancromatica utilizado
|
324 |
* @param iLine l?nea leida de la imagen multiespectral
|
325 |
* @return buffer con el resultado de la operaci?n
|
326 |
*//* |
327 |
private int[] processBrovey(byte[] bufferInputR,byte[] bufferInputG,byte[] bufferInputB , int iLine){
|
328 |
int[] bufferPancr = new int[width];
|
329 |
for(int iElem=0; iElem<width; iElem++){
|
330 |
int r = ((bufferInputR[iElem] >> 16) & 0xff);
|
331 |
int g = ((bufferInputG[iElem] >> 8) & 0xff);
|
332 |
int b = (bufferInputB[iElem] & 0xff);
|
333 |
byte i = (byte)((bufferPancr[iElem] >> 16) & 0xff);
|
334 |
double scale = (3.0*(i+coefBrovey))/(r+g+b+1.0);
|
335 |
r *= scale;g *= scale;b *= scale;
|
336 |
bufferInputR[iElem] =(byte) ((r << 16) & 0x00ff0000);
|
337 |
bufferInputG[iElem] =(byte) ((g << 8) & 0x0000ff00) ;
|
338 |
bufferInputB[iElem] =(byte) (b & 0x000000ff);
|
339 |
|
340 |
}
|
341 |
return bufferPancr;
|
342 |
|
343 |
}*/
|
344 |
|
345 |
|
346 |
} |