Package: CreateJ2DOp
CreateJ2DOp
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CreateJ2DOp() |
|
|
|
|
|
||||||||||||||||||||
canHandle(CreateOp) |
|
|
|
|
|
||||||||||||||||||||
execute(CreateOp, EditableImage, BufferedImage) |
|
|
|
|
|
||||||||||||||||||||
isZero(int[]) |
|
|
|
|
|
||||||||||||||||||||
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 java.util.Arrays;
30: import java.awt.Color;
31: import java.awt.image.BufferedImage;
32: import it.tidalwave.image.EditableImage;
33: import it.tidalwave.image.op.CreateOp;
34: import it.tidalwave.image.op.OperationImplementation;
35: import lombok.extern.slf4j.Slf4j;
36:
37: /***********************************************************************************************************************
38: *
39: * @author Emmanuele Sordini
40: * @author Fabrizio Giudici
41: *
42: **********************************************************************************************************************/
43: @Slf4j
44: public class CreateJ2DOp extends OperationImplementation<CreateOp, BufferedImage>
45: {
46: /*******************************************************************************************************************
47: *
48: * {@inheritDoc}
49: *
50: ******************************************************************************************************************/
51: @Override
52: public boolean canHandle (final CreateOp operation)
53: {
54:• switch (operation.getDataType())
55: {
56: case BYTE:
57:• switch (operation.getFiller().length)
58: {
59: case 1:
60: case 3:
61: return true;
62:
63: default:
64: return false;
65: }
66:
67: case UNSIGNED_SHORT:
68:• switch (operation.getFiller().length)
69: {
70: case 1:
71: return true;
72:
73: default:
74: return false;
75: }
76:
77: default:
78: return false;
79: }
80: }
81:
82: /*******************************************************************************************************************
83: *
84: * {@inheritDoc}
85: *
86: ******************************************************************************************************************/
87: @Override
88: protected BufferedImage execute (final CreateOp operation,
89: final EditableImage image,
90: final BufferedImage bufferedImage)
91: {
92: log.info("CreateJ2DOp.execute(" + operation + ", " + image + ")");
93: Java2DUtils.logImage(log, ">>>> bufferedImage: ", bufferedImage);
94:
95: final var filler = operation.getFiller();
96: final var dims = new int[filler.length];
97:
98:• for (var i = 0; i < filler.length; i++)
99: {
100: dims[i] = (int)filler[i];
101: }
102:
103: var type = 0;
104:
105:• switch (operation.getDataType())
106: {
107: case BYTE:
108:
109:• switch (dims.length)
110: {
111: case 1:
112: type = BufferedImage.TYPE_BYTE_GRAY;
113:
114: break;
115:
116: case 3:
117: type = BufferedImage.TYPE_3BYTE_BGR;
118:
119: break;
120:
121: default:
122: throw new IllegalArgumentException("Band count not supported (" + dims.length + ") for type " +
123: operation.getDataType());
124: }
125:
126: break;
127:
128: case UNSIGNED_SHORT:
129:
130:• switch (dims.length)
131: {
132: case 1:
133: type = BufferedImage.TYPE_USHORT_GRAY;
134:
135: break;
136:
137: default:
138: throw new IllegalArgumentException("Band count not supported (" + dims.length + ") for type " +
139: operation.getDataType());
140: }
141:
142: break;
143:
144: default:
145: throw new IllegalArgumentException("Unsupported dataType: " + operation.getDataType());
146: }
147:
148: final var result = new BufferedImage(operation.getWidth(), operation.getHeight(), type);
149:
150:• if (!isZero(dims))
151: {
152:• if (operation.getDataType() == EditableImage.DataType.UNSIGNED_SHORT)
153: {
154: final var buffer = new int[operation.getWidth() * operation.getHeight()];
155: final var value = dims[0];
156: Arrays.fill(buffer, value);
157: result.getRaster().setPixels(0, 0, operation.getWidth(), operation.getHeight(), buffer);
158: }
159:
160: else
161: {
162: final var g = result.createGraphics();
163:
164: try
165: {
166:• g.setColor((dims.length == 1)
167: ? new Color(dims[0], dims[0], dims[0])
168: : new Color(dims[0], dims[1], dims[2]));
169: g.fillRect(0, 0, operation.getWidth(), operation.getHeight());
170: }
171: finally
172: {
173:• if (g != null)
174: {
175: g.dispose();
176: }
177: }
178: }
179: }
180:
181: Java2DUtils.logImage(log, ">>>> CreateJ2DOp returning", result);
182:
183: return result;
184: }
185:
186: private boolean isZero (final int[] samples)
187: {
188:• for (var sample : samples)
189: {
190:• if (sample != 0)
191: {
192: return false;
193: }
194: }
195:
196: return true;
197: }
198: }