Revision 4181 org.gvsig.raster.lizardtech/trunk/org.gvsig.raster.lizardtech/org.gvsig.raster.lizardtech.io/src/main/java/org/gvsig/raster/lizardtech/io/LizardTechProvider.java

View differences:

LizardTechProvider.java
3 3
import java.awt.geom.AffineTransform;
4 4
import java.awt.geom.Point2D;
5 5
import java.awt.image.BufferedImage;
6
import java.io.File;
7
import java.net.URI;
8
import java.net.URISyntaxException;
6 9

  
10
import es.gva.cit.jmrsid.MrSIDException;
11

  
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

  
7 15
import org.gvsig.fmap.dal.DALFileLocator;
8 16
import org.gvsig.fmap.dal.DALLocator;
9 17
import org.gvsig.fmap.dal.DataStore;
......
14 22
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
15 23
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
16 24
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
25
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
17 26
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
18 27
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
19 28
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
20 29
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
30
import org.gvsig.fmap.dal.exception.OpenException;
21 31
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
22 32
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
23 33
import org.gvsig.metadata.MetadataLocator;
......
35 45
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
36 46
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
37 47
import org.gvsig.tools.ToolsLocator;
38

  
39
import es.gva.cit.jmrsid.MrSIDException;
40 48
/**
41 49
 * Clase encargada del acceso a los datos y repintado de imagenes MrSID. Estos
42 50
 * son registrados con la extensi?n sid
......
45 53
 * @author Nacho Brodin (nachobrodin@gmail.com)
46 54
 */
47 55
public class LizardTechProvider extends AbstractRasterProvider {
56
    private static final Logger logger = LoggerFactory.getLogger(LizardTechProvider.class);
57

  
48 58
	public static String                 NAME                     = "LizardTech Store";
49 59
	public static String                 DESCRIPTION              = "LizardTech Raster file";
50 60
	public static final String           METADATA_DEFINITION_NAME = "LizardTechStore";
51
	protected LizardTechNative           file                     = null;
61
	protected LizardTechNative           lztNative                     = null;
52 62
	private Extent                       viewRequest              = null;
53 63
	private DataStoreColorInterpretation colorInterpr             = null;
54 64
	protected DataStoreTransparency      fileTransparency         = null;
55 65
	private boolean                      open                     = false;
56 66
	protected static String[]            formatList               = null;
57
	
67

  
58 68
	public static void register() {
59 69
		RasterLocator.getManager().getProviderServices().registerFileProvidersTiled(LizardTechProvider.class);
60 70
		registerFormats();
61
		
71

  
62 72
		DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
63 73
		if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
64 74
			dataman.registerStoreProvider(NAME,
65 75
					LizardTechProvider.class, LizardTechDataParameters.class);
66 76
		}
67
		
77

  
68 78
		if(DALFileLocator.getFilesystemServerExplorerManager() != null)
69 79
			DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
70 80
					NAME, DESCRIPTION,
71 81
					LizardTechFilesystemServerExplorer.class);
72
		
82

  
73 83
		dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
74 84
	}
75
	
85

  
76 86
	private static void registerFormats() {
77 87
		formatList      = new String[] {"sid"};
78
		for (int i = 0; i < formatList.length; i++) 
88
		for (int i = 0; i < formatList.length; i++)
79 89
			RasterLocator.getManager().getProviderServices().addFormat(formatList[i], LizardTechProvider.class);
80 90
	}
81
	
91

  
82 92
	public String[] getFormatList() {
83 93
		return formatList;
84 94
	}
85
	
95

  
86 96
	/**
87 97
	 * Returns true if the extension is supported and false if doesn't
88 98
	 * @param ext
......
97 107
		}
98 108
		return false;
99 109
	}
100
	
110

  
101 111
	/**
102 112
	 * Mandatory constructor to instantiate an empty provider
103 113
	 */
104 114
	public LizardTechProvider() {
105 115
	}
