29 lines
963 B
JavaScript
29 lines
963 B
JavaScript
import sharp from "sharp";
|
|
|
|
// Empty sky faces are valid when looking up. A surface map or the view straight
|
|
// down from a surface capture must contain terrain, even if other faces are sky.
|
|
export async function assertTerrainImage(bytes, label) {
|
|
const { data, info } = await sharp(bytes)
|
|
.resize(64, 64, { fit: "fill" })
|
|
.toColourspace("srgb")
|
|
.removeAlpha()
|
|
.raw()
|
|
.toBuffer({ resolveWithObject: true });
|
|
const sky = [0x95, 0xc4, 0xeb];
|
|
let terrainPixels = 0;
|
|
for (let i = 0; i < data.length; i += info.channels) {
|
|
if (
|
|
Math.abs(data[i] - sky[0]) > 12 ||
|
|
Math.abs(data[i + 1] - sky[1]) > 12 ||
|
|
Math.abs(data[i + 2] - sky[2]) > 12
|
|
)
|
|
terrainPixels++;
|
|
}
|
|
if (terrainPixels < 4) {
|
|
throw new Error(
|
|
`${label} contains only sky. Check Minecraft asset loading, capture coordinates and graphics errors; this render will not be submitted as a playable map.`,
|
|
);
|
|
}
|
|
return terrainPixels;
|
|
}
|