public BufferedImage getImage ( ) throws IOException { if ( image == null ) { // No content, no image if ( bufferedOut == null ) { return null ; } // Read from the byte buffer InputStream byteStream = bufferedOut . createInputStream ( ) ; ImageInputStream input = null ; try { input = ImageIO . createImageInputStream ( byteStream ) ; Iterator readers = ImageIO . getImageReaders ( input ) ; if ( readers . hasNext ( ) ) { // Get the correct reader ImageReader reader = ( ImageReader ) readers . next ( ) ; try { reader . setInput ( input ) ; ImageReadParam param = reader . getDefaultReadParam ( ) ; // Get default size int originalWidth = reader . getWidth ( 0 ) ; int originalHeight = reader . getHeight ( 0 ) ; ////////////////// // PRE-PROCESS (prepare): param, size, format?, request, response? // TODO: AOI strategy? // Extract AOI from request Rectangle aoi = extractAOIFromRequest ( originalWidth , originalHeight , originalRequest ) ; if ( aoi != null ) { param . setSourceRegion ( aoi ) ; originalWidth = aoi . width ; originalHeight = aoi . height ; } // TODO: Size and subsampling strategy? // If possible, extract size from request Dimension size = extractSizeFromRequest ( originalWidth , originalHeight , originalRequest ) ; double readSubSamplingFactor = getReadSubsampleFactorFromRequest ( originalRequest ) ; if ( size != null ) { //System.out.println("Size: " + size); if ( param . canSetSourceRenderSize ( ) ) { param . setSourceRenderSize ( size ) ; } else { int subX = ( int ) Math . max ( originalWidth / ( size . width * readSubSamplingFactor ) , 1.0 ) ; int subY = ( int ) Math . max ( originalHeight / ( size . height * readSubSamplingFactor ) , 1.0 ) ; if ( subX > 1 || subY > 1 ) { param . setSourceSubsampling ( subX , subY , subX > 1 ? subX / 2 : 0 , subY > 1 ? subY / 2 : 0 ) ; } } } // Need base URI for SVG with links/stylesheets etc maybeSetBaseURIFromRequest ( param ) ; ///////////////////// // Finally, read the image using the supplied parameter BufferedImage image = reader . read ( 0 , param ) ; // TODO: If we sub-sampled, it would be a good idea to blur before resampling, // to avoid jagged lines artifacts // If reader doesn't support dynamic sizing, scale now image = resampleImage ( image , size ) ; // Fill bgcolor behind image, if transparent extractAndSetBackgroundColor ( image ) ; // TODO: Move to flush/POST-PROCESS // Set image this . image = image ; } finally { reader . dispose ( ) ; } } else { context . log ( "ERROR: No suitable image reader found (content-type: " + originalContentType + ")." ) ; context . log ( "ERROR: Available formats: " + getFormatsString ( ) ) ; throw new IIOException ( "Unable to transcode image: No suitable image reader found (content-type: " + originalContentType + ")." ) ; } // Free resources, as the image is now either read, or unreadable bufferedOut = null ; } finally { if ( input != null ) { input . close ( ) ; } } } // Image is usually a BufferedImage, but may also be a RenderedImage return image != null ? ImageUtil . toBuffered ( image ) : null ; }
Gets the decoded image from the response .