Skip to content

Commit 17cadd2

Browse files
committed
Fix CUSTOM image format calculating the wrong average colors
1 parent 9f34af3 commit 17cadd2

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

core/src/main/java/de/bluecolored/bluemap/core/resources/pack/resourcepack/texture/Texture.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage ima
132132
public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage image, @Nullable AnimationMeta animation) throws IOException {
133133

134134
//check halfTransparency
135-
boolean halfTransparent = checkHalfTransparent(image);
135+
boolean halfTransparent = BufferedImageUtil.halfTransparent(image);
136136

137137
//calculate color
138138
Color color = BufferedImageUtil.averageColor(image);
@@ -145,20 +145,6 @@ public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage ima
145145
return new Texture(resourcePath, color, halfTransparent, base64, animation, image);
146146
}
147147

148-
private static boolean checkHalfTransparent(BufferedImage image){
149-
for (int x = 0; x < image.getWidth(); x++){
150-
for (int y = 0; y < image.getHeight(); y++){
151-
int pixel = image.getRGB(x, y);
152-
int alpha = (pixel >> 24) & 0xff;
153-
if (alpha > 0x00 && alpha < 0xff){
154-
return true;
155-
}
156-
}
157-
}
158-
159-
return false;
160-
}
161-
162148
public static Texture missing(ResourcePath<Texture> resourcePath) {
163149
return new Texture(resourcePath);
164150
}

core/src/main/java/de/bluecolored/bluemap/core/util/BufferedImageUtil.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232

3333
public class BufferedImageUtil {
3434

35+
public static boolean halfTransparent(BufferedImage image){
36+
Color color = new Color();
37+
float[] buffer = null;
38+
for (int x = 0; x < image.getWidth(); x++){
39+
for (int y = 0; y < image.getHeight(); y++){
40+
buffer = readPixel(image, x, y, color, buffer);
41+
if (color.a > 0f && color.a < 1f) return true;
42+
}
43+
}
44+
return false;
45+
}
46+
3547
public static Color averageColor(BufferedImage image) {
3648
Color average = new Color();
3749
Color color = new Color();
@@ -58,7 +70,7 @@ private static float[] readPixel(BufferedImage image, int x, int y, @Nullable Co
5870
if (target == null) target = new Color();
5971

6072
// workaround for java bug: 5051418
61-
if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
73+
if (image.getType() == BufferedImage.TYPE_BYTE_GRAY || image.getType() == BufferedImage.TYPE_CUSTOM) {
6274
buffer = readPixelDirect(image, x, y, target, buffer);
6375
} else {
6476
readPixelDefault(image, x, y, target);
@@ -74,10 +86,10 @@ private static void readPixelDefault(BufferedImage image, int x, int y, Color ta
7486
private static float[] readPixelDirect(RenderedImage image, int x, int y, Color target, float @Nullable [] buffer) {
7587
buffer = image.getData().getPixel(x, y, buffer);
7688

77-
float a = buffer.length >= 4 ? buffer[3] / 255f : 1f;
7889
float r = buffer[0] / 255f;
7990
float g = buffer.length >= 3 ? buffer[1] / 255f : r;
8091
float b = buffer.length >= 3 ? buffer[2] / 255f : r;
92+
float a = buffer.length >= 4 ? buffer[3] / 255f : buffer.length == 2 ? buffer[1] / 255f : 1f;
8193

8294
target.set(r, g, b, a, image.getColorModel().isAlphaPremultiplied());
8395

0 commit comments

Comments
 (0)