1 /*
2 * SmartSprites Project
3 *
4 * Copyright (C) 2007-2009, Stanisław Osiński.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 *
10 * - Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * - Redistributions in binary form must reproduce the above copyright notice, this
14 * list of conditions and the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
16 *
17 * - Neither the name of the SmartSprites Project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * - We kindly request that you include in the end-user documentation provided with
22 * the redistribution and/or in the software itself an acknowledgement equivalent
23 * to the following: "This product includes software developed by the SmartSprites
24 * Project."
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 package org.carrot2.util;
38
39 import static org.junit.jupiter.api.Assertions.assertEquals;
40 import static org.junit.jupiter.api.Assertions.assertFalse;
41 import static org.junit.jupiter.api.Assertions.assertTrue;
42
43 import java.awt.Color;
44 import java.io.IOException;
45
46 import org.junit.jupiter.api.Test;
47
48 /**
49 * Test cases for {@link BufferedImageUtils}.
50 */
51 class BufferedImageUtilsTest extends BufferedImageTestBase {
52
53 /**
54 * Test has alpha transparency partial.
55 *
56 * @throws IOException
57 * Signals that an I/O exception has occurred.
58 */
59 @Test
60 void testHasAlphaTransparencyPartial() throws IOException {
61 assertTrue(BufferedImageUtils.hasTransparency(image("full-alpha.png")));
62 }
63
64 /**
65 * Test has alpha transparency bitmask.
66 *
67 * @throws IOException
68 * Signals that an I/O exception has occurred.
69 */
70 @Test
71 void testHasAlphaTransparencyBitmask() throws IOException {
72 assertTrue(BufferedImageUtils.hasTransparency(image("bit-alpha.png")));
73 }
74
75 /**
76 * Test has alpha transparency no transparency.
77 *
78 * @throws IOException
79 * Signals that an I/O exception has occurred.
80 */
81 @Test
82 void testHasAlphaTransparencyNoTransparency() throws IOException {
83 assertFalse(BufferedImageUtils.hasTransparency(image("no-alpha.png")));
84 }
85
86 /**
87 * Test has partial alpha transparency partial.
88 *
89 * @throws IOException
90 * Signals that an I/O exception has occurred.
91 */
92 @Test
93 void testHasPartialAlphaTransparencyPartial() throws IOException {
94 assertTrue(BufferedImageUtils.hasPartialTransparency(image("full-alpha.png")));
95 }
96
97 /**
98 * Test has partial alpha transparency bitmask.
99 *
100 * @throws IOException
101 * Signals that an I/O exception has occurred.
102 */
103 @Test
104 void testHasPartialAlphaTransparencyBitmask() throws IOException {
105 assertFalse(BufferedImageUtils.hasPartialTransparency(image("bit-alpha.png")));
106 }
107
108 /**
109 * Test has partial alpha transparency no transparency.
110 *
111 * @throws IOException
112 * Signals that an I/O exception has occurred.
113 */
114 @Test
115 void testHasPartialAlphaTransparencyNoTransparency() throws IOException {
116 assertFalse(BufferedImageUtils.hasPartialTransparency(image("no-alpha.png")));
117 }
118
119 /**
120 * Test count distinct colors transparency.
121 *
122 * @throws IOException
123 * Signals that an I/O exception has occurred.
124 */
125 @Test
126 void testCountDistinctColorsTransparency() throws IOException {
127 assertEquals(1, BufferedImageUtils.countDistinctColors(image("full-alpha.png")));
128 }
129
130 /**
131 * Test count distinct colors transparency matted.
132 *
133 * @throws IOException
134 * Signals that an I/O exception has occurred.
135 */
136 @Test
137 void testCountDistinctColorsTransparencyMatted() throws IOException {
138 assertEquals(4,
139 BufferedImageUtils.countDistinctColors(BufferedImageUtils.matte(image("full-alpha.png"), Color.WHITE)));
140 }
141
142 /**
143 * Test count distinct colors no transparency.
144 *
145 * @throws IOException
146 * Signals that an I/O exception has occurred.
147 */
148 @Test
149 void testCountDistinctColorsNoTransparency() throws IOException {
150 assertEquals(4, BufferedImageUtils.countDistinctColors(image("no-alpha.png")));
151 }
152
153 /**
154 * Test count distinct colors gradient.
155 *
156 * @throws IOException
157 * Signals that an I/O exception has occurred.
158 */
159 @Test
160 void testCountDistinctColorsGradient() throws IOException {
161 // black is the same in all bands
162 assertEquals(1021, BufferedImageUtils.countDistinctColors(image("many-colors.png")));
163 }
164 }