Skip to content

Package: WriteJ2DOp

WriteJ2DOp

nameinstructionbranchcomplexitylinemethod
WriteJ2DOp()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
execute(WriteOp, EditableImage, BufferedImage)
M: 131 C: 0
0%
M: 18 C: 0
0%
M: 10 C: 0
0%
M: 39 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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 java.io.BufferedOutputStream;
30: import java.io.File;
31: import java.io.IOException;
32: import java.io.OutputStream;
33: import java.nio.file.Files;
34: import javax.imageio.IIOImage;
35: import javax.imageio.ImageIO;
36: import javax.imageio.ImageWriter;
37: import javax.imageio.stream.ImageOutputStream;
38: import java.awt.image.BufferedImage;
39: import it.tidalwave.image.EditableImage;
40: import it.tidalwave.image.op.OperationImplementation;
41: import it.tidalwave.image.op.WriteOp;
42: import lombok.extern.slf4j.Slf4j;
43:
44: /***********************************************************************************************************************
45: *
46: * @author Fabrizio Giudici
47: *
48: **********************************************************************************************************************/
49: @Slf4j
50: public class WriteJ2DOp extends OperationImplementation<WriteOp, BufferedImage>
51: {
52: /*******************************************************************************************************************
53: *
54: * {@inheritDoc}
55: *
56: ******************************************************************************************************************/
57: @Override
58: protected BufferedImage execute (final WriteOp operation,
59: final EditableImage image,
60: final BufferedImage bufferedImage)
61: {
62: final var format = operation.getFormat();
63: final var output = operation.getOutput();
64:• final var outputForLog = (output instanceof OutputStream) ? output.getClass() : output;
65: final var imageWriteParam = operation.getImageWriteParam();
66: log.info("Write2DOp(" + format + ", " + outputForLog + ", " + imageWriteParam + ")");
67:
68: ImageOutputStream stream = null;
69: var shouldClose = true;
70:
71: try
72: {
73:• if (output instanceof OutputStream)
74: {
75: stream = ImageIO.createImageOutputStream(output);
76: shouldClose = false;
77: }
78:
79:• else if (output instanceof File)
80: {
81: stream =
82: ImageIO.createImageOutputStream(new BufferedOutputStream(Files.newOutputStream(((File)output).toPath())));
83: shouldClose = true;
84: }
85:
86:• else if (output instanceof ImageOutputStream)
87: {
88: stream = (ImageOutputStream)output;
89: }
90:
91: final var writers = ImageIO.getImageWritersByFormatName(format);
92: ImageWriter selectedWriter = null;
93: log.debug("Available writers for format: " + format);
94:
95:• while (writers.hasNext())
96: {
97: final var writer = writers.next();
98: log.debug(">>>> writer: " + writer);
99:
100:• if (selectedWriter == null) // keep the first one, keep on logging the others
101: {
102: selectedWriter = writer;
103: }
104: }
105:
106:• if (selectedWriter == null)
107: {
108: throw new IOException("No writers for format: " + format);
109: }
110:
111: selectedWriter.setOutput(stream);
112: selectedWriter.write(null, new IIOImage(bufferedImage, null, null), imageWriteParam);
113: stream.flush();
114: selectedWriter.dispose();
115:
116: return bufferedImage;
117: }
118:
119: catch (IOException e)
120: {
121: throw new RuntimeException(e);
122: }
123:
124: finally
125: {
126:• if ((stream != null) && shouldClose)
127: {
128: try
129: {
130: stream.close();
131: }
132: catch (IOException e)
133: {
134: log.warn("execute()", e);
135: }
136: }
137: }
138: }
139: }