1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.imaging.util; |
35 |
|
|
36 |
|
import info.magnolia.imaging.OutputFormat; |
37 |
|
|
38 |
|
import java.awt.Color; |
39 |
|
import java.awt.Graphics2D; |
40 |
|
import java.awt.Transparency; |
41 |
|
import java.awt.image.BufferedImage; |
42 |
|
import java.awt.image.DirectColorModel; |
43 |
|
import java.awt.image.WritableRaster; |
44 |
|
import java.io.BufferedInputStream; |
45 |
|
import java.io.InputStream; |
46 |
|
|
47 |
|
import org.slf4j.Logger; |
48 |
|
import org.slf4j.LoggerFactory; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
|
|
| 73% |
Uncovered Elements: 17 (63) |
Complexity: 16 |
Complexity Density: 0.35 |
|
53 |
|
public class ImageUtil { |
54 |
|
|
55 |
|
private static final Logger log = LoggerFactory.getLogger(ImageUtil.class); |
56 |
|
|
57 |
|
|
58 |
|
@see |
59 |
|
|
|
|
| 89.5% |
Uncovered Elements: 2 (19) |
Complexity: 6 |
Complexity Density: 0.46 |
|
60 |
28 |
public static BufferedImage flattenTransparentImageForOpaqueFormat(final BufferedImage img, OutputFormat outputFormat) {... |
61 |
28 |
if (img == null || outputFormat == null) { |
62 |
0 |
throw new IllegalArgumentException("No image or output format passed: [img:" + img + ", outputFormat:" + outputFormat + "]"); |
63 |
|
} |
64 |
|
|
65 |
28 |
final boolean isOpaque = img.getTransparency() == Transparency.OPAQUE; |
66 |
|
|
67 |
28 |
if (!isOpaque && !outputFormat.supportsTransparency()) { |
68 |
13 |
final WritableRaster raster = img.getRaster(); |
69 |
13 |
final WritableRaster newRaster = raster.createWritableChild(0, 0, img.getWidth(), img.getHeight(), 0, 0, new int[]{0, 1, 2}); |
70 |
|
|
71 |
|
|
72 |
13 |
if (img.getColorModel() instanceof java.awt.image.ComponentColorModel) { |
73 |
1 |
log.warn("flattenTransparentImageForOpaqueFormat cannot run on image with ComponentColorModel. Consider replacing image. Falling back to slower fillTransparantPixels method to flatten image. [img:" + img + "]"); |
74 |
1 |
return fillTransparentPixels(img, Color.black); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
12 |
final DirectColorModel cm = (DirectColorModel) img.getColorModel(); |
79 |
12 |
final DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask()); |
80 |
|
|
81 |
|
|
82 |
12 |
return new BufferedImage(newCM, newRaster, false, null); |
83 |
|
|
84 |
|
} else { |
85 |
15 |
return img; |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
91 |
3 |
static BufferedImage fillTransparentPixels(final BufferedImage image, Color fillColor) {... |
92 |
3 |
int w = image.getWidth(); |
93 |
3 |
int h = image.getHeight(); |
94 |
3 |
final BufferedImage image2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); |
95 |
3 |
final Graphics2D g = image2.createGraphics(); |
96 |
3 |
g.setColor(fillColor); |
97 |
3 |
g.fillRect(0, 0, w, h); |
98 |
3 |
g.drawRenderedImage(image, null); |
99 |
3 |
g.dispose(); |
100 |
3 |
return image2; |
101 |
|
} |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
0 |
public static BufferedInputStream newBufferedInputStream(InputStream in) {... |
104 |
0 |
return new BufferedInputStream(in) { |
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
105 |
0 |
@Override... |
106 |
|
public String toString() { |
107 |
0 |
final StringBuilder sb = new StringBuilder(); |
108 |
0 |
sb.append("BufferedInputStream"); |
109 |
0 |
sb.append("{pos='").append(pos).append('\''); |
110 |
0 |
sb.append(", count='").append(count).append('\''); |
111 |
0 |
sb.append(", markpos='").append(markpos).append('\''); |
112 |
0 |
sb.append(", marklimit='").append(marklimit).append('\''); |
113 |
0 |
sb.append(", buf.length='").append(buf.length).append('\''); |
114 |
0 |
sb.append(", wrapping ").append(in.toString()); |
115 |
0 |
sb.append('}'); |
116 |
0 |
return sb.toString(); |
117 |
|
} |
118 |
|
}; |
119 |
|
} |
120 |
|
|
|
|
| 89.5% |
Uncovered Elements: 2 (19) |
Complexity: 7 |
Complexity Density: 0.54 |
|
121 |
40 |
public static int getImageType(BufferedImage img) {... |
122 |
40 |
int imageType = img.getType(); |
123 |
40 |
switch (imageType) { |
124 |
1 |
case BufferedImage.TYPE_CUSTOM: |
125 |
1 |
imageType = img.getAlphaRaster() != null ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB; |
126 |
1 |
break; |
127 |
|
|
128 |
6 |
case BufferedImage.TYPE_BYTE_BINARY: |
129 |
5 |
case BufferedImage.TYPE_BYTE_INDEXED: |
130 |
11 |
if (img.getColorModel() != null) { |
131 |
11 |
imageType = img.getColorModel().hasAlpha() ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB; |
132 |
|
} |
133 |
11 |
break; |
134 |
28 |
default: |
135 |
28 |
break; |
136 |
|
} |
137 |
40 |
return imageType; |
138 |
|
} |
139 |
|
} |