/** * Creates an image consisting of the given selection of the source image rescaled to a square with the given size. * If the selection is null, a not-entirely-stupid algorithm for determining a plausible square subselection. * * @param sourceImage the image source to crop and scale from. * @param crop the subrectangle to use, usually square to avoid aspect ratio changes. * @param size the desired size of the edge of the resulting square image. * @return the new image. */ public RenderedImage getSelectedImageData(final BufferedImage sourceImage, Selection crop, final int size) { if (crop == null) { crop = autoCalculateImageCropSelection(sourceImage.getWidth(), sourceImage.getHeight(), size); } CropImageFilter cropFilter = new CropImageFilter(crop.getTopLeftX(), crop.getTopLeftY(), crop.getWidth(), crop.getHeight()); AreaAveragingScaleFilter scaleFilter = new AreaAveragingScaleFilter(size, size); FilteredImageSource croppedImageProducer = new FilteredImageSource(sourceImage.getSource(), cropFilter); FilteredImageSource croppedAndResizedImageProducer = new FilteredImageSource(croppedImageProducer, scaleFilter); ImageGenerator generator = new ImageGenerator(); croppedAndResizedImageProducer.startProduction(generator); BufferedImage scaledImage = generator.getImage(); scaledImage.flush(); return scaledImage; }