Package: ImplementationFactoryJ2D
ImplementationFactoryJ2D
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ImplementationFactoryJ2D() |
|
|
|
|
|
||||||||||||||||||||
canConvertFrom(Class) |
|
|
|
|
|
||||||||||||||||||||
canConvertTo(Class) |
|
|
|
|
|
||||||||||||||||||||
convertFrom(Object) |
|
|
|
|
|
||||||||||||||||||||
convertTo(Object) |
|
|
|
|
|
||||||||||||||||||||
createImageModel(BufferedImage) |
|
|
|
|
|
||||||||||||||||||||
getDefault() |
|
|
|
|
|
||||||||||||||||||||
lambda$getDefault$0() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.java2d;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ServiceLoader;
31: import java.util.stream.StreamSupport;
32: import java.awt.image.BufferedImage;
33: import it.tidalwave.image.ImageModel;
34: import it.tidalwave.image.op.CaptureOp;
35: import it.tidalwave.image.op.ConvertToBufferedImageOp;
36: import it.tidalwave.image.op.ConvolveOp;
37: import it.tidalwave.image.op.CreateOp;
38: import it.tidalwave.image.op.CropOp;
39: import it.tidalwave.image.op.DrawOp;
40: import it.tidalwave.image.op.ImplementationFactory;
41: import it.tidalwave.image.op.OptimizeOp;
42: import it.tidalwave.image.op.PaintOp;
43: import it.tidalwave.image.op.PrintOp;
44: import it.tidalwave.image.op.RotateOp;
45: import it.tidalwave.image.op.RotateQuadrantOp;
46: import it.tidalwave.image.op.ScaleOp;
47: import it.tidalwave.image.op.WrapOp;
48: import it.tidalwave.image.op.WriteOp;
49: import lombok.extern.slf4j.Slf4j;
50:
51: /***********************************************************************************************************************
52: *
53: * @author Fabrizio Giudici
54: *
55: **********************************************************************************************************************/
56: @Slf4j
57: public class ImplementationFactoryJ2D extends ImplementationFactory
58: {
59: private Class<?> planarImageClass;
60:
61: @Nonnull
62: public static ImplementationFactory getDefault()
63: {
64: var loader = ServiceLoader.load(ImplementationFactory.class);
65: return StreamSupport.stream(loader.spliterator(), false)
66: .findFirst()
67: .orElseThrow(() -> new RuntimeException("Can't found implementation of " + ImplementationFactory.class));
68: }
69:
70: /*******************************************************************************************************************
71: *
72: *
73: ******************************************************************************************************************/
74: public ImplementationFactoryJ2D()
75: {
76: super(BufferedImage.class);
77: registerImplementation(CaptureOp.class, CaptureJ2DOp.class);
78: registerImplementation(CreateOp.class, CreateJ2DOp.class);
79: registerImplementation(ConvolveOp.class, ConvolveJ2DOp.class);
80: registerImplementation(CropOp.class, CropJ2DOp.class);
81: registerImplementation(ConvertToBufferedImageOp.class, ConvertToBufferedImageJ2DOp.class);
82: registerImplementation(DrawOp.class, DrawJ2DOp.class);
83: registerImplementation(OptimizeOp.class, OptimizeJ2DOp.class);
84: registerImplementation(PaintOp.class, PaintJ2DOp.class);
85: registerImplementation(PrintOp.class, PrintJ2DOp.class);
86: registerImplementation(RotateQuadrantOp.class, RotateQuadrantJ2DOp.class);
87: registerImplementation(RotateOp.class, RotateJ2DOp.class);
88: registerImplementation(ScaleOp.class, ScaleJ2DOp.class);
89: registerImplementation(WrapOp.class, WrapJ2DOp.class);
90: registerImplementation(WriteOp.class, WriteJ2DOp.class);
91:
92: final var contextClassLoader = Thread.currentThread().getContextClassLoader();
93:
94: try
95: {
96: planarImageClass = contextClassLoader.loadClass("javax.media.jai.PlanarImage");
97: }
98: catch (Throwable e)
99: {
100: log.warn("JAI not available: {}", e.toString());
101: }
102:
103: try
104: {
105: final var clazz = contextClassLoader.loadClass("it.tidalwave.image.java2d.AdditionalOperations");
106: final var method = clazz.getMethod("register", ImplementationFactoryJ2D.class);
107: method.invoke(null, this);
108: }
109: catch (Throwable e)
110: {
111: log.warn("Additional Java2D operations not available: {}", e.toString());
112: }
113: }
114:
115: /*******************************************************************************************************************
116: *
117: * {@inheritDoc}
118: *
119: ******************************************************************************************************************/
120: @Override @Nonnull
121: public ImageModel createImageModel (@Nonnull final BufferedImage bufferedImage)
122: {
123: return new ImageModelJ2D(bufferedImage);
124: }
125:
126: /*******************************************************************************************************************
127: *
128: * {@inheritDoc}
129: *
130: ******************************************************************************************************************/
131: @Override
132: public boolean canConvertFrom (@Nonnull final Class imageClass)
133: {
134:• return imageClass.equals(BufferedImage.class) ||
135:• ((planarImageClass != null) && planarImageClass.isAssignableFrom(imageClass));
136: }
137:
138: /*******************************************************************************************************************
139: *
140: * {@inheritDoc}
141: *
142: ******************************************************************************************************************/
143: @Override @Nonnull
144: public ImageModel convertFrom (@Nonnull final Object image)
145: {
146: // if ((planarImageClass != null) && planarImageClass.isAssignableFrom(image.getClass())) // image instanceof
147: // PlanarImage
148:• if (canConvertFrom(image.getClass()))
149: {
150:• if (image instanceof BufferedImage)
151: {
152: return new ImageModelJ2D(image);
153: }
154:
155: try
156: {
157: final var method = planarImageClass.getMethod("getAsBufferedImage");
158: final var bufferedImage = method.invoke(image);
159:
160: return new ImageModelJ2D(bufferedImage);
161: }
162: catch (Exception e)
163: {
164: throw new UnsupportedOperationException(e);
165: }
166: }
167:
168: throw new UnsupportedOperationException("convertFrom " + image.getClass());
169: }
170:
171: /*******************************************************************************************************************
172: *
173: * {@inheritDoc}
174: *
175: ******************************************************************************************************************/
176: @Override
177: public boolean canConvertTo (@Nonnull final Class imageClass)
178: {
179:• return (planarImageClass != null) &&
180:• planarImageClass.isAssignableFrom(imageClass); // image instanceof PlanarImage
181: }
182:
183: /*******************************************************************************************************************
184: *
185: * {@inheritDoc}
186: *
187: ******************************************************************************************************************/
188: @Override @Nonnull
189: public Object convertTo (@Nonnull final Object image)
190: {
191:• if (image.getClass().getName().equals("javax.media.jai.PlanarImage"))
192: {
193: return image; // BufferedImage extends RenderedImage
194: }
195:
196: throw new UnsupportedOperationException("convertTo " + image.getClass());
197: }
198: }