List of usage examples for javax.imageio ImageReadParam setDestination
public void setDestination(BufferedImage destination)
From source file:org.jimcat.services.imagemanager.ImageUtil.java
/** * this methode will load given image using tiles (saving memory) * /*from w ww.java 2 s . c om*/ * this strategie is spliting the original image up into smaller parts * called tiles. Those tiles are downscaled one by one using given quality. * This results int probably best possible quality but may cost a lot of * time. * * @param reader - * the reader to load image from * @param size - * the resulting image size * @param quality - * the quality used for necessary rendering * @return the image as buffered image * @throws IOException */ @SuppressWarnings("unused") private static BufferedImage loadImageWithTiles(ImageReader reader, Dimension size, ImageQuality quality) throws IOException { // the image buffer used to load tiles ImageTypeSpecifier imageSpec = reader.getImageTypes(0).next(); BufferedImage tile = imageSpec.createBufferedImage(IMAGE_TILE_SIZE.width, IMAGE_TILE_SIZE.height); // the image the result is rendered into BufferedImage result = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = result.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, quality.getHint()); // prepaire image reader parameter ImageReadParam param = reader.getDefaultReadParam(); param.setDestination(tile); // count tiles int width = reader.getWidth(0); int height = reader.getHeight(0); int numX = (int) Math.ceil(width / (float) IMAGE_TILE_SIZE.width); int numY = (int) Math.ceil(height / (float) IMAGE_TILE_SIZE.height); float aspectX = (float) IMAGE_TILE_SIZE.width / width; float aspectY = (float) IMAGE_TILE_SIZE.height / height; // target tile dimension int targetTileWidth = (int) (size.width * aspectX); int targetTileHeight = (int) (size.height * aspectY); // load tiles Rectangle sourceRegion = new Rectangle(); Rectangle targetRegion = new Rectangle(); for (int i = 0; i < numX; i++) { // line increment sourceRegion.x = i * IMAGE_TILE_SIZE.width; sourceRegion.width = Math.min(IMAGE_TILE_SIZE.width, width - sourceRegion.x); targetRegion.x = i * targetTileWidth; targetRegion.width = Math.min(targetTileWidth, size.width - targetRegion.x); for (int j = 0; j < numY; j++) { // row increment sourceRegion.y = j * IMAGE_TILE_SIZE.height; sourceRegion.height = Math.min(IMAGE_TILE_SIZE.height, height - sourceRegion.y); targetRegion.y = j * targetTileHeight; targetRegion.height = Math.min(targetTileHeight, size.height - targetRegion.y); // performe read param.setSourceRegion(sourceRegion); reader.read(0, param); // insert into resulting image int dx1 = targetRegion.x; int dx2 = targetRegion.x + targetRegion.width; int dy1 = targetRegion.y; int dy2 = targetRegion.y + targetRegion.height; g.drawImage(tile, dx1, dy1, dx2, dy2, 0, 0, sourceRegion.width, sourceRegion.height, null); } } // finish drawing g.dispose(); // return result return result; }