Revision 2308 org.gvsig.raster/branches/org.gvsig.raster_dataaccess_refactoring/org.gvsig.raster.lib/org.gvsig.raster.lib.impl/src/main/java/org/gvsig/raster/impl/provider/MemoryTileMatrixBuffer.java

View differences:

MemoryTileMatrixBuffer.java
42 42
public class MemoryTileMatrixBuffer {
43 43
	//private Logger           log          = LoggerFactory.getLogger(MemoryMatrixBuffer.class);
44 44
	private Buffer[]         bufferList   = null;
45
	private Buffer[]         transpList   = null;
46 45
	private int              nRows        = 0;
47 46
	private int              nCols        = 0;	
48 47
	@SuppressWarnings("unused")
49 48
	private Extent           bbox         = null;
50 49
	private List<Tile>       tileList     = null;
50
	private int              dataType     = Buffer.TYPE_UNDEFINED;
51 51
	
52 52
	public class Position {
53 53
		int col = 0;
......
62 62
	public MemoryTileMatrixBuffer(Tile[] tileList) {
63 63
		this.tileList = new ArrayList<Tile>();
64 64
		bufferList = new Buffer[tileList.length];
65
		if(tileList[0].getData().length > 1 && tileList[0].getData()[1] instanceof Buffer)
66
			transpList = new Buffer[tileList.length];
67 65
		for (int i = 0; i < tileList.length; i++) {
68 66
			this.tileList.add(tileList[i]);
69 67
			bufferList[i] = (Buffer)tileList[i].getData()[0];
70
			if(tileList[i].getData().length > 1 && tileList[i].getData()[1] instanceof Buffer)
71
				transpList[i] = (Buffer)tileList[i].getData()[1];
68
			if(bufferList[i] != null)
69
				dataType = bufferList[i].getDataType();
72 70
			//extentList[i] = tileList[i].getExtent();
73 71
			//pxLayerList[i] = tileList[i].getCoordsPx();
74 72
		}
......
99 97
	public MemoryTileMatrixBuffer(List<Tile> tileList) {
100 98
		this.tileList = tileList;
101 99
		bufferList = new Buffer[tileList.size()];
102
		if(tileList.get(0).getData().length > 1 && tileList.get(0).getData()[1] instanceof Buffer)
103
			transpList = new Buffer[tileList.size()];
104 100
		for (int i = 0; i < tileList.size(); i++) {
105 101
			bufferList[i] = (Buffer)tileList.get(i).getData()[0];
106
			if(tileList.get(i).getData().length > 1 && tileList.get(i).getData()[1] instanceof Buffer)
107
				transpList[i] = (Buffer)tileList.get(i).getData()[1];
102
			if(bufferList[i] != null)
103
				dataType = bufferList[i].getDataType();
108 104
		}
109 105
		int minRow = Integer.MAX_VALUE;
110 106
		int maxRow = Integer.MIN_VALUE;
......
154 150
	 * @param buf
155 151
	 * @return
156 152
	 */
157
	public Buffer getWindow(Rectangle2D r, Buffer buf) {
153
	public Buffer getWindow(Rectangle2D r, int w, int h, int bandCount) {
158 154
		int shiftRow = 0;
159 155
		int shiftCol = 0;
160 156
		int posRow = 0;
......
216 212
			rBufferShiftList[i] = new Rectangle2D.Double(initPxBufferX, initPxBufferY, widthPxBuffer, heightPxBuffer);
217 213
		}			
218 214
		
215
		Buffer buf = DefaultRasterManager.getInstance().createBuffer(dataType, w, h, bandCount, true);
216
		
219 217
		for (int i = 0; i < bufferList.length; i++) {
220 218
			loadBuffer(buf, 
221 219
					bufferList[i],
......
229 227
	
230 228
	/**
231 229
	 * Gets a window from tiles
232
	 * @param ext
233
	 * @param buf
234
	 * @return
235
	 */
236
	public Buffer getWindow(Extent ext, Buffer buf) {
237
		return getWindow(ext, buf, false);
238
	}
239
	
240
	/**
241
	 * Gets a window from tiles
242 230
	 * @param requestExtent
243 231
	 * @param buf
244 232
	 * @return
245 233
	 */
246
	public Buffer getWindow(Extent requestExtent, Buffer buf, boolean alpha) {
234
	public Buffer getWindow(Extent requestExtent, int w, int h, int bandCount) {
247 235
		if(tileList.size() <= 0)
248 236
			return null;
249 237
		
250
		Buffer sourceWithoutResampling = createBufferWithoutResampling(requestExtent, buf, tileList.get(0).getExtent(), bufferList[0]);
238
		Buffer sourceWithoutResampling = createBufferWithoutResampling(requestExtent, tileList.get(0).getExtent(), bufferList[0], w, h, bandCount);
251 239
		double wcX1 = -1;
252 240
		double wcY1 = -1;
253 241
		int initXPxBuf = 0;
......
273 261

  
274 262
			//4-Copiar recorte al buffer
275 263

  
276
			if(alpha)
277
				loadBuffer(sourceWithoutResampling, transpList[i], rTile, initXPxBuf, initYPxBuf, true);
278
			else
279
				loadBuffer(sourceWithoutResampling, bufferList[i], rTile, initXPxBuf, initYPxBuf, false);
264
			loadBuffer(sourceWithoutResampling, bufferList[i], rTile, initXPxBuf, initYPxBuf, false);
280 265
		}
281 266
		/*try {
282 267
			save(sourceWithoutResampling, true, requestExtent, null);
......
292 277
		//Devuelve el buffer pero reescalandolo antes al tama?o en pixeles de la petici?n
293 278
		try {
294 279
			Buffer result = null;
295
			result = sourceWithoutResampling.getAdjustedWindow(buf.getWidth(), buf.getHeight(), Buffer.INTERPOLATION_NearestNeighbour);
280
			result = sourceWithoutResampling.getAdjustedWindow(w, h, Buffer.INTERPOLATION_NearestNeighbour);
296 281
			if(result != sourceWithoutResampling)
297 282
				sourceWithoutResampling.dispose();
298 283
			return result;
299 284
		} catch (ProcessInterruptedException e) {
300 285
		}
301
		return buf;
286
		return null;
302 287
	}
303 288
	
304 289
	/**
......
337 322
	
338 323
	/**
339 324
	 * Write data in the source buffer taking into account the view shift
340
	 * @param sourceBuf
325
	 * @param dstBuf
341 326
	 * @param tileBuf
342 327
	 * @param rTile
343 328
	 * @param initXPxBuf
344 329
	 * @param initYPxBuf
345 330
	 */
346
	private void loadBuffer(Buffer sourceBuf, Buffer tileBuf, Rectangle2D rTile, int initXPxBuf, int initYPxBuf, boolean alpha) {
331
	private void loadBuffer(Buffer dstBuf, Buffer tileBuf, Rectangle2D rTile, int initXPxBuf, int initYPxBuf, boolean alpha) {
347 332
		int r = initXPxBuf;
348 333
		int c = initYPxBuf;
349 334
		//if(!alpha) {
350 335
			if(tileBuf.getDataType() == Buffer.TYPE_BYTE) {
351 336
				for (int band = 0; band < tileBuf.getBandCount(); band++) {
352 337
					r = initYPxBuf;
353
					for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < sourceBuf.getHeight()); row++) {
338
					for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < dstBuf.getHeight()); row++) {
354 339
						c = initXPxBuf;
355
						for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < sourceBuf.getWidth()); col++) {
356
							sourceBuf.setElem(r, c, band, tileBuf.getElemByte(row, col, band));
340
						for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < dstBuf.getWidth()); col++) {
341
							dstBuf.setElem(r, c, band, tileBuf.getElemByte(row, col, band));
357 342
							c++;
358 343
						}
359 344
						r++;
......
376 361
		if(tileBuf.getDataType() == Buffer.TYPE_SHORT) {
377 362
			for (int band = 0; band < tileBuf.getBandCount(); band++) {
378 363
				r = initYPxBuf;
379
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < sourceBuf.getHeight()); row++) {
364
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < dstBuf.getHeight()); row++) {
380 365
					c = initXPxBuf;
381
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < sourceBuf.getWidth()); col++) {
382
						sourceBuf.setElem(r, c, band, tileBuf.getElemShort(row, col, band));
366
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < dstBuf.getWidth()); col++) {
367
						dstBuf.setElem(r, c, band, tileBuf.getElemShort(row, col, band));
383 368
						c++;
384 369
					}
385 370
					r++;
......
389 374
		if(tileBuf.getDataType() == Buffer.TYPE_INT) {
390 375
			for (int band = 0; band < tileBuf.getBandCount(); band++) {
391 376
				r = initYPxBuf;
392
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < sourceBuf.getHeight()); row++) {
377
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < dstBuf.getHeight()); row++) {
393 378
					c = initXPxBuf;
394
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < sourceBuf.getWidth()); col++) {
395
						sourceBuf.setElem(r, c, band, tileBuf.getElemInt(row, col, band));
379
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < dstBuf.getWidth()); col++) {
380
						dstBuf.setElem(r, c, band, tileBuf.getElemInt(row, col, band));
396 381
						c++;
397 382
					}
398 383
					r++;
......
402 387
		if(tileBuf.getDataType() == Buffer.TYPE_FLOAT) {
403 388
			for (int band = 0; band < tileBuf.getBandCount(); band++) {
404 389
				r = initYPxBuf;
405
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < sourceBuf.getHeight()); row++) {
390
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < dstBuf.getHeight()); row++) {
406 391
					c = initXPxBuf;
407
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < sourceBuf.getWidth()); col++) {
408
						sourceBuf.setElem(r, c, band, tileBuf.getElemFloat(row, col, band));
392
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < dstBuf.getWidth()); col++) {
393
						dstBuf.setElem(r, c, band, tileBuf.getElemFloat(row, col, band));
409 394
						c++;
410 395
					}
