implements PLN-0082

This commit is contained in:
2026-05-23 22:20:24 +01:00
parent ad2bfeb66f
commit 0be02622c4
8 changed files with 120 additions and 106 deletions
@@ -1,6 +1,5 @@
package p.packer.models;
public record PackerPaletteV1(
int[] originalArgb8888,
short[] convertedRgb565) {
}
int[] rgba8888) {
}
@@ -383,13 +383,10 @@ public class PackerAssetWalker {
if (!indexNode.isInt() || !paletteNode.isObject()) {
continue;
}
final int originalCount = paletteNode.path("originalArgb8888").isArray()
? paletteNode.path("originalArgb8888").size()
final int colorCount = paletteNode.path("rgba8888").isArray()
? paletteNode.path("rgba8888").size()
: 0;
final int convertedCount = paletteNode.path("convertedRgb565").isArray()
? paletteNode.path("convertedRgb565").size()
: 0;
palettes.add(new DeclaredPalette(indexNode.intValue(), Math.min(originalCount, convertedCount)));
palettes.add(new DeclaredPalette(indexNode.intValue(), colorCount));
}
palettes.sort(java.util.Comparator.comparingInt(DeclaredPalette::index));
return List.copyOf(palettes);
@@ -11,8 +11,7 @@ import java.nio.file.Path;
import java.util.*;
public class PackerGlyphBankWalker extends PackerAbstractBankWalker<PackerGlyphBankRequirements> {
private static final int MAX_COLORS_PER_PALETTE = 15;
private static final int COLOR_KEY_RGB = 0x00FF00FF;
private static final int MAX_COLORS_PER_PALETTE = 16;
private static final Set<String> SUPPORTED_MIME_TYPES = Set.of(
"image/png"
);
@@ -69,58 +68,37 @@ public class PackerGlyphBankWalker extends PackerAbstractBankWalker<PackerGlyphB
));
}
final Map<Integer, Integer> paletteIndexByRgb = new LinkedHashMap<>();
final Map<Integer, Integer> paletteIndexByRgba = new LinkedHashMap<>();
final byte[] paletteIndices = new byte[image.getWidth() * image.getHeight()];
boolean partialAlphaFound = false;
boolean exceededPaletteLimit = false;
int offset = 0;
for (var y = 0; y < image.getHeight(); y++) {
for (var x = 0; x < image.getWidth(); x++) {
final int argb = image.getRGB(x, y);
final int alpha = (argb >>> 24) & 0xFF;
final int rgb = argb & 0x00FFFFFF;
final int rgba = argbToRgba(argb);
if (alpha == 0 || rgb == COLOR_KEY_RGB) {
paletteIndices[offset++] = 0;
continue;
}
if (alpha < 0xFF) {
partialAlphaFound = true;
}
Integer paletteIndex = paletteIndexByRgb.get(rgb);
Integer paletteIndex = paletteIndexByRgba.get(rgba);
if (paletteIndex == null) {
if (paletteIndexByRgb.size() >= MAX_COLORS_PER_PALETTE) {
if (paletteIndexByRgba.size() >= MAX_COLORS_PER_PALETTE) {
exceededPaletteLimit = true;
paletteIndices[offset++] = 0;
continue;
}
paletteIndex = paletteIndexByRgb.size() + 1;
paletteIndexByRgb.put(rgb, paletteIndex);
paletteIndex = paletteIndexByRgba.size();
paletteIndexByRgba.put(rgba, paletteIndex);
}
paletteIndices[offset++] = paletteIndex.byteValue();
}
}
if (partialAlphaFound) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.WARNING,
PackerDiagnosticCategory.HYGIENE,
"Tile contains partial alpha; flattening to RGB and ignoring alpha channel",
fileProbe.path(),
false
));
}
if (exceededPaletteLimit) {
diagnostics.add(new PackerDiagnostic(
PackerDiagnosticSeverity.ERROR,
PackerDiagnosticCategory.STRUCTURAL,
"Tile image exceeds color limit for " + fileProbe.path().getFileName()
+ ": expected at most " + MAX_COLORS_PER_PALETTE + " colors for indices 1..15",
+ ": expected at most " + MAX_COLORS_PER_PALETTE + " RGBA8888 colors",
fileProbe.path(),
true
));
@@ -131,7 +109,7 @@ public class PackerGlyphBankWalker extends PackerAbstractBankWalker<PackerGlyphB
}
final var tile = new PackerTileIndexedV1(image.getWidth(), image.getHeight(), paletteIndices);
final var palette = buildPalette(paletteIndexByRgb);
final var palette = buildPalette(paletteIndexByRgba);
// Disabled for now: derived tile/palette JSON artifacts are not emitted during normal walks.
// If we want filesystem-side debug artifacts again, uncomment the call below.
// serializeProbeArtifacts(fileProbe, assetRoot, tile, palette, diagnostics);
@@ -150,7 +128,7 @@ public class PackerGlyphBankWalker extends PackerAbstractBankWalker<PackerGlyphB
final Object width = tile.get("width");
final Object height = tile.get("height");
final Object indices = tile.get("paletteIndices");
final Object colors = palette.get("originalArgb8888");
final Object colors = palette.get("rgba8888");
return width instanceof Number
&& height instanceof Number
&& indices instanceof List<?>
@@ -224,39 +202,27 @@ public class PackerGlyphBankWalker extends PackerAbstractBankWalker<PackerGlyphB
}
private Map<String, Object> toPalettePayload(final PackerPaletteV1 palette) {
final List<Integer> convertedRgb565 = new ArrayList<>(palette.convertedRgb565().length);
for (final short value : palette.convertedRgb565()) {
convertedRgb565.add(Short.toUnsignedInt(value));
}
return Map.of(
"originalArgb8888", palette.originalArgb8888(),
"convertedRgb565", convertedRgb565
"rgba8888", palette.rgba8888()
);
}
private PackerPaletteV1 buildPalette(final Map<Integer, Integer> paletteIndexByRgb) {
final int[] originalArgb8888 = new int[paletteIndexByRgb.size()];
final short[] convertedRgb565 = new short[paletteIndexByRgb.size()];
for (final var entry : paletteIndexByRgb.entrySet()) {
final int rgb = entry.getKey();
final int index = entry.getValue() - 1;
final int argb = 0xFF000000 | rgb;
originalArgb8888[index] = argb;
convertedRgb565[index] = convertToRgb565(argb);
private PackerPaletteV1 buildPalette(final Map<Integer, Integer> paletteIndexByRgba) {
final int[] rgba8888 = new int[paletteIndexByRgba.size()];
for (final var entry : paletteIndexByRgba.entrySet()) {
final int rgba = entry.getKey();
final int index = entry.getValue();
rgba8888[index] = rgba;
}
return new PackerPaletteV1(originalArgb8888, convertedRgb565);
return new PackerPaletteV1(rgba8888);
}
private short convertToRgb565(final int argb) {
final int red = (argb >> 16) & 0xFF;
final int green = (argb >> 8) & 0xFF;
private int argbToRgba(final int argb) {
final int alpha = (argb >>> 24) & 0xFF;
final int red = (argb >>> 16) & 0xFF;
final int green = (argb >>> 8) & 0xFF;
final int blue = argb & 0xFF;
final int r5 = (red >> 3) & 0x1F;
final int g6 = (green >> 2) & 0x3F;
final int b5 = (blue >> 3) & 0x1F;
return (short) ((r5 << 11) | (g6 << 5) | b5);
return (red << 24) | (green << 16) | (blue << 8) | alpha;
}
private java.awt.image.BufferedImage readImage(
@@ -1161,22 +1161,19 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
Object paletteMetadata,
String artifactFile,
Map<Integer, Integer> declaredPaletteIndexByRgb) {
final int[] originalArgb8888 = originalPaletteColors(paletteMetadata);
if (originalArgb8888.length == 0) {
final int[] rgba8888 = paletteColors(paletteMetadata);
if (rgba8888.length == 0) {
throw new IllegalStateException("Selected tile artifact is missing palette metadata required for palette remapping: " + artifactFile);
}
final byte[] remapped = Arrays.copyOf(tile.paletteIndices(), tile.paletteIndices().length);
for (int index = 0; index < remapped.length; index += 1) {
final int paletteIndex = Byte.toUnsignedInt(remapped[index]);
if (paletteIndex == 0) {
continue;
}
if (paletteIndex > originalArgb8888.length) {
if (paletteIndex >= rgba8888.length) {
throw new IllegalStateException("Tile palette index exceeds discovered palette size for artifact: " + artifactFile);
}
final int originalArgb = originalArgb8888[paletteIndex - 1];
final Integer materialIndex = declaredPaletteIndexByRgb.get(rgb24(originalArgb));
if (materialIndex == null || materialIndex < 1 || materialIndex > 15) {
final int rgba = rgba8888[paletteIndex];
final Integer materialIndex = declaredPaletteIndexByRgb.get(rgba);
if (materialIndex == null || materialIndex < 0 || materialIndex > 15) {
throw new IllegalStateException("Unable to resolve canonical palette index for artifact color in " + artifactFile);
}
remapped[index] = (byte) (materialIndex & 0x0F);
@@ -1189,29 +1186,29 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
if (!(palettesNode instanceof ArrayNode palettesArray) || palettesArray.isEmpty()) {
throw new IllegalStateException("Glyph packing requires at least one declared pipeline palette.");
}
final JsonNode paletteNode = palettesArray.get(0).path("palette").path("originalArgb8888");
final JsonNode paletteNode = palettesArray.get(0).path("palette").path("rgba8888");
if (!paletteNode.isArray() || paletteNode.isEmpty()) {
throw new IllegalStateException("Glyph packing requires originalArgb8888 entries in the declared pipeline palette.");
throw new IllegalStateException("Glyph packing requires rgba8888 entries in the declared pipeline palette.");
}
final LinkedHashMap<Integer, Integer> mapping = new LinkedHashMap<>();
for (int index = 0; index < paletteNode.size(); index += 1) {
final JsonNode colorNode = paletteNode.get(index);
if (!colorNode.canConvertToInt()) {
throw new IllegalStateException("Declared pipeline palette contains a non-integer originalArgb8888 entry.");
throw new IllegalStateException("Declared pipeline palette contains a non-integer rgba8888 entry.");
}
mapping.put(rgb24(colorNode.intValue()), index + 1);
mapping.put(colorNode.intValue(), index);
}
return Map.copyOf(mapping);
}
private int[] originalPaletteColors(Object paletteMetadata) {
private int[] paletteColors(Object paletteMetadata) {
if (paletteMetadata instanceof PackerPaletteV1 palette) {
return palette.originalArgb8888();
return palette.rgba8888();
}
if (!(paletteMetadata instanceof Map<?, ?> map)) {
return new int[0];
}
final Object colors = map.get("originalArgb8888");
final Object colors = map.get("rgba8888");
if (!(colors instanceof List<?> list)) {
return new int[0];
}
@@ -1226,10 +1223,6 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
return values;
}
private int rgb24(int argb) {
return argb & 0x00FFFFFF;
}
private PackedAsset packSceneBank(PackerRuntimeAsset runtimeAsset, PackerAssetDeclaration declaration) {
final ScenePackRequest request = runtimeAsset.walkProjection().buildCandidateFiles().stream()
.map(file -> file.metadata().get("scenePackRequest"))
@@ -109,17 +109,12 @@ public final class PackerReadMessageMapper {
"paletteIndices", List.copyOf(paletteIndices));
}
if (value instanceof PackerPaletteV1 palette) {
final List<Integer> originalArgb8888 = new java.util.ArrayList<>(palette.originalArgb8888().length);
for (int argb : palette.originalArgb8888()) {
originalArgb8888.add(argb);
}
final List<Integer> convertedRgb565 = new java.util.ArrayList<>(palette.convertedRgb565().length);
for (short rgb565 : palette.convertedRgb565()) {
convertedRgb565.add(Short.toUnsignedInt(rgb565));
final List<Integer> rgba8888 = new java.util.ArrayList<>(palette.rgba8888().length);
for (int rgba : palette.rgba8888()) {
rgba8888.add(rgba);
}
return Map.of(
"originalArgb8888", List.copyOf(originalArgb8888),
"convertedRgb565", List.copyOf(convertedRgb565));
"rgba8888", List.copyOf(rgba8888));
}
if (value instanceof Map<?, ?> nestedMap) {
final java.util.LinkedHashMap<String, Object> normalized = new java.util.LinkedHashMap<>();