Debian Codegraph @

src/com/github/weisj/jsvg/paint/impl/MaskedPaint.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2026 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.paint.impl;
23
24 import static com.github.weisj.jsvg.util.ColorUtil.div255;
25
26 import java.awt.*;
27 import java.awt.PaintContext;
28 import java.awt.geom.AffineTransform;
29 import java.awt.geom.Point2D;
30 import java.awt.geom.Rectangle2D;
31 import java.awt.image.*;
32
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35
36 import com.github.weisj.jsvg.attributes.MaskType;
37 import com.github.weisj.jsvg.renderer.output.Output;
38 import com.github.weisj.jsvg.renderer.output.impl.GraphicsUtil;
39 import com.github.weisj.jsvg.util.CachedSurfaceSupplier;
40
41 public final class MaskedPaint implements Paint, GraphicsUtil.WrappingPaint, GraphicsUtil.DisposablePaint {
42 private @NotNull Paint paint;
43 private final @NotNull Raster maskRaster;
44 private final @NotNull Point maskOffset;
45 private final @Nullable CachedSurfaceSupplier.ResourceCleaner cleaner;
46 private final @NotNull MaskType maskType;
47
48 public MaskedPaint(@NotNull Paint paint, @NotNull Raster maskRaster, @NotNull Point2D maskOffset,
49 @Nullable CachedSurfaceSupplier.ResourceCleaner cleaner, @NotNull MaskType maskType) {
50 this.paint = paint;
51 this.maskRaster = maskRaster;
52 this.maskOffset = new Point((int) Math.floor(maskOffset.getX()), (int) Math.floor(maskOffset.getY()));
53 this.cleaner = cleaner;
54 this.maskType = maskType;
55 }
56
57 @Override
58 public void cleanupIfNeeded(@NotNull Output output) {
59 if (cleaner != null) cleaner.clean(output);
60 }
61
62 @Override
63 public void setPaint(@NotNull Paint paint) {
64 // Note: Inner masked paints are handled in GraphicsUtil.safelySetPaint
65 this.paint = paint;
66 }
67
68 @Override
69 public @NotNull Paint paint() {
70 return paint;
71 }
72
73 @Override
74 public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds,
75 AffineTransform xform, RenderingHints hints) {
76 PaintContext parentContext = paint.createContext(null, deviceBounds, userBounds, xform, hints);
77 return new MaskPaintContext(parentContext, maskRaster, maskOffset, maskType);
78 }
79
80 @Override
81 public int getTransparency() {
82 return Transparency.TRANSLUCENT;
83 }
84
85 private static final class MaskPaintContext implements PaintContext {
86 private final @NotNull PaintContext parentContext;
87 private final @NotNull ColorModel colorModel;
88 private final int numColorComponents;
89 private final @NotNull ColorModel parentColorModel;
90 private final @NotNull Raster maskRaster;
91 private final @NotNull Point offset;
92 private final int maskBand;
93
94 MaskPaintContext(@NotNull PaintContext parentContext, @NotNull Raster maskRaster,
95 @NotNull Point offset, @NotNull MaskType maskType) {
96 this.parentContext = parentContext;
97 this.parentColorModel = parentContext.getColorModel();
98 this.maskRaster = maskRaster;
99 this.offset = offset;
100 if (parentContext.getColorModel().hasAlpha()) {
101 colorModel = parentColorModel;
102 } else {
103 colorModel = new ComponentColorModel(parentContext.getColorModel()
104 .getColorSpace(), true, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
105 }
106 numColorComponents = colorModel.getNumColorComponents();
107 this.maskBand = (maskType == MaskType.Alpha) ? maskRaster.getNumBands() - 1 : 0;
108 }
109
110 @Override
111 public @NotNull ColorModel getColorModel() {
112 return colorModel;
113 }
114
115 @Override
116 public void dispose() {
117 parentContext.dispose();
118 }
119
120 @Override
121 public Raster getRaster(int x, int y, int w, int h) {
122 Raster parentRaster = parentContext.getRaster(x, y, w, h);
123
124 int parentMinX = parentRaster.getMinX();
125 int parentMinY = parentRaster.getMinY();
126
127 WritableRaster result;
128 if (parentRaster instanceof WritableRaster) {
129 if (parentColorModel.equals(colorModel)) {
130 result = parentRaster.createCompatibleWritableRaster();
131 result.setDataElements(-parentMinX, -parentMinY, parentRaster);
132 } else {
133 BufferedImage parentImage = new BufferedImage(parentColorModel,
134 (WritableRaster) parentRaster,
135 parentColorModel.isAlphaPremultiplied(), null);
136 result = Raster.createWritableRaster(
137 colorModel.createCompatibleSampleModel(w, h), new Point(0, 0));
138 BufferedImage resultImage = new BufferedImage(colorModel, result, false, null);
139 Graphics graphics = resultImage.getGraphics();
140 graphics.drawImage(parentImage, 0, 0, null);
141 graphics.dispose();
142 }
143 } else {
144 result = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, w, h,
145 getColorModel().getNumComponents(), new Point(0, 0));
146 ColorConvertOp colorConvertOp = new ColorConvertOp(
147 parentColorModel.getColorSpace(), colorModel.getColorSpace(), null);
148 colorConvertOp.filter(parentRaster, result);
149 }
150
151 int softMaskMinX = maskRaster.getMinX();
152 int softMaskMinY = maskRaster.getMinY();
153 int softMaskMaxX = softMaskMinX + maskRaster.getWidth();
154 int softMaskMaxY = softMaskMinY + maskRaster.getHeight();
155
156 for (int j = 0; j < h; j++) {
157 for (int i = 0; i < w; i++) {
158 int rx = x + i - offset.x;
159 int ry = y + j - offset.y;
160
161 int luminance;
162 if ((rx >= softMaskMinX) && (rx < softMaskMaxX) && (ry >= softMaskMinY) && (ry < softMaskMaxY)) {
163 luminance = maskRaster.getSample(rx, ry, maskBand);
164 } else {
165 luminance = 0;
166 }
167 int newAlpha = div255(luminance * result.getSample(i, j, numColorComponents));
168 result.setSample(i, j, numColorComponents, newAlpha);
169 }
170 }
171
172 return result;
173 }
174 }
175 }