root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / render / ImageDrawer.java @ 20326
History | View | Annotate | Download (13.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.grid.render; |
20 |
|
21 |
import java.awt.Image; |
22 |
import java.awt.image.BufferedImage; |
23 |
|
24 |
import org.gvsig.raster.buffer.RasterBuffer; |
25 |
import org.gvsig.raster.dataset.IBuffer; |
26 |
import org.gvsig.raster.grid.GridTransparency; |
27 |
import org.gvsig.raster.process.RasterTask; |
28 |
import org.gvsig.raster.process.RasterTaskQueue; |
29 |
/**
|
30 |
* Objeto para la escritura de datos desde un buffer a un objeto Image. En este nivel de
|
31 |
* renderizado no se gestiona extents, ni rotaciones ni coordenadas del mundo, solo la
|
32 |
* escritura desde un buffer hasta otro de tama?o dado. Por medio de par?metros y de objetos
|
33 |
* de estado varia el resultado de la escritura, selecci?n de bandas a escribir desde el buffer
|
34 |
* a RGB, transparencias aplicadas o paletas.
|
35 |
*
|
36 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
37 |
*/
|
38 |
public class ImageDrawer { |
39 |
/**
|
40 |
* Fuente de datos para el renderizado
|
41 |
*/
|
42 |
private IBuffer rasterBuf = null; |
43 |
private double[] step = null; |
44 |
/**
|
45 |
* Ancho y alto del objeto image
|
46 |
*/
|
47 |
private int width = 0; |
48 |
private int height = 0; |
49 |
private Rendering rendering = null; |
50 |
|
51 |
public ImageDrawer(Rendering rendering) {
|
52 |
this.rendering = rendering;
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* Dibuja el buffer sobre un objeto Image de java.awt y devuelve el resultado.
|
57 |
*
|
58 |
* @param replicateBand Flag de comportamiento del renderizado. Al renderizar el buffer
|
59 |
* este obtiene la primera banda del buffer y la asigna al R, la segunda al G y la tercera
|
60 |
* al B. Este flag no es tomado en cuenta en caso de que existan 3 bandas en el buffer.
|
61 |
* Si no hay tres bandas, por ejemplo una y el flag es true esta ser? replicada
|
62 |
* en R, G y B, en caso de ser false la banda ser? dibujada en su posici?n (R, G o B)
|
63 |
* y en las otras bandas se rellenar? con 0.
|
64 |
*
|
65 |
* @param transparentBand. Si es true la banda 4 es alpha y si es false no lo es.
|
66 |
*
|
67 |
* @return java.awt.Image con el buffer dibujado.
|
68 |
* @throws InterruptedException
|
69 |
*/
|
70 |
public Image drawBufferOverImageObject(boolean replicateBand, int[] renderBands) throws InterruptedException { |
71 |
if (rasterBuf == null || width == 0 || height == 0) |
72 |
return null; |
73 |
|
74 |
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
75 |
|
76 |
// Dibujado de raster de 1 o 2 bandas.
|
77 |
// adaptBufferToRender(replicateBand, renderBands);
|
78 |
|
79 |
if (rasterBuf.getDataType() != IBuffer.TYPE_BYTE)
|
80 |
rasterBuf = convertToByte(rasterBuf); |
81 |
|
82 |
// Asigna la banda de transparencia si existe esta.
|
83 |
// assignTransparencyBand(renderBands);
|
84 |
|
85 |
byte[] data = new byte[rasterBuf.getBandCount()]; |
86 |
|
87 |
GridTransparency transparency = rendering.getLastTransparency(); |
88 |
if (transparency != null && transparency.isTransparencyActive()) { |
89 |
if (transparency.existAlphaBand() &&
|
90 |
transparency.getAlphaBand() != null &&
|
91 |
(transparency.getAlphaBand().getDataType() != IBuffer.TYPE_BYTE)) |
92 |
transparency.setAlphaBand(convertToByte(transparency.getAlphaBand())); |
93 |
drawWithTransparency(image, data, (step != null));
|
94 |
} else
|
95 |
drawByte(image, data, (step != null));
|
96 |
|
97 |
step = null;
|
98 |
return image;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Calcula los vectores de desplazamiento en pixels en X e Y cuando se supersamplea.
|
103 |
* @param r Array de desplazamientos para las filas. Debe tener espacio reservado
|
104 |
* @param c Array de desplazamientos para las columnas. Debe tener espacio reservado
|
105 |
* cargados.
|
106 |
*/
|
107 |
private void calcSupersamplingStepsArrays(int[] r, int[] c) { |
108 |
double pos = step[1]; |
109 |
for(int row = 0; row < r.length; row ++) { |
110 |
r[row] = (int)(pos / step[3]); |
111 |
pos ++; |
112 |
} |
113 |
pos = step[0];
|
114 |
for(int col = 0; col < c.length; col ++) { |
115 |
c[col] = (int)(pos / step[2]); |
116 |
pos ++; |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Dibuja un raster sobre un BufferedImage
|
122 |
* @param image BufferedImage sobre el que se dibuja
|
123 |
* @param data buffer vacio. Se trata de un array de bytes donde cada elemento representa una banda.
|
124 |
* @param supersampling true si se necesita supersamplear y false si no se necesita
|
125 |
* @throws InterruptedException
|
126 |
*/
|
127 |
private void drawByte(BufferedImage image, byte[] data, boolean supersampling) throws InterruptedException { |
128 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
129 |
|
130 |
if (supersampling) {
|
131 |
int[] r = new int[height]; |
132 |
int[] c = new int[width]; |
133 |
calcSupersamplingStepsArrays(r, c); |
134 |
for (int row = 0; row < height; row++) { |
135 |
for (int col = 0; col < width; col++) { |
136 |
try {
|
137 |
rasterBuf.getElemByte(r[row], c[col], data); |
138 |
image.setRGB(col, row, (0xff000000 + ((data[0] & 0xff) << 16) |
139 |
+ ((data[1] & 0xff) << 8) + (data[2] & 0xff))); |
140 |
} catch (ArrayIndexOutOfBoundsException e) { |
141 |
System.err.println("== Size Image:" + image.getWidth() + " " + image.getHeight()); |
142 |
System.err.println("== Position required:" + col + " " + row); |
143 |
break;
|
144 |
} |
145 |
} |
146 |
if (task.getEvent() != null) |
147 |
task.manageEvent(task.getEvent()); |
148 |
} |
149 |
} else {
|
150 |
for (int row = 0; row < rasterBuf.getHeight(); row++) { |
151 |
for (int col = 0; col < rasterBuf.getWidth(); col++) { |
152 |
try {
|
153 |
rasterBuf.getElemByte(row, col, data); |
154 |
image.setRGB(col, row, (0xff000000 + ((data[0] & 0xff) << 16) |
155 |
+ ((data[1] & 0xff) << 8) + (data[2] & 0xff))); |
156 |
} catch (ArrayIndexOutOfBoundsException ex) { |
157 |
System.err.println("== Size Image:" + image.getWidth() + " " + image.getHeight()); |
158 |
System.err.println("== Position required:" + col + " " + row); |
159 |
break;
|
160 |
} |
161 |
} |
162 |
if (task.getEvent() != null) |
163 |
task.manageEvent(task.getEvent()); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Dibuja un raster sobre un BufferedImage con las propiedades de paleta y transparencia
|
170 |
* @param image BufferedImage sobre el que se dibuja
|
171 |
* @param data buffer vacio. Se trata de un array de bytes donde cada elemento representa una banda.
|
172 |
* @param supersampling true si se necesita supersamplear y false si no se necesita
|
173 |
* @throws InterruptedException
|
174 |
*/
|
175 |
private void drawWithTransparency(BufferedImage image, byte[] data, boolean supersampling) throws InterruptedException { |
176 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().toString());
|
177 |
int value = 0; |
178 |
GridTransparency transparency = rendering.getLastTransparency(); |
179 |
// try {
|
180 |
if (supersampling) {
|
181 |
int[] r = new int[height]; |
182 |
int[] c = new int[width]; |
183 |
calcSupersamplingStepsArrays(r, c); |
184 |
for (int row = 0; row < height; row++) { |
185 |
for (int col = 0; col < width; col++) { |
186 |
try {
|
187 |
rasterBuf.getElemByte(r[row], c[col], data); |
188 |
value = transparency.processRGB(data[0] & 0xff, data[1] & 0xff, data[2] & 0xff, r[row], c[col]); |
189 |
image.setRGB(col, row, value); |
190 |
} catch (ArrayIndexOutOfBoundsException e) { |
191 |
System.err.println("== Size Image:" + image.getWidth() + " " + image.getHeight()); |
192 |
System.err.println("== Position required:" + col + " " + row); |
193 |
break;
|
194 |
} |
195 |
} |
196 |
if (task.getEvent() != null) |
197 |
task.manageEvent(task.getEvent()); |
198 |
} |
199 |
} else {
|
200 |
for (int row = 0; row < rasterBuf.getHeight(); row++) { |
201 |
for (int col = 0; col < rasterBuf.getWidth(); col++) { |
202 |
try {
|
203 |
rasterBuf.getElemByte(row, col, data); |
204 |
value = transparency.processRGB(data[0] & 0xff, data[1] & 0xff, data[2] & 0xff, row, col); |
205 |
image.setRGB(col, row, value); |
206 |
} catch (ArrayIndexOutOfBoundsException e) { |
207 |
System.err.println("== Size Image:" + image.getWidth() + " " + image.getHeight()); |
208 |
System.err.println("== Position required:" + col + " " + row); |
209 |
break;
|
210 |
} |
211 |
} |
212 |
if (task.getEvent() != null) |
213 |
task.manageEvent(task.getEvent()); |
214 |
} |
215 |
} |
216 |
// } finally {
|
217 |
// Quitamos el uso del free para no invocar al garbage collector en numerosas
|
218 |
// iteraciones
|
219 |
// transparency.free();
|
220 |
// }
|
221 |
} |
222 |
/**
|
223 |
* Intercala bandas en el buffer dependiendo de si hay que replicar o meter
|
224 |
* bandas en negro. Esto tiene es valido para buffers con solo una banda ya que
|
225 |
* el dibujado sobre Graphics debe ser R, G, B.
|
226 |
*
|
227 |
* @param replicateBand false si no se replican bandas y las que no existen
|
228 |
* se ponen en negro y false si hay que dibujar la misma en R,G y B. Esto
|
229 |
* tiene sentido si el raster tiene solo 1 o 2 bandas.
|
230 |
* @param renderBands array con las posiciones de renderizado.
|
231 |
* A la hora de renderizar hay que tener en cuenta que solo se renderizan las
|
232 |
* tres primeras bandas del buffer por lo que solo se tienen en cuenta los tres primeros
|
233 |
* elementos. Por ejemplo, el array {1, 0, 3} dibujar? sobre el Graphics las bandas 1,0 y 3 de un
|
234 |
* raster de al menos 4 bandas.La notaci?n con -1 en alguna posici?n del vector solo tiene sentido
|
235 |
* en la visualizaci?n pero no se puede as?gnar una banda del buffer a null.
|
236 |
* Algunos ejemplos:
|
237 |
* <P>
|
238 |
* {-1, 0, -1} Dibuja la banda 0 del raster en la G de la visualizaci?n.
|
239 |
* Si replicateBand es true R = G = B sino R = B = 0
|
240 |
* {1, 0, 3} La R = banda 1 del raster, G = 0 y B = 3
|
241 |
* {0} La R = banda 0 del raster. Si replicateBand es true R = G = B sino G = B = 0
|
242 |
* </P>
|
243 |
*/
|
244 |
/*private void adaptBufferToRender(boolean replicateBand, int[] renderBands){
|
245 |
byte[][] aux = null;
|
246 |
if(rasterBuf.getBandCount() < 3){
|
247 |
for(int i = 0; i < renderBands.length; i++){
|
248 |
if( replicateBand && renderBands[i] == -1)
|
249 |
rasterBuf.replicateBand(0, i);
|
250 |
if( !replicateBand && renderBands[i] == -1){
|
251 |
if(aux == null)
|
252 |
aux = rasterBuf.createByteBand(rasterBuf.getWidth(), rasterBuf.getHeight(), (byte)0);
|
253 |
rasterBuf.addBandByte(i, aux);
|
254 |
}
|
255 |
}
|
256 |
}
|
257 |
}*/
|
258 |
|
259 |
/**
|
260 |
* Asigna al objeto GridTransparency la banda de transparencia si la tiene para
|
261 |
* tenerla en cuenta en el renderizado.
|
262 |
* @param renderBands Lista de bandas a renderizar
|
263 |
* @param ts Objeto con las propiedades de transparencia del Grid.
|
264 |
*/
|
265 |
/*private void assignTransparencyBand(int[] renderBands) {
|
266 |
if(transparency != null){
|
267 |
for(int i = 0; i < transparency.getTransparencyBandNumberList().size(); i ++) {
|
268 |
for(int j = 0; j < renderBands.length; j ++) {
|
269 |
if(renderBands[j] == ((Integer)transparency.getTransparencyBandNumberList().get(i)).intValue()){
|
270 |
if(transparency.getBand() == null)
|
271 |
transparency.setBand(rasterBuf.getBandBuffer(renderBands[j]));
|
272 |
else {
|
273 |
IBuffer outBuf = transparency.getBand().cloneBuffer();
|
274 |
transparency.mergeTransparencyBands(new IBuffer[]{transparency.getBand(), rasterBuf.getBandBuffer(renderBands[j])}, outBuf);
|
275 |
}
|
276 |
}
|
277 |
}
|
278 |
}
|
279 |
}
|
280 |
}*/
|
281 |
|
282 |
private IBuffer convertToByte(IBuffer buf) {
|
283 |
IBuffer b = RasterBuffer.getBuffer(IBuffer.TYPE_BYTE, buf.getWidth(), buf.getHeight(), buf.getBandCount(), true);
|
284 |
if(buf.getDataType() == IBuffer.TYPE_SHORT) {
|
285 |
for (int nBand = 0; nBand < buf.getBandCount(); nBand++) |
286 |
for (int row = 0; row < buf.getHeight(); row++) |
287 |
for (int col = 0; col < buf.getWidth(); col++) |
288 |
b.setElem(row, col, nBand, (byte)(buf.getElemShort(row, col, nBand) & 0xffff)); |
289 |
} |
290 |
if(buf.getDataType() == IBuffer.TYPE_INT) {
|
291 |
for (int nBand = 0; nBand < buf.getBandCount(); nBand++) |
292 |
for (int row = 0; row < buf.getHeight(); row++) |
293 |
for (int col = 0; col < buf.getWidth(); col++) |
294 |
b.setElem(row, col, nBand, (byte)(buf.getElemInt(row, col, nBand) & 0xffffffff)); |
295 |
} |
296 |
if(buf.getDataType() == IBuffer.TYPE_FLOAT) {
|
297 |
for (int nBand = 0; nBand < buf.getBandCount(); nBand++) |
298 |
for (int row = 0; row < buf.getHeight(); row++) |
299 |
for (int col = 0; col < buf.getWidth(); col++) |
300 |
b.setElem(row, col, nBand, (byte)(Math.round(buf.getElemFloat(row, col, nBand)))); |
301 |
} |
302 |
if(buf.getDataType() == IBuffer.TYPE_DOUBLE) {
|
303 |
for (int nBand = 0; nBand < buf.getBandCount(); nBand++) |
304 |
for (int row = 0; row < buf.getHeight(); row++) |
305 |
for (int col = 0; col < buf.getWidth(); col++) |
306 |
b.setElem(row, col, nBand, (byte)(Math.round(buf.getElemDouble(row, col, nBand)))); |
307 |
} |
308 |
return b;
|
309 |
} |
310 |
|
311 |
/**
|
312 |
* Asigna el buffer a renderizar
|
313 |
* @param b Buffer a renderizar
|
314 |
*/
|
315 |
public void setBuffer(IBuffer b) { |
316 |
this.rasterBuf = b;
|
317 |
} |
318 |
|
319 |
/**
|
320 |
* Asigna la paleta asociada al grid
|
321 |
* @param palette
|
322 |
*/
|
323 |
/*public void setPalette(GridPalette palette) {
|
324 |
this.palette = palette;
|
325 |
}*/
|
326 |
|
327 |
/**
|
328 |
* Asigna el desplazamiento en pixeles desde la esquina superior izquierda. Si es null se considera que esta
|
329 |
* funci?n la ha hecho el driver quedando desactivada en el renderizador. Si es as? no debe variar el resultado
|
330 |
* en la visualizacion.
|
331 |
* Si Supersamplea el renderizador se cargar? una matriz de datos 1:1 por lo que se podr? aplicar un filtro
|
332 |
* a este buffer de datos leidos independientemente del zoom que tengamos.
|
333 |
* @param step Desplazamiento
|
334 |
*/
|
335 |
public void setStep(double[] step) { |
336 |
this.step = step;
|
337 |
} |
338 |
|
339 |
/**
|
340 |
* Asigna el ancho y el alto del BufferedImage. Esto es util para cuando hay supersampling
|
341 |
* que el tama?o del objeto Image no coincide con el buffer con los datos raster.
|
342 |
* @param w Ancho
|
343 |
* @param h Alto
|
344 |
*/
|
345 |
public void setBufferSize(int w, int h) { |
346 |
this.width = w;
|
347 |
this.height = h;
|
348 |
} |
349 |
|
350 |
} |