Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2023-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.util;
23
24 import java.awt.*;
25 import java.awt.geom.AffineTransform;
26 import java.awt.geom.Rectangle2D;
27 import java.awt.image.BufferedImage;
28 import java.util.function.BiConsumer;
29 import java.util.function.Consumer;
30
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 import com.github.weisj.jsvg.attributes.UnitType;
35 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
36 import com.github.weisj.jsvg.nodes.SVGNode;
37 import com.github.weisj.jsvg.nodes.prototype.Instantiator;
38 import com.github.weisj.jsvg.renderer.RenderContext;
39 import com.github.weisj.jsvg.renderer.impl.NodeRenderer;
40 import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor;
41 import com.github.weisj.jsvg.renderer.output.Output;
42 import com.github.weisj.jsvg.renderer.output.impl.Graphics2DOutput;
43 import com.github.weisj.jsvg.renderer.output.impl.GraphicsUtil;
44
45 /**
46 * Class that encapsulates rendering to an offscreen image.
47 * The image is aligned to the pixel boundary of the root image surface. Rendering to the
48 * image behaves and the blitting it behaves as if it was rendered directly to the root surface.
49 */
50 public final class BlittableImage {
51
52
53 @FunctionalInterface
54 public interface BufferSurfaceSupplier {
55 @NotNull
56 BufferedImage createBufferSurface(@Nullable AffineTransform at, double width, double height);
57 }
58
59 private final @NotNull BufferedImage image;
60 private final @NotNull RenderContext context;
61 private final @NotNull Rectangle2D boundsInDeviceSpace;
62 private final @NotNull Rectangle2D clippedUserBounds;
63
64 private BlittableImage(@NotNull BufferedImage image, @NotNull RenderContext context,
65 @NotNull Rectangle2D boundsInDeviceSpace, @NotNull Rectangle2D clippedUserBounds) {
66 this.image = image;
67 this.context = context;
68 this.boundsInDeviceSpace = boundsInDeviceSpace;
69 this.clippedUserBounds = clippedUserBounds;
70 }
71
72 public static @Nullable BlittableImage create(@NotNull BufferSurfaceSupplier bufferSurfaceSupplier,
73 @NotNull RenderContext context, @Nullable Rectangle2D clipBounds,
74 @NotNull Rectangle2D bounds, @NotNull Rectangle2D objectBounds, @NotNull UnitType contentUnits) {
75 RenderContextAccessor.Accessor accessor = RenderContextAccessor.instance();
76 RenderContext imageContext = accessor.createInitial(accessor.currentColor(context), context.platformSupport(),
77 contentUnits.deriveMeasure(context.measureContext()));
78 return create(bufferSurfaceSupplier, context, clipBounds, bounds, objectBounds, contentUnits, imageContext);
79 }
80
81 public static @Nullable BlittableImage create(@NotNull BufferSurfaceSupplier bufferSurfaceSupplier,
82 @NotNull RenderContext context, @Nullable Rectangle2D clipBounds,
83 @NotNull Rectangle2D bounds, @NotNull Rectangle2D objectBounds, @NotNull UnitType contentUnits,
84 @NotNull RenderContext imageContext) {
85 Rectangle2D boundsInDeviceSpace = GeometryUtil.userBoundsToDeviceBounds(context, bounds);
86
87 if (clipBounds != null) {
88 Rectangle2D clipBoundsInDeviceSpace = GeometryUtil.userBoundsToDeviceBounds(context, clipBounds);
89 Rectangle2D.intersect(clipBoundsInDeviceSpace, boundsInDeviceSpace, boundsInDeviceSpace);
90 }
91
92 if (ShapeUtil.isInvalidArea(boundsInDeviceSpace)) return null;
93
94 // Convert to integer coordinates to ensure we don't cut off any pixels due to rounding errors.
95 // Increase size by 1 to ensure we don't cut off any pixels used for anti-aliasing.
96 GeometryUtil.adjustForAliasing(boundsInDeviceSpace);
97
98 // Due to aliasing adjustments this cannot just be bounds intersected clipBounds.
99 Rectangle2D adjustedBoundsInUserSpace = GeometryUtil.convertBounds(context, boundsInDeviceSpace,
100 GeometryUtil.Space.DEVICE, GeometryUtil.Space.USER);
101
102 BufferedImage img = bufferSurfaceSupplier.createBufferSurface(null,
103 boundsInDeviceSpace.getWidth(),
104 boundsInDeviceSpace.getHeight());
105
106 AffineTransform rootTransform = new AffineTransform();
107 rootTransform.translate(-boundsInDeviceSpace.getX(), -boundsInDeviceSpace.getY());
108 rootTransform.concatenate(context.rootTransform());
109
110 AffineTransform userSpaceTransform = new AffineTransform(context.userSpaceTransform());
111 if (contentUnits == UnitType.ObjectBoundingBox) {
112 userSpaceTransform = new AffineTransform(userSpaceTransform);
113 userSpaceTransform.translate(objectBounds.getX(), objectBounds.getY());
114 userSpaceTransform.scale(objectBounds.getWidth(), objectBounds.getHeight());
115 }
116
117 // Note: This should actually be the render context from the declaration site of the mask/clipPath
118 // etc.
119 RenderContextAccessor.instance().setRootTransform(imageContext, rootTransform, userSpaceTransform);
120
121 return new BlittableImage(img, imageContext, boundsInDeviceSpace, adjustedBoundsInUserSpace);
122 }
123
124 public @NotNull RenderContext context() {
125 return context;
126 }
127
128 public @NotNull Rectangle2D imageBoundsInDeviceSpace() {
129 return boundsInDeviceSpace;
130 }
131
132 public @NotNull Rectangle2D clippedUserBounds() {
133 return clippedUserBounds;
134 }
135
136 public @NotNull BufferedImage image() {
137 return image;
138 }
139
140 public @NotNull Graphics2D createGraphics() {
141 Graphics2D g = GraphicsUtil.createGraphics(image);
142 g.transform(context.rootTransform());
143 g.transform(context.userSpaceTransform());
144 return g;
145 }
146
147 public void renderNode(@NotNull Output parentOutput, @NotNull SVGNode node,
148 @NotNull Instantiator instantiator) {
149 render(parentOutput, (out, ctx) -> NodeRenderer.renderNode(node, ctx, out, instantiator));
150 }
151
152 public void clearBackground(@NotNull Color color) {
153 Graphics2D g = image.createGraphics();
154 g.setColor(color);
155 g.fillRect(0, 0, image.getWidth(), image.getHeight());
156 g.dispose();
157 }
158
159 public void render(@NotNull Output output, @NotNull Consumer<Graphics2D> painter) {
160 Graphics2D imgGraphics = createGraphics();
161 imgGraphics.setRenderingHints(output.renderingHints());
162 painter.accept(imgGraphics);
163 imgGraphics.dispose();
164 }
165
166 public void render(@NotNull Output output, @NotNull BiConsumer<Output, RenderContext> painter) {
167 Graphics2D imgGraphics = createGraphics();
168 imgGraphics.setRenderingHints(output.renderingHints());
169 painter.accept(new Graphics2DOutput(imgGraphics), context);
170 imgGraphics.dispose();
171 }
172
173 public void prepareForBlitting(@NotNull Output output) {
174 output.setTransform(AffineTransform.getTranslateInstance(
175 boundsInDeviceSpace.getX(), boundsInDeviceSpace.getY()));
176 }
177
178 public void blitTo(@NotNull Output output) {
179 Output out = output.createChild();
180 out.setTransform(AffineTransform.getTranslateInstance(
181 boundsInDeviceSpace.getX(), boundsInDeviceSpace.getY()));
182 out.drawImage(image);
183 out.dispose();
184 }
185
186 public void debug(@NotNull Output output) {
187 debug(output, true);
188 }
189
190 public void debug(@NotNull Output output, boolean drawImage) {
191 output.debugPaint(g -> {
192 g.setComposite(AlphaComposite.SrcOver.derive(0.5f));
193 g.setTransform(AffineTransform.getTranslateInstance(
194 boundsInDeviceSpace.getX(), boundsInDeviceSpace.getY()));
195 if (drawImage) {
196 g.drawImage(image, 0, 0, null);
197 }
198 g.setColor(Color.MAGENTA);
199 g.drawRect(0, 0, image.getWidth(), image.getHeight());
200 });
201 }
202 }