RFR: 8282526: Default icon is not painted properly
Alexey Ivanov
aivanov at openjdk.java.net
Wed Apr 6 11:32:39 UTC 2022
On Thu, 17 Mar 2022 21:55:05 GMT, Phil Race <prr at openjdk.org> wrote:
>> Detect the situation where we do need to perform interpolation during ImageIcon
>> painting and set a hint to the rendering to perform bicubic approximation so
>> image details are preserved during transition.
>
> src/java.desktop/share/classes/javax/swing/ImageIcon.java line 463:
>
>> 461: if (hintChanged) {
>> 462: ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
>> 463: oldHint == null ? RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR : oldHint);
>
> So this is one of only two hints which do not have an "_DEFAULT" value. I don't know why it doesn't.
>
> Not authoritative but the only place where we currently set this hint and then restore it is here
> share/classes/javax/swing/plaf/nimbus/AbstractRegionPainter.java
> Object oldScaleingHints = g.getRenderingHint(RenderingHints.KEY_INTERPOLATION);
> g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
> ImageScalingHelper.paint(g, 0, 0, w, h, img, insets, dstInsets,
> ImageScalingHelper.PaintType.PAINT9_STRETCH, ImageScalingHelper.PAINT_ALL);
> g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
> oldScaleingHints!=null?oldScaleingHints:RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
>
> if the get() returns null and you aren't happy with that then creating a new Graphics is the only alternative I see.
>
>
> But it seems to me there are bigger problems to resolve first
>
>> Image variant = ((MultiResolutionImage) image).getResolutionVariant(this.width, this.height);
>
> So getResolutionVariant wants image pixels, but I expect ImageIcon is sized in user-space, isn't it ?
>
> Don't you need to apply the scale from the Graphics ?
>
> Next, if we have an exact match for what we want, why do we need the hint ?
>
> Next, as Sergey mentions, BICUBIC is expensive.
> Probably how that should be used is to redraw the image into a cached image that
> is dw= iw*sx and dh= iwh*sy where sx and sy are the graphics scale
> and that is the graphics on which you apply the hint and the graphics is disposed and the restore issue vanishes
>
> then you can just blit that resulting image so long as the cached dw x dy isn't changed.
>
> The MR is still useful for finding the best starting image
If I understand correctly, the problem occurs only when the image is scaled down. The test case renders 32×32 icon into 16×16 image.
So is the usage of bicubic interpolation need only when the image is scaled down?
-------------
PR: https://git.openjdk.java.net/jdk/pull/7805
More information about the client-libs-dev
mailing list