Debian Codegraph @

src/com/github/weisj/jsvg/util/ImageUtil.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.util;
23
24 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
25 import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;
26
27 import java.awt.*;
28 import java.awt.geom.AffineTransform;
29 import java.awt.image.*;
30
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
35 import com.github.weisj.jsvg.renderer.RenderContext;
36 import com.github.weisj.jsvg.renderer.output.Output;
37 import com.github.weisj.jsvg.renderer.output.impl.GraphicsUtil;
38
39 public final class ImageUtil {
40
41 private ImageUtil() {}
42
43 public enum Premultiplied {
44 Yes,
45 No
46 }
47
48 public static @NotNull BufferedImage createCompatibleTransparentImage(@NotNull Output output,
49 double width, double height) {
50 return createCompatibleTransparentImage(output.transform(), width, height);
51 }
52
53 public static @NotNull BufferedImage createCompatibleTransparentImage(int width, int height) {
54 return createCompatibleTransparentImage(width, height, Premultiplied.No);
55 }
56
57 public static @NotNull BufferedImage createCompatibleTransparentImage(int width, int height,
58 Premultiplied preMultiplied) {
59 int type = preMultiplied == Premultiplied.Yes ? TYPE_INT_ARGB_PRE : TYPE_INT_ARGB;
60 return new BufferedImage(width, height, type);
61 }
62
63 public static @NotNull BufferedImage createCompatibleTransparentImage(@Nullable AffineTransform at, double width,
64 double height) {
65 return createCompatibleTransparentImage(
66 (int) Math.ceil(GeometryUtil.scaleXOfTransform(at) * width),
67 (int) Math.ceil(GeometryUtil.scaleYOfTransform(at) * height));
68 }
69
70 public static @NotNull BufferedImage createLuminosityBuffer(@Nullable AffineTransform at, double width,
71 double height) {
72 return new BufferedImage(
73 (int) Math.ceil(GeometryUtil.scaleXOfTransform(at) * width),
74 (int) Math.ceil(GeometryUtil.scaleYOfTransform(at) * height), BufferedImage.TYPE_BYTE_GRAY);
75 }
76
77 public static boolean is_INT_PACK_Data(@NotNull SampleModel sm, boolean requireAlpha) {
78 if (!(sm instanceof SinglePixelPackedSampleModel)) return false;
79 if (sm.getDataType() != DataBuffer.TYPE_INT) return false;
80
81 int[] masks = ((SinglePixelPackedSampleModel) sm).getBitMasks();
82 if (masks.length == 3) {
83 if (requireAlpha) {
84 return false;
85 }
86 } else if (masks.length != 4) {
87 return false;
88 }
89
90 if (masks[0] != 0x00ff0000) return false;
91 if (masks[1] != 0x0000ff00) return false;
92 if (masks[2] != 0x000000ff) return false;
93 return (masks.length != 4) || (masks[3] == 0xff000000);
94 }
95
96 public static int[] getINT_RGBA_DataBank(@NotNull Raster raster) {
97 DataBufferInt dstDB = (DataBufferInt) raster.getDataBuffer();
98 return dstDB.getBankData()[0];
99 }
100
101 public static int getINT_RGBA_DataOffset(@NotNull Raster raster) {
102 DataBufferInt dstDB = (DataBufferInt) raster.getDataBuffer();
103 SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) raster.getSampleModel();
104 return dstDB.getOffset() + sppsm.getOffset(
105 raster.getMinX() - raster.getSampleModelTranslateX(),
106 raster.getMinY() - raster.getSampleModelTranslateY());
107 }
108
109 public static int getINT_RGBA_DataAdjust(@NotNull Raster raster) {
110 return getINT_RGBA_ScanlineStride(raster) - raster.getWidth();
111 }
112
113 public static int getINT_RGBA_ScanlineStride(@NotNull Raster raster) {
114 return ((SinglePixelPackedSampleModel) raster.getSampleModel()).getScanlineStride();
115 }
116
117 public static @NotNull BufferedImage copy(@NotNull RenderContext context, @NotNull ImageProducer producer,
118 Premultiplied premultiplied) {
119 return toBufferedImage(context.platformSupport().createImage(producer), premultiplied);
120 }
121
122 public static @NotNull BufferedImage toBufferedImage(@NotNull Image img) {
123 return toBufferedImage(img, Premultiplied.No);
124 }
125
126 public static @NotNull BufferedImage toBufferedImage(@NotNull Image img, Premultiplied premultiplied) {
127 BufferedImage bufferedImage = createCompatibleTransparentImage(img.getWidth(null), img.getHeight(null),
128 premultiplied);
129 Graphics2D g = GraphicsUtil.createGraphics(bufferedImage);
130 g.drawImage(img, null, null);
131 g.dispose();
132 return bufferedImage;
133 }
134
135 public static @NotNull ColorModel coerceData(@NotNull WritableRaster wr, @NotNull ColorModel cm,
136 boolean newAlphaPreMultiplied) {
137 if (!cm.hasAlpha()) return cm;
138 if (cm.isAlphaPremultiplied() == newAlphaPreMultiplied) return cm;
139 return cm.coerceData(wr, newAlphaPreMultiplied);
140 }
141 }