svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / buffer / WriterBufferServer.java @ 20119
History | View | Annotate | Download (8.26 KB)
1 | 18854 | nbrodin | /* 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.buffer; |
||
20 | |||
21 | import org.gvsig.raster.dataset.IBuffer; |
||
22 | import org.gvsig.raster.dataset.IDataWriter; |
||
23 | /**
|
||
24 | * <code>WriterBufferServer</code> sirve los datos desde un IBuffer para el
|
||
25 | * driver de escritura. El driver de escritura que va a volcar un
|
||
26 | * <code>IBuffer</code> a imagen en disco va solicitando los datos por bloques
|
||
27 | * a medida que va salvando los anteriores. Esta clase es la encargada de hacer
|
||
28 | * la divisi?n en bloques del buffer a volcar e ir sirviendolos a medida que el
|
||
29 | * driver pide m?s datos.
|
||
30 | * </P>
|
||
31 | * <P>
|
||
32 | * Implementa el interfaz <code>IDataWriter</code> que es el que define
|
||
33 | * m?todos necesarios para el driver. Adem?s es una tarea incrementable por lo
|
||
34 | * que deber? implementar el interfaz <code>IIncrementable</code> para poder
|
||
35 | * mostrar el dialogo de incremento de tarea.
|
||
36 | * </P>
|
||
37 | *
|
||
38 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
39 | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
||
40 | * @version 27/04/2007
|
||
41 | */
|
||
42 | public class WriterBufferServer implements IDataWriter { |
||
43 | private IBuffer buffer = null; |
||
44 | private IBuffer alphaBuffer = null; |
||
45 | private int row = 0; |
||
46 | /**
|
||
47 | * Tama?o de bloque. Este se asignar? en la primera petici?n
|
||
48 | */
|
||
49 | private int block = 0; |
||
50 | |||
51 | private double percent = 0; |
||
52 | private double increment = 0; |
||
53 | private int nBand = 0; |
||
54 | |||
55 | /**
|
||
56 | * Crea un nuevo <code>WriterBufferServer</code>
|
||
57 | */
|
||
58 | public WriterBufferServer() {
|
||
59 | } |
||
60 | |||
61 | /**
|
||
62 | * Crea un nuevo <code>WriterBufferServer</code> con el buffer de datos.
|
||
63 | * @param buffer
|
||
64 | */
|
||
65 | public WriterBufferServer(IBuffer buffer) {
|
||
66 | setBuffer(buffer, -1);
|
||
67 | } |
||
68 | |||
69 | /**
|
||
70 | * Asigna el buffer de datos e inicializa variables de
|
||
71 | * @param buffer
|
||
72 | * @param nband Si es menor que cero sirve datos de todas las bandas. Si es
|
||
73 | * mayor que cero sirve datos de la banda indicada por el valor.
|
||
74 | */
|
||
75 | public void setBuffer(IBuffer buffer, int nband) { |
||
76 | this.buffer = buffer;
|
||
77 | nBand = nband; |
||
78 | row = 0;
|
||
79 | block = 0;
|
||
80 | percent = 0;
|
||
81 | increment = 0;
|
||
82 | } |
||
83 | |||
84 | /*
|
||
85 | * (non-Javadoc)
|
||
86 | * @see org.gvsig.raster.dataset.IDataWriter#readARGBData(int, int, int)
|
||
87 | */
|
||
88 | public int[] readARGBData(int sizeX, int sizeY, int nBand) { |
||
89 | return null; |
||
90 | } |
||
91 | |||
92 | /**
|
||
93 | * Acciones de inicializaci?n para la lectura de de un bloque
|
||
94 | * @param sizeY Tama?o en Y del bloque
|
||
95 | */
|
||
96 | private void initRead(int sizeY) { |
||
97 | //Si es la primera linea se asigna el tama?o de bloque y el incremento
|
||
98 | if (row == 0) { |
||
99 | block = sizeY; |
||
100 | //nblocks = (int)Math.ceil(((double) buffer.getHeight() / (double) sizeY));
|
||
101 | increment = 100D / ((double) buffer.getHeight() / (double) sizeY); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /*
|
||
106 | * (non-Javadoc)
|
||
107 | * @see org.gvsig.raster.dataset.IDataWriter#readByteData(int, int)
|
||
108 | */
|
||
109 | public byte[][] readByteData(int sizeX, int sizeY) { |
||
110 | initRead(sizeY); |
||
111 | percent += increment; |
||
112 | int len = buffer.getWidth() * sizeY;
|
||
113 | int nbands = (nBand < 0) ? buffer.getBandCount() : 1; |
||
114 | byte[][] b; |
||
115 | if (alphaBuffer != null) |
||
116 | b = new byte[nbands + 1][len]; |
||
117 | else
|
||
118 | b = new byte[nbands][len]; |
||
119 | if(nBand < 0) { |
||
120 | for (int iBand = 0; iBand < nbands; iBand++) |
||
121 | for (int j = row; j < (row + sizeY); j++) |
||
122 | for (int i = 0; i < buffer.getWidth(); i++) |
||
123 | b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemByte(j, i, iBand); |
||
124 | } else {
|
||
125 | for (int j = row; j < (row + sizeY); j++) |
||
126 | for (int i = 0; i < buffer.getWidth(); i++) |
||
127 | b[0][(j % block) * buffer.getWidth() + i] = buffer.getElemByte(j, i, nBand);
|
||
128 | } |
||
129 | if (alphaBuffer != null) { |
||
130 | for (int j = row; j < (row + sizeY); j++) |
||
131 | for (int i = 0; i < alphaBuffer.getWidth(); i++) { |
||
132 | b[nbands][(j % block) * alphaBuffer.getWidth() + i] = alphaBuffer.getElemByte(j, i, 0);
|
||
133 | } |
||
134 | } |
||
135 | row += sizeY; |
||
136 | return b;
|
||
137 | } |
||
138 | |||
139 | /*
|
||
140 | * (non-Javadoc)
|
||
141 | * @see org.gvsig.raster.dataset.IDataWriter#readShortData(int, int)
|
||
142 | */
|
||
143 | public short[][] readShortData(int sizeX, int sizeY) { |
||
144 | initRead(sizeY); |
||
145 | percent += increment; |
||
146 | int len = buffer.getWidth() * sizeY;
|
||
147 | int nbands = (nBand < 0) ? buffer.getBandCount() : 1; |
||
148 | short[][] b = new short[nbands][len]; |
||
149 | if(nBand < 0) { |
||
150 | for (int iBand = 0; iBand < nbands; iBand++) |
||
151 | for (int j = row; j < (row + sizeY); j++) |
||
152 | for (int i = 0; i < buffer.getWidth(); i++) |
||
153 | b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemShort(j, i, iBand); |
||
154 | } else {
|
||
155 | for (int j = row; j < (row + sizeY); j++) |
||
156 | for (int i = 0; i < buffer.getWidth(); i++) |
||
157 | b[0][(j % block) * buffer.getWidth() + i] = buffer.getElemShort(j, i, nBand);
|
||
158 | } |
||
159 | row += sizeY; |
||
160 | return b;
|
||
161 | } |
||
162 | |||
163 | /*
|
||
164 | * (non-Javadoc)
|
||
165 | * @see org.gvsig.raster.dataset.IDataWriter#readIntData(int, int)
|
||
166 | */
|
||
167 | public int[][] readIntData(int sizeX, int sizeY) { |
||
168 | initRead(sizeY); |
||
169 | percent += increment; |
||
170 | int len = buffer.getWidth() * sizeY;
|
||
171 | int nbands = (nBand < 0) ? buffer.getBandCount() : 1; |
||
172 | int[][] b = new int[nbands][len]; |
||
173 | if(nBand < 0) { |
||
174 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
||
175 | for (int j = row; j < (row + sizeY); j++) |
||
176 | for (int i = 0; i < buffer.getWidth(); i++) |
||
177 | b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemInt(j, i, iBand); |
||
178 | } else {
|
||
179 | for (int j = row; j < (row + sizeY); j++) |
||
180 | for (int i = 0; i < buffer.getWidth(); i++) |
||
181 | b[0][(j % block) * buffer.getWidth() + i] = buffer.getElemInt(j, i, nBand);
|
||
182 | } |
||
183 | row += sizeY; |
||
184 | return b;
|
||
185 | } |
||
186 | |||
187 | /*
|
||
188 | * (non-Javadoc)
|
||
189 | * @see org.gvsig.raster.dataset.IDataWriter#readFloatData(int, int)
|
||
190 | */
|
||
191 | public float[][] readFloatData(int sizeX, int sizeY) { |
||
192 | initRead(sizeY); |
||
193 | percent += increment; |
||
194 | int len = buffer.getWidth() * sizeY;
|
||
195 | int nbands = (nBand < 0) ? buffer.getBandCount() : 1; |
||
196 | float[][] b = null; |
||
197 | if (alphaBuffer != null) |
||
198 | b = new float[nbands + 1][len]; |
||
199 | else
|
||
200 | b = new float[nbands][len]; |
||
201 | if(nBand < 0) { |
||
202 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
||
203 | for (int j = row; j < (row + sizeY); j++) |
||
204 | for (int i = 0; i < buffer.getWidth(); i++) |
||
205 | b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemFloat(j, i, iBand); |
||
206 | } else {
|
||
207 | for (int j = row; j < (row + sizeY); j++) |
||
208 | for (int i = 0; i < buffer.getWidth(); i++) |
||
209 | b[0][(j % block) * buffer.getWidth() + i] = buffer.getElemFloat(j, i, nBand);
|
||
210 | } |
||
211 | |||
212 | if (alphaBuffer != null) { |
||
213 | for (int j = row; j < (row + sizeY); j++) |
||
214 | for (int i = 0; i < alphaBuffer.getWidth(); i++) { |
||
215 | b[nbands][(j % block) * alphaBuffer.getWidth() + i] = alphaBuffer.getElemByte(j, i, 0);
|
||
216 | } |
||
217 | } |
||
218 | |||
219 | row += sizeY; |
||
220 | return b;
|
||
221 | } |
||
222 | |||
223 | /*
|
||
224 | * (non-Javadoc)
|
||
225 | * @see org.gvsig.raster.dataset.IDataWriter#readDoubleData(int, int)
|
||
226 | */
|
||
227 | public double[][] readDoubleData(int sizeX, int sizeY) { |
||
228 | initRead(sizeY); |
||
229 | percent += increment; |
||
230 | int len = buffer.getWidth() * sizeY;
|
||
231 | int nbands = (nBand < 0) ? buffer.getBandCount() : 1; |
||
232 | double[][] b = new double[nbands][len]; |
||
233 | if(nBand < 0) { |
||
234 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) |
||
235 | for (int j = row; j < (row + sizeY); j++) |
||
236 | for (int i = 0; i < buffer.getWidth(); i++) |
||
237 | b[iBand][(j % block) * buffer.getWidth() + i] = buffer.getElemDouble(j, i, iBand); |
||
238 | } else {
|
||
239 | for (int j = row; j < (row + sizeY); j++) |
||
240 | for (int i = 0; i < buffer.getWidth(); i++) |
||
241 | b[0][(j % block) * buffer.getWidth() + i] = buffer.getElemDouble(j, i, nBand);
|
||
242 | } |
||
243 | row += sizeY; |
||
244 | return b;
|
||
245 | } |
||
246 | |||
247 | /**
|
||
248 | * Obtiene el porcentaje de incremento de la lectura de datos
|
||
249 | * @return
|
||
250 | */
|
||
251 | public int getPercent() { |
||
252 | return Math.min((int)percent, 100); |
||
253 | } |
||
254 | |||
255 | /**
|
||
256 | * @param alphaBuffer the alphaBuffer to set
|
||
257 | */
|
||
258 | public void setAlphaBuffer(IBuffer alphaBuffer) { |
||
259 | this.alphaBuffer = alphaBuffer;
|
||
260 | } |
||
261 | } |