• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / smartsprites / 555

21 May 2026 03:26PM UTC coverage: 87.799%. Remained the same
555

push

github

hazendaz
[tests] Fix tests that were looking at size of original license before spdx change

557 of 670 branches covered (83.13%)

Branch coverage included in aggregate %.

1350 of 1502 relevant lines covered (89.88%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.6
/src/main/java/org/carrot2/util/BufferedImageUtils.java
1
/*
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2021-2026 Hazendaz
6
 * Copyright (C) 2007-2009, Stanisław Osiński.
7
 */
8
package org.carrot2.util;
9

10
import java.awt.AlphaComposite;
11
import java.awt.Color;
12
import java.awt.CompositeContext;
13
import java.awt.Transparency;
14
import java.awt.image.BufferedImage;
15
import java.awt.image.Raster;
16
import java.util.HashSet;
17
import java.util.Set;
18

19
/**
20
 * Various utility methods for working with {@link BufferedImage}s.
21
 */
22
public class BufferedImageUtils {
23

24
    /**
25
     * Returns <code>true</code> if the provided image has partially transparent areas (alpha channel).
26
     *
27
     * @param image
28
     *            the image
29
     *
30
     * @return true, if successful
31
     */
32
    public static boolean hasPartialTransparency(BufferedImage image) {
33
        final Raster alphaRaster = image.getAlphaRaster();
1✔
34
        if (image.getTransparency() != Transparency.TRANSLUCENT || alphaRaster == null) {
1!
35
            return false;
1✔
36
        }
37

38
        int[] pixels = alphaRaster.getPixels(0, 0, alphaRaster.getWidth(), alphaRaster.getHeight(), (int[]) null);
1✔
39
        for (int i : pixels) {
1✔
40
            if (i != 0 && i != 255) {
1✔
41
                return true;
1✔
42
            }
43
        }
44

45
        return false;
1✔
46
    }
47

48
    /**
49
     * Returns <code>true</code> if the provided image has any kind of transparent areas.
50
     *
51
     * @param image
52
     *            the image
53
     *
54
     * @return true, if successful
55
     */
56
    public static boolean hasTransparency(BufferedImage image) {
57
        final Raster alphaRaster = image.getAlphaRaster();
1✔
58
        if (image.getTransparency() != Transparency.TRANSLUCENT || alphaRaster == null) {
1!
59
            return false;
1✔
60
        }
61

62
        int[] pixels = alphaRaster.getPixels(0, 0, alphaRaster.getWidth(), alphaRaster.getHeight(), (int[]) null);
1✔
63
        for (int i : pixels) {
1!
64
            if (i != 255) {
1✔
65
                return true;
1✔
66
            }
67
        }
68

69
        return false;
×
70
    }
71

72
    /**
73
     * Returns the number of distinct colors (excluding transparency) in the <code>image</code>.
74
     *
75
     * @param image
76
     *            the image
77
     *
78
     * @return the int
79
     */
80
    public static int countDistinctColors(BufferedImage image) {
81
        return getDistinctColors(image).length;
1✔
82
    }
83

84
    /**
85
     * Returns the <code>image</code>'s distinct colors in an RGB format, discarding transparency information.
86
     *
87
     * @param image
88
     *            the image
89
     *
90
     * @return the distinct colors
91
     */
92
    public static int[] getDistinctColors(BufferedImage image) {
93
        return getDistinctColors(image, 0);
1✔
94
    }
95

96
    /**
97
     * Returns the <code>image</code>'s distinct colors in an RGB format, discarding transparency information. Adds
98
     * <code>padding</code> empty slots at the beginning of the returned array.
99
     *
100
     * @param image
101
     *            the image
102
     * @param padding
103
     *            the padding
104
     *
105
     * @return the distinct colors
106
     */
107
    public static int[] getDistinctColors(BufferedImage image, int padding) {
108
        final int width = image.getWidth();
1✔
109
        final int height = image.getHeight();
1✔
110

111
        final Set<Integer> colors = new HashSet<>();
1✔
112

113
        for (int x = 0; x < width; x++) {
1✔
114
            for (int y = 0; y < height; y++) {
1✔
115
                final int pixel = image.getRGB(x, y);
1✔
116

117
                // Count only colors for which alpha is not fully transparent
118
                if ((pixel & 0xff000000) != 0x00000000) {
1✔
119
                    colors.add(Integer.valueOf(pixel & 0x00ffffff));
1✔
120
                }
121
            }
122
        }
123

124
        final int[] colorMap = new int[colors.size() + padding];
1✔
125
        int index = padding;
1✔
126
        for (Integer color : colors) {
1✔
127
            colorMap[index] = color;
1✔
128
            index++;
1✔
129
        }
1✔
130

131
        return colorMap;
1✔
132
    }
133

134
    /**
135
     * Returns a two dimensional array of the <code>image</code>'s RGB values, including transparency.
136
     *
137
     * @param image
138
     *            the image
139
     *
140
     * @return the rgb
141
     */
142
    public static int[][] getRgb(BufferedImage image) {
143
        final int width = image.getWidth();
1✔
144
        final int height = image.getHeight();
1✔
145

146
        final int[][] rgb = new int[width][height];
1✔
147

148
        for (int x = 0; x < width; x++) {
1✔
149
            for (int y = 0; y < height; y++) {
1✔
150
                rgb[x][y] = image.getRGB(x, y);
1✔
151
            }
152
        }
153

154
        return rgb;
1✔
155
    }
156

157
    /**
158
     * Performs matting of the <code>source</code> image using <code>matteColor</code>. Matting is rendering partial
159
     * transparencies using solid color as if the original image was put on top of a bitmap filled with
160
     * <code>matteColor</code>.
161
     *
162
     * @param source
163
     *            the source
164
     * @param matteColor
165
     *            the matte color
166
     *
167
     * @return the buffered image
168
     */
169
    public static BufferedImage matte(BufferedImage source, Color matteColor) {
170
        final int width = source.getWidth();
1✔
171
        final int height = source.getHeight();
1✔
172

173
        // A workaround for possibly different custom image types we can get:
174
        // draw a copy of the image
175
        final BufferedImage sourceConverted = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
1✔
176
        sourceConverted.getGraphics().drawImage(source, 0, 0, null);
1✔
177

178
        final BufferedImage matted = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
1✔
179

180
        final BufferedImage matte = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
1✔
181
        final int matteRgb = matteColor.getRGB();
1✔
182
        for (int x = 0; x < width; x++) {
1✔
183
            for (int y = 0; y < height; y++) {
1✔
184
                matte.setRGB(x, y, matteRgb);
1✔
185
            }
186
        }
187

188
        CompositeContext context = AlphaComposite.DstOver.createContext(matte.getColorModel(),
1✔
189
                sourceConverted.getColorModel(), null);
1✔
190
        context.compose(matte.getRaster(), sourceConverted.getRaster(), matted.getRaster());
1✔
191

192
        return matted;
1✔
193
    }
194

195
    /**
196
     * Draws <code>image</code> on the <code>canvas</code> placing the top left corner of <code>image</code> at
197
     * <code>x</code> / <code>y</code> offset from the top left corner of <code>canvas</code>.
198
     *
199
     * @param image
200
     *            the image
201
     * @param canvas
202
     *            the canvas
203
     * @param x
204
     *            the x
205
     * @param y
206
     *            the y
207
     */
208
    public static void drawImage(BufferedImage image, BufferedImage canvas, int x, int y) {
209
        final int[] imgRGB = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
1✔
210
        canvas.setRGB(x, y, image.getWidth(), image.getHeight(), imgRGB, 0, image.getWidth());
1✔
211
    }
1✔
212

213
    /**
214
     * Instantiates a new buffered image utils.
215
     */
216
    private BufferedImageUtils() {
217
    }
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc