Debian Codegraph @

src/com/bulenkov/iconloader/RetinaImage.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.bulenkov.iconloader;
18
19 import com.bulenkov.iconloader.util.SystemInfo;
20 import com.bulenkov.iconloader.util.UIUtil;
21
22 import java.awt.*;
23 import java.awt.image.BufferedImage;
24 import java.awt.image.ImageObserver;
25
26 /**
27 * @author Konstantin Bulenkov
28 */
29 public class RetinaImage {
30
31 /**
32 * Creates a Retina-aware wrapper over a raw image.
33 * The raw image should be provided in scale of the Retina default scale factor (2x).
34 * The wrapper will represent the raw image in the user coordinate space.
35 *
36 * @param image the raw image
37 * @return the Retina-aware wrapper
38 */
39 public static Image createFrom(Image image) {
40 return createFrom(image, 2, IconLoader.ourComponent);
41 }
42
43 /**
44 * Creates a Retina-aware wrapper over a raw image.
45 * The raw image should be provided in the specified scale.
46 * The wrapper will represent the raw image in the user coordinate space.
47 *
48 * @param image the raw image
49 * @param scale the raw image scale
50 * @param observer the raw image observer
51 * @return the Retina-aware wrapper
52 */
53 public static Image createFrom(Image image, final int scale, ImageObserver observer) {
54 int w = image.getWidth(observer);
55 int h = image.getHeight(observer);
56
57 Image hidpi = create(image, w / scale, h / scale, BufferedImage.TYPE_INT_ARGB);
58 if (SystemInfo.isAppleJvm) {
59 Graphics2D g = (Graphics2D)hidpi.getGraphics();
60 g.scale(1f / scale, 1f / scale);
61 g.drawImage(image, 0, 0, null);
62 g.dispose();
63 }
64
65 return hidpi;
66 }
67
68 public static BufferedImage create(final int width, int height, int type) {
69 return create(null, width, height, type);
70 }
71
72
73 private static BufferedImage create(Image image, final int width, int height, int type) {
74 if (SystemInfo.isAppleJvm) {
75 return AppleHiDPIScaledImage.create(width, height, type);
76 } else {
77 if (image == null) {
78 return new JBHiDPIScaledImage(width, height, type);
79 } else {
80 return new JBHiDPIScaledImage(image, width, height, type);
81 }
82 }
83 }
84
85 public static boolean isAppleHiDPIScaledImage(Image image) {
86 return UIUtil.isAppleRetina() && AppleHiDPIScaledImage.is(image);
87 }
88 }