BaseMultiResolutionImage
via reflection,
* in case it is on classpath. This is to be expected for Java 9, but not for Java 8 runtime.
*
* @since 12722
*/
public final class HiDPISupport {
private static volatile Optional
* Will only return multi-resolution image, if HiDPI-mode is detected. Then
* the image stack will consist of the base image and one that fits the
* HiDPI scale of the main display.
* @param base the base image
* @param ir a corresponding image resource
* @return multi-resolution image if necessary and possible, the base image otherwise
*/
public static Image getMultiResolutionImage(Image base, ImageResource ir) {
double uiScale = getHiDPIScale();
if (uiScale != 1.0 && getBaseMultiResolutionImageConstructor().isPresent()) {
ImageIcon zoomed = ir.getImageIcon(new Dimension(
(int) Math.round(base.getWidth(null) * uiScale),
(int) Math.round(base.getHeight(null) * uiScale)), false);
Image mrImg = getMultiResolutionImage(Arrays.asList(base, zoomed.getImage()));
if (mrImg != null) return mrImg;
}
return base;
}
/**
* Create a multi-resolution image from a list of images.
* @param imgs the images, supposedly the same image at different resolutions,
* must not be empty
* @return corresponding multi-resolution image, if possible, the first image
* in the list otherwise
*/
public static Image getMultiResolutionImage(List
* Will return the argument
* Will return the argument as a singleton list, in case it is not a multi-resolution image.
* @param img the image
* @return if
* This method may not work as expected for a multi-monitor setup. It will
* only take the default screen device into account.
* @return the GUI scale for HiDPI mode, a value of 1.0 means standard mode.
*/
private static double getHiDPIScale() {
if (GraphicsEnvironment.isHeadless())
return 1.0;
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().
getDefaultConfiguration();
AffineTransform transform = gc.getDefaultTransform();
if (!Utils.equalsEpsilon(transform.getScaleX(), transform.getScaleY())) {
Logging.warn("Unexpected ui transform: " + transform);
}
return transform.getScaleX();
}
/**
* Perform an operation on multi-resolution images.
*
* When input image is not multi-resolution, it will simply apply the processor once.
* Otherwise, the processor will be called for each resolution variant and the
* resulting images assembled to become the output multi-resolution image.
* @param img input image, possibly multi-resolution
* @param processor processor taking a plain image as input and returning a single
* plain image as output
* @return multi-resolution image assembled from the output of calls to java.awt.image.BaseMultiResolutionImage#getBaseImage()
.
* img
unchanged, if it is not a multi-resolution image.
* @param img the image
* @return if img
is a java.awt.image.BaseMultiResolutionImage
,
* then the base image, otherwise the image itself
*/
public static Image getBaseImage(Image img) {
Optionaljava.awt.image.MultiResolutionImage#getResolutionVariants()
.
* img
is a java.awt.image.BaseMultiResolutionImage
,
* then the result of the method #getResolutionVariants()
, otherwise the image
* itself as a singleton list
*/
public static Listprocessor
* for each resolution variant
*/
public static Image processMRImage(Image img, Functionprocessor
* for each resolution variant
*/
public static Image processMRImages(List, Image> processor) {
CheckParameterUtil.ensureThat(!imgs.isEmpty(), "at least one element expected");
if (!getBaseMultiResolutionImageClass().isPresent()) {
return processor.apply(imgs);
}
List
> allVars = imgs.stream().map(HiDPISupport::getResolutionVariants).collect(Collectors.toList());
int maxVariants = allVars.stream().mapToInt(lst -> lst.size()).max().getAsInt();
if (maxVariants == 1)
return processor.apply(imgs);
List