Debian Codegraph @

src/com/bulenkov/iconloader/util/ImageUtil.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2016 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.util;
18
19 import com.bulenkov.iconloader.JBHiDPIScaledImage;
20 import org.jetbrains.annotations.NotNull;
21
22 import java.awt.*;
23 import java.awt.image.BufferedImage;
24 import java.awt.image.FilteredImageSource;
25 import java.awt.image.ImageFilter;
26
27 /**
28 * @author Konstantin Bulenkov
29 */
30 public class ImageUtil {
31 public static BufferedImage toBufferedImage(@NotNull Image image) {
32 if (image instanceof JBHiDPIScaledImage) {
33 Image img = ((JBHiDPIScaledImage)image).getDelegate();
34 if (img != null) {
35 image = img;
36 }
37 }
38 if (image instanceof BufferedImage) {
39 return (BufferedImage)image;
40 }
41
42 @SuppressWarnings("UndesirableClassUsage")
43 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
44 Graphics2D g = bufferedImage.createGraphics();
45 g.drawImage(image, 0, 0, null);
46 g.dispose();
47 return bufferedImage;
48 }
49
50 public static int getRealWidth(@NotNull Image image) {
51 if (image instanceof JBHiDPIScaledImage) {
52 Image img = ((JBHiDPIScaledImage)image).getDelegate();
53 if (img != null) image = img;
54 }
55 return image.getWidth(null);
56 }
57
58 public static int getRealHeight(@NotNull Image image) {
59 if (image instanceof JBHiDPIScaledImage) {
60 Image img = ((JBHiDPIScaledImage)image).getDelegate();
61 if (img != null) image = img;
62 }
63 return image.getHeight(null);
64 }
65
66 public static Image filter(Image image, ImageFilter filter) {
67 if (image == null || filter == null) return image;
68 return Toolkit.getDefaultToolkit().createImage(
69 new FilteredImageSource(toBufferedImage(image).getSource(), filter));
70 }
71 }