/** * Returns the needed size of the ByteBuffer in bytes. * * @return size of the buffer */ public final int getRequiredBufferSize() { // data.capacity() can not be used if the ByteBuffer was not yet instantiated. // return rasterWidth * rasterHeight * dataInfo.bands * dataInfo.dataSize; return dataAccess.requiredBufferSize(); }
/** * Calculates the position of a pixel in the ByteBuffer. * * This method calculates the position of a pixel and returns the offset to this pixel in bytes. Use this method for * direct access to ByteBuffers. * * @param x * x coordinate * @param y * y coordinate * @return byte offset to the pixel with the specified coordinate or -1 if outside of the bytebuffer. */ public final int calculatePos( int x, int y ) { // check for negative views. int yPos = ( view.y - maxViewData.y ) + y; int xPos = ( view.x - maxViewData.x ) + x; int dataPos = ( xPos * pixelStride ) + ( yPos * lineStride ); if ( yPos < 0 || xPos < 0 || yPos >= ( maxViewData.height ) || xPos >= ( maxViewData.width ) || dataPos > requiredBufferSize() ) { dataPos = -1; } return dataPos; }
/** * @param newData * to use. * @param dataRect * defining the width, height and offset for the data. */ void setByteBuffer( ByteBuffer newData, RasterRect dataRect ) { synchronized ( LOCK ) { if ( newData != null ) { if ( dataRect != null ) { createMaxView( dataRect ); } if ( newData.capacity() < requiredBufferSize() ) { // System.out.println( "required: " + requiredBufferSize() ); // System.out.println( "size: " + newData.capacity() ); LOG.error( "The given byteBuffer does not contain enough space for the current view." ); return; } // update the line stride to match the data. } this.data = newData; } }
/** * Calculates the position of a sample in the ByteBuffer. * * This method calculates the position of a pixel and returns the offset to this pixel in bytes. Use this method for * direct access to ByteBuffers. * * @param x * x coordinate * @param y * y coordinate * @param bandOfView * band index of the sample * @return byte offset to the sample with the specified coordinate or -1 if outside of the bytebuffer. */ public final int calculatePos( int x, int y, int bandOfView ) { // check for negative views. int yPos = ( view.y - maxViewData.y ) + y; int xPos = ( view.x - maxViewData.x ) + x; int dataPos = ( xPos * pixelStride ) + ( yPos * lineStride ) + ( view.getBandOffset( bandOfView ) * bandStride ); if ( yPos < 0 || xPos < 0 || yPos >= ( maxViewData.height ) || xPos >= ( maxViewData.width ) || dataPos > requiredBufferSize() || dataPos < 0 ) { dataPos = -1; } return dataPos; }
/** * Fills the entire buffer with no data values. Note this operation is only possible on writable buffers. */ public void fillWithNoData() { synchronized ( LOCK ) { if ( data == null ) { data = ByteBufferPool.allocate( requiredBufferSize(), false ); } if ( !data.isReadOnly() ) { int pos = 0; int cap = data.capacity(); byte[] noData = dataInfo.getNoDataPixel( new byte[0] ); data.position( 0 ); while ( pos < cap ) { data.put( noData ); pos = data.position(); } } } }
/** * Prepares the byte buffer for reading / writing thus instantiates it with values (no) data; */ public void prepareBuffer() { synchronized ( LOCK ) { if ( data == null ) { data = ByteBufferPool.allocate( requiredBufferSize(), false ); boolean noData = false; if ( reader != null ) { try { BufferResult dataResult = reader.read( maxViewData, data ); if ( dataResult != null ) { data = dataResult.getResult(); data.rewind(); } // RasterRect rect = dataResult.getRect(); // lineStride = pixelStride * rect.width; } catch ( IOException e ) { LOG.debug( "No data available: " + e.getLocalizedMessage(), e ); // the data is no longer available, lets just fill it with no data values noData = true; } } if ( noData ) { fillWithNoData(); } } } }