Parse given InputStream and rebuild RenderedImage.
165 final boolean littleEndian = stream.read() == 1;
168 ds =
new LEDataInputStream(stream);
171 ds =
new DataInputStream(stream);
174 final int version = ds.readUnsignedShort();
175 final int nbBand = ds.readUnsignedShort();
177 final double scaleX = ds.readDouble();
178 final double scaleY = ds.readDouble();
179 final double ipX = ds.readDouble();
180 final double ipY = ds.readDouble();
181 final double skewX = ds.readDouble();
182 final double skewY = ds.readDouble();
183 gridToCRS =
new AffineTransform2D(scaleX, skewY, skewX, scaleY, ipX, ipY);
187 final int width = ds.readUnsignedShort();
188 final int height = ds.readUnsignedShort();
195 final WKBRasterBand[] bands =
new WKBRasterBand[nbBand];
197 for(
int i=0;i<nbBand;i++){
198 final WKBRasterBand band =
new WKBRasterBand();
200 final byte b = ds.readByte();
201 band.setPixelType(b & BANDTYPE_PIXTYPE_MASK);
202 band.setOffDatabase( (b & BANDTYPE_FLAG_OFFDB) != 0);
203 band.setHasNodata( (b & BANDTYPE_FLAG_HASNODATA) != 0);
204 band.setIsNodata( (b & BANDTYPE_FLAG_ISNODATA) != 0);
205 band.setReserved( (b & BANDTYPE_FLAG_RESERVED3) != 0);
208 switch (band.getPixelType()) {
213 band.setNoDataValue(ds.readUnsignedByte());
216 band.setNoDataValue(ds.readByte());
219 band.setNoDataValue(ds.readShort());
222 band.setNoDataValue(ds.readUnsignedShort());
225 band.setNoDataValue(ds.readInt());
228 band.setNoDataValue(ds.readInt() & 0x00000000ffffffffL);
231 band.setNoDataValue(ds.readFloat());
234 band.setNoDataValue(ds.readDouble());
237 throw new IOException(
"unknowned pixel type : "+band.getPixelType());
240 if(band.isOffDatabase()){
241 throw new IOException(
"can not access data which are off database");
244 final int nbBytePerPixel = band.getNbBytePerPixel();
245 final byte[] datas =
new byte[width*height*band.getNbBytePerPixel()];
247 if(littleEndian && nbBytePerPixel > 1){
250 for(
int k=0;k<datas.length;k+=nbBytePerPixel){
251 for(
int p=0,n=nbBytePerPixel/2; p<n ;p++){
252 final int index1 = k+p;
253 final int index2 = k+(nbBytePerPixel-p-1);
254 temp = datas[index1];
255 datas[index1] = datas[index2];
256 datas[index2] = temp;
260 band.setDatas(datas);
267 final int dataBufferType = bands[0].getDataBufferType();
270 final WritableRaster raster;
271 double min = Double.MAX_VALUE;
272 double max = Double.MIN_VALUE;
274 if(dataBufferType == DataBuffer.TYPE_BYTE){
277 Integer dataType =
null;
278 final byte[][] dataArray =
new byte[nbBand][0];
279 final int[] bankIndices =
new int[nbBand];
280 final int[] bankOffsets =
new int[nbBand];
281 for(
int i=0;i<bands.length;i++){
282 final WKBRasterBand band = bands[i];
283 if(dataType ==
null){
284 dataType = band.getDataBufferType();
285 }
else if(dataType != band.getDataBufferType()){
286 throw new IOException(
"Band type differ, can not be mapped to java image.");
288 dataArray[i] = band.getDatas();
297 final DataBuffer db =
new DataBufferByte(dataArray, dataArray[0].length);
298 final int scanlineStride = width;
299 raster = RasterFactory.createBandedRaster(
300 db, width, height, scanlineStride, bankIndices, bankOffsets,
new Point(0,0));
303 raster = RasterFactory.createBandedRaster(dataBufferType,width,height,nbBand,
new Point(0,0));
304 for(
int i=0;i<bands.length;i++){
305 final byte[] datas = bands[i].getDatas();
306 final DataInputStream dds =
new DataInputStream(
new ByteArrayInputStream(datas));
307 for(
int y=0;y<height;y++){
308 for(
int x=0;x<width;x++){
309 switch (dataBufferType) {
310 case DataBuffer.TYPE_SHORT:
311 short d1 = dds.readShort();
312 raster.setSample(x, y, i, d1);
313 min = Math.min(min, (
double)d1);
314 max = Math.max(max, (
double)d1);
316 case DataBuffer.TYPE_USHORT:
317 int d2 = dds.readUnsignedShort();
318 raster.setSample(x, y, i, d2);
319 min = Math.min(min, (
double)d2);
320 max = Math.max(max, (
double)d2);
322 case DataBuffer.TYPE_INT:
323 int d3 = dds.readInt();
324 raster.setSample(x, y, i, d3);
325 min = Math.min(min, (
double)d3);
326 max = Math.max(max, (
double)d3);
328 case DataBuffer.TYPE_FLOAT:
329 float d4 = dds.readFloat();
330 raster.setSample(x, y, i, d4);
331 min = Math.min(min, (
double)d4);
332 max = Math.max(max, (
double)d4);
334 case DataBuffer.TYPE_DOUBLE:
335 double d5 = dds.readDouble();
336 raster.setSample(x, y, i, d5);
337 min = Math.min(min, d5);
338 max = Math.max(max, d5);
341 throw new IllegalArgumentException(
"unknowned data buffer type : " + dataBufferType);
348 final SampleModel sm = raster.getSampleModel();
349 ColorModel cm = PlanarImage.getDefaultColorModel(sm.getDataType(), raster.getNumBands());
352 cm = (ColorModel) ColorModelFactory.GRAYSCALE;
354 return new BufferedImage(cm, raster,
false,
null);