411 396
					r++;
......
415 400
		if(tileBuf.getDataType() == Buffer.TYPE_DOUBLE) {
416 401
			for (int band = 0; band < tileBuf.getBandCount(); band++) {
417 402
				r = initYPxBuf;
418
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < sourceBuf.getHeight()); row++) {
403
				for (int row = (int)rTile.getMinY(); (row < (int)rTile.getMaxY() && r < dstBuf.getHeight()); row++) {
419 404
					c = initXPxBuf;
420
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < sourceBuf.getWidth()); col++) {
421
						sourceBuf.setElem(r, c, band, tileBuf.getElemDouble(row, col, band));
405
					for (int col = (int)rTile.getMinX(); (col < (int)rTile.getMaxX() && c < dstBuf.getWidth()); col++) {
406
						dstBuf.setElem(r, c, band, tileBuf.getElemDouble(row, col, band));
422 407
						c++;
423 408
					}
424 409
					r++;
......
489 474
	 * @return
490 475
	 */
491 476
	private Buffer createBufferWithoutResampling(Extent extOrigin, 
492
			Buffer bufOrigin, 
493 477
			Rectangle2D extTile, 
494
			Buffer bufTile) {
495
		double psOrigin = extOrigin.width() / bufOrigin.getWidth();
478
			Buffer bufTile,
479
			int originWidth, 
480
			int originHeight,
481
			int bandCount) {
482
		double psOrigin = extOrigin.width() / originWidth;
496 483
		double psTile = extTile.getWidth() / bufTile.getWidth();
497 484
		double rel = psTile / psOrigin;
498
		int w = (int)Math.floor(bufOrigin.getWidth() / rel);
499
		int h = (int)Math.floor(bufOrigin.getHeight() / rel);
500
		return DefaultRasterManager.getInstance().createBuffer(bufOrigin.getDataType(), w, h, bufOrigin.getBandCount(), true);
485
		int w = (int)Math.floor(originWidth / rel);
486
		int h = (int)Math.floor(originHeight / rel);
487
		return DefaultRasterManager.getInstance().createBuffer(dataType, w, h, bandCount, true);
501 488
	}
502 489
	
503 490
	

Also available in: Unified diff