Skip to content

Commit

Permalink
Add basic support for the resource-changes of the 1.20.3 snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Nov 21, 2023
1 parent 5b93202 commit 5866cb5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import de.bluecolored.bluemap.core.resources.adapter.ResourcesGson;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.resources.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.util.Key;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -66,7 +68,10 @@ public synchronized int put(ResourcePath<Texture> textureResourcePath) {
}

public synchronized void put(ResourcePack resourcePack) {
resourcePack.getTextures().keySet().forEach(this::put);
resourcePack.getTextures().keySet()
.stream()
.sorted(Comparator.comparing(Key::getFormatted))
.forEach(this::put);
}

public void writeTexturesFile(ResourcePack resourcePack, OutputStream out) throws IOException {
Expand Down Expand Up @@ -98,7 +103,7 @@ public static TextureGallery readTexturesFile(InputStream in) throws IOException
for (int ordinal = 0; ordinal < textures.length; ordinal++) {
Texture texture = textures[ordinal];
if (texture != null) {
gallery.ordinalMap.put(textures[ordinal].getResourcePath(), ordinal);
gallery.ordinalMap.put(texture.getResourcePath(), ordinal);
}
}
} catch (JsonIOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import de.bluecolored.bluemap.api.debug.DebugDump;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.util.BufferedImageUtil;
import de.bluecolored.bluemap.core.util.math.Color;

import javax.imageio.ImageIO;
Expand Down Expand Up @@ -112,7 +113,7 @@ public static Texture from(ResourcePath<Texture> resourcePath, BufferedImage ima
boolean halfTransparent = checkHalfTransparent(image);

//calculate color
Color color = calculateColor(image);
Color color = BufferedImageUtil.averageColor(image);

//write to Base64
ByteArrayOutputStream os = new ByteArrayOutputStream();
Expand All @@ -136,36 +137,6 @@ private static boolean checkHalfTransparent(BufferedImage image){
return false;
}

private static Color calculateColor(BufferedImage image){
float alpha = 0f, red = 0f, green = 0f, blue = 0f;
int count = 0;

for (int x = 0; x < image.getWidth(); x++){
for (int y = 0; y < image.getHeight(); y++){
int pixel = image.getRGB(x, y);
float pixelAlpha = ((pixel >> 24) & 0xff) / 255f;
float pixelRed = ((pixel >> 16) & 0xff) / 255f;
float pixelGreen = ((pixel >> 8) & 0xff) / 255f;
float pixelBlue = (pixel & 0xff) / 255f;

count++;
alpha += pixelAlpha;
red += pixelRed * pixelAlpha;
green += pixelGreen * pixelAlpha;
blue += pixelBlue * pixelAlpha;
}
}

if (count == 0 || alpha == 0) return new Color();

red /= alpha;
green /= alpha;
blue /= alpha;
alpha /= count;

return new Color().set(red, green, blue, alpha, false);
}

public static Texture missing(ResourcePath<Texture> resourcePath) {
return new Texture(resourcePath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.bluecolored.bluemap.core.util;

import de.bluecolored.bluemap.core.util.math.Color;
import org.jetbrains.annotations.Nullable;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

public class BufferedImageUtil {

public static Color averageColor(BufferedImage image) {
Color average = new Color();
Color color = new Color();
float[] buffer = null;
int count = 0;
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
buffer = readPixel(image, x, y, color, buffer);

count++;
average.add(color.premultiplied());
}
}
average.div(count);
return average;
}

public static Color readPixel(BufferedImage image, int x, int y, @Nullable Color target) {
readPixel(image, x, y, target, null);
return target;
}

private static float[] readPixel(BufferedImage image, int x, int y, @Nullable Color target, float @Nullable [] buffer) {
if (target == null) target = new Color();

// workaround for java bug: 5051418
if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
buffer = readPixelDirect(image, x, y, target, buffer);
} else {
readPixelDefault(image, x, y, target);
}

return buffer;
}

private static void readPixelDefault(BufferedImage image, int x, int y, Color target) {
target.set(image.getRGB(x, y), image.getColorModel().isAlphaPremultiplied());
}

private static float[] readPixelDirect(RenderedImage image, int x, int y, Color target, float @Nullable [] buffer) {
buffer = image.getData().getPixel(x, y, buffer);

float a = buffer.length >= 4 ? buffer[3] / 255f : 1f;
float r = buffer[0] / 255f;
float g = buffer.length >= 3 ? buffer[1] / 255f : r;
float b = buffer.length >= 3 ? buffer[2] / 255f : r;

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

return buffer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"minecraft:lava_cauldron": "#ffffff",
"minecraft:grass_block": "@grass",
"minecraft:grass": "@grass",
"minecraft:short_grass": "@grass",
"minecraft:tall_grass": "@grass",
"minecraft:fern": "@grass",
"minecraft:large_fern": "@grass",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"minecraft:bubble_column": { "alwaysWaterlogged": true },

"minecraft:grass": { "randomOffset": true },
"minecraft:short_grass": { "randomOffset": true },
"minecraft:tall_grass": { "randomOffset": true },
"minecraft:fern": { "randomOffset": true },
"minecraft:dandelion": { "randomOffset": true },
Expand Down

0 comments on commit 5866cb5

Please sign in to comment.