106
	
116

  
107 117
	/**
108 118
	 * Constructor. Abre el dataset.
109 119
	 * @param proj Proyecci?n
110 120
	 * @param fName Nombre del fichero ecw
111 121
	 * @throws NotSupportedExtensionException
122
	 * @throws OpenException
123
     * @deprecated use {@link #LizardTechProvider(URI)}, this constructor will be removed in gvSIG 2.5
112 124
	 */
113
	public LizardTechProvider(String params) throws NotSupportedExtensionException {
125
	public LizardTechProvider(String params) throws NotSupportedExtensionException, OpenException {
114 126
		super(params);
127
        logger.info("Deprecated use of LizardTechProvider constructor");
115 128
		if(params instanceof String) {
116 129
			LizardTechDataParameters p = new LizardTechDataParameters();
117
			p.setURI((String)params);
130
            URI uriObj;
131
            try {
132
                uriObj = new URI((String)params);
133
            } catch (URISyntaxException e) {
134
                throw new OpenException("Can't create uri from "+(String)params, e);
135
            }
136
            p.setURI(uriObj);
118 137
			super.init(p, null, ToolsLocator.getDynObjectManager()
119 138
					.createDynObject(
120 139
							MetadataLocator.getMetadataManager().getDefinition(
......
122 141
			init(p, null);
123 142
		}
124 143
	}
125
	
144

  
145
    /**
146
     * Constructor. Abre el dataset.
147
     * @param proj Proyecci?n
148
     * @param fName Nombre del fichero ecw
149
     * @throws NotSupportedExtensionException
150
     */
151
    public LizardTechProvider(URI uri) throws NotSupportedExtensionException {
152
        super(uri);
153
        LizardTechDataParameters p = new LizardTechDataParameters();
154
        p.setURI(uri);
155
        super.init(
156
            p,
157
            null,
158
            ToolsLocator.getDynObjectManager().createDynObject(
159
                MetadataLocator.getMetadataManager().getDefinition(DataStore.METADATA_DEFINITION_NAME)));
160
        init(p, null);
161
    }
162

  
126 163
	public LizardTechProvider(LizardTechDataParameters params,
127 164
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
128 165
		super(params, storeServices, ToolsLocator.getDynObjectManager()
......
141 178
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
142 179
		setParam(storeServices, params);
143 180
		try {
144
			file = new LizardTechNative(params.getURI());
181
			lztNative = new LizardTechNative(params.getURI().getPath());
145 182
			load();
146
			bandCount = file.nbands;
183
			bandCount = lztNative.nbands;
147 184
			int[] dt = new int[bandCount];
148 185
			for (int i = 0; i < dt.length; i++)
149 186
				dt[i] = Buffer.TYPE_BYTE;
......
158 195
		} catch (Exception e) {
159 196
			System.out.println("Error en constructor de MrSID");
160 197
			e.printStackTrace();
161
			file = null;
198
			lztNative = null;
162 199
		}
163 200
		open = true;
164 201
	}
165 202

  
166 203
	public RasterProvider load() {
167
		ownTransformation = file.getOwnTransformation();
204
		ownTransformation = lztNative.getOwnTransformation();
168 205
		externalTransformation = (AffineTransform) ownTransformation.clone();
169 206
		return this;
170 207
	}
171
	
208

  
172 209
	public boolean isOpen() {
173 210
		return open;
174 211
	}
175 212

  
176 213
	public void close() {
177
		if (file != null) {
178
			file.close();
179
			file = null;
214
		if (lztNative != null) {
215
			lztNative.close();
216
			lztNative = null;
180 217
		}
181 218
		open = false;
182 219
	}
......
190 227
	}
191 228

  
192 229
	public double getWidth() {
193
		return file.width;
230
		return lztNative.width;
194 231
	}
195 232

  
196 233
	public double getHeight() {
197
		return file.height;
234
		return lztNative.height;
198 235
	}
199 236

  
200 237
	/**
......
215 252
	}
216 253

  
217 254
	public Object getData(int x, int y, int band) throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
218
		if (file != null) {
219
			if (x < 0 || y < 0 || x >= file.width || y >= file.height)
255
		if (lztNative != null) {
256
			if (x < 0 || y < 0 || x >= lztNative.width || y >= lztNative.height)
220 257
				throw new InvalidSetViewException("Request out of grid");
221
			Object[] data = file.getData(x, y);
258
			Object[] data = lztNative.getData(x, y);
222 259
			return data[band];
223 260
		}
224 261
		throw new FileNotOpenException("MrSIDNative not exist");
225 262
	}
226 263

  
227 264
	public int getBlockSize() {
228
		return file.blocksize;
265
		return lztNative.blocksize;
229 266
	}
230 267

  
231 268
	/**
......
234 271
	 * @return true si se ha supersampleado y false si no se ha hecho.
235 272
	 */
236 273
	public boolean isSupersampling() {
237
		return file.isSupersampling;
274
		return lztNative.isSupersampling;
238 275
	}
239
	
276

  
240 277
	@Override
241 278
	public void loadBuffer(SpiRasterQuery query)
242 279
			throws ProcessInterruptedException, RasterDriverException {
243 280
		setView(query.getAdjustedRequestBoundingBox());
244 281
		int width = query.getAdjustedBufWidth();
245 282
		int height = query.getAdjustedBufHeight();
246
		file.setView(viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), width, height);
283
		lztNative.setView(viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), width, height);
247 284

  
248 285
		int[] pRGBArray = new int[width * height];
249 286

  
250 287
		try {
251 288
			RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
252
			file.readScene(pRGBArray, task);
253
			loadBuffer(height, 
254
					width, 
255
					query.getBandList(), 
256
					query.getBufferForProviders(), 
289
			lztNative.readScene(pRGBArray, task);
290
			loadBuffer(height,
291
					width,
292
					query.getBandList(),
293
					query.getBufferForProviders(),
257 294
					pRGBArray);
258 295
		} catch (MrSIDException e) {
259 296
			throw new RasterDriverException("Error reading data");
260 297
		}
261 298
	}
262
	
299

  
263 300
//	private void loadCachedBuffer(	Buffer buf,
264 301
//									Extent inputWindow,
265
//									RasterTask task, 
266
//									int[] pRGBArray, 
302
//									RasterTask task,
303
//									int[] pRGBArray,
267 304
//									BandList bandList) throws ProcessInterruptedException, MrSIDException {
268 305
//		if(buf.isCached()) {
269 306
//			Point2D blockHeightWC = rasterToWorld(new Point2D.Double(buf.getBlockHeight(), buf.getBlockHeight()));
......
277 314
//			for (int i = 0; i < nBlocks; i++) {
278 315
//				if(lastblockWC > 0 && i == (nBlocks - 1)) {
279 316
//					ext = RasterLocator.getManager().getDataStructFactory().createExtent(
280
//							inputWindow.getULX(), 
281
//							init, 
282
//							inputWindow.getLRX(), 
317
//							inputWindow.getULX(),
318
//							init,
319
//							inputWindow.getLRX(),
283 320
//							lastblockWC);
284 321
//					file.setView(ext.getULX(), ext.getULY(), ext.getLRX(), ext.getLRY(), buf.getWidth(), buf.getBlockHeight());
285 322
//					file.readScene(pRGBArray, task);
286
//					loadPartialBuffer(new int[]{0, i * buf.getBlockHeight(), buf.getWidth(), buf.getHeight() }, 
287
//									bandList, 
288
//									buf, 
289
//									pRGBArray, 
323
//					loadPartialBuffer(new int[]{0, i * buf.getBlockHeight(), buf.getWidth(), buf.getHeight() },
324
//									bandList,
325
//									buf,
326
//									pRGBArray,
290 327
//									buf.getWidth());
291 328
//				} else {
292 329
//					ext = RasterLocator.getManager().getDataStructFactory().createExtent(
293
//							inputWindow.getULX(), 
294
//							init, 
295
//							inputWindow.getLRX(), 
330
//							inputWindow.getULX(),
331
//							init,
332
//							inputWindow.getLRX(),
296 333
//							init - blockHeightWC.getX());
297 334
//					file.setView(ext.getULX(), ext.getULY(), ext.getLRX(), ext.getLRY(), buf.getWidth(), lastBlock);
298 335
//					file.readScene(pRGBArray, task);
299
//					loadPartialBuffer(new int[]{0, i * buf.getBlockHeight(), buf.getWidth(), i * buf.getBlockHeight() + buf.getBlockHeight() }, 
300
//							bandList, 
301
//							buf, 
302
//							pRGBArray, 
336
//					loadPartialBuffer(new int[]{0, i * buf.getBlockHeight(), buf.getWidth(), i * buf.getBlockHeight() + buf.getBlockHeight() },
337
//							bandList,
338
//							buf,
339
//							pRGBArray,
303 340
//							buf.getWidth());
304 341
//					init += buf.getBlockHeight();
305 342
//				}
......
310 347
//			loadBuffer(buf.getWidth(), buf.getHeight(), bandList, buf, pRGBArray);
311 348
//		}
312 349
//	}
313
//	
314
//	private void loadPartialBuffer(int[] stepBuffer, 
315
//			BandList bandList, 
316
//			Buffer rasterBuf, 
317
//			int[] pRGBArray, 
350
//
351
//	private void loadPartialBuffer(int[] stepBuffer,
352
//			BandList bandList,
353
//			Buffer rasterBuf,
354
//			int[] pRGBArray,
318 355
//			int bufWidth) throws ProcessInterruptedException {
319 356
//		RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
320 357
//		int[] drawableBandsR = bandList.getBufferBandToDraw(getURIOfFirstProvider(), 0);
......
322 359
//		int[] drawableBandsB = bandList.getBufferBandToDraw(getURIOfFirstProvider(), 2);
323 360
//		int element = 0;
324 361
//		int rowSrc = 0;
325
//		
362
//
326 363
//		for (int row = stepBuffer[1]; row < stepBuffer[3]; row++) {
327 364
//			int colSrc = 0;
328 365
//			for (int col = stepBuffer[0]; col < stepBuffer[2]; col++) {
......
350 387
//			if (task.getEvent() != null)
351 388
//				task.manageEvent(task.getEvent());
352 389
//			rowSrc ++;
353
//		}	
390
//		}
354 391
//	}
355
	
392

  
356 393
	private void loadBuffer(int h, int w, BandList bandList, Buffer rasterBuf, int[] pRGBArray) throws ProcessInterruptedException {
357 394
		RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
358
		int[] drawableBandsR = bandList.getBufferBandToDraw(getURIOfFirstProvider(), 0);
359
		int[] drawableBandsG = bandList.getBufferBandToDraw(getURIOfFirstProvider(), 1);
360
		int[] drawableBandsB = bandList.getBufferBandToDraw(getURIOfFirstProvider(), 2);
395
		int[] drawableBandsR = bandList.getBufferBandToDraw(getURIOfFirstProvider().getPath(), 0);
396
		int[] drawableBandsG = bandList.getBufferBandToDraw(getURIOfFirstProvider().getPath(), 1);
397
		int[] drawableBandsB = bandList.getBufferBandToDraw(getURIOfFirstProvider().getPath(), 2);
361 398
		int element = 0;
362
		
399

  
363 400
		for (int row = 0; row < h; row++) {
364 401
			for (int col = 0; col < w; col++) {
365 402
				element = pRGBArray[(row * w) + col];
......
384 421
			}
385 422
			if (task.getEvent() != null)
386 423
				task.manageEvent(task.getEvent());
387
		}	
424
		}
388 425
	}
389 426

  
390 427
	public Object readBlock(int pos, int blockHeight, double scale) throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
......
393 430
		if (pos < 0)
394 431
			throw new InvalidSetViewException("Request out of grid");
395 432

  
396
		if ((pos + blockHeight) > file.height)
397
			blockHeight = Math.abs(file.height - pos);
433
		if ((pos + blockHeight) > lztNative.height)
434
			blockHeight = Math.abs(lztNative.height - pos);
398 435

  
399 436
		Point2D begin = rasterToWorld(new Point2D.Double(0, pos));
400
		Point2D end = rasterToWorld(new Point2D.Double(file.width, pos + blockHeight));
437
		Point2D end = rasterToWorld(new Point2D.Double(lztNative.width, pos + blockHeight));
401 438

  
402
		int w = file.width;
439
		int w = lztNative.width;
403 440

  
404
		file.setView(begin.getX(), begin.getY(), end.getX(), end.getY(), w, blockHeight);
441
		lztNative.setView(begin.getX(), begin.getY(), end.getX(), end.getY(), w, blockHeight);
405 442

  
406
		int[] pRGBArray = new int[file.width * blockHeight];
443
		int[] pRGBArray = new int[lztNative.width * blockHeight];
407 444
		try {
408
			file.readScene(pRGBArray, task);
445
			lztNative.readScene(pRGBArray, task);
409 446
			byte[][][] buf = new byte[3][blockHeight][w];
410 447
			for (int row = 0; row < blockHeight; row++) {
411 448
				for (int col = 0; col < w; col++) {
......
443 480
		try {
444 481
			Extent extent = getExtent();
445 482
			Point2D pt = rasterToWorld(new Point2D.Double(extent.minX(), line));
446
			file.setView(extent.minX(), pt.getY(), extent.maxX(), pt.getY(), (int)getWidth(), 1);
483
			lztNative.setView(extent.minX(), pt.getY(), extent.maxX(), pt.getY(), (int)getWidth(), 1);
447 484
			int[] pRGBArray = new int[(int)getWidth()];
448
			file.readScene(pRGBArray, task);
485
			lztNative.readScene(pRGBArray, task);
449 486
			return pRGBArray;
450 487
		} catch (MrSIDException e) {
451 488
			throw new RasterDriverException("Error reading data from MrSID library");
......
484 521

  
485 522
	public void setAffineTransform(AffineTransform t) {
486 523
		super.setAffineTransform(t);
487
		file.setExternalTransform(t);
524
		lztNative.setExternalTransform(t);
488 525
	}
489 526

  
490 527
	public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
491 528
		if (band >= getBandCount())
492 529
			throw new BandAccessException("Wrong band");
493 530
		try {
494
			return file.getNumLevels();
531
			return lztNative.getNumLevels();
495 532
		} catch (MrSIDException e) {
496 533
			throw new RasterDriverException("");
497 534
		}
......
513 550
		// No podemos escribir por lo que no podemos informar de que soporta overviews aunque el formato si lo haga.
514 551
		return false;
515 552
	}
516
	
553

  
517 554
	public String getProviderName() {
518 555
		return NAME;
519 556
	}
520
	
557

  
521 558
	public void setStatus(RasterProvider provider) {
522 559
		if(provider instanceof LizardTechProvider) {
523 560
			//Not implemented yet
524 561
		}
525 562
	}
526
	
563

  
527 564
	public TileServer getTileServer() {
528 565
		if(tileServer == null) {
529 566
			DefaultRasterStore store = new DefaultRasterStore();
......
533 570
		return tileServer;
534 571
	}
535 572

  
573
    public void addFile(File file) throws InvalidSourceException {
574
        // Do nothing
575
    }
536 576

  
577
    public void removeFile(File file) {
578
        // Do nothing
579
    }
580

  
581

  
537 582
}

Also available in: Unified diff